blob: 7093051b760f15d8ace0561fc518e3992884bdde [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));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000654static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000656static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000657static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000658static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000659static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000660static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000662static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000676static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000679static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000682static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static int get_env_len __ARGS((char_u **arg));
684static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000685static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000686static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
687#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
688#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
689 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000690static 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 +0000691static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000692static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000693static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
694static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static typval_T *alloc_tv __ARGS((void));
696static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void init_tv __ARGS((typval_T *varp));
698static long get_tv_number __ARGS((typval_T *varp));
699static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000700static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static char_u *get_tv_string __ARGS((typval_T *varp));
702static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000703static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000705static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
707static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
708static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000709static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
710static 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 +0000711static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
712static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000713static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000714static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000716static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
718static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
719static int eval_fname_script __ARGS((char_u *p));
720static int eval_fname_sid __ARGS((char_u *p));
721static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static ufunc_T *find_func __ARGS((char_u *name));
723static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000724static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000725#ifdef FEAT_PROFILE
726static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000727static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
728static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
729static int
730# ifdef __BORLANDC__
731 _RTLENTRYF
732# endif
733 prof_total_cmp __ARGS((const void *s1, const void *s2));
734static int
735# ifdef __BORLANDC__
736 _RTLENTRYF
737# endif
738 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000739#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000740static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000741static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000742static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static void func_free __ARGS((ufunc_T *fp));
744static void func_unref __ARGS((char_u *name));
745static void func_ref __ARGS((char_u *name));
746static 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));
747static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000748static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
749static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000750static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000751static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000752static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000754/* Character used as separated in autoload function/variable names. */
755#define AUTOLOAD_CHAR '#'
756
Bram Moolenaar33570922005-01-25 22:26:29 +0000757/*
758 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000759 */
760 void
761eval_init()
762{
Bram Moolenaar33570922005-01-25 22:26:29 +0000763 int i;
764 struct vimvar *p;
765
766 init_var_dict(&globvardict, &globvars_var);
767 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000768 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000769 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770
771 for (i = 0; i < VV_LEN; ++i)
772 {
773 p = &vimvars[i];
774 STRCPY(p->vv_di.di_key, p->vv_name);
775 if (p->vv_flags & VV_RO)
776 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
777 else if (p->vv_flags & VV_RO_SBX)
778 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
779 else
780 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000781
782 /* add to v: scope dict, unless the value is not always available */
783 if (p->vv_type != VAR_UNKNOWN)
784 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000785 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000786 /* add to compat scope dict */
787 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000788 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000789}
790
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000791#if defined(EXITFREE) || defined(PROTO)
792 void
793eval_clear()
794{
795 int i;
796 struct vimvar *p;
797
798 for (i = 0; i < VV_LEN; ++i)
799 {
800 p = &vimvars[i];
801 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000802 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000803 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000804 p->vv_di.di_tv.vval.v_string = NULL;
805 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000806 }
807 hash_clear(&vimvarht);
808 hash_clear(&compat_hashtab);
809
810 /* script-local variables */
811 for (i = 1; i <= ga_scripts.ga_len; ++i)
812 vars_clear(&SCRIPT_VARS(i));
813 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000814 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000815
816 /* global variables */
817 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000820 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000821 hash_clear(&func_hashtab);
822
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000823 /* autoloaded script names */
824 ga_clear_strings(&ga_loaded);
825
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000826 /* unreferenced lists and dicts */
827 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000828}
829#endif
830
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * Return the name of the executed function.
833 */
834 char_u *
835func_name(cookie)
836 void *cookie;
837{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000838 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840
841/*
842 * Return the address holding the next breakpoint line for a funccall cookie.
843 */
844 linenr_T *
845func_breakpoint(cookie)
846 void *cookie;
847{
Bram Moolenaar33570922005-01-25 22:26:29 +0000848 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849}
850
851/*
852 * Return the address holding the debug tick for a funccall cookie.
853 */
854 int *
855func_dbg_tick(cookie)
856 void *cookie;
857{
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860
861/*
862 * Return the nesting level for a funccall cookie.
863 */
864 int
865func_level(cookie)
866 void *cookie;
867{
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000872funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
874/*
875 * Return TRUE when a function was ended by a ":return" command.
876 */
877 int
878current_func_returned()
879{
880 return current_funccal->returned;
881}
882
883
884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * Set an internal variable to a string value. Creates the variable if it does
886 * not already exist.
887 */
888 void
889set_internal_string_var(name, value)
890 char_u *name;
891 char_u *value;
892{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000893 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000894 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
896 val = vim_strsave(value);
897 if (val != NULL)
898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000900 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000902 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000903 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905 }
906}
907
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000909static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910static char_u *redir_endp = NULL;
911static char_u *redir_varname = NULL;
912
913/*
914 * Start recording command output to a variable
915 * Returns OK if successfully completed the setup. FAIL otherwise.
916 */
917 int
918var_redir_start(name, append)
919 char_u *name;
920 int append; /* append to an existing variable */
921{
922 int save_emsg;
923 int err;
924 typval_T tv;
925
926 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000927 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000928 {
929 EMSG(_(e_invarg));
930 return FAIL;
931 }
932
933 redir_varname = vim_strsave(name);
934 if (redir_varname == NULL)
935 return FAIL;
936
937 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
938 if (redir_lval == NULL)
939 {
940 var_redir_stop();
941 return FAIL;
942 }
943
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000944 /* The output is stored in growarray "redir_ga" until redirection ends. */
945 ga_init2(&redir_ga, (int)sizeof(char), 500);
946
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000947 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000948 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
949 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000950 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
951 {
952 if (redir_endp != NULL && *redir_endp != NUL)
953 /* Trailing characters are present after the variable name */
954 EMSG(_(e_trailing));
955 else
956 EMSG(_(e_invarg));
957 var_redir_stop();
958 return FAIL;
959 }
960
961 /* check if we can write to the variable: set it to or append an empty
962 * string */
963 save_emsg = did_emsg;
964 did_emsg = FALSE;
965 tv.v_type = VAR_STRING;
966 tv.vval.v_string = (char_u *)"";
967 if (append)
968 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
969 else
970 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
971 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000972 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000973 if (err)
974 {
975 var_redir_stop();
976 return FAIL;
977 }
978 if (redir_lval->ll_newkey != NULL)
979 {
980 /* Dictionary item was created, don't do it again. */
981 vim_free(redir_lval->ll_newkey);
982 redir_lval->ll_newkey = NULL;
983 }
984
985 return OK;
986}
987
988/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000989 * Append "value[value_len]" to the variable set by var_redir_start().
990 * The actual appending is postponed until redirection ends, because the value
991 * appended may in fact be the string we write to, changing it may cause freed
992 * memory to be used:
993 * :redir => foo
994 * :let foo
995 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000996 */
997 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000998var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001000 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001002 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001003
1004 if (redir_lval == NULL)
1005 return;
1006
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001007 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001008 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001010 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001012 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001013 {
1014 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001015 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001016 }
1017 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019}
1020
1021/*
1022 * Stop redirecting command output to a variable.
1023 */
1024 void
1025var_redir_stop()
1026{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001027 typval_T tv;
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 if (redir_lval != NULL)
1030 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001031 /* Append the trailing NUL. */
1032 ga_append(&redir_ga, NUL);
1033
1034 /* Assign the text to the variable. */
1035 tv.v_type = VAR_STRING;
1036 tv.vval.v_string = redir_ga.ga_data;
1037 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1038 vim_free(tv.vval.v_string);
1039
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 clear_lval(redir_lval);
1041 vim_free(redir_lval);
1042 redir_lval = NULL;
1043 }
1044 vim_free(redir_varname);
1045 redir_varname = NULL;
1046}
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048# if defined(FEAT_MBYTE) || defined(PROTO)
1049 int
1050eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1051 char_u *enc_from;
1052 char_u *enc_to;
1053 char_u *fname_from;
1054 char_u *fname_to;
1055{
1056 int err = FALSE;
1057
1058 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1059 set_vim_var_string(VV_CC_TO, enc_to, -1);
1060 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1061 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1062 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1063 err = TRUE;
1064 set_vim_var_string(VV_CC_FROM, NULL, -1);
1065 set_vim_var_string(VV_CC_TO, NULL, -1);
1066 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1067 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1068
1069 if (err)
1070 return FAIL;
1071 return OK;
1072}
1073# endif
1074
1075# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1076 int
1077eval_printexpr(fname, args)
1078 char_u *fname;
1079 char_u *args;
1080{
1081 int err = FALSE;
1082
1083 set_vim_var_string(VV_FNAME_IN, fname, -1);
1084 set_vim_var_string(VV_CMDARG, args, -1);
1085 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1086 err = TRUE;
1087 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1088 set_vim_var_string(VV_CMDARG, NULL, -1);
1089
1090 if (err)
1091 {
1092 mch_remove(fname);
1093 return FAIL;
1094 }
1095 return OK;
1096}
1097# endif
1098
1099# if defined(FEAT_DIFF) || defined(PROTO)
1100 void
1101eval_diff(origfile, newfile, outfile)
1102 char_u *origfile;
1103 char_u *newfile;
1104 char_u *outfile;
1105{
1106 int err = FALSE;
1107
1108 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1109 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1110 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1111 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1112 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1113 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1114 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1115}
1116
1117 void
1118eval_patch(origfile, difffile, outfile)
1119 char_u *origfile;
1120 char_u *difffile;
1121 char_u *outfile;
1122{
1123 int err;
1124
1125 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1126 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1127 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1128 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1129 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1130 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1131 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1132}
1133# endif
1134
1135/*
1136 * Top level evaluation function, returning a boolean.
1137 * Sets "error" to TRUE if there was an error.
1138 * Return TRUE or FALSE.
1139 */
1140 int
1141eval_to_bool(arg, error, nextcmd, skip)
1142 char_u *arg;
1143 int *error;
1144 char_u **nextcmd;
1145 int skip; /* only parse, don't execute */
1146{
Bram Moolenaar33570922005-01-25 22:26:29 +00001147 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 int retval = FALSE;
1149
1150 if (skip)
1151 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 else
1155 {
1156 *error = FALSE;
1157 if (!skip)
1158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001159 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162 }
1163 if (skip)
1164 --emsg_skip;
1165
1166 return retval;
1167}
1168
1169/*
1170 * Top level evaluation function, returning a string. If "skip" is TRUE,
1171 * only parsing to "nextcmd" is done, without reporting errors. Return
1172 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1173 */
1174 char_u *
1175eval_to_string_skip(arg, nextcmd, skip)
1176 char_u *arg;
1177 char_u **nextcmd;
1178 int skip; /* only parse, don't execute */
1179{
Bram Moolenaar33570922005-01-25 22:26:29 +00001180 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 char_u *retval;
1182
1183 if (skip)
1184 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001185 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 retval = NULL;
1187 else
1188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001189 retval = vim_strsave(get_tv_string(&tv));
1190 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 if (skip)
1193 --emsg_skip;
1194
1195 return retval;
1196}
1197
1198/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001199 * Skip over an expression at "*pp".
1200 * Return FAIL for an error, OK otherwise.
1201 */
1202 int
1203skip_expr(pp)
1204 char_u **pp;
1205{
Bram Moolenaar33570922005-01-25 22:26:29 +00001206 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001207
1208 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001210}
1211
1212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 * Top level evaluation function, returning a string.
1214 * Return pointer to allocated memory, or NULL for failure.
1215 */
1216 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001217eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 char_u *arg;
1219 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001220 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221{
Bram Moolenaar33570922005-01-25 22:26:29 +00001222 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001224 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001226 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 retval = NULL;
1228 else
1229 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001230 if (dolist && tv.v_type == VAR_LIST)
1231 {
1232 ga_init2(&ga, (int)sizeof(char), 80);
1233 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1234 ga_append(&ga, NUL);
1235 retval = (char_u *)ga.ga_data;
1236 }
1237 else
1238 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001239 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241
1242 return retval;
1243}
1244
1245/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001246 * Call eval_to_string() without using current local variables and using
1247 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 */
1249 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001250eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 char_u *arg;
1252 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001253 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254{
1255 char_u *retval;
1256 void *save_funccalp;
1257
1258 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001259 if (use_sandbox)
1260 ++sandbox;
1261 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001262 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001263 if (use_sandbox)
1264 --sandbox;
1265 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 restore_funccal(save_funccalp);
1267 return retval;
1268}
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270/*
1271 * Top level evaluation function, returning a number.
1272 * Evaluates "expr" silently.
1273 * Returns -1 for an error.
1274 */
1275 int
1276eval_to_number(expr)
1277 char_u *expr;
1278{
Bram Moolenaar33570922005-01-25 22:26:29 +00001279 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001281 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283 ++emsg_off;
1284
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001285 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 retval = -1;
1287 else
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 --emsg_off;
1293
1294 return retval;
1295}
1296
Bram Moolenaara40058a2005-07-11 22:42:07 +00001297/*
1298 * Prepare v: variable "idx" to be used.
1299 * Save the current typeval in "save_tv".
1300 * When not used yet add the variable to the v: hashtable.
1301 */
1302 static void
1303prepare_vimvar(idx, save_tv)
1304 int idx;
1305 typval_T *save_tv;
1306{
1307 *save_tv = vimvars[idx].vv_tv;
1308 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1309 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1310}
1311
1312/*
1313 * Restore v: variable "idx" to typeval "save_tv".
1314 * When no longer defined, remove the variable from the v: hashtable.
1315 */
1316 static void
1317restore_vimvar(idx, save_tv)
1318 int idx;
1319 typval_T *save_tv;
1320{
1321 hashitem_T *hi;
1322
Bram Moolenaara40058a2005-07-11 22:42:07 +00001323 vimvars[idx].vv_tv = *save_tv;
1324 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1325 {
1326 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1327 if (HASHITEM_EMPTY(hi))
1328 EMSG2(_(e_intern2), "restore_vimvar()");
1329 else
1330 hash_remove(&vimvarht, hi);
1331 }
1332}
1333
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001334#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001335/*
1336 * Evaluate an expression to a list with suggestions.
1337 * For the "expr:" part of 'spellsuggest'.
1338 */
1339 list_T *
1340eval_spell_expr(badword, expr)
1341 char_u *badword;
1342 char_u *expr;
1343{
1344 typval_T save_val;
1345 typval_T rettv;
1346 list_T *list = NULL;
1347 char_u *p = skipwhite(expr);
1348
1349 /* Set "v:val" to the bad word. */
1350 prepare_vimvar(VV_VAL, &save_val);
1351 vimvars[VV_VAL].vv_type = VAR_STRING;
1352 vimvars[VV_VAL].vv_str = badword;
1353 if (p_verbose == 0)
1354 ++emsg_off;
1355
1356 if (eval1(&p, &rettv, TRUE) == OK)
1357 {
1358 if (rettv.v_type != VAR_LIST)
1359 clear_tv(&rettv);
1360 else
1361 list = rettv.vval.v_list;
1362 }
1363
1364 if (p_verbose == 0)
1365 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001366 restore_vimvar(VV_VAL, &save_val);
1367
1368 return list;
1369}
1370
1371/*
1372 * "list" is supposed to contain two items: a word and a number. Return the
1373 * word in "pp" and the number as the return value.
1374 * Return -1 if anything isn't right.
1375 * Used to get the good word and score from the eval_spell_expr() result.
1376 */
1377 int
1378get_spellword(list, pp)
1379 list_T *list;
1380 char_u **pp;
1381{
1382 listitem_T *li;
1383
1384 li = list->lv_first;
1385 if (li == NULL)
1386 return -1;
1387 *pp = get_tv_string(&li->li_tv);
1388
1389 li = li->li_next;
1390 if (li == NULL)
1391 return -1;
1392 return get_tv_number(&li->li_tv);
1393}
1394#endif
1395
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001396/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001397 * Top level evaluation function.
1398 * Returns an allocated typval_T with the result.
1399 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001400 */
1401 typval_T *
1402eval_expr(arg, nextcmd)
1403 char_u *arg;
1404 char_u **nextcmd;
1405{
1406 typval_T *tv;
1407
1408 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001409 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001410 {
1411 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001412 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001413 }
1414
1415 return tv;
1416}
1417
1418
Bram Moolenaar4f688582007-07-24 12:34:30 +00001419#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1420 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 static int
1427call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 char_u *func;
1429 int argc;
1430 char_u **argv;
1431 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
Bram Moolenaar33570922005-01-25 22:26:29 +00001434 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 long n;
1436 int len;
1437 int i;
1438 int doesrange;
1439 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001440 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001442 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 for (i = 0; i < argc; i++)
1447 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001448 /* Pass a NULL or empty argument as an empty string */
1449 if (argv[i] == NULL || *argv[i] == NUL)
1450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001451 argvars[i].v_type = VAR_STRING;
1452 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001453 continue;
1454 }
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 /* Recognize a number argument, the others must be strings. */
1457 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1458 if (len != 0 && len == (int)STRLEN(argv[i]))
1459 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001460 argvars[i].v_type = VAR_NUMBER;
1461 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 else
1464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001465 argvars[i].v_type = VAR_STRING;
1466 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468 }
1469
1470 if (safe)
1471 {
1472 save_funccalp = save_funccal();
1473 ++sandbox;
1474 }
1475
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001476 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1477 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (safe)
1481 {
1482 --sandbox;
1483 restore_funccal(save_funccalp);
1484 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485 vim_free(argvars);
1486
1487 if (ret == FAIL)
1488 clear_tv(rettv);
1489
1490 return ret;
1491}
1492
Bram Moolenaar4f688582007-07-24 12:34:30 +00001493# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001494/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001495 * Call vimL function "func" and return the result as a string.
1496 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497 * Uses argv[argc] for the function arguments.
1498 */
1499 void *
1500call_func_retstr(func, argc, argv, safe)
1501 char_u *func;
1502 int argc;
1503 char_u **argv;
1504 int safe; /* use the sandbox */
1505{
1506 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001507 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508
1509 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1510 return NULL;
1511
1512 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001513 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 return retval;
1515}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001516# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001517
Bram Moolenaar4f688582007-07-24 12:34:30 +00001518# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001519/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001520 * Call vimL function "func" and return the result as a number.
1521 * Returns -1 when calling the function fails.
1522 * Uses argv[argc] for the function arguments.
1523 */
1524 long
1525call_func_retnr(func, argc, argv, safe)
1526 char_u *func;
1527 int argc;
1528 char_u **argv;
1529 int safe; /* use the sandbox */
1530{
1531 typval_T rettv;
1532 long retval;
1533
1534 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1535 return -1;
1536
1537 retval = get_tv_number_chk(&rettv, NULL);
1538 clear_tv(&rettv);
1539 return retval;
1540}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001541# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001542
1543/*
1544 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001545 * Uses argv[argc] for the function arguments.
1546 */
1547 void *
1548call_func_retlist(func, argc, argv, safe)
1549 char_u *func;
1550 int argc;
1551 char_u **argv;
1552 int safe; /* use the sandbox */
1553{
1554 typval_T rettv;
1555
1556 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1557 return NULL;
1558
1559 if (rettv.v_type != VAR_LIST)
1560 {
1561 clear_tv(&rettv);
1562 return NULL;
1563 }
1564
1565 return rettv.vval.v_list;
1566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567#endif
1568
Bram Moolenaar4f688582007-07-24 12:34:30 +00001569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570/*
1571 * Save the current function call pointer, and set it to NULL.
1572 * Used when executing autocommands and for ":source".
1573 */
1574 void *
1575save_funccal()
1576{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001577 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 current_funccal = NULL;
1580 return (void *)fc;
1581}
1582
1583 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001584restore_funccal(vfc)
1585 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001587 funccall_T *fc = (funccall_T *)vfc;
1588
1589 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590}
1591
Bram Moolenaar05159a02005-02-26 23:04:13 +00001592#if defined(FEAT_PROFILE) || defined(PROTO)
1593/*
1594 * Prepare profiling for entering a child or something else that is not
1595 * counted for the script/function itself.
1596 * Should always be called in pair with prof_child_exit().
1597 */
1598 void
1599prof_child_enter(tm)
1600 proftime_T *tm; /* place to store waittime */
1601{
1602 funccall_T *fc = current_funccal;
1603
1604 if (fc != NULL && fc->func->uf_profiling)
1605 profile_start(&fc->prof_child);
1606 script_prof_save(tm);
1607}
1608
1609/*
1610 * Take care of time spent in a child.
1611 * Should always be called after prof_child_enter().
1612 */
1613 void
1614prof_child_exit(tm)
1615 proftime_T *tm; /* where waittime was stored */
1616{
1617 funccall_T *fc = current_funccal;
1618
1619 if (fc != NULL && fc->func->uf_profiling)
1620 {
1621 profile_end(&fc->prof_child);
1622 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1623 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1624 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1625 }
1626 script_prof_restore(tm);
1627}
1628#endif
1629
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#ifdef FEAT_FOLDING
1632/*
1633 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1634 * it in "*cp". Doesn't give error messages.
1635 */
1636 int
1637eval_foldexpr(arg, cp)
1638 char_u *arg;
1639 int *cp;
1640{
Bram Moolenaar33570922005-01-25 22:26:29 +00001641 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 int retval;
1643 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001644 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1645 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001648 if (use_sandbox)
1649 ++sandbox;
1650 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 retval = 0;
1654 else
1655 {
1656 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 if (tv.v_type == VAR_NUMBER)
1658 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001659 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 retval = 0;
1661 else
1662 {
1663 /* If the result is a string, check if there is a non-digit before
1664 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001665 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (!VIM_ISDIGIT(*s) && *s != '-')
1667 *cp = *s++;
1668 retval = atol((char *)s);
1669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
1672 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001673 if (use_sandbox)
1674 --sandbox;
1675 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677 return retval;
1678}
1679#endif
1680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 * ":let" list all variable values
1683 * ":let var1 var2" list variable values
1684 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 * ":let var += expr" assignment command.
1686 * ":let var -= expr" assignment command.
1687 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001688 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 */
1690 void
1691ex_let(eap)
1692 exarg_T *eap;
1693{
1694 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001695 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698 int var_count = 0;
1699 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001701 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001702 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaardb552d602006-03-23 22:59:57 +00001704 argend = skip_var_list(arg, &var_count, &semicolon);
1705 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001707 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1708 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001709 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001712 /*
1713 * ":let" without "=": list variables
1714 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 if (*arg == '[')
1716 EMSG(_(e_invarg));
1717 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001719 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001723 list_glob_vars(&first);
1724 list_buf_vars(&first);
1725 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001726#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001727 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001728#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001729 list_script_vars(&first);
1730 list_func_vars(&first);
1731 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 eap->nextcmd = check_nextcmd(arg);
1734 }
1735 else
1736 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 op[0] = '=';
1738 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001739 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 {
1741 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1742 op[0] = expr[-1]; /* +=, -= or .= */
1743 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 if (eap->skip)
1747 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (eap->skip)
1750 {
1751 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 --emsg_skip;
1754 }
1755 else if (i != FAIL)
1756 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 }
1762}
1763
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764/*
1765 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1766 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 * When "nextchars" is not NULL it points to a string with characters that
1768 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1769 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 * Returns OK or FAIL;
1771 */
1772 static int
1773ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1774 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001775 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776 int copy; /* copy values from "tv", don't move */
1777 int semicolon; /* from skip_var_list() */
1778 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780{
1781 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 listitem_T *item;
1785 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786
1787 if (*arg != '[')
1788 {
1789 /*
1790 * ":let var = expr" or ":for var in list"
1791 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001792 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 return FAIL;
1794 return OK;
1795 }
1796
1797 /*
1798 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1799 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001800 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 {
1802 EMSG(_(e_listreq));
1803 return FAIL;
1804 }
1805
1806 i = list_len(l);
1807 if (semicolon == 0 && var_count < i)
1808 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001809 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 return FAIL;
1811 }
1812 if (var_count - semicolon > i)
1813 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001814 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 return FAIL;
1816 }
1817
1818 item = l->lv_first;
1819 while (*arg != ']')
1820 {
1821 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 item = item->li_next;
1824 if (arg == NULL)
1825 return FAIL;
1826
1827 arg = skipwhite(arg);
1828 if (*arg == ';')
1829 {
1830 /* Put the rest of the list (may be empty) in the var after ';'.
1831 * Create a new list for this. */
1832 l = list_alloc();
1833 if (l == NULL)
1834 return FAIL;
1835 while (item != NULL)
1836 {
1837 list_append_tv(l, &item->li_tv);
1838 item = item->li_next;
1839 }
1840
1841 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001842 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 ltv.vval.v_list = l;
1844 l->lv_refcount = 1;
1845
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1847 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 clear_tv(&ltv);
1849 if (arg == NULL)
1850 return FAIL;
1851 break;
1852 }
1853 else if (*arg != ',' && *arg != ']')
1854 {
1855 EMSG2(_(e_intern2), "ex_let_vars()");
1856 return FAIL;
1857 }
1858 }
1859
1860 return OK;
1861}
1862
1863/*
1864 * Skip over assignable variable "var" or list of variables "[var, var]".
1865 * Used for ":let varvar = expr" and ":for varvar in expr".
1866 * For "[var, var]" increment "*var_count" for each variable.
1867 * for "[var, var; var]" set "semicolon".
1868 * Return NULL for an error.
1869 */
1870 static char_u *
1871skip_var_list(arg, var_count, semicolon)
1872 char_u *arg;
1873 int *var_count;
1874 int *semicolon;
1875{
1876 char_u *p, *s;
1877
1878 if (*arg == '[')
1879 {
1880 /* "[var, var]": find the matching ']'. */
1881 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001882 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 {
1884 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1885 s = skip_var_one(p);
1886 if (s == p)
1887 {
1888 EMSG2(_(e_invarg2), p);
1889 return NULL;
1890 }
1891 ++*var_count;
1892
1893 p = skipwhite(s);
1894 if (*p == ']')
1895 break;
1896 else if (*p == ';')
1897 {
1898 if (*semicolon == 1)
1899 {
1900 EMSG(_("Double ; in list of variables"));
1901 return NULL;
1902 }
1903 *semicolon = 1;
1904 }
1905 else if (*p != ',')
1906 {
1907 EMSG2(_(e_invarg2), p);
1908 return NULL;
1909 }
1910 }
1911 return p + 1;
1912 }
1913 else
1914 return skip_var_one(arg);
1915}
1916
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001917/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001918 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001919 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001920 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 static char_u *
1922skip_var_one(arg)
1923 char_u *arg;
1924{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001925 if (*arg == '@' && arg[1] != NUL)
1926 return arg + 2;
1927 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1928 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929}
1930
Bram Moolenaara7043832005-01-21 11:56:39 +00001931/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 * List variables for hashtab "ht" with prefix "prefix".
1933 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001936list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001938 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001940 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001941{
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 hashitem_T *hi;
1943 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001944 int todo;
1945
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001946 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001947 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1948 {
1949 if (!HASHITEM_EMPTY(hi))
1950 {
1951 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001952 di = HI2DI(hi);
1953 if (empty || di->di_tv.v_type != VAR_STRING
1954 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001955 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001956 }
1957 }
1958}
1959
1960/*
1961 * List global variables.
1962 */
1963 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001964list_glob_vars(first)
1965 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001966{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001967 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968}
1969
1970/*
1971 * List buffer variables.
1972 */
1973 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001974list_buf_vars(first)
1975 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001976{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001977 char_u numbuf[NUMBUFLEN];
1978
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001979 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1980 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001981
1982 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001983 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1984 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001985}
1986
1987/*
1988 * List window variables.
1989 */
1990 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001991list_win_vars(first)
1992 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001993{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001994 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1995 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001996}
1997
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001998#ifdef FEAT_WINDOWS
1999/*
2000 * List tab page variables.
2001 */
2002 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002003list_tab_vars(first)
2004 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002005{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002006 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2007 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002008}
2009#endif
2010
Bram Moolenaara7043832005-01-21 11:56:39 +00002011/*
2012 * List Vim variables.
2013 */
2014 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002015list_vim_vars(first)
2016 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002018 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019}
2020
2021/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002022 * List script-local variables, if there is a script.
2023 */
2024 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002025list_script_vars(first)
2026 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002027{
2028 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002029 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2030 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002031}
2032
2033/*
2034 * List function variables, if there is a function.
2035 */
2036 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002037list_func_vars(first)
2038 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002039{
2040 if (current_funccal != NULL)
2041 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002042 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002043}
2044
2045/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 * List variables in "arg".
2047 */
2048 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002049list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 exarg_T *eap;
2051 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002052 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002053{
2054 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 char_u *name_start;
2058 char_u *arg_subsc;
2059 char_u *tofree;
2060 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061
2062 while (!ends_excmd(*arg) && !got_int)
2063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002064 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002066 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002067 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2068 {
2069 emsg_severe = TRUE;
2070 EMSG(_(e_trailing));
2071 break;
2072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 /* get_name_len() takes care of expanding curly braces */
2077 name_start = name = arg;
2078 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2079 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002081 /* This is mainly to keep test 49 working: when expanding
2082 * curly braces fails overrule the exception error message. */
2083 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 emsg_severe = TRUE;
2086 EMSG2(_(e_invarg2), arg);
2087 break;
2088 }
2089 error = TRUE;
2090 }
2091 else
2092 {
2093 if (tofree != NULL)
2094 name = tofree;
2095 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 else
2098 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 /* handle d.key, l[idx], f(expr) */
2100 arg_subsc = arg;
2101 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002105 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002108 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 case 'g': list_glob_vars(first); break;
2110 case 'b': list_buf_vars(first); break;
2111 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002112#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002114#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 case 'v': list_vim_vars(first); break;
2116 case 's': list_script_vars(first); break;
2117 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 default:
2119 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 {
2124 char_u numbuf[NUMBUFLEN];
2125 char_u *tf;
2126 int c;
2127 char_u *s;
2128
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002129 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130 c = *arg;
2131 *arg = NUL;
2132 list_one_var_a((char_u *)"",
2133 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 tv.v_type,
2135 s == NULL ? (char_u *)"" : s,
2136 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002137 *arg = c;
2138 vim_free(tf);
2139 }
2140 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 }
2143 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144
2145 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002147
2148 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 }
2150
2151 return arg;
2152}
2153
2154/*
2155 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2156 * Returns a pointer to the char just after the var name.
2157 * Returns NULL if there is an error.
2158 */
2159 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002160ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002162 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002165 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166{
2167 int c1;
2168 char_u *name;
2169 char_u *p;
2170 char_u *arg_end = NULL;
2171 int len;
2172 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174
2175 /*
2176 * ":let $VAR = expr": Set environment variable.
2177 */
2178 if (*arg == '$')
2179 {
2180 /* Find the end of the name. */
2181 ++arg;
2182 name = arg;
2183 len = get_env_len(&arg);
2184 if (len == 0)
2185 EMSG2(_(e_invarg2), name - 1);
2186 else
2187 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 if (op != NULL && (*op == '+' || *op == '-'))
2189 EMSG2(_(e_letwrong), op);
2190 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002191 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 EMSG(_(e_letunexp));
2193 else
2194 {
2195 c1 = name[len];
2196 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002197 p = get_tv_string_chk(tv);
2198 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002199 {
2200 int mustfree = FALSE;
2201 char_u *s = vim_getenv(name, &mustfree);
2202
2203 if (s != NULL)
2204 {
2205 p = tofree = concat_str(s, p);
2206 if (mustfree)
2207 vim_free(s);
2208 }
2209 }
2210 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002212 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002213 if (STRICMP(name, "HOME") == 0)
2214 init_homedir();
2215 else if (didset_vim && STRICMP(name, "VIM") == 0)
2216 didset_vim = FALSE;
2217 else if (didset_vimruntime
2218 && STRICMP(name, "VIMRUNTIME") == 0)
2219 didset_vimruntime = FALSE;
2220 arg_end = arg;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002223 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 }
2225 }
2226 }
2227
2228 /*
2229 * ":let &option = expr": Set option value.
2230 * ":let &l:option = expr": Set local option value.
2231 * ":let &g:option = expr": Set global option value.
2232 */
2233 else if (*arg == '&')
2234 {
2235 /* Find the end of the name. */
2236 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002237 if (p == NULL || (endchars != NULL
2238 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 EMSG(_(e_letunexp));
2240 else
2241 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242 long n;
2243 int opt_type;
2244 long numval;
2245 char_u *stringval = NULL;
2246 char_u *s;
2247
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 c1 = *p;
2249 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002250
2251 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002252 s = get_tv_string_chk(tv); /* != NULL if number or string */
2253 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 {
2255 opt_type = get_option_value(arg, &numval,
2256 &stringval, opt_flags);
2257 if ((opt_type == 1 && *op == '.')
2258 || (opt_type == 0 && *op != '.'))
2259 EMSG2(_(e_letwrong), op);
2260 else
2261 {
2262 if (opt_type == 1) /* number */
2263 {
2264 if (*op == '+')
2265 n = numval + n;
2266 else
2267 n = numval - n;
2268 }
2269 else if (opt_type == 0 && stringval != NULL) /* string */
2270 {
2271 s = concat_str(stringval, s);
2272 vim_free(stringval);
2273 stringval = s;
2274 }
2275 }
2276 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 if (s != NULL)
2278 {
2279 set_option_value(arg, n, s, opt_flags);
2280 arg_end = p;
2281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002283 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
2286
2287 /*
2288 * ":let @r = expr": Set register contents.
2289 */
2290 else if (*arg == '@')
2291 {
2292 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 if (op != NULL && (*op == '+' || *op == '-'))
2294 EMSG2(_(e_letwrong), op);
2295 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002296 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 EMSG(_(e_letunexp));
2298 else
2299 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002300 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 char_u *s;
2302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002303 p = get_tv_string_chk(tv);
2304 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002306 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 if (s != NULL)
2308 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002309 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 vim_free(s);
2311 }
2312 }
2313 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 arg_end = arg + 1;
2317 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002318 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
2320 }
2321
2322 /*
2323 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002326 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002328 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002330 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2334 EMSG(_(e_letunexp));
2335 else
2336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 arg_end = p;
2339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
2343
2344 else
2345 EMSG2(_(e_invarg2), arg);
2346
2347 return arg_end;
2348}
2349
2350/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2352 */
2353 static int
2354check_changedtick(arg)
2355 char_u *arg;
2356{
2357 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2358 {
2359 EMSG2(_(e_readonlyvar), arg);
2360 return TRUE;
2361 }
2362 return FALSE;
2363}
2364
2365/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 * Get an lval: variable, Dict item or List item that can be assigned a value
2367 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2368 * "name.key", "name.key[expr]" etc.
2369 * Indexing only works if "name" is an existing List or Dictionary.
2370 * "name" points to the start of the name.
2371 * If "rettv" is not NULL it points to the value to be assigned.
2372 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2373 * wrong; must end in space or cmd separator.
2374 *
2375 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002376 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 */
2379 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002380get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002382 typval_T *rettv;
2383 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 int unlet;
2385 int skip;
2386 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002387 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 char_u *expr_start, *expr_end;
2391 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002392 dictitem_T *v;
2393 typval_T var1;
2394 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002396 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002399 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002402 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403
2404 if (skip)
2405 {
2406 /* When skipping just find the end of the name. */
2407 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002408 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 }
2410
2411 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002412 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (expr_start != NULL)
2414 {
2415 /* Don't expand the name when we already know there is an error. */
2416 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2417 && *p != '[' && *p != '.')
2418 {
2419 EMSG(_(e_trailing));
2420 return NULL;
2421 }
2422
2423 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2424 if (lp->ll_exp_name == NULL)
2425 {
2426 /* Report an invalid expression in braces, unless the
2427 * expression evaluation has been cancelled due to an
2428 * aborting error, an interrupt, or an exception. */
2429 if (!aborting() && !quiet)
2430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002431 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 EMSG2(_(e_invarg2), name);
2433 return NULL;
2434 }
2435 }
2436 lp->ll_name = lp->ll_exp_name;
2437 }
2438 else
2439 lp->ll_name = name;
2440
2441 /* Without [idx] or .key we are done. */
2442 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2443 return p;
2444
2445 cc = *p;
2446 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002447 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (v == NULL && !quiet)
2449 EMSG2(_(e_undefvar), lp->ll_name);
2450 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 if (v == NULL)
2452 return NULL;
2453
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 /*
2455 * Loop until no more [idx] or .key is following.
2456 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002457 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2461 && !(lp->ll_tv->v_type == VAR_DICT
2462 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (!quiet)
2465 EMSG(_("E689: Can only index a List or Dictionary"));
2466 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (!quiet)
2471 EMSG(_("E708: [:] must come last"));
2472 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002474
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 len = -1;
2476 if (*p == '.')
2477 {
2478 key = p + 1;
2479 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2480 ;
2481 if (len == 0)
2482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (!quiet)
2484 EMSG(_(e_emptykey));
2485 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 }
2487 p = key + len;
2488 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002489 else
2490 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002492 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 if (*p == ':')
2494 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002495 else
2496 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 empty1 = FALSE;
2498 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 if (get_tv_string_chk(&var1) == NULL)
2501 {
2502 /* not a number or string */
2503 clear_tv(&var1);
2504 return NULL;
2505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 }
2507
2508 /* Optionally get the second index [ :expr]. */
2509 if (*p == ':')
2510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002515 if (!empty1)
2516 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (rettv != NULL && (rettv->v_type != VAR_LIST
2520 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (!quiet)
2523 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 if (!empty1)
2525 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 }
2528 p = skipwhite(p + 1);
2529 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 else
2532 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2535 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 if (!empty1)
2537 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002540 if (get_tv_string_chk(&var2) == NULL)
2541 {
2542 /* not a number or string */
2543 if (!empty1)
2544 clear_tv(&var1);
2545 clear_tv(&var2);
2546 return NULL;
2547 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (!quiet)
2557 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 if (!empty1)
2559 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 }
2564
2565 /* Skip to past ']'. */
2566 ++p;
2567 }
2568
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 {
2571 if (len == -1)
2572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002574 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 if (*key == NUL)
2576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (!quiet)
2578 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 }
2582 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583 lp->ll_list = NULL;
2584 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002588 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002592 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 if (len == -1)
2594 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 }
2597 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 if (len == -1)
2602 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 p = NULL;
2605 break;
2606 }
2607 if (len == -1)
2608 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 }
2611 else
2612 {
2613 /*
2614 * Get the number and item for the only or first index of the List.
2615 */
2616 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 else
2619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002620 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 clear_tv(&var1);
2622 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002623 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 lp->ll_list = lp->ll_tv->vval.v_list;
2625 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2626 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002628 if (lp->ll_n1 < 0)
2629 {
2630 lp->ll_n1 = 0;
2631 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2632 }
2633 }
2634 if (lp->ll_li == NULL)
2635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 }
2640
2641 /*
2642 * May need to find the item or absolute index for the second
2643 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 * When no index given: "lp->ll_empty2" is TRUE.
2645 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002649 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 }
2658
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2660 if (lp->ll_n1 < 0)
2661 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2662 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 }
2665
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 }
2669
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 return p;
2671}
2672
2673/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 */
2676 static void
2677clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002678 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679{
2680 vim_free(lp->ll_exp_name);
2681 vim_free(lp->ll_newkey);
2682}
2683
2684/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002685 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002687 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 */
2689 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002691 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002693 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002695 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696{
2697 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002698 listitem_T *ri;
2699 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700
2701 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 cc = *endp;
2706 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002707 if (op != NULL && *op != '=')
2708 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002709 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002711 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002712 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002713 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 {
2715 if (tv_op(&tv, rettv, op) == OK)
2716 set_var(lp->ll_name, &tv, FALSE);
2717 clear_tv(&tv);
2718 }
2719 }
2720 else
2721 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002723 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002725 else if (tv_check_lock(lp->ll_newkey == NULL
2726 ? lp->ll_tv->v_lock
2727 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2728 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 else if (lp->ll_range)
2730 {
2731 /*
2732 * Assign the List values to the list items.
2733 */
2734 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 if (op != NULL && *op != '=')
2737 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2738 else
2739 {
2740 clear_tv(&lp->ll_li->li_tv);
2741 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2742 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 ri = ri->li_next;
2744 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2745 break;
2746 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002749 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 ri = NULL;
2752 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_li = lp->ll_li->li_next;
2756 ++lp->ll_n1;
2757 }
2758 if (ri != NULL)
2759 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 else if (lp->ll_empty2
2761 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 : lp->ll_n1 != lp->ll_n2)
2763 EMSG(_("E711: List value has not enough items"));
2764 }
2765 else
2766 {
2767 /*
2768 * Assign to a List or Dictionary item.
2769 */
2770 if (lp->ll_newkey != NULL)
2771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 if (op != NULL && *op != '=')
2773 {
2774 EMSG2(_(e_letwrong), op);
2775 return;
2776 }
2777
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002779 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (di == NULL)
2781 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002782 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2783 {
2784 vim_free(di);
2785 return;
2786 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002788 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002789 else if (op != NULL && *op != '=')
2790 {
2791 tv_op(lp->ll_tv, rettv, op);
2792 return;
2793 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002794 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 /*
2798 * Assign the value to the variable or list item.
2799 */
2800 if (copy)
2801 copy_tv(rettv, lp->ll_tv);
2802 else
2803 {
2804 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002805 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807 }
2808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002809}
2810
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002811/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2813 * Returns OK or FAIL.
2814 */
2815 static int
2816tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002817 typval_T *tv1;
2818 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819 char_u *op;
2820{
2821 long n;
2822 char_u numbuf[NUMBUFLEN];
2823 char_u *s;
2824
2825 /* Can't do anything with a Funcref or a Dict on the right. */
2826 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2827 {
2828 switch (tv1->v_type)
2829 {
2830 case VAR_DICT:
2831 case VAR_FUNC:
2832 break;
2833
2834 case VAR_LIST:
2835 if (*op != '+' || tv2->v_type != VAR_LIST)
2836 break;
2837 /* List += List */
2838 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2839 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2840 return OK;
2841
2842 case VAR_NUMBER:
2843 case VAR_STRING:
2844 if (tv2->v_type == VAR_LIST)
2845 break;
2846 if (*op == '+' || *op == '-')
2847 {
2848 /* nr += nr or nr -= nr*/
2849 n = get_tv_number(tv1);
2850 if (*op == '+')
2851 n += get_tv_number(tv2);
2852 else
2853 n -= get_tv_number(tv2);
2854 clear_tv(tv1);
2855 tv1->v_type = VAR_NUMBER;
2856 tv1->vval.v_number = n;
2857 }
2858 else
2859 {
2860 /* str .= str */
2861 s = get_tv_string(tv1);
2862 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2863 clear_tv(tv1);
2864 tv1->v_type = VAR_STRING;
2865 tv1->vval.v_string = s;
2866 }
2867 return OK;
2868 }
2869 }
2870
2871 EMSG2(_(e_letwrong), op);
2872 return FAIL;
2873}
2874
2875/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876 * Add a watcher to a list.
2877 */
2878 static void
2879list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 list_T *l;
2881 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882{
2883 lw->lw_next = l->lv_watch;
2884 l->lv_watch = lw;
2885}
2886
2887/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002888 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 * No warning when it isn't found...
2890 */
2891 static void
2892list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 list_T *l;
2894 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895{
Bram Moolenaar33570922005-01-25 22:26:29 +00002896 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002897
2898 lwp = &l->lv_watch;
2899 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2900 {
2901 if (lw == lwrem)
2902 {
2903 *lwp = lw->lw_next;
2904 break;
2905 }
2906 lwp = &lw->lw_next;
2907 }
2908}
2909
2910/*
2911 * Just before removing an item from a list: advance watchers to the next
2912 * item.
2913 */
2914 static void
2915list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 list_T *l;
2917 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918{
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002920
2921 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2922 if (lw->lw_item == item)
2923 lw->lw_item = item->li_next;
2924}
2925
2926/*
2927 * Evaluate the expression used in a ":for var in expr" command.
2928 * "arg" points to "var".
2929 * Set "*errp" to TRUE for an error, FALSE otherwise;
2930 * Return a pointer that holds the info. Null when there is an error.
2931 */
2932 void *
2933eval_for_line(arg, errp, nextcmdp, skip)
2934 char_u *arg;
2935 int *errp;
2936 char_u **nextcmdp;
2937 int skip;
2938{
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002940 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 typval_T tv;
2942 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943
2944 *errp = TRUE; /* default: there is an error */
2945
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002947 if (fi == NULL)
2948 return NULL;
2949
2950 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2951 if (expr == NULL)
2952 return fi;
2953
2954 expr = skipwhite(expr);
2955 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2956 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002957 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002958 return fi;
2959 }
2960
2961 if (skip)
2962 ++emsg_skip;
2963 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2964 {
2965 *errp = FALSE;
2966 if (!skip)
2967 {
2968 l = tv.vval.v_list;
2969 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002970 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002971 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002972 clear_tv(&tv);
2973 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002974 else
2975 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002976 /* No need to increment the refcount, it's already set for the
2977 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002978 fi->fi_list = l;
2979 list_add_watch(l, &fi->fi_lw);
2980 fi->fi_lw.lw_item = l->lv_first;
2981 }
2982 }
2983 }
2984 if (skip)
2985 --emsg_skip;
2986
2987 return fi;
2988}
2989
2990/*
2991 * Use the first item in a ":for" list. Advance to the next.
2992 * Assign the values to the variable (list). "arg" points to the first one.
2993 * Return TRUE when a valid item was found, FALSE when at end of list or
2994 * something wrong.
2995 */
2996 int
2997next_for_item(fi_void, arg)
2998 void *fi_void;
2999 char_u *arg;
3000{
Bram Moolenaar33570922005-01-25 22:26:29 +00003001 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004
3005 item = fi->fi_lw.lw_item;
3006 if (item == NULL)
3007 result = FALSE;
3008 else
3009 {
3010 fi->fi_lw.lw_item = item->li_next;
3011 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3012 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3013 }
3014 return result;
3015}
3016
3017/*
3018 * Free the structure used to store info used by ":for".
3019 */
3020 void
3021free_for_info(fi_void)
3022 void *fi_void;
3023{
Bram Moolenaar33570922005-01-25 22:26:29 +00003024 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003025
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003026 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003027 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003028 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003029 list_unref(fi->fi_list);
3030 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003031 vim_free(fi);
3032}
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3035
3036 void
3037set_context_for_expression(xp, arg, cmdidx)
3038 expand_T *xp;
3039 char_u *arg;
3040 cmdidx_T cmdidx;
3041{
3042 int got_eq = FALSE;
3043 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003046 if (cmdidx == CMD_let)
3047 {
3048 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003049 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003050 {
3051 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003052 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053 {
3054 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003055 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056 if (vim_iswhite(*p))
3057 break;
3058 }
3059 return;
3060 }
3061 }
3062 else
3063 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3064 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 while ((xp->xp_pattern = vim_strpbrk(arg,
3066 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3067 {
3068 c = *xp->xp_pattern;
3069 if (c == '&')
3070 {
3071 c = xp->xp_pattern[1];
3072 if (c == '&')
3073 {
3074 ++xp->xp_pattern;
3075 xp->xp_context = cmdidx != CMD_let || got_eq
3076 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3077 }
3078 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003081 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3082 xp->xp_pattern += 2;
3083
3084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
3086 else if (c == '$')
3087 {
3088 /* environment variable */
3089 xp->xp_context = EXPAND_ENV_VARS;
3090 }
3091 else if (c == '=')
3092 {
3093 got_eq = TRUE;
3094 xp->xp_context = EXPAND_EXPRESSION;
3095 }
3096 else if (c == '<'
3097 && xp->xp_context == EXPAND_FUNCTIONS
3098 && vim_strchr(xp->xp_pattern, '(') == NULL)
3099 {
3100 /* Function name can start with "<SNR>" */
3101 break;
3102 }
3103 else if (cmdidx != CMD_let || got_eq)
3104 {
3105 if (c == '"') /* string */
3106 {
3107 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3108 if (c == '\\' && xp->xp_pattern[1] != NUL)
3109 ++xp->xp_pattern;
3110 xp->xp_context = EXPAND_NOTHING;
3111 }
3112 else if (c == '\'') /* literal string */
3113 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003114 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3116 /* skip */ ;
3117 xp->xp_context = EXPAND_NOTHING;
3118 }
3119 else if (c == '|')
3120 {
3121 if (xp->xp_pattern[1] == '|')
3122 {
3123 ++xp->xp_pattern;
3124 xp->xp_context = EXPAND_EXPRESSION;
3125 }
3126 else
3127 xp->xp_context = EXPAND_COMMANDS;
3128 }
3129 else
3130 xp->xp_context = EXPAND_EXPRESSION;
3131 }
3132 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133 /* Doesn't look like something valid, expand as an expression
3134 * anyway. */
3135 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 arg = xp->xp_pattern;
3137 if (*arg != NUL)
3138 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3139 /* skip */ ;
3140 }
3141 xp->xp_pattern = arg;
3142}
3143
3144#endif /* FEAT_CMDL_COMPL */
3145
3146/*
3147 * ":1,25call func(arg1, arg2)" function call.
3148 */
3149 void
3150ex_call(eap)
3151 exarg_T *eap;
3152{
3153 char_u *arg = eap->arg;
3154 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 linenr_T lnum;
3160 int doesrange;
3161 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003162 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003164 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003165 if (fudi.fd_newkey != NULL)
3166 {
3167 /* Still need to give an error message for missing key. */
3168 EMSG2(_(e_dictkey), fudi.fd_newkey);
3169 vim_free(fudi.fd_newkey);
3170 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003171 if (tofree == NULL)
3172 return;
3173
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003174 /* Increase refcount on dictionary, it could get deleted when evaluating
3175 * the arguments. */
3176 if (fudi.fd_dict != NULL)
3177 ++fudi.fd_dict->dv_refcount;
3178
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003180 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
Bram Moolenaar532c7802005-01-27 14:44:31 +00003183 /* Skip white space to allow ":call func ()". Not good, but required for
3184 * backward compatibility. */
3185 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 if (*startarg != '(')
3189 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003190 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 goto end;
3192 }
3193
3194 /*
3195 * When skipping, evaluate the function once, to find the end of the
3196 * arguments.
3197 * When the function takes a range, this is discovered after the first
3198 * call, and the loop is broken.
3199 */
3200 if (eap->skip)
3201 {
3202 ++emsg_skip;
3203 lnum = eap->line2; /* do it once, also with an invalid range */
3204 }
3205 else
3206 lnum = eap->line1;
3207 for ( ; lnum <= eap->line2; ++lnum)
3208 {
3209 if (!eap->skip && eap->addr_count > 0)
3210 {
3211 curwin->w_cursor.lnum = lnum;
3212 curwin->w_cursor.col = 0;
3213 }
3214 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003215 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003216 eap->line1, eap->line2, &doesrange,
3217 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 failed = TRUE;
3220 break;
3221 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003222
3223 /* Handle a function returning a Funcref, Dictionary or List. */
3224 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3225 {
3226 failed = TRUE;
3227 break;
3228 }
3229
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003230 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (doesrange || eap->skip)
3232 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003235 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003236 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003237 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (aborting())
3239 break;
3240 }
3241 if (eap->skip)
3242 --emsg_skip;
3243
3244 if (!failed)
3245 {
3246 /* Check for trailing illegal characters and a following command. */
3247 if (!ends_excmd(*arg))
3248 {
3249 emsg_severe = TRUE;
3250 EMSG(_(e_trailing));
3251 }
3252 else
3253 eap->nextcmd = check_nextcmd(arg);
3254 }
3255
3256end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003257 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003258 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259}
3260
3261/*
3262 * ":unlet[!] var1 ... " command.
3263 */
3264 void
3265ex_unlet(eap)
3266 exarg_T *eap;
3267{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 ex_unletlock(eap, eap->arg, 0);
3269}
3270
3271/*
3272 * ":lockvar" and ":unlockvar" commands
3273 */
3274 void
3275ex_lockvar(eap)
3276 exarg_T *eap;
3277{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003279 int deep = 2;
3280
3281 if (eap->forceit)
3282 deep = -1;
3283 else if (vim_isdigit(*arg))
3284 {
3285 deep = getdigits(&arg);
3286 arg = skipwhite(arg);
3287 }
3288
3289 ex_unletlock(eap, arg, deep);
3290}
3291
3292/*
3293 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3294 */
3295 static void
3296ex_unletlock(eap, argstart, deep)
3297 exarg_T *eap;
3298 char_u *argstart;
3299 int deep;
3300{
3301 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003304 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
3306 do
3307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003308 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003309 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3310 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 if (lv.ll_name == NULL)
3312 error = TRUE; /* error but continue parsing */
3313 if (name_end == NULL || (!vim_iswhite(*name_end)
3314 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003316 if (name_end != NULL)
3317 {
3318 emsg_severe = TRUE;
3319 EMSG(_(e_trailing));
3320 }
3321 if (!(eap->skip || error))
3322 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 break;
3324 }
3325
3326 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003327 {
3328 if (eap->cmdidx == CMD_unlet)
3329 {
3330 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3331 error = TRUE;
3332 }
3333 else
3334 {
3335 if (do_lock_var(&lv, name_end, deep,
3336 eap->cmdidx == CMD_lockvar) == FAIL)
3337 error = TRUE;
3338 }
3339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003341 if (!eap->skip)
3342 clear_lval(&lv);
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 arg = skipwhite(name_end);
3345 } while (!ends_excmd(*arg));
3346
3347 eap->nextcmd = check_nextcmd(arg);
3348}
3349
Bram Moolenaar8c711452005-01-14 21:53:12 +00003350 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003352 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003353 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003354 int forceit;
3355{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003356 int ret = OK;
3357 int cc;
3358
3359 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003360 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003361 cc = *name_end;
3362 *name_end = NUL;
3363
3364 /* Normal name or expanded name. */
3365 if (check_changedtick(lp->ll_name))
3366 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003367 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003368 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003369 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003370 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003371 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3372 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003373 else if (lp->ll_range)
3374 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003375 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003376
3377 /* Delete a range of List items. */
3378 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3379 {
3380 li = lp->ll_li->li_next;
3381 listitem_remove(lp->ll_list, lp->ll_li);
3382 lp->ll_li = li;
3383 ++lp->ll_n1;
3384 }
3385 }
3386 else
3387 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003388 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003389 /* unlet a List item. */
3390 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003391 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003392 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003393 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003394 }
3395
3396 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
3400 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003401 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 */
3403 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003404do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003406 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaar33570922005-01-25 22:26:29 +00003408 hashtab_T *ht;
3409 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003410 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003411 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 ht = find_var_ht(name, &varname);
3414 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003416 hi = hash_find(ht, varname);
3417 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003418 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003419 di = HI2DI(hi);
3420 if (var_check_fixed(di->di_flags, name)
3421 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003422 return FAIL;
3423 delete_var(ht, hi);
3424 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003427 if (forceit)
3428 return OK;
3429 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 return FAIL;
3431}
3432
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003433/*
3434 * Lock or unlock variable indicated by "lp".
3435 * "deep" is the levels to go (-1 for unlimited);
3436 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3437 */
3438 static int
3439do_lock_var(lp, name_end, deep, lock)
3440 lval_T *lp;
3441 char_u *name_end;
3442 int deep;
3443 int lock;
3444{
3445 int ret = OK;
3446 int cc;
3447 dictitem_T *di;
3448
3449 if (deep == 0) /* nothing to do */
3450 return OK;
3451
3452 if (lp->ll_tv == NULL)
3453 {
3454 cc = *name_end;
3455 *name_end = NUL;
3456
3457 /* Normal name or expanded name. */
3458 if (check_changedtick(lp->ll_name))
3459 ret = FAIL;
3460 else
3461 {
3462 di = find_var(lp->ll_name, NULL);
3463 if (di == NULL)
3464 ret = FAIL;
3465 else
3466 {
3467 if (lock)
3468 di->di_flags |= DI_FLAGS_LOCK;
3469 else
3470 di->di_flags &= ~DI_FLAGS_LOCK;
3471 item_lock(&di->di_tv, deep, lock);
3472 }
3473 }
3474 *name_end = cc;
3475 }
3476 else if (lp->ll_range)
3477 {
3478 listitem_T *li = lp->ll_li;
3479
3480 /* (un)lock a range of List items. */
3481 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3482 {
3483 item_lock(&li->li_tv, deep, lock);
3484 li = li->li_next;
3485 ++lp->ll_n1;
3486 }
3487 }
3488 else if (lp->ll_list != NULL)
3489 /* (un)lock a List item. */
3490 item_lock(&lp->ll_li->li_tv, deep, lock);
3491 else
3492 /* un(lock) a Dictionary item. */
3493 item_lock(&lp->ll_di->di_tv, deep, lock);
3494
3495 return ret;
3496}
3497
3498/*
3499 * Lock or unlock an item. "deep" is nr of levels to go.
3500 */
3501 static void
3502item_lock(tv, deep, lock)
3503 typval_T *tv;
3504 int deep;
3505 int lock;
3506{
3507 static int recurse = 0;
3508 list_T *l;
3509 listitem_T *li;
3510 dict_T *d;
3511 hashitem_T *hi;
3512 int todo;
3513
3514 if (recurse >= DICT_MAXNEST)
3515 {
3516 EMSG(_("E743: variable nested too deep for (un)lock"));
3517 return;
3518 }
3519 if (deep == 0)
3520 return;
3521 ++recurse;
3522
3523 /* lock/unlock the item itself */
3524 if (lock)
3525 tv->v_lock |= VAR_LOCKED;
3526 else
3527 tv->v_lock &= ~VAR_LOCKED;
3528
3529 switch (tv->v_type)
3530 {
3531 case VAR_LIST:
3532 if ((l = tv->vval.v_list) != NULL)
3533 {
3534 if (lock)
3535 l->lv_lock |= VAR_LOCKED;
3536 else
3537 l->lv_lock &= ~VAR_LOCKED;
3538 if (deep < 0 || deep > 1)
3539 /* recursive: lock/unlock the items the List contains */
3540 for (li = l->lv_first; li != NULL; li = li->li_next)
3541 item_lock(&li->li_tv, deep - 1, lock);
3542 }
3543 break;
3544 case VAR_DICT:
3545 if ((d = tv->vval.v_dict) != NULL)
3546 {
3547 if (lock)
3548 d->dv_lock |= VAR_LOCKED;
3549 else
3550 d->dv_lock &= ~VAR_LOCKED;
3551 if (deep < 0 || deep > 1)
3552 {
3553 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003554 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003555 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3556 {
3557 if (!HASHITEM_EMPTY(hi))
3558 {
3559 --todo;
3560 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3561 }
3562 }
3563 }
3564 }
3565 }
3566 --recurse;
3567}
3568
Bram Moolenaara40058a2005-07-11 22:42:07 +00003569/*
3570 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3571 * it refers to a List or Dictionary that is locked.
3572 */
3573 static int
3574tv_islocked(tv)
3575 typval_T *tv;
3576{
3577 return (tv->v_lock & VAR_LOCKED)
3578 || (tv->v_type == VAR_LIST
3579 && tv->vval.v_list != NULL
3580 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3581 || (tv->v_type == VAR_DICT
3582 && tv->vval.v_dict != NULL
3583 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3584}
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3587/*
3588 * Delete all "menutrans_" variables.
3589 */
3590 void
3591del_menutrans_vars()
3592{
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003594 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar33570922005-01-25 22:26:29 +00003596 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003597 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003599 {
3600 if (!HASHITEM_EMPTY(hi))
3601 {
3602 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003603 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3604 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003605 }
3606 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003607 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608}
3609#endif
3610
3611#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3612
3613/*
3614 * Local string buffer for the next two functions to store a variable name
3615 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3616 * get_user_var_name().
3617 */
3618
3619static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3620
3621static char_u *varnamebuf = NULL;
3622static int varnamebuflen = 0;
3623
3624/*
3625 * Function to concatenate a prefix and a variable name.
3626 */
3627 static char_u *
3628cat_prefix_varname(prefix, name)
3629 int prefix;
3630 char_u *name;
3631{
3632 int len;
3633
3634 len = (int)STRLEN(name) + 3;
3635 if (len > varnamebuflen)
3636 {
3637 vim_free(varnamebuf);
3638 len += 10; /* some additional space */
3639 varnamebuf = alloc(len);
3640 if (varnamebuf == NULL)
3641 {
3642 varnamebuflen = 0;
3643 return NULL;
3644 }
3645 varnamebuflen = len;
3646 }
3647 *varnamebuf = prefix;
3648 varnamebuf[1] = ':';
3649 STRCPY(varnamebuf + 2, name);
3650 return varnamebuf;
3651}
3652
3653/*
3654 * Function given to ExpandGeneric() to obtain the list of user defined
3655 * (global/buffer/window/built-in) variable names.
3656 */
3657/*ARGSUSED*/
3658 char_u *
3659get_user_var_name(xp, idx)
3660 expand_T *xp;
3661 int idx;
3662{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003663 static long_u gdone;
3664 static long_u bdone;
3665 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003666#ifdef FEAT_WINDOWS
3667 static long_u tdone;
3668#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003669 static int vidx;
3670 static hashitem_T *hi;
3671 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003674 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003675 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003676#ifdef FEAT_WINDOWS
3677 tdone = 0;
3678#endif
3679 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003680
3681 /* Global variables */
3682 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003684 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003686 else
3687 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003688 while (HASHITEM_EMPTY(hi))
3689 ++hi;
3690 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3691 return cat_prefix_varname('g', hi->hi_key);
3692 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003694
3695 /* b: variables */
3696 ht = &curbuf->b_vars.dv_hashtab;
3697 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003699 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003700 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003701 else
3702 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003703 while (HASHITEM_EMPTY(hi))
3704 ++hi;
3705 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003707 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003709 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 return (char_u *)"b:changedtick";
3711 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003712
3713 /* w: variables */
3714 ht = &curwin->w_vars.dv_hashtab;
3715 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003717 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003719 else
3720 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003721 while (HASHITEM_EMPTY(hi))
3722 ++hi;
3723 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003725
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003726#ifdef FEAT_WINDOWS
3727 /* t: variables */
3728 ht = &curtab->tp_vars.dv_hashtab;
3729 if (tdone < ht->ht_used)
3730 {
3731 if (tdone++ == 0)
3732 hi = ht->ht_array;
3733 else
3734 ++hi;
3735 while (HASHITEM_EMPTY(hi))
3736 ++hi;
3737 return cat_prefix_varname('t', hi->hi_key);
3738 }
3739#endif
3740
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 /* v: variables */
3742 if (vidx < VV_LEN)
3743 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 vim_free(varnamebuf);
3746 varnamebuf = NULL;
3747 varnamebuflen = 0;
3748 return NULL;
3749}
3750
3751#endif /* FEAT_CMDL_COMPL */
3752
3753/*
3754 * types for expressions.
3755 */
3756typedef enum
3757{
3758 TYPE_UNKNOWN = 0
3759 , TYPE_EQUAL /* == */
3760 , TYPE_NEQUAL /* != */
3761 , TYPE_GREATER /* > */
3762 , TYPE_GEQUAL /* >= */
3763 , TYPE_SMALLER /* < */
3764 , TYPE_SEQUAL /* <= */
3765 , TYPE_MATCH /* =~ */
3766 , TYPE_NOMATCH /* !~ */
3767} exptype_T;
3768
3769/*
3770 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3773 */
3774
3775/*
3776 * Handle zero level expression.
3777 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003778 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003779 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 * Return OK or FAIL.
3781 */
3782 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003783eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 char_u **nextcmd;
3787 int evaluate;
3788{
3789 int ret;
3790 char_u *p;
3791
3792 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003793 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (ret == FAIL || !ends_excmd(*p))
3795 {
3796 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 /*
3799 * Report the invalid expression unless the expression evaluation has
3800 * been cancelled due to an aborting error, an interrupt, or an
3801 * exception.
3802 */
3803 if (!aborting())
3804 EMSG2(_(e_invexpr2), arg);
3805 ret = FAIL;
3806 }
3807 if (nextcmd != NULL)
3808 *nextcmd = check_nextcmd(p);
3809
3810 return ret;
3811}
3812
3813/*
3814 * Handle top level expression:
3815 * expr1 ? expr0 : expr0
3816 *
3817 * "arg" must point to the first non-white of the expression.
3818 * "arg" is advanced to the next non-white after the recognized expression.
3819 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003820 * Note: "rettv.v_lock" is not set.
3821 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 * Return OK or FAIL.
3823 */
3824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003825eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 int evaluate;
3829{
3830 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003831 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833 /*
3834 * Get the first variable.
3835 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 return FAIL;
3838
3839 if ((*arg)[0] == '?')
3840 {
3841 result = FALSE;
3842 if (evaluate)
3843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003844 int error = FALSE;
3845
3846 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003848 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003849 if (error)
3850 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852
3853 /*
3854 * Get the second variable.
3855 */
3856 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003857 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 return FAIL;
3859
3860 /*
3861 * Check for the ":".
3862 */
3863 if ((*arg)[0] != ':')
3864 {
3865 EMSG(_("E109: Missing ':' after '?'"));
3866 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 return FAIL;
3869 }
3870
3871 /*
3872 * Get the third variable.
3873 */
3874 *arg = skipwhite(*arg + 1);
3875 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3876 {
3877 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003878 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 return FAIL;
3880 }
3881 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884
3885 return OK;
3886}
3887
3888/*
3889 * Handle first level expression:
3890 * expr2 || expr2 || expr2 logical OR
3891 *
3892 * "arg" must point to the first non-white of the expression.
3893 * "arg" is advanced to the next non-white after the recognized expression.
3894 *
3895 * Return OK or FAIL.
3896 */
3897 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003898eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 int evaluate;
3902{
Bram Moolenaar33570922005-01-25 22:26:29 +00003903 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 long result;
3905 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003906 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908 /*
3909 * Get the first variable.
3910 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003911 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 return FAIL;
3913
3914 /*
3915 * Repeat until there is no following "||".
3916 */
3917 first = TRUE;
3918 result = FALSE;
3919 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3920 {
3921 if (evaluate && first)
3922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003923 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003926 if (error)
3927 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 first = FALSE;
3929 }
3930
3931 /*
3932 * Get the second variable.
3933 */
3934 *arg = skipwhite(*arg + 2);
3935 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3936 return FAIL;
3937
3938 /*
3939 * Compute the result.
3940 */
3941 if (evaluate && !result)
3942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003945 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003946 if (error)
3947 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949 if (evaluate)
3950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003951 rettv->v_type = VAR_NUMBER;
3952 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954 }
3955
3956 return OK;
3957}
3958
3959/*
3960 * Handle second level expression:
3961 * expr3 && expr3 && expr3 logical AND
3962 *
3963 * "arg" must point to the first non-white of the expression.
3964 * "arg" is advanced to the next non-white after the recognized expression.
3965 *
3966 * Return OK or FAIL.
3967 */
3968 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 int evaluate;
3973{
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 long result;
3976 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003977 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979 /*
3980 * Get the first variable.
3981 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003982 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 return FAIL;
3984
3985 /*
3986 * Repeat until there is no following "&&".
3987 */
3988 first = TRUE;
3989 result = TRUE;
3990 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3991 {
3992 if (evaluate && first)
3993 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003994 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003997 if (error)
3998 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 first = FALSE;
4000 }
4001
4002 /*
4003 * Get the second variable.
4004 */
4005 *arg = skipwhite(*arg + 2);
4006 if (eval4(arg, &var2, evaluate && result) == FAIL)
4007 return FAIL;
4008
4009 /*
4010 * Compute the result.
4011 */
4012 if (evaluate && result)
4013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004017 if (error)
4018 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
4020 if (evaluate)
4021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 rettv->v_type = VAR_NUMBER;
4023 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
4025 }
4026
4027 return OK;
4028}
4029
4030/*
4031 * Handle third level expression:
4032 * var1 == var2
4033 * var1 =~ var2
4034 * var1 != var2
4035 * var1 !~ var2
4036 * var1 > var2
4037 * var1 >= var2
4038 * var1 < var2
4039 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004040 * var1 is var2
4041 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 *
4043 * "arg" must point to the first non-white of the expression.
4044 * "arg" is advanced to the next non-white after the recognized expression.
4045 *
4046 * Return OK or FAIL.
4047 */
4048 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 int evaluate;
4053{
Bram Moolenaar33570922005-01-25 22:26:29 +00004054 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 char_u *p;
4056 int i;
4057 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004058 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 int len = 2;
4060 long n1, n2;
4061 char_u *s1, *s2;
4062 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4063 regmatch_T regmatch;
4064 int ic;
4065 char_u *save_cpo;
4066
4067 /*
4068 * Get the first variable.
4069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 return FAIL;
4072
4073 p = *arg;
4074 switch (p[0])
4075 {
4076 case '=': if (p[1] == '=')
4077 type = TYPE_EQUAL;
4078 else if (p[1] == '~')
4079 type = TYPE_MATCH;
4080 break;
4081 case '!': if (p[1] == '=')
4082 type = TYPE_NEQUAL;
4083 else if (p[1] == '~')
4084 type = TYPE_NOMATCH;
4085 break;
4086 case '>': if (p[1] != '=')
4087 {
4088 type = TYPE_GREATER;
4089 len = 1;
4090 }
4091 else
4092 type = TYPE_GEQUAL;
4093 break;
4094 case '<': if (p[1] != '=')
4095 {
4096 type = TYPE_SMALLER;
4097 len = 1;
4098 }
4099 else
4100 type = TYPE_SEQUAL;
4101 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004102 case 'i': if (p[1] == 's')
4103 {
4104 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4105 len = 5;
4106 if (!vim_isIDc(p[len]))
4107 {
4108 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4109 type_is = TRUE;
4110 }
4111 }
4112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114
4115 /*
4116 * If there is a comparitive operator, use it.
4117 */
4118 if (type != TYPE_UNKNOWN)
4119 {
4120 /* extra question mark appended: ignore case */
4121 if (p[len] == '?')
4122 {
4123 ic = TRUE;
4124 ++len;
4125 }
4126 /* extra '#' appended: match case */
4127 else if (p[len] == '#')
4128 {
4129 ic = FALSE;
4130 ++len;
4131 }
4132 /* nothing appened: use 'ignorecase' */
4133 else
4134 ic = p_ic;
4135
4136 /*
4137 * Get the second variable.
4138 */
4139 *arg = skipwhite(p + len);
4140 if (eval5(arg, &var2, evaluate) == FAIL)
4141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 return FAIL;
4144 }
4145
4146 if (evaluate)
4147 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004148 if (type_is && rettv->v_type != var2.v_type)
4149 {
4150 /* For "is" a different type always means FALSE, for "notis"
4151 * it means TRUE. */
4152 n1 = (type == TYPE_NEQUAL);
4153 }
4154 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4155 {
4156 if (type_is)
4157 {
4158 n1 = (rettv->v_type == var2.v_type
4159 && rettv->vval.v_list == var2.vval.v_list);
4160 if (type == TYPE_NEQUAL)
4161 n1 = !n1;
4162 }
4163 else if (rettv->v_type != var2.v_type
4164 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4165 {
4166 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004167 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004168 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004169 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004170 clear_tv(rettv);
4171 clear_tv(&var2);
4172 return FAIL;
4173 }
4174 else
4175 {
4176 /* Compare two Lists for being equal or unequal. */
4177 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4178 if (type == TYPE_NEQUAL)
4179 n1 = !n1;
4180 }
4181 }
4182
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004183 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4184 {
4185 if (type_is)
4186 {
4187 n1 = (rettv->v_type == var2.v_type
4188 && rettv->vval.v_dict == var2.vval.v_dict);
4189 if (type == TYPE_NEQUAL)
4190 n1 = !n1;
4191 }
4192 else if (rettv->v_type != var2.v_type
4193 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4194 {
4195 if (rettv->v_type != var2.v_type)
4196 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4197 else
4198 EMSG(_("E736: Invalid operation for Dictionary"));
4199 clear_tv(rettv);
4200 clear_tv(&var2);
4201 return FAIL;
4202 }
4203 else
4204 {
4205 /* Compare two Dictionaries for being equal or unequal. */
4206 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4207 if (type == TYPE_NEQUAL)
4208 n1 = !n1;
4209 }
4210 }
4211
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004212 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4213 {
4214 if (rettv->v_type != var2.v_type
4215 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4216 {
4217 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004218 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004219 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004220 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004221 clear_tv(rettv);
4222 clear_tv(&var2);
4223 return FAIL;
4224 }
4225 else
4226 {
4227 /* Compare two Funcrefs for being equal or unequal. */
4228 if (rettv->vval.v_string == NULL
4229 || var2.vval.v_string == NULL)
4230 n1 = FALSE;
4231 else
4232 n1 = STRCMP(rettv->vval.v_string,
4233 var2.vval.v_string) == 0;
4234 if (type == TYPE_NEQUAL)
4235 n1 = !n1;
4236 }
4237 }
4238
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 /*
4240 * If one of the two variables is a number, compare as a number.
4241 * When using "=~" or "!~", always compare as string.
4242 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004243 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 n1 = get_tv_number(rettv);
4247 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 switch (type)
4249 {
4250 case TYPE_EQUAL: n1 = (n1 == n2); break;
4251 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4252 case TYPE_GREATER: n1 = (n1 > n2); break;
4253 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4254 case TYPE_SMALLER: n1 = (n1 < n2); break;
4255 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4256 case TYPE_UNKNOWN:
4257 case TYPE_MATCH:
4258 case TYPE_NOMATCH: break; /* avoid gcc warning */
4259 }
4260 }
4261 else
4262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 s1 = get_tv_string_buf(rettv, buf1);
4264 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4266 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4267 else
4268 i = 0;
4269 n1 = FALSE;
4270 switch (type)
4271 {
4272 case TYPE_EQUAL: n1 = (i == 0); break;
4273 case TYPE_NEQUAL: n1 = (i != 0); break;
4274 case TYPE_GREATER: n1 = (i > 0); break;
4275 case TYPE_GEQUAL: n1 = (i >= 0); break;
4276 case TYPE_SMALLER: n1 = (i < 0); break;
4277 case TYPE_SEQUAL: n1 = (i <= 0); break;
4278
4279 case TYPE_MATCH:
4280 case TYPE_NOMATCH:
4281 /* avoid 'l' flag in 'cpoptions' */
4282 save_cpo = p_cpo;
4283 p_cpo = (char_u *)"";
4284 regmatch.regprog = vim_regcomp(s2,
4285 RE_MAGIC + RE_STRING);
4286 regmatch.rm_ic = ic;
4287 if (regmatch.regprog != NULL)
4288 {
4289 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4290 vim_free(regmatch.regprog);
4291 if (type == TYPE_NOMATCH)
4292 n1 = !n1;
4293 }
4294 p_cpo = save_cpo;
4295 break;
4296
4297 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4298 }
4299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 clear_tv(rettv);
4301 clear_tv(&var2);
4302 rettv->v_type = VAR_NUMBER;
4303 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 }
4306
4307 return OK;
4308}
4309
4310/*
4311 * Handle fourth level expression:
4312 * + number addition
4313 * - number subtraction
4314 * . string concatenation
4315 *
4316 * "arg" must point to the first non-white of the expression.
4317 * "arg" is advanced to the next non-white after the recognized expression.
4318 *
4319 * Return OK or FAIL.
4320 */
4321 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 int evaluate;
4326{
Bram Moolenaar33570922005-01-25 22:26:29 +00004327 typval_T var2;
4328 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 int op;
4330 long n1, n2;
4331 char_u *s1, *s2;
4332 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4333 char_u *p;
4334
4335 /*
4336 * Get the first variable.
4337 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 return FAIL;
4340
4341 /*
4342 * Repeat computing, until no '+', '-' or '.' is following.
4343 */
4344 for (;;)
4345 {
4346 op = **arg;
4347 if (op != '+' && op != '-' && op != '.')
4348 break;
4349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 if (op != '+' || rettv->v_type != VAR_LIST)
4351 {
4352 /* For "list + ...", an illegal use of the first operand as
4353 * a number cannot be determined before evaluating the 2nd
4354 * operand: if this is also a list, all is ok.
4355 * For "something . ...", "something - ..." or "non-list + ...",
4356 * we know that the first operand needs to be a string or number
4357 * without evaluating the 2nd operand. So check before to avoid
4358 * side effects after an error. */
4359 if (evaluate && get_tv_string_chk(rettv) == NULL)
4360 {
4361 clear_tv(rettv);
4362 return FAIL;
4363 }
4364 }
4365
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 /*
4367 * Get the second variable.
4368 */
4369 *arg = skipwhite(*arg + 1);
4370 if (eval6(arg, &var2, evaluate) == FAIL)
4371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 return FAIL;
4374 }
4375
4376 if (evaluate)
4377 {
4378 /*
4379 * Compute the result.
4380 */
4381 if (op == '.')
4382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004383 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4384 s2 = get_tv_string_buf_chk(&var2, buf2);
4385 if (s2 == NULL) /* type error ? */
4386 {
4387 clear_tv(rettv);
4388 clear_tv(&var2);
4389 return FAIL;
4390 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004391 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 clear_tv(rettv);
4393 rettv->v_type = VAR_STRING;
4394 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004396 else if (op == '+' && rettv->v_type == VAR_LIST
4397 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 {
4399 /* concatenate Lists */
4400 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4401 &var3) == FAIL)
4402 {
4403 clear_tv(rettv);
4404 clear_tv(&var2);
4405 return FAIL;
4406 }
4407 clear_tv(rettv);
4408 *rettv = var3;
4409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 else
4411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 int error = FALSE;
4413
4414 n1 = get_tv_number_chk(rettv, &error);
4415 if (error)
4416 {
4417 /* This can only happen for "list + non-list".
4418 * For "non-list + ..." or "something - ...", we returned
4419 * before evaluating the 2nd operand. */
4420 clear_tv(rettv);
4421 return FAIL;
4422 }
4423 n2 = get_tv_number_chk(&var2, &error);
4424 if (error)
4425 {
4426 clear_tv(rettv);
4427 clear_tv(&var2);
4428 return FAIL;
4429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (op == '+')
4432 n1 = n1 + n2;
4433 else
4434 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 rettv->v_type = VAR_NUMBER;
4436 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 }
4441 return OK;
4442}
4443
4444/*
4445 * Handle fifth level expression:
4446 * * number multiplication
4447 * / number division
4448 * % number modulo
4449 *
4450 * "arg" must point to the first non-white of the expression.
4451 * "arg" is advanced to the next non-white after the recognized expression.
4452 *
4453 * Return OK or FAIL.
4454 */
4455 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004456eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 int evaluate;
4460{
Bram Moolenaar33570922005-01-25 22:26:29 +00004461 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 int op;
4463 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004464 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 /*
4467 * Get the first variable.
4468 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 return FAIL;
4471
4472 /*
4473 * Repeat computing, until no '*', '/' or '%' is following.
4474 */
4475 for (;;)
4476 {
4477 op = **arg;
4478 if (op != '*' && op != '/' && op != '%')
4479 break;
4480
4481 if (evaluate)
4482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004485 if (error)
4486 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 else
4489 n1 = 0;
4490
4491 /*
4492 * Get the second variable.
4493 */
4494 *arg = skipwhite(*arg + 1);
4495 if (eval7(arg, &var2, evaluate) == FAIL)
4496 return FAIL;
4497
4498 if (evaluate)
4499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004500 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004501 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502 if (error)
4503 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 /*
4506 * Compute the result.
4507 */
4508 if (op == '*')
4509 n1 = n1 * n2;
4510 else if (op == '/')
4511 {
4512 if (n2 == 0) /* give an error message? */
4513 n1 = 0x7fffffffL;
4514 else
4515 n1 = n1 / n2;
4516 }
4517 else
4518 {
4519 if (n2 == 0) /* give an error message? */
4520 n1 = 0;
4521 else
4522 n1 = n1 % n2;
4523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004524 rettv->v_type = VAR_NUMBER;
4525 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527 }
4528
4529 return OK;
4530}
4531
4532/*
4533 * Handle sixth level expression:
4534 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004535 * "string" string constant
4536 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 * &option-name option value
4538 * @r register contents
4539 * identifier variable value
4540 * function() function call
4541 * $VAR environment variable
4542 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004543 * [expr, expr] List
4544 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 *
4546 * Also handle:
4547 * ! in front logical NOT
4548 * - in front unary minus
4549 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 * trailing [] subscript in String or List
4551 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 *
4553 * "arg" must point to the first non-white of the expression.
4554 * "arg" is advanced to the next non-white after the recognized expression.
4555 *
4556 * Return OK or FAIL.
4557 */
4558 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004559eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 int evaluate;
4563{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 long n;
4565 int len;
4566 char_u *s;
4567 int val;
4568 char_u *start_leader, *end_leader;
4569 int ret = OK;
4570 char_u *alias;
4571
4572 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
4578 /*
4579 * Skip '!' and '-' characters. They are handled later.
4580 */
4581 start_leader = *arg;
4582 while (**arg == '!' || **arg == '-' || **arg == '+')
4583 *arg = skipwhite(*arg + 1);
4584 end_leader = *arg;
4585
4586 switch (**arg)
4587 {
4588 /*
4589 * Number constant.
4590 */
4591 case '0':
4592 case '1':
4593 case '2':
4594 case '3':
4595 case '4':
4596 case '5':
4597 case '6':
4598 case '7':
4599 case '8':
4600 case '9':
4601 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4602 *arg += len;
4603 if (evaluate)
4604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 rettv->v_type = VAR_NUMBER;
4606 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 break;
4609
4610 /*
4611 * String constant: "string".
4612 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 break;
4615
4616 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 break;
4621
4622 /*
4623 * List: [expr, expr]
4624 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 break;
4627
4628 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 * Dictionary: {key: val, key: val}
4630 */
4631 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4632 break;
4633
4634 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004635 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004637 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 break;
4639
4640 /*
4641 * Environment variable: $VAR.
4642 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 break;
4645
4646 /*
4647 * Register contents: @r.
4648 */
4649 case '@': ++*arg;
4650 if (evaluate)
4651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004653 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655 if (**arg != NUL)
4656 ++*arg;
4657 break;
4658
4659 /*
4660 * nested expression: (expression).
4661 */
4662 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004663 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 if (**arg == ')')
4665 ++*arg;
4666 else if (ret == OK)
4667 {
4668 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 ret = FAIL;
4671 }
4672 break;
4673
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 break;
4676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677
4678 if (ret == NOTDONE)
4679 {
4680 /*
4681 * Must be a variable or function name.
4682 * Can also be a curly-braces kind of name: {expr}.
4683 */
4684 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004685 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004686 if (alias != NULL)
4687 s = alias;
4688
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004689 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 ret = FAIL;
4691 else
4692 {
4693 if (**arg == '(') /* recursive! */
4694 {
4695 /* If "s" is the name of a variable of type VAR_FUNC
4696 * use its contents. */
4697 s = deref_func_name(s, &len);
4698
4699 /* Invoke the function. */
4700 ret = get_func_tv(s, len, rettv, arg,
4701 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004702 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004703 /* Stop the expression evaluation when immediately
4704 * aborting on error, or when an interrupt occurred or
4705 * an exception was thrown but not caught. */
4706 if (aborting())
4707 {
4708 if (ret == OK)
4709 clear_tv(rettv);
4710 ret = FAIL;
4711 }
4712 }
4713 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004714 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004715 else
4716 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 }
4718
4719 if (alias != NULL)
4720 vim_free(alias);
4721 }
4722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 *arg = skipwhite(*arg);
4724
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004725 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4726 * expr(expr). */
4727 if (ret == OK)
4728 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
4730 /*
4731 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4732 */
4733 if (ret == OK && evaluate && end_leader > start_leader)
4734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004735 int error = FALSE;
4736
4737 val = get_tv_number_chk(rettv, &error);
4738 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 clear_tv(rettv);
4741 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004743 else
4744 {
4745 while (end_leader > start_leader)
4746 {
4747 --end_leader;
4748 if (*end_leader == '!')
4749 val = !val;
4750 else if (*end_leader == '-')
4751 val = -val;
4752 }
4753 clear_tv(rettv);
4754 rettv->v_type = VAR_NUMBER;
4755 rettv->vval.v_number = val;
4756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758
4759 return ret;
4760}
4761
4762/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004763 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4764 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4766 */
4767 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004768eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004770 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004772 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773{
4774 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004775 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 long len = -1;
4778 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004782 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004784 if (verbose)
4785 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004786 return FAIL;
4787 }
4788
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 /*
4792 * dict.name
4793 */
4794 key = *arg + 1;
4795 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4796 ;
4797 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 }
4801 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 /*
4804 * something[idx]
4805 *
4806 * Get the (first) variable from inside the [].
4807 */
4808 *arg = skipwhite(*arg + 1);
4809 if (**arg == ':')
4810 empty1 = TRUE;
4811 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4812 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4814 {
4815 /* not a number or string */
4816 clear_tv(&var1);
4817 return FAIL;
4818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004819
4820 /*
4821 * Get the second variable from inside the [:].
4822 */
4823 if (**arg == ':')
4824 {
4825 range = TRUE;
4826 *arg = skipwhite(*arg + 1);
4827 if (**arg == ']')
4828 empty2 = TRUE;
4829 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004831 if (!empty1)
4832 clear_tv(&var1);
4833 return FAIL;
4834 }
4835 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4836 {
4837 /* not a number or string */
4838 if (!empty1)
4839 clear_tv(&var1);
4840 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004841 return FAIL;
4842 }
4843 }
4844
4845 /* Check for the ']'. */
4846 if (**arg != ']')
4847 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004848 if (verbose)
4849 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004850 clear_tv(&var1);
4851 if (range)
4852 clear_tv(&var2);
4853 return FAIL;
4854 }
4855 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 }
4857
4858 if (evaluate)
4859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004860 n1 = 0;
4861 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 n1 = get_tv_number(&var1);
4864 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 }
4866 if (range)
4867 {
4868 if (empty2)
4869 n2 = -1;
4870 else
4871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004872 n2 = get_tv_number(&var2);
4873 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004874 }
4875 }
4876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004878 {
4879 case VAR_NUMBER:
4880 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004882 len = (long)STRLEN(s);
4883 if (range)
4884 {
4885 /* The resulting variable is a substring. If the indexes
4886 * are out of range the result is empty. */
4887 if (n1 < 0)
4888 {
4889 n1 = len + n1;
4890 if (n1 < 0)
4891 n1 = 0;
4892 }
4893 if (n2 < 0)
4894 n2 = len + n2;
4895 else if (n2 >= len)
4896 n2 = len;
4897 if (n1 >= len || n2 < 0 || n1 > n2)
4898 s = NULL;
4899 else
4900 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4901 }
4902 else
4903 {
4904 /* The resulting variable is a string of a single
4905 * character. If the index is too big or negative the
4906 * result is empty. */
4907 if (n1 >= len || n1 < 0)
4908 s = NULL;
4909 else
4910 s = vim_strnsave(s + n1, 1);
4911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 clear_tv(rettv);
4913 rettv->v_type = VAR_STRING;
4914 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004915 break;
4916
4917 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919 if (n1 < 0)
4920 n1 = len + n1;
4921 if (!empty1 && (n1 < 0 || n1 >= len))
4922 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004923 /* For a range we allow invalid values and return an empty
4924 * list. A list index out of range is an error. */
4925 if (!range)
4926 {
4927 if (verbose)
4928 EMSGN(_(e_listidx), n1);
4929 return FAIL;
4930 }
4931 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004932 }
4933 if (range)
4934 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004935 list_T *l;
4936 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004937
4938 if (n2 < 0)
4939 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004940 else if (n2 >= len)
4941 n2 = len - 1;
4942 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004943 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004944 l = list_alloc();
4945 if (l == NULL)
4946 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004947 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 n1 <= n2; ++n1)
4949 {
4950 if (list_append_tv(l, &item->li_tv) == FAIL)
4951 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004952 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004953 return FAIL;
4954 }
4955 item = item->li_next;
4956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004957 clear_tv(rettv);
4958 rettv->v_type = VAR_LIST;
4959 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004960 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004961 }
4962 else
4963 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004964 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 clear_tv(rettv);
4966 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967 }
4968 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969
4970 case VAR_DICT:
4971 if (range)
4972 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004973 if (verbose)
4974 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 if (len == -1)
4976 clear_tv(&var1);
4977 return FAIL;
4978 }
4979 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004980 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981
4982 if (len == -1)
4983 {
4984 key = get_tv_string(&var1);
4985 if (*key == NUL)
4986 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004987 if (verbose)
4988 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 clear_tv(&var1);
4990 return FAIL;
4991 }
4992 }
4993
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004994 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004996 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004997 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 if (len == -1)
4999 clear_tv(&var1);
5000 if (item == NULL)
5001 return FAIL;
5002
5003 copy_tv(&item->di_tv, &var1);
5004 clear_tv(rettv);
5005 *rettv = var1;
5006 }
5007 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 }
5009 }
5010
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 return OK;
5012}
5013
5014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 * Get an option value.
5016 * "arg" points to the '&' or '+' before the option name.
5017 * "arg" is advanced to character after the option name.
5018 * Return OK or FAIL.
5019 */
5020 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005021get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005023 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 int evaluate;
5025{
5026 char_u *option_end;
5027 long numval;
5028 char_u *stringval;
5029 int opt_type;
5030 int c;
5031 int working = (**arg == '+'); /* has("+option") */
5032 int ret = OK;
5033 int opt_flags;
5034
5035 /*
5036 * Isolate the option name and find its value.
5037 */
5038 option_end = find_option_end(arg, &opt_flags);
5039 if (option_end == NULL)
5040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 EMSG2(_("E112: Option name missing: %s"), *arg);
5043 return FAIL;
5044 }
5045
5046 if (!evaluate)
5047 {
5048 *arg = option_end;
5049 return OK;
5050 }
5051
5052 c = *option_end;
5053 *option_end = NUL;
5054 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
5057 if (opt_type == -3) /* invalid name */
5058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 EMSG2(_("E113: Unknown option: %s"), *arg);
5061 ret = FAIL;
5062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 if (opt_type == -2) /* hidden string option */
5066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 rettv->v_type = VAR_STRING;
5068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070 else if (opt_type == -1) /* hidden number option */
5071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 rettv->v_type = VAR_NUMBER;
5073 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075 else if (opt_type == 1) /* number option */
5076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 rettv->v_type = VAR_NUMBER;
5078 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 else /* string option */
5081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 rettv->v_type = VAR_STRING;
5083 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 }
5085 }
5086 else if (working && (opt_type == -2 || opt_type == -1))
5087 ret = FAIL;
5088
5089 *option_end = c; /* put back for error messages */
5090 *arg = option_end;
5091
5092 return ret;
5093}
5094
5095/*
5096 * Allocate a variable for a string constant.
5097 * Return OK or FAIL.
5098 */
5099 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 int evaluate;
5104{
5105 char_u *p;
5106 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 int extra = 0;
5108
5109 /*
5110 * Find the end of the string, skipping backslashed characters.
5111 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 if (*p == '\\' && p[1] != NUL)
5115 {
5116 ++p;
5117 /* A "\<x>" form occupies at least 4 characters, and produces up
5118 * to 6 characters: reserve space for 2 extra */
5119 if (*p == '<')
5120 extra += 2;
5121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123
5124 if (*p != '"')
5125 {
5126 EMSG2(_("E114: Missing quote: %s"), *arg);
5127 return FAIL;
5128 }
5129
5130 /* If only parsing, set *arg and return here */
5131 if (!evaluate)
5132 {
5133 *arg = p + 1;
5134 return OK;
5135 }
5136
5137 /*
5138 * Copy the string into allocated memory, handling backslashed
5139 * characters.
5140 */
5141 name = alloc((unsigned)(p - *arg + extra));
5142 if (name == NULL)
5143 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 rettv->v_type = VAR_STRING;
5145 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 if (*p == '\\')
5150 {
5151 switch (*++p)
5152 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 case 'b': *name++ = BS; ++p; break;
5154 case 'e': *name++ = ESC; ++p; break;
5155 case 'f': *name++ = FF; ++p; break;
5156 case 'n': *name++ = NL; ++p; break;
5157 case 'r': *name++ = CAR; ++p; break;
5158 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
5160 case 'X': /* hex: "\x1", "\x12" */
5161 case 'x':
5162 case 'u': /* Unicode: "\u0023" */
5163 case 'U':
5164 if (vim_isxdigit(p[1]))
5165 {
5166 int n, nr;
5167 int c = toupper(*p);
5168
5169 if (c == 'X')
5170 n = 2;
5171 else
5172 n = 4;
5173 nr = 0;
5174 while (--n >= 0 && vim_isxdigit(p[1]))
5175 {
5176 ++p;
5177 nr = (nr << 4) + hex2nr(*p);
5178 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#ifdef FEAT_MBYTE
5181 /* For "\u" store the number according to
5182 * 'encoding'. */
5183 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 else
5186#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 break;
5190
5191 /* octal: "\1", "\12", "\123" */
5192 case '0':
5193 case '1':
5194 case '2':
5195 case '3':
5196 case '4':
5197 case '5':
5198 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 case '7': *name = *p++ - '0';
5200 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 *name = (*name << 3) + *p++ - '0';
5203 if (*p >= '0' && *p <= '7')
5204 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
5208
5209 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (extra != 0)
5212 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
5216 /* FALLTHROUGH */
5217
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 break;
5220 }
5221 }
5222 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 *arg = p + 1;
5228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 return OK;
5230}
5231
5232/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 * Return OK or FAIL.
5235 */
5236 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 int evaluate;
5241{
5242 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005243 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005244 int reduce = 0;
5245
5246 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005248 */
5249 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5250 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005252 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005254 break;
5255 ++reduce;
5256 ++p;
5257 }
5258 }
5259
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005261 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005263 return FAIL;
5264 }
5265
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005267 if (!evaluate)
5268 {
5269 *arg = p + 1;
5270 return OK;
5271 }
5272
5273 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005275 */
5276 str = alloc((unsigned)((p - *arg) - reduce));
5277 if (str == NULL)
5278 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 rettv->v_type = VAR_STRING;
5280 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005281
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005285 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005287 break;
5288 ++p;
5289 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005293 *arg = p + 1;
5294
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005295 return OK;
5296}
5297
5298/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 * Allocate a variable for a List and fill it from "*arg".
5300 * Return OK or FAIL.
5301 */
5302 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005305 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 int evaluate;
5307{
Bram Moolenaar33570922005-01-25 22:26:29 +00005308 list_T *l = NULL;
5309 typval_T tv;
5310 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311
5312 if (evaluate)
5313 {
5314 l = list_alloc();
5315 if (l == NULL)
5316 return FAIL;
5317 }
5318
5319 *arg = skipwhite(*arg + 1);
5320 while (**arg != ']' && **arg != NUL)
5321 {
5322 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5323 goto failret;
5324 if (evaluate)
5325 {
5326 item = listitem_alloc();
5327 if (item != NULL)
5328 {
5329 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005330 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 list_append(l, item);
5332 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005333 else
5334 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 }
5336
5337 if (**arg == ']')
5338 break;
5339 if (**arg != ',')
5340 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 goto failret;
5343 }
5344 *arg = skipwhite(*arg + 1);
5345 }
5346
5347 if (**arg != ']')
5348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350failret:
5351 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005352 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 return FAIL;
5354 }
5355
5356 *arg = skipwhite(*arg + 1);
5357 if (evaluate)
5358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005359 rettv->v_type = VAR_LIST;
5360 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 ++l->lv_refcount;
5362 }
5363
5364 return OK;
5365}
5366
5367/*
5368 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005369 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005371 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372list_alloc()
5373{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005374 list_T *l;
5375
5376 l = (list_T *)alloc_clear(sizeof(list_T));
5377 if (l != NULL)
5378 {
5379 /* Prepend the list to the list of lists for garbage collection. */
5380 if (first_list != NULL)
5381 first_list->lv_used_prev = l;
5382 l->lv_used_prev = NULL;
5383 l->lv_used_next = first_list;
5384 first_list = l;
5385 }
5386 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387}
5388
5389/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005390 * Allocate an empty list for a return value.
5391 * Returns OK or FAIL.
5392 */
5393 static int
5394rettv_list_alloc(rettv)
5395 typval_T *rettv;
5396{
5397 list_T *l = list_alloc();
5398
5399 if (l == NULL)
5400 return FAIL;
5401
5402 rettv->vval.v_list = l;
5403 rettv->v_type = VAR_LIST;
5404 ++l->lv_refcount;
5405 return OK;
5406}
5407
5408/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 * Unreference a list: decrement the reference count and free it when it
5410 * becomes zero.
5411 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005412 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005414 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005416 if (l != NULL && --l->lv_refcount <= 0)
5417 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418}
5419
5420/*
5421 * Free a list, including all items it points to.
5422 * Ignores the reference count.
5423 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005424 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005425list_free(l, recurse)
5426 list_T *l;
5427 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428{
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005431 /* Remove the list from the list of lists for garbage collection. */
5432 if (l->lv_used_prev == NULL)
5433 first_list = l->lv_used_next;
5434 else
5435 l->lv_used_prev->lv_used_next = l->lv_used_next;
5436 if (l->lv_used_next != NULL)
5437 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5438
Bram Moolenaard9fba312005-06-26 22:34:35 +00005439 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005441 /* Remove the item before deleting it. */
5442 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005443 if (recurse || (item->li_tv.v_type != VAR_LIST
5444 && item->li_tv.v_type != VAR_DICT))
5445 clear_tv(&item->li_tv);
5446 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 }
5448 vim_free(l);
5449}
5450
5451/*
5452 * Allocate a list item.
5453 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455listitem_alloc()
5456{
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458}
5459
5460/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005461 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 */
5463 static void
5464listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005465 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 vim_free(item);
5469}
5470
5471/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005472 * Remove a list item from a List and free it. Also clears the value.
5473 */
5474 static void
5475listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 list_T *l;
5477 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005478{
5479 list_remove(l, item, item);
5480 listitem_free(item);
5481}
5482
5483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 * Get the number of items in a list.
5485 */
5486 static long
5487list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005488 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 if (l == NULL)
5491 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005492 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493}
5494
5495/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005496 * Return TRUE when two lists have exactly the same values.
5497 */
5498 static int
5499list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 list_T *l1;
5501 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005502 int ic; /* ignore case for strings */
5503{
Bram Moolenaar33570922005-01-25 22:26:29 +00005504 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005505
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005506 if (l1 == l2)
5507 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005508 if (list_len(l1) != list_len(l2))
5509 return FALSE;
5510
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005511 for (item1 = l1->lv_first, item2 = l2->lv_first;
5512 item1 != NULL && item2 != NULL;
5513 item1 = item1->li_next, item2 = item2->li_next)
5514 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5515 return FALSE;
5516 return item1 == NULL && item2 == NULL;
5517}
5518
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005519#if defined(FEAT_PYTHON) || defined(PROTO)
5520/*
5521 * Return the dictitem that an entry in a hashtable points to.
5522 */
5523 dictitem_T *
5524dict_lookup(hi)
5525 hashitem_T *hi;
5526{
5527 return HI2DI(hi);
5528}
5529#endif
5530
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005531/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005532 * Return TRUE when two dictionaries have exactly the same key/values.
5533 */
5534 static int
5535dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005536 dict_T *d1;
5537 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005538 int ic; /* ignore case for strings */
5539{
Bram Moolenaar33570922005-01-25 22:26:29 +00005540 hashitem_T *hi;
5541 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005542 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005543
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005544 if (d1 == d2)
5545 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005546 if (dict_len(d1) != dict_len(d2))
5547 return FALSE;
5548
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005549 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005550 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005552 if (!HASHITEM_EMPTY(hi))
5553 {
5554 item2 = dict_find(d2, hi->hi_key, -1);
5555 if (item2 == NULL)
5556 return FALSE;
5557 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5558 return FALSE;
5559 --todo;
5560 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005561 }
5562 return TRUE;
5563}
5564
5565/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005566 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005567 * Compares the items just like "==" would compare them, but strings and
5568 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005569 */
5570 static int
5571tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 typval_T *tv1;
5573 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005574 int ic; /* ignore case */
5575{
5576 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005577 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005578 static int recursive = 0; /* cach recursive loops */
5579 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005580
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005581 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005582 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005583 /* Catch lists and dicts that have an endless loop by limiting
5584 * recursiveness to 1000. We guess they are equal then. */
5585 if (recursive >= 1000)
5586 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005587
5588 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005589 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005590 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005591 ++recursive;
5592 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5593 --recursive;
5594 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005595
5596 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005597 ++recursive;
5598 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5599 --recursive;
5600 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005601
5602 case VAR_FUNC:
5603 return (tv1->vval.v_string != NULL
5604 && tv2->vval.v_string != NULL
5605 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5606
5607 case VAR_NUMBER:
5608 return tv1->vval.v_number == tv2->vval.v_number;
5609
5610 case VAR_STRING:
5611 s1 = get_tv_string_buf(tv1, buf1);
5612 s2 = get_tv_string_buf(tv2, buf2);
5613 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005614 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005615
5616 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005617 return TRUE;
5618}
5619
5620/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 * Locate item with index "n" in list "l" and return it.
5622 * A negative index is counted from the end; -1 is the last item.
5623 * Returns NULL when "n" is out of range.
5624 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005625 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 long n;
5629{
Bram Moolenaar33570922005-01-25 22:26:29 +00005630 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005631 long idx;
5632
5633 if (l == NULL)
5634 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005635
5636 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005637 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005638 n = l->lv_len + n;
5639
5640 /* Check for index out of range. */
5641 if (n < 0 || n >= l->lv_len)
5642 return NULL;
5643
5644 /* When there is a cached index may start search from there. */
5645 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005647 if (n < l->lv_idx / 2)
5648 {
5649 /* closest to the start of the list */
5650 item = l->lv_first;
5651 idx = 0;
5652 }
5653 else if (n > (l->lv_idx + l->lv_len) / 2)
5654 {
5655 /* closest to the end of the list */
5656 item = l->lv_last;
5657 idx = l->lv_len - 1;
5658 }
5659 else
5660 {
5661 /* closest to the cached index */
5662 item = l->lv_idx_item;
5663 idx = l->lv_idx;
5664 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 }
5666 else
5667 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005668 if (n < l->lv_len / 2)
5669 {
5670 /* closest to the start of the list */
5671 item = l->lv_first;
5672 idx = 0;
5673 }
5674 else
5675 {
5676 /* closest to the end of the list */
5677 item = l->lv_last;
5678 idx = l->lv_len - 1;
5679 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005681
5682 while (n > idx)
5683 {
5684 /* search forward */
5685 item = item->li_next;
5686 ++idx;
5687 }
5688 while (n < idx)
5689 {
5690 /* search backward */
5691 item = item->li_prev;
5692 --idx;
5693 }
5694
5695 /* cache the used index */
5696 l->lv_idx = idx;
5697 l->lv_idx_item = item;
5698
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699 return item;
5700}
5701
5702/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005703 * Get list item "l[idx]" as a number.
5704 */
5705 static long
5706list_find_nr(l, idx, errorp)
5707 list_T *l;
5708 long idx;
5709 int *errorp; /* set to TRUE when something wrong */
5710{
5711 listitem_T *li;
5712
5713 li = list_find(l, idx);
5714 if (li == NULL)
5715 {
5716 if (errorp != NULL)
5717 *errorp = TRUE;
5718 return -1L;
5719 }
5720 return get_tv_number_chk(&li->li_tv, errorp);
5721}
5722
5723/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005724 * Locate "item" list "l" and return its index.
5725 * Returns -1 when "item" is not in the list.
5726 */
5727 static long
5728list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 list_T *l;
5730 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005731{
5732 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005733 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005734
5735 if (l == NULL)
5736 return -1;
5737 idx = 0;
5738 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5739 ++idx;
5740 if (li == NULL)
5741 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005742 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005743}
5744
5745/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 * Append item "item" to the end of list "l".
5747 */
5748 static void
5749list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005750 list_T *l;
5751 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752{
5753 if (l->lv_last == NULL)
5754 {
5755 /* empty list */
5756 l->lv_first = item;
5757 l->lv_last = item;
5758 item->li_prev = NULL;
5759 }
5760 else
5761 {
5762 l->lv_last->li_next = item;
5763 item->li_prev = l->lv_last;
5764 l->lv_last = item;
5765 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005766 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767 item->li_next = NULL;
5768}
5769
5770/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005771 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005772 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773 */
5774 static int
5775list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 list_T *l;
5777 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005779 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780
Bram Moolenaar05159a02005-02-26 23:04:13 +00005781 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005783 copy_tv(tv, &li->li_tv);
5784 list_append(l, li);
5785 return OK;
5786}
5787
5788/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005789 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005790 * Return FAIL when out of memory.
5791 */
5792 int
5793list_append_dict(list, dict)
5794 list_T *list;
5795 dict_T *dict;
5796{
5797 listitem_T *li = listitem_alloc();
5798
5799 if (li == NULL)
5800 return FAIL;
5801 li->li_tv.v_type = VAR_DICT;
5802 li->li_tv.v_lock = 0;
5803 li->li_tv.vval.v_dict = dict;
5804 list_append(list, li);
5805 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 return OK;
5807}
5808
5809/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005810 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005811 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005812 * Returns FAIL when out of memory.
5813 */
5814 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005815list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005816 list_T *l;
5817 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005818 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005819{
5820 listitem_T *li = listitem_alloc();
5821
5822 if (li == NULL)
5823 return FAIL;
5824 list_append(l, li);
5825 li->li_tv.v_type = VAR_STRING;
5826 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005827 if (str == NULL)
5828 li->li_tv.vval.v_string = NULL;
5829 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005830 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005831 return FAIL;
5832 return OK;
5833}
5834
5835/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005836 * Append "n" to list "l".
5837 * Returns FAIL when out of memory.
5838 */
5839 static int
5840list_append_number(l, n)
5841 list_T *l;
5842 varnumber_T n;
5843{
5844 listitem_T *li;
5845
5846 li = listitem_alloc();
5847 if (li == NULL)
5848 return FAIL;
5849 li->li_tv.v_type = VAR_NUMBER;
5850 li->li_tv.v_lock = 0;
5851 li->li_tv.vval.v_number = n;
5852 list_append(l, li);
5853 return OK;
5854}
5855
5856/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005857 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005858 * If "item" is NULL append at the end.
5859 * Return FAIL when out of memory.
5860 */
5861 static int
5862list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005863 list_T *l;
5864 typval_T *tv;
5865 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866{
Bram Moolenaar33570922005-01-25 22:26:29 +00005867 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005868
5869 if (ni == NULL)
5870 return FAIL;
5871 copy_tv(tv, &ni->li_tv);
5872 if (item == NULL)
5873 /* Append new item at end of list. */
5874 list_append(l, ni);
5875 else
5876 {
5877 /* Insert new item before existing item. */
5878 ni->li_prev = item->li_prev;
5879 ni->li_next = item;
5880 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005881 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005883 ++l->lv_idx;
5884 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005885 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005886 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005888 l->lv_idx_item = NULL;
5889 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005890 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005891 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005892 }
5893 return OK;
5894}
5895
5896/*
5897 * Extend "l1" with "l2".
5898 * If "bef" is NULL append at the end, otherwise insert before this item.
5899 * Returns FAIL when out of memory.
5900 */
5901 static int
5902list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 list_T *l1;
5904 list_T *l2;
5905 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005906{
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005908
5909 for (item = l2->lv_first; item != NULL; item = item->li_next)
5910 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5911 return FAIL;
5912 return OK;
5913}
5914
5915/*
5916 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5917 * Return FAIL when out of memory.
5918 */
5919 static int
5920list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 list_T *l1;
5922 list_T *l2;
5923 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005924{
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926
5927 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005928 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005929 if (l == NULL)
5930 return FAIL;
5931 tv->v_type = VAR_LIST;
5932 tv->vval.v_list = l;
5933
5934 /* append all items from the second list */
5935 return list_extend(l, l2, NULL);
5936}
5937
5938/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005939 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005941 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 * Returns NULL when out of memory.
5943 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005945list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005946 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005948 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949{
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 list_T *copy;
5951 listitem_T *item;
5952 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953
5954 if (orig == NULL)
5955 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956
5957 copy = list_alloc();
5958 if (copy != NULL)
5959 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 if (copyID != 0)
5961 {
5962 /* Do this before adding the items, because one of the items may
5963 * refer back to this list. */
5964 orig->lv_copyID = copyID;
5965 orig->lv_copylist = copy;
5966 }
5967 for (item = orig->lv_first; item != NULL && !got_int;
5968 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 {
5970 ni = listitem_alloc();
5971 if (ni == NULL)
5972 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005973 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005974 {
5975 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5976 {
5977 vim_free(ni);
5978 break;
5979 }
5980 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005982 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 list_append(copy, ni);
5984 }
5985 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005986 if (item != NULL)
5987 {
5988 list_unref(copy);
5989 copy = NULL;
5990 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 }
5992
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993 return copy;
5994}
5995
5996/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005998 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006000 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006001list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006002 list_T *l;
6003 listitem_T *item;
6004 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005{
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006008 /* notify watchers */
6009 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006011 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006012 list_fix_watch(l, ip);
6013 if (ip == item2)
6014 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016
6017 if (item2->li_next == NULL)
6018 l->lv_last = item->li_prev;
6019 else
6020 item2->li_next->li_prev = item->li_prev;
6021 if (item->li_prev == NULL)
6022 l->lv_first = item2->li_next;
6023 else
6024 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006025 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026}
6027
6028/*
6029 * Return an allocated string with the string representation of a list.
6030 * May return NULL.
6031 */
6032 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006035 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036{
6037 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038
6039 if (tv->vval.v_list == NULL)
6040 return NULL;
6041 ga_init2(&ga, (int)sizeof(char), 80);
6042 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006043 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006044 {
6045 vim_free(ga.ga_data);
6046 return NULL;
6047 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 ga_append(&ga, ']');
6049 ga_append(&ga, NUL);
6050 return (char_u *)ga.ga_data;
6051}
6052
6053/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054 * Join list "l" into a string in "*gap", using separator "sep".
6055 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006056 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006058 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006059list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006060 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006062 char_u *sep;
6063 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006064 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006065{
6066 int first = TRUE;
6067 char_u *tofree;
6068 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006070 char_u *s;
6071
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006072 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006073 {
6074 if (first)
6075 first = FALSE;
6076 else
6077 ga_concat(gap, sep);
6078
6079 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006080 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006081 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006082 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006083 if (s != NULL)
6084 ga_concat(gap, s);
6085 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006086 if (s == NULL)
6087 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006088 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006089 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006090}
6091
6092/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006093 * Garbage collection for lists and dictionaries.
6094 *
6095 * We use reference counts to be able to free most items right away when they
6096 * are no longer used. But for composite items it's possible that it becomes
6097 * unused while the reference count is > 0: When there is a recursive
6098 * reference. Example:
6099 * :let l = [1, 2, 3]
6100 * :let d = {9: l}
6101 * :let l[1] = d
6102 *
6103 * Since this is quite unusual we handle this with garbage collection: every
6104 * once in a while find out which lists and dicts are not referenced from any
6105 * variable.
6106 *
6107 * Here is a good reference text about garbage collection (refers to Python
6108 * but it applies to all reference-counting mechanisms):
6109 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006110 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006111
6112/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 * Do garbage collection for lists and dicts.
6114 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006115 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006116 int
6117garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006118{
6119 dict_T *dd;
6120 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 int copyID = ++current_copyID;
6122 buf_T *buf;
6123 win_T *wp;
6124 int i;
6125 funccall_T *fc;
6126 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006127#ifdef FEAT_WINDOWS
6128 tabpage_T *tp;
6129#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006130
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006131 /* Only do this once. */
6132 want_garbage_collect = FALSE;
6133 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006134 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006135
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006136 /*
6137 * 1. Go through all accessible variables and mark all lists and dicts
6138 * with copyID.
6139 */
6140 /* script-local variables */
6141 for (i = 1; i <= ga_scripts.ga_len; ++i)
6142 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6143
6144 /* buffer-local variables */
6145 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6146 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6147
6148 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006149 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006150 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6151
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006152#ifdef FEAT_WINDOWS
6153 /* tabpage-local variables */
6154 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6155 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6156#endif
6157
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006158 /* global variables */
6159 set_ref_in_ht(&globvarht, copyID);
6160
6161 /* function-local variables */
6162 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6163 {
6164 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6165 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6166 }
6167
6168 /*
6169 * 2. Go through the list of dicts and free items without the copyID.
6170 */
6171 for (dd = first_dict; dd != NULL; )
6172 if (dd->dv_copyID != copyID)
6173 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006174 /* Free the Dictionary and ordinary items it contains, but don't
6175 * recurse into Lists and Dictionaries, they will be in the list
6176 * of dicts or list of lists. */
6177 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006178 did_free = TRUE;
6179
6180 /* restart, next dict may also have been freed */
6181 dd = first_dict;
6182 }
6183 else
6184 dd = dd->dv_used_next;
6185
6186 /*
6187 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006188 * But don't free a list that has a watcher (used in a for loop), these
6189 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006190 */
6191 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006192 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006193 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006194 /* Free the List and ordinary items it contains, but don't recurse
6195 * into Lists and Dictionaries, they will be in the list of dicts
6196 * or list of lists. */
6197 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006198 did_free = TRUE;
6199
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006200 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006201 ll = first_list;
6202 }
6203 else
6204 ll = ll->lv_used_next;
6205
6206 return did_free;
6207}
6208
6209/*
6210 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6211 */
6212 static void
6213set_ref_in_ht(ht, copyID)
6214 hashtab_T *ht;
6215 int copyID;
6216{
6217 int todo;
6218 hashitem_T *hi;
6219
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006220 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006221 for (hi = ht->ht_array; todo > 0; ++hi)
6222 if (!HASHITEM_EMPTY(hi))
6223 {
6224 --todo;
6225 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6226 }
6227}
6228
6229/*
6230 * Mark all lists and dicts referenced through list "l" with "copyID".
6231 */
6232 static void
6233set_ref_in_list(l, copyID)
6234 list_T *l;
6235 int copyID;
6236{
6237 listitem_T *li;
6238
6239 for (li = l->lv_first; li != NULL; li = li->li_next)
6240 set_ref_in_item(&li->li_tv, copyID);
6241}
6242
6243/*
6244 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6245 */
6246 static void
6247set_ref_in_item(tv, copyID)
6248 typval_T *tv;
6249 int copyID;
6250{
6251 dict_T *dd;
6252 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006253
6254 switch (tv->v_type)
6255 {
6256 case VAR_DICT:
6257 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006258 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006259 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006260 /* Didn't see this dict yet. */
6261 dd->dv_copyID = copyID;
6262 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006263 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006264 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006265
6266 case VAR_LIST:
6267 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006268 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006269 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006270 /* Didn't see this list yet. */
6271 ll->lv_copyID = copyID;
6272 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006273 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006274 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006275 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006276 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006277}
6278
6279/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006280 * Allocate an empty header for a dictionary.
6281 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006282 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283dict_alloc()
6284{
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006289 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006290 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006291 if (first_dict != NULL)
6292 first_dict->dv_used_prev = d;
6293 d->dv_used_next = first_dict;
6294 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006295 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006296
Bram Moolenaar33570922005-01-25 22:26:29 +00006297 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006298 d->dv_lock = 0;
6299 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006300 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006301 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006302 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006303}
6304
6305/*
6306 * Unreference a Dictionary: decrement the reference count and free it when it
6307 * becomes zero.
6308 */
6309 static void
6310dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006312{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006313 if (d != NULL && --d->dv_refcount <= 0)
6314 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006315}
6316
6317/*
6318 * Free a Dictionary, including all items it contains.
6319 * Ignores the reference count.
6320 */
6321 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006322dict_free(d, recurse)
6323 dict_T *d;
6324 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006325{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006326 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006328 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006329
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006330 /* Remove the dict from the list of dicts for garbage collection. */
6331 if (d->dv_used_prev == NULL)
6332 first_dict = d->dv_used_next;
6333 else
6334 d->dv_used_prev->dv_used_next = d->dv_used_next;
6335 if (d->dv_used_next != NULL)
6336 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6337
6338 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006339 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006340 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006343 if (!HASHITEM_EMPTY(hi))
6344 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006345 /* Remove the item before deleting it, just in case there is
6346 * something recursive causing trouble. */
6347 di = HI2DI(hi);
6348 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006349 if (recurse || (di->di_tv.v_type != VAR_LIST
6350 && di->di_tv.v_type != VAR_DICT))
6351 clear_tv(&di->di_tv);
6352 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006353 --todo;
6354 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006355 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006357 vim_free(d);
6358}
6359
6360/*
6361 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006362 * The "key" is copied to the new item.
6363 * Note that the value of the item "di_tv" still needs to be initialized!
6364 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367dictitem_alloc(key)
6368 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006369{
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006372 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006373 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006376 di->di_flags = 0;
6377 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006378 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006379}
6380
6381/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006382 * Make a copy of a Dictionary item.
6383 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387{
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006389
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006390 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6391 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006392 if (di != NULL)
6393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006394 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006395 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006396 copy_tv(&org->di_tv, &di->di_tv);
6397 }
6398 return di;
6399}
6400
6401/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006402 * Remove item "item" from Dictionary "dict" and free it.
6403 */
6404 static void
6405dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 dict_T *dict;
6407 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408{
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412 if (HASHITEM_EMPTY(hi))
6413 EMSG2(_(e_intern2), "dictitem_remove()");
6414 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006416 dictitem_free(item);
6417}
6418
6419/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420 * Free a dict item. Also clears the value.
6421 */
6422 static void
6423dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006424 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426 clear_tv(&item->di_tv);
6427 vim_free(item);
6428}
6429
6430/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006431 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6432 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006433 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006434 * Returns NULL when out of memory.
6435 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006437dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006440 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006441{
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 dict_T *copy;
6443 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006444 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006445 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006446
6447 if (orig == NULL)
6448 return NULL;
6449
6450 copy = dict_alloc();
6451 if (copy != NULL)
6452 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006453 if (copyID != 0)
6454 {
6455 orig->dv_copyID = copyID;
6456 orig->dv_copydict = copy;
6457 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006458 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006459 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006461 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006463 --todo;
6464
6465 di = dictitem_alloc(hi->hi_key);
6466 if (di == NULL)
6467 break;
6468 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 {
6470 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6471 copyID) == FAIL)
6472 {
6473 vim_free(di);
6474 break;
6475 }
6476 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006477 else
6478 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6479 if (dict_add(copy, di) == FAIL)
6480 {
6481 dictitem_free(di);
6482 break;
6483 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006486
Bram Moolenaare9a41262005-01-15 22:18:47 +00006487 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488 if (todo > 0)
6489 {
6490 dict_unref(copy);
6491 copy = NULL;
6492 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006493 }
6494
6495 return copy;
6496}
6497
6498/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006502 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006503dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 dict_T *d;
6505 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506{
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508}
6509
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006511 * Add a number or string entry to dictionary "d".
6512 * When "str" is NULL use number "nr", otherwise use "str".
6513 * Returns FAIL when out of memory and when key already exists.
6514 */
6515 int
6516dict_add_nr_str(d, key, nr, str)
6517 dict_T *d;
6518 char *key;
6519 long nr;
6520 char_u *str;
6521{
6522 dictitem_T *item;
6523
6524 item = dictitem_alloc((char_u *)key);
6525 if (item == NULL)
6526 return FAIL;
6527 item->di_tv.v_lock = 0;
6528 if (str == NULL)
6529 {
6530 item->di_tv.v_type = VAR_NUMBER;
6531 item->di_tv.vval.v_number = nr;
6532 }
6533 else
6534 {
6535 item->di_tv.v_type = VAR_STRING;
6536 item->di_tv.vval.v_string = vim_strsave(str);
6537 }
6538 if (dict_add(d, item) == FAIL)
6539 {
6540 dictitem_free(item);
6541 return FAIL;
6542 }
6543 return OK;
6544}
6545
6546/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006547 * Get the number of items in a Dictionary.
6548 */
6549 static long
6550dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 if (d == NULL)
6554 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006555 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556}
6557
6558/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559 * Find item "key[len]" in Dictionary "d".
6560 * If "len" is negative use strlen(key).
6561 * Returns NULL when not found.
6562 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006564dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006565 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006566 char_u *key;
6567 int len;
6568{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006569#define AKEYLEN 200
6570 char_u buf[AKEYLEN];
6571 char_u *akey;
6572 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006574
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006575 if (len < 0)
6576 akey = key;
6577 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006579 tofree = akey = vim_strnsave(key, len);
6580 if (akey == NULL)
6581 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006582 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006583 else
6584 {
6585 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006586 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006587 akey = buf;
6588 }
6589
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006591 vim_free(tofree);
6592 if (HASHITEM_EMPTY(hi))
6593 return NULL;
6594 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006595}
6596
6597/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006598 * Get a string item from a dictionary.
6599 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006600 * Returns NULL if the entry doesn't exist or out of memory.
6601 */
6602 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006603get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006604 dict_T *d;
6605 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006606 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006607{
6608 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006609 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006610
6611 di = dict_find(d, key, -1);
6612 if (di == NULL)
6613 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006614 s = get_tv_string(&di->di_tv);
6615 if (save && s != NULL)
6616 s = vim_strsave(s);
6617 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006618}
6619
6620/*
6621 * Get a number item from a dictionary.
6622 * Returns 0 if the entry doesn't exist or out of memory.
6623 */
6624 long
6625get_dict_number(d, key)
6626 dict_T *d;
6627 char_u *key;
6628{
6629 dictitem_T *di;
6630
6631 di = dict_find(d, key, -1);
6632 if (di == NULL)
6633 return 0;
6634 return get_tv_number(&di->di_tv);
6635}
6636
6637/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006638 * Return an allocated string with the string representation of a Dictionary.
6639 * May return NULL.
6640 */
6641 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006642dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006643 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006644 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006645{
6646 garray_T ga;
6647 int first = TRUE;
6648 char_u *tofree;
6649 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006650 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006651 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006652 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006653 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006654
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006655 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656 return NULL;
6657 ga_init2(&ga, (int)sizeof(char), 80);
6658 ga_append(&ga, '{');
6659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006660 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006661 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006663 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006665 --todo;
6666
6667 if (first)
6668 first = FALSE;
6669 else
6670 ga_concat(&ga, (char_u *)", ");
6671
6672 tofree = string_quote(hi->hi_key, FALSE);
6673 if (tofree != NULL)
6674 {
6675 ga_concat(&ga, tofree);
6676 vim_free(tofree);
6677 }
6678 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006679 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006680 if (s != NULL)
6681 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006682 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006683 if (s == NULL)
6684 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006687 if (todo > 0)
6688 {
6689 vim_free(ga.ga_data);
6690 return NULL;
6691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006692
6693 ga_append(&ga, '}');
6694 ga_append(&ga, NUL);
6695 return (char_u *)ga.ga_data;
6696}
6697
6698/*
6699 * Allocate a variable for a Dictionary and fill it from "*arg".
6700 * Return OK or FAIL. Returns NOTDONE for {expr}.
6701 */
6702 static int
6703get_dict_tv(arg, rettv, evaluate)
6704 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006705 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006706 int evaluate;
6707{
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 dict_T *d = NULL;
6709 typval_T tvkey;
6710 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00006711 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006712 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006714 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006715
6716 /*
6717 * First check if it's not a curly-braces thing: {expr}.
6718 * Must do this without evaluating, otherwise a function may be called
6719 * twice. Unfortunately this means we need to call eval1() twice for the
6720 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006721 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006722 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006723 if (*start != '}')
6724 {
6725 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6726 return FAIL;
6727 if (*start == '}')
6728 return NOTDONE;
6729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006730
6731 if (evaluate)
6732 {
6733 d = dict_alloc();
6734 if (d == NULL)
6735 return FAIL;
6736 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006737 tvkey.v_type = VAR_UNKNOWN;
6738 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739
6740 *arg = skipwhite(*arg + 1);
6741 while (**arg != '}' && **arg != NUL)
6742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006743 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006744 goto failret;
6745 if (**arg != ':')
6746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006747 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006748 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749 goto failret;
6750 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006751 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006753 key = get_tv_string_buf_chk(&tvkey, buf);
6754 if (key == NULL || *key == NUL)
6755 {
6756 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6757 if (key != NULL)
6758 EMSG(_(e_emptykey));
6759 clear_tv(&tvkey);
6760 goto failret;
6761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006763
6764 *arg = skipwhite(*arg + 1);
6765 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6766 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006767 if (evaluate)
6768 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006769 goto failret;
6770 }
6771 if (evaluate)
6772 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006773 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774 if (item != NULL)
6775 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006776 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006777 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006778 clear_tv(&tv);
6779 goto failret;
6780 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006781 item = dictitem_alloc(key);
6782 clear_tv(&tvkey);
6783 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006784 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006786 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006787 if (dict_add(d, item) == FAIL)
6788 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006789 }
6790 }
6791
6792 if (**arg == '}')
6793 break;
6794 if (**arg != ',')
6795 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006796 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006797 goto failret;
6798 }
6799 *arg = skipwhite(*arg + 1);
6800 }
6801
6802 if (**arg != '}')
6803 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006804 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006805failret:
6806 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006807 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006808 return FAIL;
6809 }
6810
6811 *arg = skipwhite(*arg + 1);
6812 if (evaluate)
6813 {
6814 rettv->v_type = VAR_DICT;
6815 rettv->vval.v_dict = d;
6816 ++d->dv_refcount;
6817 }
6818
6819 return OK;
6820}
6821
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006823 * Return a string with the string representation of a variable.
6824 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006825 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006826 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006827 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006828 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006829 */
6830 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006831echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006832 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006834 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006835 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837 static int recurse = 0;
6838 char_u *r = NULL;
6839
Bram Moolenaar33570922005-01-25 22:26:29 +00006840 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006842 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006843 *tofree = NULL;
6844 return NULL;
6845 }
6846 ++recurse;
6847
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006848 switch (tv->v_type)
6849 {
6850 case VAR_FUNC:
6851 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006852 r = tv->vval.v_string;
6853 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006854
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006855 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006856 if (tv->vval.v_list == NULL)
6857 {
6858 *tofree = NULL;
6859 r = NULL;
6860 }
6861 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6862 {
6863 *tofree = NULL;
6864 r = (char_u *)"[...]";
6865 }
6866 else
6867 {
6868 tv->vval.v_list->lv_copyID = copyID;
6869 *tofree = list2string(tv, copyID);
6870 r = *tofree;
6871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006873
Bram Moolenaar8c711452005-01-14 21:53:12 +00006874 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006875 if (tv->vval.v_dict == NULL)
6876 {
6877 *tofree = NULL;
6878 r = NULL;
6879 }
6880 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6881 {
6882 *tofree = NULL;
6883 r = (char_u *)"{...}";
6884 }
6885 else
6886 {
6887 tv->vval.v_dict->dv_copyID = copyID;
6888 *tofree = dict2string(tv, copyID);
6889 r = *tofree;
6890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006891 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 case VAR_STRING:
6894 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006895 *tofree = NULL;
6896 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006897 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006898
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006899 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006901 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006902 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006903
6904 --recurse;
6905 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006906}
6907
6908/*
6909 * Return a string with the string representation of a variable.
6910 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6911 * "numbuf" is used for a number.
6912 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006913 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006914 */
6915 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006916tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006918 char_u **tofree;
6919 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006920 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006921{
6922 switch (tv->v_type)
6923 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006924 case VAR_FUNC:
6925 *tofree = string_quote(tv->vval.v_string, TRUE);
6926 return *tofree;
6927 case VAR_STRING:
6928 *tofree = string_quote(tv->vval.v_string, FALSE);
6929 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006930 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006931 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006932 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006933 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006934 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006936 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006937 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006938}
6939
6940/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 * Return string "str" in ' quotes, doubling ' characters.
6942 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006943 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006944 */
6945 static char_u *
6946string_quote(str, function)
6947 char_u *str;
6948 int function;
6949{
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006951 char_u *p, *r, *s;
6952
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 len = (function ? 13 : 3);
6954 if (str != NULL)
6955 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006956 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006957 for (p = str; *p != NUL; mb_ptr_adv(p))
6958 if (*p == '\'')
6959 ++len;
6960 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006961 s = r = alloc(len);
6962 if (r != NULL)
6963 {
6964 if (function)
6965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006966 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006967 r += 10;
6968 }
6969 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006971 if (str != NULL)
6972 for (p = str; *p != NUL; )
6973 {
6974 if (*p == '\'')
6975 *r++ = '\'';
6976 MB_COPY_CHAR(p, r);
6977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006979 if (function)
6980 *r++ = ')';
6981 *r++ = NUL;
6982 }
6983 return s;
6984}
6985
6986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 * Get the value of an environment variable.
6988 * "arg" is pointing to the '$'. It is advanced to after the name.
6989 * If the environment variable was not set, silently assume it is empty.
6990 * Always return OK.
6991 */
6992 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006993get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 int evaluate;
6997{
6998 char_u *string = NULL;
6999 int len;
7000 int cc;
7001 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007002 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 ++*arg;
7005 name = *arg;
7006 len = get_env_len(arg);
7007 if (evaluate)
7008 {
7009 if (len != 0)
7010 {
7011 cc = name[len];
7012 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007013 /* first try vim_getenv(), fast for normal environment vars */
7014 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007016 {
7017 if (!mustfree)
7018 string = vim_strsave(string);
7019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 else
7021 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007022 if (mustfree)
7023 vim_free(string);
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 /* next try expanding things like $VIM and ${HOME} */
7026 string = expand_env_save(name - 1);
7027 if (string != NULL && *string == '$')
7028 {
7029 vim_free(string);
7030 string = NULL;
7031 }
7032 }
7033 name[len] = cc;
7034 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007035 rettv->v_type = VAR_STRING;
7036 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
7038
7039 return OK;
7040}
7041
7042/*
7043 * Array with names and number of arguments of all internal functions
7044 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7045 */
7046static struct fst
7047{
7048 char *f_name; /* function name */
7049 char f_min_argc; /* minimal number of arguments */
7050 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007051 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007052 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053} functions[] =
7054{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007055 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {"append", 2, 2, f_append},
7057 {"argc", 0, 0, f_argc},
7058 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007059 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007061 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {"bufexists", 1, 1, f_bufexists},
7063 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7064 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7065 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7066 {"buflisted", 1, 1, f_buflisted},
7067 {"bufloaded", 1, 1, f_bufloaded},
7068 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007069 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {"bufwinnr", 1, 1, f_bufwinnr},
7071 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007072 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007073 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007074 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {"char2nr", 1, 1, f_char2nr},
7076 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007077 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007079#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007080 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007081 {"complete_add", 1, 1, f_complete_add},
7082 {"complete_check", 0, 0, f_complete_check},
7083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007085 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007086 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007088 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007089 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 {"delete", 1, 1, f_delete},
7091 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007092 {"diff_filler", 1, 1, f_diff_filler},
7093 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007094 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007096 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"eventhandler", 0, 0, f_eventhandler},
7098 {"executable", 1, 1, f_executable},
7099 {"exists", 1, 1, f_exists},
7100 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007101 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007102 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7104 {"filereadable", 1, 1, f_filereadable},
7105 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007106 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007107 {"finddir", 1, 3, f_finddir},
7108 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {"fnamemodify", 2, 2, f_fnamemodify},
7110 {"foldclosed", 1, 1, f_foldclosed},
7111 {"foldclosedend", 1, 1, f_foldclosedend},
7112 {"foldlevel", 1, 1, f_foldlevel},
7113 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007114 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007116 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007117 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007118 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007119 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {"getbufvar", 2, 2, f_getbufvar},
7121 {"getchar", 0, 1, f_getchar},
7122 {"getcharmod", 0, 0, f_getcharmod},
7123 {"getcmdline", 0, 0, f_getcmdline},
7124 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007125 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007127 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007128 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {"getfsize", 1, 1, f_getfsize},
7130 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007131 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007132 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007133 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007134 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007135 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007136 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007137 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007139 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {"getwinposx", 0, 0, f_getwinposx},
7141 {"getwinposy", 0, 0, f_getwinposy},
7142 {"getwinvar", 2, 2, f_getwinvar},
7143 {"glob", 1, 1, f_glob},
7144 {"globpath", 2, 2, f_globpath},
7145 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007147 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007148 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7150 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7151 {"histadd", 2, 2, f_histadd},
7152 {"histdel", 1, 2, f_histdel},
7153 {"histget", 1, 2, f_histget},
7154 {"histnr", 1, 1, f_histnr},
7155 {"hlID", 1, 1, f_hlID},
7156 {"hlexists", 1, 1, f_hlexists},
7157 {"hostname", 0, 0, f_hostname},
7158 {"iconv", 3, 3, f_iconv},
7159 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007160 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007161 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007163 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {"inputrestore", 0, 0, f_inputrestore},
7165 {"inputsave", 0, 0, f_inputsave},
7166 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007167 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007169 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007171 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007174 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {"libcall", 3, 3, f_libcall},
7176 {"libcallnr", 3, 3, f_libcallnr},
7177 {"line", 1, 1, f_line},
7178 {"line2byte", 1, 1, f_line2byte},
7179 {"lispindent", 1, 1, f_lispindent},
7180 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007181 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007182 {"maparg", 1, 3, f_maparg},
7183 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007184 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007185 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007186 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007187 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007188 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007189 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007190 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007191 {"max", 1, 1, f_max},
7192 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007193#ifdef vim_mkdir
7194 {"mkdir", 1, 3, f_mkdir},
7195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 {"mode", 0, 0, f_mode},
7197 {"nextnonblank", 1, 1, f_nextnonblank},
7198 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007199 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007201 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007202 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007204 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007205 {"reltime", 0, 2, f_reltime},
7206 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {"remote_expr", 2, 3, f_remote_expr},
7208 {"remote_foreground", 1, 1, f_remote_foreground},
7209 {"remote_peek", 1, 2, f_remote_peek},
7210 {"remote_read", 1, 1, f_remote_read},
7211 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007212 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007214 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007216 {"reverse", 1, 1, f_reverse},
Bram Moolenaar76929292008-01-06 19:07:36 +00007217 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007218 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007219 {"searchpair", 3, 7, f_searchpair},
7220 {"searchpairpos", 3, 7, f_searchpairpos},
7221 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {"server2client", 2, 2, f_server2client},
7223 {"serverlist", 0, 0, f_serverlist},
7224 {"setbufvar", 3, 3, f_setbufvar},
7225 {"setcmdpos", 1, 1, f_setcmdpos},
7226 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007227 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007228 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007229 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007230 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007232 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007234 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007236 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007237 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007238 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007239 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007240 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007241 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242#ifdef HAVE_STRFTIME
7243 {"strftime", 1, 2, f_strftime},
7244#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007246 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {"strlen", 1, 1, f_strlen},
7248 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007249 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {"strtrans", 1, 1, f_strtrans},
7251 {"submatch", 1, 1, f_submatch},
7252 {"substitute", 4, 4, f_substitute},
7253 {"synID", 3, 3, f_synID},
7254 {"synIDattr", 2, 3, f_synIDattr},
7255 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007256 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007257 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007258 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007259 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007260 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007261 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007262 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007264 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {"tolower", 1, 1, f_tolower},
7266 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007267 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {"virtcol", 1, 1, f_virtcol},
7271 {"visualmode", 0, 1, f_visualmode},
7272 {"winbufnr", 1, 1, f_winbufnr},
7273 {"wincol", 0, 0, f_wincol},
7274 {"winheight", 1, 1, f_winheight},
7275 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007276 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007278 {"winrestview", 1, 1, f_winrestview},
7279 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007281 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282};
7283
7284#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7285
7286/*
7287 * Function given to ExpandGeneric() to obtain the list of internal
7288 * or user defined function names.
7289 */
7290 char_u *
7291get_function_name(xp, idx)
7292 expand_T *xp;
7293 int idx;
7294{
7295 static int intidx = -1;
7296 char_u *name;
7297
7298 if (idx == 0)
7299 intidx = -1;
7300 if (intidx < 0)
7301 {
7302 name = get_user_func_name(xp, idx);
7303 if (name != NULL)
7304 return name;
7305 }
7306 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7307 {
7308 STRCPY(IObuff, functions[intidx].f_name);
7309 STRCAT(IObuff, "(");
7310 if (functions[intidx].f_max_argc == 0)
7311 STRCAT(IObuff, ")");
7312 return IObuff;
7313 }
7314
7315 return NULL;
7316}
7317
7318/*
7319 * Function given to ExpandGeneric() to obtain the list of internal or
7320 * user defined variable or function names.
7321 */
7322/*ARGSUSED*/
7323 char_u *
7324get_expr_name(xp, idx)
7325 expand_T *xp;
7326 int idx;
7327{
7328 static int intidx = -1;
7329 char_u *name;
7330
7331 if (idx == 0)
7332 intidx = -1;
7333 if (intidx < 0)
7334 {
7335 name = get_function_name(xp, idx);
7336 if (name != NULL)
7337 return name;
7338 }
7339 return get_user_var_name(xp, ++intidx);
7340}
7341
7342#endif /* FEAT_CMDL_COMPL */
7343
7344/*
7345 * Find internal function in table above.
7346 * Return index, or -1 if not found
7347 */
7348 static int
7349find_internal_func(name)
7350 char_u *name; /* name of the function */
7351{
7352 int first = 0;
7353 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7354 int cmp;
7355 int x;
7356
7357 /*
7358 * Find the function name in the table. Binary search.
7359 */
7360 while (first <= last)
7361 {
7362 x = first + ((unsigned)(last - first) >> 1);
7363 cmp = STRCMP(name, functions[x].f_name);
7364 if (cmp < 0)
7365 last = x - 1;
7366 else if (cmp > 0)
7367 first = x + 1;
7368 else
7369 return x;
7370 }
7371 return -1;
7372}
7373
7374/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007375 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7376 * name it contains, otherwise return "name".
7377 */
7378 static char_u *
7379deref_func_name(name, lenp)
7380 char_u *name;
7381 int *lenp;
7382{
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007384 int cc;
7385
7386 cc = name[*lenp];
7387 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007388 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007389 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007393 {
7394 *lenp = 0;
7395 return (char_u *)""; /* just in case */
7396 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007397 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007399 }
7400
7401 return name;
7402}
7403
7404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 * Allocate a variable for the result of a function.
7406 * Return OK or FAIL.
7407 */
7408 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7410 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 char_u *name; /* name of the function */
7412 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 char_u **arg; /* argument, pointing to the '(' */
7415 linenr_T firstline; /* first line of range */
7416 linenr_T lastline; /* last line of range */
7417 int *doesrange; /* return: function handled range */
7418 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 char_u *argp;
7422 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007423 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 int argcount = 0; /* number of arguments found */
7425
7426 /*
7427 * Get the arguments.
7428 */
7429 argp = *arg;
7430 while (argcount < MAX_FUNC_ARGS)
7431 {
7432 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7433 if (*argp == ')' || *argp == ',' || *argp == NUL)
7434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7436 {
7437 ret = FAIL;
7438 break;
7439 }
7440 ++argcount;
7441 if (*argp != ',')
7442 break;
7443 }
7444 if (*argp == ')')
7445 ++argp;
7446 else
7447 ret = FAIL;
7448
7449 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007451 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 {
7454 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007455 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007457 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
7460 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007461 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462
7463 *arg = skipwhite(argp);
7464 return ret;
7465}
7466
7467
7468/*
7469 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007470 * Return OK when the function can't be called, FAIL otherwise.
7471 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 */
7473 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007474call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 char_u *name; /* name of the function */
7477 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007480 typval_T *argvars; /* vars for arguments, must have "argcount"
7481 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 linenr_T firstline; /* first line of range */
7483 linenr_T lastline; /* last line of range */
7484 int *doesrange; /* return: function handled range */
7485 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
7488 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489#define ERROR_UNKNOWN 0
7490#define ERROR_TOOMANY 1
7491#define ERROR_TOOFEW 2
7492#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493#define ERROR_DICT 4
7494#define ERROR_NONE 5
7495#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 int error = ERROR_NONE;
7497 int i;
7498 int llen;
7499 ufunc_T *fp;
7500 int cc;
7501#define FLEN_FIXED 40
7502 char_u fname_buf[FLEN_FIXED + 1];
7503 char_u *fname;
7504
7505 /*
7506 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7507 * Change <SNR>123_name() to K_SNR 123_name().
7508 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7509 */
7510 cc = name[len];
7511 name[len] = NUL;
7512 llen = eval_fname_script(name);
7513 if (llen > 0)
7514 {
7515 fname_buf[0] = K_SPECIAL;
7516 fname_buf[1] = KS_EXTRA;
7517 fname_buf[2] = (int)KE_SNR;
7518 i = 3;
7519 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7520 {
7521 if (current_SID <= 0)
7522 error = ERROR_SCRIPT;
7523 else
7524 {
7525 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7526 i = (int)STRLEN(fname_buf);
7527 }
7528 }
7529 if (i + STRLEN(name + llen) < FLEN_FIXED)
7530 {
7531 STRCPY(fname_buf + i, name + llen);
7532 fname = fname_buf;
7533 }
7534 else
7535 {
7536 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7537 if (fname == NULL)
7538 error = ERROR_OTHER;
7539 else
7540 {
7541 mch_memmove(fname, fname_buf, (size_t)i);
7542 STRCPY(fname + i, name + llen);
7543 }
7544 }
7545 }
7546 else
7547 fname = name;
7548
7549 *doesrange = FALSE;
7550
7551
7552 /* execute the function if no errors detected and executing */
7553 if (evaluate && error == ERROR_NONE)
7554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 error = ERROR_UNKNOWN;
7557
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007558 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
7560 /*
7561 * User defined function.
7562 */
7563 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007564
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007566 /* Trigger FuncUndefined event, may load the function. */
7567 if (fp == NULL
7568 && apply_autocmds(EVENT_FUNCUNDEFINED,
7569 fname, fname, TRUE, NULL)
7570 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007572 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 fp = find_func(fname);
7574 }
7575#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007576 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007577 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007578 {
7579 /* loaded a package, search for the function again */
7580 fp = find_func(fname);
7581 }
7582
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 if (fp != NULL)
7584 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007585 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007587 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007589 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007591 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007592 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 else
7594 {
7595 /*
7596 * Call the user function.
7597 * Save and restore search patterns, script variables and
7598 * redo buffer.
7599 */
7600 save_search_patterns();
7601 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007602 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007603 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007604 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007605 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7606 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7607 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007608 /* Function was unreferenced while being used, free it
7609 * now. */
7610 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 restoreRedobuff();
7612 restore_search_patterns();
7613 error = ERROR_NONE;
7614 }
7615 }
7616 }
7617 else
7618 {
7619 /*
7620 * Find the function name in the table, call its implementation.
7621 */
7622 i = find_internal_func(fname);
7623 if (i >= 0)
7624 {
7625 if (argcount < functions[i].f_min_argc)
7626 error = ERROR_TOOFEW;
7627 else if (argcount > functions[i].f_max_argc)
7628 error = ERROR_TOOMANY;
7629 else
7630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007632 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 error = ERROR_NONE;
7634 }
7635 }
7636 }
7637 /*
7638 * The function call (or "FuncUndefined" autocommand sequence) might
7639 * have been aborted by an error, an interrupt, or an explicitly thrown
7640 * exception that has not been caught so far. This situation can be
7641 * tested for by calling aborting(). For an error in an internal
7642 * function or for the "E132" error in call_user_func(), however, the
7643 * throw point at which the "force_abort" flag (temporarily reset by
7644 * emsg()) is normally updated has not been reached yet. We need to
7645 * update that flag first to make aborting() reliable.
7646 */
7647 update_force_abort();
7648 }
7649 if (error == ERROR_NONE)
7650 ret = OK;
7651
7652 /*
7653 * Report an error unless the argument evaluation or function call has been
7654 * cancelled due to an aborting error, an interrupt, or an exception.
7655 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 if (!aborting())
7657 {
7658 switch (error)
7659 {
7660 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007661 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 break;
7663 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 break;
7666 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007667 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 name);
7669 break;
7670 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007671 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 name);
7673 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007674 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007675 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 name);
7677 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 }
7679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
7681 name[len] = cc;
7682 if (fname != name && fname != fname_buf)
7683 vim_free(fname);
7684
7685 return ret;
7686}
7687
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007688/*
7689 * Give an error message with a function name. Handle <SNR> things.
7690 */
7691 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007692emsg_funcname(ermsg, name)
7693 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007694 char_u *name;
7695{
7696 char_u *p;
7697
7698 if (*name == K_SPECIAL)
7699 p = concat_str((char_u *)"<SNR>", name + 3);
7700 else
7701 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007702 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007703 if (p != name)
7704 vim_free(p);
7705}
7706
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707/*********************************************
7708 * Implementation of the built-in functions
7709 */
7710
7711/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007712 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 */
7714 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007715f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 typval_T *argvars;
7717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007721 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007724 if ((l = argvars[0].vval.v_list) != NULL
7725 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7726 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007727 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728 }
7729 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007730 EMSG(_(e_listreq));
7731}
7732
7733/*
7734 * "append(lnum, string/list)" function
7735 */
7736 static void
7737f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 typval_T *argvars;
7739 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007740{
7741 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 list_T *l = NULL;
7744 listitem_T *li = NULL;
7745 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007746 long added = 0;
7747
Bram Moolenaar0d660222005-01-07 21:51:51 +00007748 lnum = get_tv_lnum(argvars);
7749 if (lnum >= 0
7750 && lnum <= curbuf->b_ml.ml_line_count
7751 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007753 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007754 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007755 l = argvars[1].vval.v_list;
7756 if (l == NULL)
7757 return;
7758 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007759 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007760 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007761 for (;;)
7762 {
7763 if (l == NULL)
7764 tv = &argvars[1]; /* append a string */
7765 else if (li == NULL)
7766 break; /* end of list */
7767 else
7768 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769 line = get_tv_string_chk(tv);
7770 if (line == NULL) /* type error */
7771 {
7772 rettv->vval.v_number = 1; /* Failed */
7773 break;
7774 }
7775 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007776 ++added;
7777 if (l == NULL)
7778 break;
7779 li = li->li_next;
7780 }
7781
7782 appended_lines_mark(lnum, added);
7783 if (curwin->w_cursor.lnum > lnum)
7784 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007786 else
7787 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788}
7789
7790/*
7791 * "argc()" function
7792 */
7793/* ARGSUSED */
7794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T *argvars;
7797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007799 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800}
7801
7802/*
7803 * "argidx()" function
7804 */
7805/* ARGSUSED */
7806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *argvars;
7809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812}
7813
7814/*
7815 * "argv(nr)" function
7816 */
7817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007818f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007819 typval_T *argvars;
7820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
7822 int idx;
7823
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007824 if (argvars[0].v_type != VAR_UNKNOWN)
7825 {
7826 idx = get_tv_number_chk(&argvars[0], NULL);
7827 if (idx >= 0 && idx < ARGCOUNT)
7828 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7829 else
7830 rettv->vval.v_string = NULL;
7831 rettv->v_type = VAR_STRING;
7832 }
7833 else if (rettv_list_alloc(rettv) == OK)
7834 for (idx = 0; idx < ARGCOUNT; ++idx)
7835 list_append_string(rettv->vval.v_list,
7836 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837}
7838
7839/*
7840 * "browse(save, title, initdir, default)" function
7841 */
7842/* ARGSUSED */
7843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 typval_T *argvars;
7846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848#ifdef FEAT_BROWSE
7849 int save;
7850 char_u *title;
7851 char_u *initdir;
7852 char_u *defname;
7853 char_u buf[NUMBUFLEN];
7854 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007857 save = get_tv_number_chk(&argvars[0], &error);
7858 title = get_tv_string_chk(&argvars[1]);
7859 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7860 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007862 if (error || title == NULL || initdir == NULL || defname == NULL)
7863 rettv->vval.v_string = NULL;
7864 else
7865 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007866 do_browse(save ? BROWSE_SAVE : 0,
7867 title, defname, NULL, initdir, NULL, curbuf);
7868#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007869 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007870#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007872}
7873
7874/*
7875 * "browsedir(title, initdir)" function
7876 */
7877/* ARGSUSED */
7878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 typval_T *argvars;
7881 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007882{
7883#ifdef FEAT_BROWSE
7884 char_u *title;
7885 char_u *initdir;
7886 char_u buf[NUMBUFLEN];
7887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007888 title = get_tv_string_chk(&argvars[0]);
7889 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007891 if (title == NULL || initdir == NULL)
7892 rettv->vval.v_string = NULL;
7893 else
7894 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007895 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007899 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900}
7901
Bram Moolenaar33570922005-01-25 22:26:29 +00007902static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007903
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904/*
7905 * Find a buffer by number or exact name.
7906 */
7907 static buf_T *
7908find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
7911 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 if (avar->v_type == VAR_NUMBER)
7914 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007915 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007918 if (buf == NULL)
7919 {
7920 /* No full path name match, try a match with a URL or a "nofile"
7921 * buffer, these don't use the full path. */
7922 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7923 if (buf->b_fname != NULL
7924 && (path_with_url(buf->b_fname)
7925#ifdef FEAT_QUICKFIX
7926 || bt_nofile(buf)
7927#endif
7928 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007929 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007930 break;
7931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 }
7933 return buf;
7934}
7935
7936/*
7937 * "bufexists(expr)" function
7938 */
7939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 typval_T *argvars;
7942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945}
7946
7947/*
7948 * "buflisted(expr)" function
7949 */
7950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 typval_T *argvars;
7953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 buf_T *buf;
7956
7957 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959}
7960
7961/*
7962 * "bufloaded(expr)" function
7963 */
7964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 typval_T *argvars;
7967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 buf_T *buf;
7970
7971 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973}
7974
Bram Moolenaar33570922005-01-25 22:26:29 +00007975static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007976
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977/*
7978 * Get buffer by number or pattern.
7979 */
7980 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 int save_magic;
7986 char_u *save_cpo;
7987 buf_T *buf;
7988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007989 if (tv->v_type == VAR_NUMBER)
7990 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007991 if (tv->v_type != VAR_STRING)
7992 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (name == NULL || *name == NUL)
7994 return curbuf;
7995 if (name[0] == '$' && name[1] == NUL)
7996 return lastbuf;
7997
7998 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7999 save_magic = p_magic;
8000 p_magic = TRUE;
8001 save_cpo = p_cpo;
8002 p_cpo = (char_u *)"";
8003
8004 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8005 TRUE, FALSE));
8006
8007 p_magic = save_magic;
8008 p_cpo = save_cpo;
8009
8010 /* If not found, try expanding the name, like done for bufexists(). */
8011 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
8014 return buf;
8015}
8016
8017/*
8018 * "bufname(expr)" function
8019 */
8020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008021f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008022 typval_T *argvars;
8023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 buf_T *buf;
8026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008027 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 buf = get_buf_tv(&argvars[0]);
8030 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008034 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 --emsg_off;
8036}
8037
8038/*
8039 * "bufnr(expr)" function
8040 */
8041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008042f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008043 typval_T *argvars;
8044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
8046 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008047 int error = FALSE;
8048 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008050 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008052 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008053 --emsg_off;
8054
8055 /* If the buffer isn't found and the second argument is not zero create a
8056 * new buffer. */
8057 if (buf == NULL
8058 && argvars[1].v_type != VAR_UNKNOWN
8059 && get_tv_number_chk(&argvars[1], &error) != 0
8060 && !error
8061 && (name = get_tv_string_chk(&argvars[0])) != NULL
8062 && !error)
8063 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8064
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008066 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008068 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069}
8070
8071/*
8072 * "bufwinnr(nr)" function
8073 */
8074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008075f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 typval_T *argvars;
8077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078{
8079#ifdef FEAT_WINDOWS
8080 win_T *wp;
8081 int winnr = 0;
8082#endif
8083 buf_T *buf;
8084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008085 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008087 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088#ifdef FEAT_WINDOWS
8089 for (wp = firstwin; wp; wp = wp->w_next)
8090 {
8091 ++winnr;
8092 if (wp->w_buffer == buf)
8093 break;
8094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008095 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098#endif
8099 --emsg_off;
8100}
8101
8102/*
8103 * "byte2line(byte)" function
8104 */
8105/*ARGSUSED*/
8106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008107f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 typval_T *argvars;
8109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
8111#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008112 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113#else
8114 long boff = 0;
8115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008116 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008120 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 (linenr_T)0, &boff);
8122#endif
8123}
8124
8125/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008126 * "byteidx()" function
8127 */
8128/*ARGSUSED*/
8129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008130f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008131 typval_T *argvars;
8132 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008133{
8134#ifdef FEAT_MBYTE
8135 char_u *t;
8136#endif
8137 char_u *str;
8138 long idx;
8139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008140 str = get_tv_string_chk(&argvars[0]);
8141 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008142 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008143 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008144 return;
8145
8146#ifdef FEAT_MBYTE
8147 t = str;
8148 for ( ; idx > 0; idx--)
8149 {
8150 if (*t == NUL) /* EOL reached */
8151 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008152 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008153 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008154 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008155#else
8156 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008158#endif
8159}
8160
8161/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008162 * "call(func, arglist)" function
8163 */
8164 static void
8165f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008166 typval_T *argvars;
8167 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008168{
8169 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008170 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008171 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008173 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008175
8176 rettv->vval.v_number = 0;
8177 if (argvars[1].v_type != VAR_LIST)
8178 {
8179 EMSG(_(e_listreq));
8180 return;
8181 }
8182 if (argvars[1].vval.v_list == NULL)
8183 return;
8184
8185 if (argvars[0].v_type == VAR_FUNC)
8186 func = argvars[0].vval.v_string;
8187 else
8188 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008189 if (*func == NUL)
8190 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008191
Bram Moolenaare9a41262005-01-15 22:18:47 +00008192 if (argvars[2].v_type != VAR_UNKNOWN)
8193 {
8194 if (argvars[2].v_type != VAR_DICT)
8195 {
8196 EMSG(_(e_dictreq));
8197 return;
8198 }
8199 selfdict = argvars[2].vval.v_dict;
8200 }
8201
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008202 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8203 item = item->li_next)
8204 {
8205 if (argc == MAX_FUNC_ARGS)
8206 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008207 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008208 break;
8209 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008210 /* Make a copy of each argument. This is needed to be able to set
8211 * v_lock to VAR_FIXED in the copy without changing the original list.
8212 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008213 copy_tv(&item->li_tv, &argv[argc++]);
8214 }
8215
8216 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008217 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8219 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008220
8221 /* Free the arguments. */
8222 while (argc > 0)
8223 clear_tv(&argv[--argc]);
8224}
8225
8226/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008227 * "changenr()" function
8228 */
8229/*ARGSUSED*/
8230 static void
8231f_changenr(argvars, rettv)
8232 typval_T *argvars;
8233 typval_T *rettv;
8234{
8235 rettv->vval.v_number = curbuf->b_u_seq_cur;
8236}
8237
8238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 * "char2nr(string)" function
8240 */
8241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 typval_T *argvars;
8244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
8246#ifdef FEAT_MBYTE
8247 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008248 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 else
8250#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252}
8253
8254/*
8255 * "cindent(lnum)" function
8256 */
8257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008258f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008259 typval_T *argvars;
8260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262#ifdef FEAT_CINDENT
8263 pos_T pos;
8264 linenr_T lnum;
8265
8266 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8269 {
8270 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 curwin->w_cursor = pos;
8273 }
8274 else
8275#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008276 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277}
8278
8279/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008280 * "clearmatches()" function
8281 */
8282/*ARGSUSED*/
8283 static void
8284f_clearmatches(argvars, rettv)
8285 typval_T *argvars;
8286 typval_T *rettv;
8287{
8288#ifdef FEAT_SEARCH_EXTRA
8289 clear_matches(curwin);
8290#endif
8291}
8292
8293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 * "col(string)" function
8295 */
8296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008297f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 typval_T *argvars;
8299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 colnr_T col = 0;
8302 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008303 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008305 fp = var2fpos(&argvars[0], FALSE, &fnum);
8306 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
8308 if (fp->col == MAXCOL)
8309 {
8310 /* '> can be MAXCOL, get the length of the line then */
8311 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008312 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 else
8314 col = MAXCOL;
8315 }
8316 else
8317 {
8318 col = fp->col + 1;
8319#ifdef FEAT_VIRTUALEDIT
8320 /* col(".") when the cursor is on the NUL at the end of the line
8321 * because of "coladd" can be seen as an extra column. */
8322 if (virtual_active() && fp == &curwin->w_cursor)
8323 {
8324 char_u *p = ml_get_cursor();
8325
8326 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8327 curwin->w_virtcol - curwin->w_cursor.coladd))
8328 {
8329# ifdef FEAT_MBYTE
8330 int l;
8331
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008332 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 col += l;
8334# else
8335 if (*p != NUL && p[1] == NUL)
8336 ++col;
8337# endif
8338 }
8339 }
8340#endif
8341 }
8342 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344}
8345
Bram Moolenaar572cb562005-08-05 21:35:02 +00008346#if defined(FEAT_INS_EXPAND)
8347/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008348 * "complete()" function
8349 */
8350/*ARGSUSED*/
8351 static void
8352f_complete(argvars, rettv)
8353 typval_T *argvars;
8354 typval_T *rettv;
8355{
8356 int startcol;
8357
8358 if ((State & INSERT) == 0)
8359 {
8360 EMSG(_("E785: complete() can only be used in Insert mode"));
8361 return;
8362 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008363
8364 /* Check for undo allowed here, because if something was already inserted
8365 * the line was already saved for undo and this check isn't done. */
8366 if (!undo_allowed())
8367 return;
8368
Bram Moolenaarade00832006-03-10 21:46:58 +00008369 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8370 {
8371 EMSG(_(e_invarg));
8372 return;
8373 }
8374
8375 startcol = get_tv_number_chk(&argvars[0], NULL);
8376 if (startcol <= 0)
8377 return;
8378
8379 set_completion(startcol - 1, argvars[1].vval.v_list);
8380}
8381
8382/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008383 * "complete_add()" function
8384 */
8385/*ARGSUSED*/
8386 static void
8387f_complete_add(argvars, rettv)
8388 typval_T *argvars;
8389 typval_T *rettv;
8390{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008391 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008392}
8393
8394/*
8395 * "complete_check()" function
8396 */
8397/*ARGSUSED*/
8398 static void
8399f_complete_check(argvars, rettv)
8400 typval_T *argvars;
8401 typval_T *rettv;
8402{
8403 int saved = RedrawingDisabled;
8404
8405 RedrawingDisabled = 0;
8406 ins_compl_check_keys(0);
8407 rettv->vval.v_number = compl_interrupted;
8408 RedrawingDisabled = saved;
8409}
8410#endif
8411
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412/*
8413 * "confirm(message, buttons[, default [, type]])" function
8414 */
8415/*ARGSUSED*/
8416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 typval_T *argvars;
8419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
8421#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8422 char_u *message;
8423 char_u *buttons = NULL;
8424 char_u buf[NUMBUFLEN];
8425 char_u buf2[NUMBUFLEN];
8426 int def = 1;
8427 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008428 char_u *typestr;
8429 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 message = get_tv_string_chk(&argvars[0]);
8432 if (message == NULL)
8433 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008436 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8437 if (buttons == NULL)
8438 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008439 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008442 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008444 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8445 if (typestr == NULL)
8446 error = TRUE;
8447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008449 switch (TOUPPER_ASC(*typestr))
8450 {
8451 case 'E': type = VIM_ERROR; break;
8452 case 'Q': type = VIM_QUESTION; break;
8453 case 'I': type = VIM_INFO; break;
8454 case 'W': type = VIM_WARNING; break;
8455 case 'G': type = VIM_GENERIC; break;
8456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 }
8458 }
8459 }
8460 }
8461
8462 if (buttons == NULL || *buttons == NUL)
8463 buttons = (char_u *)_("&Ok");
8464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008465 if (error)
8466 rettv->vval.v_number = 0;
8467 else
8468 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 def, NULL);
8470#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472#endif
8473}
8474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475/*
8476 * "copy()" function
8477 */
8478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008479f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008480 typval_T *argvars;
8481 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008482{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008483 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485
8486/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008487 * "count()" function
8488 */
8489 static void
8490f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *argvars;
8492 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008493{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008494 long n = 0;
8495 int ic = FALSE;
8496
Bram Moolenaare9a41262005-01-15 22:18:47 +00008497 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008498 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 listitem_T *li;
8500 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008502
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 if ((l = argvars[0].vval.v_list) != NULL)
8504 {
8505 li = l->lv_first;
8506 if (argvars[2].v_type != VAR_UNKNOWN)
8507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008508 int error = FALSE;
8509
8510 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008511 if (argvars[3].v_type != VAR_UNKNOWN)
8512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008513 idx = get_tv_number_chk(&argvars[3], &error);
8514 if (!error)
8515 {
8516 li = list_find(l, idx);
8517 if (li == NULL)
8518 EMSGN(_(e_listidx), idx);
8519 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008521 if (error)
8522 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 }
8524
8525 for ( ; li != NULL; li = li->li_next)
8526 if (tv_equal(&li->li_tv, &argvars[1], ic))
8527 ++n;
8528 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008529 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008530 else if (argvars[0].v_type == VAR_DICT)
8531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 int todo;
8533 dict_T *d;
8534 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008535
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008536 if ((d = argvars[0].vval.v_dict) != NULL)
8537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008538 int error = FALSE;
8539
Bram Moolenaare9a41262005-01-15 22:18:47 +00008540 if (argvars[2].v_type != VAR_UNKNOWN)
8541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008542 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008543 if (argvars[3].v_type != VAR_UNKNOWN)
8544 EMSG(_(e_invarg));
8545 }
8546
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008547 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008549 {
8550 if (!HASHITEM_EMPTY(hi))
8551 {
8552 --todo;
8553 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8554 ++n;
8555 }
8556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557 }
8558 }
8559 else
8560 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008561 rettv->vval.v_number = n;
8562}
8563
8564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8566 *
8567 * Checks the existence of a cscope connection.
8568 */
8569/*ARGSUSED*/
8570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *argvars;
8573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575#ifdef FEAT_CSCOPE
8576 int num = 0;
8577 char_u *dbpath = NULL;
8578 char_u *prepend = NULL;
8579 char_u buf[NUMBUFLEN];
8580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 if (argvars[0].v_type != VAR_UNKNOWN
8582 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 num = (int)get_tv_number(&argvars[0]);
8585 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008586 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#endif
8594}
8595
8596/*
8597 * "cursor(lnum, col)" function
8598 *
8599 * Moves the cursor to the specified line and column
8600 */
8601/*ARGSUSED*/
8602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 typval_T *argvars;
8605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008608#ifdef FEAT_VIRTUALEDIT
8609 long coladd = 0;
8610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
Bram Moolenaara5525202006-03-02 22:52:09 +00008612 if (argvars[1].v_type == VAR_UNKNOWN)
8613 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008614 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008615
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008616 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008617 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008618 line = pos.lnum;
8619 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008620#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008621 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008622#endif
8623 }
8624 else
8625 {
8626 line = get_tv_lnum(argvars);
8627 col = get_tv_number_chk(&argvars[1], NULL);
8628#ifdef FEAT_VIRTUALEDIT
8629 if (argvars[2].v_type != VAR_UNKNOWN)
8630 coladd = get_tv_number_chk(&argvars[2], NULL);
8631#endif
8632 }
8633 if (line < 0 || col < 0
8634#ifdef FEAT_VIRTUALEDIT
8635 || coladd < 0
8636#endif
8637 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 if (line > 0)
8640 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 if (col > 0)
8642 curwin->w_cursor.col = col - 1;
8643#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008644 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#endif
8646
8647 /* Make sure the cursor is in a valid position. */
8648 check_cursor();
8649#ifdef FEAT_MBYTE
8650 /* Correct cursor for multi-byte character. */
8651 if (has_mbyte)
8652 mb_adjust_cursor();
8653#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008654
8655 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656}
8657
8658/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 */
8661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008662f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008663 typval_T *argvars;
8664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008666 int noref = 0;
8667
8668 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008669 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008670 if (noref < 0 || noref > 1)
8671 EMSG(_(e_invarg));
8672 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008673 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674}
8675
8676/*
8677 * "delete()" function
8678 */
8679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 typval_T *argvars;
8682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
8684 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688}
8689
8690/*
8691 * "did_filetype()" function
8692 */
8693/*ARGSUSED*/
8694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 typval_T *argvars;
8697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#endif
8704}
8705
8706/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008707 * "diff_filler()" function
8708 */
8709/*ARGSUSED*/
8710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008712 typval_T *argvars;
8713 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008714{
8715#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008717#endif
8718}
8719
8720/*
8721 * "diff_hlID()" function
8722 */
8723/*ARGSUSED*/
8724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *argvars;
8727 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008728{
8729#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008731 static linenr_T prev_lnum = 0;
8732 static int changedtick = 0;
8733 static int fnum = 0;
8734 static int change_start = 0;
8735 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00008736 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008737 int filler_lines;
8738 int col;
8739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 if (lnum < 0) /* ignore type error in {lnum} arg */
8741 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008742 if (lnum != prev_lnum
8743 || changedtick != curbuf->b_changedtick
8744 || fnum != curbuf->b_fnum)
8745 {
8746 /* New line, buffer, change: need to get the values. */
8747 filler_lines = diff_check(curwin, lnum);
8748 if (filler_lines < 0)
8749 {
8750 if (filler_lines == -1)
8751 {
8752 change_start = MAXCOL;
8753 change_end = -1;
8754 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8755 hlID = HLF_ADD; /* added line */
8756 else
8757 hlID = HLF_CHD; /* changed line */
8758 }
8759 else
8760 hlID = HLF_ADD; /* added line */
8761 }
8762 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008763 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008764 prev_lnum = lnum;
8765 changedtick = curbuf->b_changedtick;
8766 fnum = curbuf->b_fnum;
8767 }
8768
8769 if (hlID == HLF_CHD || hlID == HLF_TXD)
8770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008772 if (col >= change_start && col <= change_end)
8773 hlID = HLF_TXD; /* changed text */
8774 else
8775 hlID = HLF_CHD; /* changed line */
8776 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008777 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008778#endif
8779}
8780
8781/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008782 * "empty({expr})" function
8783 */
8784 static void
8785f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 typval_T *argvars;
8787 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008788{
8789 int n;
8790
8791 switch (argvars[0].v_type)
8792 {
8793 case VAR_STRING:
8794 case VAR_FUNC:
8795 n = argvars[0].vval.v_string == NULL
8796 || *argvars[0].vval.v_string == NUL;
8797 break;
8798 case VAR_NUMBER:
8799 n = argvars[0].vval.v_number == 0;
8800 break;
8801 case VAR_LIST:
8802 n = argvars[0].vval.v_list == NULL
8803 || argvars[0].vval.v_list->lv_first == NULL;
8804 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008805 case VAR_DICT:
8806 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008807 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008808 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008809 default:
8810 EMSG2(_(e_intern2), "f_empty()");
8811 n = 0;
8812 }
8813
8814 rettv->vval.v_number = n;
8815}
8816
8817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 * "escape({string}, {chars})" function
8819 */
8820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 typval_T *argvars;
8823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 char_u buf[NUMBUFLEN];
8826
Bram Moolenaar758711c2005-02-02 23:11:38 +00008827 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8828 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830}
8831
8832/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008833 * "eval()" function
8834 */
8835/*ARGSUSED*/
8836 static void
8837f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008838 typval_T *argvars;
8839 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008840{
8841 char_u *s;
8842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008843 s = get_tv_string_chk(&argvars[0]);
8844 if (s != NULL)
8845 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008847 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8848 {
8849 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008850 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008851 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008852 else if (*s != NUL)
8853 EMSG(_(e_trailing));
8854}
8855
8856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 * "eventhandler()" function
8858 */
8859/*ARGSUSED*/
8860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 typval_T *argvars;
8863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866}
8867
8868/*
8869 * "executable()" function
8870 */
8871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008873 typval_T *argvars;
8874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877}
8878
8879/*
8880 * "exists()" function
8881 */
8882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 typval_T *argvars;
8885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886{
8887 char_u *p;
8888 char_u *name;
8889 int n = FALSE;
8890 int len = 0;
8891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 if (*p == '$') /* environment variable */
8894 {
8895 /* first try "normal" environment variables (fast) */
8896 if (mch_getenv(p + 1) != NULL)
8897 n = TRUE;
8898 else
8899 {
8900 /* try expanding things like $VIM and ${HOME} */
8901 p = expand_env_save(p);
8902 if (p != NULL && *p != '$')
8903 n = TRUE;
8904 vim_free(p);
8905 }
8906 }
8907 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008910 if (*skipwhite(p) != NUL)
8911 n = FALSE; /* trailing garbage */
8912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 else if (*p == '*') /* internal or user defined function */
8914 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008915 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 }
8917 else if (*p == ':')
8918 {
8919 n = cmd_exists(p + 1);
8920 }
8921 else if (*p == '#')
8922 {
8923#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008924 if (p[1] == '#')
8925 n = autocmd_supported(p + 2);
8926 else
8927 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928#endif
8929 }
8930 else /* internal variable */
8931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008932 char_u *tofree;
8933 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008935 /* get_name_len() takes care of expanding curly braces */
8936 name = p;
8937 len = get_name_len(&p, &tofree, TRUE, FALSE);
8938 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008940 if (tofree != NULL)
8941 name = tofree;
8942 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8943 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008945 /* handle d.key, l[idx], f(expr) */
8946 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8947 if (n)
8948 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 }
8950 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008951 if (*p != NUL)
8952 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008954 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 }
8956
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
8959
8960/*
8961 * "expand()" function
8962 */
8963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T *argvars;
8966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967{
8968 char_u *s;
8969 int len;
8970 char_u *errormsg;
8971 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8972 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008973 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 rettv->v_type = VAR_STRING;
8976 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 if (*s == '%' || *s == '#' || *s == '<')
8978 {
8979 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008980 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 --emsg_off;
8982 }
8983 else
8984 {
8985 /* When the optional second argument is non-zero, don't remove matches
8986 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 if (argvars[1].v_type != VAR_UNKNOWN
8988 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (!error)
8991 {
8992 ExpandInit(&xpc);
8993 xpc.xp_context = EXPAND_FILES;
8994 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008995 }
8996 else
8997 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 }
8999}
9000
9001/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009002 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009003 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009004 */
9005 static void
9006f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009010 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009011 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009012 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 list_T *l1, *l2;
9014 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009016 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009017
Bram Moolenaare9a41262005-01-15 22:18:47 +00009018 l1 = argvars[0].vval.v_list;
9019 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009020 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9021 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009022 {
9023 if (argvars[2].v_type != VAR_UNKNOWN)
9024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009025 before = get_tv_number_chk(&argvars[2], &error);
9026 if (error)
9027 return; /* type error; errmsg already given */
9028
Bram Moolenaar758711c2005-02-02 23:11:38 +00009029 if (before == l1->lv_len)
9030 item = NULL;
9031 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009033 item = list_find(l1, before);
9034 if (item == NULL)
9035 {
9036 EMSGN(_(e_listidx), before);
9037 return;
9038 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009039 }
9040 }
9041 else
9042 item = NULL;
9043 list_extend(l1, l2, item);
9044
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 copy_tv(&argvars[0], rettv);
9046 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009047 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009048 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9049 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009050 dict_T *d1, *d2;
9051 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009052 char_u *action;
9053 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009055 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056
9057 d1 = argvars[0].vval.v_dict;
9058 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009059 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9060 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009061 {
9062 /* Check the third argument. */
9063 if (argvars[2].v_type != VAR_UNKNOWN)
9064 {
9065 static char *(av[]) = {"keep", "force", "error"};
9066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 action = get_tv_string_chk(&argvars[2]);
9068 if (action == NULL)
9069 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009070 for (i = 0; i < 3; ++i)
9071 if (STRCMP(action, av[i]) == 0)
9072 break;
9073 if (i == 3)
9074 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009075 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009076 return;
9077 }
9078 }
9079 else
9080 action = (char_u *)"force";
9081
9082 /* Go over all entries in the second dict and add them to the
9083 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009084 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009085 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009087 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009089 --todo;
9090 di1 = dict_find(d1, hi2->hi_key, -1);
9091 if (di1 == NULL)
9092 {
9093 di1 = dictitem_copy(HI2DI(hi2));
9094 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9095 dictitem_free(di1);
9096 }
9097 else if (*action == 'e')
9098 {
9099 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9100 break;
9101 }
9102 else if (*action == 'f')
9103 {
9104 clear_tv(&di1->di_tv);
9105 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009107 }
9108 }
9109
Bram Moolenaare9a41262005-01-15 22:18:47 +00009110 copy_tv(&argvars[0], rettv);
9111 }
9112 }
9113 else
9114 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009115}
9116
9117/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009118 * "feedkeys()" function
9119 */
9120/*ARGSUSED*/
9121 static void
9122f_feedkeys(argvars, rettv)
9123 typval_T *argvars;
9124 typval_T *rettv;
9125{
9126 int remap = TRUE;
9127 char_u *keys, *flags;
9128 char_u nbuf[NUMBUFLEN];
9129 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009130 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009131
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009132 /* This is not allowed in the sandbox. If the commands would still be
9133 * executed in the sandbox it would be OK, but it probably happens later,
9134 * when "sandbox" is no longer set. */
9135 if (check_secure())
9136 return;
9137
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009138 rettv->vval.v_number = 0;
9139 keys = get_tv_string(&argvars[0]);
9140 if (*keys != NUL)
9141 {
9142 if (argvars[1].v_type != VAR_UNKNOWN)
9143 {
9144 flags = get_tv_string_buf(&argvars[1], nbuf);
9145 for ( ; *flags != NUL; ++flags)
9146 {
9147 switch (*flags)
9148 {
9149 case 'n': remap = FALSE; break;
9150 case 'm': remap = TRUE; break;
9151 case 't': typed = TRUE; break;
9152 }
9153 }
9154 }
9155
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009156 /* Need to escape K_SPECIAL and CSI before putting the string in the
9157 * typeahead buffer. */
9158 keys_esc = vim_strsave_escape_csi(keys);
9159 if (keys_esc != NULL)
9160 {
9161 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009162 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009163 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009164 if (vgetc_busy)
9165 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009166 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009167 }
9168}
9169
9170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 * "filereadable()" function
9172 */
9173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009175 typval_T *argvars;
9176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178 FILE *fd;
9179 char_u *p;
9180 int n;
9181
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9184 {
9185 n = TRUE;
9186 fclose(fd);
9187 }
9188 else
9189 n = FALSE;
9190
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192}
9193
9194/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009195 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 * rights to write into.
9197 */
9198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009200 typval_T *argvars;
9201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009203 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204}
9205
Bram Moolenaar33570922005-01-25 22:26:29 +00009206static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009207
9208 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009209findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 typval_T *argvars;
9211 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009212 int dir;
9213{
9214#ifdef FEAT_SEARCHPATH
9215 char_u *fname;
9216 char_u *fresult = NULL;
9217 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9218 char_u *p;
9219 char_u pathbuf[NUMBUFLEN];
9220 int count = 1;
9221 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009222 int error = FALSE;
9223#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009224
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009225 rettv->vval.v_string = NULL;
9226 rettv->v_type = VAR_STRING;
9227
9228#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009230
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009231 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009233 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9234 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009235 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 else
9237 {
9238 if (*p != NUL)
9239 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009242 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009244 }
9245
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009246 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9247 error = TRUE;
9248
9249 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009251 do
9252 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009253 if (rettv->v_type == VAR_STRING)
9254 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 fresult = find_file_in_path_option(first ? fname : NULL,
9256 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009257 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009258 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009259 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009260
9261 if (fresult != NULL && rettv->v_type == VAR_LIST)
9262 list_append_string(rettv->vval.v_list, fresult, -1);
9263
9264 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009266
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009267 if (rettv->v_type == VAR_STRING)
9268 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009269#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009270}
9271
Bram Moolenaar33570922005-01-25 22:26:29 +00009272static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9273static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009274
9275/*
9276 * Implementation of map() and filter().
9277 */
9278 static void
9279filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 typval_T *argvars;
9281 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009282 int map;
9283{
9284 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009285 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009286 listitem_T *li, *nli;
9287 list_T *l = NULL;
9288 dictitem_T *di;
9289 hashtab_T *ht;
9290 hashitem_T *hi;
9291 dict_T *d = NULL;
9292 typval_T save_val;
9293 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009294 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009295 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009296 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009297 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009298
9299 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009300 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009301 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009302 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009303 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009304 return;
9305 }
9306 else if (argvars[0].v_type == VAR_DICT)
9307 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009308 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009309 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009310 return;
9311 }
9312 else
9313 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009314 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009315 return;
9316 }
9317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009318 expr = get_tv_string_buf_chk(&argvars[1], buf);
9319 /* On type errors, the preceding call has already displayed an error
9320 * message. Avoid a misleading error message for an empty string that
9321 * was not passed as argument. */
9322 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009324 prepare_vimvar(VV_VAL, &save_val);
9325 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009326
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009327 /* We reset "did_emsg" to be able to detect whether an error
9328 * occurred during evaluation of the expression. */
9329 save_did_emsg = did_emsg;
9330 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009334 prepare_vimvar(VV_KEY, &save_key);
9335 vimvars[VV_KEY].vv_type = VAR_STRING;
9336
9337 ht = &d->dv_hashtab;
9338 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009339 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009340 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 if (!HASHITEM_EMPTY(hi))
9343 {
9344 --todo;
9345 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009346 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009347 break;
9348 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009349 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009350 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 break;
9352 if (!map && rem)
9353 dictitem_remove(d, di);
9354 clear_tv(&vimvars[VV_KEY].vv_tv);
9355 }
9356 }
9357 hash_unlock(ht);
9358
9359 restore_vimvar(VV_KEY, &save_key);
9360 }
9361 else
9362 {
9363 for (li = l->lv_first; li != NULL; li = nli)
9364 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009365 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009366 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009367 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009368 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009369 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009370 break;
9371 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009372 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009373 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009374 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009376 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009377
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009378 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009379 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009380
9381 copy_tv(&argvars[0], rettv);
9382}
9383
9384 static int
9385filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387 char_u *expr;
9388 int map;
9389 int *remp;
9390{
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009393 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009394
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009396 s = expr;
9397 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009398 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009399 if (*s != NUL) /* check for trailing chars after expr */
9400 {
9401 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009402 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009403 }
9404 if (map)
9405 {
9406 /* map(): replace the list item value */
9407 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009408 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009409 *tv = rettv;
9410 }
9411 else
9412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009413 int error = FALSE;
9414
Bram Moolenaare9a41262005-01-15 22:18:47 +00009415 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009416 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009417 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009418 /* On type error, nothing has been removed; return FAIL to stop the
9419 * loop. The error message was given by get_tv_number_chk(). */
9420 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009421 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009422 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009423 retval = OK;
9424theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009425 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009426 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009427}
9428
9429/*
9430 * "filter()" function
9431 */
9432 static void
9433f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009434 typval_T *argvars;
9435 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009436{
9437 filter_map(argvars, rettv, FALSE);
9438}
9439
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009440/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009441 * "finddir({fname}[, {path}[, {count}]])" function
9442 */
9443 static void
9444f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009445 typval_T *argvars;
9446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009447{
9448 findfilendir(argvars, rettv, TRUE);
9449}
9450
9451/*
9452 * "findfile({fname}[, {path}[, {count}]])" function
9453 */
9454 static void
9455f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009456 typval_T *argvars;
9457 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009458{
9459 findfilendir(argvars, rettv, FALSE);
9460}
9461
9462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 * "fnamemodify({fname}, {mods})" function
9464 */
9465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009467 typval_T *argvars;
9468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 char_u *fname;
9471 char_u *mods;
9472 int usedlen = 0;
9473 int len;
9474 char_u *fbuf = NULL;
9475 char_u buf[NUMBUFLEN];
9476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009477 fname = get_tv_string_chk(&argvars[0]);
9478 mods = get_tv_string_buf_chk(&argvars[1], buf);
9479 if (fname == NULL || mods == NULL)
9480 fname = NULL;
9481 else
9482 {
9483 len = (int)STRLEN(fname);
9484 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 vim_free(fbuf);
9493}
9494
Bram Moolenaar33570922005-01-25 22:26:29 +00009495static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496
9497/*
9498 * "foldclosed()" function
9499 */
9500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009502 typval_T *argvars;
9503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 int end;
9505{
9506#ifdef FEAT_FOLDING
9507 linenr_T lnum;
9508 linenr_T first, last;
9509
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9512 {
9513 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9514 {
9515 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 return;
9520 }
9521 }
9522#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524}
9525
9526/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009527 * "foldclosed()" function
9528 */
9529 static void
9530f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009531 typval_T *argvars;
9532 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009533{
9534 foldclosed_both(argvars, rettv, FALSE);
9535}
9536
9537/*
9538 * "foldclosedend()" function
9539 */
9540 static void
9541f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009542 typval_T *argvars;
9543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009544{
9545 foldclosed_both(argvars, rettv, TRUE);
9546}
9547
9548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 * "foldlevel()" function
9550 */
9551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009553 typval_T *argvars;
9554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556#ifdef FEAT_FOLDING
9557 linenr_T lnum;
9558
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 else
9563#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565}
9566
9567/*
9568 * "foldtext()" function
9569 */
9570/*ARGSUSED*/
9571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009573 typval_T *argvars;
9574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575{
9576#ifdef FEAT_FOLDING
9577 linenr_T lnum;
9578 char_u *s;
9579 char_u *r;
9580 int len;
9581 char *txt;
9582#endif
9583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->v_type = VAR_STRING;
9585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009587 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9588 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9589 <= curbuf->b_ml.ml_line_count
9590 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 {
9592 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009593 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9594 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 {
9596 if (!linewhite(lnum))
9597 break;
9598 ++lnum;
9599 }
9600
9601 /* Find interesting text in this line. */
9602 s = skipwhite(ml_get(lnum));
9603 /* skip C comment-start */
9604 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009607 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009608 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009609 {
9610 s = skipwhite(ml_get(lnum + 1));
9611 if (*s == '*')
9612 s = skipwhite(s + 1);
9613 }
9614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 txt = _("+-%s%3ld lines: ");
9616 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009617 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 + 20 /* for %3ld */
9619 + STRLEN(s))); /* concatenated */
9620 if (r != NULL)
9621 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9623 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9624 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 len = (int)STRLEN(r);
9626 STRCAT(r, s);
9627 /* remove 'foldmarker' and 'commentstring' */
9628 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 }
9631 }
9632#endif
9633}
9634
9635/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009636 * "foldtextresult(lnum)" function
9637 */
9638/*ARGSUSED*/
9639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009641 typval_T *argvars;
9642 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009643{
9644#ifdef FEAT_FOLDING
9645 linenr_T lnum;
9646 char_u *text;
9647 char_u buf[51];
9648 foldinfo_T foldinfo;
9649 int fold_count;
9650#endif
9651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 rettv->v_type = VAR_STRING;
9653 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009654#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 /* treat illegal types and illegal string values for {lnum} the same */
9657 if (lnum < 0)
9658 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009659 fold_count = foldedCount(curwin, lnum, &foldinfo);
9660 if (fold_count > 0)
9661 {
9662 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9663 &foldinfo, buf);
9664 if (text == buf)
9665 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009667 }
9668#endif
9669}
9670
9671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 * "foreground()" function
9673 */
9674/*ARGSUSED*/
9675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 typval_T *argvars;
9678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009680 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681#ifdef FEAT_GUI
9682 if (gui.in_use)
9683 gui_mch_set_foreground();
9684#else
9685# ifdef WIN32
9686 win32_set_foreground();
9687# endif
9688#endif
9689}
9690
9691/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009692 * "function()" function
9693 */
9694/*ARGSUSED*/
9695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009697 typval_T *argvars;
9698 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009699{
9700 char_u *s;
9701
Bram Moolenaara7043832005-01-21 11:56:39 +00009702 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009704 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009705 EMSG2(_(e_invarg2), s);
9706 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009707 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009708 else
9709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 rettv->vval.v_string = vim_strsave(s);
9711 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009712 }
9713}
9714
9715/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009716 * "garbagecollect()" function
9717 */
9718/*ARGSUSED*/
9719 static void
9720f_garbagecollect(argvars, rettv)
9721 typval_T *argvars;
9722 typval_T *rettv;
9723{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009724 /* This is postponed until we are back at the toplevel, because we may be
9725 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9726 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00009727
9728 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
9729 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009730}
9731
9732/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009733 * "get()" function
9734 */
9735 static void
9736f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009737 typval_T *argvars;
9738 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009739{
Bram Moolenaar33570922005-01-25 22:26:29 +00009740 listitem_T *li;
9741 list_T *l;
9742 dictitem_T *di;
9743 dict_T *d;
9744 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009745
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009747 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009748 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009750 int error = FALSE;
9751
9752 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9753 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009754 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009755 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009756 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009757 else if (argvars[0].v_type == VAR_DICT)
9758 {
9759 if ((d = argvars[0].vval.v_dict) != NULL)
9760 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009761 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009762 if (di != NULL)
9763 tv = &di->di_tv;
9764 }
9765 }
9766 else
9767 EMSG2(_(e_listdictarg), "get()");
9768
9769 if (tv == NULL)
9770 {
9771 if (argvars[2].v_type == VAR_UNKNOWN)
9772 rettv->vval.v_number = 0;
9773 else
9774 copy_tv(&argvars[2], rettv);
9775 }
9776 else
9777 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009778}
9779
Bram Moolenaar342337a2005-07-21 21:11:17 +00009780static 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 +00009781
9782/*
9783 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009784 * Return a range (from start to end) of lines in rettv from the specified
9785 * buffer.
9786 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009787 */
9788 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009789get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009790 buf_T *buf;
9791 linenr_T start;
9792 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009793 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009794 typval_T *rettv;
9795{
9796 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009797
Bram Moolenaar342337a2005-07-21 21:11:17 +00009798 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009799 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009800 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009801 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009802 }
9803 else
9804 rettv->vval.v_number = 0;
9805
9806 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9807 return;
9808
9809 if (!retlist)
9810 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009811 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9812 p = ml_get_buf(buf, start, FALSE);
9813 else
9814 p = (char_u *)"";
9815
9816 rettv->v_type = VAR_STRING;
9817 rettv->vval.v_string = vim_strsave(p);
9818 }
9819 else
9820 {
9821 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009822 return;
9823
9824 if (start < 1)
9825 start = 1;
9826 if (end > buf->b_ml.ml_line_count)
9827 end = buf->b_ml.ml_line_count;
9828 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009829 if (list_append_string(rettv->vval.v_list,
9830 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009831 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009832 }
9833}
9834
9835/*
9836 * "getbufline()" function
9837 */
9838 static void
9839f_getbufline(argvars, rettv)
9840 typval_T *argvars;
9841 typval_T *rettv;
9842{
9843 linenr_T lnum;
9844 linenr_T end;
9845 buf_T *buf;
9846
9847 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9848 ++emsg_off;
9849 buf = get_buf_tv(&argvars[0]);
9850 --emsg_off;
9851
Bram Moolenaar661b1822005-07-28 22:36:45 +00009852 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009853 if (argvars[2].v_type == VAR_UNKNOWN)
9854 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009855 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009856 end = get_tv_lnum_buf(&argvars[2], buf);
9857
Bram Moolenaar342337a2005-07-21 21:11:17 +00009858 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009859}
9860
Bram Moolenaar0d660222005-01-07 21:51:51 +00009861/*
9862 * "getbufvar()" function
9863 */
9864 static void
9865f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009866 typval_T *argvars;
9867 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009868{
9869 buf_T *buf;
9870 buf_T *save_curbuf;
9871 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009872 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9875 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009876 ++emsg_off;
9877 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009878
9879 rettv->v_type = VAR_STRING;
9880 rettv->vval.v_string = NULL;
9881
9882 if (buf != NULL && varname != NULL)
9883 {
9884 if (*varname == '&') /* buffer-local-option */
9885 {
9886 /* set curbuf to be our buf, temporarily */
9887 save_curbuf = curbuf;
9888 curbuf = buf;
9889
9890 get_option_tv(&varname, rettv, TRUE);
9891
9892 /* restore previous notion of curbuf */
9893 curbuf = save_curbuf;
9894 }
9895 else
9896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009897 if (*varname == NUL)
9898 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9899 * scope prefix before the NUL byte is required by
9900 * find_var_in_ht(). */
9901 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009902 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009903 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009904 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009905 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009906 }
9907 }
9908
9909 --emsg_off;
9910}
9911
9912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 * "getchar()" function
9914 */
9915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009917 typval_T *argvars;
9918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919{
9920 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009921 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009923 /* Position the cursor. Needed after a message that ends in a space. */
9924 windgoto(msg_row, msg_col);
9925
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 ++no_mapping;
9927 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009928 for (;;)
9929 {
9930 if (argvars[0].v_type == VAR_UNKNOWN)
9931 /* getchar(): blocking wait. */
9932 n = safe_vgetc();
9933 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9934 /* getchar(1): only check if char avail */
9935 n = vpeekc();
9936 else if (error || vpeekc() == NUL)
9937 /* illegal argument or getchar(0) and no char avail: return zero */
9938 n = 0;
9939 else
9940 /* getchar(0) and char avail: return char */
9941 n = safe_vgetc();
9942 if (n == K_IGNORE)
9943 continue;
9944 break;
9945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 --no_mapping;
9947 --allow_keys;
9948
Bram Moolenaar219b8702006-11-01 14:32:36 +00009949 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9950 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9951 vimvars[VV_MOUSE_COL].vv_nr = 0;
9952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 if (IS_SPECIAL(n) || mod_mask != 0)
9955 {
9956 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9957 int i = 0;
9958
9959 /* Turn a special key into three bytes, plus modifier. */
9960 if (mod_mask != 0)
9961 {
9962 temp[i++] = K_SPECIAL;
9963 temp[i++] = KS_MODIFIER;
9964 temp[i++] = mod_mask;
9965 }
9966 if (IS_SPECIAL(n))
9967 {
9968 temp[i++] = K_SPECIAL;
9969 temp[i++] = K_SECOND(n);
9970 temp[i++] = K_THIRD(n);
9971 }
9972#ifdef FEAT_MBYTE
9973 else if (has_mbyte)
9974 i += (*mb_char2bytes)(n, temp + i);
9975#endif
9976 else
9977 temp[i++] = n;
9978 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->v_type = VAR_STRING;
9980 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009981
9982#ifdef FEAT_MOUSE
9983 if (n == K_LEFTMOUSE
9984 || n == K_LEFTMOUSE_NM
9985 || n == K_LEFTDRAG
9986 || n == K_LEFTRELEASE
9987 || n == K_LEFTRELEASE_NM
9988 || n == K_MIDDLEMOUSE
9989 || n == K_MIDDLEDRAG
9990 || n == K_MIDDLERELEASE
9991 || n == K_RIGHTMOUSE
9992 || n == K_RIGHTDRAG
9993 || n == K_RIGHTRELEASE
9994 || n == K_X1MOUSE
9995 || n == K_X1DRAG
9996 || n == K_X1RELEASE
9997 || n == K_X2MOUSE
9998 || n == K_X2DRAG
9999 || n == K_X2RELEASE
10000 || n == K_MOUSEDOWN
10001 || n == K_MOUSEUP)
10002 {
10003 int row = mouse_row;
10004 int col = mouse_col;
10005 win_T *win;
10006 linenr_T lnum;
10007# ifdef FEAT_WINDOWS
10008 win_T *wp;
10009# endif
10010 int n = 1;
10011
10012 if (row >= 0 && col >= 0)
10013 {
10014 /* Find the window at the mouse coordinates and compute the
10015 * text position. */
10016 win = mouse_find_win(&row, &col);
10017 (void)mouse_comp_pos(win, &row, &col, &lnum);
10018# ifdef FEAT_WINDOWS
10019 for (wp = firstwin; wp != win; wp = wp->w_next)
10020 ++n;
10021# endif
10022 vimvars[VV_MOUSE_WIN].vv_nr = n;
10023 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10024 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10025 }
10026 }
10027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 }
10029}
10030
10031/*
10032 * "getcharmod()" function
10033 */
10034/*ARGSUSED*/
10035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010037 typval_T *argvars;
10038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041}
10042
10043/*
10044 * "getcmdline()" function
10045 */
10046/*ARGSUSED*/
10047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010049 typval_T *argvars;
10050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->v_type = VAR_STRING;
10053 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054}
10055
10056/*
10057 * "getcmdpos()" function
10058 */
10059/*ARGSUSED*/
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010065 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066}
10067
10068/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010069 * "getcmdtype()" function
10070 */
10071/*ARGSUSED*/
10072 static void
10073f_getcmdtype(argvars, rettv)
10074 typval_T *argvars;
10075 typval_T *rettv;
10076{
10077 rettv->v_type = VAR_STRING;
10078 rettv->vval.v_string = alloc(2);
10079 if (rettv->vval.v_string != NULL)
10080 {
10081 rettv->vval.v_string[0] = get_cmdline_type();
10082 rettv->vval.v_string[1] = NUL;
10083 }
10084}
10085
10086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 * "getcwd()" function
10088 */
10089/*ARGSUSED*/
10090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010092 typval_T *argvars;
10093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094{
10095 char_u cwd[MAXPATHL];
10096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 else
10101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010104 if (rettv->vval.v_string != NULL)
10105 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106#endif
10107 }
10108}
10109
10110/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010111 * "getfontname()" function
10112 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010113/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010116 typval_T *argvars;
10117 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010118{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->v_type = VAR_STRING;
10120 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010121#ifdef FEAT_GUI
10122 if (gui.in_use)
10123 {
10124 GuiFont font;
10125 char_u *name = NULL;
10126
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010127 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010128 {
10129 /* Get the "Normal" font. Either the name saved by
10130 * hl_set_font_name() or from the font ID. */
10131 font = gui.norm_font;
10132 name = hl_get_font_name();
10133 }
10134 else
10135 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010137 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10138 return;
10139 font = gui_mch_get_font(name, FALSE);
10140 if (font == NOFONT)
10141 return; /* Invalid font name, return empty string. */
10142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010144 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010145 gui_mch_free_font(font);
10146 }
10147#endif
10148}
10149
10150/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010151 * "getfperm({fname})" function
10152 */
10153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010154f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010155 typval_T *argvars;
10156 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010157{
10158 char_u *fname;
10159 struct stat st;
10160 char_u *perm = NULL;
10161 char_u flags[] = "rwx";
10162 int i;
10163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010166 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010167 if (mch_stat((char *)fname, &st) >= 0)
10168 {
10169 perm = vim_strsave((char_u *)"---------");
10170 if (perm != NULL)
10171 {
10172 for (i = 0; i < 9; i++)
10173 {
10174 if (st.st_mode & (1 << (8 - i)))
10175 perm[i] = flags[i % 3];
10176 }
10177 }
10178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010180}
10181
10182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 * "getfsize({fname})" function
10184 */
10185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010187 typval_T *argvars;
10188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189{
10190 char_u *fname;
10191 struct stat st;
10192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010193 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010195 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196
10197 if (mch_stat((char *)fname, &st) >= 0)
10198 {
10199 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010203 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010204
10205 /* non-perfect check for overflow */
10206 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10207 rettv->vval.v_number = -2;
10208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 }
10210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212}
10213
10214/*
10215 * "getftime({fname})" function
10216 */
10217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010218f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010219 typval_T *argvars;
10220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221{
10222 char_u *fname;
10223 struct stat st;
10224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010225 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226
10227 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010230 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231}
10232
10233/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010234 * "getftype({fname})" function
10235 */
10236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010237f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010238 typval_T *argvars;
10239 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010240{
10241 char_u *fname;
10242 struct stat st;
10243 char_u *type = NULL;
10244 char *t;
10245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010246 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010249 if (mch_lstat((char *)fname, &st) >= 0)
10250 {
10251#ifdef S_ISREG
10252 if (S_ISREG(st.st_mode))
10253 t = "file";
10254 else if (S_ISDIR(st.st_mode))
10255 t = "dir";
10256# ifdef S_ISLNK
10257 else if (S_ISLNK(st.st_mode))
10258 t = "link";
10259# endif
10260# ifdef S_ISBLK
10261 else if (S_ISBLK(st.st_mode))
10262 t = "bdev";
10263# endif
10264# ifdef S_ISCHR
10265 else if (S_ISCHR(st.st_mode))
10266 t = "cdev";
10267# endif
10268# ifdef S_ISFIFO
10269 else if (S_ISFIFO(st.st_mode))
10270 t = "fifo";
10271# endif
10272# ifdef S_ISSOCK
10273 else if (S_ISSOCK(st.st_mode))
10274 t = "fifo";
10275# endif
10276 else
10277 t = "other";
10278#else
10279# ifdef S_IFMT
10280 switch (st.st_mode & S_IFMT)
10281 {
10282 case S_IFREG: t = "file"; break;
10283 case S_IFDIR: t = "dir"; break;
10284# ifdef S_IFLNK
10285 case S_IFLNK: t = "link"; break;
10286# endif
10287# ifdef S_IFBLK
10288 case S_IFBLK: t = "bdev"; break;
10289# endif
10290# ifdef S_IFCHR
10291 case S_IFCHR: t = "cdev"; break;
10292# endif
10293# ifdef S_IFIFO
10294 case S_IFIFO: t = "fifo"; break;
10295# endif
10296# ifdef S_IFSOCK
10297 case S_IFSOCK: t = "socket"; break;
10298# endif
10299 default: t = "other";
10300 }
10301# else
10302 if (mch_isdir(fname))
10303 t = "dir";
10304 else
10305 t = "file";
10306# endif
10307#endif
10308 type = vim_strsave((char_u *)t);
10309 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010311}
10312
10313/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010314 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010315 */
10316 static void
10317f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010318 typval_T *argvars;
10319 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010320{
10321 linenr_T lnum;
10322 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010323 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010324
10325 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010326 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010327 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010328 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010329 retlist = FALSE;
10330 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010331 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010332 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010333 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010334 retlist = TRUE;
10335 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010336
Bram Moolenaar342337a2005-07-21 21:11:17 +000010337 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010338}
10339
10340/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010341 * "getmatches()" function
10342 */
10343/*ARGSUSED*/
10344 static void
10345f_getmatches(argvars, rettv)
10346 typval_T *argvars;
10347 typval_T *rettv;
10348{
10349#ifdef FEAT_SEARCH_EXTRA
10350 dict_T *dict;
10351 matchitem_T *cur = curwin->w_match_head;
10352
10353 rettv->vval.v_number = 0;
10354
10355 if (rettv_list_alloc(rettv) == OK)
10356 {
10357 while (cur != NULL)
10358 {
10359 dict = dict_alloc();
10360 if (dict == NULL)
10361 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010362 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10363 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10364 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10365 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10366 list_append_dict(rettv->vval.v_list, dict);
10367 cur = cur->next;
10368 }
10369 }
10370#endif
10371}
10372
10373/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010374 * "getpos(string)" function
10375 */
10376 static void
10377f_getpos(argvars, rettv)
10378 typval_T *argvars;
10379 typval_T *rettv;
10380{
10381 pos_T *fp;
10382 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010383 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010384
10385 if (rettv_list_alloc(rettv) == OK)
10386 {
10387 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010388 fp = var2fpos(&argvars[0], TRUE, &fnum);
10389 if (fnum != -1)
10390 list_append_number(l, (varnumber_T)fnum);
10391 else
10392 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010393 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10394 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010395 list_append_number(l, (fp != NULL)
10396 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010397 : (varnumber_T)0);
10398 list_append_number(l,
10399#ifdef FEAT_VIRTUALEDIT
10400 (fp != NULL) ? (varnumber_T)fp->coladd :
10401#endif
10402 (varnumber_T)0);
10403 }
10404 else
10405 rettv->vval.v_number = FALSE;
10406}
10407
10408/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010409 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010410 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010411/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010412 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010413f_getqflist(argvars, rettv)
10414 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010415 typval_T *rettv;
10416{
10417#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010418 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010419#endif
10420
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010421 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010422#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010423 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010424 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010425 wp = NULL;
10426 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10427 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010428 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010429 if (wp == NULL)
10430 return;
10431 }
10432
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010433 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010434 }
10435#endif
10436}
10437
10438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 * "getreg()" function
10440 */
10441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010442f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010443 typval_T *argvars;
10444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445{
10446 char_u *strregname;
10447 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010448 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010451 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 strregname = get_tv_string_chk(&argvars[0]);
10454 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010455 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010456 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010459 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 regname = (strregname == NULL ? '"' : *strregname);
10461 if (regname == 0)
10462 regname = '"';
10463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010465 rettv->vval.v_string = error ? NULL :
10466 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467}
10468
10469/*
10470 * "getregtype()" function
10471 */
10472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010474 typval_T *argvars;
10475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476{
10477 char_u *strregname;
10478 int regname;
10479 char_u buf[NUMBUFLEN + 2];
10480 long reglen = 0;
10481
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010482 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010483 {
10484 strregname = get_tv_string_chk(&argvars[0]);
10485 if (strregname == NULL) /* type error; errmsg already given */
10486 {
10487 rettv->v_type = VAR_STRING;
10488 rettv->vval.v_string = NULL;
10489 return;
10490 }
10491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 else
10493 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495
10496 regname = (strregname == NULL ? '"' : *strregname);
10497 if (regname == 0)
10498 regname = '"';
10499
10500 buf[0] = NUL;
10501 buf[1] = NUL;
10502 switch (get_reg_type(regname, &reglen))
10503 {
10504 case MLINE: buf[0] = 'V'; break;
10505 case MCHAR: buf[0] = 'v'; break;
10506#ifdef FEAT_VISUAL
10507 case MBLOCK:
10508 buf[0] = Ctrl_V;
10509 sprintf((char *)buf + 1, "%ld", reglen + 1);
10510 break;
10511#endif
10512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513 rettv->v_type = VAR_STRING;
10514 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515}
10516
10517/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010518 * "gettabwinvar()" function
10519 */
10520 static void
10521f_gettabwinvar(argvars, rettv)
10522 typval_T *argvars;
10523 typval_T *rettv;
10524{
10525 getwinvar(argvars, rettv, 1);
10526}
10527
10528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 * "getwinposx()" function
10530 */
10531/*ARGSUSED*/
10532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010533f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010534 typval_T *argvars;
10535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538#ifdef FEAT_GUI
10539 if (gui.in_use)
10540 {
10541 int x, y;
10542
10543 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 }
10546#endif
10547}
10548
10549/*
10550 * "getwinposy()" function
10551 */
10552/*ARGSUSED*/
10553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010555 typval_T *argvars;
10556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559#ifdef FEAT_GUI
10560 if (gui.in_use)
10561 {
10562 int x, y;
10563
10564 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 }
10567#endif
10568}
10569
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010570/*
10571 * Find window specifed by "vp" in tabpage "tp".
10572 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010573 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010574find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010575 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010576 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010577{
10578#ifdef FEAT_WINDOWS
10579 win_T *wp;
10580#endif
10581 int nr;
10582
10583 nr = get_tv_number_chk(vp, NULL);
10584
10585#ifdef FEAT_WINDOWS
10586 if (nr < 0)
10587 return NULL;
10588 if (nr == 0)
10589 return curwin;
10590
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010591 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10592 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010593 if (--nr <= 0)
10594 break;
10595 return wp;
10596#else
10597 if (nr == 0 || nr == 1)
10598 return curwin;
10599 return NULL;
10600#endif
10601}
10602
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603/*
10604 * "getwinvar()" function
10605 */
10606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010608 typval_T *argvars;
10609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010611 getwinvar(argvars, rettv, 0);
10612}
10613
10614/*
10615 * getwinvar() and gettabwinvar()
10616 */
10617 static void
10618getwinvar(argvars, rettv, off)
10619 typval_T *argvars;
10620 typval_T *rettv;
10621 int off; /* 1 for gettabwinvar() */
10622{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 win_T *win, *oldcurwin;
10624 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010626 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010628#ifdef FEAT_WINDOWS
10629 if (off == 1)
10630 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10631 else
10632 tp = curtab;
10633#endif
10634 win = find_win_by_nr(&argvars[off], tp);
10635 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010636 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638 rettv->v_type = VAR_STRING;
10639 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640
10641 if (win != NULL && varname != NULL)
10642 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010643 /* Set curwin to be our win, temporarily. Also set curbuf, so
10644 * that we can get buffer-local options. */
10645 oldcurwin = curwin;
10646 curwin = win;
10647 curbuf = win->w_buffer;
10648
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 else
10652 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010653 if (*varname == NUL)
10654 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10655 * scope prefix before the NUL byte is required by
10656 * find_var_in_ht(). */
10657 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010659 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010661 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010663
10664 /* restore previous notion of curwin */
10665 curwin = oldcurwin;
10666 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 }
10668
10669 --emsg_off;
10670}
10671
10672/*
10673 * "glob()" function
10674 */
10675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010677 typval_T *argvars;
10678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679{
10680 expand_T xpc;
10681
10682 ExpandInit(&xpc);
10683 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 rettv->v_type = VAR_STRING;
10685 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687}
10688
10689/*
10690 * "globpath()" function
10691 */
10692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010694 typval_T *argvars;
10695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696{
10697 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010700 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 if (file == NULL)
10702 rettv->vval.v_string = NULL;
10703 else
10704 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705}
10706
10707/*
10708 * "has()" function
10709 */
10710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010712 typval_T *argvars;
10713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
10715 int i;
10716 char_u *name;
10717 int n = FALSE;
10718 static char *(has_list[]) =
10719 {
10720#ifdef AMIGA
10721 "amiga",
10722# ifdef FEAT_ARP
10723 "arp",
10724# endif
10725#endif
10726#ifdef __BEOS__
10727 "beos",
10728#endif
10729#ifdef MSDOS
10730# ifdef DJGPP
10731 "dos32",
10732# else
10733 "dos16",
10734# endif
10735#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010736#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 "mac",
10738#endif
10739#if defined(MACOS_X_UNIX)
10740 "macunix",
10741#endif
10742#ifdef OS2
10743 "os2",
10744#endif
10745#ifdef __QNX__
10746 "qnx",
10747#endif
10748#ifdef RISCOS
10749 "riscos",
10750#endif
10751#ifdef UNIX
10752 "unix",
10753#endif
10754#ifdef VMS
10755 "vms",
10756#endif
10757#ifdef WIN16
10758 "win16",
10759#endif
10760#ifdef WIN32
10761 "win32",
10762#endif
10763#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10764 "win32unix",
10765#endif
10766#ifdef WIN64
10767 "win64",
10768#endif
10769#ifdef EBCDIC
10770 "ebcdic",
10771#endif
10772#ifndef CASE_INSENSITIVE_FILENAME
10773 "fname_case",
10774#endif
10775#ifdef FEAT_ARABIC
10776 "arabic",
10777#endif
10778#ifdef FEAT_AUTOCMD
10779 "autocmd",
10780#endif
10781#ifdef FEAT_BEVAL
10782 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010783# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10784 "balloon_multiline",
10785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786#endif
10787#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10788 "builtin_terms",
10789# ifdef ALL_BUILTIN_TCAPS
10790 "all_builtin_terms",
10791# endif
10792#endif
10793#ifdef FEAT_BYTEOFF
10794 "byte_offset",
10795#endif
10796#ifdef FEAT_CINDENT
10797 "cindent",
10798#endif
10799#ifdef FEAT_CLIENTSERVER
10800 "clientserver",
10801#endif
10802#ifdef FEAT_CLIPBOARD
10803 "clipboard",
10804#endif
10805#ifdef FEAT_CMDL_COMPL
10806 "cmdline_compl",
10807#endif
10808#ifdef FEAT_CMDHIST
10809 "cmdline_hist",
10810#endif
10811#ifdef FEAT_COMMENTS
10812 "comments",
10813#endif
10814#ifdef FEAT_CRYPT
10815 "cryptv",
10816#endif
10817#ifdef FEAT_CSCOPE
10818 "cscope",
10819#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010820#ifdef CURSOR_SHAPE
10821 "cursorshape",
10822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823#ifdef DEBUG
10824 "debug",
10825#endif
10826#ifdef FEAT_CON_DIALOG
10827 "dialog_con",
10828#endif
10829#ifdef FEAT_GUI_DIALOG
10830 "dialog_gui",
10831#endif
10832#ifdef FEAT_DIFF
10833 "diff",
10834#endif
10835#ifdef FEAT_DIGRAPHS
10836 "digraphs",
10837#endif
10838#ifdef FEAT_DND
10839 "dnd",
10840#endif
10841#ifdef FEAT_EMACS_TAGS
10842 "emacs_tags",
10843#endif
10844 "eval", /* always present, of course! */
10845#ifdef FEAT_EX_EXTRA
10846 "ex_extra",
10847#endif
10848#ifdef FEAT_SEARCH_EXTRA
10849 "extra_search",
10850#endif
10851#ifdef FEAT_FKMAP
10852 "farsi",
10853#endif
10854#ifdef FEAT_SEARCHPATH
10855 "file_in_path",
10856#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010857#if defined(UNIX) && !defined(USE_SYSTEM)
10858 "filterpipe",
10859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860#ifdef FEAT_FIND_ID
10861 "find_in_path",
10862#endif
10863#ifdef FEAT_FOLDING
10864 "folding",
10865#endif
10866#ifdef FEAT_FOOTER
10867 "footer",
10868#endif
10869#if !defined(USE_SYSTEM) && defined(UNIX)
10870 "fork",
10871#endif
10872#ifdef FEAT_GETTEXT
10873 "gettext",
10874#endif
10875#ifdef FEAT_GUI
10876 "gui",
10877#endif
10878#ifdef FEAT_GUI_ATHENA
10879# ifdef FEAT_GUI_NEXTAW
10880 "gui_neXtaw",
10881# else
10882 "gui_athena",
10883# endif
10884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885#ifdef FEAT_GUI_GTK
10886 "gui_gtk",
10887# ifdef HAVE_GTK2
10888 "gui_gtk2",
10889# endif
10890#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000010891#ifdef FEAT_GUI_GNOME
10892 "gui_gnome",
10893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894#ifdef FEAT_GUI_MAC
10895 "gui_mac",
10896#endif
10897#ifdef FEAT_GUI_MOTIF
10898 "gui_motif",
10899#endif
10900#ifdef FEAT_GUI_PHOTON
10901 "gui_photon",
10902#endif
10903#ifdef FEAT_GUI_W16
10904 "gui_win16",
10905#endif
10906#ifdef FEAT_GUI_W32
10907 "gui_win32",
10908#endif
10909#ifdef FEAT_HANGULIN
10910 "hangul_input",
10911#endif
10912#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10913 "iconv",
10914#endif
10915#ifdef FEAT_INS_EXPAND
10916 "insert_expand",
10917#endif
10918#ifdef FEAT_JUMPLIST
10919 "jumplist",
10920#endif
10921#ifdef FEAT_KEYMAP
10922 "keymap",
10923#endif
10924#ifdef FEAT_LANGMAP
10925 "langmap",
10926#endif
10927#ifdef FEAT_LIBCALL
10928 "libcall",
10929#endif
10930#ifdef FEAT_LINEBREAK
10931 "linebreak",
10932#endif
10933#ifdef FEAT_LISP
10934 "lispindent",
10935#endif
10936#ifdef FEAT_LISTCMDS
10937 "listcmds",
10938#endif
10939#ifdef FEAT_LOCALMAP
10940 "localmap",
10941#endif
10942#ifdef FEAT_MENU
10943 "menu",
10944#endif
10945#ifdef FEAT_SESSION
10946 "mksession",
10947#endif
10948#ifdef FEAT_MODIFY_FNAME
10949 "modify_fname",
10950#endif
10951#ifdef FEAT_MOUSE
10952 "mouse",
10953#endif
10954#ifdef FEAT_MOUSESHAPE
10955 "mouseshape",
10956#endif
10957#if defined(UNIX) || defined(VMS)
10958# ifdef FEAT_MOUSE_DEC
10959 "mouse_dec",
10960# endif
10961# ifdef FEAT_MOUSE_GPM
10962 "mouse_gpm",
10963# endif
10964# ifdef FEAT_MOUSE_JSB
10965 "mouse_jsbterm",
10966# endif
10967# ifdef FEAT_MOUSE_NET
10968 "mouse_netterm",
10969# endif
10970# ifdef FEAT_MOUSE_PTERM
10971 "mouse_pterm",
10972# endif
10973# ifdef FEAT_MOUSE_XTERM
10974 "mouse_xterm",
10975# endif
10976#endif
10977#ifdef FEAT_MBYTE
10978 "multi_byte",
10979#endif
10980#ifdef FEAT_MBYTE_IME
10981 "multi_byte_ime",
10982#endif
10983#ifdef FEAT_MULTI_LANG
10984 "multi_lang",
10985#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010986#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010987#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010988 "mzscheme",
10989#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991#ifdef FEAT_OLE
10992 "ole",
10993#endif
10994#ifdef FEAT_OSFILETYPE
10995 "osfiletype",
10996#endif
10997#ifdef FEAT_PATH_EXTRA
10998 "path_extra",
10999#endif
11000#ifdef FEAT_PERL
11001#ifndef DYNAMIC_PERL
11002 "perl",
11003#endif
11004#endif
11005#ifdef FEAT_PYTHON
11006#ifndef DYNAMIC_PYTHON
11007 "python",
11008#endif
11009#endif
11010#ifdef FEAT_POSTSCRIPT
11011 "postscript",
11012#endif
11013#ifdef FEAT_PRINTER
11014 "printer",
11015#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011016#ifdef FEAT_PROFILE
11017 "profile",
11018#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011019#ifdef FEAT_RELTIME
11020 "reltime",
11021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022#ifdef FEAT_QUICKFIX
11023 "quickfix",
11024#endif
11025#ifdef FEAT_RIGHTLEFT
11026 "rightleft",
11027#endif
11028#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11029 "ruby",
11030#endif
11031#ifdef FEAT_SCROLLBIND
11032 "scrollbind",
11033#endif
11034#ifdef FEAT_CMDL_INFO
11035 "showcmd",
11036 "cmdline_info",
11037#endif
11038#ifdef FEAT_SIGNS
11039 "signs",
11040#endif
11041#ifdef FEAT_SMARTINDENT
11042 "smartindent",
11043#endif
11044#ifdef FEAT_SNIFF
11045 "sniff",
11046#endif
11047#ifdef FEAT_STL_OPT
11048 "statusline",
11049#endif
11050#ifdef FEAT_SUN_WORKSHOP
11051 "sun_workshop",
11052#endif
11053#ifdef FEAT_NETBEANS_INTG
11054 "netbeans_intg",
11055#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011056#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011057 "spell",
11058#endif
11059#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 "syntax",
11061#endif
11062#if defined(USE_SYSTEM) || !defined(UNIX)
11063 "system",
11064#endif
11065#ifdef FEAT_TAG_BINS
11066 "tag_binary",
11067#endif
11068#ifdef FEAT_TAG_OLDSTATIC
11069 "tag_old_static",
11070#endif
11071#ifdef FEAT_TAG_ANYWHITE
11072 "tag_any_white",
11073#endif
11074#ifdef FEAT_TCL
11075# ifndef DYNAMIC_TCL
11076 "tcl",
11077# endif
11078#endif
11079#ifdef TERMINFO
11080 "terminfo",
11081#endif
11082#ifdef FEAT_TERMRESPONSE
11083 "termresponse",
11084#endif
11085#ifdef FEAT_TEXTOBJ
11086 "textobjects",
11087#endif
11088#ifdef HAVE_TGETENT
11089 "tgetent",
11090#endif
11091#ifdef FEAT_TITLE
11092 "title",
11093#endif
11094#ifdef FEAT_TOOLBAR
11095 "toolbar",
11096#endif
11097#ifdef FEAT_USR_CMDS
11098 "user-commands", /* was accidentally included in 5.4 */
11099 "user_commands",
11100#endif
11101#ifdef FEAT_VIMINFO
11102 "viminfo",
11103#endif
11104#ifdef FEAT_VERTSPLIT
11105 "vertsplit",
11106#endif
11107#ifdef FEAT_VIRTUALEDIT
11108 "virtualedit",
11109#endif
11110#ifdef FEAT_VISUAL
11111 "visual",
11112#endif
11113#ifdef FEAT_VISUALEXTRA
11114 "visualextra",
11115#endif
11116#ifdef FEAT_VREPLACE
11117 "vreplace",
11118#endif
11119#ifdef FEAT_WILDIGN
11120 "wildignore",
11121#endif
11122#ifdef FEAT_WILDMENU
11123 "wildmenu",
11124#endif
11125#ifdef FEAT_WINDOWS
11126 "windows",
11127#endif
11128#ifdef FEAT_WAK
11129 "winaltkeys",
11130#endif
11131#ifdef FEAT_WRITEBACKUP
11132 "writebackup",
11133#endif
11134#ifdef FEAT_XIM
11135 "xim",
11136#endif
11137#ifdef FEAT_XFONTSET
11138 "xfontset",
11139#endif
11140#ifdef USE_XSMP
11141 "xsmp",
11142#endif
11143#ifdef USE_XSMP_INTERACT
11144 "xsmp_interact",
11145#endif
11146#ifdef FEAT_XCLIPBOARD
11147 "xterm_clipboard",
11148#endif
11149#ifdef FEAT_XTERM_SAVE
11150 "xterm_save",
11151#endif
11152#if defined(UNIX) && defined(FEAT_X11)
11153 "X11",
11154#endif
11155 NULL
11156 };
11157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 for (i = 0; has_list[i] != NULL; ++i)
11160 if (STRICMP(name, has_list[i]) == 0)
11161 {
11162 n = TRUE;
11163 break;
11164 }
11165
11166 if (n == FALSE)
11167 {
11168 if (STRNICMP(name, "patch", 5) == 0)
11169 n = has_patch(atoi((char *)name + 5));
11170 else if (STRICMP(name, "vim_starting") == 0)
11171 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011172#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11173 else if (STRICMP(name, "balloon_multiline") == 0)
11174 n = multiline_balloon_available();
11175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176#ifdef DYNAMIC_TCL
11177 else if (STRICMP(name, "tcl") == 0)
11178 n = tcl_enabled(FALSE);
11179#endif
11180#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11181 else if (STRICMP(name, "iconv") == 0)
11182 n = iconv_enabled(FALSE);
11183#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011184#ifdef DYNAMIC_MZSCHEME
11185 else if (STRICMP(name, "mzscheme") == 0)
11186 n = mzscheme_enabled(FALSE);
11187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188#ifdef DYNAMIC_RUBY
11189 else if (STRICMP(name, "ruby") == 0)
11190 n = ruby_enabled(FALSE);
11191#endif
11192#ifdef DYNAMIC_PYTHON
11193 else if (STRICMP(name, "python") == 0)
11194 n = python_enabled(FALSE);
11195#endif
11196#ifdef DYNAMIC_PERL
11197 else if (STRICMP(name, "perl") == 0)
11198 n = perl_enabled(FALSE);
11199#endif
11200#ifdef FEAT_GUI
11201 else if (STRICMP(name, "gui_running") == 0)
11202 n = (gui.in_use || gui.starting);
11203# ifdef FEAT_GUI_W32
11204 else if (STRICMP(name, "gui_win32s") == 0)
11205 n = gui_is_win32s();
11206# endif
11207# ifdef FEAT_BROWSE
11208 else if (STRICMP(name, "browse") == 0)
11209 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11210# endif
11211#endif
11212#ifdef FEAT_SYN_HL
11213 else if (STRICMP(name, "syntax_items") == 0)
11214 n = syntax_present(curbuf);
11215#endif
11216#if defined(WIN3264)
11217 else if (STRICMP(name, "win95") == 0)
11218 n = mch_windows95();
11219#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011220#ifdef FEAT_NETBEANS_INTG
11221 else if (STRICMP(name, "netbeans_enabled") == 0)
11222 n = usingNetbeans;
11223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 }
11225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227}
11228
11229/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011230 * "has_key()" function
11231 */
11232 static void
11233f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011234 typval_T *argvars;
11235 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236{
11237 rettv->vval.v_number = 0;
11238 if (argvars[0].v_type != VAR_DICT)
11239 {
11240 EMSG(_(e_dictreq));
11241 return;
11242 }
11243 if (argvars[0].vval.v_dict == NULL)
11244 return;
11245
11246 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011247 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011248}
11249
11250/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011251 * "haslocaldir()" function
11252 */
11253/*ARGSUSED*/
11254 static void
11255f_haslocaldir(argvars, rettv)
11256 typval_T *argvars;
11257 typval_T *rettv;
11258{
11259 rettv->vval.v_number = (curwin->w_localdir != NULL);
11260}
11261
11262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 * "hasmapto()" function
11264 */
11265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 typval_T *argvars;
11268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269{
11270 char_u *name;
11271 char_u *mode;
11272 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011273 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011276 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 mode = (char_u *)"nvo";
11278 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011281 if (argvars[2].v_type != VAR_UNKNOWN)
11282 abbr = get_tv_number(&argvars[2]);
11283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284
Bram Moolenaar2c932302006-03-18 21:42:09 +000011285 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289}
11290
11291/*
11292 * "histadd()" function
11293 */
11294/*ARGSUSED*/
11295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011297 typval_T *argvars;
11298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299{
11300#ifdef FEAT_CMDHIST
11301 int histype;
11302 char_u *str;
11303 char_u buf[NUMBUFLEN];
11304#endif
11305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 if (check_restricted() || check_secure())
11308 return;
11309#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011310 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11311 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 if (histype >= 0)
11313 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 if (*str != NUL)
11316 {
11317 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 return;
11320 }
11321 }
11322#endif
11323}
11324
11325/*
11326 * "histdel()" function
11327 */
11328/*ARGSUSED*/
11329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011331 typval_T *argvars;
11332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333{
11334#ifdef FEAT_CMDHIST
11335 int n;
11336 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11340 if (str == NULL)
11341 n = 0;
11342 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011344 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011345 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 else
11350 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011351 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 get_tv_string_buf(&argvars[1], buf));
11353 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356#endif
11357}
11358
11359/*
11360 * "histget()" function
11361 */
11362/*ARGSUSED*/
11363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011365 typval_T *argvars;
11366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367{
11368#ifdef FEAT_CMDHIST
11369 int type;
11370 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11374 if (str == NULL)
11375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 {
11378 type = get_histtype(str);
11379 if (argvars[1].v_type == VAR_UNKNOWN)
11380 idx = get_history_idx(type);
11381 else
11382 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11383 /* -1 on type error */
11384 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390}
11391
11392/*
11393 * "histnr()" function
11394 */
11395/*ARGSUSED*/
11396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011398 typval_T *argvars;
11399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400{
11401 int i;
11402
11403#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 char_u *history = get_tv_string_chk(&argvars[0]);
11405
11406 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 if (i >= HIST_CMD && i < HIST_COUNT)
11408 i = get_history_idx(i);
11409 else
11410#endif
11411 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413}
11414
11415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416 * "highlightID(name)" function
11417 */
11418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 typval_T *argvars;
11421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424}
11425
11426/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011427 * "highlight_exists()" function
11428 */
11429 static void
11430f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011431 typval_T *argvars;
11432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011433{
11434 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11435}
11436
11437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 * "hostname()" function
11439 */
11440/*ARGSUSED*/
11441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011443 typval_T *argvars;
11444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445{
11446 char_u hostname[256];
11447
11448 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->v_type = VAR_STRING;
11450 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451}
11452
11453/*
11454 * iconv() function
11455 */
11456/*ARGSUSED*/
11457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011459 typval_T *argvars;
11460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461{
11462#ifdef FEAT_MBYTE
11463 char_u buf1[NUMBUFLEN];
11464 char_u buf2[NUMBUFLEN];
11465 char_u *from, *to, *str;
11466 vimconv_T vimconv;
11467#endif
11468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->v_type = VAR_STRING;
11470 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471
11472#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 str = get_tv_string(&argvars[0]);
11474 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11475 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 vimconv.vc_type = CONV_NONE;
11477 convert_setup(&vimconv, from, to);
11478
11479 /* If the encodings are equal, no conversion needed. */
11480 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484
11485 convert_setup(&vimconv, NULL, NULL);
11486 vim_free(from);
11487 vim_free(to);
11488#endif
11489}
11490
11491/*
11492 * "indent()" function
11493 */
11494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011496 typval_T *argvars;
11497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498{
11499 linenr_T lnum;
11500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011505 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506}
11507
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011508/*
11509 * "index()" function
11510 */
11511 static void
11512f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 typval_T *argvars;
11514 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011515{
Bram Moolenaar33570922005-01-25 22:26:29 +000011516 list_T *l;
11517 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011518 long idx = 0;
11519 int ic = FALSE;
11520
11521 rettv->vval.v_number = -1;
11522 if (argvars[0].v_type != VAR_LIST)
11523 {
11524 EMSG(_(e_listreq));
11525 return;
11526 }
11527 l = argvars[0].vval.v_list;
11528 if (l != NULL)
11529 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011530 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011531 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011533 int error = FALSE;
11534
Bram Moolenaar758711c2005-02-02 23:11:38 +000011535 /* Start at specified item. Use the cached index that list_find()
11536 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011538 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011539 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011540 ic = get_tv_number_chk(&argvars[3], &error);
11541 if (error)
11542 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011543 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011544
Bram Moolenaar758711c2005-02-02 23:11:38 +000011545 for ( ; item != NULL; item = item->li_next, ++idx)
11546 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011547 {
11548 rettv->vval.v_number = idx;
11549 break;
11550 }
11551 }
11552}
11553
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554static int inputsecret_flag = 0;
11555
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011556static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11557
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011559 * This function is used by f_input() and f_inputdialog() functions. The third
11560 * argument to f_input() specifies the type of completion to use at the
11561 * prompt. The third argument to f_inputdialog() specifies the value to return
11562 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 */
11564 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011565get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011566 typval_T *argvars;
11567 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011568 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011570 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 char_u *p = NULL;
11572 int c;
11573 char_u buf[NUMBUFLEN];
11574 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011575 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011576 int xp_type = EXPAND_NOTHING;
11577 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581
11582#ifdef NO_CONSOLE_INPUT
11583 /* While starting up, there is no place to enter text. */
11584 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586#endif
11587
11588 cmd_silent = FALSE; /* Want to see the prompt. */
11589 if (prompt != NULL)
11590 {
11591 /* Only the part of the message after the last NL is considered as
11592 * prompt for the command line */
11593 p = vim_strrchr(prompt, '\n');
11594 if (p == NULL)
11595 p = prompt;
11596 else
11597 {
11598 ++p;
11599 c = *p;
11600 *p = NUL;
11601 msg_start();
11602 msg_clr_eos();
11603 msg_puts_attr(prompt, echo_attr);
11604 msg_didout = FALSE;
11605 msg_starthere();
11606 *p = c;
11607 }
11608 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 if (argvars[1].v_type != VAR_UNKNOWN)
11611 {
11612 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11613 if (defstr != NULL)
11614 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011616 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011617 {
11618 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011619 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011620 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011621
Bram Moolenaar4463f292005-09-25 22:20:24 +000011622 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011623
Bram Moolenaar4463f292005-09-25 22:20:24 +000011624 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11625 if (xp_name == NULL)
11626 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011627
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011628 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011629
Bram Moolenaar4463f292005-09-25 22:20:24 +000011630 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11631 &xp_arg) == FAIL)
11632 return;
11633 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011634 }
11635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011636 if (defstr != NULL)
11637 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011638 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11639 xp_type, xp_arg);
11640
11641 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011643 /* since the user typed this, no need to wait for return */
11644 need_wait_return = FALSE;
11645 msg_didout = FALSE;
11646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 cmd_silent = cmd_silent_save;
11648}
11649
11650/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011651 * "input()" function
11652 * Also handles inputsecret() when inputsecret is set.
11653 */
11654 static void
11655f_input(argvars, rettv)
11656 typval_T *argvars;
11657 typval_T *rettv;
11658{
11659 get_user_input(argvars, rettv, FALSE);
11660}
11661
11662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663 * "inputdialog()" function
11664 */
11665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011667 typval_T *argvars;
11668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669{
11670#if defined(FEAT_GUI_TEXTDIALOG)
11671 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11672 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11673 {
11674 char_u *message;
11675 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 message = get_tv_string_chk(&argvars[0]);
11679 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011680 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011681 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 else
11683 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 if (message != NULL && defstr != NULL
11685 && do_dialog(VIM_QUESTION, NULL, message,
11686 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011687 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 else
11689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011690 if (message != NULL && defstr != NULL
11691 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011692 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011693 rettv->vval.v_string = vim_strsave(
11694 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011698 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 }
11700 else
11701#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011702 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703}
11704
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011705/*
11706 * "inputlist()" function
11707 */
11708 static void
11709f_inputlist(argvars, rettv)
11710 typval_T *argvars;
11711 typval_T *rettv;
11712{
11713 listitem_T *li;
11714 int selected;
11715 int mouse_used;
11716
11717 rettv->vval.v_number = 0;
11718#ifdef NO_CONSOLE_INPUT
11719 /* While starting up, there is no place to enter text. */
11720 if (no_console_input())
11721 return;
11722#endif
11723 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11724 {
11725 EMSG2(_(e_listarg), "inputlist()");
11726 return;
11727 }
11728
11729 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011730 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011731 lines_left = Rows; /* avoid more prompt */
11732 msg_scroll = TRUE;
11733 msg_clr_eos();
11734
11735 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11736 {
11737 msg_puts(get_tv_string(&li->li_tv));
11738 msg_putchar('\n');
11739 }
11740
11741 /* Ask for choice. */
11742 selected = prompt_for_number(&mouse_used);
11743 if (mouse_used)
11744 selected -= lines_left;
11745
11746 rettv->vval.v_number = selected;
11747}
11748
11749
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11751
11752/*
11753 * "inputrestore()" function
11754 */
11755/*ARGSUSED*/
11756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011758 typval_T *argvars;
11759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760{
11761 if (ga_userinput.ga_len > 0)
11762 {
11763 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11765 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 }
11768 else if (p_verbose > 1)
11769 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011770 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 }
11773}
11774
11775/*
11776 * "inputsave()" function
11777 */
11778/*ARGSUSED*/
11779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011780f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011781 typval_T *argvars;
11782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783{
11784 /* Add an entry to the stack of typehead storage. */
11785 if (ga_grow(&ga_userinput, 1) == OK)
11786 {
11787 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11788 + ga_userinput.ga_len);
11789 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 }
11792 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794}
11795
11796/*
11797 * "inputsecret()" function
11798 */
11799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011801 typval_T *argvars;
11802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803{
11804 ++cmdline_star;
11805 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 --cmdline_star;
11808 --inputsecret_flag;
11809}
11810
11811/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812 * "insert()" function
11813 */
11814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 typval_T *argvars;
11817 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011818{
11819 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011820 listitem_T *item;
11821 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011823
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011824 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011825 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011826 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011827 else if ((l = argvars[0].vval.v_list) != NULL
11828 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011829 {
11830 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011831 before = get_tv_number_chk(&argvars[2], &error);
11832 if (error)
11833 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011834
Bram Moolenaar758711c2005-02-02 23:11:38 +000011835 if (before == l->lv_len)
11836 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011837 else
11838 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011839 item = list_find(l, before);
11840 if (item == NULL)
11841 {
11842 EMSGN(_(e_listidx), before);
11843 l = NULL;
11844 }
11845 }
11846 if (l != NULL)
11847 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011848 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011849 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011850 }
11851 }
11852}
11853
11854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 * "isdirectory()" function
11856 */
11857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011859 typval_T *argvars;
11860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863}
11864
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011865/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011866 * "islocked()" function
11867 */
11868 static void
11869f_islocked(argvars, rettv)
11870 typval_T *argvars;
11871 typval_T *rettv;
11872{
11873 lval_T lv;
11874 char_u *end;
11875 dictitem_T *di;
11876
11877 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011878 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11879 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011880 if (end != NULL && lv.ll_name != NULL)
11881 {
11882 if (*end != NUL)
11883 EMSG(_(e_trailing));
11884 else
11885 {
11886 if (lv.ll_tv == NULL)
11887 {
11888 if (check_changedtick(lv.ll_name))
11889 rettv->vval.v_number = 1; /* always locked */
11890 else
11891 {
11892 di = find_var(lv.ll_name, NULL);
11893 if (di != NULL)
11894 {
11895 /* Consider a variable locked when:
11896 * 1. the variable itself is locked
11897 * 2. the value of the variable is locked.
11898 * 3. the List or Dict value is locked.
11899 */
11900 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11901 || tv_islocked(&di->di_tv));
11902 }
11903 }
11904 }
11905 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011906 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011907 else if (lv.ll_newkey != NULL)
11908 EMSG2(_(e_dictkey), lv.ll_newkey);
11909 else if (lv.ll_list != NULL)
11910 /* List item. */
11911 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11912 else
11913 /* Dictionary item. */
11914 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11915 }
11916 }
11917
11918 clear_lval(&lv);
11919}
11920
Bram Moolenaar33570922005-01-25 22:26:29 +000011921static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011922
11923/*
11924 * Turn a dict into a list:
11925 * "what" == 0: list of keys
11926 * "what" == 1: list of values
11927 * "what" == 2: list of items
11928 */
11929 static void
11930dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011931 typval_T *argvars;
11932 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011933 int what;
11934{
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 list_T *l2;
11936 dictitem_T *di;
11937 hashitem_T *hi;
11938 listitem_T *li;
11939 listitem_T *li2;
11940 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011941 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011942
11943 rettv->vval.v_number = 0;
11944 if (argvars[0].v_type != VAR_DICT)
11945 {
11946 EMSG(_(e_dictreq));
11947 return;
11948 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011949 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011950 return;
11951
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011952 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011953 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011954
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011955 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011956 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011957 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011958 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011959 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011960 --todo;
11961 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011962
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011963 li = listitem_alloc();
11964 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011965 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011966 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011967
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011968 if (what == 0)
11969 {
11970 /* keys() */
11971 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011972 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011973 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11974 }
11975 else if (what == 1)
11976 {
11977 /* values() */
11978 copy_tv(&di->di_tv, &li->li_tv);
11979 }
11980 else
11981 {
11982 /* items() */
11983 l2 = list_alloc();
11984 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011985 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011986 li->li_tv.vval.v_list = l2;
11987 if (l2 == NULL)
11988 break;
11989 ++l2->lv_refcount;
11990
11991 li2 = listitem_alloc();
11992 if (li2 == NULL)
11993 break;
11994 list_append(l2, li2);
11995 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011996 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011997 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11998
11999 li2 = listitem_alloc();
12000 if (li2 == NULL)
12001 break;
12002 list_append(l2, li2);
12003 copy_tv(&di->di_tv, &li2->li_tv);
12004 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012005 }
12006 }
12007}
12008
12009/*
12010 * "items(dict)" function
12011 */
12012 static void
12013f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012014 typval_T *argvars;
12015 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012016{
12017 dict_list(argvars, rettv, 2);
12018}
12019
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012021 * "join()" function
12022 */
12023 static void
12024f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012025 typval_T *argvars;
12026 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012027{
12028 garray_T ga;
12029 char_u *sep;
12030
12031 rettv->vval.v_number = 0;
12032 if (argvars[0].v_type != VAR_LIST)
12033 {
12034 EMSG(_(e_listreq));
12035 return;
12036 }
12037 if (argvars[0].vval.v_list == NULL)
12038 return;
12039 if (argvars[1].v_type == VAR_UNKNOWN)
12040 sep = (char_u *)" ";
12041 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012042 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012043
12044 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045
12046 if (sep != NULL)
12047 {
12048 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012049 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012050 ga_append(&ga, NUL);
12051 rettv->vval.v_string = (char_u *)ga.ga_data;
12052 }
12053 else
12054 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012055}
12056
12057/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012058 * "keys()" function
12059 */
12060 static void
12061f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012062 typval_T *argvars;
12063 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012064{
12065 dict_list(argvars, rettv, 0);
12066}
12067
12068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 * "last_buffer_nr()" function.
12070 */
12071/*ARGSUSED*/
12072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012074 typval_T *argvars;
12075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076{
12077 int n = 0;
12078 buf_T *buf;
12079
12080 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12081 if (n < buf->b_fnum)
12082 n = buf->b_fnum;
12083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012084 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012085}
12086
12087/*
12088 * "len()" function
12089 */
12090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012092 typval_T *argvars;
12093 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012094{
12095 switch (argvars[0].v_type)
12096 {
12097 case VAR_STRING:
12098 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099 rettv->vval.v_number = (varnumber_T)STRLEN(
12100 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012101 break;
12102 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012104 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012105 case VAR_DICT:
12106 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12107 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012108 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012109 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012110 break;
12111 }
12112}
12113
Bram Moolenaar33570922005-01-25 22:26:29 +000012114static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012115
12116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012118 typval_T *argvars;
12119 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012120 int type;
12121{
12122#ifdef FEAT_LIBCALL
12123 char_u *string_in;
12124 char_u **string_result;
12125 int nr_result;
12126#endif
12127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012129 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012132 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012133
12134 if (check_restricted() || check_secure())
12135 return;
12136
12137#ifdef FEAT_LIBCALL
12138 /* The first two args must be strings, otherwise its meaningless */
12139 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12140 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012141 string_in = NULL;
12142 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012143 string_in = argvars[2].vval.v_string;
12144 if (type == VAR_NUMBER)
12145 string_result = NULL;
12146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012148 if (mch_libcall(argvars[0].vval.v_string,
12149 argvars[1].vval.v_string,
12150 string_in,
12151 argvars[2].vval.v_number,
12152 string_result,
12153 &nr_result) == OK
12154 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012156 }
12157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158}
12159
12160/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161 * "libcall()" function
12162 */
12163 static void
12164f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012165 typval_T *argvars;
12166 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012167{
12168 libcall_common(argvars, rettv, VAR_STRING);
12169}
12170
12171/*
12172 * "libcallnr()" function
12173 */
12174 static void
12175f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012176 typval_T *argvars;
12177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012178{
12179 libcall_common(argvars, rettv, VAR_NUMBER);
12180}
12181
12182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 * "line(string)" function
12184 */
12185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012187 typval_T *argvars;
12188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189{
12190 linenr_T lnum = 0;
12191 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012192 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012194 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 if (fp != NULL)
12196 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012197 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198}
12199
12200/*
12201 * "line2byte(lnum)" function
12202 */
12203/*ARGSUSED*/
12204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012206 typval_T *argvars;
12207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208{
12209#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012210 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211#else
12212 linenr_T lnum;
12213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012218 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12219 if (rettv->vval.v_number >= 0)
12220 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221#endif
12222}
12223
12224/*
12225 * "lispindent(lnum)" function
12226 */
12227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012228f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012229 typval_T *argvars;
12230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231{
12232#ifdef FEAT_LISP
12233 pos_T pos;
12234 linenr_T lnum;
12235
12236 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12239 {
12240 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012241 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 curwin->w_cursor = pos;
12243 }
12244 else
12245#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012246 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247}
12248
12249/*
12250 * "localtime()" function
12251 */
12252/*ARGSUSED*/
12253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012255 typval_T *argvars;
12256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259}
12260
Bram Moolenaar33570922005-01-25 22:26:29 +000012261static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262
12263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012265 typval_T *argvars;
12266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 int exact;
12268{
12269 char_u *keys;
12270 char_u *which;
12271 char_u buf[NUMBUFLEN];
12272 char_u *keys_buf = NULL;
12273 char_u *rhs;
12274 int mode;
12275 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012276 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277
12278 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 rettv->v_type = VAR_STRING;
12280 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012282 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 if (*keys == NUL)
12284 return;
12285
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012286 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012288 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012289 if (argvars[2].v_type != VAR_UNKNOWN)
12290 abbr = get_tv_number(&argvars[2]);
12291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 else
12293 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012294 if (which == NULL)
12295 return;
12296
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 mode = get_map_mode(&which, 0);
12298
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012299 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012300 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 vim_free(keys_buf);
12302 if (rhs != NULL)
12303 {
12304 ga_init(&ga);
12305 ga.ga_itemsize = 1;
12306 ga.ga_growsize = 40;
12307
12308 while (*rhs != NUL)
12309 ga_concat(&ga, str2special(&rhs, FALSE));
12310
12311 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313 }
12314}
12315
12316/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012317 * "map()" function
12318 */
12319 static void
12320f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012321 typval_T *argvars;
12322 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012323{
12324 filter_map(argvars, rettv, TRUE);
12325}
12326
12327/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 */
12330 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012331f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012332 typval_T *argvars;
12333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336}
12337
12338/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012339 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 */
12341 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012342f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012343 typval_T *argvars;
12344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012346 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347}
12348
Bram Moolenaar33570922005-01-25 22:26:29 +000012349static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350
12351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012352find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012353 typval_T *argvars;
12354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 int type;
12356{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012357 char_u *str = NULL;
12358 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 char_u *pat;
12360 regmatch_T regmatch;
12361 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012362 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 char_u *save_cpo;
12364 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012365 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012366 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012367 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012368 list_T *l = NULL;
12369 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012370 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012371 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372
12373 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12374 save_cpo = p_cpo;
12375 p_cpo = (char_u *)"";
12376
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012377 rettv->vval.v_number = -1;
12378 if (type == 3)
12379 {
12380 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012381 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012382 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012383 }
12384 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012386 rettv->v_type = VAR_STRING;
12387 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012390 if (argvars[0].v_type == VAR_LIST)
12391 {
12392 if ((l = argvars[0].vval.v_list) == NULL)
12393 goto theend;
12394 li = l->lv_first;
12395 }
12396 else
12397 expr = str = get_tv_string(&argvars[0]);
12398
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012399 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12400 if (pat == NULL)
12401 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012402
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012403 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 int error = FALSE;
12406
12407 start = get_tv_number_chk(&argvars[2], &error);
12408 if (error)
12409 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012410 if (l != NULL)
12411 {
12412 li = list_find(l, start);
12413 if (li == NULL)
12414 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012415 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012416 }
12417 else
12418 {
12419 if (start < 0)
12420 start = 0;
12421 if (start > (long)STRLEN(str))
12422 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012423 /* When "count" argument is there ignore matches before "start",
12424 * otherwise skip part of the string. Differs when pattern is "^"
12425 * or "\<". */
12426 if (argvars[3].v_type != VAR_UNKNOWN)
12427 startcol = start;
12428 else
12429 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012430 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012431
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012432 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012433 nth = get_tv_number_chk(&argvars[3], &error);
12434 if (error)
12435 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 }
12437
12438 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12439 if (regmatch.regprog != NULL)
12440 {
12441 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012442
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012443 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012444 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012445 if (l != NULL)
12446 {
12447 if (li == NULL)
12448 {
12449 match = FALSE;
12450 break;
12451 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012452 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012453 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012454 if (str == NULL)
12455 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012456 }
12457
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012458 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012459
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012460 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012461 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012462 if (l == NULL && !match)
12463 break;
12464
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012465 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012466 if (l != NULL)
12467 {
12468 li = li->li_next;
12469 ++idx;
12470 }
12471 else
12472 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012473#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012474 startcol = (colnr_T)(regmatch.startp[0]
12475 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012476#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012477 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012478#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012479 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012480 }
12481
12482 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012484 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012485 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012486 int i;
12487
12488 /* return list with matched string and submatches */
12489 for (i = 0; i < NSUBEXP; ++i)
12490 {
12491 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012492 {
12493 if (list_append_string(rettv->vval.v_list,
12494 (char_u *)"", 0) == FAIL)
12495 break;
12496 }
12497 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012498 regmatch.startp[i],
12499 (int)(regmatch.endp[i] - regmatch.startp[i]))
12500 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012501 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012502 }
12503 }
12504 else if (type == 2)
12505 {
12506 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012507 if (l != NULL)
12508 copy_tv(&li->li_tv, rettv);
12509 else
12510 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012512 }
12513 else if (l != NULL)
12514 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515 else
12516 {
12517 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012518 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 (varnumber_T)(regmatch.startp[0] - str);
12520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012523 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 }
12525 }
12526 vim_free(regmatch.regprog);
12527 }
12528
12529theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012530 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531 p_cpo = save_cpo;
12532}
12533
12534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012535 * "match()" function
12536 */
12537 static void
12538f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012539 typval_T *argvars;
12540 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012541{
12542 find_some_match(argvars, rettv, 1);
12543}
12544
12545/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012546 * "matchadd()" function
12547 */
12548 static void
12549f_matchadd(argvars, rettv)
12550 typval_T *argvars;
12551 typval_T *rettv;
12552{
12553#ifdef FEAT_SEARCH_EXTRA
12554 char_u buf[NUMBUFLEN];
12555 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12556 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12557 int prio = 10; /* default priority */
12558 int id = -1;
12559 int error = FALSE;
12560
12561 rettv->vval.v_number = -1;
12562
12563 if (grp == NULL || pat == NULL)
12564 return;
12565 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012566 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012567 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012568 if (argvars[3].v_type != VAR_UNKNOWN)
12569 id = get_tv_number_chk(&argvars[3], &error);
12570 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012571 if (error == TRUE)
12572 return;
12573 if (id >= 1 && id <= 3)
12574 {
12575 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12576 return;
12577 }
12578
12579 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12580#endif
12581}
12582
12583/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012584 * "matcharg()" function
12585 */
12586 static void
12587f_matcharg(argvars, rettv)
12588 typval_T *argvars;
12589 typval_T *rettv;
12590{
12591 if (rettv_list_alloc(rettv) == OK)
12592 {
12593#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012594 int id = get_tv_number(&argvars[0]);
12595 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012596
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012597 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012598 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012599 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12600 {
12601 list_append_string(rettv->vval.v_list,
12602 syn_id2name(m->hlg_id), -1);
12603 list_append_string(rettv->vval.v_list, m->pattern, -1);
12604 }
12605 else
12606 {
12607 list_append_string(rettv->vval.v_list, NUL, -1);
12608 list_append_string(rettv->vval.v_list, NUL, -1);
12609 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012610 }
12611#endif
12612 }
12613}
12614
12615/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012616 * "matchdelete()" function
12617 */
12618 static void
12619f_matchdelete(argvars, rettv)
12620 typval_T *argvars;
12621 typval_T *rettv;
12622{
12623#ifdef FEAT_SEARCH_EXTRA
12624 rettv->vval.v_number = match_delete(curwin,
12625 (int)get_tv_number(&argvars[0]), TRUE);
12626#endif
12627}
12628
12629/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012630 * "matchend()" function
12631 */
12632 static void
12633f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012634 typval_T *argvars;
12635 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012636{
12637 find_some_match(argvars, rettv, 0);
12638}
12639
12640/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012641 * "matchlist()" function
12642 */
12643 static void
12644f_matchlist(argvars, rettv)
12645 typval_T *argvars;
12646 typval_T *rettv;
12647{
12648 find_some_match(argvars, rettv, 3);
12649}
12650
12651/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012652 * "matchstr()" function
12653 */
12654 static void
12655f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012656 typval_T *argvars;
12657 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012658{
12659 find_some_match(argvars, rettv, 2);
12660}
12661
Bram Moolenaar33570922005-01-25 22:26:29 +000012662static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012663
12664 static void
12665max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012666 typval_T *argvars;
12667 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012668 int domax;
12669{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012670 long n = 0;
12671 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012673
12674 if (argvars[0].v_type == VAR_LIST)
12675 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 list_T *l;
12677 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012678
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012679 l = argvars[0].vval.v_list;
12680 if (l != NULL)
12681 {
12682 li = l->lv_first;
12683 if (li != NULL)
12684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012686 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012687 {
12688 li = li->li_next;
12689 if (li == NULL)
12690 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012691 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012692 if (domax ? i > n : i < n)
12693 n = i;
12694 }
12695 }
12696 }
12697 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012698 else if (argvars[0].v_type == VAR_DICT)
12699 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012700 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012701 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012703 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012704
12705 d = argvars[0].vval.v_dict;
12706 if (d != NULL)
12707 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012708 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012709 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012710 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012711 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012712 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012713 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012715 if (first)
12716 {
12717 n = i;
12718 first = FALSE;
12719 }
12720 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012721 n = i;
12722 }
12723 }
12724 }
12725 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012726 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012727 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012728 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012729}
12730
12731/*
12732 * "max()" function
12733 */
12734 static void
12735f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012736 typval_T *argvars;
12737 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012738{
12739 max_min(argvars, rettv, TRUE);
12740}
12741
12742/*
12743 * "min()" function
12744 */
12745 static void
12746f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012747 typval_T *argvars;
12748 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012749{
12750 max_min(argvars, rettv, FALSE);
12751}
12752
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012753static int mkdir_recurse __ARGS((char_u *dir, int prot));
12754
12755/*
12756 * Create the directory in which "dir" is located, and higher levels when
12757 * needed.
12758 */
12759 static int
12760mkdir_recurse(dir, prot)
12761 char_u *dir;
12762 int prot;
12763{
12764 char_u *p;
12765 char_u *updir;
12766 int r = FAIL;
12767
12768 /* Get end of directory name in "dir".
12769 * We're done when it's "/" or "c:/". */
12770 p = gettail_sep(dir);
12771 if (p <= get_past_head(dir))
12772 return OK;
12773
12774 /* If the directory exists we're done. Otherwise: create it.*/
12775 updir = vim_strnsave(dir, (int)(p - dir));
12776 if (updir == NULL)
12777 return FAIL;
12778 if (mch_isdir(updir))
12779 r = OK;
12780 else if (mkdir_recurse(updir, prot) == OK)
12781 r = vim_mkdir_emsg(updir, prot);
12782 vim_free(updir);
12783 return r;
12784}
12785
12786#ifdef vim_mkdir
12787/*
12788 * "mkdir()" function
12789 */
12790 static void
12791f_mkdir(argvars, rettv)
12792 typval_T *argvars;
12793 typval_T *rettv;
12794{
12795 char_u *dir;
12796 char_u buf[NUMBUFLEN];
12797 int prot = 0755;
12798
12799 rettv->vval.v_number = FAIL;
12800 if (check_restricted() || check_secure())
12801 return;
12802
12803 dir = get_tv_string_buf(&argvars[0], buf);
12804 if (argvars[1].v_type != VAR_UNKNOWN)
12805 {
12806 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 prot = get_tv_number_chk(&argvars[2], NULL);
12808 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012809 mkdir_recurse(dir, prot);
12810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012811 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012812}
12813#endif
12814
Bram Moolenaar0d660222005-01-07 21:51:51 +000012815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 * "mode()" function
12817 */
12818/*ARGSUSED*/
12819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012821 typval_T *argvars;
12822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823{
12824 char_u buf[2];
12825
12826#ifdef FEAT_VISUAL
12827 if (VIsual_active)
12828 {
12829 if (VIsual_select)
12830 buf[0] = VIsual_mode + 's' - 'v';
12831 else
12832 buf[0] = VIsual_mode;
12833 }
12834 else
12835#endif
12836 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12837 buf[0] = 'r';
12838 else if (State & INSERT)
12839 {
12840 if (State & REPLACE_FLAG)
12841 buf[0] = 'R';
12842 else
12843 buf[0] = 'i';
12844 }
12845 else if (State & CMDLINE)
12846 buf[0] = 'c';
12847 else
12848 buf[0] = 'n';
12849
12850 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 rettv->vval.v_string = vim_strsave(buf);
12852 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853}
12854
12855/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012856 * "nextnonblank()" function
12857 */
12858 static void
12859f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012860 typval_T *argvars;
12861 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012862{
12863 linenr_T lnum;
12864
12865 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012867 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012868 {
12869 lnum = 0;
12870 break;
12871 }
12872 if (*skipwhite(ml_get(lnum)) != NUL)
12873 break;
12874 }
12875 rettv->vval.v_number = lnum;
12876}
12877
12878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 * "nr2char()" function
12880 */
12881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012883 typval_T *argvars;
12884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885{
12886 char_u buf[NUMBUFLEN];
12887
12888#ifdef FEAT_MBYTE
12889 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 else
12892#endif
12893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 buf[1] = NUL;
12896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->v_type = VAR_STRING;
12898 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012899}
12900
12901/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012902 * "pathshorten()" function
12903 */
12904 static void
12905f_pathshorten(argvars, rettv)
12906 typval_T *argvars;
12907 typval_T *rettv;
12908{
12909 char_u *p;
12910
12911 rettv->v_type = VAR_STRING;
12912 p = get_tv_string_chk(&argvars[0]);
12913 if (p == NULL)
12914 rettv->vval.v_string = NULL;
12915 else
12916 {
12917 p = vim_strsave(p);
12918 rettv->vval.v_string = p;
12919 if (p != NULL)
12920 shorten_dir(p);
12921 }
12922}
12923
12924/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012925 * "prevnonblank()" function
12926 */
12927 static void
12928f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012929 typval_T *argvars;
12930 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012931{
12932 linenr_T lnum;
12933
12934 lnum = get_tv_lnum(argvars);
12935 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12936 lnum = 0;
12937 else
12938 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12939 --lnum;
12940 rettv->vval.v_number = lnum;
12941}
12942
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012943#ifdef HAVE_STDARG_H
12944/* This dummy va_list is here because:
12945 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12946 * - locally in the function results in a "used before set" warning
12947 * - using va_start() to initialize it gives "function with fixed args" error */
12948static va_list ap;
12949#endif
12950
Bram Moolenaar8c711452005-01-14 21:53:12 +000012951/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012952 * "printf()" function
12953 */
12954 static void
12955f_printf(argvars, rettv)
12956 typval_T *argvars;
12957 typval_T *rettv;
12958{
12959 rettv->v_type = VAR_STRING;
12960 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012961#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012962 {
12963 char_u buf[NUMBUFLEN];
12964 int len;
12965 char_u *s;
12966 int saved_did_emsg = did_emsg;
12967 char *fmt;
12968
12969 /* Get the required length, allocate the buffer and do it for real. */
12970 did_emsg = FALSE;
12971 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012972 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012973 if (!did_emsg)
12974 {
12975 s = alloc(len + 1);
12976 if (s != NULL)
12977 {
12978 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012979 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012980 }
12981 }
12982 did_emsg |= saved_did_emsg;
12983 }
12984#endif
12985}
12986
12987/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012988 * "pumvisible()" function
12989 */
12990/*ARGSUSED*/
12991 static void
12992f_pumvisible(argvars, rettv)
12993 typval_T *argvars;
12994 typval_T *rettv;
12995{
12996 rettv->vval.v_number = 0;
12997#ifdef FEAT_INS_EXPAND
12998 if (pum_visible())
12999 rettv->vval.v_number = 1;
13000#endif
13001}
13002
13003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013004 * "range()" function
13005 */
13006 static void
13007f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013008 typval_T *argvars;
13009 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013010{
13011 long start;
13012 long end;
13013 long stride = 1;
13014 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013015 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013017 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013018 if (argvars[1].v_type == VAR_UNKNOWN)
13019 {
13020 end = start - 1;
13021 start = 0;
13022 }
13023 else
13024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013025 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013026 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013027 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013028 }
13029
13030 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013031 if (error)
13032 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013033 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013034 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013035 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013036 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013037 else
13038 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013039 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013040 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013041 if (list_append_number(rettv->vval.v_list,
13042 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013043 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013044 }
13045}
13046
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013047/*
13048 * "readfile()" function
13049 */
13050 static void
13051f_readfile(argvars, rettv)
13052 typval_T *argvars;
13053 typval_T *rettv;
13054{
13055 int binary = FALSE;
13056 char_u *fname;
13057 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013058 listitem_T *li;
13059#define FREAD_SIZE 200 /* optimized for text lines */
13060 char_u buf[FREAD_SIZE];
13061 int readlen; /* size of last fread() */
13062 int buflen; /* nr of valid chars in buf[] */
13063 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13064 int tolist; /* first byte in buf[] still to be put in list */
13065 int chop; /* how many CR to chop off */
13066 char_u *prev = NULL; /* previously read bytes, if any */
13067 int prevlen = 0; /* length of "prev" if not NULL */
13068 char_u *s;
13069 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013070 long maxline = MAXLNUM;
13071 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013072
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013073 if (argvars[1].v_type != VAR_UNKNOWN)
13074 {
13075 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13076 binary = TRUE;
13077 if (argvars[2].v_type != VAR_UNKNOWN)
13078 maxline = get_tv_number(&argvars[2]);
13079 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013080
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013081 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013082 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013083
13084 /* Always open the file in binary mode, library functions have a mind of
13085 * their own about CR-LF conversion. */
13086 fname = get_tv_string(&argvars[0]);
13087 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13088 {
13089 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13090 return;
13091 }
13092
13093 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013094 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013095 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013096 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013097 buflen = filtd + readlen;
13098 tolist = 0;
13099 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13100 {
13101 if (buf[filtd] == '\n' || readlen <= 0)
13102 {
13103 /* Only when in binary mode add an empty list item when the
13104 * last line ends in a '\n'. */
13105 if (!binary && readlen == 0 && filtd == 0)
13106 break;
13107
13108 /* Found end-of-line or end-of-file: add a text line to the
13109 * list. */
13110 chop = 0;
13111 if (!binary)
13112 while (filtd - chop - 1 >= tolist
13113 && buf[filtd - chop - 1] == '\r')
13114 ++chop;
13115 len = filtd - tolist - chop;
13116 if (prev == NULL)
13117 s = vim_strnsave(buf + tolist, len);
13118 else
13119 {
13120 s = alloc((unsigned)(prevlen + len + 1));
13121 if (s != NULL)
13122 {
13123 mch_memmove(s, prev, prevlen);
13124 vim_free(prev);
13125 prev = NULL;
13126 mch_memmove(s + prevlen, buf + tolist, len);
13127 s[prevlen + len] = NUL;
13128 }
13129 }
13130 tolist = filtd + 1;
13131
13132 li = listitem_alloc();
13133 if (li == NULL)
13134 {
13135 vim_free(s);
13136 break;
13137 }
13138 li->li_tv.v_type = VAR_STRING;
13139 li->li_tv.v_lock = 0;
13140 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013141 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013142
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013143 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013144 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013145 if (readlen <= 0)
13146 break;
13147 }
13148 else if (buf[filtd] == NUL)
13149 buf[filtd] = '\n';
13150 }
13151 if (readlen <= 0)
13152 break;
13153
13154 if (tolist == 0)
13155 {
13156 /* "buf" is full, need to move text to an allocated buffer */
13157 if (prev == NULL)
13158 {
13159 prev = vim_strnsave(buf, buflen);
13160 prevlen = buflen;
13161 }
13162 else
13163 {
13164 s = alloc((unsigned)(prevlen + buflen));
13165 if (s != NULL)
13166 {
13167 mch_memmove(s, prev, prevlen);
13168 mch_memmove(s + prevlen, buf, buflen);
13169 vim_free(prev);
13170 prev = s;
13171 prevlen += buflen;
13172 }
13173 }
13174 filtd = 0;
13175 }
13176 else
13177 {
13178 mch_memmove(buf, buf + tolist, buflen - tolist);
13179 filtd -= tolist;
13180 }
13181 }
13182
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013183 /*
13184 * For a negative line count use only the lines at the end of the file,
13185 * free the rest.
13186 */
13187 if (maxline < 0)
13188 while (cnt > -maxline)
13189 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013190 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013191 --cnt;
13192 }
13193
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013194 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013195 fclose(fd);
13196}
13197
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013198#if defined(FEAT_RELTIME)
13199static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13200
13201/*
13202 * Convert a List to proftime_T.
13203 * Return FAIL when there is something wrong.
13204 */
13205 static int
13206list2proftime(arg, tm)
13207 typval_T *arg;
13208 proftime_T *tm;
13209{
13210 long n1, n2;
13211 int error = FALSE;
13212
13213 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13214 || arg->vval.v_list->lv_len != 2)
13215 return FAIL;
13216 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13217 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13218# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013219 tm->HighPart = n1;
13220 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013221# else
13222 tm->tv_sec = n1;
13223 tm->tv_usec = n2;
13224# endif
13225 return error ? FAIL : OK;
13226}
13227#endif /* FEAT_RELTIME */
13228
13229/*
13230 * "reltime()" function
13231 */
13232 static void
13233f_reltime(argvars, rettv)
13234 typval_T *argvars;
13235 typval_T *rettv;
13236{
13237#ifdef FEAT_RELTIME
13238 proftime_T res;
13239 proftime_T start;
13240
13241 if (argvars[0].v_type == VAR_UNKNOWN)
13242 {
13243 /* No arguments: get current time. */
13244 profile_start(&res);
13245 }
13246 else if (argvars[1].v_type == VAR_UNKNOWN)
13247 {
13248 if (list2proftime(&argvars[0], &res) == FAIL)
13249 return;
13250 profile_end(&res);
13251 }
13252 else
13253 {
13254 /* Two arguments: compute the difference. */
13255 if (list2proftime(&argvars[0], &start) == FAIL
13256 || list2proftime(&argvars[1], &res) == FAIL)
13257 return;
13258 profile_sub(&res, &start);
13259 }
13260
13261 if (rettv_list_alloc(rettv) == OK)
13262 {
13263 long n1, n2;
13264
13265# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013266 n1 = res.HighPart;
13267 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013268# else
13269 n1 = res.tv_sec;
13270 n2 = res.tv_usec;
13271# endif
13272 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13273 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13274 }
13275#endif
13276}
13277
13278/*
13279 * "reltimestr()" function
13280 */
13281 static void
13282f_reltimestr(argvars, rettv)
13283 typval_T *argvars;
13284 typval_T *rettv;
13285{
13286#ifdef FEAT_RELTIME
13287 proftime_T tm;
13288#endif
13289
13290 rettv->v_type = VAR_STRING;
13291 rettv->vval.v_string = NULL;
13292#ifdef FEAT_RELTIME
13293 if (list2proftime(&argvars[0], &tm) == OK)
13294 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13295#endif
13296}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013297
Bram Moolenaar0d660222005-01-07 21:51:51 +000013298#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13299static void make_connection __ARGS((void));
13300static int check_connection __ARGS((void));
13301
13302 static void
13303make_connection()
13304{
13305 if (X_DISPLAY == NULL
13306# ifdef FEAT_GUI
13307 && !gui.in_use
13308# endif
13309 )
13310 {
13311 x_force_connect = TRUE;
13312 setup_term_clip();
13313 x_force_connect = FALSE;
13314 }
13315}
13316
13317 static int
13318check_connection()
13319{
13320 make_connection();
13321 if (X_DISPLAY == NULL)
13322 {
13323 EMSG(_("E240: No connection to Vim server"));
13324 return FAIL;
13325 }
13326 return OK;
13327}
13328#endif
13329
13330#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013331static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013332
13333 static void
13334remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013335 typval_T *argvars;
13336 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013337 int expr;
13338{
13339 char_u *server_name;
13340 char_u *keys;
13341 char_u *r = NULL;
13342 char_u buf[NUMBUFLEN];
13343# ifdef WIN32
13344 HWND w;
13345# else
13346 Window w;
13347# endif
13348
13349 if (check_restricted() || check_secure())
13350 return;
13351
13352# ifdef FEAT_X11
13353 if (check_connection() == FAIL)
13354 return;
13355# endif
13356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013357 server_name = get_tv_string_chk(&argvars[0]);
13358 if (server_name == NULL)
13359 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013360 keys = get_tv_string_buf(&argvars[1], buf);
13361# ifdef WIN32
13362 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13363# else
13364 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13365 < 0)
13366# endif
13367 {
13368 if (r != NULL)
13369 EMSG(r); /* sending worked but evaluation failed */
13370 else
13371 EMSG2(_("E241: Unable to send to %s"), server_name);
13372 return;
13373 }
13374
13375 rettv->vval.v_string = r;
13376
13377 if (argvars[2].v_type != VAR_UNKNOWN)
13378 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013380 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013381 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013382
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013383 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013384 v.di_tv.v_type = VAR_STRING;
13385 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 idvar = get_tv_string_chk(&argvars[2]);
13387 if (idvar != NULL)
13388 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013389 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390 }
13391}
13392#endif
13393
13394/*
13395 * "remote_expr()" function
13396 */
13397/*ARGSUSED*/
13398 static void
13399f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013400 typval_T *argvars;
13401 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013402{
13403 rettv->v_type = VAR_STRING;
13404 rettv->vval.v_string = NULL;
13405#ifdef FEAT_CLIENTSERVER
13406 remote_common(argvars, rettv, TRUE);
13407#endif
13408}
13409
13410/*
13411 * "remote_foreground()" function
13412 */
13413/*ARGSUSED*/
13414 static void
13415f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013416 typval_T *argvars;
13417 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013418{
13419 rettv->vval.v_number = 0;
13420#ifdef FEAT_CLIENTSERVER
13421# ifdef WIN32
13422 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013423 {
13424 char_u *server_name = get_tv_string_chk(&argvars[0]);
13425
13426 if (server_name != NULL)
13427 serverForeground(server_name);
13428 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013429# else
13430 /* Send a foreground() expression to the server. */
13431 argvars[1].v_type = VAR_STRING;
13432 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13433 argvars[2].v_type = VAR_UNKNOWN;
13434 remote_common(argvars, rettv, TRUE);
13435 vim_free(argvars[1].vval.v_string);
13436# endif
13437#endif
13438}
13439
13440/*ARGSUSED*/
13441 static void
13442f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013443 typval_T *argvars;
13444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013445{
13446#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013447 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013448 char_u *s = NULL;
13449# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013450 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013451# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013452 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013453
13454 if (check_restricted() || check_secure())
13455 {
13456 rettv->vval.v_number = -1;
13457 return;
13458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013459 serverid = get_tv_string_chk(&argvars[0]);
13460 if (serverid == NULL)
13461 {
13462 rettv->vval.v_number = -1;
13463 return; /* type error; errmsg already given */
13464 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013465# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013466 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013467 if (n == 0)
13468 rettv->vval.v_number = -1;
13469 else
13470 {
13471 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13472 rettv->vval.v_number = (s != NULL);
13473 }
13474# else
13475 rettv->vval.v_number = 0;
13476 if (check_connection() == FAIL)
13477 return;
13478
13479 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013480 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013481# endif
13482
13483 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013485 char_u *retvar;
13486
Bram Moolenaar33570922005-01-25 22:26:29 +000013487 v.di_tv.v_type = VAR_STRING;
13488 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013489 retvar = get_tv_string_chk(&argvars[1]);
13490 if (retvar != NULL)
13491 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013492 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013493 }
13494#else
13495 rettv->vval.v_number = -1;
13496#endif
13497}
13498
13499/*ARGSUSED*/
13500 static void
13501f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013502 typval_T *argvars;
13503 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013504{
13505 char_u *r = NULL;
13506
13507#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013508 char_u *serverid = get_tv_string_chk(&argvars[0]);
13509
13510 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013511 {
13512# ifdef WIN32
13513 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013514 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013515
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013516 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013517 if (n != 0)
13518 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13519 if (r == NULL)
13520# else
13521 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013522 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013523# endif
13524 EMSG(_("E277: Unable to read a server reply"));
13525 }
13526#endif
13527 rettv->v_type = VAR_STRING;
13528 rettv->vval.v_string = r;
13529}
13530
13531/*
13532 * "remote_send()" function
13533 */
13534/*ARGSUSED*/
13535 static void
13536f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 typval_T *argvars;
13538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013539{
13540 rettv->v_type = VAR_STRING;
13541 rettv->vval.v_string = NULL;
13542#ifdef FEAT_CLIENTSERVER
13543 remote_common(argvars, rettv, FALSE);
13544#endif
13545}
13546
13547/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013548 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013549 */
13550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013552 typval_T *argvars;
13553 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013554{
Bram Moolenaar33570922005-01-25 22:26:29 +000013555 list_T *l;
13556 listitem_T *item, *item2;
13557 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013558 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013559 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013560 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013561 dict_T *d;
13562 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013563
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013564 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013565 if (argvars[0].v_type == VAR_DICT)
13566 {
13567 if (argvars[2].v_type != VAR_UNKNOWN)
13568 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013569 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013570 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013572 key = get_tv_string_chk(&argvars[1]);
13573 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013575 di = dict_find(d, key, -1);
13576 if (di == NULL)
13577 EMSG2(_(e_dictkey), key);
13578 else
13579 {
13580 *rettv = di->di_tv;
13581 init_tv(&di->di_tv);
13582 dictitem_remove(d, di);
13583 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013584 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013585 }
13586 }
13587 else if (argvars[0].v_type != VAR_LIST)
13588 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013589 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013590 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013592 int error = FALSE;
13593
13594 idx = get_tv_number_chk(&argvars[1], &error);
13595 if (error)
13596 ; /* type error: do nothing, errmsg already given */
13597 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013598 EMSGN(_(e_listidx), idx);
13599 else
13600 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013601 if (argvars[2].v_type == VAR_UNKNOWN)
13602 {
13603 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013604 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013605 *rettv = item->li_tv;
13606 vim_free(item);
13607 }
13608 else
13609 {
13610 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013611 end = get_tv_number_chk(&argvars[2], &error);
13612 if (error)
13613 ; /* type error: do nothing */
13614 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013615 EMSGN(_(e_listidx), end);
13616 else
13617 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013618 int cnt = 0;
13619
13620 for (li = item; li != NULL; li = li->li_next)
13621 {
13622 ++cnt;
13623 if (li == item2)
13624 break;
13625 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013626 if (li == NULL) /* didn't find "item2" after "item" */
13627 EMSG(_(e_invrange));
13628 else
13629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013630 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013631 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013632 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013633 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013634 l->lv_first = item;
13635 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013636 item->li_prev = NULL;
13637 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013638 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013639 }
13640 }
13641 }
13642 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013643 }
13644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645}
13646
13647/*
13648 * "rename({from}, {to})" function
13649 */
13650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013652 typval_T *argvars;
13653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654{
13655 char_u buf[NUMBUFLEN];
13656
13657 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13661 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662}
13663
13664/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013665 * "repeat()" function
13666 */
13667/*ARGSUSED*/
13668 static void
13669f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013670 typval_T *argvars;
13671 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013672{
13673 char_u *p;
13674 int n;
13675 int slen;
13676 int len;
13677 char_u *r;
13678 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013679
13680 n = get_tv_number(&argvars[1]);
13681 if (argvars[0].v_type == VAR_LIST)
13682 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013683 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013684 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013685 if (list_extend(rettv->vval.v_list,
13686 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013687 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013688 }
13689 else
13690 {
13691 p = get_tv_string(&argvars[0]);
13692 rettv->v_type = VAR_STRING;
13693 rettv->vval.v_string = NULL;
13694
13695 slen = (int)STRLEN(p);
13696 len = slen * n;
13697 if (len <= 0)
13698 return;
13699
13700 r = alloc(len + 1);
13701 if (r != NULL)
13702 {
13703 for (i = 0; i < n; i++)
13704 mch_memmove(r + i * slen, p, (size_t)slen);
13705 r[len] = NUL;
13706 }
13707
13708 rettv->vval.v_string = r;
13709 }
13710}
13711
13712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 * "resolve()" function
13714 */
13715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013717 typval_T *argvars;
13718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719{
13720 char_u *p;
13721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723#ifdef FEAT_SHORTCUT
13724 {
13725 char_u *v = NULL;
13726
13727 v = mch_resolve_shortcut(p);
13728 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 }
13733#else
13734# ifdef HAVE_READLINK
13735 {
13736 char_u buf[MAXPATHL + 1];
13737 char_u *cpy;
13738 int len;
13739 char_u *remain = NULL;
13740 char_u *q;
13741 int is_relative_to_current = FALSE;
13742 int has_trailing_pathsep = FALSE;
13743 int limit = 100;
13744
13745 p = vim_strsave(p);
13746
13747 if (p[0] == '.' && (vim_ispathsep(p[1])
13748 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13749 is_relative_to_current = TRUE;
13750
13751 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013752 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 has_trailing_pathsep = TRUE;
13754
13755 q = getnextcomp(p);
13756 if (*q != NUL)
13757 {
13758 /* Separate the first path component in "p", and keep the
13759 * remainder (beginning with the path separator). */
13760 remain = vim_strsave(q - 1);
13761 q[-1] = NUL;
13762 }
13763
13764 for (;;)
13765 {
13766 for (;;)
13767 {
13768 len = readlink((char *)p, (char *)buf, MAXPATHL);
13769 if (len <= 0)
13770 break;
13771 buf[len] = NUL;
13772
13773 if (limit-- == 0)
13774 {
13775 vim_free(p);
13776 vim_free(remain);
13777 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 goto fail;
13780 }
13781
13782 /* Ensure that the result will have a trailing path separator
13783 * if the argument has one. */
13784 if (remain == NULL && has_trailing_pathsep)
13785 add_pathsep(buf);
13786
13787 /* Separate the first path component in the link value and
13788 * concatenate the remainders. */
13789 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13790 if (*q != NUL)
13791 {
13792 if (remain == NULL)
13793 remain = vim_strsave(q - 1);
13794 else
13795 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013796 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 if (cpy != NULL)
13798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 vim_free(remain);
13800 remain = cpy;
13801 }
13802 }
13803 q[-1] = NUL;
13804 }
13805
13806 q = gettail(p);
13807 if (q > p && *q == NUL)
13808 {
13809 /* Ignore trailing path separator. */
13810 q[-1] = NUL;
13811 q = gettail(p);
13812 }
13813 if (q > p && !mch_isFullName(buf))
13814 {
13815 /* symlink is relative to directory of argument */
13816 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13817 if (cpy != NULL)
13818 {
13819 STRCPY(cpy, p);
13820 STRCPY(gettail(cpy), buf);
13821 vim_free(p);
13822 p = cpy;
13823 }
13824 }
13825 else
13826 {
13827 vim_free(p);
13828 p = vim_strsave(buf);
13829 }
13830 }
13831
13832 if (remain == NULL)
13833 break;
13834
13835 /* Append the first path component of "remain" to "p". */
13836 q = getnextcomp(remain + 1);
13837 len = q - remain - (*q != NUL);
13838 cpy = vim_strnsave(p, STRLEN(p) + len);
13839 if (cpy != NULL)
13840 {
13841 STRNCAT(cpy, remain, len);
13842 vim_free(p);
13843 p = cpy;
13844 }
13845 /* Shorten "remain". */
13846 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013847 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 else
13849 {
13850 vim_free(remain);
13851 remain = NULL;
13852 }
13853 }
13854
13855 /* If the result is a relative path name, make it explicitly relative to
13856 * the current directory if and only if the argument had this form. */
13857 if (!vim_ispathsep(*p))
13858 {
13859 if (is_relative_to_current
13860 && *p != NUL
13861 && !(p[0] == '.'
13862 && (p[1] == NUL
13863 || vim_ispathsep(p[1])
13864 || (p[1] == '.'
13865 && (p[2] == NUL
13866 || vim_ispathsep(p[2]))))))
13867 {
13868 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013869 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 if (cpy != NULL)
13871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 vim_free(p);
13873 p = cpy;
13874 }
13875 }
13876 else if (!is_relative_to_current)
13877 {
13878 /* Strip leading "./". */
13879 q = p;
13880 while (q[0] == '.' && vim_ispathsep(q[1]))
13881 q += 2;
13882 if (q > p)
13883 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13884 }
13885 }
13886
13887 /* Ensure that the result will have no trailing path separator
13888 * if the argument had none. But keep "/" or "//". */
13889 if (!has_trailing_pathsep)
13890 {
13891 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013892 if (after_pathsep(p, q))
13893 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 }
13895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013896 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 }
13898# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013899 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900# endif
13901#endif
13902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013903 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904
13905#ifdef HAVE_READLINK
13906fail:
13907#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909}
13910
13911/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013912 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 */
13914 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013915f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013916 typval_T *argvars;
13917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918{
Bram Moolenaar33570922005-01-25 22:26:29 +000013919 list_T *l;
13920 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921
Bram Moolenaar0d660222005-01-07 21:51:51 +000013922 rettv->vval.v_number = 0;
13923 if (argvars[0].v_type != VAR_LIST)
13924 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013925 else if ((l = argvars[0].vval.v_list) != NULL
13926 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013927 {
13928 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013929 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013930 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013931 while (li != NULL)
13932 {
13933 ni = li->li_prev;
13934 list_append(l, li);
13935 li = ni;
13936 }
13937 rettv->vval.v_list = l;
13938 rettv->v_type = VAR_LIST;
13939 ++l->lv_refcount;
13940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941}
13942
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013943#define SP_NOMOVE 0x01 /* don't move cursor */
13944#define SP_REPEAT 0x02 /* repeat to find outer pair */
13945#define SP_RETCOUNT 0x04 /* return matchcount */
13946#define SP_SETPCMARK 0x08 /* set previous context mark */
13947#define SP_START 0x10 /* accept match at start position */
13948#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13949#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013950
Bram Moolenaar33570922005-01-25 22:26:29 +000013951static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013952
13953/*
13954 * Get flags for a search function.
13955 * Possibly sets "p_ws".
13956 * Returns BACKWARD, FORWARD or zero (for an error).
13957 */
13958 static int
13959get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013960 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013961 int *flagsp;
13962{
13963 int dir = FORWARD;
13964 char_u *flags;
13965 char_u nbuf[NUMBUFLEN];
13966 int mask;
13967
13968 if (varp->v_type != VAR_UNKNOWN)
13969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 flags = get_tv_string_buf_chk(varp, nbuf);
13971 if (flags == NULL)
13972 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013973 while (*flags != NUL)
13974 {
13975 switch (*flags)
13976 {
13977 case 'b': dir = BACKWARD; break;
13978 case 'w': p_ws = TRUE; break;
13979 case 'W': p_ws = FALSE; break;
13980 default: mask = 0;
13981 if (flagsp != NULL)
13982 switch (*flags)
13983 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013984 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013985 case 'e': mask = SP_END; break;
13986 case 'm': mask = SP_RETCOUNT; break;
13987 case 'n': mask = SP_NOMOVE; break;
13988 case 'p': mask = SP_SUBPAT; break;
13989 case 'r': mask = SP_REPEAT; break;
13990 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013991 }
13992 if (mask == 0)
13993 {
13994 EMSG2(_(e_invarg2), flags);
13995 dir = 0;
13996 }
13997 else
13998 *flagsp |= mask;
13999 }
14000 if (dir == 0)
14001 break;
14002 ++flags;
14003 }
14004 }
14005 return dir;
14006}
14007
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014009 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014011 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014012search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014013 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014014 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014015 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014017 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 char_u *pat;
14019 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014020 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 int save_p_ws = p_ws;
14022 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014023 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014024 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014025 proftime_T tm;
14026#ifdef FEAT_RELTIME
14027 long time_limit = 0;
14028#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014029 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014030 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014033 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014034 if (dir == 0)
14035 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014036 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014037 if (flags & SP_START)
14038 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014039 if (flags & SP_END)
14040 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014041
Bram Moolenaar76929292008-01-06 19:07:36 +000014042 /* Optional arguments: line number to stop searching and timeout. */
14043 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014044 {
14045 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14046 if (lnum_stop < 0)
14047 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014048#ifdef FEAT_RELTIME
14049 if (argvars[3].v_type != VAR_UNKNOWN)
14050 {
14051 time_limit = get_tv_number_chk(&argvars[3], NULL);
14052 if (time_limit < 0)
14053 goto theend;
14054 }
14055#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014056 }
14057
Bram Moolenaar76929292008-01-06 19:07:36 +000014058#ifdef FEAT_RELTIME
14059 /* Set the time limit, if there is one. */
14060 profile_setlimit(time_limit, &tm);
14061#endif
14062
Bram Moolenaar231334e2005-07-25 20:46:57 +000014063 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014064 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014065 * Check to make sure only those flags are set.
14066 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14067 * flags cannot be set. Check for that condition also.
14068 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014069 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014070 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014072 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014073 goto theend;
14074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014076 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014077 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014078 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014079 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014081 if (flags & SP_SUBPAT)
14082 retval = subpatnum;
14083 else
14084 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014085 if (flags & SP_SETPCMARK)
14086 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014088 if (match_pos != NULL)
14089 {
14090 /* Store the match cursor position */
14091 match_pos->lnum = pos.lnum;
14092 match_pos->col = pos.col + 1;
14093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 /* "/$" will put the cursor after the end of the line, may need to
14095 * correct that here */
14096 check_cursor();
14097 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014098
14099 /* If 'n' flag is used: restore cursor position. */
14100 if (flags & SP_NOMOVE)
14101 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014102 else
14103 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014104theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014106
14107 return retval;
14108}
14109
14110/*
14111 * "search()" function
14112 */
14113 static void
14114f_search(argvars, rettv)
14115 typval_T *argvars;
14116 typval_T *rettv;
14117{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014118 int flags = 0;
14119
14120 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121}
14122
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014124 * "searchdecl()" function
14125 */
14126 static void
14127f_searchdecl(argvars, rettv)
14128 typval_T *argvars;
14129 typval_T *rettv;
14130{
14131 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014132 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014133 int error = FALSE;
14134 char_u *name;
14135
14136 rettv->vval.v_number = 1; /* default: FAIL */
14137
14138 name = get_tv_string_chk(&argvars[0]);
14139 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014140 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014141 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014142 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14143 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14144 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014145 if (!error && name != NULL)
14146 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014147 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014148}
14149
14150/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014151 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014153 static int
14154searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014155 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014156 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157{
14158 char_u *spat, *mpat, *epat;
14159 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 int dir;
14162 int flags = 0;
14163 char_u nbuf1[NUMBUFLEN];
14164 char_u nbuf2[NUMBUFLEN];
14165 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014166 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014167 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014168 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 spat = get_tv_string_chk(&argvars[0]);
14172 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14173 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14174 if (spat == NULL || mpat == NULL || epat == NULL)
14175 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 /* Handle the optional fourth argument: flags */
14178 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014179 if (dir == 0)
14180 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014181
14182 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014183 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14184 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014185 if ((flags & (SP_END | SP_SUBPAT)) != 0
14186 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014187 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014188 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014189 goto theend;
14190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014192 /* Using 'r' implies 'W', otherwise it doesn't work. */
14193 if (flags & SP_REPEAT)
14194 p_ws = FALSE;
14195
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014196 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014197 if (argvars[3].v_type == VAR_UNKNOWN
14198 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 skip = (char_u *)"";
14200 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014202 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014203 if (argvars[5].v_type != VAR_UNKNOWN)
14204 {
14205 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14206 if (lnum_stop < 0)
14207 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014208#ifdef FEAT_RELTIME
14209 if (argvars[6].v_type != VAR_UNKNOWN)
14210 {
14211 time_limit = get_tv_number_chk(&argvars[6], NULL);
14212 if (time_limit < 0)
14213 goto theend;
14214 }
14215#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014216 }
14217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014218 if (skip == NULL)
14219 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014221 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014222 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014223
14224theend:
14225 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014226
14227 return retval;
14228}
14229
14230/*
14231 * "searchpair()" function
14232 */
14233 static void
14234f_searchpair(argvars, rettv)
14235 typval_T *argvars;
14236 typval_T *rettv;
14237{
14238 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14239}
14240
14241/*
14242 * "searchpairpos()" function
14243 */
14244 static void
14245f_searchpairpos(argvars, rettv)
14246 typval_T *argvars;
14247 typval_T *rettv;
14248{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014249 pos_T match_pos;
14250 int lnum = 0;
14251 int col = 0;
14252
14253 rettv->vval.v_number = 0;
14254
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014255 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014256 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014257
14258 if (searchpair_cmn(argvars, &match_pos) > 0)
14259 {
14260 lnum = match_pos.lnum;
14261 col = match_pos.col;
14262 }
14263
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014264 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14265 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014266}
14267
14268/*
14269 * Search for a start/middle/end thing.
14270 * Used by searchpair(), see its documentation for the details.
14271 * Returns 0 or -1 for no match,
14272 */
14273 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014274do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14275 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014276 char_u *spat; /* start pattern */
14277 char_u *mpat; /* middle pattern */
14278 char_u *epat; /* end pattern */
14279 int dir; /* BACKWARD or FORWARD */
14280 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014281 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014282 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014283 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014284 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014285{
14286 char_u *save_cpo;
14287 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14288 long retval = 0;
14289 pos_T pos;
14290 pos_T firstpos;
14291 pos_T foundpos;
14292 pos_T save_cursor;
14293 pos_T save_pos;
14294 int n;
14295 int r;
14296 int nest = 1;
14297 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014298 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014299 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014300
14301 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14302 save_cpo = p_cpo;
14303 p_cpo = (char_u *)"";
14304
Bram Moolenaar76929292008-01-06 19:07:36 +000014305#ifdef FEAT_RELTIME
14306 /* Set the time limit, if there is one. */
14307 profile_setlimit(time_limit, &tm);
14308#endif
14309
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014310 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14311 * start/middle/end (pat3, for the top pair). */
14312 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14313 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14314 if (pat2 == NULL || pat3 == NULL)
14315 goto theend;
14316 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14317 if (*mpat == NUL)
14318 STRCPY(pat3, pat2);
14319 else
14320 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14321 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014322 if (flags & SP_START)
14323 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014324
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 save_cursor = curwin->w_cursor;
14326 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014327 clearpos(&firstpos);
14328 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 pat = pat3;
14330 for (;;)
14331 {
14332 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014333 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14335 /* didn't find it or found the first match again: FAIL */
14336 break;
14337
14338 if (firstpos.lnum == 0)
14339 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014340 if (equalpos(pos, foundpos))
14341 {
14342 /* Found the same position again. Can happen with a pattern that
14343 * has "\zs" at the end and searching backwards. Advance one
14344 * character and try again. */
14345 if (dir == BACKWARD)
14346 decl(&pos);
14347 else
14348 incl(&pos);
14349 }
14350 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014352 /* clear the start flag to avoid getting stuck here */
14353 options &= ~SEARCH_START;
14354
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 /* If the skip pattern matches, ignore this match. */
14356 if (*skip != NUL)
14357 {
14358 save_pos = curwin->w_cursor;
14359 curwin->w_cursor = pos;
14360 r = eval_to_bool(skip, &err, NULL, FALSE);
14361 curwin->w_cursor = save_pos;
14362 if (err)
14363 {
14364 /* Evaluating {skip} caused an error, break here. */
14365 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014366 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 break;
14368 }
14369 if (r)
14370 continue;
14371 }
14372
14373 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14374 {
14375 /* Found end when searching backwards or start when searching
14376 * forward: nested pair. */
14377 ++nest;
14378 pat = pat2; /* nested, don't search for middle */
14379 }
14380 else
14381 {
14382 /* Found end when searching forward or start when searching
14383 * backward: end of (nested) pair; or found middle in outer pair. */
14384 if (--nest == 1)
14385 pat = pat3; /* outer level, search for middle */
14386 }
14387
14388 if (nest == 0)
14389 {
14390 /* Found the match: return matchcount or line number. */
14391 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014392 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014394 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014395 if (flags & SP_SETPCMARK)
14396 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 curwin->w_cursor = pos;
14398 if (!(flags & SP_REPEAT))
14399 break;
14400 nest = 1; /* search for next unmatched */
14401 }
14402 }
14403
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014404 if (match_pos != NULL)
14405 {
14406 /* Store the match cursor position */
14407 match_pos->lnum = curwin->w_cursor.lnum;
14408 match_pos->col = curwin->w_cursor.col + 1;
14409 }
14410
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014412 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413 curwin->w_cursor = save_cursor;
14414
14415theend:
14416 vim_free(pat2);
14417 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014419
14420 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421}
14422
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014423/*
14424 * "searchpos()" function
14425 */
14426 static void
14427f_searchpos(argvars, rettv)
14428 typval_T *argvars;
14429 typval_T *rettv;
14430{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014431 pos_T match_pos;
14432 int lnum = 0;
14433 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014434 int n;
14435 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014436
14437 rettv->vval.v_number = 0;
14438
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014439 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014440 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014441
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014442 n = search_cmn(argvars, &match_pos, &flags);
14443 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014444 {
14445 lnum = match_pos.lnum;
14446 col = match_pos.col;
14447 }
14448
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014449 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14450 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014451 if (flags & SP_SUBPAT)
14452 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014453}
14454
14455
Bram Moolenaar0d660222005-01-07 21:51:51 +000014456/*ARGSUSED*/
14457 static void
14458f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014459 typval_T *argvars;
14460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462#ifdef FEAT_CLIENTSERVER
14463 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014464 char_u *server = get_tv_string_chk(&argvars[0]);
14465 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466
Bram Moolenaar0d660222005-01-07 21:51:51 +000014467 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 if (server == NULL || reply == NULL)
14469 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014470 if (check_restricted() || check_secure())
14471 return;
14472# ifdef FEAT_X11
14473 if (check_connection() == FAIL)
14474 return;
14475# endif
14476
14477 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014479 EMSG(_("E258: Unable to send to client"));
14480 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014482 rettv->vval.v_number = 0;
14483#else
14484 rettv->vval.v_number = -1;
14485#endif
14486}
14487
14488/*ARGSUSED*/
14489 static void
14490f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014491 typval_T *argvars;
14492 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493{
14494 char_u *r = NULL;
14495
14496#ifdef FEAT_CLIENTSERVER
14497# ifdef WIN32
14498 r = serverGetVimNames();
14499# else
14500 make_connection();
14501 if (X_DISPLAY != NULL)
14502 r = serverGetVimNames(X_DISPLAY);
14503# endif
14504#endif
14505 rettv->v_type = VAR_STRING;
14506 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507}
14508
14509/*
14510 * "setbufvar()" function
14511 */
14512/*ARGSUSED*/
14513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014515 typval_T *argvars;
14516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517{
14518 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014521 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 char_u nbuf[NUMBUFLEN];
14523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 rettv->vval.v_number = 0;
14525
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 if (check_restricted() || check_secure())
14527 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014528 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14529 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 varp = &argvars[2];
14532
14533 if (buf != NULL && varname != NULL && varp != NULL)
14534 {
14535 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537
14538 if (*varname == '&')
14539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014540 long numval;
14541 char_u *strval;
14542 int error = FALSE;
14543
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014545 numval = get_tv_number_chk(varp, &error);
14546 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014547 if (!error && strval != NULL)
14548 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549 }
14550 else
14551 {
14552 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14553 if (bufvarname != NULL)
14554 {
14555 STRCPY(bufvarname, "b:");
14556 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014557 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 vim_free(bufvarname);
14559 }
14560 }
14561
14562 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565}
14566
14567/*
14568 * "setcmdpos()" function
14569 */
14570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014571f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014572 typval_T *argvars;
14573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014575 int pos = (int)get_tv_number(&argvars[0]) - 1;
14576
14577 if (pos >= 0)
14578 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579}
14580
14581/*
14582 * "setline()" function
14583 */
14584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014586 typval_T *argvars;
14587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588{
14589 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014590 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014591 list_T *l = NULL;
14592 listitem_T *li = NULL;
14593 long added = 0;
14594 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014596 lnum = get_tv_lnum(&argvars[0]);
14597 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014599 l = argvars[1].vval.v_list;
14600 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014602 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014604
14605 rettv->vval.v_number = 0; /* OK */
14606 for (;;)
14607 {
14608 if (l != NULL)
14609 {
14610 /* list argument, get next string */
14611 if (li == NULL)
14612 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014614 li = li->li_next;
14615 }
14616
14617 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014618 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014619 break;
14620 if (lnum <= curbuf->b_ml.ml_line_count)
14621 {
14622 /* existing line, replace it */
14623 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14624 {
14625 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014626 if (lnum == curwin->w_cursor.lnum)
14627 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014628 rettv->vval.v_number = 0; /* OK */
14629 }
14630 }
14631 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14632 {
14633 /* lnum is one past the last line, append the line */
14634 ++added;
14635 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14636 rettv->vval.v_number = 0; /* OK */
14637 }
14638
14639 if (l == NULL) /* only one string argument */
14640 break;
14641 ++lnum;
14642 }
14643
14644 if (added > 0)
14645 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646}
14647
14648/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014649 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014650 */
14651/*ARGSUSED*/
14652 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014653set_qf_ll_list(wp, list_arg, action_arg, rettv)
14654 win_T *wp;
14655 typval_T *list_arg;
14656 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014657 typval_T *rettv;
14658{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014659#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014660 char_u *act;
14661 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014662#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014663
Bram Moolenaar2641f772005-03-25 21:58:17 +000014664 rettv->vval.v_number = -1;
14665
14666#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014667 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014668 EMSG(_(e_listreq));
14669 else
14670 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014671 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014672
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014673 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014674 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014675 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014676 if (act == NULL)
14677 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014678 if (*act == 'a' || *act == 'r')
14679 action = *act;
14680 }
14681
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014682 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014683 rettv->vval.v_number = 0;
14684 }
14685#endif
14686}
14687
14688/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014689 * "setloclist()" function
14690 */
14691/*ARGSUSED*/
14692 static void
14693f_setloclist(argvars, rettv)
14694 typval_T *argvars;
14695 typval_T *rettv;
14696{
14697 win_T *win;
14698
14699 rettv->vval.v_number = -1;
14700
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014701 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014702 if (win != NULL)
14703 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14704}
14705
14706/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014707 * "setmatches()" function
14708 */
14709 static void
14710f_setmatches(argvars, rettv)
14711 typval_T *argvars;
14712 typval_T *rettv;
14713{
14714#ifdef FEAT_SEARCH_EXTRA
14715 list_T *l;
14716 listitem_T *li;
14717 dict_T *d;
14718
14719 rettv->vval.v_number = -1;
14720 if (argvars[0].v_type != VAR_LIST)
14721 {
14722 EMSG(_(e_listreq));
14723 return;
14724 }
14725 if ((l = argvars[0].vval.v_list) != NULL)
14726 {
14727
14728 /* To some extent make sure that we are dealing with a list from
14729 * "getmatches()". */
14730 li = l->lv_first;
14731 while (li != NULL)
14732 {
14733 if (li->li_tv.v_type != VAR_DICT
14734 || (d = li->li_tv.vval.v_dict) == NULL)
14735 {
14736 EMSG(_(e_invarg));
14737 return;
14738 }
14739 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14740 && dict_find(d, (char_u *)"pattern", -1) != NULL
14741 && dict_find(d, (char_u *)"priority", -1) != NULL
14742 && dict_find(d, (char_u *)"id", -1) != NULL))
14743 {
14744 EMSG(_(e_invarg));
14745 return;
14746 }
14747 li = li->li_next;
14748 }
14749
14750 clear_matches(curwin);
14751 li = l->lv_first;
14752 while (li != NULL)
14753 {
14754 d = li->li_tv.vval.v_dict;
14755 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14756 get_dict_string(d, (char_u *)"pattern", FALSE),
14757 (int)get_dict_number(d, (char_u *)"priority"),
14758 (int)get_dict_number(d, (char_u *)"id"));
14759 li = li->li_next;
14760 }
14761 rettv->vval.v_number = 0;
14762 }
14763#endif
14764}
14765
14766/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014767 * "setpos()" function
14768 */
14769/*ARGSUSED*/
14770 static void
14771f_setpos(argvars, rettv)
14772 typval_T *argvars;
14773 typval_T *rettv;
14774{
14775 pos_T pos;
14776 int fnum;
14777 char_u *name;
14778
14779 name = get_tv_string_chk(argvars);
14780 if (name != NULL)
14781 {
14782 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14783 {
14784 --pos.col;
14785 if (name[0] == '.') /* cursor */
14786 {
14787 if (fnum == curbuf->b_fnum)
14788 {
14789 curwin->w_cursor = pos;
14790 check_cursor();
14791 }
14792 else
14793 EMSG(_(e_invarg));
14794 }
14795 else if (name[0] == '\'') /* mark */
14796 (void)setmark_pos(name[1], &pos, fnum);
14797 else
14798 EMSG(_(e_invarg));
14799 }
14800 }
14801}
14802
14803/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014804 * "setqflist()" function
14805 */
14806/*ARGSUSED*/
14807 static void
14808f_setqflist(argvars, rettv)
14809 typval_T *argvars;
14810 typval_T *rettv;
14811{
14812 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14813}
14814
14815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 * "setreg()" function
14817 */
14818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014819f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014820 typval_T *argvars;
14821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822{
14823 int regname;
14824 char_u *strregname;
14825 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014826 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 int append;
14828 char_u yank_type;
14829 long block_len;
14830
14831 block_len = -1;
14832 yank_type = MAUTO;
14833 append = FALSE;
14834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014835 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014836 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014838 if (strregname == NULL)
14839 return; /* type error; errmsg already given */
14840 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 if (regname == 0 || regname == '@')
14842 regname = '"';
14843 else if (regname == '=')
14844 return;
14845
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014846 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014848 stropt = get_tv_string_chk(&argvars[2]);
14849 if (stropt == NULL)
14850 return; /* type error */
14851 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 switch (*stropt)
14853 {
14854 case 'a': case 'A': /* append */
14855 append = TRUE;
14856 break;
14857 case 'v': case 'c': /* character-wise selection */
14858 yank_type = MCHAR;
14859 break;
14860 case 'V': case 'l': /* line-wise selection */
14861 yank_type = MLINE;
14862 break;
14863#ifdef FEAT_VISUAL
14864 case 'b': case Ctrl_V: /* block-wise selection */
14865 yank_type = MBLOCK;
14866 if (VIM_ISDIGIT(stropt[1]))
14867 {
14868 ++stropt;
14869 block_len = getdigits(&stropt) - 1;
14870 --stropt;
14871 }
14872 break;
14873#endif
14874 }
14875 }
14876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014877 strval = get_tv_string_chk(&argvars[1]);
14878 if (strval != NULL)
14879 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014881 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882}
14883
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014884/*
14885 * "settabwinvar()" function
14886 */
14887 static void
14888f_settabwinvar(argvars, rettv)
14889 typval_T *argvars;
14890 typval_T *rettv;
14891{
14892 setwinvar(argvars, rettv, 1);
14893}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894
14895/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014896 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014899f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014900 typval_T *argvars;
14901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014903 setwinvar(argvars, rettv, 0);
14904}
14905
14906/*
14907 * "setwinvar()" and "settabwinvar()" functions
14908 */
14909 static void
14910setwinvar(argvars, rettv, off)
14911 typval_T *argvars;
14912 typval_T *rettv;
14913 int off;
14914{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 win_T *win;
14916#ifdef FEAT_WINDOWS
14917 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014918 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919#endif
14920 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014921 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014923 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014925 rettv->vval.v_number = 0;
14926
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 if (check_restricted() || check_secure())
14928 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014929
14930#ifdef FEAT_WINDOWS
14931 if (off == 1)
14932 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14933 else
14934 tp = curtab;
14935#endif
14936 win = find_win_by_nr(&argvars[off], tp);
14937 varname = get_tv_string_chk(&argvars[off + 1]);
14938 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939
14940 if (win != NULL && varname != NULL && varp != NULL)
14941 {
14942#ifdef FEAT_WINDOWS
14943 /* set curwin to be our win, temporarily */
14944 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014945 save_curtab = curtab;
14946 goto_tabpage_tp(tp);
14947 if (!win_valid(win))
14948 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 curwin = win;
14950 curbuf = curwin->w_buffer;
14951#endif
14952
14953 if (*varname == '&')
14954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014955 long numval;
14956 char_u *strval;
14957 int error = FALSE;
14958
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014960 numval = get_tv_number_chk(varp, &error);
14961 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014962 if (!error && strval != NULL)
14963 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 }
14965 else
14966 {
14967 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14968 if (winvarname != NULL)
14969 {
14970 STRCPY(winvarname, "w:");
14971 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014972 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973 vim_free(winvarname);
14974 }
14975 }
14976
14977#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014978 /* Restore current tabpage and window, if still valid (autocomands can
14979 * make them invalid). */
14980 if (valid_tabpage(save_curtab))
14981 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982 if (win_valid(save_curwin))
14983 {
14984 curwin = save_curwin;
14985 curbuf = curwin->w_buffer;
14986 }
14987#endif
14988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989}
14990
14991/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014992 * "shellescape({string})" function
14993 */
14994 static void
14995f_shellescape(argvars, rettv)
14996 typval_T *argvars;
14997 typval_T *rettv;
14998{
14999 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
15000 rettv->v_type = VAR_STRING;
15001}
15002
15003/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015004 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 */
15006 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015008 typval_T *argvars;
15009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012
Bram Moolenaar0d660222005-01-07 21:51:51 +000015013 p = get_tv_string(&argvars[0]);
15014 rettv->vval.v_string = vim_strsave(p);
15015 simplify_filename(rettv->vval.v_string); /* simplify in place */
15016 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017}
15018
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019static int
15020#ifdef __BORLANDC__
15021 _RTLENTRYF
15022#endif
15023 item_compare __ARGS((const void *s1, const void *s2));
15024static int
15025#ifdef __BORLANDC__
15026 _RTLENTRYF
15027#endif
15028 item_compare2 __ARGS((const void *s1, const void *s2));
15029
15030static int item_compare_ic;
15031static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015032static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015033#define ITEM_COMPARE_FAIL 999
15034
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015038 static int
15039#ifdef __BORLANDC__
15040_RTLENTRYF
15041#endif
15042item_compare(s1, s2)
15043 const void *s1;
15044 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046 char_u *p1, *p2;
15047 char_u *tofree1, *tofree2;
15048 int res;
15049 char_u numbuf1[NUMBUFLEN];
15050 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015052 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15053 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015054 if (p1 == NULL)
15055 p1 = (char_u *)"";
15056 if (p2 == NULL)
15057 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015058 if (item_compare_ic)
15059 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061 res = STRCMP(p1, p2);
15062 vim_free(tofree1);
15063 vim_free(tofree2);
15064 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065}
15066
15067 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068#ifdef __BORLANDC__
15069_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071item_compare2(s1, s2)
15072 const void *s1;
15073 const void *s2;
15074{
15075 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015076 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015077 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015080 /* shortcut after failure in previous call; compare all items equal */
15081 if (item_compare_func_err)
15082 return 0;
15083
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015084 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15085 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015086 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15087 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088
15089 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015090 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015091 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092 clear_tv(&argv[0]);
15093 clear_tv(&argv[1]);
15094
15095 if (res == FAIL)
15096 res = ITEM_COMPARE_FAIL;
15097 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015098 /* return value has wrong type */
15099 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15100 if (item_compare_func_err)
15101 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 clear_tv(&rettv);
15103 return res;
15104}
15105
15106/*
15107 * "sort({list})" function
15108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015110f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015111 typval_T *argvars;
15112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113{
Bram Moolenaar33570922005-01-25 22:26:29 +000015114 list_T *l;
15115 listitem_T *li;
15116 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117 long len;
15118 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119
Bram Moolenaar0d660222005-01-07 21:51:51 +000015120 rettv->vval.v_number = 0;
15121 if (argvars[0].v_type != VAR_LIST)
15122 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 else
15124 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015125 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015126 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 return;
15128 rettv->vval.v_list = l;
15129 rettv->v_type = VAR_LIST;
15130 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132 len = list_len(l);
15133 if (len <= 1)
15134 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135
Bram Moolenaar0d660222005-01-07 21:51:51 +000015136 item_compare_ic = FALSE;
15137 item_compare_func = NULL;
15138 if (argvars[1].v_type != VAR_UNKNOWN)
15139 {
15140 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015141 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142 else
15143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015144 int error = FALSE;
15145
15146 i = get_tv_number_chk(&argvars[1], &error);
15147 if (error)
15148 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149 if (i == 1)
15150 item_compare_ic = TRUE;
15151 else
15152 item_compare_func = get_tv_string(&argvars[1]);
15153 }
15154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015157 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 if (ptrs == NULL)
15159 return;
15160 i = 0;
15161 for (li = l->lv_first; li != NULL; li = li->li_next)
15162 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015164 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165 /* test the compare function */
15166 if (item_compare_func != NULL
15167 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15168 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015169 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 {
15172 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015173 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174 item_compare_func == NULL ? item_compare : item_compare2);
15175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015176 if (!item_compare_func_err)
15177 {
15178 /* Clear the List and append the items in the sorted order. */
15179 l->lv_first = l->lv_last = NULL;
15180 l->lv_len = 0;
15181 for (i = 0; i < len; ++i)
15182 list_append(l, ptrs[i]);
15183 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015184 }
15185
15186 vim_free(ptrs);
15187 }
15188}
15189
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015190/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015191 * "soundfold({word})" function
15192 */
15193 static void
15194f_soundfold(argvars, rettv)
15195 typval_T *argvars;
15196 typval_T *rettv;
15197{
15198 char_u *s;
15199
15200 rettv->v_type = VAR_STRING;
15201 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015202#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015203 rettv->vval.v_string = eval_soundfold(s);
15204#else
15205 rettv->vval.v_string = vim_strsave(s);
15206#endif
15207}
15208
15209/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015210 * "spellbadword()" function
15211 */
15212/* ARGSUSED */
15213 static void
15214f_spellbadword(argvars, rettv)
15215 typval_T *argvars;
15216 typval_T *rettv;
15217{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015218 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015219 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015220 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015221
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015222 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015223 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015224
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015225#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015226 if (argvars[0].v_type == VAR_UNKNOWN)
15227 {
15228 /* Find the start and length of the badly spelled word. */
15229 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15230 if (len != 0)
15231 word = ml_get_cursor();
15232 }
15233 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15234 {
15235 char_u *str = get_tv_string_chk(&argvars[0]);
15236 int capcol = -1;
15237
15238 if (str != NULL)
15239 {
15240 /* Check the argument for spelling. */
15241 while (*str != NUL)
15242 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015243 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015244 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015245 {
15246 word = str;
15247 break;
15248 }
15249 str += len;
15250 }
15251 }
15252 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015253#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015254
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015255 list_append_string(rettv->vval.v_list, word, len);
15256 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015257 attr == HLF_SPB ? "bad" :
15258 attr == HLF_SPR ? "rare" :
15259 attr == HLF_SPL ? "local" :
15260 attr == HLF_SPC ? "caps" :
15261 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015262}
15263
15264/*
15265 * "spellsuggest()" function
15266 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015267/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015268 static void
15269f_spellsuggest(argvars, rettv)
15270 typval_T *argvars;
15271 typval_T *rettv;
15272{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015273#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015274 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015275 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015276 int maxcount;
15277 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015278 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015279 listitem_T *li;
15280 int need_capital = FALSE;
15281#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015282
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015283 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015284 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015285
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015286#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015287 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15288 {
15289 str = get_tv_string(&argvars[0]);
15290 if (argvars[1].v_type != VAR_UNKNOWN)
15291 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015292 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015293 if (maxcount <= 0)
15294 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015295 if (argvars[2].v_type != VAR_UNKNOWN)
15296 {
15297 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15298 if (typeerr)
15299 return;
15300 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015301 }
15302 else
15303 maxcount = 25;
15304
Bram Moolenaar4770d092006-01-12 23:22:24 +000015305 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015306
15307 for (i = 0; i < ga.ga_len; ++i)
15308 {
15309 str = ((char_u **)ga.ga_data)[i];
15310
15311 li = listitem_alloc();
15312 if (li == NULL)
15313 vim_free(str);
15314 else
15315 {
15316 li->li_tv.v_type = VAR_STRING;
15317 li->li_tv.v_lock = 0;
15318 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015319 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015320 }
15321 }
15322 ga_clear(&ga);
15323 }
15324#endif
15325}
15326
Bram Moolenaar0d660222005-01-07 21:51:51 +000015327 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015328f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015329 typval_T *argvars;
15330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015331{
15332 char_u *str;
15333 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015334 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015335 regmatch_T regmatch;
15336 char_u patbuf[NUMBUFLEN];
15337 char_u *save_cpo;
15338 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015340 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015341 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015342
15343 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15344 save_cpo = p_cpo;
15345 p_cpo = (char_u *)"";
15346
15347 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015348 if (argvars[1].v_type != VAR_UNKNOWN)
15349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015350 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15351 if (pat == NULL)
15352 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015353 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015354 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015355 }
15356 if (pat == NULL || *pat == NUL)
15357 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015358
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015359 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015361 if (typeerr)
15362 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363
Bram Moolenaar0d660222005-01-07 21:51:51 +000015364 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15365 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015367 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015368 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015369 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015370 if (*str == NUL)
15371 match = FALSE; /* empty item at the end */
15372 else
15373 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015374 if (match)
15375 end = regmatch.startp[0];
15376 else
15377 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015378 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15379 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015380 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015381 if (list_append_string(rettv->vval.v_list, str,
15382 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015383 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015384 }
15385 if (!match)
15386 break;
15387 /* Advance to just after the match. */
15388 if (regmatch.endp[0] > str)
15389 col = 0;
15390 else
15391 {
15392 /* Don't get stuck at the same match. */
15393#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015394 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015395#else
15396 col = 1;
15397#endif
15398 }
15399 str = regmatch.endp[0];
15400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401
Bram Moolenaar0d660222005-01-07 21:51:51 +000015402 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404
Bram Moolenaar0d660222005-01-07 21:51:51 +000015405 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406}
15407
Bram Moolenaar2c932302006-03-18 21:42:09 +000015408/*
15409 * "str2nr()" function
15410 */
15411 static void
15412f_str2nr(argvars, rettv)
15413 typval_T *argvars;
15414 typval_T *rettv;
15415{
15416 int base = 10;
15417 char_u *p;
15418 long n;
15419
15420 if (argvars[1].v_type != VAR_UNKNOWN)
15421 {
15422 base = get_tv_number(&argvars[1]);
15423 if (base != 8 && base != 10 && base != 16)
15424 {
15425 EMSG(_(e_invarg));
15426 return;
15427 }
15428 }
15429
15430 p = skipwhite(get_tv_string(&argvars[0]));
15431 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15432 rettv->vval.v_number = n;
15433}
15434
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435#ifdef HAVE_STRFTIME
15436/*
15437 * "strftime({format}[, {time}])" function
15438 */
15439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015441 typval_T *argvars;
15442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443{
15444 char_u result_buf[256];
15445 struct tm *curtime;
15446 time_t seconds;
15447 char_u *p;
15448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015451 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015452 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 seconds = time(NULL);
15454 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015455 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 curtime = localtime(&seconds);
15457 /* MSVC returns NULL for an invalid value of seconds. */
15458 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015459 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 else
15461 {
15462# ifdef FEAT_MBYTE
15463 vimconv_T conv;
15464 char_u *enc;
15465
15466 conv.vc_type = CONV_NONE;
15467 enc = enc_locale();
15468 convert_setup(&conv, p_enc, enc);
15469 if (conv.vc_type != CONV_NONE)
15470 p = string_convert(&conv, p, NULL);
15471# endif
15472 if (p != NULL)
15473 (void)strftime((char *)result_buf, sizeof(result_buf),
15474 (char *)p, curtime);
15475 else
15476 result_buf[0] = NUL;
15477
15478# ifdef FEAT_MBYTE
15479 if (conv.vc_type != CONV_NONE)
15480 vim_free(p);
15481 convert_setup(&conv, enc, p_enc);
15482 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015483 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 else
15485# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487
15488# ifdef FEAT_MBYTE
15489 /* Release conversion descriptors */
15490 convert_setup(&conv, NULL, NULL);
15491 vim_free(enc);
15492# endif
15493 }
15494}
15495#endif
15496
15497/*
15498 * "stridx()" function
15499 */
15500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015502 typval_T *argvars;
15503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504{
15505 char_u buf[NUMBUFLEN];
15506 char_u *needle;
15507 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015508 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015510 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015512 needle = get_tv_string_chk(&argvars[1]);
15513 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015514 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015515 if (needle == NULL || haystack == NULL)
15516 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517
Bram Moolenaar33570922005-01-25 22:26:29 +000015518 if (argvars[2].v_type != VAR_UNKNOWN)
15519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015520 int error = FALSE;
15521
15522 start_idx = get_tv_number_chk(&argvars[2], &error);
15523 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015524 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015525 if (start_idx >= 0)
15526 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015527 }
15528
15529 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15530 if (pos != NULL)
15531 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532}
15533
15534/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015535 * "string()" function
15536 */
15537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015539 typval_T *argvars;
15540 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015541{
15542 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015543 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015546 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015547 /* Make a copy if we have a value but it's not in allocate memory. */
15548 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550}
15551
15552/*
15553 * "strlen()" function
15554 */
15555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015557 typval_T *argvars;
15558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->vval.v_number = (varnumber_T)(STRLEN(
15561 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562}
15563
15564/*
15565 * "strpart()" function
15566 */
15567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015568f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015569 typval_T *argvars;
15570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571{
15572 char_u *p;
15573 int n;
15574 int len;
15575 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015576 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015578 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 slen = (int)STRLEN(p);
15580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015581 n = get_tv_number_chk(&argvars[1], &error);
15582 if (error)
15583 len = 0;
15584 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015585 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 else
15587 len = slen - n; /* default len: all bytes that are available. */
15588
15589 /*
15590 * Only return the overlap between the specified part and the actual
15591 * string.
15592 */
15593 if (n < 0)
15594 {
15595 len += n;
15596 n = 0;
15597 }
15598 else if (n > slen)
15599 n = slen;
15600 if (len < 0)
15601 len = 0;
15602 else if (n + len > slen)
15603 len = slen - n;
15604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015605 rettv->v_type = VAR_STRING;
15606 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
15609/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015610 * "strridx()" function
15611 */
15612 static void
15613f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015614 typval_T *argvars;
15615 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015616{
15617 char_u buf[NUMBUFLEN];
15618 char_u *needle;
15619 char_u *haystack;
15620 char_u *rest;
15621 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015622 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015624 needle = get_tv_string_chk(&argvars[1]);
15625 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015626
15627 rettv->vval.v_number = -1;
15628 if (needle == NULL || haystack == NULL)
15629 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015630
15631 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015632 if (argvars[2].v_type != VAR_UNKNOWN)
15633 {
15634 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015635 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015636 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015638 }
15639 else
15640 end_idx = haystack_len;
15641
Bram Moolenaar0d660222005-01-07 21:51:51 +000015642 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015643 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015644 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015645 lastmatch = haystack + end_idx;
15646 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015647 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015648 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015649 for (rest = haystack; *rest != '\0'; ++rest)
15650 {
15651 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015652 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015653 break;
15654 lastmatch = rest;
15655 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015656 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657
15658 if (lastmatch == NULL)
15659 rettv->vval.v_number = -1;
15660 else
15661 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15662}
15663
15664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 * "strtrans()" function
15666 */
15667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015668f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015669 typval_T *argvars;
15670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015672 rettv->v_type = VAR_STRING;
15673 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674}
15675
15676/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677 * "submatch()" function
15678 */
15679 static void
15680f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015681 typval_T *argvars;
15682 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015683{
15684 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015685 rettv->vval.v_string =
15686 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015687}
15688
15689/*
15690 * "substitute()" function
15691 */
15692 static void
15693f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015694 typval_T *argvars;
15695 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015696{
15697 char_u patbuf[NUMBUFLEN];
15698 char_u subbuf[NUMBUFLEN];
15699 char_u flagsbuf[NUMBUFLEN];
15700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015701 char_u *str = get_tv_string_chk(&argvars[0]);
15702 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15703 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15704 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15705
Bram Moolenaar0d660222005-01-07 21:51:51 +000015706 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015707 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15708 rettv->vval.v_string = NULL;
15709 else
15710 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015711}
15712
15713/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015714 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 */
15716/*ARGSUSED*/
15717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015718f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015719 typval_T *argvars;
15720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721{
15722 int id = 0;
15723#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015724 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 long col;
15726 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015727 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015729 lnum = get_tv_lnum(argvars); /* -1 on type error */
15730 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15731 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015733 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015734 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015735 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736#endif
15737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739}
15740
15741/*
15742 * "synIDattr(id, what [, mode])" function
15743 */
15744/*ARGSUSED*/
15745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015746f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015747 typval_T *argvars;
15748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749{
15750 char_u *p = NULL;
15751#ifdef FEAT_SYN_HL
15752 int id;
15753 char_u *what;
15754 char_u *mode;
15755 char_u modebuf[NUMBUFLEN];
15756 int modec;
15757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758 id = get_tv_number(&argvars[0]);
15759 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015760 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 modec = TOLOWER_ASC(mode[0]);
15764 if (modec != 't' && modec != 'c'
15765#ifdef FEAT_GUI
15766 && modec != 'g'
15767#endif
15768 )
15769 modec = 0; /* replace invalid with current */
15770 }
15771 else
15772 {
15773#ifdef FEAT_GUI
15774 if (gui.in_use)
15775 modec = 'g';
15776 else
15777#endif
15778 if (t_colors > 1)
15779 modec = 'c';
15780 else
15781 modec = 't';
15782 }
15783
15784
15785 switch (TOLOWER_ASC(what[0]))
15786 {
15787 case 'b':
15788 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15789 p = highlight_color(id, what, modec);
15790 else /* bold */
15791 p = highlight_has_attr(id, HL_BOLD, modec);
15792 break;
15793
15794 case 'f': /* fg[#] */
15795 p = highlight_color(id, what, modec);
15796 break;
15797
15798 case 'i':
15799 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15800 p = highlight_has_attr(id, HL_INVERSE, modec);
15801 else /* italic */
15802 p = highlight_has_attr(id, HL_ITALIC, modec);
15803 break;
15804
15805 case 'n': /* name */
15806 p = get_highlight_name(NULL, id - 1);
15807 break;
15808
15809 case 'r': /* reverse */
15810 p = highlight_has_attr(id, HL_INVERSE, modec);
15811 break;
15812
15813 case 's': /* standout */
15814 p = highlight_has_attr(id, HL_STANDOUT, modec);
15815 break;
15816
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015817 case 'u':
15818 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15819 /* underline */
15820 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15821 else
15822 /* undercurl */
15823 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 break;
15825 }
15826
15827 if (p != NULL)
15828 p = vim_strsave(p);
15829#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015830 rettv->v_type = VAR_STRING;
15831 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832}
15833
15834/*
15835 * "synIDtrans(id)" function
15836 */
15837/*ARGSUSED*/
15838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015839f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015840 typval_T *argvars;
15841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842{
15843 int id;
15844
15845#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015846 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847
15848 if (id > 0)
15849 id = syn_get_final_id(id);
15850 else
15851#endif
15852 id = 0;
15853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015854 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855}
15856
15857/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015858 * "synstack(lnum, col)" function
15859 */
15860/*ARGSUSED*/
15861 static void
15862f_synstack(argvars, rettv)
15863 typval_T *argvars;
15864 typval_T *rettv;
15865{
15866#ifdef FEAT_SYN_HL
15867 long lnum;
15868 long col;
15869 int i;
15870 int id;
15871#endif
15872
15873 rettv->v_type = VAR_LIST;
15874 rettv->vval.v_list = NULL;
15875
15876#ifdef FEAT_SYN_HL
15877 lnum = get_tv_lnum(argvars); /* -1 on type error */
15878 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15879
15880 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
15881 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
15882 && rettv_list_alloc(rettv) != FAIL)
15883 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015884 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015885 for (i = 0; ; ++i)
15886 {
15887 id = syn_get_stack_item(i);
15888 if (id < 0)
15889 break;
15890 if (list_append_number(rettv->vval.v_list, id) == FAIL)
15891 break;
15892 }
15893 }
15894#endif
15895}
15896
15897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898 * "system()" function
15899 */
15900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015901f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015902 typval_T *argvars;
15903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015905 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015907 char_u *infile = NULL;
15908 char_u buf[NUMBUFLEN];
15909 int err = FALSE;
15910 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015912 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015913 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015914
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015915 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015916 {
15917 /*
15918 * Write the string to a temp file, to be used for input of the shell
15919 * command.
15920 */
15921 if ((infile = vim_tempname('i')) == NULL)
15922 {
15923 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015924 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015925 }
15926
15927 fd = mch_fopen((char *)infile, WRITEBIN);
15928 if (fd == NULL)
15929 {
15930 EMSG2(_(e_notopen), infile);
15931 goto done;
15932 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015933 p = get_tv_string_buf_chk(&argvars[1], buf);
15934 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015935 {
15936 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015937 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015938 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015939 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15940 err = TRUE;
15941 if (fclose(fd) != 0)
15942 err = TRUE;
15943 if (err)
15944 {
15945 EMSG(_("E677: Error writing temp file"));
15946 goto done;
15947 }
15948 }
15949
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015950 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15951 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015952
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953#ifdef USE_CR
15954 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015955 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956 {
15957 char_u *s;
15958
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015959 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960 {
15961 if (*s == CAR)
15962 *s = NL;
15963 }
15964 }
15965#else
15966# ifdef USE_CRNL
15967 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015968 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969 {
15970 char_u *s, *d;
15971
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015972 d = res;
15973 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 {
15975 if (s[0] == CAR && s[1] == NL)
15976 ++s;
15977 *d++ = *s;
15978 }
15979 *d = NUL;
15980 }
15981# endif
15982#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015983
15984done:
15985 if (infile != NULL)
15986 {
15987 mch_remove(infile);
15988 vim_free(infile);
15989 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015990 rettv->v_type = VAR_STRING;
15991 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992}
15993
15994/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015995 * "tabpagebuflist()" function
15996 */
15997/* ARGSUSED */
15998 static void
15999f_tabpagebuflist(argvars, rettv)
16000 typval_T *argvars;
16001 typval_T *rettv;
16002{
16003#ifndef FEAT_WINDOWS
16004 rettv->vval.v_number = 0;
16005#else
16006 tabpage_T *tp;
16007 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016008
16009 if (argvars[0].v_type == VAR_UNKNOWN)
16010 wp = firstwin;
16011 else
16012 {
16013 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16014 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016015 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016016 }
16017 if (wp == NULL)
16018 rettv->vval.v_number = 0;
16019 else
16020 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016021 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016022 rettv->vval.v_number = 0;
16023 else
16024 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016025 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016026 if (list_append_number(rettv->vval.v_list,
16027 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016028 break;
16029 }
16030 }
16031#endif
16032}
16033
16034
16035/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016036 * "tabpagenr()" function
16037 */
16038/* ARGSUSED */
16039 static void
16040f_tabpagenr(argvars, rettv)
16041 typval_T *argvars;
16042 typval_T *rettv;
16043{
16044 int nr = 1;
16045#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016046 char_u *arg;
16047
16048 if (argvars[0].v_type != VAR_UNKNOWN)
16049 {
16050 arg = get_tv_string_chk(&argvars[0]);
16051 nr = 0;
16052 if (arg != NULL)
16053 {
16054 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016055 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016056 else
16057 EMSG2(_(e_invexpr2), arg);
16058 }
16059 }
16060 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016061 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016062#endif
16063 rettv->vval.v_number = nr;
16064}
16065
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016066
16067#ifdef FEAT_WINDOWS
16068static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16069
16070/*
16071 * Common code for tabpagewinnr() and winnr().
16072 */
16073 static int
16074get_winnr(tp, argvar)
16075 tabpage_T *tp;
16076 typval_T *argvar;
16077{
16078 win_T *twin;
16079 int nr = 1;
16080 win_T *wp;
16081 char_u *arg;
16082
16083 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16084 if (argvar->v_type != VAR_UNKNOWN)
16085 {
16086 arg = get_tv_string_chk(argvar);
16087 if (arg == NULL)
16088 nr = 0; /* type error; errmsg already given */
16089 else if (STRCMP(arg, "$") == 0)
16090 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16091 else if (STRCMP(arg, "#") == 0)
16092 {
16093 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16094 if (twin == NULL)
16095 nr = 0;
16096 }
16097 else
16098 {
16099 EMSG2(_(e_invexpr2), arg);
16100 nr = 0;
16101 }
16102 }
16103
16104 if (nr > 0)
16105 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16106 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016107 {
16108 if (wp == NULL)
16109 {
16110 /* didn't find it in this tabpage */
16111 nr = 0;
16112 break;
16113 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016114 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016115 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016116 return nr;
16117}
16118#endif
16119
16120/*
16121 * "tabpagewinnr()" function
16122 */
16123/* ARGSUSED */
16124 static void
16125f_tabpagewinnr(argvars, rettv)
16126 typval_T *argvars;
16127 typval_T *rettv;
16128{
16129 int nr = 1;
16130#ifdef FEAT_WINDOWS
16131 tabpage_T *tp;
16132
16133 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16134 if (tp == NULL)
16135 nr = 0;
16136 else
16137 nr = get_winnr(tp, &argvars[1]);
16138#endif
16139 rettv->vval.v_number = nr;
16140}
16141
16142
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016143/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016144 * "tagfiles()" function
16145 */
16146/*ARGSUSED*/
16147 static void
16148f_tagfiles(argvars, rettv)
16149 typval_T *argvars;
16150 typval_T *rettv;
16151{
16152 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016153 tagname_T tn;
16154 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016155
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016156 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016157 {
16158 rettv->vval.v_number = 0;
16159 return;
16160 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016161
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016162 for (first = TRUE; ; first = FALSE)
16163 if (get_tagfname(&tn, first, fname) == FAIL
16164 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016165 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016166 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016167}
16168
16169/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016170 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016171 */
16172 static void
16173f_taglist(argvars, rettv)
16174 typval_T *argvars;
16175 typval_T *rettv;
16176{
16177 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016178
16179 tag_pattern = get_tv_string(&argvars[0]);
16180
16181 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016182 if (*tag_pattern == NUL)
16183 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016184
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016185 if (rettv_list_alloc(rettv) == OK)
16186 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016187}
16188
16189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 * "tempname()" function
16191 */
16192/*ARGSUSED*/
16193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016194f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016195 typval_T *argvars;
16196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197{
16198 static int x = 'A';
16199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016200 rettv->v_type = VAR_STRING;
16201 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202
16203 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16204 * names. Skip 'I' and 'O', they are used for shell redirection. */
16205 do
16206 {
16207 if (x == 'Z')
16208 x = '0';
16209 else if (x == '9')
16210 x = 'A';
16211 else
16212 {
16213#ifdef EBCDIC
16214 if (x == 'I')
16215 x = 'J';
16216 else if (x == 'R')
16217 x = 'S';
16218 else
16219#endif
16220 ++x;
16221 }
16222 } while (x == 'I' || x == 'O');
16223}
16224
16225/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016226 * "test(list)" function: Just checking the walls...
16227 */
16228/*ARGSUSED*/
16229 static void
16230f_test(argvars, rettv)
16231 typval_T *argvars;
16232 typval_T *rettv;
16233{
16234 /* Used for unit testing. Change the code below to your liking. */
16235#if 0
16236 listitem_T *li;
16237 list_T *l;
16238 char_u *bad, *good;
16239
16240 if (argvars[0].v_type != VAR_LIST)
16241 return;
16242 l = argvars[0].vval.v_list;
16243 if (l == NULL)
16244 return;
16245 li = l->lv_first;
16246 if (li == NULL)
16247 return;
16248 bad = get_tv_string(&li->li_tv);
16249 li = li->li_next;
16250 if (li == NULL)
16251 return;
16252 good = get_tv_string(&li->li_tv);
16253 rettv->vval.v_number = test_edit_score(bad, good);
16254#endif
16255}
16256
16257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 * "tolower(string)" function
16259 */
16260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016261f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016262 typval_T *argvars;
16263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264{
16265 char_u *p;
16266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016267 p = vim_strsave(get_tv_string(&argvars[0]));
16268 rettv->v_type = VAR_STRING;
16269 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270
16271 if (p != NULL)
16272 while (*p != NUL)
16273 {
16274#ifdef FEAT_MBYTE
16275 int l;
16276
16277 if (enc_utf8)
16278 {
16279 int c, lc;
16280
16281 c = utf_ptr2char(p);
16282 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016283 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 /* TODO: reallocate string when byte count changes. */
16285 if (utf_char2len(lc) == l)
16286 utf_char2bytes(lc, p);
16287 p += l;
16288 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016289 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 p += l; /* skip multi-byte character */
16291 else
16292#endif
16293 {
16294 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16295 ++p;
16296 }
16297 }
16298}
16299
16300/*
16301 * "toupper(string)" function
16302 */
16303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016304f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016305 typval_T *argvars;
16306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016308 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016309 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310}
16311
16312/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016313 * "tr(string, fromstr, tostr)" function
16314 */
16315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016316f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016317 typval_T *argvars;
16318 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016319{
16320 char_u *instr;
16321 char_u *fromstr;
16322 char_u *tostr;
16323 char_u *p;
16324#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016325 int inlen;
16326 int fromlen;
16327 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016328 int idx;
16329 char_u *cpstr;
16330 int cplen;
16331 int first = TRUE;
16332#endif
16333 char_u buf[NUMBUFLEN];
16334 char_u buf2[NUMBUFLEN];
16335 garray_T ga;
16336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016337 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016338 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16339 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016340
16341 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016342 rettv->v_type = VAR_STRING;
16343 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016344 if (fromstr == NULL || tostr == NULL)
16345 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016346 ga_init2(&ga, (int)sizeof(char), 80);
16347
16348#ifdef FEAT_MBYTE
16349 if (!has_mbyte)
16350#endif
16351 /* not multi-byte: fromstr and tostr must be the same length */
16352 if (STRLEN(fromstr) != STRLEN(tostr))
16353 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016354#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016355error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016356#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016357 EMSG2(_(e_invarg2), fromstr);
16358 ga_clear(&ga);
16359 return;
16360 }
16361
16362 /* fromstr and tostr have to contain the same number of chars */
16363 while (*instr != NUL)
16364 {
16365#ifdef FEAT_MBYTE
16366 if (has_mbyte)
16367 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016368 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016369 cpstr = instr;
16370 cplen = inlen;
16371 idx = 0;
16372 for (p = fromstr; *p != NUL; p += fromlen)
16373 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016374 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016375 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16376 {
16377 for (p = tostr; *p != NUL; p += tolen)
16378 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016379 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016380 if (idx-- == 0)
16381 {
16382 cplen = tolen;
16383 cpstr = p;
16384 break;
16385 }
16386 }
16387 if (*p == NUL) /* tostr is shorter than fromstr */
16388 goto error;
16389 break;
16390 }
16391 ++idx;
16392 }
16393
16394 if (first && cpstr == instr)
16395 {
16396 /* Check that fromstr and tostr have the same number of
16397 * (multi-byte) characters. Done only once when a character
16398 * of instr doesn't appear in fromstr. */
16399 first = FALSE;
16400 for (p = tostr; *p != NUL; p += tolen)
16401 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016402 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016403 --idx;
16404 }
16405 if (idx != 0)
16406 goto error;
16407 }
16408
16409 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016410 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016411 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016412
16413 instr += inlen;
16414 }
16415 else
16416#endif
16417 {
16418 /* When not using multi-byte chars we can do it faster. */
16419 p = vim_strchr(fromstr, *instr);
16420 if (p != NULL)
16421 ga_append(&ga, tostr[p - fromstr]);
16422 else
16423 ga_append(&ga, *instr);
16424 ++instr;
16425 }
16426 }
16427
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016428 /* add a terminating NUL */
16429 ga_grow(&ga, 1);
16430 ga_append(&ga, NUL);
16431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016432 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016433}
16434
16435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 * "type(expr)" function
16437 */
16438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016440 typval_T *argvars;
16441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016443 int n;
16444
16445 switch (argvars[0].v_type)
16446 {
16447 case VAR_NUMBER: n = 0; break;
16448 case VAR_STRING: n = 1; break;
16449 case VAR_FUNC: n = 2; break;
16450 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016451 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016452 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16453 }
16454 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455}
16456
16457/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016458 * "values(dict)" function
16459 */
16460 static void
16461f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 typval_T *argvars;
16463 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016464{
16465 dict_list(argvars, rettv, 1);
16466}
16467
16468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 * "virtcol(string)" function
16470 */
16471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016472f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016473 typval_T *argvars;
16474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475{
16476 colnr_T vcol = 0;
16477 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016478 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016480 fp = var2fpos(&argvars[0], FALSE, &fnum);
16481 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16482 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 {
16484 getvvcol(curwin, fp, NULL, NULL, &vcol);
16485 ++vcol;
16486 }
16487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016488 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489}
16490
16491/*
16492 * "visualmode()" function
16493 */
16494/*ARGSUSED*/
16495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016497 typval_T *argvars;
16498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499{
16500#ifdef FEAT_VISUAL
16501 char_u str[2];
16502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016503 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504 str[0] = curbuf->b_visual_mode_eval;
16505 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016506 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507
16508 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016509 if ((argvars[0].v_type == VAR_NUMBER
16510 && argvars[0].vval.v_number != 0)
16511 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016512 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 curbuf->b_visual_mode_eval = NUL;
16514#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016515 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516#endif
16517}
16518
16519/*
16520 * "winbufnr(nr)" function
16521 */
16522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016523f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 typval_T *argvars;
16525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526{
16527 win_T *wp;
16528
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016529 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016531 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016533 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534}
16535
16536/*
16537 * "wincol()" function
16538 */
16539/*ARGSUSED*/
16540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016541f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016542 typval_T *argvars;
16543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544{
16545 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016546 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547}
16548
16549/*
16550 * "winheight(nr)" function
16551 */
16552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016553f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016554 typval_T *argvars;
16555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556{
16557 win_T *wp;
16558
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016559 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016563 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564}
16565
16566/*
16567 * "winline()" function
16568 */
16569/*ARGSUSED*/
16570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016571f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016572 typval_T *argvars;
16573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574{
16575 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016576 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577}
16578
16579/*
16580 * "winnr()" function
16581 */
16582/* ARGSUSED */
16583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016584f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016585 typval_T *argvars;
16586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587{
16588 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016589
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016591 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016593 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594}
16595
16596/*
16597 * "winrestcmd()" function
16598 */
16599/* ARGSUSED */
16600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016601f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016602 typval_T *argvars;
16603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604{
16605#ifdef FEAT_WINDOWS
16606 win_T *wp;
16607 int winnr = 1;
16608 garray_T ga;
16609 char_u buf[50];
16610
16611 ga_init2(&ga, (int)sizeof(char), 70);
16612 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16613 {
16614 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16615 ga_concat(&ga, buf);
16616# ifdef FEAT_VERTSPLIT
16617 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16618 ga_concat(&ga, buf);
16619# endif
16620 ++winnr;
16621 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016622 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016624 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016628 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629}
16630
16631/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016632 * "winrestview()" function
16633 */
16634/* ARGSUSED */
16635 static void
16636f_winrestview(argvars, rettv)
16637 typval_T *argvars;
16638 typval_T *rettv;
16639{
16640 dict_T *dict;
16641
16642 if (argvars[0].v_type != VAR_DICT
16643 || (dict = argvars[0].vval.v_dict) == NULL)
16644 EMSG(_(e_invarg));
16645 else
16646 {
16647 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16648 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16649#ifdef FEAT_VIRTUALEDIT
16650 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16651#endif
16652 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016653 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016654
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016655 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016656#ifdef FEAT_DIFF
16657 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16658#endif
16659 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16660 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16661
16662 check_cursor();
16663 changed_cline_bef_curs();
16664 invalidate_botline();
16665 redraw_later(VALID);
16666
16667 if (curwin->w_topline == 0)
16668 curwin->w_topline = 1;
16669 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16670 curwin->w_topline = curbuf->b_ml.ml_line_count;
16671#ifdef FEAT_DIFF
16672 check_topfill(curwin, TRUE);
16673#endif
16674 }
16675}
16676
16677/*
16678 * "winsaveview()" function
16679 */
16680/* ARGSUSED */
16681 static void
16682f_winsaveview(argvars, rettv)
16683 typval_T *argvars;
16684 typval_T *rettv;
16685{
16686 dict_T *dict;
16687
16688 dict = dict_alloc();
16689 if (dict == NULL)
16690 return;
16691 rettv->v_type = VAR_DICT;
16692 rettv->vval.v_dict = dict;
16693 ++dict->dv_refcount;
16694
16695 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16696 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16697#ifdef FEAT_VIRTUALEDIT
16698 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16699#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016700 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016701 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16702
16703 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16704#ifdef FEAT_DIFF
16705 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16706#endif
16707 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16708 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16709}
16710
16711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 * "winwidth(nr)" function
16713 */
16714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016715f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016716 typval_T *argvars;
16717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718{
16719 win_T *wp;
16720
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016721 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016723 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 else
16725#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016726 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016728 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729#endif
16730}
16731
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016733 * "writefile()" function
16734 */
16735 static void
16736f_writefile(argvars, rettv)
16737 typval_T *argvars;
16738 typval_T *rettv;
16739{
16740 int binary = FALSE;
16741 char_u *fname;
16742 FILE *fd;
16743 listitem_T *li;
16744 char_u *s;
16745 int ret = 0;
16746 int c;
16747
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016748 if (check_restricted() || check_secure())
16749 return;
16750
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016751 if (argvars[0].v_type != VAR_LIST)
16752 {
16753 EMSG2(_(e_listarg), "writefile()");
16754 return;
16755 }
16756 if (argvars[0].vval.v_list == NULL)
16757 return;
16758
16759 if (argvars[2].v_type != VAR_UNKNOWN
16760 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16761 binary = TRUE;
16762
16763 /* Always open the file in binary mode, library functions have a mind of
16764 * their own about CR-LF conversion. */
16765 fname = get_tv_string(&argvars[1]);
16766 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16767 {
16768 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16769 ret = -1;
16770 }
16771 else
16772 {
16773 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16774 li = li->li_next)
16775 {
16776 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16777 {
16778 if (*s == '\n')
16779 c = putc(NUL, fd);
16780 else
16781 c = putc(*s, fd);
16782 if (c == EOF)
16783 {
16784 ret = -1;
16785 break;
16786 }
16787 }
16788 if (!binary || li->li_next != NULL)
16789 if (putc('\n', fd) == EOF)
16790 {
16791 ret = -1;
16792 break;
16793 }
16794 if (ret < 0)
16795 {
16796 EMSG(_(e_write));
16797 break;
16798 }
16799 }
16800 fclose(fd);
16801 }
16802
16803 rettv->vval.v_number = ret;
16804}
16805
16806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016808 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 */
16810 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016811var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016812 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016813 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016814 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016816 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016818 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819
Bram Moolenaara5525202006-03-02 22:52:09 +000016820 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016821 if (varp->v_type == VAR_LIST)
16822 {
16823 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016824 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016825 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016826 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016827
16828 l = varp->vval.v_list;
16829 if (l == NULL)
16830 return NULL;
16831
16832 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016833 pos.lnum = list_find_nr(l, 0L, &error);
16834 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016835 return NULL; /* invalid line number */
16836
16837 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016838 pos.col = list_find_nr(l, 1L, &error);
16839 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016840 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016841 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016842
16843 /* We accept "$" for the column number: last column. */
16844 li = list_find(l, 1L);
16845 if (li != NULL && li->li_tv.v_type == VAR_STRING
16846 && li->li_tv.vval.v_string != NULL
16847 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16848 pos.col = len + 1;
16849
Bram Moolenaara5525202006-03-02 22:52:09 +000016850 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016851 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016852 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016853 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016854
Bram Moolenaara5525202006-03-02 22:52:09 +000016855#ifdef FEAT_VIRTUALEDIT
16856 /* Get the virtual offset. Defaults to zero. */
16857 pos.coladd = list_find_nr(l, 2L, &error);
16858 if (error)
16859 pos.coladd = 0;
16860#endif
16861
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016862 return &pos;
16863 }
16864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016865 name = get_tv_string_chk(varp);
16866 if (name == NULL)
16867 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 if (name[0] == '.') /* cursor */
16869 return &curwin->w_cursor;
16870 if (name[0] == '\'') /* mark */
16871 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016872 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16874 return NULL;
16875 return pp;
16876 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016877
16878#ifdef FEAT_VIRTUALEDIT
16879 pos.coladd = 0;
16880#endif
16881
Bram Moolenaar477933c2007-07-17 14:32:23 +000016882 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016883 {
16884 pos.col = 0;
16885 if (name[1] == '0') /* "w0": first visible line */
16886 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016887 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016888 pos.lnum = curwin->w_topline;
16889 return &pos;
16890 }
16891 else if (name[1] == '$') /* "w$": last visible line */
16892 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016893 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016894 pos.lnum = curwin->w_botline - 1;
16895 return &pos;
16896 }
16897 }
16898 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016900 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 {
16902 pos.lnum = curbuf->b_ml.ml_line_count;
16903 pos.col = 0;
16904 }
16905 else
16906 {
16907 pos.lnum = curwin->w_cursor.lnum;
16908 pos.col = (colnr_T)STRLEN(ml_get_curline());
16909 }
16910 return &pos;
16911 }
16912 return NULL;
16913}
16914
16915/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016916 * Convert list in "arg" into a position and optional file number.
16917 * When "fnump" is NULL there is no file number, only 3 items.
16918 * Note that the column is passed on as-is, the caller may want to decrement
16919 * it to use 1 for the first column.
16920 * Return FAIL when conversion is not possible, doesn't check the position for
16921 * validity.
16922 */
16923 static int
16924list2fpos(arg, posp, fnump)
16925 typval_T *arg;
16926 pos_T *posp;
16927 int *fnump;
16928{
16929 list_T *l = arg->vval.v_list;
16930 long i = 0;
16931 long n;
16932
Bram Moolenaarbde35262006-07-23 20:12:24 +000016933 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16934 * when "fnump" isn't NULL and "coladd" is optional. */
16935 if (arg->v_type != VAR_LIST
16936 || l == NULL
16937 || l->lv_len < (fnump == NULL ? 2 : 3)
16938 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016939 return FAIL;
16940
16941 if (fnump != NULL)
16942 {
16943 n = list_find_nr(l, i++, NULL); /* fnum */
16944 if (n < 0)
16945 return FAIL;
16946 if (n == 0)
16947 n = curbuf->b_fnum; /* current buffer */
16948 *fnump = n;
16949 }
16950
16951 n = list_find_nr(l, i++, NULL); /* lnum */
16952 if (n < 0)
16953 return FAIL;
16954 posp->lnum = n;
16955
16956 n = list_find_nr(l, i++, NULL); /* col */
16957 if (n < 0)
16958 return FAIL;
16959 posp->col = n;
16960
16961#ifdef FEAT_VIRTUALEDIT
16962 n = list_find_nr(l, i, NULL);
16963 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016964 posp->coladd = 0;
16965 else
16966 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016967#endif
16968
16969 return OK;
16970}
16971
16972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 * Get the length of an environment variable name.
16974 * Advance "arg" to the first character after the name.
16975 * Return 0 for error.
16976 */
16977 static int
16978get_env_len(arg)
16979 char_u **arg;
16980{
16981 char_u *p;
16982 int len;
16983
16984 for (p = *arg; vim_isIDc(*p); ++p)
16985 ;
16986 if (p == *arg) /* no name found */
16987 return 0;
16988
16989 len = (int)(p - *arg);
16990 *arg = p;
16991 return len;
16992}
16993
16994/*
16995 * Get the length of the name of a function or internal variable.
16996 * "arg" is advanced to the first non-white character after the name.
16997 * Return 0 if something is wrong.
16998 */
16999 static int
17000get_id_len(arg)
17001 char_u **arg;
17002{
17003 char_u *p;
17004 int len;
17005
17006 /* Find the end of the name. */
17007 for (p = *arg; eval_isnamec(*p); ++p)
17008 ;
17009 if (p == *arg) /* no name found */
17010 return 0;
17011
17012 len = (int)(p - *arg);
17013 *arg = skipwhite(p);
17014
17015 return len;
17016}
17017
17018/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017019 * Get the length of the name of a variable or function.
17020 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017022 * Return -1 if curly braces expansion failed.
17023 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 * If the name contains 'magic' {}'s, expand them and return the
17025 * expanded name in an allocated string via 'alias' - caller must free.
17026 */
17027 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017028get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 char_u **arg;
17030 char_u **alias;
17031 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017032 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033{
17034 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 char_u *p;
17036 char_u *expr_start;
17037 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038
17039 *alias = NULL; /* default to no alias */
17040
17041 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17042 && (*arg)[2] == (int)KE_SNR)
17043 {
17044 /* hard coded <SNR>, already translated */
17045 *arg += 3;
17046 return get_id_len(arg) + 3;
17047 }
17048 len = eval_fname_script(*arg);
17049 if (len > 0)
17050 {
17051 /* literal "<SID>", "s:" or "<SNR>" */
17052 *arg += len;
17053 }
17054
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017056 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017058 p = find_name_end(*arg, &expr_start, &expr_end,
17059 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 if (expr_start != NULL)
17061 {
17062 char_u *temp_string;
17063
17064 if (!evaluate)
17065 {
17066 len += (int)(p - *arg);
17067 *arg = skipwhite(p);
17068 return len;
17069 }
17070
17071 /*
17072 * Include any <SID> etc in the expanded string:
17073 * Thus the -len here.
17074 */
17075 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17076 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017077 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 *alias = temp_string;
17079 *arg = skipwhite(p);
17080 return (int)STRLEN(temp_string);
17081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082
17083 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017084 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 EMSG2(_(e_invexpr2), *arg);
17086
17087 return len;
17088}
17089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017090/*
17091 * Find the end of a variable or function name, taking care of magic braces.
17092 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17093 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017094 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017095 * Return a pointer to just after the name. Equal to "arg" if there is no
17096 * valid name.
17097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017099find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 char_u *arg;
17101 char_u **expr_start;
17102 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017103 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017105 int mb_nest = 0;
17106 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 char_u *p;
17108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017109 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017111 *expr_start = NULL;
17112 *expr_end = NULL;
17113 }
17114
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017115 /* Quick check for valid starting character. */
17116 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17117 return arg;
17118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119 for (p = arg; *p != NUL
17120 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017121 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017122 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017123 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017124 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017125 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017126 if (*p == '\'')
17127 {
17128 /* skip over 'string' to avoid counting [ and ] inside it. */
17129 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17130 ;
17131 if (*p == NUL)
17132 break;
17133 }
17134 else if (*p == '"')
17135 {
17136 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17137 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17138 if (*p == '\\' && p[1] != NUL)
17139 ++p;
17140 if (*p == NUL)
17141 break;
17142 }
17143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017144 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017146 if (*p == '[')
17147 ++br_nest;
17148 else if (*p == ']')
17149 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017152 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017154 if (*p == '{')
17155 {
17156 mb_nest++;
17157 if (expr_start != NULL && *expr_start == NULL)
17158 *expr_start = p;
17159 }
17160 else if (*p == '}')
17161 {
17162 mb_nest--;
17163 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17164 *expr_end = p;
17165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 }
17168
17169 return p;
17170}
17171
17172/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017173 * Expands out the 'magic' {}'s in a variable/function name.
17174 * Note that this can call itself recursively, to deal with
17175 * constructs like foo{bar}{baz}{bam}
17176 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17177 * "in_start" ^
17178 * "expr_start" ^
17179 * "expr_end" ^
17180 * "in_end" ^
17181 *
17182 * Returns a new allocated string, which the caller must free.
17183 * Returns NULL for failure.
17184 */
17185 static char_u *
17186make_expanded_name(in_start, expr_start, expr_end, in_end)
17187 char_u *in_start;
17188 char_u *expr_start;
17189 char_u *expr_end;
17190 char_u *in_end;
17191{
17192 char_u c1;
17193 char_u *retval = NULL;
17194 char_u *temp_result;
17195 char_u *nextcmd = NULL;
17196
17197 if (expr_end == NULL || in_end == NULL)
17198 return NULL;
17199 *expr_start = NUL;
17200 *expr_end = NUL;
17201 c1 = *in_end;
17202 *in_end = NUL;
17203
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017204 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017205 if (temp_result != NULL && nextcmd == NULL)
17206 {
17207 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17208 + (in_end - expr_end) + 1));
17209 if (retval != NULL)
17210 {
17211 STRCPY(retval, in_start);
17212 STRCAT(retval, temp_result);
17213 STRCAT(retval, expr_end + 1);
17214 }
17215 }
17216 vim_free(temp_result);
17217
17218 *in_end = c1; /* put char back for error messages */
17219 *expr_start = '{';
17220 *expr_end = '}';
17221
17222 if (retval != NULL)
17223 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017224 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017225 if (expr_start != NULL)
17226 {
17227 /* Further expansion! */
17228 temp_result = make_expanded_name(retval, expr_start,
17229 expr_end, temp_result);
17230 vim_free(retval);
17231 retval = temp_result;
17232 }
17233 }
17234
17235 return retval;
17236}
17237
17238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017240 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 */
17242 static int
17243eval_isnamec(c)
17244 int c;
17245{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017246 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17247}
17248
17249/*
17250 * Return TRUE if character "c" can be used as the first character in a
17251 * variable or function name (excluding '{' and '}').
17252 */
17253 static int
17254eval_isnamec1(c)
17255 int c;
17256{
17257 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258}
17259
17260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 * Set number v: variable to "val".
17262 */
17263 void
17264set_vim_var_nr(idx, val)
17265 int idx;
17266 long val;
17267{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017268 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269}
17270
17271/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017272 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 */
17274 long
17275get_vim_var_nr(idx)
17276 int idx;
17277{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017278 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279}
17280
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017281#if defined(FEAT_AUTOCMD) || defined(PROTO)
17282/*
17283 * Get string v: variable value. Uses a static buffer, can only be used once.
17284 */
17285 char_u *
17286get_vim_var_str(idx)
17287 int idx;
17288{
17289 return get_tv_string(&vimvars[idx].vv_tv);
17290}
17291#endif
17292
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293/*
17294 * Set v:count, v:count1 and v:prevcount.
17295 */
17296 void
17297set_vcount(count, count1)
17298 long count;
17299 long count1;
17300{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017301 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17302 vimvars[VV_COUNT].vv_nr = count;
17303 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304}
17305
17306/*
17307 * Set string v: variable to a copy of "val".
17308 */
17309 void
17310set_vim_var_string(idx, val, len)
17311 int idx;
17312 char_u *val;
17313 int len; /* length of "val" to use or -1 (whole string) */
17314{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017315 /* Need to do this (at least) once, since we can't initialize a union.
17316 * Will always be invoked when "v:progname" is set. */
17317 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17318
Bram Moolenaare9a41262005-01-15 22:18:47 +000017319 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017321 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017323 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017325 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326}
17327
17328/*
17329 * Set v:register if needed.
17330 */
17331 void
17332set_reg_var(c)
17333 int c;
17334{
17335 char_u regname;
17336
17337 if (c == 0 || c == ' ')
17338 regname = '"';
17339 else
17340 regname = c;
17341 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017342 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 set_vim_var_string(VV_REG, &regname, 1);
17344}
17345
17346/*
17347 * Get or set v:exception. If "oldval" == NULL, return the current value.
17348 * Otherwise, restore the value to "oldval" and return NULL.
17349 * Must always be called in pairs to save and restore v:exception! Does not
17350 * take care of memory allocations.
17351 */
17352 char_u *
17353v_exception(oldval)
17354 char_u *oldval;
17355{
17356 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017357 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358
Bram Moolenaare9a41262005-01-15 22:18:47 +000017359 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 return NULL;
17361}
17362
17363/*
17364 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17365 * Otherwise, restore the value to "oldval" and return NULL.
17366 * Must always be called in pairs to save and restore v:throwpoint! Does not
17367 * take care of memory allocations.
17368 */
17369 char_u *
17370v_throwpoint(oldval)
17371 char_u *oldval;
17372{
17373 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017374 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375
Bram Moolenaare9a41262005-01-15 22:18:47 +000017376 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 return NULL;
17378}
17379
17380#if defined(FEAT_AUTOCMD) || defined(PROTO)
17381/*
17382 * Set v:cmdarg.
17383 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17384 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17385 * Must always be called in pairs!
17386 */
17387 char_u *
17388set_cmdarg(eap, oldarg)
17389 exarg_T *eap;
17390 char_u *oldarg;
17391{
17392 char_u *oldval;
17393 char_u *newval;
17394 unsigned len;
17395
Bram Moolenaare9a41262005-01-15 22:18:47 +000017396 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017397 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017399 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017400 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017401 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 }
17403
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017404 if (eap->force_bin == FORCE_BIN)
17405 len = 6;
17406 else if (eap->force_bin == FORCE_NOBIN)
17407 len = 8;
17408 else
17409 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017410
17411 if (eap->read_edit)
17412 len += 7;
17413
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017414 if (eap->force_ff != 0)
17415 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17416# ifdef FEAT_MBYTE
17417 if (eap->force_enc != 0)
17418 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017419 if (eap->bad_char != 0)
17420 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017421# endif
17422
17423 newval = alloc(len + 1);
17424 if (newval == NULL)
17425 return NULL;
17426
17427 if (eap->force_bin == FORCE_BIN)
17428 sprintf((char *)newval, " ++bin");
17429 else if (eap->force_bin == FORCE_NOBIN)
17430 sprintf((char *)newval, " ++nobin");
17431 else
17432 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017433
17434 if (eap->read_edit)
17435 STRCAT(newval, " ++edit");
17436
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017437 if (eap->force_ff != 0)
17438 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17439 eap->cmd + eap->force_ff);
17440# ifdef FEAT_MBYTE
17441 if (eap->force_enc != 0)
17442 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17443 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017444 if (eap->bad_char != 0)
17445 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17446 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017447# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017448 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017449 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450}
17451#endif
17452
17453/*
17454 * Get the value of internal variable "name".
17455 * Return OK or FAIL.
17456 */
17457 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017458get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459 char_u *name;
17460 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017462 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463{
17464 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017465 typval_T *tv = NULL;
17466 typval_T atv;
17467 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469
17470 /* truncate the name, so that we can use strcmp() */
17471 cc = name[len];
17472 name[len] = NUL;
17473
17474 /*
17475 * Check for "b:changedtick".
17476 */
17477 if (STRCMP(name, "b:changedtick") == 0)
17478 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017479 atv.v_type = VAR_NUMBER;
17480 atv.vval.v_number = curbuf->b_changedtick;
17481 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 }
17483
17484 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 * Check for user-defined variables.
17486 */
17487 else
17488 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017489 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017491 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 }
17493
Bram Moolenaare9a41262005-01-15 22:18:47 +000017494 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017496 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017497 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 ret = FAIL;
17499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017500 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017501 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502
17503 name[len] = cc;
17504
17505 return ret;
17506}
17507
17508/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017509 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17510 * Also handle function call with Funcref variable: func(expr)
17511 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17512 */
17513 static int
17514handle_subscript(arg, rettv, evaluate, verbose)
17515 char_u **arg;
17516 typval_T *rettv;
17517 int evaluate; /* do more than finding the end */
17518 int verbose; /* give error messages */
17519{
17520 int ret = OK;
17521 dict_T *selfdict = NULL;
17522 char_u *s;
17523 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017524 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017525
17526 while (ret == OK
17527 && (**arg == '['
17528 || (**arg == '.' && rettv->v_type == VAR_DICT)
17529 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17530 && !vim_iswhite(*(*arg - 1)))
17531 {
17532 if (**arg == '(')
17533 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017534 /* need to copy the funcref so that we can clear rettv */
17535 functv = *rettv;
17536 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017537
17538 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017539 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017540 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017541 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17542 &len, evaluate, selfdict);
17543
17544 /* Clear the funcref afterwards, so that deleting it while
17545 * evaluating the arguments is possible (see test55). */
17546 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017547
17548 /* Stop the expression evaluation when immediately aborting on
17549 * error, or when an interrupt occurred or an exception was thrown
17550 * but not caught. */
17551 if (aborting())
17552 {
17553 if (ret == OK)
17554 clear_tv(rettv);
17555 ret = FAIL;
17556 }
17557 dict_unref(selfdict);
17558 selfdict = NULL;
17559 }
17560 else /* **arg == '[' || **arg == '.' */
17561 {
17562 dict_unref(selfdict);
17563 if (rettv->v_type == VAR_DICT)
17564 {
17565 selfdict = rettv->vval.v_dict;
17566 if (selfdict != NULL)
17567 ++selfdict->dv_refcount;
17568 }
17569 else
17570 selfdict = NULL;
17571 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17572 {
17573 clear_tv(rettv);
17574 ret = FAIL;
17575 }
17576 }
17577 }
17578 dict_unref(selfdict);
17579 return ret;
17580}
17581
17582/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017583 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17584 * value).
17585 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017586 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017587alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017588{
Bram Moolenaar33570922005-01-25 22:26:29 +000017589 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017590}
17591
17592/*
17593 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 * The string "s" must have been allocated, it is consumed.
17595 * Return NULL for out of memory, the variable otherwise.
17596 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017597 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017598alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 char_u *s;
17600{
Bram Moolenaar33570922005-01-25 22:26:29 +000017601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017603 rettv = alloc_tv();
17604 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017606 rettv->v_type = VAR_STRING;
17607 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 }
17609 else
17610 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017611 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612}
17613
17614/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017615 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017617 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017618free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017619 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620{
17621 if (varp != NULL)
17622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017623 switch (varp->v_type)
17624 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017625 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017626 func_unref(varp->vval.v_string);
17627 /*FALLTHROUGH*/
17628 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017629 vim_free(varp->vval.v_string);
17630 break;
17631 case VAR_LIST:
17632 list_unref(varp->vval.v_list);
17633 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017634 case VAR_DICT:
17635 dict_unref(varp->vval.v_dict);
17636 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017637 case VAR_NUMBER:
17638 case VAR_UNKNOWN:
17639 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017640 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017641 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017642 break;
17643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 vim_free(varp);
17645 }
17646}
17647
17648/*
17649 * Free the memory for a variable value and set the value to NULL or 0.
17650 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017651 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017652clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017653 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654{
17655 if (varp != NULL)
17656 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017657 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017659 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017660 func_unref(varp->vval.v_string);
17661 /*FALLTHROUGH*/
17662 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017663 vim_free(varp->vval.v_string);
17664 varp->vval.v_string = NULL;
17665 break;
17666 case VAR_LIST:
17667 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017668 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017669 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017670 case VAR_DICT:
17671 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017672 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017673 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017674 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017675 varp->vval.v_number = 0;
17676 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017677 case VAR_UNKNOWN:
17678 break;
17679 default:
17680 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017682 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 }
17684}
17685
17686/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017687 * Set the value of a variable to NULL without freeing items.
17688 */
17689 static void
17690init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017691 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017692{
17693 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017694 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017695}
17696
17697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 * Get the number value of a variable.
17699 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017700 * For incompatible types, return 0.
17701 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17702 * caller of incompatible types: it sets *denote to TRUE if "denote"
17703 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 */
17705 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017706get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017707 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017709 int error = FALSE;
17710
17711 return get_tv_number_chk(varp, &error); /* return 0L on error */
17712}
17713
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017714 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017715get_tv_number_chk(varp, denote)
17716 typval_T *varp;
17717 int *denote;
17718{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017719 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017721 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017723 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017724 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017725 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017726 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017727 break;
17728 case VAR_STRING:
17729 if (varp->vval.v_string != NULL)
17730 vim_str2nr(varp->vval.v_string, NULL, NULL,
17731 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017732 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017733 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017734 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017735 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017736 case VAR_DICT:
17737 EMSG(_("E728: Using a Dictionary as a number"));
17738 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017739 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017740 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017741 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017743 if (denote == NULL) /* useful for values that must be unsigned */
17744 n = -1;
17745 else
17746 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017747 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748}
17749
17750/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017751 * Get the lnum from the first argument.
17752 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017753 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 */
17755 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017756get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017757 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758{
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 linenr_T lnum;
17761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017762 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763 if (lnum == 0) /* no valid number, try using line() */
17764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017765 rettv.v_type = VAR_NUMBER;
17766 f_line(argvars, &rettv);
17767 lnum = rettv.vval.v_number;
17768 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 }
17770 return lnum;
17771}
17772
17773/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017774 * Get the lnum from the first argument.
17775 * Also accepts "$", then "buf" is used.
17776 * Returns 0 on error.
17777 */
17778 static linenr_T
17779get_tv_lnum_buf(argvars, buf)
17780 typval_T *argvars;
17781 buf_T *buf;
17782{
17783 if (argvars[0].v_type == VAR_STRING
17784 && argvars[0].vval.v_string != NULL
17785 && argvars[0].vval.v_string[0] == '$'
17786 && buf != NULL)
17787 return buf->b_ml.ml_line_count;
17788 return get_tv_number_chk(&argvars[0], NULL);
17789}
17790
17791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 * Get the string value of a variable.
17793 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017794 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17795 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 * If the String variable has never been set, return an empty string.
17797 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017798 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17799 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800 */
17801 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017802get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017803 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804{
17805 static char_u mybuf[NUMBUFLEN];
17806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017807 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808}
17809
17810 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017811get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017812 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017813 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017815 char_u *res = get_tv_string_buf_chk(varp, buf);
17816
17817 return res != NULL ? res : (char_u *)"";
17818}
17819
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017820 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017821get_tv_string_chk(varp)
17822 typval_T *varp;
17823{
17824 static char_u mybuf[NUMBUFLEN];
17825
17826 return get_tv_string_buf_chk(varp, mybuf);
17827}
17828
17829 static char_u *
17830get_tv_string_buf_chk(varp, buf)
17831 typval_T *varp;
17832 char_u *buf;
17833{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017834 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017836 case VAR_NUMBER:
17837 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17838 return buf;
17839 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017840 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017841 break;
17842 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017843 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017844 break;
17845 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017846 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017847 break;
17848 case VAR_STRING:
17849 if (varp->vval.v_string != NULL)
17850 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017851 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017852 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017853 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017856 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857}
17858
17859/*
17860 * Find variable "name" in the list of variables.
17861 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017862 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017863 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017864 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017866 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017867find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017869 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017872 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873
Bram Moolenaara7043832005-01-21 11:56:39 +000017874 ht = find_var_ht(name, &varname);
17875 if (htp != NULL)
17876 *htp = ht;
17877 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017879 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880}
17881
17882/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017883 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017884 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017886 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017887find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017888 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017889 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017890 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017891{
Bram Moolenaar33570922005-01-25 22:26:29 +000017892 hashitem_T *hi;
17893
17894 if (*varname == NUL)
17895 {
17896 /* Must be something like "s:", otherwise "ht" would be NULL. */
17897 switch (varname[-2])
17898 {
17899 case 's': return &SCRIPT_SV(current_SID).sv_var;
17900 case 'g': return &globvars_var;
17901 case 'v': return &vimvars_var;
17902 case 'b': return &curbuf->b_bufvar;
17903 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017904#ifdef FEAT_WINDOWS
17905 case 't': return &curtab->tp_winvar;
17906#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017907 case 'l': return current_funccal == NULL
17908 ? NULL : &current_funccal->l_vars_var;
17909 case 'a': return current_funccal == NULL
17910 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017911 }
17912 return NULL;
17913 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017914
17915 hi = hash_find(ht, varname);
17916 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017917 {
17918 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017919 * worked find the variable again. Don't auto-load a script if it was
17920 * loaded already, otherwise it would be loaded every time when
17921 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017922 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017923 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017924 hi = hash_find(ht, varname);
17925 if (HASHITEM_EMPTY(hi))
17926 return NULL;
17927 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017928 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017929}
17930
17931/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017932 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017933 * Set "varname" to the start of name without ':'.
17934 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017935 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017936find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 char_u *name;
17938 char_u **varname;
17939{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017940 hashitem_T *hi;
17941
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 if (name[1] != ':')
17943 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017944 /* The name must not start with a colon or #. */
17945 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946 return NULL;
17947 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017948
17949 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017950 hi = hash_find(&compat_hashtab, name);
17951 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017952 return &compat_hashtab;
17953
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017955 return &globvarht; /* global variable */
17956 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 }
17958 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017959 if (*name == 'g') /* global variable */
17960 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017961 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17962 */
17963 if (vim_strchr(name + 2, ':') != NULL
17964 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017965 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017969 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017970#ifdef FEAT_WINDOWS
17971 if (*name == 't') /* tab page variable */
17972 return &curtab->tp_vars.dv_hashtab;
17973#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017974 if (*name == 'v') /* v: variable */
17975 return &vimvarht;
17976 if (*name == 'a' && current_funccal != NULL) /* function argument */
17977 return &current_funccal->l_avars.dv_hashtab;
17978 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17979 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 if (*name == 's' /* script variable */
17981 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17982 return &SCRIPT_VARS(current_SID);
17983 return NULL;
17984}
17985
17986/*
17987 * Get the string value of a (global/local) variable.
17988 * Returns NULL when it doesn't exist.
17989 */
17990 char_u *
17991get_var_value(name)
17992 char_u *name;
17993{
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995
Bram Moolenaara7043832005-01-21 11:56:39 +000017996 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 if (v == NULL)
17998 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017999 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000}
18001
18002/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018003 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 * sourcing this script and when executing functions defined in the script.
18005 */
18006 void
18007new_script_vars(id)
18008 scid_T id;
18009{
Bram Moolenaara7043832005-01-21 11:56:39 +000018010 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018011 hashtab_T *ht;
18012 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018013
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18015 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018016 /* Re-allocating ga_data means that an ht_array pointing to
18017 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018019 for (i = 1; i <= ga_scripts.ga_len; ++i)
18020 {
18021 ht = &SCRIPT_VARS(i);
18022 if (ht->ht_mask == HT_INIT_SIZE - 1)
18023 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018024 sv = &SCRIPT_SV(i);
18025 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018026 }
18027
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 while (ga_scripts.ga_len < id)
18029 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018030 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18031 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 }
18034 }
18035}
18036
18037/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018038 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18039 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 */
18041 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018042init_var_dict(dict, dict_var)
18043 dict_T *dict;
18044 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045{
Bram Moolenaar33570922005-01-25 22:26:29 +000018046 hash_init(&dict->dv_hashtab);
18047 dict->dv_refcount = 99999;
18048 dict_var->di_tv.vval.v_dict = dict;
18049 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018050 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018051 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18052 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053}
18054
18055/*
18056 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018057 * Frees all allocated variables and the value they contain.
18058 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 */
18060 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018061vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018062 hashtab_T *ht;
18063{
18064 vars_clear_ext(ht, TRUE);
18065}
18066
18067/*
18068 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18069 */
18070 static void
18071vars_clear_ext(ht, free_val)
18072 hashtab_T *ht;
18073 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074{
Bram Moolenaara7043832005-01-21 11:56:39 +000018075 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018076 hashitem_T *hi;
18077 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078
Bram Moolenaar33570922005-01-25 22:26:29 +000018079 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018080 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018081 for (hi = ht->ht_array; todo > 0; ++hi)
18082 {
18083 if (!HASHITEM_EMPTY(hi))
18084 {
18085 --todo;
18086
Bram Moolenaar33570922005-01-25 22:26:29 +000018087 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018088 * ht_array might change then. hash_clear() takes care of it
18089 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018090 v = HI2DI(hi);
18091 if (free_val)
18092 clear_tv(&v->di_tv);
18093 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18094 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018095 }
18096 }
18097 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018098 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099}
18100
Bram Moolenaara7043832005-01-21 11:56:39 +000018101/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018102 * Delete a variable from hashtab "ht" at item "hi".
18103 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018106delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018107 hashtab_T *ht;
18108 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109{
Bram Moolenaar33570922005-01-25 22:26:29 +000018110 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018111
18112 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018113 clear_tv(&di->di_tv);
18114 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115}
18116
18117/*
18118 * List the value of one internal variable.
18119 */
18120 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018121list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018122 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018124 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018126 char_u *tofree;
18127 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018128 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018129
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018130 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018131 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018132 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018133 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134}
18135
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018137list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 char_u *prefix;
18139 char_u *name;
18140 int type;
18141 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018142 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143{
Bram Moolenaar31859182007-08-14 20:41:13 +000018144 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18145 msg_start();
18146 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 if (name != NULL) /* "a:" vars don't have a name stored */
18148 msg_puts(name);
18149 msg_putchar(' ');
18150 msg_advance(22);
18151 if (type == VAR_NUMBER)
18152 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018153 else if (type == VAR_FUNC)
18154 msg_putchar('*');
18155 else if (type == VAR_LIST)
18156 {
18157 msg_putchar('[');
18158 if (*string == '[')
18159 ++string;
18160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018161 else if (type == VAR_DICT)
18162 {
18163 msg_putchar('{');
18164 if (*string == '{')
18165 ++string;
18166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 else
18168 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018169
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018171
18172 if (type == VAR_FUNC)
18173 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018174 if (*first)
18175 {
18176 msg_clr_eos();
18177 *first = FALSE;
18178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179}
18180
18181/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018182 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 * If the variable already exists, the value is updated.
18184 * Otherwise the variable is created.
18185 */
18186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018187set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018189 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018190 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191{
Bram Moolenaar33570922005-01-25 22:26:29 +000018192 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018194 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018195 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018197 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018198 {
18199 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18200 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18201 ? name[2] : name[0]))
18202 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018203 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018204 return;
18205 }
18206 if (function_exists(name))
18207 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018208 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018209 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018210 return;
18211 }
18212 }
18213
Bram Moolenaara7043832005-01-21 11:56:39 +000018214 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018215 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018216 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018217 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018218 return;
18219 }
18220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018221 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018222 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018224 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018225 if (var_check_ro(v->di_flags, name)
18226 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018227 return;
18228 if (v->di_tv.v_type != tv->v_type
18229 && !((v->di_tv.v_type == VAR_STRING
18230 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018231 && (tv->v_type == VAR_STRING
18232 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018233 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018234 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018235 return;
18236 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018237
18238 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018239 * Handle setting internal v: variables separately: we don't change
18240 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018241 */
18242 if (ht == &vimvarht)
18243 {
18244 if (v->di_tv.v_type == VAR_STRING)
18245 {
18246 vim_free(v->di_tv.vval.v_string);
18247 if (copy || tv->v_type != VAR_STRING)
18248 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18249 else
18250 {
18251 /* Take over the string to avoid an extra alloc/free. */
18252 v->di_tv.vval.v_string = tv->vval.v_string;
18253 tv->vval.v_string = NULL;
18254 }
18255 }
18256 else if (v->di_tv.v_type != VAR_NUMBER)
18257 EMSG2(_(e_intern2), "set_var()");
18258 else
18259 v->di_tv.vval.v_number = get_tv_number(tv);
18260 return;
18261 }
18262
18263 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 }
18265 else /* add a new variable */
18266 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018267 /* Can't add "v:" variable. */
18268 if (ht == &vimvarht)
18269 {
18270 EMSG2(_(e_illvar), name);
18271 return;
18272 }
18273
Bram Moolenaar92124a32005-06-17 22:03:40 +000018274 /* Make sure the variable name is valid. */
18275 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018276 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18277 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018278 {
18279 EMSG2(_(e_illvar), varname);
18280 return;
18281 }
18282
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018283 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18284 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018285 if (v == NULL)
18286 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018287 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018288 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018290 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291 return;
18292 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018293 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018296 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018297 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018298 else
18299 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018300 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018301 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018302 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304}
18305
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018306/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018307 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018308 * Also give an error message.
18309 */
18310 static int
18311var_check_ro(flags, name)
18312 int flags;
18313 char_u *name;
18314{
18315 if (flags & DI_FLAGS_RO)
18316 {
18317 EMSG2(_(e_readonlyvar), name);
18318 return TRUE;
18319 }
18320 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18321 {
18322 EMSG2(_(e_readonlysbx), name);
18323 return TRUE;
18324 }
18325 return FALSE;
18326}
18327
18328/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018329 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18330 * Also give an error message.
18331 */
18332 static int
18333var_check_fixed(flags, name)
18334 int flags;
18335 char_u *name;
18336{
18337 if (flags & DI_FLAGS_FIX)
18338 {
18339 EMSG2(_("E795: Cannot delete variable %s"), name);
18340 return TRUE;
18341 }
18342 return FALSE;
18343}
18344
18345/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018346 * Return TRUE if typeval "tv" is set to be locked (immutable).
18347 * Also give an error message, using "name".
18348 */
18349 static int
18350tv_check_lock(lock, name)
18351 int lock;
18352 char_u *name;
18353{
18354 if (lock & VAR_LOCKED)
18355 {
18356 EMSG2(_("E741: Value is locked: %s"),
18357 name == NULL ? (char_u *)_("Unknown") : name);
18358 return TRUE;
18359 }
18360 if (lock & VAR_FIXED)
18361 {
18362 EMSG2(_("E742: Cannot change value of %s"),
18363 name == NULL ? (char_u *)_("Unknown") : name);
18364 return TRUE;
18365 }
18366 return FALSE;
18367}
18368
18369/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018370 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018371 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018372 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018375copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018376 typval_T *from;
18377 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018379 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018380 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018381 switch (from->v_type)
18382 {
18383 case VAR_NUMBER:
18384 to->vval.v_number = from->vval.v_number;
18385 break;
18386 case VAR_STRING:
18387 case VAR_FUNC:
18388 if (from->vval.v_string == NULL)
18389 to->vval.v_string = NULL;
18390 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018391 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018392 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018393 if (from->v_type == VAR_FUNC)
18394 func_ref(to->vval.v_string);
18395 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018396 break;
18397 case VAR_LIST:
18398 if (from->vval.v_list == NULL)
18399 to->vval.v_list = NULL;
18400 else
18401 {
18402 to->vval.v_list = from->vval.v_list;
18403 ++to->vval.v_list->lv_refcount;
18404 }
18405 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018406 case VAR_DICT:
18407 if (from->vval.v_dict == NULL)
18408 to->vval.v_dict = NULL;
18409 else
18410 {
18411 to->vval.v_dict = from->vval.v_dict;
18412 ++to->vval.v_dict->dv_refcount;
18413 }
18414 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018415 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018416 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018417 break;
18418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419}
18420
18421/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018422 * Make a copy of an item.
18423 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018424 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18425 * reference to an already copied list/dict can be used.
18426 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018427 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018428 static int
18429item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 typval_T *from;
18431 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018432 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018433 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018434{
18435 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018436 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018437
Bram Moolenaar33570922005-01-25 22:26:29 +000018438 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018439 {
18440 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018441 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018442 }
18443 ++recurse;
18444
18445 switch (from->v_type)
18446 {
18447 case VAR_NUMBER:
18448 case VAR_STRING:
18449 case VAR_FUNC:
18450 copy_tv(from, to);
18451 break;
18452 case VAR_LIST:
18453 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018454 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018455 if (from->vval.v_list == NULL)
18456 to->vval.v_list = NULL;
18457 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18458 {
18459 /* use the copy made earlier */
18460 to->vval.v_list = from->vval.v_list->lv_copylist;
18461 ++to->vval.v_list->lv_refcount;
18462 }
18463 else
18464 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18465 if (to->vval.v_list == NULL)
18466 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018467 break;
18468 case VAR_DICT:
18469 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018470 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018471 if (from->vval.v_dict == NULL)
18472 to->vval.v_dict = NULL;
18473 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18474 {
18475 /* use the copy made earlier */
18476 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18477 ++to->vval.v_dict->dv_refcount;
18478 }
18479 else
18480 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18481 if (to->vval.v_dict == NULL)
18482 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018483 break;
18484 default:
18485 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018486 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018487 }
18488 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018489 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018490}
18491
18492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 * ":echo expr1 ..." print each argument separated with a space, add a
18494 * newline at the end.
18495 * ":echon expr1 ..." print each argument plain.
18496 */
18497 void
18498ex_echo(eap)
18499 exarg_T *eap;
18500{
18501 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018502 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018503 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 char_u *p;
18505 int needclr = TRUE;
18506 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018507 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508
18509 if (eap->skip)
18510 ++emsg_skip;
18511 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18512 {
18513 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018514 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 {
18516 /*
18517 * Report the invalid expression unless the expression evaluation
18518 * has been cancelled due to an aborting error, an interrupt, or an
18519 * exception.
18520 */
18521 if (!aborting())
18522 EMSG2(_(e_invexpr2), p);
18523 break;
18524 }
18525 if (!eap->skip)
18526 {
18527 if (atstart)
18528 {
18529 atstart = FALSE;
18530 /* Call msg_start() after eval1(), evaluating the expression
18531 * may cause a message to appear. */
18532 if (eap->cmdidx == CMD_echo)
18533 msg_start();
18534 }
18535 else if (eap->cmdidx == CMD_echo)
18536 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018537 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018538 if (p != NULL)
18539 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018541 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018543 if (*p != TAB && needclr)
18544 {
18545 /* remove any text still there from the command */
18546 msg_clr_eos();
18547 needclr = FALSE;
18548 }
18549 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 }
18551 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018552 {
18553#ifdef FEAT_MBYTE
18554 if (has_mbyte)
18555 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018556 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018557
18558 (void)msg_outtrans_len_attr(p, i, echo_attr);
18559 p += i - 1;
18560 }
18561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018563 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018566 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018568 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 arg = skipwhite(arg);
18570 }
18571 eap->nextcmd = check_nextcmd(arg);
18572
18573 if (eap->skip)
18574 --emsg_skip;
18575 else
18576 {
18577 /* remove text that may still be there from the command */
18578 if (needclr)
18579 msg_clr_eos();
18580 if (eap->cmdidx == CMD_echo)
18581 msg_end();
18582 }
18583}
18584
18585/*
18586 * ":echohl {name}".
18587 */
18588 void
18589ex_echohl(eap)
18590 exarg_T *eap;
18591{
18592 int id;
18593
18594 id = syn_name2id(eap->arg);
18595 if (id == 0)
18596 echo_attr = 0;
18597 else
18598 echo_attr = syn_id2attr(id);
18599}
18600
18601/*
18602 * ":execute expr1 ..." execute the result of an expression.
18603 * ":echomsg expr1 ..." Print a message
18604 * ":echoerr expr1 ..." Print an error
18605 * Each gets spaces around each argument and a newline at the end for
18606 * echo commands
18607 */
18608 void
18609ex_execute(eap)
18610 exarg_T *eap;
18611{
18612 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018613 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 int ret = OK;
18615 char_u *p;
18616 garray_T ga;
18617 int len;
18618 int save_did_emsg;
18619
18620 ga_init2(&ga, 1, 80);
18621
18622 if (eap->skip)
18623 ++emsg_skip;
18624 while (*arg != NUL && *arg != '|' && *arg != '\n')
18625 {
18626 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018627 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 {
18629 /*
18630 * Report the invalid expression unless the expression evaluation
18631 * has been cancelled due to an aborting error, an interrupt, or an
18632 * exception.
18633 */
18634 if (!aborting())
18635 EMSG2(_(e_invexpr2), p);
18636 ret = FAIL;
18637 break;
18638 }
18639
18640 if (!eap->skip)
18641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018642 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 len = (int)STRLEN(p);
18644 if (ga_grow(&ga, len + 2) == FAIL)
18645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 ret = FAIL;
18648 break;
18649 }
18650 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 ga.ga_len += len;
18654 }
18655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 arg = skipwhite(arg);
18658 }
18659
18660 if (ret != FAIL && ga.ga_data != NULL)
18661 {
18662 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018663 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018665 out_flush();
18666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 else if (eap->cmdidx == CMD_echoerr)
18668 {
18669 /* We don't want to abort following commands, restore did_emsg. */
18670 save_did_emsg = did_emsg;
18671 EMSG((char_u *)ga.ga_data);
18672 if (!force_abort)
18673 did_emsg = save_did_emsg;
18674 }
18675 else if (eap->cmdidx == CMD_execute)
18676 do_cmdline((char_u *)ga.ga_data,
18677 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18678 }
18679
18680 ga_clear(&ga);
18681
18682 if (eap->skip)
18683 --emsg_skip;
18684
18685 eap->nextcmd = check_nextcmd(arg);
18686}
18687
18688/*
18689 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18690 * "arg" points to the "&" or '+' when called, to "option" when returning.
18691 * Returns NULL when no option name found. Otherwise pointer to the char
18692 * after the option name.
18693 */
18694 static char_u *
18695find_option_end(arg, opt_flags)
18696 char_u **arg;
18697 int *opt_flags;
18698{
18699 char_u *p = *arg;
18700
18701 ++p;
18702 if (*p == 'g' && p[1] == ':')
18703 {
18704 *opt_flags = OPT_GLOBAL;
18705 p += 2;
18706 }
18707 else if (*p == 'l' && p[1] == ':')
18708 {
18709 *opt_flags = OPT_LOCAL;
18710 p += 2;
18711 }
18712 else
18713 *opt_flags = 0;
18714
18715 if (!ASCII_ISALPHA(*p))
18716 return NULL;
18717 *arg = p;
18718
18719 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18720 p += 4; /* termcap option */
18721 else
18722 while (ASCII_ISALPHA(*p))
18723 ++p;
18724 return p;
18725}
18726
18727/*
18728 * ":function"
18729 */
18730 void
18731ex_function(eap)
18732 exarg_T *eap;
18733{
18734 char_u *theline;
18735 int j;
18736 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 char_u *name = NULL;
18739 char_u *p;
18740 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018741 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 garray_T newargs;
18743 garray_T newlines;
18744 int varargs = FALSE;
18745 int mustend = FALSE;
18746 int flags = 0;
18747 ufunc_T *fp;
18748 int indent;
18749 int nesting;
18750 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 dictitem_T *v;
18752 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018753 static int func_nr = 0; /* number for nameless function */
18754 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018755 hashtab_T *ht;
18756 int todo;
18757 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018758 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759
18760 /*
18761 * ":function" without argument: list functions.
18762 */
18763 if (ends_excmd(*eap->arg))
18764 {
18765 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018766 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018767 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018768 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018769 {
18770 if (!HASHITEM_EMPTY(hi))
18771 {
18772 --todo;
18773 fp = HI2UF(hi);
18774 if (!isdigit(*fp->uf_name))
18775 list_func_head(fp, FALSE);
18776 }
18777 }
18778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 eap->nextcmd = check_nextcmd(eap->arg);
18780 return;
18781 }
18782
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018783 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018784 * ":function /pat": list functions matching pattern.
18785 */
18786 if (*eap->arg == '/')
18787 {
18788 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18789 if (!eap->skip)
18790 {
18791 regmatch_T regmatch;
18792
18793 c = *p;
18794 *p = NUL;
18795 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18796 *p = c;
18797 if (regmatch.regprog != NULL)
18798 {
18799 regmatch.rm_ic = p_ic;
18800
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018801 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018802 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18803 {
18804 if (!HASHITEM_EMPTY(hi))
18805 {
18806 --todo;
18807 fp = HI2UF(hi);
18808 if (!isdigit(*fp->uf_name)
18809 && vim_regexec(&regmatch, fp->uf_name, 0))
18810 list_func_head(fp, FALSE);
18811 }
18812 }
18813 }
18814 }
18815 if (*p == '/')
18816 ++p;
18817 eap->nextcmd = check_nextcmd(p);
18818 return;
18819 }
18820
18821 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018822 * Get the function name. There are these situations:
18823 * func normal function name
18824 * "name" == func, "fudi.fd_dict" == NULL
18825 * dict.func new dictionary entry
18826 * "name" == NULL, "fudi.fd_dict" set,
18827 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18828 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018829 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018830 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18831 * dict.func existing dict entry that's not a Funcref
18832 * "name" == NULL, "fudi.fd_dict" set,
18833 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018836 name = trans_function_name(&p, eap->skip, 0, &fudi);
18837 paren = (vim_strchr(p, '(') != NULL);
18838 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 {
18840 /*
18841 * Return on an invalid expression in braces, unless the expression
18842 * evaluation has been cancelled due to an aborting error, an
18843 * interrupt, or an exception.
18844 */
18845 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018846 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018847 if (!eap->skip && fudi.fd_newkey != NULL)
18848 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018849 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 else
18853 eap->skip = TRUE;
18854 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018855
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 /* An error in a function call during evaluation of an expression in magic
18857 * braces should not cause the function not to be defined. */
18858 saved_did_emsg = did_emsg;
18859 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860
18861 /*
18862 * ":function func" with only function name: list function.
18863 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018864 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 {
18866 if (!ends_excmd(*skipwhite(p)))
18867 {
18868 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018869 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 }
18871 eap->nextcmd = check_nextcmd(p);
18872 if (eap->nextcmd != NULL)
18873 *p = NUL;
18874 if (!eap->skip && !got_int)
18875 {
18876 fp = find_func(name);
18877 if (fp != NULL)
18878 {
18879 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018880 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018882 if (FUNCLINE(fp, j) == NULL)
18883 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 msg_putchar('\n');
18885 msg_outnum((long)(j + 1));
18886 if (j < 9)
18887 msg_putchar(' ');
18888 if (j < 99)
18889 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018890 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891 out_flush(); /* show a line at a time */
18892 ui_breakcheck();
18893 }
18894 if (!got_int)
18895 {
18896 msg_putchar('\n');
18897 msg_puts((char_u *)" endfunction");
18898 }
18899 }
18900 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018901 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018903 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 }
18905
18906 /*
18907 * ":function name(arg1, arg2)" Define function.
18908 */
18909 p = skipwhite(p);
18910 if (*p != '(')
18911 {
18912 if (!eap->skip)
18913 {
18914 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018915 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 }
18917 /* attempt to continue by skipping some text */
18918 if (vim_strchr(p, '(') != NULL)
18919 p = vim_strchr(p, '(');
18920 }
18921 p = skipwhite(p + 1);
18922
18923 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18924 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18925
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018926 if (!eap->skip)
18927 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018928 /* Check the name of the function. Unless it's a dictionary function
18929 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018930 if (name != NULL)
18931 arg = name;
18932 else
18933 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018934 if (arg != NULL && (fudi.fd_di == NULL
18935 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018936 {
18937 if (*arg == K_SPECIAL)
18938 j = 3;
18939 else
18940 j = 0;
18941 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18942 : eval_isnamec(arg[j])))
18943 ++j;
18944 if (arg[j] != NUL)
18945 emsg_funcname(_(e_invarg2), arg);
18946 }
18947 }
18948
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 /*
18950 * Isolate the arguments: "arg1, arg2, ...)"
18951 */
18952 while (*p != ')')
18953 {
18954 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18955 {
18956 varargs = TRUE;
18957 p += 3;
18958 mustend = TRUE;
18959 }
18960 else
18961 {
18962 arg = p;
18963 while (ASCII_ISALNUM(*p) || *p == '_')
18964 ++p;
18965 if (arg == p || isdigit(*arg)
18966 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18967 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18968 {
18969 if (!eap->skip)
18970 EMSG2(_("E125: Illegal argument: %s"), arg);
18971 break;
18972 }
18973 if (ga_grow(&newargs, 1) == FAIL)
18974 goto erret;
18975 c = *p;
18976 *p = NUL;
18977 arg = vim_strsave(arg);
18978 if (arg == NULL)
18979 goto erret;
18980 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18981 *p = c;
18982 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983 if (*p == ',')
18984 ++p;
18985 else
18986 mustend = TRUE;
18987 }
18988 p = skipwhite(p);
18989 if (mustend && *p != ')')
18990 {
18991 if (!eap->skip)
18992 EMSG2(_(e_invarg2), eap->arg);
18993 break;
18994 }
18995 }
18996 ++p; /* skip the ')' */
18997
Bram Moolenaare9a41262005-01-15 22:18:47 +000018998 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 for (;;)
19000 {
19001 p = skipwhite(p);
19002 if (STRNCMP(p, "range", 5) == 0)
19003 {
19004 flags |= FC_RANGE;
19005 p += 5;
19006 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019007 else if (STRNCMP(p, "dict", 4) == 0)
19008 {
19009 flags |= FC_DICT;
19010 p += 4;
19011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 else if (STRNCMP(p, "abort", 5) == 0)
19013 {
19014 flags |= FC_ABORT;
19015 p += 5;
19016 }
19017 else
19018 break;
19019 }
19020
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019021 /* When there is a line break use what follows for the function body.
19022 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19023 if (*p == '\n')
19024 line_arg = p + 1;
19025 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 EMSG(_(e_trailing));
19027
19028 /*
19029 * Read the body of the function, until ":endfunction" is found.
19030 */
19031 if (KeyTyped)
19032 {
19033 /* Check if the function already exists, don't let the user type the
19034 * whole function before telling him it doesn't work! For a script we
19035 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019036 if (!eap->skip && !eap->forceit)
19037 {
19038 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19039 EMSG(_(e_funcdict));
19040 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019041 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019044 if (!eap->skip && did_emsg)
19045 goto erret;
19046
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 msg_putchar('\n'); /* don't overwrite the function name */
19048 cmdline_row = msg_row;
19049 }
19050
19051 indent = 2;
19052 nesting = 0;
19053 for (;;)
19054 {
19055 msg_scroll = TRUE;
19056 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019057 sourcing_lnum_off = sourcing_lnum;
19058
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019059 if (line_arg != NULL)
19060 {
19061 /* Use eap->arg, split up in parts by line breaks. */
19062 theline = line_arg;
19063 p = vim_strchr(theline, '\n');
19064 if (p == NULL)
19065 line_arg += STRLEN(line_arg);
19066 else
19067 {
19068 *p = NUL;
19069 line_arg = p + 1;
19070 }
19071 }
19072 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 theline = getcmdline(':', 0L, indent);
19074 else
19075 theline = eap->getline(':', eap->cookie, indent);
19076 if (KeyTyped)
19077 lines_left = Rows - 1;
19078 if (theline == NULL)
19079 {
19080 EMSG(_("E126: Missing :endfunction"));
19081 goto erret;
19082 }
19083
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019084 /* Detect line continuation: sourcing_lnum increased more than one. */
19085 if (sourcing_lnum > sourcing_lnum_off + 1)
19086 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19087 else
19088 sourcing_lnum_off = 0;
19089
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 if (skip_until != NULL)
19091 {
19092 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19093 * don't check for ":endfunc". */
19094 if (STRCMP(theline, skip_until) == 0)
19095 {
19096 vim_free(skip_until);
19097 skip_until = NULL;
19098 }
19099 }
19100 else
19101 {
19102 /* skip ':' and blanks*/
19103 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19104 ;
19105
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019106 /* Check for "endfunction". */
19107 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019109 if (line_arg == NULL)
19110 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 break;
19112 }
19113
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019114 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 * at "end". */
19116 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19117 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019118 else if (STRNCMP(p, "if", 2) == 0
19119 || STRNCMP(p, "wh", 2) == 0
19120 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 || STRNCMP(p, "try", 3) == 0)
19122 indent += 2;
19123
19124 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019125 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019127 if (*p == '!')
19128 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 p += eval_fname_script(p);
19130 if (ASCII_ISALPHA(*p))
19131 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019132 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 if (*skipwhite(p) == '(')
19134 {
19135 ++nesting;
19136 indent += 2;
19137 }
19138 }
19139 }
19140
19141 /* Check for ":append" or ":insert". */
19142 p = skip_range(p, NULL);
19143 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19144 || (p[0] == 'i'
19145 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19146 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19147 skip_until = vim_strsave((char_u *)".");
19148
19149 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19150 arg = skipwhite(skiptowhite(p));
19151 if (arg[0] == '<' && arg[1] =='<'
19152 && ((p[0] == 'p' && p[1] == 'y'
19153 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19154 || (p[0] == 'p' && p[1] == 'e'
19155 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19156 || (p[0] == 't' && p[1] == 'c'
19157 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19158 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19159 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019160 || (p[0] == 'm' && p[1] == 'z'
19161 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 ))
19163 {
19164 /* ":python <<" continues until a dot, like ":append" */
19165 p = skipwhite(arg + 2);
19166 if (*p == NUL)
19167 skip_until = vim_strsave((char_u *)".");
19168 else
19169 skip_until = vim_strsave(p);
19170 }
19171 }
19172
19173 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019174 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019175 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019176 if (line_arg == NULL)
19177 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019179 }
19180
19181 /* Copy the line to newly allocated memory. get_one_sourceline()
19182 * allocates 250 bytes per line, this saves 80% on average. The cost
19183 * is an extra alloc/free. */
19184 p = vim_strsave(theline);
19185 if (p != NULL)
19186 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019187 if (line_arg == NULL)
19188 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019189 theline = p;
19190 }
19191
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019192 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19193
19194 /* Add NULL lines for continuation lines, so that the line count is
19195 * equal to the index in the growarray. */
19196 while (sourcing_lnum_off-- > 0)
19197 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019198
19199 /* Check for end of eap->arg. */
19200 if (line_arg != NULL && *line_arg == NUL)
19201 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 }
19203
19204 /* Don't define the function when skipping commands or when an error was
19205 * detected. */
19206 if (eap->skip || did_emsg)
19207 goto erret;
19208
19209 /*
19210 * If there are no errors, add the function
19211 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019212 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019213 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019214 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019215 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019216 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019217 emsg_funcname("E707: Function name conflicts with variable: %s",
19218 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019219 goto erret;
19220 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019221
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019222 fp = find_func(name);
19223 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019225 if (!eap->forceit)
19226 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019227 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019228 goto erret;
19229 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019230 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019231 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019232 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019233 name);
19234 goto erret;
19235 }
19236 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019237 ga_clear_strings(&(fp->uf_args));
19238 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019239 vim_free(name);
19240 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 }
19243 else
19244 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019245 char numbuf[20];
19246
19247 fp = NULL;
19248 if (fudi.fd_newkey == NULL && !eap->forceit)
19249 {
19250 EMSG(_(e_funcdict));
19251 goto erret;
19252 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019253 if (fudi.fd_di == NULL)
19254 {
19255 /* Can't add a function to a locked dictionary */
19256 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19257 goto erret;
19258 }
19259 /* Can't change an existing function if it is locked */
19260 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19261 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019262
19263 /* Give the function a sequential number. Can only be used with a
19264 * Funcref! */
19265 vim_free(name);
19266 sprintf(numbuf, "%d", ++func_nr);
19267 name = vim_strsave((char_u *)numbuf);
19268 if (name == NULL)
19269 goto erret;
19270 }
19271
19272 if (fp == NULL)
19273 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019274 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019275 {
19276 int slen, plen;
19277 char_u *scriptname;
19278
19279 /* Check that the autoload name matches the script name. */
19280 j = FAIL;
19281 if (sourcing_name != NULL)
19282 {
19283 scriptname = autoload_name(name);
19284 if (scriptname != NULL)
19285 {
19286 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019287 plen = (int)STRLEN(p);
19288 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019289 if (slen > plen && fnamecmp(p,
19290 sourcing_name + slen - plen) == 0)
19291 j = OK;
19292 vim_free(scriptname);
19293 }
19294 }
19295 if (j == FAIL)
19296 {
19297 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19298 goto erret;
19299 }
19300 }
19301
19302 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 if (fp == NULL)
19304 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019305
19306 if (fudi.fd_dict != NULL)
19307 {
19308 if (fudi.fd_di == NULL)
19309 {
19310 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019311 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019312 if (fudi.fd_di == NULL)
19313 {
19314 vim_free(fp);
19315 goto erret;
19316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019317 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19318 {
19319 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019320 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019321 goto erret;
19322 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019323 }
19324 else
19325 /* overwrite existing dict entry */
19326 clear_tv(&fudi.fd_di->di_tv);
19327 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019328 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019329 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019330 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019331
19332 /* behave like "dict" was used */
19333 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019334 }
19335
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019337 STRCPY(fp->uf_name, name);
19338 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019340 fp->uf_args = newargs;
19341 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019342#ifdef FEAT_PROFILE
19343 fp->uf_tml_count = NULL;
19344 fp->uf_tml_total = NULL;
19345 fp->uf_tml_self = NULL;
19346 fp->uf_profiling = FALSE;
19347 if (prof_def_func())
19348 func_do_profile(fp);
19349#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019350 fp->uf_varargs = varargs;
19351 fp->uf_flags = flags;
19352 fp->uf_calls = 0;
19353 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019354 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355
19356erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357 ga_clear_strings(&newargs);
19358 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019359ret_free:
19360 vim_free(skip_until);
19361 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364}
19365
19366/*
19367 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019368 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019370 * flags:
19371 * TFN_INT: internal function name OK
19372 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 * Advances "pp" to just after the function name (if no error).
19374 */
19375 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019376trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 char_u **pp;
19378 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019379 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019380 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019382 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 char_u *start;
19384 char_u *end;
19385 int lead;
19386 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019388 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019389
19390 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019391 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019393
19394 /* Check for hard coded <SNR>: already translated function ID (from a user
19395 * command). */
19396 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19397 && (*pp)[2] == (int)KE_SNR)
19398 {
19399 *pp += 3;
19400 len = get_id_len(pp) + 3;
19401 return vim_strnsave(start, len);
19402 }
19403
19404 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19405 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019407 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019409
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019410 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19411 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019412 if (end == start)
19413 {
19414 if (!skip)
19415 EMSG(_("E129: Function name required"));
19416 goto theend;
19417 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019418 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019419 {
19420 /*
19421 * Report an invalid expression in braces, unless the expression
19422 * evaluation has been cancelled due to an aborting error, an
19423 * interrupt, or an exception.
19424 */
19425 if (!aborting())
19426 {
19427 if (end != NULL)
19428 EMSG2(_(e_invarg2), start);
19429 }
19430 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019431 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019432 goto theend;
19433 }
19434
19435 if (lv.ll_tv != NULL)
19436 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019437 if (fdp != NULL)
19438 {
19439 fdp->fd_dict = lv.ll_dict;
19440 fdp->fd_newkey = lv.ll_newkey;
19441 lv.ll_newkey = NULL;
19442 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019443 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019444 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19445 {
19446 name = vim_strsave(lv.ll_tv->vval.v_string);
19447 *pp = end;
19448 }
19449 else
19450 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019451 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19452 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019453 EMSG(_(e_funcref));
19454 else
19455 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019456 name = NULL;
19457 }
19458 goto theend;
19459 }
19460
19461 if (lv.ll_name == NULL)
19462 {
19463 /* Error found, but continue after the function name. */
19464 *pp = end;
19465 goto theend;
19466 }
19467
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019468 /* Check if the name is a Funcref. If so, use the value. */
19469 if (lv.ll_exp_name != NULL)
19470 {
19471 len = (int)STRLEN(lv.ll_exp_name);
19472 name = deref_func_name(lv.ll_exp_name, &len);
19473 if (name == lv.ll_exp_name)
19474 name = NULL;
19475 }
19476 else
19477 {
19478 len = (int)(end - *pp);
19479 name = deref_func_name(*pp, &len);
19480 if (name == *pp)
19481 name = NULL;
19482 }
19483 if (name != NULL)
19484 {
19485 name = vim_strsave(name);
19486 *pp = end;
19487 goto theend;
19488 }
19489
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019490 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019491 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019492 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019493 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19494 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19495 {
19496 /* When there was "s:" already or the name expanded to get a
19497 * leading "s:" then remove it. */
19498 lv.ll_name += 2;
19499 len -= 2;
19500 lead = 2;
19501 }
19502 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019503 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019504 {
19505 if (lead == 2) /* skip over "s:" */
19506 lv.ll_name += 2;
19507 len = (int)(end - lv.ll_name);
19508 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019509
19510 /*
19511 * Copy the function name to allocated memory.
19512 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19513 * Accept <SNR>123_name() outside a script.
19514 */
19515 if (skip)
19516 lead = 0; /* do nothing */
19517 else if (lead > 0)
19518 {
19519 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019520 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19521 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019522 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019523 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019524 if (current_SID <= 0)
19525 {
19526 EMSG(_(e_usingsid));
19527 goto theend;
19528 }
19529 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19530 lead += (int)STRLEN(sid_buf);
19531 }
19532 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019533 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019534 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019535 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019536 goto theend;
19537 }
19538 name = alloc((unsigned)(len + lead + 1));
19539 if (name != NULL)
19540 {
19541 if (lead > 0)
19542 {
19543 name[0] = K_SPECIAL;
19544 name[1] = KS_EXTRA;
19545 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019546 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019547 STRCPY(name + 3, sid_buf);
19548 }
19549 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19550 name[len + lead] = NUL;
19551 }
19552 *pp = end;
19553
19554theend:
19555 clear_lval(&lv);
19556 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557}
19558
19559/*
19560 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19561 * Return 2 if "p" starts with "s:".
19562 * Return 0 otherwise.
19563 */
19564 static int
19565eval_fname_script(p)
19566 char_u *p;
19567{
19568 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19569 || STRNICMP(p + 1, "SNR>", 4) == 0))
19570 return 5;
19571 if (p[0] == 's' && p[1] == ':')
19572 return 2;
19573 return 0;
19574}
19575
19576/*
19577 * Return TRUE if "p" starts with "<SID>" or "s:".
19578 * Only works if eval_fname_script() returned non-zero for "p"!
19579 */
19580 static int
19581eval_fname_sid(p)
19582 char_u *p;
19583{
19584 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19585}
19586
19587/*
19588 * List the head of the function: "name(arg1, arg2)".
19589 */
19590 static void
19591list_func_head(fp, indent)
19592 ufunc_T *fp;
19593 int indent;
19594{
19595 int j;
19596
19597 msg_start();
19598 if (indent)
19599 MSG_PUTS(" ");
19600 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019601 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 {
19603 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019604 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 }
19606 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019607 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019609 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 {
19611 if (j)
19612 MSG_PUTS(", ");
19613 msg_puts(FUNCARG(fp, j));
19614 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019615 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 {
19617 if (j)
19618 MSG_PUTS(", ");
19619 MSG_PUTS("...");
19620 }
19621 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019622 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019623 if (p_verbose > 0)
19624 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625}
19626
19627/*
19628 * Find a function by name, return pointer to it in ufuncs.
19629 * Return NULL for unknown function.
19630 */
19631 static ufunc_T *
19632find_func(name)
19633 char_u *name;
19634{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019635 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019637 hi = hash_find(&func_hashtab, name);
19638 if (!HASHITEM_EMPTY(hi))
19639 return HI2UF(hi);
19640 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641}
19642
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019643#if defined(EXITFREE) || defined(PROTO)
19644 void
19645free_all_functions()
19646{
19647 hashitem_T *hi;
19648
19649 /* Need to start all over every time, because func_free() may change the
19650 * hash table. */
19651 while (func_hashtab.ht_used > 0)
19652 for (hi = func_hashtab.ht_array; ; ++hi)
19653 if (!HASHITEM_EMPTY(hi))
19654 {
19655 func_free(HI2UF(hi));
19656 break;
19657 }
19658}
19659#endif
19660
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019661/*
19662 * Return TRUE if a function "name" exists.
19663 */
19664 static int
19665function_exists(name)
19666 char_u *name;
19667{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019668 char_u *nm = name;
19669 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019670 int n = FALSE;
19671
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019672 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019673 nm = skipwhite(nm);
19674
19675 /* Only accept "funcname", "funcname ", "funcname (..." and
19676 * "funcname(...", not "funcname!...". */
19677 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019678 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019679 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019680 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019681 else
19682 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019683 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019684 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019685 return n;
19686}
19687
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019688/*
19689 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019690 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019691 */
19692 static int
19693builtin_function(name)
19694 char_u *name;
19695{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019696 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19697 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019698}
19699
Bram Moolenaar05159a02005-02-26 23:04:13 +000019700#if defined(FEAT_PROFILE) || defined(PROTO)
19701/*
19702 * Start profiling function "fp".
19703 */
19704 static void
19705func_do_profile(fp)
19706 ufunc_T *fp;
19707{
19708 fp->uf_tm_count = 0;
19709 profile_zero(&fp->uf_tm_self);
19710 profile_zero(&fp->uf_tm_total);
19711 if (fp->uf_tml_count == NULL)
19712 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19713 (sizeof(int) * fp->uf_lines.ga_len));
19714 if (fp->uf_tml_total == NULL)
19715 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19716 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19717 if (fp->uf_tml_self == NULL)
19718 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19719 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19720 fp->uf_tml_idx = -1;
19721 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19722 || fp->uf_tml_self == NULL)
19723 return; /* out of memory */
19724
19725 fp->uf_profiling = TRUE;
19726}
19727
19728/*
19729 * Dump the profiling results for all functions in file "fd".
19730 */
19731 void
19732func_dump_profile(fd)
19733 FILE *fd;
19734{
19735 hashitem_T *hi;
19736 int todo;
19737 ufunc_T *fp;
19738 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019739 ufunc_T **sorttab;
19740 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019741
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019742 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019743 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19744
Bram Moolenaar05159a02005-02-26 23:04:13 +000019745 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19746 {
19747 if (!HASHITEM_EMPTY(hi))
19748 {
19749 --todo;
19750 fp = HI2UF(hi);
19751 if (fp->uf_profiling)
19752 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019753 if (sorttab != NULL)
19754 sorttab[st_len++] = fp;
19755
Bram Moolenaar05159a02005-02-26 23:04:13 +000019756 if (fp->uf_name[0] == K_SPECIAL)
19757 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19758 else
19759 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19760 if (fp->uf_tm_count == 1)
19761 fprintf(fd, "Called 1 time\n");
19762 else
19763 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19764 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19765 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19766 fprintf(fd, "\n");
19767 fprintf(fd, "count total (s) self (s)\n");
19768
19769 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19770 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019771 if (FUNCLINE(fp, i) == NULL)
19772 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019773 prof_func_line(fd, fp->uf_tml_count[i],
19774 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019775 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19776 }
19777 fprintf(fd, "\n");
19778 }
19779 }
19780 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019781
19782 if (sorttab != NULL && st_len > 0)
19783 {
19784 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19785 prof_total_cmp);
19786 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19787 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19788 prof_self_cmp);
19789 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19790 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019791}
Bram Moolenaar73830342005-02-28 22:48:19 +000019792
19793 static void
19794prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19795 FILE *fd;
19796 ufunc_T **sorttab;
19797 int st_len;
19798 char *title;
19799 int prefer_self; /* when equal print only self time */
19800{
19801 int i;
19802 ufunc_T *fp;
19803
19804 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19805 fprintf(fd, "count total (s) self (s) function\n");
19806 for (i = 0; i < 20 && i < st_len; ++i)
19807 {
19808 fp = sorttab[i];
19809 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19810 prefer_self);
19811 if (fp->uf_name[0] == K_SPECIAL)
19812 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19813 else
19814 fprintf(fd, " %s()\n", fp->uf_name);
19815 }
19816 fprintf(fd, "\n");
19817}
19818
19819/*
19820 * Print the count and times for one function or function line.
19821 */
19822 static void
19823prof_func_line(fd, count, total, self, prefer_self)
19824 FILE *fd;
19825 int count;
19826 proftime_T *total;
19827 proftime_T *self;
19828 int prefer_self; /* when equal print only self time */
19829{
19830 if (count > 0)
19831 {
19832 fprintf(fd, "%5d ", count);
19833 if (prefer_self && profile_equal(total, self))
19834 fprintf(fd, " ");
19835 else
19836 fprintf(fd, "%s ", profile_msg(total));
19837 if (!prefer_self && profile_equal(total, self))
19838 fprintf(fd, " ");
19839 else
19840 fprintf(fd, "%s ", profile_msg(self));
19841 }
19842 else
19843 fprintf(fd, " ");
19844}
19845
19846/*
19847 * Compare function for total time sorting.
19848 */
19849 static int
19850#ifdef __BORLANDC__
19851_RTLENTRYF
19852#endif
19853prof_total_cmp(s1, s2)
19854 const void *s1;
19855 const void *s2;
19856{
19857 ufunc_T *p1, *p2;
19858
19859 p1 = *(ufunc_T **)s1;
19860 p2 = *(ufunc_T **)s2;
19861 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19862}
19863
19864/*
19865 * Compare function for self time sorting.
19866 */
19867 static int
19868#ifdef __BORLANDC__
19869_RTLENTRYF
19870#endif
19871prof_self_cmp(s1, s2)
19872 const void *s1;
19873 const void *s2;
19874{
19875 ufunc_T *p1, *p2;
19876
19877 p1 = *(ufunc_T **)s1;
19878 p2 = *(ufunc_T **)s2;
19879 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19880}
19881
Bram Moolenaar05159a02005-02-26 23:04:13 +000019882#endif
19883
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019884/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019885 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019886 * Return TRUE if a package was loaded.
19887 */
19888 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019889script_autoload(name, reload)
19890 char_u *name;
19891 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019892{
19893 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019894 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019895 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019896 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019897
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019898 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019899 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019900 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019901 return FALSE;
19902
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019903 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019904
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019905 /* Find the name in the list of previously loaded package names. Skip
19906 * "autoload/", it's always the same. */
19907 for (i = 0; i < ga_loaded.ga_len; ++i)
19908 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19909 break;
19910 if (!reload && i < ga_loaded.ga_len)
19911 ret = FALSE; /* was loaded already */
19912 else
19913 {
19914 /* Remember the name if it wasn't loaded already. */
19915 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19916 {
19917 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19918 tofree = NULL;
19919 }
19920
19921 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019922 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019923 ret = TRUE;
19924 }
19925
19926 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019927 return ret;
19928}
19929
19930/*
19931 * Return the autoload script name for a function or variable name.
19932 * Returns NULL when out of memory.
19933 */
19934 static char_u *
19935autoload_name(name)
19936 char_u *name;
19937{
19938 char_u *p;
19939 char_u *scriptname;
19940
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019941 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019942 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19943 if (scriptname == NULL)
19944 return FALSE;
19945 STRCPY(scriptname, "autoload/");
19946 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019947 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019948 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019949 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019950 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019951 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019952}
19953
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19955
19956/*
19957 * Function given to ExpandGeneric() to obtain the list of user defined
19958 * function names.
19959 */
19960 char_u *
19961get_user_func_name(xp, idx)
19962 expand_T *xp;
19963 int idx;
19964{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019965 static long_u done;
19966 static hashitem_T *hi;
19967 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968
19969 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019971 done = 0;
19972 hi = func_hashtab.ht_array;
19973 }
19974 if (done < func_hashtab.ht_used)
19975 {
19976 if (done++ > 0)
19977 ++hi;
19978 while (HASHITEM_EMPTY(hi))
19979 ++hi;
19980 fp = HI2UF(hi);
19981
19982 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19983 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984
19985 cat_func_name(IObuff, fp);
19986 if (xp->xp_context != EXPAND_USER_FUNC)
19987 {
19988 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019989 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 STRCAT(IObuff, ")");
19991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 return IObuff;
19993 }
19994 return NULL;
19995}
19996
19997#endif /* FEAT_CMDL_COMPL */
19998
19999/*
20000 * Copy the function name of "fp" to buffer "buf".
20001 * "buf" must be able to hold the function name plus three bytes.
20002 * Takes care of script-local function names.
20003 */
20004 static void
20005cat_func_name(buf, fp)
20006 char_u *buf;
20007 ufunc_T *fp;
20008{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020009 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 {
20011 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020012 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 }
20014 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020015 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016}
20017
20018/*
20019 * ":delfunction {name}"
20020 */
20021 void
20022ex_delfunction(eap)
20023 exarg_T *eap;
20024{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020025 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 char_u *p;
20027 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020028 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029
20030 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020031 name = trans_function_name(&p, eap->skip, 0, &fudi);
20032 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020034 {
20035 if (fudi.fd_dict != NULL && !eap->skip)
20036 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 if (!ends_excmd(*skipwhite(p)))
20040 {
20041 vim_free(name);
20042 EMSG(_(e_trailing));
20043 return;
20044 }
20045 eap->nextcmd = check_nextcmd(p);
20046 if (eap->nextcmd != NULL)
20047 *p = NUL;
20048
20049 if (!eap->skip)
20050 fp = find_func(name);
20051 vim_free(name);
20052
20053 if (!eap->skip)
20054 {
20055 if (fp == NULL)
20056 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020057 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 return;
20059 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020060 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 {
20062 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20063 return;
20064 }
20065
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020066 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020068 /* Delete the dict item that refers to the function, it will
20069 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020070 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020072 else
20073 func_free(fp);
20074 }
20075}
20076
20077/*
20078 * Free a function and remove it from the list of functions.
20079 */
20080 static void
20081func_free(fp)
20082 ufunc_T *fp;
20083{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020084 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020085
20086 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020087 ga_clear_strings(&(fp->uf_args));
20088 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020089#ifdef FEAT_PROFILE
20090 vim_free(fp->uf_tml_count);
20091 vim_free(fp->uf_tml_total);
20092 vim_free(fp->uf_tml_self);
20093#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020094
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020095 /* remove the function from the function hashtable */
20096 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20097 if (HASHITEM_EMPTY(hi))
20098 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020099 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020100 hash_remove(&func_hashtab, hi);
20101
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020102 vim_free(fp);
20103}
20104
20105/*
20106 * Unreference a Function: decrement the reference count and free it when it
20107 * becomes zero. Only for numbered functions.
20108 */
20109 static void
20110func_unref(name)
20111 char_u *name;
20112{
20113 ufunc_T *fp;
20114
20115 if (name != NULL && isdigit(*name))
20116 {
20117 fp = find_func(name);
20118 if (fp == NULL)
20119 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020120 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020121 {
20122 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020123 * when "uf_calls" becomes zero. */
20124 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020125 func_free(fp);
20126 }
20127 }
20128}
20129
20130/*
20131 * Count a reference to a Function.
20132 */
20133 static void
20134func_ref(name)
20135 char_u *name;
20136{
20137 ufunc_T *fp;
20138
20139 if (name != NULL && isdigit(*name))
20140 {
20141 fp = find_func(name);
20142 if (fp == NULL)
20143 EMSG2(_(e_intern2), "func_ref()");
20144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020145 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 }
20147}
20148
20149/*
20150 * Call a user function.
20151 */
20152 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020153call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154 ufunc_T *fp; /* pointer to function */
20155 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 typval_T *argvars; /* arguments */
20157 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 linenr_T firstline; /* first line of range */
20159 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020160 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161{
Bram Moolenaar33570922005-01-25 22:26:29 +000020162 char_u *save_sourcing_name;
20163 linenr_T save_sourcing_lnum;
20164 scid_T save_current_SID;
20165 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 int save_did_emsg;
20167 static int depth = 0;
20168 dictitem_T *v;
20169 int fixvar_idx = 0; /* index in fixvar[] */
20170 int i;
20171 int ai;
20172 char_u numbuf[NUMBUFLEN];
20173 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020174#ifdef FEAT_PROFILE
20175 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020176 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178
20179 /* If depth of calling is getting too high, don't execute the function */
20180 if (depth >= p_mfd)
20181 {
20182 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020183 rettv->v_type = VAR_NUMBER;
20184 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 return;
20186 }
20187 ++depth;
20188
20189 line_breakcheck(); /* check for CTRL-C hit */
20190
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020191 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020192 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020194 fc.rettv = rettv;
20195 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 fc.linenr = 0;
20197 fc.returned = FALSE;
20198 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020200 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 fc.dbg_tick = debug_tick;
20202
Bram Moolenaar33570922005-01-25 22:26:29 +000020203 /*
20204 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20205 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20206 * each argument variable and saves a lot of time.
20207 */
20208 /*
20209 * Init l: variables.
20210 */
20211 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020212 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020213 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020214 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20215 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020216 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020217 name = v->di_key;
20218 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020219 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20220 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20221 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020222 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 v->di_tv.vval.v_dict = selfdict;
20224 ++selfdict->dv_refcount;
20225 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020226
Bram Moolenaar33570922005-01-25 22:26:29 +000020227 /*
20228 * Init a: variables.
20229 * Set a:0 to "argcount".
20230 * Set a:000 to a list with room for the "..." arguments.
20231 */
20232 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20233 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020234 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 v = &fc.fixvar[fixvar_idx++].var;
20236 STRCPY(v->di_key, "000");
20237 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20238 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20239 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020240 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020241 v->di_tv.vval.v_list = &fc.l_varlist;
20242 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20243 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020244 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020245
20246 /*
20247 * Set a:firstline to "firstline" and a:lastline to "lastline".
20248 * Set a:name to named arguments.
20249 * Set a:N to the "..." arguments.
20250 */
20251 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20252 (varnumber_T)firstline);
20253 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20254 (varnumber_T)lastline);
20255 for (i = 0; i < argcount; ++i)
20256 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020257 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020258 if (ai < 0)
20259 /* named argument a:name */
20260 name = FUNCARG(fp, i);
20261 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020262 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020263 /* "..." argument a:1, a:2, etc. */
20264 sprintf((char *)numbuf, "%d", ai + 1);
20265 name = numbuf;
20266 }
20267 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20268 {
20269 v = &fc.fixvar[fixvar_idx++].var;
20270 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20271 }
20272 else
20273 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020274 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20275 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020276 if (v == NULL)
20277 break;
20278 v->di_flags = DI_FLAGS_RO;
20279 }
20280 STRCPY(v->di_key, name);
20281 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20282
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020283 /* Note: the values are copied directly to avoid alloc/free.
20284 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020285 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020286 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020287
20288 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20289 {
20290 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20291 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020292 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020293 }
20294 }
20295
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 /* Don't redraw while executing the function. */
20297 ++RedrawingDisabled;
20298 save_sourcing_name = sourcing_name;
20299 save_sourcing_lnum = sourcing_lnum;
20300 sourcing_lnum = 1;
20301 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020302 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 if (sourcing_name != NULL)
20304 {
20305 if (save_sourcing_name != NULL
20306 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20307 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20308 else
20309 STRCPY(sourcing_name, "function ");
20310 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20311
20312 if (p_verbose >= 12)
20313 {
20314 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020315 verbose_enter_scroll();
20316
Bram Moolenaar555b2802005-05-19 21:08:39 +000020317 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 if (p_verbose >= 14)
20319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020321 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020322 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020323 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324
20325 msg_puts((char_u *)"(");
20326 for (i = 0; i < argcount; ++i)
20327 {
20328 if (i > 0)
20329 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020330 if (argvars[i].v_type == VAR_NUMBER)
20331 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 else
20333 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020334 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20335 if (s != NULL)
20336 {
20337 trunc_string(s, buf, MSG_BUF_CLEN);
20338 msg_puts(buf);
20339 vim_free(tofree);
20340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 }
20342 }
20343 msg_puts((char_u *)")");
20344 }
20345 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020346
20347 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 --no_wait_return;
20349 }
20350 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020351#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020352 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020353 {
20354 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20355 func_do_profile(fp);
20356 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020357 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020358 {
20359 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020360 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020361 profile_zero(&fp->uf_tm_children);
20362 }
20363 script_prof_save(&wait_start);
20364 }
20365#endif
20366
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020368 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 save_did_emsg = did_emsg;
20370 did_emsg = FALSE;
20371
20372 /* call do_cmdline() to execute the lines */
20373 do_cmdline(NULL, get_func_line, (void *)&fc,
20374 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20375
20376 --RedrawingDisabled;
20377
20378 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020379 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 clear_tv(rettv);
20382 rettv->v_type = VAR_NUMBER;
20383 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 }
20385
Bram Moolenaar05159a02005-02-26 23:04:13 +000020386#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020387 if (do_profiling == PROF_YES && (fp->uf_profiling
20388 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020389 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020390 profile_end(&call_start);
20391 profile_sub_wait(&wait_start, &call_start);
20392 profile_add(&fp->uf_tm_total, &call_start);
20393 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020394 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020395 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020396 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20397 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020398 }
20399 }
20400#endif
20401
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402 /* when being verbose, mention the return value */
20403 if (p_verbose >= 12)
20404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020406 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020409 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020410 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020411 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20412 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020413 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020415 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020416 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020417 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020418 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020419
Bram Moolenaar555b2802005-05-19 21:08:39 +000020420 /* The value may be very long. Skip the middle part, so that we
20421 * have some idea how it starts and ends. smsg() would always
20422 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020423 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20424 if (s != NULL)
20425 {
20426 trunc_string(s, buf, MSG_BUF_CLEN);
20427 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20428 vim_free(tofree);
20429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 }
20431 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020432
20433 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 --no_wait_return;
20435 }
20436
20437 vim_free(sourcing_name);
20438 sourcing_name = save_sourcing_name;
20439 sourcing_lnum = save_sourcing_lnum;
20440 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020441#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020442 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020443 script_prof_restore(&wait_start);
20444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445
20446 if (p_verbose >= 12 && sourcing_name != NULL)
20447 {
20448 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020449 verbose_enter_scroll();
20450
Bram Moolenaar555b2802005-05-19 21:08:39 +000020451 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020453
20454 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 --no_wait_return;
20456 }
20457
20458 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020459 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460
Bram Moolenaar33570922005-01-25 22:26:29 +000020461 /* The a: variables typevals were not alloced, only free the allocated
20462 * variables. */
20463 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20464
20465 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 --depth;
20467}
20468
20469/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020470 * Add a number variable "name" to dict "dp" with value "nr".
20471 */
20472 static void
20473add_nr_var(dp, v, name, nr)
20474 dict_T *dp;
20475 dictitem_T *v;
20476 char *name;
20477 varnumber_T nr;
20478{
20479 STRCPY(v->di_key, name);
20480 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20481 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20482 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020483 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020484 v->di_tv.vval.v_number = nr;
20485}
20486
20487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 * ":return [expr]"
20489 */
20490 void
20491ex_return(eap)
20492 exarg_T *eap;
20493{
20494 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020495 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 int returning = FALSE;
20497
20498 if (current_funccal == NULL)
20499 {
20500 EMSG(_("E133: :return not inside a function"));
20501 return;
20502 }
20503
20504 if (eap->skip)
20505 ++emsg_skip;
20506
20507 eap->nextcmd = NULL;
20508 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020509 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 {
20511 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020512 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020514 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 }
20516 /* It's safer to return also on error. */
20517 else if (!eap->skip)
20518 {
20519 /*
20520 * Return unless the expression evaluation has been cancelled due to an
20521 * aborting error, an interrupt, or an exception.
20522 */
20523 if (!aborting())
20524 returning = do_return(eap, FALSE, TRUE, NULL);
20525 }
20526
20527 /* When skipping or the return gets pending, advance to the next command
20528 * in this line (!returning). Otherwise, ignore the rest of the line.
20529 * Following lines will be ignored by get_func_line(). */
20530 if (returning)
20531 eap->nextcmd = NULL;
20532 else if (eap->nextcmd == NULL) /* no argument */
20533 eap->nextcmd = check_nextcmd(arg);
20534
20535 if (eap->skip)
20536 --emsg_skip;
20537}
20538
20539/*
20540 * Return from a function. Possibly makes the return pending. Also called
20541 * for a pending return at the ":endtry" or after returning from an extra
20542 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020543 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020544 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 * FALSE when the return gets pending.
20546 */
20547 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020548do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 exarg_T *eap;
20550 int reanimate;
20551 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020552 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553{
20554 int idx;
20555 struct condstack *cstack = eap->cstack;
20556
20557 if (reanimate)
20558 /* Undo the return. */
20559 current_funccal->returned = FALSE;
20560
20561 /*
20562 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20563 * not in its finally clause (which then is to be executed next) is found.
20564 * In this case, make the ":return" pending for execution at the ":endtry".
20565 * Otherwise, return normally.
20566 */
20567 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20568 if (idx >= 0)
20569 {
20570 cstack->cs_pending[idx] = CSTP_RETURN;
20571
20572 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020573 /* A pending return again gets pending. "rettv" points to an
20574 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020576 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 else
20578 {
20579 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020580 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020582 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020584 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 {
20586 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020587 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020588 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 else
20590 EMSG(_(e_outofmem));
20591 }
20592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020593 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594
20595 if (reanimate)
20596 {
20597 /* The pending return value could be overwritten by a ":return"
20598 * without argument in a finally clause; reset the default
20599 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020600 current_funccal->rettv->v_type = VAR_NUMBER;
20601 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 }
20603 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020604 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 }
20606 else
20607 {
20608 current_funccal->returned = TRUE;
20609
20610 /* If the return is carried out now, store the return value. For
20611 * a return immediately after reanimation, the value is already
20612 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020613 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020615 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020616 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020618 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 }
20620 }
20621
20622 return idx < 0;
20623}
20624
20625/*
20626 * Free the variable with a pending return value.
20627 */
20628 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020629discard_pending_return(rettv)
20630 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631{
Bram Moolenaar33570922005-01-25 22:26:29 +000020632 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633}
20634
20635/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020636 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 * is an allocated string. Used by report_pending() for verbose messages.
20638 */
20639 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020640get_return_cmd(rettv)
20641 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020643 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020644 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020645 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020647 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020648 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020649 if (s == NULL)
20650 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020651
20652 STRCPY(IObuff, ":return ");
20653 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20654 if (STRLEN(s) + 8 >= IOSIZE)
20655 STRCPY(IObuff + IOSIZE - 4, "...");
20656 vim_free(tofree);
20657 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658}
20659
20660/*
20661 * Get next function line.
20662 * Called by do_cmdline() to get the next line.
20663 * Returns allocated string, or NULL for end of function.
20664 */
20665/* ARGSUSED */
20666 char_u *
20667get_func_line(c, cookie, indent)
20668 int c; /* not used */
20669 void *cookie;
20670 int indent; /* not used */
20671{
Bram Moolenaar33570922005-01-25 22:26:29 +000020672 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020673 ufunc_T *fp = fcp->func;
20674 char_u *retval;
20675 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676
20677 /* If breakpoints have been added/deleted need to check for it. */
20678 if (fcp->dbg_tick != debug_tick)
20679 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020680 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 sourcing_lnum);
20682 fcp->dbg_tick = debug_tick;
20683 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020684#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020685 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020686 func_line_end(cookie);
20687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688
Bram Moolenaar05159a02005-02-26 23:04:13 +000020689 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020690 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20691 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 retval = NULL;
20693 else
20694 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020695 /* Skip NULL lines (continuation lines). */
20696 while (fcp->linenr < gap->ga_len
20697 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20698 ++fcp->linenr;
20699 if (fcp->linenr >= gap->ga_len)
20700 retval = NULL;
20701 else
20702 {
20703 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20704 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020705#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020706 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020707 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020708#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 }
20711
20712 /* Did we encounter a breakpoint? */
20713 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20714 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020715 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020717 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 sourcing_lnum);
20719 fcp->dbg_tick = debug_tick;
20720 }
20721
20722 return retval;
20723}
20724
Bram Moolenaar05159a02005-02-26 23:04:13 +000020725#if defined(FEAT_PROFILE) || defined(PROTO)
20726/*
20727 * Called when starting to read a function line.
20728 * "sourcing_lnum" must be correct!
20729 * When skipping lines it may not actually be executed, but we won't find out
20730 * until later and we need to store the time now.
20731 */
20732 void
20733func_line_start(cookie)
20734 void *cookie;
20735{
20736 funccall_T *fcp = (funccall_T *)cookie;
20737 ufunc_T *fp = fcp->func;
20738
20739 if (fp->uf_profiling && sourcing_lnum >= 1
20740 && sourcing_lnum <= fp->uf_lines.ga_len)
20741 {
20742 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020743 /* Skip continuation lines. */
20744 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20745 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020746 fp->uf_tml_execed = FALSE;
20747 profile_start(&fp->uf_tml_start);
20748 profile_zero(&fp->uf_tml_children);
20749 profile_get_wait(&fp->uf_tml_wait);
20750 }
20751}
20752
20753/*
20754 * Called when actually executing a function line.
20755 */
20756 void
20757func_line_exec(cookie)
20758 void *cookie;
20759{
20760 funccall_T *fcp = (funccall_T *)cookie;
20761 ufunc_T *fp = fcp->func;
20762
20763 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20764 fp->uf_tml_execed = TRUE;
20765}
20766
20767/*
20768 * Called when done with a function line.
20769 */
20770 void
20771func_line_end(cookie)
20772 void *cookie;
20773{
20774 funccall_T *fcp = (funccall_T *)cookie;
20775 ufunc_T *fp = fcp->func;
20776
20777 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20778 {
20779 if (fp->uf_tml_execed)
20780 {
20781 ++fp->uf_tml_count[fp->uf_tml_idx];
20782 profile_end(&fp->uf_tml_start);
20783 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020784 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020785 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20786 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020787 }
20788 fp->uf_tml_idx = -1;
20789 }
20790}
20791#endif
20792
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793/*
20794 * Return TRUE if the currently active function should be ended, because a
20795 * return was encountered or an error occured. Used inside a ":while".
20796 */
20797 int
20798func_has_ended(cookie)
20799 void *cookie;
20800{
Bram Moolenaar33570922005-01-25 22:26:29 +000020801 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802
20803 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20804 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020805 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 || fcp->returned);
20807}
20808
20809/*
20810 * return TRUE if cookie indicates a function which "abort"s on errors.
20811 */
20812 int
20813func_has_abort(cookie)
20814 void *cookie;
20815{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020816 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817}
20818
20819#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20820typedef enum
20821{
20822 VAR_FLAVOUR_DEFAULT,
20823 VAR_FLAVOUR_SESSION,
20824 VAR_FLAVOUR_VIMINFO
20825} var_flavour_T;
20826
20827static var_flavour_T var_flavour __ARGS((char_u *varname));
20828
20829 static var_flavour_T
20830var_flavour(varname)
20831 char_u *varname;
20832{
20833 char_u *p = varname;
20834
20835 if (ASCII_ISUPPER(*p))
20836 {
20837 while (*(++p))
20838 if (ASCII_ISLOWER(*p))
20839 return VAR_FLAVOUR_SESSION;
20840 return VAR_FLAVOUR_VIMINFO;
20841 }
20842 else
20843 return VAR_FLAVOUR_DEFAULT;
20844}
20845#endif
20846
20847#if defined(FEAT_VIMINFO) || defined(PROTO)
20848/*
20849 * Restore global vars that start with a capital from the viminfo file
20850 */
20851 int
20852read_viminfo_varlist(virp, writing)
20853 vir_T *virp;
20854 int writing;
20855{
20856 char_u *tab;
20857 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020858 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859
20860 if (!writing && (find_viminfo_parameter('!') != NULL))
20861 {
20862 tab = vim_strchr(virp->vir_line + 1, '\t');
20863 if (tab != NULL)
20864 {
20865 *tab++ = '\0'; /* isolate the variable name */
20866 if (*tab == 'S') /* string var */
20867 is_string = TRUE;
20868
20869 tab = vim_strchr(tab, '\t');
20870 if (tab != NULL)
20871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 if (is_string)
20873 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020874 tv.v_type = VAR_STRING;
20875 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 }
20878 else
20879 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020880 tv.v_type = VAR_NUMBER;
20881 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020883 set_var(virp->vir_line + 1, &tv, FALSE);
20884 if (is_string)
20885 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 }
20887 }
20888 }
20889
20890 return viminfo_readline(virp);
20891}
20892
20893/*
20894 * Write global vars that start with a capital to the viminfo file
20895 */
20896 void
20897write_viminfo_varlist(fp)
20898 FILE *fp;
20899{
Bram Moolenaar33570922005-01-25 22:26:29 +000020900 hashitem_T *hi;
20901 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020902 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020903 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020904 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020905 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020906 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907
20908 if (find_viminfo_parameter('!') == NULL)
20909 return;
20910
20911 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020912
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020913 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020914 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020916 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020918 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020919 this_var = HI2DI(hi);
20920 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020921 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020922 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020923 {
20924 case VAR_STRING: s = "STR"; break;
20925 case VAR_NUMBER: s = "NUM"; break;
20926 default: continue;
20927 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020928 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020929 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020930 if (p != NULL)
20931 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020932 vim_free(tofree);
20933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 }
20935 }
20936}
20937#endif
20938
20939#if defined(FEAT_SESSION) || defined(PROTO)
20940 int
20941store_session_globals(fd)
20942 FILE *fd;
20943{
Bram Moolenaar33570922005-01-25 22:26:29 +000020944 hashitem_T *hi;
20945 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020946 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 char_u *p, *t;
20948
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020949 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020950 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020952 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020954 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020955 this_var = HI2DI(hi);
20956 if ((this_var->di_tv.v_type == VAR_NUMBER
20957 || this_var->di_tv.v_type == VAR_STRING)
20958 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020959 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020960 /* Escape special characters with a backslash. Turn a LF and
20961 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020962 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020963 (char_u *)"\\\"\n\r");
20964 if (p == NULL) /* out of memory */
20965 break;
20966 for (t = p; *t != NUL; ++t)
20967 if (*t == '\n')
20968 *t = 'n';
20969 else if (*t == '\r')
20970 *t = 'r';
20971 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020972 this_var->di_key,
20973 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20974 : ' ',
20975 p,
20976 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20977 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020978 || put_eol(fd) == FAIL)
20979 {
20980 vim_free(p);
20981 return FAIL;
20982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 vim_free(p);
20984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 }
20986 }
20987 return OK;
20988}
20989#endif
20990
Bram Moolenaar661b1822005-07-28 22:36:45 +000020991/*
20992 * Display script name where an item was last set.
20993 * Should only be invoked when 'verbose' is non-zero.
20994 */
20995 void
20996last_set_msg(scriptID)
20997 scid_T scriptID;
20998{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020999 char_u *p;
21000
Bram Moolenaar661b1822005-07-28 22:36:45 +000021001 if (scriptID != 0)
21002 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021003 p = home_replace_save(NULL, get_scriptname(scriptID));
21004 if (p != NULL)
21005 {
21006 verbose_enter();
21007 MSG_PUTS(_("\n\tLast set from "));
21008 MSG_PUTS(p);
21009 vim_free(p);
21010 verbose_leave();
21011 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021012 }
21013}
21014
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015#endif /* FEAT_EVAL */
21016
21017#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21018
21019
21020#ifdef WIN3264
21021/*
21022 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21023 */
21024static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21025static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21026static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21027
21028/*
21029 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021030 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 */
21032 static int
21033get_short_pathname(fnamep, bufp, fnamelen)
21034 char_u **fnamep;
21035 char_u **bufp;
21036 int *fnamelen;
21037{
21038 int l,len;
21039 char_u *newbuf;
21040
21041 len = *fnamelen;
21042
21043 l = GetShortPathName(*fnamep, *fnamep, len);
21044 if (l > len - 1)
21045 {
21046 /* If that doesn't work (not enough space), then save the string
21047 * and try again with a new buffer big enough
21048 */
21049 newbuf = vim_strnsave(*fnamep, l);
21050 if (newbuf == NULL)
21051 return 0;
21052
21053 vim_free(*bufp);
21054 *fnamep = *bufp = newbuf;
21055
21056 l = GetShortPathName(*fnamep,*fnamep,l+1);
21057
21058 /* Really should always succeed, as the buffer is big enough */
21059 }
21060
21061 *fnamelen = l;
21062 return 1;
21063}
21064
21065/*
21066 * Create a short path name. Returns the length of the buffer it needs.
21067 * Doesn't copy over the end of the buffer passed in.
21068 */
21069 static int
21070shortpath_for_invalid_fname(fname, bufp, fnamelen)
21071 char_u **fname;
21072 char_u **bufp;
21073 int *fnamelen;
21074{
21075 char_u *s, *p, *pbuf2, *pbuf3;
21076 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021077 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078
21079 /* Make a copy */
21080 len2 = *fnamelen;
21081 pbuf2 = vim_strnsave(*fname, len2);
21082 pbuf3 = NULL;
21083
21084 s = pbuf2 + len2 - 1; /* Find the end */
21085 slen = 1;
21086 plen = len2;
21087
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021088 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 {
21090 --s;
21091 ++slen;
21092 --plen;
21093 }
21094
21095 do
21096 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021097 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021098 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 {
21100 --s;
21101 ++slen;
21102 --plen;
21103 }
21104 if (s <= pbuf2)
21105 break;
21106
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021107 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 ch = *s;
21109 *s = 0; /* get_short_pathname requires a null-terminated string */
21110
21111 /* Try it in situ */
21112 p = pbuf2;
21113 if (!get_short_pathname(&p, &pbuf3, &plen))
21114 {
21115 vim_free(pbuf2);
21116 return -1;
21117 }
21118 *s = ch; /* Preserve the string */
21119 } while (plen == 0);
21120
21121 if (plen > 0)
21122 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021123 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 *fnamelen = len = plen + slen;
21125 vim_free(*bufp);
21126 if (len > len2)
21127 {
21128 /* If there's not enough space in the currently allocated string,
21129 * then copy it to a buffer big enough.
21130 */
21131 *fname= *bufp = vim_strnsave(p, len);
21132 if (*fname == NULL)
21133 return -1;
21134 }
21135 else
21136 {
21137 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21138 *fname = *bufp = pbuf2;
21139 if (p != pbuf2)
21140 strncpy(*fname, p, plen);
21141 pbuf2 = NULL;
21142 }
21143 /* Concat the next bit */
21144 strncpy(*fname + plen, s, slen);
21145 (*fname)[len] = '\0';
21146 }
21147 vim_free(pbuf3);
21148 vim_free(pbuf2);
21149 return 0;
21150}
21151
21152/*
21153 * Get a pathname for a partial path.
21154 */
21155 static int
21156shortpath_for_partial(fnamep, bufp, fnamelen)
21157 char_u **fnamep;
21158 char_u **bufp;
21159 int *fnamelen;
21160{
21161 int sepcount, len, tflen;
21162 char_u *p;
21163 char_u *pbuf, *tfname;
21164 int hasTilde;
21165
21166 /* Count up the path seperators from the RHS.. so we know which part
21167 * of the path to return.
21168 */
21169 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021170 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 if (vim_ispathsep(*p))
21172 ++sepcount;
21173
21174 /* Need full path first (use expand_env() to remove a "~/") */
21175 hasTilde = (**fnamep == '~');
21176 if (hasTilde)
21177 pbuf = tfname = expand_env_save(*fnamep);
21178 else
21179 pbuf = tfname = FullName_save(*fnamep, FALSE);
21180
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021181 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182
21183 if (!get_short_pathname(&tfname, &pbuf, &len))
21184 return -1;
21185
21186 if (len == 0)
21187 {
21188 /* Don't have a valid filename, so shorten the rest of the
21189 * path if we can. This CAN give us invalid 8.3 filenames, but
21190 * there's not a lot of point in guessing what it might be.
21191 */
21192 len = tflen;
21193 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21194 return -1;
21195 }
21196
21197 /* Count the paths backward to find the beginning of the desired string. */
21198 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021199 {
21200#ifdef FEAT_MBYTE
21201 if (has_mbyte)
21202 p -= mb_head_off(tfname, p);
21203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 if (vim_ispathsep(*p))
21205 {
21206 if (sepcount == 0 || (hasTilde && sepcount == 1))
21207 break;
21208 else
21209 sepcount --;
21210 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 if (hasTilde)
21213 {
21214 --p;
21215 if (p >= tfname)
21216 *p = '~';
21217 else
21218 return -1;
21219 }
21220 else
21221 ++p;
21222
21223 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21224 vim_free(*bufp);
21225 *fnamelen = (int)STRLEN(p);
21226 *bufp = pbuf;
21227 *fnamep = p;
21228
21229 return 0;
21230}
21231#endif /* WIN3264 */
21232
21233/*
21234 * Adjust a filename, according to a string of modifiers.
21235 * *fnamep must be NUL terminated when called. When returning, the length is
21236 * determined by *fnamelen.
21237 * Returns valid flags.
21238 * When there is an error, *fnamep is set to NULL.
21239 */
21240 int
21241modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21242 char_u *src; /* string with modifiers */
21243 int *usedlen; /* characters after src that are used */
21244 char_u **fnamep; /* file name so far */
21245 char_u **bufp; /* buffer for allocated file name or NULL */
21246 int *fnamelen; /* length of fnamep */
21247{
21248 int valid = 0;
21249 char_u *tail;
21250 char_u *s, *p, *pbuf;
21251 char_u dirname[MAXPATHL];
21252 int c;
21253 int has_fullname = 0;
21254#ifdef WIN3264
21255 int has_shortname = 0;
21256#endif
21257
21258repeat:
21259 /* ":p" - full path/file_name */
21260 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21261 {
21262 has_fullname = 1;
21263
21264 valid |= VALID_PATH;
21265 *usedlen += 2;
21266
21267 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21268 if ((*fnamep)[0] == '~'
21269#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21270 && ((*fnamep)[1] == '/'
21271# ifdef BACKSLASH_IN_FILENAME
21272 || (*fnamep)[1] == '\\'
21273# endif
21274 || (*fnamep)[1] == NUL)
21275
21276#endif
21277 )
21278 {
21279 *fnamep = expand_env_save(*fnamep);
21280 vim_free(*bufp); /* free any allocated file name */
21281 *bufp = *fnamep;
21282 if (*fnamep == NULL)
21283 return -1;
21284 }
21285
21286 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021287 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288 {
21289 if (vim_ispathsep(*p)
21290 && p[1] == '.'
21291 && (p[2] == NUL
21292 || vim_ispathsep(p[2])
21293 || (p[2] == '.'
21294 && (p[3] == NUL || vim_ispathsep(p[3])))))
21295 break;
21296 }
21297
21298 /* FullName_save() is slow, don't use it when not needed. */
21299 if (*p != NUL || !vim_isAbsName(*fnamep))
21300 {
21301 *fnamep = FullName_save(*fnamep, *p != NUL);
21302 vim_free(*bufp); /* free any allocated file name */
21303 *bufp = *fnamep;
21304 if (*fnamep == NULL)
21305 return -1;
21306 }
21307
21308 /* Append a path separator to a directory. */
21309 if (mch_isdir(*fnamep))
21310 {
21311 /* Make room for one or two extra characters. */
21312 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21313 vim_free(*bufp); /* free any allocated file name */
21314 *bufp = *fnamep;
21315 if (*fnamep == NULL)
21316 return -1;
21317 add_pathsep(*fnamep);
21318 }
21319 }
21320
21321 /* ":." - path relative to the current directory */
21322 /* ":~" - path relative to the home directory */
21323 /* ":8" - shortname path - postponed till after */
21324 while (src[*usedlen] == ':'
21325 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21326 {
21327 *usedlen += 2;
21328 if (c == '8')
21329 {
21330#ifdef WIN3264
21331 has_shortname = 1; /* Postpone this. */
21332#endif
21333 continue;
21334 }
21335 pbuf = NULL;
21336 /* Need full path first (use expand_env() to remove a "~/") */
21337 if (!has_fullname)
21338 {
21339 if (c == '.' && **fnamep == '~')
21340 p = pbuf = expand_env_save(*fnamep);
21341 else
21342 p = pbuf = FullName_save(*fnamep, FALSE);
21343 }
21344 else
21345 p = *fnamep;
21346
21347 has_fullname = 0;
21348
21349 if (p != NULL)
21350 {
21351 if (c == '.')
21352 {
21353 mch_dirname(dirname, MAXPATHL);
21354 s = shorten_fname(p, dirname);
21355 if (s != NULL)
21356 {
21357 *fnamep = s;
21358 if (pbuf != NULL)
21359 {
21360 vim_free(*bufp); /* free any allocated file name */
21361 *bufp = pbuf;
21362 pbuf = NULL;
21363 }
21364 }
21365 }
21366 else
21367 {
21368 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21369 /* Only replace it when it starts with '~' */
21370 if (*dirname == '~')
21371 {
21372 s = vim_strsave(dirname);
21373 if (s != NULL)
21374 {
21375 *fnamep = s;
21376 vim_free(*bufp);
21377 *bufp = s;
21378 }
21379 }
21380 }
21381 vim_free(pbuf);
21382 }
21383 }
21384
21385 tail = gettail(*fnamep);
21386 *fnamelen = (int)STRLEN(*fnamep);
21387
21388 /* ":h" - head, remove "/file_name", can be repeated */
21389 /* Don't remove the first "/" or "c:\" */
21390 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21391 {
21392 valid |= VALID_HEAD;
21393 *usedlen += 2;
21394 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021395 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021396 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 *fnamelen = (int)(tail - *fnamep);
21398#ifdef VMS
21399 if (*fnamelen > 0)
21400 *fnamelen += 1; /* the path separator is part of the path */
21401#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021402 if (*fnamelen == 0)
21403 {
21404 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21405 p = vim_strsave((char_u *)".");
21406 if (p == NULL)
21407 return -1;
21408 vim_free(*bufp);
21409 *bufp = *fnamep = tail = p;
21410 *fnamelen = 1;
21411 }
21412 else
21413 {
21414 while (tail > s && !after_pathsep(s, tail))
21415 mb_ptr_back(*fnamep, tail);
21416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 }
21418
21419 /* ":8" - shortname */
21420 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21421 {
21422 *usedlen += 2;
21423#ifdef WIN3264
21424 has_shortname = 1;
21425#endif
21426 }
21427
21428#ifdef WIN3264
21429 /* Check shortname after we have done 'heads' and before we do 'tails'
21430 */
21431 if (has_shortname)
21432 {
21433 pbuf = NULL;
21434 /* Copy the string if it is shortened by :h */
21435 if (*fnamelen < (int)STRLEN(*fnamep))
21436 {
21437 p = vim_strnsave(*fnamep, *fnamelen);
21438 if (p == 0)
21439 return -1;
21440 vim_free(*bufp);
21441 *bufp = *fnamep = p;
21442 }
21443
21444 /* Split into two implementations - makes it easier. First is where
21445 * there isn't a full name already, second is where there is.
21446 */
21447 if (!has_fullname && !vim_isAbsName(*fnamep))
21448 {
21449 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21450 return -1;
21451 }
21452 else
21453 {
21454 int l;
21455
21456 /* Simple case, already have the full-name
21457 * Nearly always shorter, so try first time. */
21458 l = *fnamelen;
21459 if (!get_short_pathname(fnamep, bufp, &l))
21460 return -1;
21461
21462 if (l == 0)
21463 {
21464 /* Couldn't find the filename.. search the paths.
21465 */
21466 l = *fnamelen;
21467 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21468 return -1;
21469 }
21470 *fnamelen = l;
21471 }
21472 }
21473#endif /* WIN3264 */
21474
21475 /* ":t" - tail, just the basename */
21476 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21477 {
21478 *usedlen += 2;
21479 *fnamelen -= (int)(tail - *fnamep);
21480 *fnamep = tail;
21481 }
21482
21483 /* ":e" - extension, can be repeated */
21484 /* ":r" - root, without extension, can be repeated */
21485 while (src[*usedlen] == ':'
21486 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21487 {
21488 /* find a '.' in the tail:
21489 * - for second :e: before the current fname
21490 * - otherwise: The last '.'
21491 */
21492 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21493 s = *fnamep - 2;
21494 else
21495 s = *fnamep + *fnamelen - 1;
21496 for ( ; s > tail; --s)
21497 if (s[0] == '.')
21498 break;
21499 if (src[*usedlen + 1] == 'e') /* :e */
21500 {
21501 if (s > tail)
21502 {
21503 *fnamelen += (int)(*fnamep - (s + 1));
21504 *fnamep = s + 1;
21505#ifdef VMS
21506 /* cut version from the extension */
21507 s = *fnamep + *fnamelen - 1;
21508 for ( ; s > *fnamep; --s)
21509 if (s[0] == ';')
21510 break;
21511 if (s > *fnamep)
21512 *fnamelen = s - *fnamep;
21513#endif
21514 }
21515 else if (*fnamep <= tail)
21516 *fnamelen = 0;
21517 }
21518 else /* :r */
21519 {
21520 if (s > tail) /* remove one extension */
21521 *fnamelen = (int)(s - *fnamep);
21522 }
21523 *usedlen += 2;
21524 }
21525
21526 /* ":s?pat?foo?" - substitute */
21527 /* ":gs?pat?foo?" - global substitute */
21528 if (src[*usedlen] == ':'
21529 && (src[*usedlen + 1] == 's'
21530 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21531 {
21532 char_u *str;
21533 char_u *pat;
21534 char_u *sub;
21535 int sep;
21536 char_u *flags;
21537 int didit = FALSE;
21538
21539 flags = (char_u *)"";
21540 s = src + *usedlen + 2;
21541 if (src[*usedlen + 1] == 'g')
21542 {
21543 flags = (char_u *)"g";
21544 ++s;
21545 }
21546
21547 sep = *s++;
21548 if (sep)
21549 {
21550 /* find end of pattern */
21551 p = vim_strchr(s, sep);
21552 if (p != NULL)
21553 {
21554 pat = vim_strnsave(s, (int)(p - s));
21555 if (pat != NULL)
21556 {
21557 s = p + 1;
21558 /* find end of substitution */
21559 p = vim_strchr(s, sep);
21560 if (p != NULL)
21561 {
21562 sub = vim_strnsave(s, (int)(p - s));
21563 str = vim_strnsave(*fnamep, *fnamelen);
21564 if (sub != NULL && str != NULL)
21565 {
21566 *usedlen = (int)(p + 1 - src);
21567 s = do_string_sub(str, pat, sub, flags);
21568 if (s != NULL)
21569 {
21570 *fnamep = s;
21571 *fnamelen = (int)STRLEN(s);
21572 vim_free(*bufp);
21573 *bufp = s;
21574 didit = TRUE;
21575 }
21576 }
21577 vim_free(sub);
21578 vim_free(str);
21579 }
21580 vim_free(pat);
21581 }
21582 }
21583 /* after using ":s", repeat all the modifiers */
21584 if (didit)
21585 goto repeat;
21586 }
21587 }
21588
21589 return valid;
21590}
21591
21592/*
21593 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21594 * "flags" can be "g" to do a global substitute.
21595 * Returns an allocated string, NULL for error.
21596 */
21597 char_u *
21598do_string_sub(str, pat, sub, flags)
21599 char_u *str;
21600 char_u *pat;
21601 char_u *sub;
21602 char_u *flags;
21603{
21604 int sublen;
21605 regmatch_T regmatch;
21606 int i;
21607 int do_all;
21608 char_u *tail;
21609 garray_T ga;
21610 char_u *ret;
21611 char_u *save_cpo;
21612
21613 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21614 save_cpo = p_cpo;
21615 p_cpo = (char_u *)"";
21616
21617 ga_init2(&ga, 1, 200);
21618
21619 do_all = (flags[0] == 'g');
21620
21621 regmatch.rm_ic = p_ic;
21622 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21623 if (regmatch.regprog != NULL)
21624 {
21625 tail = str;
21626 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21627 {
21628 /*
21629 * Get some space for a temporary buffer to do the substitution
21630 * into. It will contain:
21631 * - The text up to where the match is.
21632 * - The substituted text.
21633 * - The text after the match.
21634 */
21635 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21636 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21637 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21638 {
21639 ga_clear(&ga);
21640 break;
21641 }
21642
21643 /* copy the text up to where the match is */
21644 i = (int)(regmatch.startp[0] - tail);
21645 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21646 /* add the substituted text */
21647 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21648 + ga.ga_len + i, TRUE, TRUE, FALSE);
21649 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 /* avoid getting stuck on a match with an empty string */
21651 if (tail == regmatch.endp[0])
21652 {
21653 if (*tail == NUL)
21654 break;
21655 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21656 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 }
21658 else
21659 {
21660 tail = regmatch.endp[0];
21661 if (*tail == NUL)
21662 break;
21663 }
21664 if (!do_all)
21665 break;
21666 }
21667
21668 if (ga.ga_data != NULL)
21669 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21670
21671 vim_free(regmatch.regprog);
21672 }
21673
21674 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21675 ga_clear(&ga);
21676 p_cpo = save_cpo;
21677
21678 return ret;
21679}
21680
21681#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */