blob: 0f2461e1126d99f3e48c3351c384732658a22f71 [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
Bram Moolenaar08250432008-02-13 11:42:46 +000014779 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014780 name = get_tv_string_chk(argvars);
14781 if (name != NULL)
14782 {
14783 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14784 {
14785 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000014786 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014787 {
Bram Moolenaar08250432008-02-13 11:42:46 +000014788 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014789 if (fnum == curbuf->b_fnum)
14790 {
14791 curwin->w_cursor = pos;
14792 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000014793 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014794 }
14795 else
14796 EMSG(_(e_invarg));
14797 }
Bram Moolenaar08250432008-02-13 11:42:46 +000014798 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
14799 {
14800 /* set mark */
14801 if (setmark_pos(name[1], &pos, fnum) == OK)
14802 rettv->vval.v_number = 0;
14803 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014804 else
14805 EMSG(_(e_invarg));
14806 }
14807 }
14808}
14809
14810/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014811 * "setqflist()" function
14812 */
14813/*ARGSUSED*/
14814 static void
14815f_setqflist(argvars, rettv)
14816 typval_T *argvars;
14817 typval_T *rettv;
14818{
14819 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14820}
14821
14822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 * "setreg()" function
14824 */
14825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014826f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014827 typval_T *argvars;
14828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829{
14830 int regname;
14831 char_u *strregname;
14832 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014833 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 int append;
14835 char_u yank_type;
14836 long block_len;
14837
14838 block_len = -1;
14839 yank_type = MAUTO;
14840 append = FALSE;
14841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014842 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014843 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014845 if (strregname == NULL)
14846 return; /* type error; errmsg already given */
14847 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 if (regname == 0 || regname == '@')
14849 regname = '"';
14850 else if (regname == '=')
14851 return;
14852
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014855 stropt = get_tv_string_chk(&argvars[2]);
14856 if (stropt == NULL)
14857 return; /* type error */
14858 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 switch (*stropt)
14860 {
14861 case 'a': case 'A': /* append */
14862 append = TRUE;
14863 break;
14864 case 'v': case 'c': /* character-wise selection */
14865 yank_type = MCHAR;
14866 break;
14867 case 'V': case 'l': /* line-wise selection */
14868 yank_type = MLINE;
14869 break;
14870#ifdef FEAT_VISUAL
14871 case 'b': case Ctrl_V: /* block-wise selection */
14872 yank_type = MBLOCK;
14873 if (VIM_ISDIGIT(stropt[1]))
14874 {
14875 ++stropt;
14876 block_len = getdigits(&stropt) - 1;
14877 --stropt;
14878 }
14879 break;
14880#endif
14881 }
14882 }
14883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014884 strval = get_tv_string_chk(&argvars[1]);
14885 if (strval != NULL)
14886 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889}
14890
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014891/*
14892 * "settabwinvar()" function
14893 */
14894 static void
14895f_settabwinvar(argvars, rettv)
14896 typval_T *argvars;
14897 typval_T *rettv;
14898{
14899 setwinvar(argvars, rettv, 1);
14900}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901
14902/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014903 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014906f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014907 typval_T *argvars;
14908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014910 setwinvar(argvars, rettv, 0);
14911}
14912
14913/*
14914 * "setwinvar()" and "settabwinvar()" functions
14915 */
14916 static void
14917setwinvar(argvars, rettv, off)
14918 typval_T *argvars;
14919 typval_T *rettv;
14920 int off;
14921{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 win_T *win;
14923#ifdef FEAT_WINDOWS
14924 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014925 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926#endif
14927 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014928 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014930 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014932 rettv->vval.v_number = 0;
14933
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934 if (check_restricted() || check_secure())
14935 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014936
14937#ifdef FEAT_WINDOWS
14938 if (off == 1)
14939 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14940 else
14941 tp = curtab;
14942#endif
14943 win = find_win_by_nr(&argvars[off], tp);
14944 varname = get_tv_string_chk(&argvars[off + 1]);
14945 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946
14947 if (win != NULL && varname != NULL && varp != NULL)
14948 {
14949#ifdef FEAT_WINDOWS
14950 /* set curwin to be our win, temporarily */
14951 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014952 save_curtab = curtab;
14953 goto_tabpage_tp(tp);
14954 if (!win_valid(win))
14955 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 curwin = win;
14957 curbuf = curwin->w_buffer;
14958#endif
14959
14960 if (*varname == '&')
14961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014962 long numval;
14963 char_u *strval;
14964 int error = FALSE;
14965
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014967 numval = get_tv_number_chk(varp, &error);
14968 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014969 if (!error && strval != NULL)
14970 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 }
14972 else
14973 {
14974 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14975 if (winvarname != NULL)
14976 {
14977 STRCPY(winvarname, "w:");
14978 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014979 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 vim_free(winvarname);
14981 }
14982 }
14983
14984#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014985 /* Restore current tabpage and window, if still valid (autocomands can
14986 * make them invalid). */
14987 if (valid_tabpage(save_curtab))
14988 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 if (win_valid(save_curwin))
14990 {
14991 curwin = save_curwin;
14992 curbuf = curwin->w_buffer;
14993 }
14994#endif
14995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996}
14997
14998/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014999 * "shellescape({string})" function
15000 */
15001 static void
15002f_shellescape(argvars, rettv)
15003 typval_T *argvars;
15004 typval_T *rettv;
15005{
15006 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
15007 rettv->v_type = VAR_STRING;
15008}
15009
15010/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012 */
15013 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015014f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 typval_T *argvars;
15016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019
Bram Moolenaar0d660222005-01-07 21:51:51 +000015020 p = get_tv_string(&argvars[0]);
15021 rettv->vval.v_string = vim_strsave(p);
15022 simplify_filename(rettv->vval.v_string); /* simplify in place */
15023 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024}
15025
Bram Moolenaar0d660222005-01-07 21:51:51 +000015026static int
15027#ifdef __BORLANDC__
15028 _RTLENTRYF
15029#endif
15030 item_compare __ARGS((const void *s1, const void *s2));
15031static int
15032#ifdef __BORLANDC__
15033 _RTLENTRYF
15034#endif
15035 item_compare2 __ARGS((const void *s1, const void *s2));
15036
15037static int item_compare_ic;
15038static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015039static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015040#define ITEM_COMPARE_FAIL 999
15041
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015045 static int
15046#ifdef __BORLANDC__
15047_RTLENTRYF
15048#endif
15049item_compare(s1, s2)
15050 const void *s1;
15051 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015053 char_u *p1, *p2;
15054 char_u *tofree1, *tofree2;
15055 int res;
15056 char_u numbuf1[NUMBUFLEN];
15057 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015059 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15060 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015061 if (p1 == NULL)
15062 p1 = (char_u *)"";
15063 if (p2 == NULL)
15064 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065 if (item_compare_ic)
15066 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 res = STRCMP(p1, p2);
15069 vim_free(tofree1);
15070 vim_free(tofree2);
15071 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072}
15073
15074 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075#ifdef __BORLANDC__
15076_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078item_compare2(s1, s2)
15079 const void *s1;
15080 const void *s2;
15081{
15082 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015083 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015084 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015087 /* shortcut after failure in previous call; compare all items equal */
15088 if (item_compare_func_err)
15089 return 0;
15090
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015091 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15092 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015093 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15094 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095
15096 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015097 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015098 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099 clear_tv(&argv[0]);
15100 clear_tv(&argv[1]);
15101
15102 if (res == FAIL)
15103 res = ITEM_COMPARE_FAIL;
15104 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015105 /* return value has wrong type */
15106 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15107 if (item_compare_func_err)
15108 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109 clear_tv(&rettv);
15110 return res;
15111}
15112
15113/*
15114 * "sort({list})" function
15115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015118 typval_T *argvars;
15119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120{
Bram Moolenaar33570922005-01-25 22:26:29 +000015121 list_T *l;
15122 listitem_T *li;
15123 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015124 long len;
15125 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 rettv->vval.v_number = 0;
15128 if (argvars[0].v_type != VAR_LIST)
15129 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130 else
15131 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015133 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134 return;
15135 rettv->vval.v_list = l;
15136 rettv->v_type = VAR_LIST;
15137 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139 len = list_len(l);
15140 if (len <= 1)
15141 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143 item_compare_ic = FALSE;
15144 item_compare_func = NULL;
15145 if (argvars[1].v_type != VAR_UNKNOWN)
15146 {
15147 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149 else
15150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015151 int error = FALSE;
15152
15153 i = get_tv_number_chk(&argvars[1], &error);
15154 if (error)
15155 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 if (i == 1)
15157 item_compare_ic = TRUE;
15158 else
15159 item_compare_func = get_tv_string(&argvars[1]);
15160 }
15161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015164 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165 if (ptrs == NULL)
15166 return;
15167 i = 0;
15168 for (li = l->lv_first; li != NULL; li = li->li_next)
15169 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015171 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172 /* test the compare function */
15173 if (item_compare_func != NULL
15174 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15175 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015176 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178 {
15179 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015180 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181 item_compare_func == NULL ? item_compare : item_compare2);
15182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015183 if (!item_compare_func_err)
15184 {
15185 /* Clear the List and append the items in the sorted order. */
15186 l->lv_first = l->lv_last = NULL;
15187 l->lv_len = 0;
15188 for (i = 0; i < len; ++i)
15189 list_append(l, ptrs[i]);
15190 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191 }
15192
15193 vim_free(ptrs);
15194 }
15195}
15196
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015197/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015198 * "soundfold({word})" function
15199 */
15200 static void
15201f_soundfold(argvars, rettv)
15202 typval_T *argvars;
15203 typval_T *rettv;
15204{
15205 char_u *s;
15206
15207 rettv->v_type = VAR_STRING;
15208 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015209#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015210 rettv->vval.v_string = eval_soundfold(s);
15211#else
15212 rettv->vval.v_string = vim_strsave(s);
15213#endif
15214}
15215
15216/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015217 * "spellbadword()" function
15218 */
15219/* ARGSUSED */
15220 static void
15221f_spellbadword(argvars, rettv)
15222 typval_T *argvars;
15223 typval_T *rettv;
15224{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015225 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015226 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015227 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015228
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015229 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015230 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015231
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015232#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015233 if (argvars[0].v_type == VAR_UNKNOWN)
15234 {
15235 /* Find the start and length of the badly spelled word. */
15236 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15237 if (len != 0)
15238 word = ml_get_cursor();
15239 }
15240 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15241 {
15242 char_u *str = get_tv_string_chk(&argvars[0]);
15243 int capcol = -1;
15244
15245 if (str != NULL)
15246 {
15247 /* Check the argument for spelling. */
15248 while (*str != NUL)
15249 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015250 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015251 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015252 {
15253 word = str;
15254 break;
15255 }
15256 str += len;
15257 }
15258 }
15259 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015260#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015261
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015262 list_append_string(rettv->vval.v_list, word, len);
15263 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015264 attr == HLF_SPB ? "bad" :
15265 attr == HLF_SPR ? "rare" :
15266 attr == HLF_SPL ? "local" :
15267 attr == HLF_SPC ? "caps" :
15268 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015269}
15270
15271/*
15272 * "spellsuggest()" function
15273 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015274/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015275 static void
15276f_spellsuggest(argvars, rettv)
15277 typval_T *argvars;
15278 typval_T *rettv;
15279{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015280#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015281 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015282 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015283 int maxcount;
15284 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015285 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015286 listitem_T *li;
15287 int need_capital = FALSE;
15288#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015289
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015290 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015291 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015292
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015293#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015294 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15295 {
15296 str = get_tv_string(&argvars[0]);
15297 if (argvars[1].v_type != VAR_UNKNOWN)
15298 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015299 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015300 if (maxcount <= 0)
15301 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015302 if (argvars[2].v_type != VAR_UNKNOWN)
15303 {
15304 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15305 if (typeerr)
15306 return;
15307 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015308 }
15309 else
15310 maxcount = 25;
15311
Bram Moolenaar4770d092006-01-12 23:22:24 +000015312 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015313
15314 for (i = 0; i < ga.ga_len; ++i)
15315 {
15316 str = ((char_u **)ga.ga_data)[i];
15317
15318 li = listitem_alloc();
15319 if (li == NULL)
15320 vim_free(str);
15321 else
15322 {
15323 li->li_tv.v_type = VAR_STRING;
15324 li->li_tv.v_lock = 0;
15325 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015326 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015327 }
15328 }
15329 ga_clear(&ga);
15330 }
15331#endif
15332}
15333
Bram Moolenaar0d660222005-01-07 21:51:51 +000015334 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015335f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015336 typval_T *argvars;
15337 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015338{
15339 char_u *str;
15340 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015341 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015342 regmatch_T regmatch;
15343 char_u patbuf[NUMBUFLEN];
15344 char_u *save_cpo;
15345 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015346 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015347 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015348 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015349
15350 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15351 save_cpo = p_cpo;
15352 p_cpo = (char_u *)"";
15353
15354 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015355 if (argvars[1].v_type != VAR_UNKNOWN)
15356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015357 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15358 if (pat == NULL)
15359 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015360 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015361 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015362 }
15363 if (pat == NULL || *pat == NUL)
15364 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015365
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015366 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015368 if (typeerr)
15369 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370
Bram Moolenaar0d660222005-01-07 21:51:51 +000015371 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15372 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015374 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015375 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015376 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015377 if (*str == NUL)
15378 match = FALSE; /* empty item at the end */
15379 else
15380 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015381 if (match)
15382 end = regmatch.startp[0];
15383 else
15384 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015385 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15386 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015387 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015388 if (list_append_string(rettv->vval.v_list, str,
15389 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015390 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015391 }
15392 if (!match)
15393 break;
15394 /* Advance to just after the match. */
15395 if (regmatch.endp[0] > str)
15396 col = 0;
15397 else
15398 {
15399 /* Don't get stuck at the same match. */
15400#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015401 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015402#else
15403 col = 1;
15404#endif
15405 }
15406 str = regmatch.endp[0];
15407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408
Bram Moolenaar0d660222005-01-07 21:51:51 +000015409 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411
Bram Moolenaar0d660222005-01-07 21:51:51 +000015412 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413}
15414
Bram Moolenaar2c932302006-03-18 21:42:09 +000015415/*
15416 * "str2nr()" function
15417 */
15418 static void
15419f_str2nr(argvars, rettv)
15420 typval_T *argvars;
15421 typval_T *rettv;
15422{
15423 int base = 10;
15424 char_u *p;
15425 long n;
15426
15427 if (argvars[1].v_type != VAR_UNKNOWN)
15428 {
15429 base = get_tv_number(&argvars[1]);
15430 if (base != 8 && base != 10 && base != 16)
15431 {
15432 EMSG(_(e_invarg));
15433 return;
15434 }
15435 }
15436
15437 p = skipwhite(get_tv_string(&argvars[0]));
15438 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15439 rettv->vval.v_number = n;
15440}
15441
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442#ifdef HAVE_STRFTIME
15443/*
15444 * "strftime({format}[, {time}])" function
15445 */
15446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015448 typval_T *argvars;
15449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450{
15451 char_u result_buf[256];
15452 struct tm *curtime;
15453 time_t seconds;
15454 char_u *p;
15455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015456 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015459 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 seconds = time(NULL);
15461 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 curtime = localtime(&seconds);
15464 /* MSVC returns NULL for an invalid value of seconds. */
15465 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015466 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 else
15468 {
15469# ifdef FEAT_MBYTE
15470 vimconv_T conv;
15471 char_u *enc;
15472
15473 conv.vc_type = CONV_NONE;
15474 enc = enc_locale();
15475 convert_setup(&conv, p_enc, enc);
15476 if (conv.vc_type != CONV_NONE)
15477 p = string_convert(&conv, p, NULL);
15478# endif
15479 if (p != NULL)
15480 (void)strftime((char *)result_buf, sizeof(result_buf),
15481 (char *)p, curtime);
15482 else
15483 result_buf[0] = NUL;
15484
15485# ifdef FEAT_MBYTE
15486 if (conv.vc_type != CONV_NONE)
15487 vim_free(p);
15488 convert_setup(&conv, enc, p_enc);
15489 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015490 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 else
15492# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494
15495# ifdef FEAT_MBYTE
15496 /* Release conversion descriptors */
15497 convert_setup(&conv, NULL, NULL);
15498 vim_free(enc);
15499# endif
15500 }
15501}
15502#endif
15503
15504/*
15505 * "stridx()" function
15506 */
15507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015508f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015509 typval_T *argvars;
15510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511{
15512 char_u buf[NUMBUFLEN];
15513 char_u *needle;
15514 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015515 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015517 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015519 needle = get_tv_string_chk(&argvars[1]);
15520 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015521 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015522 if (needle == NULL || haystack == NULL)
15523 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524
Bram Moolenaar33570922005-01-25 22:26:29 +000015525 if (argvars[2].v_type != VAR_UNKNOWN)
15526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015527 int error = FALSE;
15528
15529 start_idx = get_tv_number_chk(&argvars[2], &error);
15530 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015531 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015532 if (start_idx >= 0)
15533 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015534 }
15535
15536 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15537 if (pos != NULL)
15538 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539}
15540
15541/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015542 * "string()" function
15543 */
15544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015546 typval_T *argvars;
15547 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015548{
15549 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015550 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015553 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015554 /* Make a copy if we have a value but it's not in allocate memory. */
15555 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557}
15558
15559/*
15560 * "strlen()" function
15561 */
15562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015563f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015564 typval_T *argvars;
15565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567 rettv->vval.v_number = (varnumber_T)(STRLEN(
15568 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569}
15570
15571/*
15572 * "strpart()" function
15573 */
15574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015575f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015576 typval_T *argvars;
15577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578{
15579 char_u *p;
15580 int n;
15581 int len;
15582 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015585 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 slen = (int)STRLEN(p);
15587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 n = get_tv_number_chk(&argvars[1], &error);
15589 if (error)
15590 len = 0;
15591 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 else
15594 len = slen - n; /* default len: all bytes that are available. */
15595
15596 /*
15597 * Only return the overlap between the specified part and the actual
15598 * string.
15599 */
15600 if (n < 0)
15601 {
15602 len += n;
15603 n = 0;
15604 }
15605 else if (n > slen)
15606 n = slen;
15607 if (len < 0)
15608 len = 0;
15609 else if (n + len > slen)
15610 len = slen - n;
15611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015612 rettv->v_type = VAR_STRING;
15613 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614}
15615
15616/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015617 * "strridx()" function
15618 */
15619 static void
15620f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015621 typval_T *argvars;
15622 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015623{
15624 char_u buf[NUMBUFLEN];
15625 char_u *needle;
15626 char_u *haystack;
15627 char_u *rest;
15628 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015629 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015631 needle = get_tv_string_chk(&argvars[1]);
15632 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015633
15634 rettv->vval.v_number = -1;
15635 if (needle == NULL || haystack == NULL)
15636 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015637
15638 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015639 if (argvars[2].v_type != VAR_UNKNOWN)
15640 {
15641 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015642 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015643 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015644 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015645 }
15646 else
15647 end_idx = haystack_len;
15648
Bram Moolenaar0d660222005-01-07 21:51:51 +000015649 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015650 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015651 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015652 lastmatch = haystack + end_idx;
15653 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015655 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015656 for (rest = haystack; *rest != '\0'; ++rest)
15657 {
15658 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015659 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015660 break;
15661 lastmatch = rest;
15662 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015663 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015664
15665 if (lastmatch == NULL)
15666 rettv->vval.v_number = -1;
15667 else
15668 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15669}
15670
15671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 * "strtrans()" function
15673 */
15674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015675f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015676 typval_T *argvars;
15677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679 rettv->v_type = VAR_STRING;
15680 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681}
15682
15683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015684 * "submatch()" function
15685 */
15686 static void
15687f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015688 typval_T *argvars;
15689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015690{
15691 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015692 rettv->vval.v_string =
15693 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015694}
15695
15696/*
15697 * "substitute()" function
15698 */
15699 static void
15700f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015701 typval_T *argvars;
15702 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015703{
15704 char_u patbuf[NUMBUFLEN];
15705 char_u subbuf[NUMBUFLEN];
15706 char_u flagsbuf[NUMBUFLEN];
15707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015708 char_u *str = get_tv_string_chk(&argvars[0]);
15709 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15710 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15711 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15712
Bram Moolenaar0d660222005-01-07 21:51:51 +000015713 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015714 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15715 rettv->vval.v_string = NULL;
15716 else
15717 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015718}
15719
15720/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015721 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 */
15723/*ARGSUSED*/
15724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015725f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015726 typval_T *argvars;
15727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728{
15729 int id = 0;
15730#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015731 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 long col;
15733 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015734 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015736 lnum = get_tv_lnum(argvars); /* -1 on type error */
15737 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15738 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015740 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015741 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015742 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743#endif
15744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015745 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746}
15747
15748/*
15749 * "synIDattr(id, what [, mode])" function
15750 */
15751/*ARGSUSED*/
15752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015753f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015754 typval_T *argvars;
15755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756{
15757 char_u *p = NULL;
15758#ifdef FEAT_SYN_HL
15759 int id;
15760 char_u *what;
15761 char_u *mode;
15762 char_u modebuf[NUMBUFLEN];
15763 int modec;
15764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015765 id = get_tv_number(&argvars[0]);
15766 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015769 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 modec = TOLOWER_ASC(mode[0]);
15771 if (modec != 't' && modec != 'c'
15772#ifdef FEAT_GUI
15773 && modec != 'g'
15774#endif
15775 )
15776 modec = 0; /* replace invalid with current */
15777 }
15778 else
15779 {
15780#ifdef FEAT_GUI
15781 if (gui.in_use)
15782 modec = 'g';
15783 else
15784#endif
15785 if (t_colors > 1)
15786 modec = 'c';
15787 else
15788 modec = 't';
15789 }
15790
15791
15792 switch (TOLOWER_ASC(what[0]))
15793 {
15794 case 'b':
15795 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15796 p = highlight_color(id, what, modec);
15797 else /* bold */
15798 p = highlight_has_attr(id, HL_BOLD, modec);
15799 break;
15800
15801 case 'f': /* fg[#] */
15802 p = highlight_color(id, what, modec);
15803 break;
15804
15805 case 'i':
15806 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15807 p = highlight_has_attr(id, HL_INVERSE, modec);
15808 else /* italic */
15809 p = highlight_has_attr(id, HL_ITALIC, modec);
15810 break;
15811
15812 case 'n': /* name */
15813 p = get_highlight_name(NULL, id - 1);
15814 break;
15815
15816 case 'r': /* reverse */
15817 p = highlight_has_attr(id, HL_INVERSE, modec);
15818 break;
15819
15820 case 's': /* standout */
15821 p = highlight_has_attr(id, HL_STANDOUT, modec);
15822 break;
15823
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015824 case 'u':
15825 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15826 /* underline */
15827 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15828 else
15829 /* undercurl */
15830 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 break;
15832 }
15833
15834 if (p != NULL)
15835 p = vim_strsave(p);
15836#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015837 rettv->v_type = VAR_STRING;
15838 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839}
15840
15841/*
15842 * "synIDtrans(id)" function
15843 */
15844/*ARGSUSED*/
15845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015846f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015847 typval_T *argvars;
15848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849{
15850 int id;
15851
15852#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015853 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854
15855 if (id > 0)
15856 id = syn_get_final_id(id);
15857 else
15858#endif
15859 id = 0;
15860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015861 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862}
15863
15864/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015865 * "synstack(lnum, col)" function
15866 */
15867/*ARGSUSED*/
15868 static void
15869f_synstack(argvars, rettv)
15870 typval_T *argvars;
15871 typval_T *rettv;
15872{
15873#ifdef FEAT_SYN_HL
15874 long lnum;
15875 long col;
15876 int i;
15877 int id;
15878#endif
15879
15880 rettv->v_type = VAR_LIST;
15881 rettv->vval.v_list = NULL;
15882
15883#ifdef FEAT_SYN_HL
15884 lnum = get_tv_lnum(argvars); /* -1 on type error */
15885 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15886
15887 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
15888 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
15889 && rettv_list_alloc(rettv) != FAIL)
15890 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015891 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015892 for (i = 0; ; ++i)
15893 {
15894 id = syn_get_stack_item(i);
15895 if (id < 0)
15896 break;
15897 if (list_append_number(rettv->vval.v_list, id) == FAIL)
15898 break;
15899 }
15900 }
15901#endif
15902}
15903
15904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 * "system()" function
15906 */
15907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015908f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015909 typval_T *argvars;
15910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015912 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015914 char_u *infile = NULL;
15915 char_u buf[NUMBUFLEN];
15916 int err = FALSE;
15917 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015919 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015920 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015921
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015922 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015923 {
15924 /*
15925 * Write the string to a temp file, to be used for input of the shell
15926 * command.
15927 */
15928 if ((infile = vim_tempname('i')) == NULL)
15929 {
15930 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015931 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015932 }
15933
15934 fd = mch_fopen((char *)infile, WRITEBIN);
15935 if (fd == NULL)
15936 {
15937 EMSG2(_(e_notopen), infile);
15938 goto done;
15939 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015940 p = get_tv_string_buf_chk(&argvars[1], buf);
15941 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015942 {
15943 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015944 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015945 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015946 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15947 err = TRUE;
15948 if (fclose(fd) != 0)
15949 err = TRUE;
15950 if (err)
15951 {
15952 EMSG(_("E677: Error writing temp file"));
15953 goto done;
15954 }
15955 }
15956
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015957 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15958 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015959
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960#ifdef USE_CR
15961 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015962 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963 {
15964 char_u *s;
15965
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015966 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 {
15968 if (*s == CAR)
15969 *s = NL;
15970 }
15971 }
15972#else
15973# ifdef USE_CRNL
15974 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015975 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976 {
15977 char_u *s, *d;
15978
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015979 d = res;
15980 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 {
15982 if (s[0] == CAR && s[1] == NL)
15983 ++s;
15984 *d++ = *s;
15985 }
15986 *d = NUL;
15987 }
15988# endif
15989#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015990
15991done:
15992 if (infile != NULL)
15993 {
15994 mch_remove(infile);
15995 vim_free(infile);
15996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015997 rettv->v_type = VAR_STRING;
15998 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999}
16000
16001/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016002 * "tabpagebuflist()" function
16003 */
16004/* ARGSUSED */
16005 static void
16006f_tabpagebuflist(argvars, rettv)
16007 typval_T *argvars;
16008 typval_T *rettv;
16009{
16010#ifndef FEAT_WINDOWS
16011 rettv->vval.v_number = 0;
16012#else
16013 tabpage_T *tp;
16014 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016015
16016 if (argvars[0].v_type == VAR_UNKNOWN)
16017 wp = firstwin;
16018 else
16019 {
16020 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16021 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016022 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016023 }
16024 if (wp == NULL)
16025 rettv->vval.v_number = 0;
16026 else
16027 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016028 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016029 rettv->vval.v_number = 0;
16030 else
16031 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016032 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016033 if (list_append_number(rettv->vval.v_list,
16034 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016035 break;
16036 }
16037 }
16038#endif
16039}
16040
16041
16042/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016043 * "tabpagenr()" function
16044 */
16045/* ARGSUSED */
16046 static void
16047f_tabpagenr(argvars, rettv)
16048 typval_T *argvars;
16049 typval_T *rettv;
16050{
16051 int nr = 1;
16052#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016053 char_u *arg;
16054
16055 if (argvars[0].v_type != VAR_UNKNOWN)
16056 {
16057 arg = get_tv_string_chk(&argvars[0]);
16058 nr = 0;
16059 if (arg != NULL)
16060 {
16061 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016062 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016063 else
16064 EMSG2(_(e_invexpr2), arg);
16065 }
16066 }
16067 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016068 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016069#endif
16070 rettv->vval.v_number = nr;
16071}
16072
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016073
16074#ifdef FEAT_WINDOWS
16075static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16076
16077/*
16078 * Common code for tabpagewinnr() and winnr().
16079 */
16080 static int
16081get_winnr(tp, argvar)
16082 tabpage_T *tp;
16083 typval_T *argvar;
16084{
16085 win_T *twin;
16086 int nr = 1;
16087 win_T *wp;
16088 char_u *arg;
16089
16090 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16091 if (argvar->v_type != VAR_UNKNOWN)
16092 {
16093 arg = get_tv_string_chk(argvar);
16094 if (arg == NULL)
16095 nr = 0; /* type error; errmsg already given */
16096 else if (STRCMP(arg, "$") == 0)
16097 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16098 else if (STRCMP(arg, "#") == 0)
16099 {
16100 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16101 if (twin == NULL)
16102 nr = 0;
16103 }
16104 else
16105 {
16106 EMSG2(_(e_invexpr2), arg);
16107 nr = 0;
16108 }
16109 }
16110
16111 if (nr > 0)
16112 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16113 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016114 {
16115 if (wp == NULL)
16116 {
16117 /* didn't find it in this tabpage */
16118 nr = 0;
16119 break;
16120 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016121 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016122 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016123 return nr;
16124}
16125#endif
16126
16127/*
16128 * "tabpagewinnr()" function
16129 */
16130/* ARGSUSED */
16131 static void
16132f_tabpagewinnr(argvars, rettv)
16133 typval_T *argvars;
16134 typval_T *rettv;
16135{
16136 int nr = 1;
16137#ifdef FEAT_WINDOWS
16138 tabpage_T *tp;
16139
16140 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16141 if (tp == NULL)
16142 nr = 0;
16143 else
16144 nr = get_winnr(tp, &argvars[1]);
16145#endif
16146 rettv->vval.v_number = nr;
16147}
16148
16149
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016150/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016151 * "tagfiles()" function
16152 */
16153/*ARGSUSED*/
16154 static void
16155f_tagfiles(argvars, rettv)
16156 typval_T *argvars;
16157 typval_T *rettv;
16158{
16159 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016160 tagname_T tn;
16161 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016162
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016163 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016164 {
16165 rettv->vval.v_number = 0;
16166 return;
16167 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016168
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016169 for (first = TRUE; ; first = FALSE)
16170 if (get_tagfname(&tn, first, fname) == FAIL
16171 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016172 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016173 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016174}
16175
16176/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016177 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016178 */
16179 static void
16180f_taglist(argvars, rettv)
16181 typval_T *argvars;
16182 typval_T *rettv;
16183{
16184 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016185
16186 tag_pattern = get_tv_string(&argvars[0]);
16187
16188 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016189 if (*tag_pattern == NUL)
16190 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016191
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016192 if (rettv_list_alloc(rettv) == OK)
16193 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016194}
16195
16196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 * "tempname()" function
16198 */
16199/*ARGSUSED*/
16200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016201f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016202 typval_T *argvars;
16203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204{
16205 static int x = 'A';
16206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016207 rettv->v_type = VAR_STRING;
16208 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209
16210 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16211 * names. Skip 'I' and 'O', they are used for shell redirection. */
16212 do
16213 {
16214 if (x == 'Z')
16215 x = '0';
16216 else if (x == '9')
16217 x = 'A';
16218 else
16219 {
16220#ifdef EBCDIC
16221 if (x == 'I')
16222 x = 'J';
16223 else if (x == 'R')
16224 x = 'S';
16225 else
16226#endif
16227 ++x;
16228 }
16229 } while (x == 'I' || x == 'O');
16230}
16231
16232/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016233 * "test(list)" function: Just checking the walls...
16234 */
16235/*ARGSUSED*/
16236 static void
16237f_test(argvars, rettv)
16238 typval_T *argvars;
16239 typval_T *rettv;
16240{
16241 /* Used for unit testing. Change the code below to your liking. */
16242#if 0
16243 listitem_T *li;
16244 list_T *l;
16245 char_u *bad, *good;
16246
16247 if (argvars[0].v_type != VAR_LIST)
16248 return;
16249 l = argvars[0].vval.v_list;
16250 if (l == NULL)
16251 return;
16252 li = l->lv_first;
16253 if (li == NULL)
16254 return;
16255 bad = get_tv_string(&li->li_tv);
16256 li = li->li_next;
16257 if (li == NULL)
16258 return;
16259 good = get_tv_string(&li->li_tv);
16260 rettv->vval.v_number = test_edit_score(bad, good);
16261#endif
16262}
16263
16264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 * "tolower(string)" function
16266 */
16267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 typval_T *argvars;
16270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271{
16272 char_u *p;
16273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016274 p = vim_strsave(get_tv_string(&argvars[0]));
16275 rettv->v_type = VAR_STRING;
16276 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277
16278 if (p != NULL)
16279 while (*p != NUL)
16280 {
16281#ifdef FEAT_MBYTE
16282 int l;
16283
16284 if (enc_utf8)
16285 {
16286 int c, lc;
16287
16288 c = utf_ptr2char(p);
16289 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016290 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291 /* TODO: reallocate string when byte count changes. */
16292 if (utf_char2len(lc) == l)
16293 utf_char2bytes(lc, p);
16294 p += l;
16295 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016296 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 p += l; /* skip multi-byte character */
16298 else
16299#endif
16300 {
16301 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16302 ++p;
16303 }
16304 }
16305}
16306
16307/*
16308 * "toupper(string)" function
16309 */
16310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016311f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016312 typval_T *argvars;
16313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016315 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016316 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317}
16318
16319/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016320 * "tr(string, fromstr, tostr)" function
16321 */
16322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016323f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016324 typval_T *argvars;
16325 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016326{
16327 char_u *instr;
16328 char_u *fromstr;
16329 char_u *tostr;
16330 char_u *p;
16331#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016332 int inlen;
16333 int fromlen;
16334 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016335 int idx;
16336 char_u *cpstr;
16337 int cplen;
16338 int first = TRUE;
16339#endif
16340 char_u buf[NUMBUFLEN];
16341 char_u buf2[NUMBUFLEN];
16342 garray_T ga;
16343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016344 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016345 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16346 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016347
16348 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016349 rettv->v_type = VAR_STRING;
16350 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016351 if (fromstr == NULL || tostr == NULL)
16352 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016353 ga_init2(&ga, (int)sizeof(char), 80);
16354
16355#ifdef FEAT_MBYTE
16356 if (!has_mbyte)
16357#endif
16358 /* not multi-byte: fromstr and tostr must be the same length */
16359 if (STRLEN(fromstr) != STRLEN(tostr))
16360 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016361#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016362error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016363#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016364 EMSG2(_(e_invarg2), fromstr);
16365 ga_clear(&ga);
16366 return;
16367 }
16368
16369 /* fromstr and tostr have to contain the same number of chars */
16370 while (*instr != NUL)
16371 {
16372#ifdef FEAT_MBYTE
16373 if (has_mbyte)
16374 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016375 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016376 cpstr = instr;
16377 cplen = inlen;
16378 idx = 0;
16379 for (p = fromstr; *p != NUL; p += fromlen)
16380 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016381 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016382 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16383 {
16384 for (p = tostr; *p != NUL; p += tolen)
16385 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016386 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016387 if (idx-- == 0)
16388 {
16389 cplen = tolen;
16390 cpstr = p;
16391 break;
16392 }
16393 }
16394 if (*p == NUL) /* tostr is shorter than fromstr */
16395 goto error;
16396 break;
16397 }
16398 ++idx;
16399 }
16400
16401 if (first && cpstr == instr)
16402 {
16403 /* Check that fromstr and tostr have the same number of
16404 * (multi-byte) characters. Done only once when a character
16405 * of instr doesn't appear in fromstr. */
16406 first = FALSE;
16407 for (p = tostr; *p != NUL; p += tolen)
16408 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016409 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016410 --idx;
16411 }
16412 if (idx != 0)
16413 goto error;
16414 }
16415
16416 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016417 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016418 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016419
16420 instr += inlen;
16421 }
16422 else
16423#endif
16424 {
16425 /* When not using multi-byte chars we can do it faster. */
16426 p = vim_strchr(fromstr, *instr);
16427 if (p != NULL)
16428 ga_append(&ga, tostr[p - fromstr]);
16429 else
16430 ga_append(&ga, *instr);
16431 ++instr;
16432 }
16433 }
16434
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016435 /* add a terminating NUL */
16436 ga_grow(&ga, 1);
16437 ga_append(&ga, NUL);
16438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016440}
16441
16442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 * "type(expr)" function
16444 */
16445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016446f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016447 typval_T *argvars;
16448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016450 int n;
16451
16452 switch (argvars[0].v_type)
16453 {
16454 case VAR_NUMBER: n = 0; break;
16455 case VAR_STRING: n = 1; break;
16456 case VAR_FUNC: n = 2; break;
16457 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016458 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016459 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16460 }
16461 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462}
16463
16464/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016465 * "values(dict)" function
16466 */
16467 static void
16468f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016469 typval_T *argvars;
16470 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016471{
16472 dict_list(argvars, rettv, 1);
16473}
16474
16475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476 * "virtcol(string)" function
16477 */
16478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016479f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016480 typval_T *argvars;
16481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482{
16483 colnr_T vcol = 0;
16484 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016485 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016487 fp = var2fpos(&argvars[0], FALSE, &fnum);
16488 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16489 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 {
16491 getvvcol(curwin, fp, NULL, NULL, &vcol);
16492 ++vcol;
16493 }
16494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016495 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496}
16497
16498/*
16499 * "visualmode()" function
16500 */
16501/*ARGSUSED*/
16502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016503f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016504 typval_T *argvars;
16505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506{
16507#ifdef FEAT_VISUAL
16508 char_u str[2];
16509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 str[0] = curbuf->b_visual_mode_eval;
16512 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016513 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514
16515 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016516 if ((argvars[0].v_type == VAR_NUMBER
16517 && argvars[0].vval.v_number != 0)
16518 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016519 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 curbuf->b_visual_mode_eval = NUL;
16521#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016522 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523#endif
16524}
16525
16526/*
16527 * "winbufnr(nr)" function
16528 */
16529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016530f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016531 typval_T *argvars;
16532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533{
16534 win_T *wp;
16535
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016536 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016538 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016540 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541}
16542
16543/*
16544 * "wincol()" function
16545 */
16546/*ARGSUSED*/
16547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016548f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016549 typval_T *argvars;
16550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551{
16552 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016553 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554}
16555
16556/*
16557 * "winheight(nr)" function
16558 */
16559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016560f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016561 typval_T *argvars;
16562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563{
16564 win_T *wp;
16565
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016566 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016568 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016570 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571}
16572
16573/*
16574 * "winline()" function
16575 */
16576/*ARGSUSED*/
16577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016578f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016579 typval_T *argvars;
16580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581{
16582 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016583 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584}
16585
16586/*
16587 * "winnr()" function
16588 */
16589/* ARGSUSED */
16590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016591f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016592 typval_T *argvars;
16593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594{
16595 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016596
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016598 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016600 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601}
16602
16603/*
16604 * "winrestcmd()" function
16605 */
16606/* ARGSUSED */
16607 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016608f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016609 typval_T *argvars;
16610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611{
16612#ifdef FEAT_WINDOWS
16613 win_T *wp;
16614 int winnr = 1;
16615 garray_T ga;
16616 char_u buf[50];
16617
16618 ga_init2(&ga, (int)sizeof(char), 70);
16619 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16620 {
16621 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16622 ga_concat(&ga, buf);
16623# ifdef FEAT_VERTSPLIT
16624 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16625 ga_concat(&ga, buf);
16626# endif
16627 ++winnr;
16628 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016629 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016631 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016635 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636}
16637
16638/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016639 * "winrestview()" function
16640 */
16641/* ARGSUSED */
16642 static void
16643f_winrestview(argvars, rettv)
16644 typval_T *argvars;
16645 typval_T *rettv;
16646{
16647 dict_T *dict;
16648
16649 if (argvars[0].v_type != VAR_DICT
16650 || (dict = argvars[0].vval.v_dict) == NULL)
16651 EMSG(_(e_invarg));
16652 else
16653 {
16654 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16655 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16656#ifdef FEAT_VIRTUALEDIT
16657 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16658#endif
16659 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016660 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016661
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016662 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016663#ifdef FEAT_DIFF
16664 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16665#endif
16666 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16667 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16668
16669 check_cursor();
16670 changed_cline_bef_curs();
16671 invalidate_botline();
16672 redraw_later(VALID);
16673
16674 if (curwin->w_topline == 0)
16675 curwin->w_topline = 1;
16676 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16677 curwin->w_topline = curbuf->b_ml.ml_line_count;
16678#ifdef FEAT_DIFF
16679 check_topfill(curwin, TRUE);
16680#endif
16681 }
16682}
16683
16684/*
16685 * "winsaveview()" function
16686 */
16687/* ARGSUSED */
16688 static void
16689f_winsaveview(argvars, rettv)
16690 typval_T *argvars;
16691 typval_T *rettv;
16692{
16693 dict_T *dict;
16694
16695 dict = dict_alloc();
16696 if (dict == NULL)
16697 return;
16698 rettv->v_type = VAR_DICT;
16699 rettv->vval.v_dict = dict;
16700 ++dict->dv_refcount;
16701
16702 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16703 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16704#ifdef FEAT_VIRTUALEDIT
16705 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16706#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016707 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016708 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16709
16710 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16711#ifdef FEAT_DIFF
16712 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16713#endif
16714 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16715 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16716}
16717
16718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 * "winwidth(nr)" function
16720 */
16721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016722f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016723 typval_T *argvars;
16724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725{
16726 win_T *wp;
16727
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016728 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016730 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 else
16732#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016733 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016735 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736#endif
16737}
16738
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016740 * "writefile()" function
16741 */
16742 static void
16743f_writefile(argvars, rettv)
16744 typval_T *argvars;
16745 typval_T *rettv;
16746{
16747 int binary = FALSE;
16748 char_u *fname;
16749 FILE *fd;
16750 listitem_T *li;
16751 char_u *s;
16752 int ret = 0;
16753 int c;
16754
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016755 if (check_restricted() || check_secure())
16756 return;
16757
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016758 if (argvars[0].v_type != VAR_LIST)
16759 {
16760 EMSG2(_(e_listarg), "writefile()");
16761 return;
16762 }
16763 if (argvars[0].vval.v_list == NULL)
16764 return;
16765
16766 if (argvars[2].v_type != VAR_UNKNOWN
16767 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16768 binary = TRUE;
16769
16770 /* Always open the file in binary mode, library functions have a mind of
16771 * their own about CR-LF conversion. */
16772 fname = get_tv_string(&argvars[1]);
16773 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16774 {
16775 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16776 ret = -1;
16777 }
16778 else
16779 {
16780 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16781 li = li->li_next)
16782 {
16783 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16784 {
16785 if (*s == '\n')
16786 c = putc(NUL, fd);
16787 else
16788 c = putc(*s, fd);
16789 if (c == EOF)
16790 {
16791 ret = -1;
16792 break;
16793 }
16794 }
16795 if (!binary || li->li_next != NULL)
16796 if (putc('\n', fd) == EOF)
16797 {
16798 ret = -1;
16799 break;
16800 }
16801 if (ret < 0)
16802 {
16803 EMSG(_(e_write));
16804 break;
16805 }
16806 }
16807 fclose(fd);
16808 }
16809
16810 rettv->vval.v_number = ret;
16811}
16812
16813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016815 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 */
16817 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016818var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016819 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016820 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016821 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016823 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016825 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826
Bram Moolenaara5525202006-03-02 22:52:09 +000016827 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016828 if (varp->v_type == VAR_LIST)
16829 {
16830 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016831 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016832 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016833 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016834
16835 l = varp->vval.v_list;
16836 if (l == NULL)
16837 return NULL;
16838
16839 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016840 pos.lnum = list_find_nr(l, 0L, &error);
16841 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016842 return NULL; /* invalid line number */
16843
16844 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016845 pos.col = list_find_nr(l, 1L, &error);
16846 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016847 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016848 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016849
16850 /* We accept "$" for the column number: last column. */
16851 li = list_find(l, 1L);
16852 if (li != NULL && li->li_tv.v_type == VAR_STRING
16853 && li->li_tv.vval.v_string != NULL
16854 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16855 pos.col = len + 1;
16856
Bram Moolenaara5525202006-03-02 22:52:09 +000016857 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016858 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016859 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016860 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016861
Bram Moolenaara5525202006-03-02 22:52:09 +000016862#ifdef FEAT_VIRTUALEDIT
16863 /* Get the virtual offset. Defaults to zero. */
16864 pos.coladd = list_find_nr(l, 2L, &error);
16865 if (error)
16866 pos.coladd = 0;
16867#endif
16868
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016869 return &pos;
16870 }
16871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016872 name = get_tv_string_chk(varp);
16873 if (name == NULL)
16874 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 if (name[0] == '.') /* cursor */
16876 return &curwin->w_cursor;
16877 if (name[0] == '\'') /* mark */
16878 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016879 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16881 return NULL;
16882 return pp;
16883 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016884
16885#ifdef FEAT_VIRTUALEDIT
16886 pos.coladd = 0;
16887#endif
16888
Bram Moolenaar477933c2007-07-17 14:32:23 +000016889 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016890 {
16891 pos.col = 0;
16892 if (name[1] == '0') /* "w0": first visible line */
16893 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016894 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016895 pos.lnum = curwin->w_topline;
16896 return &pos;
16897 }
16898 else if (name[1] == '$') /* "w$": last visible line */
16899 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016900 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016901 pos.lnum = curwin->w_botline - 1;
16902 return &pos;
16903 }
16904 }
16905 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016907 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908 {
16909 pos.lnum = curbuf->b_ml.ml_line_count;
16910 pos.col = 0;
16911 }
16912 else
16913 {
16914 pos.lnum = curwin->w_cursor.lnum;
16915 pos.col = (colnr_T)STRLEN(ml_get_curline());
16916 }
16917 return &pos;
16918 }
16919 return NULL;
16920}
16921
16922/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016923 * Convert list in "arg" into a position and optional file number.
16924 * When "fnump" is NULL there is no file number, only 3 items.
16925 * Note that the column is passed on as-is, the caller may want to decrement
16926 * it to use 1 for the first column.
16927 * Return FAIL when conversion is not possible, doesn't check the position for
16928 * validity.
16929 */
16930 static int
16931list2fpos(arg, posp, fnump)
16932 typval_T *arg;
16933 pos_T *posp;
16934 int *fnump;
16935{
16936 list_T *l = arg->vval.v_list;
16937 long i = 0;
16938 long n;
16939
Bram Moolenaarbde35262006-07-23 20:12:24 +000016940 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16941 * when "fnump" isn't NULL and "coladd" is optional. */
16942 if (arg->v_type != VAR_LIST
16943 || l == NULL
16944 || l->lv_len < (fnump == NULL ? 2 : 3)
16945 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016946 return FAIL;
16947
16948 if (fnump != NULL)
16949 {
16950 n = list_find_nr(l, i++, NULL); /* fnum */
16951 if (n < 0)
16952 return FAIL;
16953 if (n == 0)
16954 n = curbuf->b_fnum; /* current buffer */
16955 *fnump = n;
16956 }
16957
16958 n = list_find_nr(l, i++, NULL); /* lnum */
16959 if (n < 0)
16960 return FAIL;
16961 posp->lnum = n;
16962
16963 n = list_find_nr(l, i++, NULL); /* col */
16964 if (n < 0)
16965 return FAIL;
16966 posp->col = n;
16967
16968#ifdef FEAT_VIRTUALEDIT
16969 n = list_find_nr(l, i, NULL);
16970 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016971 posp->coladd = 0;
16972 else
16973 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016974#endif
16975
16976 return OK;
16977}
16978
16979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 * Get the length of an environment variable name.
16981 * Advance "arg" to the first character after the name.
16982 * Return 0 for error.
16983 */
16984 static int
16985get_env_len(arg)
16986 char_u **arg;
16987{
16988 char_u *p;
16989 int len;
16990
16991 for (p = *arg; vim_isIDc(*p); ++p)
16992 ;
16993 if (p == *arg) /* no name found */
16994 return 0;
16995
16996 len = (int)(p - *arg);
16997 *arg = p;
16998 return len;
16999}
17000
17001/*
17002 * Get the length of the name of a function or internal variable.
17003 * "arg" is advanced to the first non-white character after the name.
17004 * Return 0 if something is wrong.
17005 */
17006 static int
17007get_id_len(arg)
17008 char_u **arg;
17009{
17010 char_u *p;
17011 int len;
17012
17013 /* Find the end of the name. */
17014 for (p = *arg; eval_isnamec(*p); ++p)
17015 ;
17016 if (p == *arg) /* no name found */
17017 return 0;
17018
17019 len = (int)(p - *arg);
17020 *arg = skipwhite(p);
17021
17022 return len;
17023}
17024
17025/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017026 * Get the length of the name of a variable or function.
17027 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017029 * Return -1 if curly braces expansion failed.
17030 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 * If the name contains 'magic' {}'s, expand them and return the
17032 * expanded name in an allocated string via 'alias' - caller must free.
17033 */
17034 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017035get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 char_u **arg;
17037 char_u **alias;
17038 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017039 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040{
17041 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 char_u *p;
17043 char_u *expr_start;
17044 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045
17046 *alias = NULL; /* default to no alias */
17047
17048 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17049 && (*arg)[2] == (int)KE_SNR)
17050 {
17051 /* hard coded <SNR>, already translated */
17052 *arg += 3;
17053 return get_id_len(arg) + 3;
17054 }
17055 len = eval_fname_script(*arg);
17056 if (len > 0)
17057 {
17058 /* literal "<SID>", "s:" or "<SNR>" */
17059 *arg += len;
17060 }
17061
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017063 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017065 p = find_name_end(*arg, &expr_start, &expr_end,
17066 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 if (expr_start != NULL)
17068 {
17069 char_u *temp_string;
17070
17071 if (!evaluate)
17072 {
17073 len += (int)(p - *arg);
17074 *arg = skipwhite(p);
17075 return len;
17076 }
17077
17078 /*
17079 * Include any <SID> etc in the expanded string:
17080 * Thus the -len here.
17081 */
17082 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17083 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017084 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 *alias = temp_string;
17086 *arg = skipwhite(p);
17087 return (int)STRLEN(temp_string);
17088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089
17090 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017091 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 EMSG2(_(e_invexpr2), *arg);
17093
17094 return len;
17095}
17096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017097/*
17098 * Find the end of a variable or function name, taking care of magic braces.
17099 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17100 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017101 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017102 * Return a pointer to just after the name. Equal to "arg" if there is no
17103 * valid name.
17104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017106find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 char_u *arg;
17108 char_u **expr_start;
17109 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017110 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017112 int mb_nest = 0;
17113 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 char_u *p;
17115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017118 *expr_start = NULL;
17119 *expr_end = NULL;
17120 }
17121
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017122 /* Quick check for valid starting character. */
17123 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17124 return arg;
17125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017126 for (p = arg; *p != NUL
17127 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017128 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017129 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017130 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017131 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017132 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017133 if (*p == '\'')
17134 {
17135 /* skip over 'string' to avoid counting [ and ] inside it. */
17136 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17137 ;
17138 if (*p == NUL)
17139 break;
17140 }
17141 else if (*p == '"')
17142 {
17143 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17144 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17145 if (*p == '\\' && p[1] != NUL)
17146 ++p;
17147 if (*p == NUL)
17148 break;
17149 }
17150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017151 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017153 if (*p == '[')
17154 ++br_nest;
17155 else if (*p == ']')
17156 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017159 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017161 if (*p == '{')
17162 {
17163 mb_nest++;
17164 if (expr_start != NULL && *expr_start == NULL)
17165 *expr_start = p;
17166 }
17167 else if (*p == '}')
17168 {
17169 mb_nest--;
17170 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17171 *expr_end = p;
17172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 }
17175
17176 return p;
17177}
17178
17179/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017180 * Expands out the 'magic' {}'s in a variable/function name.
17181 * Note that this can call itself recursively, to deal with
17182 * constructs like foo{bar}{baz}{bam}
17183 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17184 * "in_start" ^
17185 * "expr_start" ^
17186 * "expr_end" ^
17187 * "in_end" ^
17188 *
17189 * Returns a new allocated string, which the caller must free.
17190 * Returns NULL for failure.
17191 */
17192 static char_u *
17193make_expanded_name(in_start, expr_start, expr_end, in_end)
17194 char_u *in_start;
17195 char_u *expr_start;
17196 char_u *expr_end;
17197 char_u *in_end;
17198{
17199 char_u c1;
17200 char_u *retval = NULL;
17201 char_u *temp_result;
17202 char_u *nextcmd = NULL;
17203
17204 if (expr_end == NULL || in_end == NULL)
17205 return NULL;
17206 *expr_start = NUL;
17207 *expr_end = NUL;
17208 c1 = *in_end;
17209 *in_end = NUL;
17210
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017211 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017212 if (temp_result != NULL && nextcmd == NULL)
17213 {
17214 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17215 + (in_end - expr_end) + 1));
17216 if (retval != NULL)
17217 {
17218 STRCPY(retval, in_start);
17219 STRCAT(retval, temp_result);
17220 STRCAT(retval, expr_end + 1);
17221 }
17222 }
17223 vim_free(temp_result);
17224
17225 *in_end = c1; /* put char back for error messages */
17226 *expr_start = '{';
17227 *expr_end = '}';
17228
17229 if (retval != NULL)
17230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017231 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017232 if (expr_start != NULL)
17233 {
17234 /* Further expansion! */
17235 temp_result = make_expanded_name(retval, expr_start,
17236 expr_end, temp_result);
17237 vim_free(retval);
17238 retval = temp_result;
17239 }
17240 }
17241
17242 return retval;
17243}
17244
17245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017247 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 */
17249 static int
17250eval_isnamec(c)
17251 int c;
17252{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017253 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17254}
17255
17256/*
17257 * Return TRUE if character "c" can be used as the first character in a
17258 * variable or function name (excluding '{' and '}').
17259 */
17260 static int
17261eval_isnamec1(c)
17262 int c;
17263{
17264 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265}
17266
17267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 * Set number v: variable to "val".
17269 */
17270 void
17271set_vim_var_nr(idx, val)
17272 int idx;
17273 long val;
17274{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017275 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276}
17277
17278/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017279 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 */
17281 long
17282get_vim_var_nr(idx)
17283 int idx;
17284{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017285 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286}
17287
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017288#if defined(FEAT_AUTOCMD) || defined(PROTO)
17289/*
17290 * Get string v: variable value. Uses a static buffer, can only be used once.
17291 */
17292 char_u *
17293get_vim_var_str(idx)
17294 int idx;
17295{
17296 return get_tv_string(&vimvars[idx].vv_tv);
17297}
17298#endif
17299
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300/*
17301 * Set v:count, v:count1 and v:prevcount.
17302 */
17303 void
17304set_vcount(count, count1)
17305 long count;
17306 long count1;
17307{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017308 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17309 vimvars[VV_COUNT].vv_nr = count;
17310 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311}
17312
17313/*
17314 * Set string v: variable to a copy of "val".
17315 */
17316 void
17317set_vim_var_string(idx, val, len)
17318 int idx;
17319 char_u *val;
17320 int len; /* length of "val" to use or -1 (whole string) */
17321{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017322 /* Need to do this (at least) once, since we can't initialize a union.
17323 * Will always be invoked when "v:progname" is set. */
17324 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17325
Bram Moolenaare9a41262005-01-15 22:18:47 +000017326 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017328 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017330 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017332 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333}
17334
17335/*
17336 * Set v:register if needed.
17337 */
17338 void
17339set_reg_var(c)
17340 int c;
17341{
17342 char_u regname;
17343
17344 if (c == 0 || c == ' ')
17345 regname = '"';
17346 else
17347 regname = c;
17348 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017349 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 set_vim_var_string(VV_REG, &regname, 1);
17351}
17352
17353/*
17354 * Get or set v:exception. If "oldval" == NULL, return the current value.
17355 * Otherwise, restore the value to "oldval" and return NULL.
17356 * Must always be called in pairs to save and restore v:exception! Does not
17357 * take care of memory allocations.
17358 */
17359 char_u *
17360v_exception(oldval)
17361 char_u *oldval;
17362{
17363 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017364 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365
Bram Moolenaare9a41262005-01-15 22:18:47 +000017366 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 return NULL;
17368}
17369
17370/*
17371 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17372 * Otherwise, restore the value to "oldval" and return NULL.
17373 * Must always be called in pairs to save and restore v:throwpoint! Does not
17374 * take care of memory allocations.
17375 */
17376 char_u *
17377v_throwpoint(oldval)
17378 char_u *oldval;
17379{
17380 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017381 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382
Bram Moolenaare9a41262005-01-15 22:18:47 +000017383 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 return NULL;
17385}
17386
17387#if defined(FEAT_AUTOCMD) || defined(PROTO)
17388/*
17389 * Set v:cmdarg.
17390 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17391 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17392 * Must always be called in pairs!
17393 */
17394 char_u *
17395set_cmdarg(eap, oldarg)
17396 exarg_T *eap;
17397 char_u *oldarg;
17398{
17399 char_u *oldval;
17400 char_u *newval;
17401 unsigned len;
17402
Bram Moolenaare9a41262005-01-15 22:18:47 +000017403 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017404 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017406 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017407 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017408 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 }
17410
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017411 if (eap->force_bin == FORCE_BIN)
17412 len = 6;
17413 else if (eap->force_bin == FORCE_NOBIN)
17414 len = 8;
17415 else
17416 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017417
17418 if (eap->read_edit)
17419 len += 7;
17420
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017421 if (eap->force_ff != 0)
17422 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17423# ifdef FEAT_MBYTE
17424 if (eap->force_enc != 0)
17425 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017426 if (eap->bad_char != 0)
17427 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017428# endif
17429
17430 newval = alloc(len + 1);
17431 if (newval == NULL)
17432 return NULL;
17433
17434 if (eap->force_bin == FORCE_BIN)
17435 sprintf((char *)newval, " ++bin");
17436 else if (eap->force_bin == FORCE_NOBIN)
17437 sprintf((char *)newval, " ++nobin");
17438 else
17439 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017440
17441 if (eap->read_edit)
17442 STRCAT(newval, " ++edit");
17443
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017444 if (eap->force_ff != 0)
17445 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17446 eap->cmd + eap->force_ff);
17447# ifdef FEAT_MBYTE
17448 if (eap->force_enc != 0)
17449 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17450 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017451 if (eap->bad_char != 0)
17452 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17453 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017454# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017455 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017456 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457}
17458#endif
17459
17460/*
17461 * Get the value of internal variable "name".
17462 * Return OK or FAIL.
17463 */
17464 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017465get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466 char_u *name;
17467 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017468 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017469 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470{
17471 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017472 typval_T *tv = NULL;
17473 typval_T atv;
17474 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476
17477 /* truncate the name, so that we can use strcmp() */
17478 cc = name[len];
17479 name[len] = NUL;
17480
17481 /*
17482 * Check for "b:changedtick".
17483 */
17484 if (STRCMP(name, "b:changedtick") == 0)
17485 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017486 atv.v_type = VAR_NUMBER;
17487 atv.vval.v_number = curbuf->b_changedtick;
17488 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 }
17490
17491 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 * Check for user-defined variables.
17493 */
17494 else
17495 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017496 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 }
17500
Bram Moolenaare9a41262005-01-15 22:18:47 +000017501 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017503 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017504 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 ret = FAIL;
17506 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017508 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509
17510 name[len] = cc;
17511
17512 return ret;
17513}
17514
17515/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017516 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17517 * Also handle function call with Funcref variable: func(expr)
17518 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17519 */
17520 static int
17521handle_subscript(arg, rettv, evaluate, verbose)
17522 char_u **arg;
17523 typval_T *rettv;
17524 int evaluate; /* do more than finding the end */
17525 int verbose; /* give error messages */
17526{
17527 int ret = OK;
17528 dict_T *selfdict = NULL;
17529 char_u *s;
17530 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017531 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017532
17533 while (ret == OK
17534 && (**arg == '['
17535 || (**arg == '.' && rettv->v_type == VAR_DICT)
17536 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17537 && !vim_iswhite(*(*arg - 1)))
17538 {
17539 if (**arg == '(')
17540 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017541 /* need to copy the funcref so that we can clear rettv */
17542 functv = *rettv;
17543 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017544
17545 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017546 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017547 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017548 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17549 &len, evaluate, selfdict);
17550
17551 /* Clear the funcref afterwards, so that deleting it while
17552 * evaluating the arguments is possible (see test55). */
17553 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017554
17555 /* Stop the expression evaluation when immediately aborting on
17556 * error, or when an interrupt occurred or an exception was thrown
17557 * but not caught. */
17558 if (aborting())
17559 {
17560 if (ret == OK)
17561 clear_tv(rettv);
17562 ret = FAIL;
17563 }
17564 dict_unref(selfdict);
17565 selfdict = NULL;
17566 }
17567 else /* **arg == '[' || **arg == '.' */
17568 {
17569 dict_unref(selfdict);
17570 if (rettv->v_type == VAR_DICT)
17571 {
17572 selfdict = rettv->vval.v_dict;
17573 if (selfdict != NULL)
17574 ++selfdict->dv_refcount;
17575 }
17576 else
17577 selfdict = NULL;
17578 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17579 {
17580 clear_tv(rettv);
17581 ret = FAIL;
17582 }
17583 }
17584 }
17585 dict_unref(selfdict);
17586 return ret;
17587}
17588
17589/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017590 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17591 * value).
17592 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017593 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017594alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017595{
Bram Moolenaar33570922005-01-25 22:26:29 +000017596 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017597}
17598
17599/*
17600 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 * The string "s" must have been allocated, it is consumed.
17602 * Return NULL for out of memory, the variable otherwise.
17603 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017604 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017605alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 char_u *s;
17607{
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017610 rettv = alloc_tv();
17611 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017613 rettv->v_type = VAR_STRING;
17614 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 }
17616 else
17617 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017618 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619}
17620
17621/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017622 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017624 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017625free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017626 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627{
17628 if (varp != NULL)
17629 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017630 switch (varp->v_type)
17631 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017632 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017633 func_unref(varp->vval.v_string);
17634 /*FALLTHROUGH*/
17635 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017636 vim_free(varp->vval.v_string);
17637 break;
17638 case VAR_LIST:
17639 list_unref(varp->vval.v_list);
17640 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017641 case VAR_DICT:
17642 dict_unref(varp->vval.v_dict);
17643 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017644 case VAR_NUMBER:
17645 case VAR_UNKNOWN:
17646 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017647 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017648 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017649 break;
17650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 vim_free(varp);
17652 }
17653}
17654
17655/*
17656 * Free the memory for a variable value and set the value to NULL or 0.
17657 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017658 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017659clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017660 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661{
17662 if (varp != NULL)
17663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017664 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017666 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017667 func_unref(varp->vval.v_string);
17668 /*FALLTHROUGH*/
17669 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017670 vim_free(varp->vval.v_string);
17671 varp->vval.v_string = NULL;
17672 break;
17673 case VAR_LIST:
17674 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017675 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017676 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017677 case VAR_DICT:
17678 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017679 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017680 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017681 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017682 varp->vval.v_number = 0;
17683 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017684 case VAR_UNKNOWN:
17685 break;
17686 default:
17687 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017689 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 }
17691}
17692
17693/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017694 * Set the value of a variable to NULL without freeing items.
17695 */
17696 static void
17697init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017698 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017699{
17700 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017701 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017702}
17703
17704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 * Get the number value of a variable.
17706 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017707 * For incompatible types, return 0.
17708 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17709 * caller of incompatible types: it sets *denote to TRUE if "denote"
17710 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 */
17712 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017714 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017716 int error = FALSE;
17717
17718 return get_tv_number_chk(varp, &error); /* return 0L on error */
17719}
17720
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017721 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017722get_tv_number_chk(varp, denote)
17723 typval_T *varp;
17724 int *denote;
17725{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017726 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017728 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017730 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017731 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017732 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017733 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017734 break;
17735 case VAR_STRING:
17736 if (varp->vval.v_string != NULL)
17737 vim_str2nr(varp->vval.v_string, NULL, NULL,
17738 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017739 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017740 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017741 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017742 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017743 case VAR_DICT:
17744 EMSG(_("E728: Using a Dictionary as a number"));
17745 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017746 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017747 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017748 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017750 if (denote == NULL) /* useful for values that must be unsigned */
17751 n = -1;
17752 else
17753 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017754 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755}
17756
17757/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017758 * Get the lnum from the first argument.
17759 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017760 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 */
17762 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017763get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017764 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765{
Bram Moolenaar33570922005-01-25 22:26:29 +000017766 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 linenr_T lnum;
17768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017769 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 if (lnum == 0) /* no valid number, try using line() */
17771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017772 rettv.v_type = VAR_NUMBER;
17773 f_line(argvars, &rettv);
17774 lnum = rettv.vval.v_number;
17775 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 }
17777 return lnum;
17778}
17779
17780/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017781 * Get the lnum from the first argument.
17782 * Also accepts "$", then "buf" is used.
17783 * Returns 0 on error.
17784 */
17785 static linenr_T
17786get_tv_lnum_buf(argvars, buf)
17787 typval_T *argvars;
17788 buf_T *buf;
17789{
17790 if (argvars[0].v_type == VAR_STRING
17791 && argvars[0].vval.v_string != NULL
17792 && argvars[0].vval.v_string[0] == '$'
17793 && buf != NULL)
17794 return buf->b_ml.ml_line_count;
17795 return get_tv_number_chk(&argvars[0], NULL);
17796}
17797
17798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 * Get the string value of a variable.
17800 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017801 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17802 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 * If the String variable has never been set, return an empty string.
17804 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017805 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17806 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 */
17808 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017809get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017810 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811{
17812 static char_u mybuf[NUMBUFLEN];
17813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017814 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815}
17816
17817 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017818get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017819 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017820 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017822 char_u *res = get_tv_string_buf_chk(varp, buf);
17823
17824 return res != NULL ? res : (char_u *)"";
17825}
17826
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017827 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017828get_tv_string_chk(varp)
17829 typval_T *varp;
17830{
17831 static char_u mybuf[NUMBUFLEN];
17832
17833 return get_tv_string_buf_chk(varp, mybuf);
17834}
17835
17836 static char_u *
17837get_tv_string_buf_chk(varp, buf)
17838 typval_T *varp;
17839 char_u *buf;
17840{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017841 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017843 case VAR_NUMBER:
17844 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17845 return buf;
17846 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017847 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017848 break;
17849 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017850 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017851 break;
17852 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017853 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017854 break;
17855 case VAR_STRING:
17856 if (varp->vval.v_string != NULL)
17857 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017858 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017859 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017860 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017861 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017863 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864}
17865
17866/*
17867 * Find variable "name" in the list of variables.
17868 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017869 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017870 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017871 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017873 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017874find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017876 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017879 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880
Bram Moolenaara7043832005-01-21 11:56:39 +000017881 ht = find_var_ht(name, &varname);
17882 if (htp != NULL)
17883 *htp = ht;
17884 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017886 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887}
17888
17889/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017890 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017891 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017894find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017895 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017896 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017897 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017898{
Bram Moolenaar33570922005-01-25 22:26:29 +000017899 hashitem_T *hi;
17900
17901 if (*varname == NUL)
17902 {
17903 /* Must be something like "s:", otherwise "ht" would be NULL. */
17904 switch (varname[-2])
17905 {
17906 case 's': return &SCRIPT_SV(current_SID).sv_var;
17907 case 'g': return &globvars_var;
17908 case 'v': return &vimvars_var;
17909 case 'b': return &curbuf->b_bufvar;
17910 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017911#ifdef FEAT_WINDOWS
17912 case 't': return &curtab->tp_winvar;
17913#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017914 case 'l': return current_funccal == NULL
17915 ? NULL : &current_funccal->l_vars_var;
17916 case 'a': return current_funccal == NULL
17917 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017918 }
17919 return NULL;
17920 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017921
17922 hi = hash_find(ht, varname);
17923 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017924 {
17925 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017926 * worked find the variable again. Don't auto-load a script if it was
17927 * loaded already, otherwise it would be loaded every time when
17928 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017929 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017930 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017931 hi = hash_find(ht, varname);
17932 if (HASHITEM_EMPTY(hi))
17933 return NULL;
17934 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017935 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017936}
17937
17938/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017939 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017940 * Set "varname" to the start of name without ':'.
17941 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017942 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017943find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 char_u *name;
17945 char_u **varname;
17946{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017947 hashitem_T *hi;
17948
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 if (name[1] != ':')
17950 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017951 /* The name must not start with a colon or #. */
17952 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 return NULL;
17954 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017955
17956 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017957 hi = hash_find(&compat_hashtab, name);
17958 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017959 return &compat_hashtab;
17960
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017962 return &globvarht; /* global variable */
17963 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 }
17965 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017966 if (*name == 'g') /* global variable */
17967 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017968 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17969 */
17970 if (vim_strchr(name + 2, ':') != NULL
17971 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017972 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017974 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017976 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017977#ifdef FEAT_WINDOWS
17978 if (*name == 't') /* tab page variable */
17979 return &curtab->tp_vars.dv_hashtab;
17980#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017981 if (*name == 'v') /* v: variable */
17982 return &vimvarht;
17983 if (*name == 'a' && current_funccal != NULL) /* function argument */
17984 return &current_funccal->l_avars.dv_hashtab;
17985 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17986 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987 if (*name == 's' /* script variable */
17988 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17989 return &SCRIPT_VARS(current_SID);
17990 return NULL;
17991}
17992
17993/*
17994 * Get the string value of a (global/local) variable.
17995 * Returns NULL when it doesn't exist.
17996 */
17997 char_u *
17998get_var_value(name)
17999 char_u *name;
18000{
Bram Moolenaar33570922005-01-25 22:26:29 +000018001 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002
Bram Moolenaara7043832005-01-21 11:56:39 +000018003 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 if (v == NULL)
18005 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018006 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007}
18008
18009/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018010 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 * sourcing this script and when executing functions defined in the script.
18012 */
18013 void
18014new_script_vars(id)
18015 scid_T id;
18016{
Bram Moolenaara7043832005-01-21 11:56:39 +000018017 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 hashtab_T *ht;
18019 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018020
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18022 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018023 /* Re-allocating ga_data means that an ht_array pointing to
18024 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018025 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018026 for (i = 1; i <= ga_scripts.ga_len; ++i)
18027 {
18028 ht = &SCRIPT_VARS(i);
18029 if (ht->ht_mask == HT_INIT_SIZE - 1)
18030 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018031 sv = &SCRIPT_SV(i);
18032 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018033 }
18034
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 while (ga_scripts.ga_len < id)
18036 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018037 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18038 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 }
18041 }
18042}
18043
18044/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18046 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 */
18048 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018049init_var_dict(dict, dict_var)
18050 dict_T *dict;
18051 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052{
Bram Moolenaar33570922005-01-25 22:26:29 +000018053 hash_init(&dict->dv_hashtab);
18054 dict->dv_refcount = 99999;
18055 dict_var->di_tv.vval.v_dict = dict;
18056 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018057 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018058 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18059 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060}
18061
18062/*
18063 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018064 * Frees all allocated variables and the value they contain.
18065 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 */
18067 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018068vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018069 hashtab_T *ht;
18070{
18071 vars_clear_ext(ht, TRUE);
18072}
18073
18074/*
18075 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18076 */
18077 static void
18078vars_clear_ext(ht, free_val)
18079 hashtab_T *ht;
18080 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081{
Bram Moolenaara7043832005-01-21 11:56:39 +000018082 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018083 hashitem_T *hi;
18084 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085
Bram Moolenaar33570922005-01-25 22:26:29 +000018086 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018087 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018088 for (hi = ht->ht_array; todo > 0; ++hi)
18089 {
18090 if (!HASHITEM_EMPTY(hi))
18091 {
18092 --todo;
18093
Bram Moolenaar33570922005-01-25 22:26:29 +000018094 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018095 * ht_array might change then. hash_clear() takes care of it
18096 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018097 v = HI2DI(hi);
18098 if (free_val)
18099 clear_tv(&v->di_tv);
18100 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18101 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018102 }
18103 }
18104 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018105 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106}
18107
Bram Moolenaara7043832005-01-21 11:56:39 +000018108/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018109 * Delete a variable from hashtab "ht" at item "hi".
18110 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018113delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018114 hashtab_T *ht;
18115 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116{
Bram Moolenaar33570922005-01-25 22:26:29 +000018117 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018118
18119 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018120 clear_tv(&di->di_tv);
18121 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122}
18123
18124/*
18125 * List the value of one internal variable.
18126 */
18127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018128list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018129 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018131 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018133 char_u *tofree;
18134 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018135 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018136
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018137 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018138 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018139 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018140 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141}
18142
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018144list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 char_u *prefix;
18146 char_u *name;
18147 int type;
18148 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018149 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150{
Bram Moolenaar31859182007-08-14 20:41:13 +000018151 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18152 msg_start();
18153 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 if (name != NULL) /* "a:" vars don't have a name stored */
18155 msg_puts(name);
18156 msg_putchar(' ');
18157 msg_advance(22);
18158 if (type == VAR_NUMBER)
18159 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018160 else if (type == VAR_FUNC)
18161 msg_putchar('*');
18162 else if (type == VAR_LIST)
18163 {
18164 msg_putchar('[');
18165 if (*string == '[')
18166 ++string;
18167 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018168 else if (type == VAR_DICT)
18169 {
18170 msg_putchar('{');
18171 if (*string == '{')
18172 ++string;
18173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 else
18175 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018176
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018178
18179 if (type == VAR_FUNC)
18180 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018181 if (*first)
18182 {
18183 msg_clr_eos();
18184 *first = FALSE;
18185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186}
18187
18188/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018189 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 * If the variable already exists, the value is updated.
18191 * Otherwise the variable is created.
18192 */
18193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018194set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018196 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018197 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198{
Bram Moolenaar33570922005-01-25 22:26:29 +000018199 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018201 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018202 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018204 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018205 {
18206 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18207 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18208 ? name[2] : name[0]))
18209 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018210 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018211 return;
18212 }
18213 if (function_exists(name))
18214 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018215 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018216 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018217 return;
18218 }
18219 }
18220
Bram Moolenaara7043832005-01-21 11:56:39 +000018221 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018222 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018223 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018224 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018225 return;
18226 }
18227
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018228 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018229 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018231 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018232 if (var_check_ro(v->di_flags, name)
18233 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018234 return;
18235 if (v->di_tv.v_type != tv->v_type
18236 && !((v->di_tv.v_type == VAR_STRING
18237 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018238 && (tv->v_type == VAR_STRING
18239 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018240 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018241 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018242 return;
18243 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018244
18245 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018246 * Handle setting internal v: variables separately: we don't change
18247 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018248 */
18249 if (ht == &vimvarht)
18250 {
18251 if (v->di_tv.v_type == VAR_STRING)
18252 {
18253 vim_free(v->di_tv.vval.v_string);
18254 if (copy || tv->v_type != VAR_STRING)
18255 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18256 else
18257 {
18258 /* Take over the string to avoid an extra alloc/free. */
18259 v->di_tv.vval.v_string = tv->vval.v_string;
18260 tv->vval.v_string = NULL;
18261 }
18262 }
18263 else if (v->di_tv.v_type != VAR_NUMBER)
18264 EMSG2(_(e_intern2), "set_var()");
18265 else
18266 v->di_tv.vval.v_number = get_tv_number(tv);
18267 return;
18268 }
18269
18270 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 }
18272 else /* add a new variable */
18273 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018274 /* Can't add "v:" variable. */
18275 if (ht == &vimvarht)
18276 {
18277 EMSG2(_(e_illvar), name);
18278 return;
18279 }
18280
Bram Moolenaar92124a32005-06-17 22:03:40 +000018281 /* Make sure the variable name is valid. */
18282 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018283 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18284 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018285 {
18286 EMSG2(_(e_illvar), varname);
18287 return;
18288 }
18289
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018290 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18291 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018292 if (v == NULL)
18293 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018294 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018295 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018297 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 return;
18299 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018300 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018303 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018304 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018305 else
18306 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018308 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018309 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311}
18312
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018313/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018314 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018315 * Also give an error message.
18316 */
18317 static int
18318var_check_ro(flags, name)
18319 int flags;
18320 char_u *name;
18321{
18322 if (flags & DI_FLAGS_RO)
18323 {
18324 EMSG2(_(e_readonlyvar), name);
18325 return TRUE;
18326 }
18327 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18328 {
18329 EMSG2(_(e_readonlysbx), name);
18330 return TRUE;
18331 }
18332 return FALSE;
18333}
18334
18335/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018336 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18337 * Also give an error message.
18338 */
18339 static int
18340var_check_fixed(flags, name)
18341 int flags;
18342 char_u *name;
18343{
18344 if (flags & DI_FLAGS_FIX)
18345 {
18346 EMSG2(_("E795: Cannot delete variable %s"), name);
18347 return TRUE;
18348 }
18349 return FALSE;
18350}
18351
18352/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018353 * Return TRUE if typeval "tv" is set to be locked (immutable).
18354 * Also give an error message, using "name".
18355 */
18356 static int
18357tv_check_lock(lock, name)
18358 int lock;
18359 char_u *name;
18360{
18361 if (lock & VAR_LOCKED)
18362 {
18363 EMSG2(_("E741: Value is locked: %s"),
18364 name == NULL ? (char_u *)_("Unknown") : name);
18365 return TRUE;
18366 }
18367 if (lock & VAR_FIXED)
18368 {
18369 EMSG2(_("E742: Cannot change value of %s"),
18370 name == NULL ? (char_u *)_("Unknown") : name);
18371 return TRUE;
18372 }
18373 return FALSE;
18374}
18375
18376/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018377 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018378 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018379 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018382copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018383 typval_T *from;
18384 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018386 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018387 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018388 switch (from->v_type)
18389 {
18390 case VAR_NUMBER:
18391 to->vval.v_number = from->vval.v_number;
18392 break;
18393 case VAR_STRING:
18394 case VAR_FUNC:
18395 if (from->vval.v_string == NULL)
18396 to->vval.v_string = NULL;
18397 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018398 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018399 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018400 if (from->v_type == VAR_FUNC)
18401 func_ref(to->vval.v_string);
18402 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018403 break;
18404 case VAR_LIST:
18405 if (from->vval.v_list == NULL)
18406 to->vval.v_list = NULL;
18407 else
18408 {
18409 to->vval.v_list = from->vval.v_list;
18410 ++to->vval.v_list->lv_refcount;
18411 }
18412 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018413 case VAR_DICT:
18414 if (from->vval.v_dict == NULL)
18415 to->vval.v_dict = NULL;
18416 else
18417 {
18418 to->vval.v_dict = from->vval.v_dict;
18419 ++to->vval.v_dict->dv_refcount;
18420 }
18421 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018422 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018423 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018424 break;
18425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426}
18427
18428/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018429 * Make a copy of an item.
18430 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018431 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18432 * reference to an already copied list/dict can be used.
18433 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018434 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018435 static int
18436item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018437 typval_T *from;
18438 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018439 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018440 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018441{
18442 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018443 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018444
Bram Moolenaar33570922005-01-25 22:26:29 +000018445 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018446 {
18447 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018448 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018449 }
18450 ++recurse;
18451
18452 switch (from->v_type)
18453 {
18454 case VAR_NUMBER:
18455 case VAR_STRING:
18456 case VAR_FUNC:
18457 copy_tv(from, to);
18458 break;
18459 case VAR_LIST:
18460 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018461 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018462 if (from->vval.v_list == NULL)
18463 to->vval.v_list = NULL;
18464 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18465 {
18466 /* use the copy made earlier */
18467 to->vval.v_list = from->vval.v_list->lv_copylist;
18468 ++to->vval.v_list->lv_refcount;
18469 }
18470 else
18471 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18472 if (to->vval.v_list == NULL)
18473 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018474 break;
18475 case VAR_DICT:
18476 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018477 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018478 if (from->vval.v_dict == NULL)
18479 to->vval.v_dict = NULL;
18480 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18481 {
18482 /* use the copy made earlier */
18483 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18484 ++to->vval.v_dict->dv_refcount;
18485 }
18486 else
18487 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18488 if (to->vval.v_dict == NULL)
18489 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018490 break;
18491 default:
18492 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018493 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018494 }
18495 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018496 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018497}
18498
18499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500 * ":echo expr1 ..." print each argument separated with a space, add a
18501 * newline at the end.
18502 * ":echon expr1 ..." print each argument plain.
18503 */
18504 void
18505ex_echo(eap)
18506 exarg_T *eap;
18507{
18508 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018509 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018510 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 char_u *p;
18512 int needclr = TRUE;
18513 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018514 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515
18516 if (eap->skip)
18517 ++emsg_skip;
18518 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18519 {
18520 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018521 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 {
18523 /*
18524 * Report the invalid expression unless the expression evaluation
18525 * has been cancelled due to an aborting error, an interrupt, or an
18526 * exception.
18527 */
18528 if (!aborting())
18529 EMSG2(_(e_invexpr2), p);
18530 break;
18531 }
18532 if (!eap->skip)
18533 {
18534 if (atstart)
18535 {
18536 atstart = FALSE;
18537 /* Call msg_start() after eval1(), evaluating the expression
18538 * may cause a message to appear. */
18539 if (eap->cmdidx == CMD_echo)
18540 msg_start();
18541 }
18542 else if (eap->cmdidx == CMD_echo)
18543 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018544 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018545 if (p != NULL)
18546 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018548 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018550 if (*p != TAB && needclr)
18551 {
18552 /* remove any text still there from the command */
18553 msg_clr_eos();
18554 needclr = FALSE;
18555 }
18556 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 }
18558 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018559 {
18560#ifdef FEAT_MBYTE
18561 if (has_mbyte)
18562 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018563 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018564
18565 (void)msg_outtrans_len_attr(p, i, echo_attr);
18566 p += i - 1;
18567 }
18568 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018570 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018573 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 arg = skipwhite(arg);
18577 }
18578 eap->nextcmd = check_nextcmd(arg);
18579
18580 if (eap->skip)
18581 --emsg_skip;
18582 else
18583 {
18584 /* remove text that may still be there from the command */
18585 if (needclr)
18586 msg_clr_eos();
18587 if (eap->cmdidx == CMD_echo)
18588 msg_end();
18589 }
18590}
18591
18592/*
18593 * ":echohl {name}".
18594 */
18595 void
18596ex_echohl(eap)
18597 exarg_T *eap;
18598{
18599 int id;
18600
18601 id = syn_name2id(eap->arg);
18602 if (id == 0)
18603 echo_attr = 0;
18604 else
18605 echo_attr = syn_id2attr(id);
18606}
18607
18608/*
18609 * ":execute expr1 ..." execute the result of an expression.
18610 * ":echomsg expr1 ..." Print a message
18611 * ":echoerr expr1 ..." Print an error
18612 * Each gets spaces around each argument and a newline at the end for
18613 * echo commands
18614 */
18615 void
18616ex_execute(eap)
18617 exarg_T *eap;
18618{
18619 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018620 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 int ret = OK;
18622 char_u *p;
18623 garray_T ga;
18624 int len;
18625 int save_did_emsg;
18626
18627 ga_init2(&ga, 1, 80);
18628
18629 if (eap->skip)
18630 ++emsg_skip;
18631 while (*arg != NUL && *arg != '|' && *arg != '\n')
18632 {
18633 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018634 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 {
18636 /*
18637 * Report the invalid expression unless the expression evaluation
18638 * has been cancelled due to an aborting error, an interrupt, or an
18639 * exception.
18640 */
18641 if (!aborting())
18642 EMSG2(_(e_invexpr2), p);
18643 ret = FAIL;
18644 break;
18645 }
18646
18647 if (!eap->skip)
18648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018649 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 len = (int)STRLEN(p);
18651 if (ga_grow(&ga, len + 2) == FAIL)
18652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 ret = FAIL;
18655 break;
18656 }
18657 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 ga.ga_len += len;
18661 }
18662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 arg = skipwhite(arg);
18665 }
18666
18667 if (ret != FAIL && ga.ga_data != NULL)
18668 {
18669 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018672 out_flush();
18673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 else if (eap->cmdidx == CMD_echoerr)
18675 {
18676 /* We don't want to abort following commands, restore did_emsg. */
18677 save_did_emsg = did_emsg;
18678 EMSG((char_u *)ga.ga_data);
18679 if (!force_abort)
18680 did_emsg = save_did_emsg;
18681 }
18682 else if (eap->cmdidx == CMD_execute)
18683 do_cmdline((char_u *)ga.ga_data,
18684 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18685 }
18686
18687 ga_clear(&ga);
18688
18689 if (eap->skip)
18690 --emsg_skip;
18691
18692 eap->nextcmd = check_nextcmd(arg);
18693}
18694
18695/*
18696 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18697 * "arg" points to the "&" or '+' when called, to "option" when returning.
18698 * Returns NULL when no option name found. Otherwise pointer to the char
18699 * after the option name.
18700 */
18701 static char_u *
18702find_option_end(arg, opt_flags)
18703 char_u **arg;
18704 int *opt_flags;
18705{
18706 char_u *p = *arg;
18707
18708 ++p;
18709 if (*p == 'g' && p[1] == ':')
18710 {
18711 *opt_flags = OPT_GLOBAL;
18712 p += 2;
18713 }
18714 else if (*p == 'l' && p[1] == ':')
18715 {
18716 *opt_flags = OPT_LOCAL;
18717 p += 2;
18718 }
18719 else
18720 *opt_flags = 0;
18721
18722 if (!ASCII_ISALPHA(*p))
18723 return NULL;
18724 *arg = p;
18725
18726 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18727 p += 4; /* termcap option */
18728 else
18729 while (ASCII_ISALPHA(*p))
18730 ++p;
18731 return p;
18732}
18733
18734/*
18735 * ":function"
18736 */
18737 void
18738ex_function(eap)
18739 exarg_T *eap;
18740{
18741 char_u *theline;
18742 int j;
18743 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 char_u *name = NULL;
18746 char_u *p;
18747 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018748 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 garray_T newargs;
18750 garray_T newlines;
18751 int varargs = FALSE;
18752 int mustend = FALSE;
18753 int flags = 0;
18754 ufunc_T *fp;
18755 int indent;
18756 int nesting;
18757 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018758 dictitem_T *v;
18759 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018760 static int func_nr = 0; /* number for nameless function */
18761 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018762 hashtab_T *ht;
18763 int todo;
18764 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018765 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766
18767 /*
18768 * ":function" without argument: list functions.
18769 */
18770 if (ends_excmd(*eap->arg))
18771 {
18772 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018773 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018774 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018775 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018776 {
18777 if (!HASHITEM_EMPTY(hi))
18778 {
18779 --todo;
18780 fp = HI2UF(hi);
18781 if (!isdigit(*fp->uf_name))
18782 list_func_head(fp, FALSE);
18783 }
18784 }
18785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 eap->nextcmd = check_nextcmd(eap->arg);
18787 return;
18788 }
18789
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018790 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018791 * ":function /pat": list functions matching pattern.
18792 */
18793 if (*eap->arg == '/')
18794 {
18795 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18796 if (!eap->skip)
18797 {
18798 regmatch_T regmatch;
18799
18800 c = *p;
18801 *p = NUL;
18802 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18803 *p = c;
18804 if (regmatch.regprog != NULL)
18805 {
18806 regmatch.rm_ic = p_ic;
18807
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018808 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018809 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18810 {
18811 if (!HASHITEM_EMPTY(hi))
18812 {
18813 --todo;
18814 fp = HI2UF(hi);
18815 if (!isdigit(*fp->uf_name)
18816 && vim_regexec(&regmatch, fp->uf_name, 0))
18817 list_func_head(fp, FALSE);
18818 }
18819 }
18820 }
18821 }
18822 if (*p == '/')
18823 ++p;
18824 eap->nextcmd = check_nextcmd(p);
18825 return;
18826 }
18827
18828 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018829 * Get the function name. There are these situations:
18830 * func normal function name
18831 * "name" == func, "fudi.fd_dict" == NULL
18832 * dict.func new dictionary entry
18833 * "name" == NULL, "fudi.fd_dict" set,
18834 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18835 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018836 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018837 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18838 * dict.func existing dict entry that's not a Funcref
18839 * "name" == NULL, "fudi.fd_dict" set,
18840 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018843 name = trans_function_name(&p, eap->skip, 0, &fudi);
18844 paren = (vim_strchr(p, '(') != NULL);
18845 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846 {
18847 /*
18848 * Return on an invalid expression in braces, unless the expression
18849 * evaluation has been cancelled due to an aborting error, an
18850 * interrupt, or an exception.
18851 */
18852 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018853 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018854 if (!eap->skip && fudi.fd_newkey != NULL)
18855 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018856 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 else
18860 eap->skip = TRUE;
18861 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018862
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 /* An error in a function call during evaluation of an expression in magic
18864 * braces should not cause the function not to be defined. */
18865 saved_did_emsg = did_emsg;
18866 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867
18868 /*
18869 * ":function func" with only function name: list function.
18870 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018871 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872 {
18873 if (!ends_excmd(*skipwhite(p)))
18874 {
18875 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018876 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 }
18878 eap->nextcmd = check_nextcmd(p);
18879 if (eap->nextcmd != NULL)
18880 *p = NUL;
18881 if (!eap->skip && !got_int)
18882 {
18883 fp = find_func(name);
18884 if (fp != NULL)
18885 {
18886 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018887 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018889 if (FUNCLINE(fp, j) == NULL)
18890 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891 msg_putchar('\n');
18892 msg_outnum((long)(j + 1));
18893 if (j < 9)
18894 msg_putchar(' ');
18895 if (j < 99)
18896 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018897 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 out_flush(); /* show a line at a time */
18899 ui_breakcheck();
18900 }
18901 if (!got_int)
18902 {
18903 msg_putchar('\n');
18904 msg_puts((char_u *)" endfunction");
18905 }
18906 }
18907 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018908 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018910 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 }
18912
18913 /*
18914 * ":function name(arg1, arg2)" Define function.
18915 */
18916 p = skipwhite(p);
18917 if (*p != '(')
18918 {
18919 if (!eap->skip)
18920 {
18921 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018922 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 }
18924 /* attempt to continue by skipping some text */
18925 if (vim_strchr(p, '(') != NULL)
18926 p = vim_strchr(p, '(');
18927 }
18928 p = skipwhite(p + 1);
18929
18930 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18931 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18932
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018933 if (!eap->skip)
18934 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018935 /* Check the name of the function. Unless it's a dictionary function
18936 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018937 if (name != NULL)
18938 arg = name;
18939 else
18940 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018941 if (arg != NULL && (fudi.fd_di == NULL
18942 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018943 {
18944 if (*arg == K_SPECIAL)
18945 j = 3;
18946 else
18947 j = 0;
18948 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18949 : eval_isnamec(arg[j])))
18950 ++j;
18951 if (arg[j] != NUL)
18952 emsg_funcname(_(e_invarg2), arg);
18953 }
18954 }
18955
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 /*
18957 * Isolate the arguments: "arg1, arg2, ...)"
18958 */
18959 while (*p != ')')
18960 {
18961 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18962 {
18963 varargs = TRUE;
18964 p += 3;
18965 mustend = TRUE;
18966 }
18967 else
18968 {
18969 arg = p;
18970 while (ASCII_ISALNUM(*p) || *p == '_')
18971 ++p;
18972 if (arg == p || isdigit(*arg)
18973 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18974 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18975 {
18976 if (!eap->skip)
18977 EMSG2(_("E125: Illegal argument: %s"), arg);
18978 break;
18979 }
18980 if (ga_grow(&newargs, 1) == FAIL)
18981 goto erret;
18982 c = *p;
18983 *p = NUL;
18984 arg = vim_strsave(arg);
18985 if (arg == NULL)
18986 goto erret;
18987 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18988 *p = c;
18989 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 if (*p == ',')
18991 ++p;
18992 else
18993 mustend = TRUE;
18994 }
18995 p = skipwhite(p);
18996 if (mustend && *p != ')')
18997 {
18998 if (!eap->skip)
18999 EMSG2(_(e_invarg2), eap->arg);
19000 break;
19001 }
19002 }
19003 ++p; /* skip the ')' */
19004
Bram Moolenaare9a41262005-01-15 22:18:47 +000019005 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 for (;;)
19007 {
19008 p = skipwhite(p);
19009 if (STRNCMP(p, "range", 5) == 0)
19010 {
19011 flags |= FC_RANGE;
19012 p += 5;
19013 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019014 else if (STRNCMP(p, "dict", 4) == 0)
19015 {
19016 flags |= FC_DICT;
19017 p += 4;
19018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 else if (STRNCMP(p, "abort", 5) == 0)
19020 {
19021 flags |= FC_ABORT;
19022 p += 5;
19023 }
19024 else
19025 break;
19026 }
19027
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019028 /* When there is a line break use what follows for the function body.
19029 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19030 if (*p == '\n')
19031 line_arg = p + 1;
19032 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 EMSG(_(e_trailing));
19034
19035 /*
19036 * Read the body of the function, until ":endfunction" is found.
19037 */
19038 if (KeyTyped)
19039 {
19040 /* Check if the function already exists, don't let the user type the
19041 * whole function before telling him it doesn't work! For a script we
19042 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019043 if (!eap->skip && !eap->forceit)
19044 {
19045 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19046 EMSG(_(e_funcdict));
19047 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019048 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019051 if (!eap->skip && did_emsg)
19052 goto erret;
19053
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 msg_putchar('\n'); /* don't overwrite the function name */
19055 cmdline_row = msg_row;
19056 }
19057
19058 indent = 2;
19059 nesting = 0;
19060 for (;;)
19061 {
19062 msg_scroll = TRUE;
19063 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019064 sourcing_lnum_off = sourcing_lnum;
19065
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019066 if (line_arg != NULL)
19067 {
19068 /* Use eap->arg, split up in parts by line breaks. */
19069 theline = line_arg;
19070 p = vim_strchr(theline, '\n');
19071 if (p == NULL)
19072 line_arg += STRLEN(line_arg);
19073 else
19074 {
19075 *p = NUL;
19076 line_arg = p + 1;
19077 }
19078 }
19079 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 theline = getcmdline(':', 0L, indent);
19081 else
19082 theline = eap->getline(':', eap->cookie, indent);
19083 if (KeyTyped)
19084 lines_left = Rows - 1;
19085 if (theline == NULL)
19086 {
19087 EMSG(_("E126: Missing :endfunction"));
19088 goto erret;
19089 }
19090
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019091 /* Detect line continuation: sourcing_lnum increased more than one. */
19092 if (sourcing_lnum > sourcing_lnum_off + 1)
19093 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19094 else
19095 sourcing_lnum_off = 0;
19096
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 if (skip_until != NULL)
19098 {
19099 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19100 * don't check for ":endfunc". */
19101 if (STRCMP(theline, skip_until) == 0)
19102 {
19103 vim_free(skip_until);
19104 skip_until = NULL;
19105 }
19106 }
19107 else
19108 {
19109 /* skip ':' and blanks*/
19110 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19111 ;
19112
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019113 /* Check for "endfunction". */
19114 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019116 if (line_arg == NULL)
19117 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 break;
19119 }
19120
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019121 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 * at "end". */
19123 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19124 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019125 else if (STRNCMP(p, "if", 2) == 0
19126 || STRNCMP(p, "wh", 2) == 0
19127 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 || STRNCMP(p, "try", 3) == 0)
19129 indent += 2;
19130
19131 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019132 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019134 if (*p == '!')
19135 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136 p += eval_fname_script(p);
19137 if (ASCII_ISALPHA(*p))
19138 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019139 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 if (*skipwhite(p) == '(')
19141 {
19142 ++nesting;
19143 indent += 2;
19144 }
19145 }
19146 }
19147
19148 /* Check for ":append" or ":insert". */
19149 p = skip_range(p, NULL);
19150 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19151 || (p[0] == 'i'
19152 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19153 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19154 skip_until = vim_strsave((char_u *)".");
19155
19156 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19157 arg = skipwhite(skiptowhite(p));
19158 if (arg[0] == '<' && arg[1] =='<'
19159 && ((p[0] == 'p' && p[1] == 'y'
19160 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19161 || (p[0] == 'p' && p[1] == 'e'
19162 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19163 || (p[0] == 't' && p[1] == 'c'
19164 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19165 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19166 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019167 || (p[0] == 'm' && p[1] == 'z'
19168 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 ))
19170 {
19171 /* ":python <<" continues until a dot, like ":append" */
19172 p = skipwhite(arg + 2);
19173 if (*p == NUL)
19174 skip_until = vim_strsave((char_u *)".");
19175 else
19176 skip_until = vim_strsave(p);
19177 }
19178 }
19179
19180 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019181 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019182 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019183 if (line_arg == NULL)
19184 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019186 }
19187
19188 /* Copy the line to newly allocated memory. get_one_sourceline()
19189 * allocates 250 bytes per line, this saves 80% on average. The cost
19190 * is an extra alloc/free. */
19191 p = vim_strsave(theline);
19192 if (p != NULL)
19193 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019194 if (line_arg == NULL)
19195 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019196 theline = p;
19197 }
19198
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019199 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19200
19201 /* Add NULL lines for continuation lines, so that the line count is
19202 * equal to the index in the growarray. */
19203 while (sourcing_lnum_off-- > 0)
19204 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019205
19206 /* Check for end of eap->arg. */
19207 if (line_arg != NULL && *line_arg == NUL)
19208 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 }
19210
19211 /* Don't define the function when skipping commands or when an error was
19212 * detected. */
19213 if (eap->skip || did_emsg)
19214 goto erret;
19215
19216 /*
19217 * If there are no errors, add the function
19218 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019219 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019220 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019221 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019222 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019223 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019224 emsg_funcname("E707: Function name conflicts with variable: %s",
19225 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019226 goto erret;
19227 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019228
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019229 fp = find_func(name);
19230 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019232 if (!eap->forceit)
19233 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019234 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019235 goto erret;
19236 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019237 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019238 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019239 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019240 name);
19241 goto erret;
19242 }
19243 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019244 ga_clear_strings(&(fp->uf_args));
19245 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019246 vim_free(name);
19247 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 }
19250 else
19251 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019252 char numbuf[20];
19253
19254 fp = NULL;
19255 if (fudi.fd_newkey == NULL && !eap->forceit)
19256 {
19257 EMSG(_(e_funcdict));
19258 goto erret;
19259 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019260 if (fudi.fd_di == NULL)
19261 {
19262 /* Can't add a function to a locked dictionary */
19263 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19264 goto erret;
19265 }
19266 /* Can't change an existing function if it is locked */
19267 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19268 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019269
19270 /* Give the function a sequential number. Can only be used with a
19271 * Funcref! */
19272 vim_free(name);
19273 sprintf(numbuf, "%d", ++func_nr);
19274 name = vim_strsave((char_u *)numbuf);
19275 if (name == NULL)
19276 goto erret;
19277 }
19278
19279 if (fp == NULL)
19280 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019281 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019282 {
19283 int slen, plen;
19284 char_u *scriptname;
19285
19286 /* Check that the autoload name matches the script name. */
19287 j = FAIL;
19288 if (sourcing_name != NULL)
19289 {
19290 scriptname = autoload_name(name);
19291 if (scriptname != NULL)
19292 {
19293 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019294 plen = (int)STRLEN(p);
19295 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019296 if (slen > plen && fnamecmp(p,
19297 sourcing_name + slen - plen) == 0)
19298 j = OK;
19299 vim_free(scriptname);
19300 }
19301 }
19302 if (j == FAIL)
19303 {
19304 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19305 goto erret;
19306 }
19307 }
19308
19309 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 if (fp == NULL)
19311 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019312
19313 if (fudi.fd_dict != NULL)
19314 {
19315 if (fudi.fd_di == NULL)
19316 {
19317 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019318 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019319 if (fudi.fd_di == NULL)
19320 {
19321 vim_free(fp);
19322 goto erret;
19323 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019324 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19325 {
19326 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019327 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019328 goto erret;
19329 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019330 }
19331 else
19332 /* overwrite existing dict entry */
19333 clear_tv(&fudi.fd_di->di_tv);
19334 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019335 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019336 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019337 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019338
19339 /* behave like "dict" was used */
19340 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019341 }
19342
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019344 STRCPY(fp->uf_name, name);
19345 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019347 fp->uf_args = newargs;
19348 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019349#ifdef FEAT_PROFILE
19350 fp->uf_tml_count = NULL;
19351 fp->uf_tml_total = NULL;
19352 fp->uf_tml_self = NULL;
19353 fp->uf_profiling = FALSE;
19354 if (prof_def_func())
19355 func_do_profile(fp);
19356#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019357 fp->uf_varargs = varargs;
19358 fp->uf_flags = flags;
19359 fp->uf_calls = 0;
19360 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019361 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362
19363erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 ga_clear_strings(&newargs);
19365 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019366ret_free:
19367 vim_free(skip_until);
19368 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371}
19372
19373/*
19374 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019375 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019377 * flags:
19378 * TFN_INT: internal function name OK
19379 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 * Advances "pp" to just after the function name (if no error).
19381 */
19382 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019383trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 char_u **pp;
19385 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019386 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019387 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019389 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 char_u *start;
19391 char_u *end;
19392 int lead;
19393 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019396
19397 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019398 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019400
19401 /* Check for hard coded <SNR>: already translated function ID (from a user
19402 * command). */
19403 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19404 && (*pp)[2] == (int)KE_SNR)
19405 {
19406 *pp += 3;
19407 len = get_id_len(pp) + 3;
19408 return vim_strnsave(start, len);
19409 }
19410
19411 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19412 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019414 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019416
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019417 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19418 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019419 if (end == start)
19420 {
19421 if (!skip)
19422 EMSG(_("E129: Function name required"));
19423 goto theend;
19424 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019425 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019426 {
19427 /*
19428 * Report an invalid expression in braces, unless the expression
19429 * evaluation has been cancelled due to an aborting error, an
19430 * interrupt, or an exception.
19431 */
19432 if (!aborting())
19433 {
19434 if (end != NULL)
19435 EMSG2(_(e_invarg2), start);
19436 }
19437 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019438 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019439 goto theend;
19440 }
19441
19442 if (lv.ll_tv != NULL)
19443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019444 if (fdp != NULL)
19445 {
19446 fdp->fd_dict = lv.ll_dict;
19447 fdp->fd_newkey = lv.ll_newkey;
19448 lv.ll_newkey = NULL;
19449 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019450 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019451 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19452 {
19453 name = vim_strsave(lv.ll_tv->vval.v_string);
19454 *pp = end;
19455 }
19456 else
19457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019458 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19459 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019460 EMSG(_(e_funcref));
19461 else
19462 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019463 name = NULL;
19464 }
19465 goto theend;
19466 }
19467
19468 if (lv.ll_name == NULL)
19469 {
19470 /* Error found, but continue after the function name. */
19471 *pp = end;
19472 goto theend;
19473 }
19474
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019475 /* Check if the name is a Funcref. If so, use the value. */
19476 if (lv.ll_exp_name != NULL)
19477 {
19478 len = (int)STRLEN(lv.ll_exp_name);
19479 name = deref_func_name(lv.ll_exp_name, &len);
19480 if (name == lv.ll_exp_name)
19481 name = NULL;
19482 }
19483 else
19484 {
19485 len = (int)(end - *pp);
19486 name = deref_func_name(*pp, &len);
19487 if (name == *pp)
19488 name = NULL;
19489 }
19490 if (name != NULL)
19491 {
19492 name = vim_strsave(name);
19493 *pp = end;
19494 goto theend;
19495 }
19496
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019497 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019498 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019499 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019500 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19501 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19502 {
19503 /* When there was "s:" already or the name expanded to get a
19504 * leading "s:" then remove it. */
19505 lv.ll_name += 2;
19506 len -= 2;
19507 lead = 2;
19508 }
19509 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019510 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019511 {
19512 if (lead == 2) /* skip over "s:" */
19513 lv.ll_name += 2;
19514 len = (int)(end - lv.ll_name);
19515 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019516
19517 /*
19518 * Copy the function name to allocated memory.
19519 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19520 * Accept <SNR>123_name() outside a script.
19521 */
19522 if (skip)
19523 lead = 0; /* do nothing */
19524 else if (lead > 0)
19525 {
19526 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019527 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19528 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019529 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019530 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019531 if (current_SID <= 0)
19532 {
19533 EMSG(_(e_usingsid));
19534 goto theend;
19535 }
19536 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19537 lead += (int)STRLEN(sid_buf);
19538 }
19539 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019540 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019541 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019542 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019543 goto theend;
19544 }
19545 name = alloc((unsigned)(len + lead + 1));
19546 if (name != NULL)
19547 {
19548 if (lead > 0)
19549 {
19550 name[0] = K_SPECIAL;
19551 name[1] = KS_EXTRA;
19552 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019553 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019554 STRCPY(name + 3, sid_buf);
19555 }
19556 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19557 name[len + lead] = NUL;
19558 }
19559 *pp = end;
19560
19561theend:
19562 clear_lval(&lv);
19563 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564}
19565
19566/*
19567 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19568 * Return 2 if "p" starts with "s:".
19569 * Return 0 otherwise.
19570 */
19571 static int
19572eval_fname_script(p)
19573 char_u *p;
19574{
19575 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19576 || STRNICMP(p + 1, "SNR>", 4) == 0))
19577 return 5;
19578 if (p[0] == 's' && p[1] == ':')
19579 return 2;
19580 return 0;
19581}
19582
19583/*
19584 * Return TRUE if "p" starts with "<SID>" or "s:".
19585 * Only works if eval_fname_script() returned non-zero for "p"!
19586 */
19587 static int
19588eval_fname_sid(p)
19589 char_u *p;
19590{
19591 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19592}
19593
19594/*
19595 * List the head of the function: "name(arg1, arg2)".
19596 */
19597 static void
19598list_func_head(fp, indent)
19599 ufunc_T *fp;
19600 int indent;
19601{
19602 int j;
19603
19604 msg_start();
19605 if (indent)
19606 MSG_PUTS(" ");
19607 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019608 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 {
19610 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019611 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 }
19613 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019614 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019616 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 {
19618 if (j)
19619 MSG_PUTS(", ");
19620 msg_puts(FUNCARG(fp, j));
19621 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019622 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 {
19624 if (j)
19625 MSG_PUTS(", ");
19626 MSG_PUTS("...");
19627 }
19628 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019629 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019630 if (p_verbose > 0)
19631 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632}
19633
19634/*
19635 * Find a function by name, return pointer to it in ufuncs.
19636 * Return NULL for unknown function.
19637 */
19638 static ufunc_T *
19639find_func(name)
19640 char_u *name;
19641{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019642 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019644 hi = hash_find(&func_hashtab, name);
19645 if (!HASHITEM_EMPTY(hi))
19646 return HI2UF(hi);
19647 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648}
19649
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019650#if defined(EXITFREE) || defined(PROTO)
19651 void
19652free_all_functions()
19653{
19654 hashitem_T *hi;
19655
19656 /* Need to start all over every time, because func_free() may change the
19657 * hash table. */
19658 while (func_hashtab.ht_used > 0)
19659 for (hi = func_hashtab.ht_array; ; ++hi)
19660 if (!HASHITEM_EMPTY(hi))
19661 {
19662 func_free(HI2UF(hi));
19663 break;
19664 }
19665}
19666#endif
19667
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019668/*
19669 * Return TRUE if a function "name" exists.
19670 */
19671 static int
19672function_exists(name)
19673 char_u *name;
19674{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019675 char_u *nm = name;
19676 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019677 int n = FALSE;
19678
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019679 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019680 nm = skipwhite(nm);
19681
19682 /* Only accept "funcname", "funcname ", "funcname (..." and
19683 * "funcname(...", not "funcname!...". */
19684 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019685 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019686 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019687 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019688 else
19689 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019690 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019691 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019692 return n;
19693}
19694
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019695/*
19696 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019697 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019698 */
19699 static int
19700builtin_function(name)
19701 char_u *name;
19702{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019703 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19704 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019705}
19706
Bram Moolenaar05159a02005-02-26 23:04:13 +000019707#if defined(FEAT_PROFILE) || defined(PROTO)
19708/*
19709 * Start profiling function "fp".
19710 */
19711 static void
19712func_do_profile(fp)
19713 ufunc_T *fp;
19714{
19715 fp->uf_tm_count = 0;
19716 profile_zero(&fp->uf_tm_self);
19717 profile_zero(&fp->uf_tm_total);
19718 if (fp->uf_tml_count == NULL)
19719 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19720 (sizeof(int) * fp->uf_lines.ga_len));
19721 if (fp->uf_tml_total == NULL)
19722 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19723 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19724 if (fp->uf_tml_self == NULL)
19725 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19726 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19727 fp->uf_tml_idx = -1;
19728 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19729 || fp->uf_tml_self == NULL)
19730 return; /* out of memory */
19731
19732 fp->uf_profiling = TRUE;
19733}
19734
19735/*
19736 * Dump the profiling results for all functions in file "fd".
19737 */
19738 void
19739func_dump_profile(fd)
19740 FILE *fd;
19741{
19742 hashitem_T *hi;
19743 int todo;
19744 ufunc_T *fp;
19745 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019746 ufunc_T **sorttab;
19747 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019748
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019749 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019750 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19751
Bram Moolenaar05159a02005-02-26 23:04:13 +000019752 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19753 {
19754 if (!HASHITEM_EMPTY(hi))
19755 {
19756 --todo;
19757 fp = HI2UF(hi);
19758 if (fp->uf_profiling)
19759 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019760 if (sorttab != NULL)
19761 sorttab[st_len++] = fp;
19762
Bram Moolenaar05159a02005-02-26 23:04:13 +000019763 if (fp->uf_name[0] == K_SPECIAL)
19764 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19765 else
19766 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19767 if (fp->uf_tm_count == 1)
19768 fprintf(fd, "Called 1 time\n");
19769 else
19770 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19771 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19772 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19773 fprintf(fd, "\n");
19774 fprintf(fd, "count total (s) self (s)\n");
19775
19776 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19777 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019778 if (FUNCLINE(fp, i) == NULL)
19779 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019780 prof_func_line(fd, fp->uf_tml_count[i],
19781 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019782 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19783 }
19784 fprintf(fd, "\n");
19785 }
19786 }
19787 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019788
19789 if (sorttab != NULL && st_len > 0)
19790 {
19791 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19792 prof_total_cmp);
19793 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19794 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19795 prof_self_cmp);
19796 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19797 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019798}
Bram Moolenaar73830342005-02-28 22:48:19 +000019799
19800 static void
19801prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19802 FILE *fd;
19803 ufunc_T **sorttab;
19804 int st_len;
19805 char *title;
19806 int prefer_self; /* when equal print only self time */
19807{
19808 int i;
19809 ufunc_T *fp;
19810
19811 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19812 fprintf(fd, "count total (s) self (s) function\n");
19813 for (i = 0; i < 20 && i < st_len; ++i)
19814 {
19815 fp = sorttab[i];
19816 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19817 prefer_self);
19818 if (fp->uf_name[0] == K_SPECIAL)
19819 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19820 else
19821 fprintf(fd, " %s()\n", fp->uf_name);
19822 }
19823 fprintf(fd, "\n");
19824}
19825
19826/*
19827 * Print the count and times for one function or function line.
19828 */
19829 static void
19830prof_func_line(fd, count, total, self, prefer_self)
19831 FILE *fd;
19832 int count;
19833 proftime_T *total;
19834 proftime_T *self;
19835 int prefer_self; /* when equal print only self time */
19836{
19837 if (count > 0)
19838 {
19839 fprintf(fd, "%5d ", count);
19840 if (prefer_self && profile_equal(total, self))
19841 fprintf(fd, " ");
19842 else
19843 fprintf(fd, "%s ", profile_msg(total));
19844 if (!prefer_self && profile_equal(total, self))
19845 fprintf(fd, " ");
19846 else
19847 fprintf(fd, "%s ", profile_msg(self));
19848 }
19849 else
19850 fprintf(fd, " ");
19851}
19852
19853/*
19854 * Compare function for total time sorting.
19855 */
19856 static int
19857#ifdef __BORLANDC__
19858_RTLENTRYF
19859#endif
19860prof_total_cmp(s1, s2)
19861 const void *s1;
19862 const void *s2;
19863{
19864 ufunc_T *p1, *p2;
19865
19866 p1 = *(ufunc_T **)s1;
19867 p2 = *(ufunc_T **)s2;
19868 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19869}
19870
19871/*
19872 * Compare function for self time sorting.
19873 */
19874 static int
19875#ifdef __BORLANDC__
19876_RTLENTRYF
19877#endif
19878prof_self_cmp(s1, s2)
19879 const void *s1;
19880 const void *s2;
19881{
19882 ufunc_T *p1, *p2;
19883
19884 p1 = *(ufunc_T **)s1;
19885 p2 = *(ufunc_T **)s2;
19886 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19887}
19888
Bram Moolenaar05159a02005-02-26 23:04:13 +000019889#endif
19890
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019891/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019892 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019893 * Return TRUE if a package was loaded.
19894 */
19895 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019896script_autoload(name, reload)
19897 char_u *name;
19898 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019899{
19900 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019901 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019902 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019903 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019904
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019905 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019906 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019907 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019908 return FALSE;
19909
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019910 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019911
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019912 /* Find the name in the list of previously loaded package names. Skip
19913 * "autoload/", it's always the same. */
19914 for (i = 0; i < ga_loaded.ga_len; ++i)
19915 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19916 break;
19917 if (!reload && i < ga_loaded.ga_len)
19918 ret = FALSE; /* was loaded already */
19919 else
19920 {
19921 /* Remember the name if it wasn't loaded already. */
19922 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19923 {
19924 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19925 tofree = NULL;
19926 }
19927
19928 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019929 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019930 ret = TRUE;
19931 }
19932
19933 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019934 return ret;
19935}
19936
19937/*
19938 * Return the autoload script name for a function or variable name.
19939 * Returns NULL when out of memory.
19940 */
19941 static char_u *
19942autoload_name(name)
19943 char_u *name;
19944{
19945 char_u *p;
19946 char_u *scriptname;
19947
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019948 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019949 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19950 if (scriptname == NULL)
19951 return FALSE;
19952 STRCPY(scriptname, "autoload/");
19953 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019954 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019955 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019956 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019957 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019958 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019959}
19960
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19962
19963/*
19964 * Function given to ExpandGeneric() to obtain the list of user defined
19965 * function names.
19966 */
19967 char_u *
19968get_user_func_name(xp, idx)
19969 expand_T *xp;
19970 int idx;
19971{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019972 static long_u done;
19973 static hashitem_T *hi;
19974 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975
19976 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019978 done = 0;
19979 hi = func_hashtab.ht_array;
19980 }
19981 if (done < func_hashtab.ht_used)
19982 {
19983 if (done++ > 0)
19984 ++hi;
19985 while (HASHITEM_EMPTY(hi))
19986 ++hi;
19987 fp = HI2UF(hi);
19988
19989 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19990 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991
19992 cat_func_name(IObuff, fp);
19993 if (xp->xp_context != EXPAND_USER_FUNC)
19994 {
19995 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019996 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 STRCAT(IObuff, ")");
19998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 return IObuff;
20000 }
20001 return NULL;
20002}
20003
20004#endif /* FEAT_CMDL_COMPL */
20005
20006/*
20007 * Copy the function name of "fp" to buffer "buf".
20008 * "buf" must be able to hold the function name plus three bytes.
20009 * Takes care of script-local function names.
20010 */
20011 static void
20012cat_func_name(buf, fp)
20013 char_u *buf;
20014 ufunc_T *fp;
20015{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020016 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 {
20018 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020019 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 }
20021 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020022 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023}
20024
20025/*
20026 * ":delfunction {name}"
20027 */
20028 void
20029ex_delfunction(eap)
20030 exarg_T *eap;
20031{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020032 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 char_u *p;
20034 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020035 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036
20037 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020038 name = trans_function_name(&p, eap->skip, 0, &fudi);
20039 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020041 {
20042 if (fudi.fd_dict != NULL && !eap->skip)
20043 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 if (!ends_excmd(*skipwhite(p)))
20047 {
20048 vim_free(name);
20049 EMSG(_(e_trailing));
20050 return;
20051 }
20052 eap->nextcmd = check_nextcmd(p);
20053 if (eap->nextcmd != NULL)
20054 *p = NUL;
20055
20056 if (!eap->skip)
20057 fp = find_func(name);
20058 vim_free(name);
20059
20060 if (!eap->skip)
20061 {
20062 if (fp == NULL)
20063 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020064 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 return;
20066 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020067 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 {
20069 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20070 return;
20071 }
20072
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020073 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020075 /* Delete the dict item that refers to the function, it will
20076 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020077 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020079 else
20080 func_free(fp);
20081 }
20082}
20083
20084/*
20085 * Free a function and remove it from the list of functions.
20086 */
20087 static void
20088func_free(fp)
20089 ufunc_T *fp;
20090{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020091 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020092
20093 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020094 ga_clear_strings(&(fp->uf_args));
20095 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020096#ifdef FEAT_PROFILE
20097 vim_free(fp->uf_tml_count);
20098 vim_free(fp->uf_tml_total);
20099 vim_free(fp->uf_tml_self);
20100#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020101
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020102 /* remove the function from the function hashtable */
20103 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20104 if (HASHITEM_EMPTY(hi))
20105 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020106 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020107 hash_remove(&func_hashtab, hi);
20108
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020109 vim_free(fp);
20110}
20111
20112/*
20113 * Unreference a Function: decrement the reference count and free it when it
20114 * becomes zero. Only for numbered functions.
20115 */
20116 static void
20117func_unref(name)
20118 char_u *name;
20119{
20120 ufunc_T *fp;
20121
20122 if (name != NULL && isdigit(*name))
20123 {
20124 fp = find_func(name);
20125 if (fp == NULL)
20126 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020127 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020128 {
20129 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020130 * when "uf_calls" becomes zero. */
20131 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020132 func_free(fp);
20133 }
20134 }
20135}
20136
20137/*
20138 * Count a reference to a Function.
20139 */
20140 static void
20141func_ref(name)
20142 char_u *name;
20143{
20144 ufunc_T *fp;
20145
20146 if (name != NULL && isdigit(*name))
20147 {
20148 fp = find_func(name);
20149 if (fp == NULL)
20150 EMSG2(_(e_intern2), "func_ref()");
20151 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020152 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 }
20154}
20155
20156/*
20157 * Call a user function.
20158 */
20159 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020160call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 ufunc_T *fp; /* pointer to function */
20162 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020163 typval_T *argvars; /* arguments */
20164 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 linenr_T firstline; /* first line of range */
20166 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020167 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168{
Bram Moolenaar33570922005-01-25 22:26:29 +000020169 char_u *save_sourcing_name;
20170 linenr_T save_sourcing_lnum;
20171 scid_T save_current_SID;
20172 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 int save_did_emsg;
20174 static int depth = 0;
20175 dictitem_T *v;
20176 int fixvar_idx = 0; /* index in fixvar[] */
20177 int i;
20178 int ai;
20179 char_u numbuf[NUMBUFLEN];
20180 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020181#ifdef FEAT_PROFILE
20182 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020183 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185
20186 /* If depth of calling is getting too high, don't execute the function */
20187 if (depth >= p_mfd)
20188 {
20189 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 rettv->v_type = VAR_NUMBER;
20191 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192 return;
20193 }
20194 ++depth;
20195
20196 line_breakcheck(); /* check for CTRL-C hit */
20197
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020198 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020201 fc.rettv = rettv;
20202 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 fc.linenr = 0;
20204 fc.returned = FALSE;
20205 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020207 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 fc.dbg_tick = debug_tick;
20209
Bram Moolenaar33570922005-01-25 22:26:29 +000020210 /*
20211 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20212 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20213 * each argument variable and saves a lot of time.
20214 */
20215 /*
20216 * Init l: variables.
20217 */
20218 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020219 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020220 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020221 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20222 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020224 name = v->di_key;
20225 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20227 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20228 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020229 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020230 v->di_tv.vval.v_dict = selfdict;
20231 ++selfdict->dv_refcount;
20232 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020233
Bram Moolenaar33570922005-01-25 22:26:29 +000020234 /*
20235 * Init a: variables.
20236 * Set a:0 to "argcount".
20237 * Set a:000 to a list with room for the "..." arguments.
20238 */
20239 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20240 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020241 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020242 v = &fc.fixvar[fixvar_idx++].var;
20243 STRCPY(v->di_key, "000");
20244 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20245 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20246 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020247 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020248 v->di_tv.vval.v_list = &fc.l_varlist;
20249 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20250 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020251 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020252
20253 /*
20254 * Set a:firstline to "firstline" and a:lastline to "lastline".
20255 * Set a:name to named arguments.
20256 * Set a:N to the "..." arguments.
20257 */
20258 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20259 (varnumber_T)firstline);
20260 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20261 (varnumber_T)lastline);
20262 for (i = 0; i < argcount; ++i)
20263 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020264 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 if (ai < 0)
20266 /* named argument a:name */
20267 name = FUNCARG(fp, i);
20268 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020269 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020270 /* "..." argument a:1, a:2, etc. */
20271 sprintf((char *)numbuf, "%d", ai + 1);
20272 name = numbuf;
20273 }
20274 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20275 {
20276 v = &fc.fixvar[fixvar_idx++].var;
20277 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20278 }
20279 else
20280 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020281 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20282 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020283 if (v == NULL)
20284 break;
20285 v->di_flags = DI_FLAGS_RO;
20286 }
20287 STRCPY(v->di_key, name);
20288 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20289
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020290 /* Note: the values are copied directly to avoid alloc/free.
20291 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020292 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020293 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020294
20295 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20296 {
20297 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20298 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020299 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020300 }
20301 }
20302
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 /* Don't redraw while executing the function. */
20304 ++RedrawingDisabled;
20305 save_sourcing_name = sourcing_name;
20306 save_sourcing_lnum = sourcing_lnum;
20307 sourcing_lnum = 1;
20308 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020309 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 if (sourcing_name != NULL)
20311 {
20312 if (save_sourcing_name != NULL
20313 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20314 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20315 else
20316 STRCPY(sourcing_name, "function ");
20317 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20318
20319 if (p_verbose >= 12)
20320 {
20321 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020322 verbose_enter_scroll();
20323
Bram Moolenaar555b2802005-05-19 21:08:39 +000020324 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 if (p_verbose >= 14)
20326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020328 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020329 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020330 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331
20332 msg_puts((char_u *)"(");
20333 for (i = 0; i < argcount; ++i)
20334 {
20335 if (i > 0)
20336 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020337 if (argvars[i].v_type == VAR_NUMBER)
20338 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 else
20340 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020341 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20342 if (s != NULL)
20343 {
20344 trunc_string(s, buf, MSG_BUF_CLEN);
20345 msg_puts(buf);
20346 vim_free(tofree);
20347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 }
20349 }
20350 msg_puts((char_u *)")");
20351 }
20352 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020353
20354 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 --no_wait_return;
20356 }
20357 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020358#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020359 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020360 {
20361 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20362 func_do_profile(fp);
20363 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020364 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020365 {
20366 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020367 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020368 profile_zero(&fp->uf_tm_children);
20369 }
20370 script_prof_save(&wait_start);
20371 }
20372#endif
20373
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020375 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 save_did_emsg = did_emsg;
20377 did_emsg = FALSE;
20378
20379 /* call do_cmdline() to execute the lines */
20380 do_cmdline(NULL, get_func_line, (void *)&fc,
20381 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20382
20383 --RedrawingDisabled;
20384
20385 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020386 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020388 clear_tv(rettv);
20389 rettv->v_type = VAR_NUMBER;
20390 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 }
20392
Bram Moolenaar05159a02005-02-26 23:04:13 +000020393#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020394 if (do_profiling == PROF_YES && (fp->uf_profiling
20395 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020396 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020397 profile_end(&call_start);
20398 profile_sub_wait(&wait_start, &call_start);
20399 profile_add(&fp->uf_tm_total, &call_start);
20400 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020401 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020402 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020403 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20404 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020405 }
20406 }
20407#endif
20408
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 /* when being verbose, mention the return value */
20410 if (p_verbose >= 12)
20411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020413 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020416 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020417 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020418 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20419 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020420 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020422 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020423 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020424 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020425 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020426
Bram Moolenaar555b2802005-05-19 21:08:39 +000020427 /* The value may be very long. Skip the middle part, so that we
20428 * have some idea how it starts and ends. smsg() would always
20429 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020430 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20431 if (s != NULL)
20432 {
20433 trunc_string(s, buf, MSG_BUF_CLEN);
20434 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20435 vim_free(tofree);
20436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 }
20438 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020439
20440 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 --no_wait_return;
20442 }
20443
20444 vim_free(sourcing_name);
20445 sourcing_name = save_sourcing_name;
20446 sourcing_lnum = save_sourcing_lnum;
20447 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020448#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020449 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020450 script_prof_restore(&wait_start);
20451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452
20453 if (p_verbose >= 12 && sourcing_name != NULL)
20454 {
20455 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020456 verbose_enter_scroll();
20457
Bram Moolenaar555b2802005-05-19 21:08:39 +000020458 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020460
20461 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 --no_wait_return;
20463 }
20464
20465 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020466 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467
Bram Moolenaar33570922005-01-25 22:26:29 +000020468 /* The a: variables typevals were not alloced, only free the allocated
20469 * variables. */
20470 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20471
20472 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 --depth;
20474}
20475
20476/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020477 * Add a number variable "name" to dict "dp" with value "nr".
20478 */
20479 static void
20480add_nr_var(dp, v, name, nr)
20481 dict_T *dp;
20482 dictitem_T *v;
20483 char *name;
20484 varnumber_T nr;
20485{
20486 STRCPY(v->di_key, name);
20487 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20488 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20489 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020490 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020491 v->di_tv.vval.v_number = nr;
20492}
20493
20494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 * ":return [expr]"
20496 */
20497 void
20498ex_return(eap)
20499 exarg_T *eap;
20500{
20501 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020502 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503 int returning = FALSE;
20504
20505 if (current_funccal == NULL)
20506 {
20507 EMSG(_("E133: :return not inside a function"));
20508 return;
20509 }
20510
20511 if (eap->skip)
20512 ++emsg_skip;
20513
20514 eap->nextcmd = NULL;
20515 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020516 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 {
20518 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020521 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 }
20523 /* It's safer to return also on error. */
20524 else if (!eap->skip)
20525 {
20526 /*
20527 * Return unless the expression evaluation has been cancelled due to an
20528 * aborting error, an interrupt, or an exception.
20529 */
20530 if (!aborting())
20531 returning = do_return(eap, FALSE, TRUE, NULL);
20532 }
20533
20534 /* When skipping or the return gets pending, advance to the next command
20535 * in this line (!returning). Otherwise, ignore the rest of the line.
20536 * Following lines will be ignored by get_func_line(). */
20537 if (returning)
20538 eap->nextcmd = NULL;
20539 else if (eap->nextcmd == NULL) /* no argument */
20540 eap->nextcmd = check_nextcmd(arg);
20541
20542 if (eap->skip)
20543 --emsg_skip;
20544}
20545
20546/*
20547 * Return from a function. Possibly makes the return pending. Also called
20548 * for a pending return at the ":endtry" or after returning from an extra
20549 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020550 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 * FALSE when the return gets pending.
20553 */
20554 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020555do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 exarg_T *eap;
20557 int reanimate;
20558 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020559 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560{
20561 int idx;
20562 struct condstack *cstack = eap->cstack;
20563
20564 if (reanimate)
20565 /* Undo the return. */
20566 current_funccal->returned = FALSE;
20567
20568 /*
20569 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20570 * not in its finally clause (which then is to be executed next) is found.
20571 * In this case, make the ":return" pending for execution at the ":endtry".
20572 * Otherwise, return normally.
20573 */
20574 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20575 if (idx >= 0)
20576 {
20577 cstack->cs_pending[idx] = CSTP_RETURN;
20578
20579 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020580 /* A pending return again gets pending. "rettv" points to an
20581 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020583 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 else
20585 {
20586 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020587 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020589 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020591 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 {
20593 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020594 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020595 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 else
20597 EMSG(_(e_outofmem));
20598 }
20599 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020600 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601
20602 if (reanimate)
20603 {
20604 /* The pending return value could be overwritten by a ":return"
20605 * without argument in a finally clause; reset the default
20606 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020607 current_funccal->rettv->v_type = VAR_NUMBER;
20608 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 }
20610 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 }
20613 else
20614 {
20615 current_funccal->returned = TRUE;
20616
20617 /* If the return is carried out now, store the return value. For
20618 * a return immediately after reanimation, the value is already
20619 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020620 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020622 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020623 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020625 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 }
20627 }
20628
20629 return idx < 0;
20630}
20631
20632/*
20633 * Free the variable with a pending return value.
20634 */
20635 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020636discard_pending_return(rettv)
20637 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638{
Bram Moolenaar33570922005-01-25 22:26:29 +000020639 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640}
20641
20642/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020643 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 * is an allocated string. Used by report_pending() for verbose messages.
20645 */
20646 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020647get_return_cmd(rettv)
20648 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020650 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020651 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020652 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020654 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020655 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020656 if (s == NULL)
20657 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020658
20659 STRCPY(IObuff, ":return ");
20660 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20661 if (STRLEN(s) + 8 >= IOSIZE)
20662 STRCPY(IObuff + IOSIZE - 4, "...");
20663 vim_free(tofree);
20664 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665}
20666
20667/*
20668 * Get next function line.
20669 * Called by do_cmdline() to get the next line.
20670 * Returns allocated string, or NULL for end of function.
20671 */
20672/* ARGSUSED */
20673 char_u *
20674get_func_line(c, cookie, indent)
20675 int c; /* not used */
20676 void *cookie;
20677 int indent; /* not used */
20678{
Bram Moolenaar33570922005-01-25 22:26:29 +000020679 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020680 ufunc_T *fp = fcp->func;
20681 char_u *retval;
20682 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683
20684 /* If breakpoints have been added/deleted need to check for it. */
20685 if (fcp->dbg_tick != debug_tick)
20686 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020687 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 sourcing_lnum);
20689 fcp->dbg_tick = debug_tick;
20690 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020691#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020692 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020693 func_line_end(cookie);
20694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695
Bram Moolenaar05159a02005-02-26 23:04:13 +000020696 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020697 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20698 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 retval = NULL;
20700 else
20701 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020702 /* Skip NULL lines (continuation lines). */
20703 while (fcp->linenr < gap->ga_len
20704 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20705 ++fcp->linenr;
20706 if (fcp->linenr >= gap->ga_len)
20707 retval = NULL;
20708 else
20709 {
20710 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20711 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020712#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020713 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020714 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020715#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 }
20718
20719 /* Did we encounter a breakpoint? */
20720 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20721 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020722 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020724 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 sourcing_lnum);
20726 fcp->dbg_tick = debug_tick;
20727 }
20728
20729 return retval;
20730}
20731
Bram Moolenaar05159a02005-02-26 23:04:13 +000020732#if defined(FEAT_PROFILE) || defined(PROTO)
20733/*
20734 * Called when starting to read a function line.
20735 * "sourcing_lnum" must be correct!
20736 * When skipping lines it may not actually be executed, but we won't find out
20737 * until later and we need to store the time now.
20738 */
20739 void
20740func_line_start(cookie)
20741 void *cookie;
20742{
20743 funccall_T *fcp = (funccall_T *)cookie;
20744 ufunc_T *fp = fcp->func;
20745
20746 if (fp->uf_profiling && sourcing_lnum >= 1
20747 && sourcing_lnum <= fp->uf_lines.ga_len)
20748 {
20749 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020750 /* Skip continuation lines. */
20751 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20752 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020753 fp->uf_tml_execed = FALSE;
20754 profile_start(&fp->uf_tml_start);
20755 profile_zero(&fp->uf_tml_children);
20756 profile_get_wait(&fp->uf_tml_wait);
20757 }
20758}
20759
20760/*
20761 * Called when actually executing a function line.
20762 */
20763 void
20764func_line_exec(cookie)
20765 void *cookie;
20766{
20767 funccall_T *fcp = (funccall_T *)cookie;
20768 ufunc_T *fp = fcp->func;
20769
20770 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20771 fp->uf_tml_execed = TRUE;
20772}
20773
20774/*
20775 * Called when done with a function line.
20776 */
20777 void
20778func_line_end(cookie)
20779 void *cookie;
20780{
20781 funccall_T *fcp = (funccall_T *)cookie;
20782 ufunc_T *fp = fcp->func;
20783
20784 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20785 {
20786 if (fp->uf_tml_execed)
20787 {
20788 ++fp->uf_tml_count[fp->uf_tml_idx];
20789 profile_end(&fp->uf_tml_start);
20790 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020791 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020792 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20793 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020794 }
20795 fp->uf_tml_idx = -1;
20796 }
20797}
20798#endif
20799
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800/*
20801 * Return TRUE if the currently active function should be ended, because a
20802 * return was encountered or an error occured. Used inside a ":while".
20803 */
20804 int
20805func_has_ended(cookie)
20806 void *cookie;
20807{
Bram Moolenaar33570922005-01-25 22:26:29 +000020808 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809
20810 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20811 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020812 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 || fcp->returned);
20814}
20815
20816/*
20817 * return TRUE if cookie indicates a function which "abort"s on errors.
20818 */
20819 int
20820func_has_abort(cookie)
20821 void *cookie;
20822{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020823 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824}
20825
20826#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20827typedef enum
20828{
20829 VAR_FLAVOUR_DEFAULT,
20830 VAR_FLAVOUR_SESSION,
20831 VAR_FLAVOUR_VIMINFO
20832} var_flavour_T;
20833
20834static var_flavour_T var_flavour __ARGS((char_u *varname));
20835
20836 static var_flavour_T
20837var_flavour(varname)
20838 char_u *varname;
20839{
20840 char_u *p = varname;
20841
20842 if (ASCII_ISUPPER(*p))
20843 {
20844 while (*(++p))
20845 if (ASCII_ISLOWER(*p))
20846 return VAR_FLAVOUR_SESSION;
20847 return VAR_FLAVOUR_VIMINFO;
20848 }
20849 else
20850 return VAR_FLAVOUR_DEFAULT;
20851}
20852#endif
20853
20854#if defined(FEAT_VIMINFO) || defined(PROTO)
20855/*
20856 * Restore global vars that start with a capital from the viminfo file
20857 */
20858 int
20859read_viminfo_varlist(virp, writing)
20860 vir_T *virp;
20861 int writing;
20862{
20863 char_u *tab;
20864 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020865 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866
20867 if (!writing && (find_viminfo_parameter('!') != NULL))
20868 {
20869 tab = vim_strchr(virp->vir_line + 1, '\t');
20870 if (tab != NULL)
20871 {
20872 *tab++ = '\0'; /* isolate the variable name */
20873 if (*tab == 'S') /* string var */
20874 is_string = TRUE;
20875
20876 tab = vim_strchr(tab, '\t');
20877 if (tab != NULL)
20878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 if (is_string)
20880 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020881 tv.v_type = VAR_STRING;
20882 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884 }
20885 else
20886 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020887 tv.v_type = VAR_NUMBER;
20888 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020890 set_var(virp->vir_line + 1, &tv, FALSE);
20891 if (is_string)
20892 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 }
20894 }
20895 }
20896
20897 return viminfo_readline(virp);
20898}
20899
20900/*
20901 * Write global vars that start with a capital to the viminfo file
20902 */
20903 void
20904write_viminfo_varlist(fp)
20905 FILE *fp;
20906{
Bram Moolenaar33570922005-01-25 22:26:29 +000020907 hashitem_T *hi;
20908 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020909 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020910 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020911 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020912 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020913 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914
20915 if (find_viminfo_parameter('!') == NULL)
20916 return;
20917
20918 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020919
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020920 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020921 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020923 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020925 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020926 this_var = HI2DI(hi);
20927 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020928 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020929 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020930 {
20931 case VAR_STRING: s = "STR"; break;
20932 case VAR_NUMBER: s = "NUM"; break;
20933 default: continue;
20934 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020935 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020936 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020937 if (p != NULL)
20938 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020939 vim_free(tofree);
20940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 }
20942 }
20943}
20944#endif
20945
20946#if defined(FEAT_SESSION) || defined(PROTO)
20947 int
20948store_session_globals(fd)
20949 FILE *fd;
20950{
Bram Moolenaar33570922005-01-25 22:26:29 +000020951 hashitem_T *hi;
20952 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020953 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 char_u *p, *t;
20955
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020956 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020957 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020959 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020961 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020962 this_var = HI2DI(hi);
20963 if ((this_var->di_tv.v_type == VAR_NUMBER
20964 || this_var->di_tv.v_type == VAR_STRING)
20965 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020966 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020967 /* Escape special characters with a backslash. Turn a LF and
20968 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020969 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020970 (char_u *)"\\\"\n\r");
20971 if (p == NULL) /* out of memory */
20972 break;
20973 for (t = p; *t != NUL; ++t)
20974 if (*t == '\n')
20975 *t = 'n';
20976 else if (*t == '\r')
20977 *t = 'r';
20978 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020979 this_var->di_key,
20980 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20981 : ' ',
20982 p,
20983 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20984 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020985 || put_eol(fd) == FAIL)
20986 {
20987 vim_free(p);
20988 return FAIL;
20989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 vim_free(p);
20991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 }
20993 }
20994 return OK;
20995}
20996#endif
20997
Bram Moolenaar661b1822005-07-28 22:36:45 +000020998/*
20999 * Display script name where an item was last set.
21000 * Should only be invoked when 'verbose' is non-zero.
21001 */
21002 void
21003last_set_msg(scriptID)
21004 scid_T scriptID;
21005{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021006 char_u *p;
21007
Bram Moolenaar661b1822005-07-28 22:36:45 +000021008 if (scriptID != 0)
21009 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021010 p = home_replace_save(NULL, get_scriptname(scriptID));
21011 if (p != NULL)
21012 {
21013 verbose_enter();
21014 MSG_PUTS(_("\n\tLast set from "));
21015 MSG_PUTS(p);
21016 vim_free(p);
21017 verbose_leave();
21018 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021019 }
21020}
21021
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022#endif /* FEAT_EVAL */
21023
21024#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21025
21026
21027#ifdef WIN3264
21028/*
21029 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21030 */
21031static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21032static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21033static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21034
21035/*
21036 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021037 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 */
21039 static int
21040get_short_pathname(fnamep, bufp, fnamelen)
21041 char_u **fnamep;
21042 char_u **bufp;
21043 int *fnamelen;
21044{
21045 int l,len;
21046 char_u *newbuf;
21047
21048 len = *fnamelen;
21049
21050 l = GetShortPathName(*fnamep, *fnamep, len);
21051 if (l > len - 1)
21052 {
21053 /* If that doesn't work (not enough space), then save the string
21054 * and try again with a new buffer big enough
21055 */
21056 newbuf = vim_strnsave(*fnamep, l);
21057 if (newbuf == NULL)
21058 return 0;
21059
21060 vim_free(*bufp);
21061 *fnamep = *bufp = newbuf;
21062
21063 l = GetShortPathName(*fnamep,*fnamep,l+1);
21064
21065 /* Really should always succeed, as the buffer is big enough */
21066 }
21067
21068 *fnamelen = l;
21069 return 1;
21070}
21071
21072/*
21073 * Create a short path name. Returns the length of the buffer it needs.
21074 * Doesn't copy over the end of the buffer passed in.
21075 */
21076 static int
21077shortpath_for_invalid_fname(fname, bufp, fnamelen)
21078 char_u **fname;
21079 char_u **bufp;
21080 int *fnamelen;
21081{
21082 char_u *s, *p, *pbuf2, *pbuf3;
21083 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021084 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085
21086 /* Make a copy */
21087 len2 = *fnamelen;
21088 pbuf2 = vim_strnsave(*fname, len2);
21089 pbuf3 = NULL;
21090
21091 s = pbuf2 + len2 - 1; /* Find the end */
21092 slen = 1;
21093 plen = len2;
21094
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021095 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 {
21097 --s;
21098 ++slen;
21099 --plen;
21100 }
21101
21102 do
21103 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021104 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021105 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 {
21107 --s;
21108 ++slen;
21109 --plen;
21110 }
21111 if (s <= pbuf2)
21112 break;
21113
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021114 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 ch = *s;
21116 *s = 0; /* get_short_pathname requires a null-terminated string */
21117
21118 /* Try it in situ */
21119 p = pbuf2;
21120 if (!get_short_pathname(&p, &pbuf3, &plen))
21121 {
21122 vim_free(pbuf2);
21123 return -1;
21124 }
21125 *s = ch; /* Preserve the string */
21126 } while (plen == 0);
21127
21128 if (plen > 0)
21129 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021130 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 *fnamelen = len = plen + slen;
21132 vim_free(*bufp);
21133 if (len > len2)
21134 {
21135 /* If there's not enough space in the currently allocated string,
21136 * then copy it to a buffer big enough.
21137 */
21138 *fname= *bufp = vim_strnsave(p, len);
21139 if (*fname == NULL)
21140 return -1;
21141 }
21142 else
21143 {
21144 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21145 *fname = *bufp = pbuf2;
21146 if (p != pbuf2)
21147 strncpy(*fname, p, plen);
21148 pbuf2 = NULL;
21149 }
21150 /* Concat the next bit */
21151 strncpy(*fname + plen, s, slen);
21152 (*fname)[len] = '\0';
21153 }
21154 vim_free(pbuf3);
21155 vim_free(pbuf2);
21156 return 0;
21157}
21158
21159/*
21160 * Get a pathname for a partial path.
21161 */
21162 static int
21163shortpath_for_partial(fnamep, bufp, fnamelen)
21164 char_u **fnamep;
21165 char_u **bufp;
21166 int *fnamelen;
21167{
21168 int sepcount, len, tflen;
21169 char_u *p;
21170 char_u *pbuf, *tfname;
21171 int hasTilde;
21172
21173 /* Count up the path seperators from the RHS.. so we know which part
21174 * of the path to return.
21175 */
21176 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021177 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 if (vim_ispathsep(*p))
21179 ++sepcount;
21180
21181 /* Need full path first (use expand_env() to remove a "~/") */
21182 hasTilde = (**fnamep == '~');
21183 if (hasTilde)
21184 pbuf = tfname = expand_env_save(*fnamep);
21185 else
21186 pbuf = tfname = FullName_save(*fnamep, FALSE);
21187
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021188 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189
21190 if (!get_short_pathname(&tfname, &pbuf, &len))
21191 return -1;
21192
21193 if (len == 0)
21194 {
21195 /* Don't have a valid filename, so shorten the rest of the
21196 * path if we can. This CAN give us invalid 8.3 filenames, but
21197 * there's not a lot of point in guessing what it might be.
21198 */
21199 len = tflen;
21200 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21201 return -1;
21202 }
21203
21204 /* Count the paths backward to find the beginning of the desired string. */
21205 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021206 {
21207#ifdef FEAT_MBYTE
21208 if (has_mbyte)
21209 p -= mb_head_off(tfname, p);
21210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 if (vim_ispathsep(*p))
21212 {
21213 if (sepcount == 0 || (hasTilde && sepcount == 1))
21214 break;
21215 else
21216 sepcount --;
21217 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 if (hasTilde)
21220 {
21221 --p;
21222 if (p >= tfname)
21223 *p = '~';
21224 else
21225 return -1;
21226 }
21227 else
21228 ++p;
21229
21230 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21231 vim_free(*bufp);
21232 *fnamelen = (int)STRLEN(p);
21233 *bufp = pbuf;
21234 *fnamep = p;
21235
21236 return 0;
21237}
21238#endif /* WIN3264 */
21239
21240/*
21241 * Adjust a filename, according to a string of modifiers.
21242 * *fnamep must be NUL terminated when called. When returning, the length is
21243 * determined by *fnamelen.
21244 * Returns valid flags.
21245 * When there is an error, *fnamep is set to NULL.
21246 */
21247 int
21248modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21249 char_u *src; /* string with modifiers */
21250 int *usedlen; /* characters after src that are used */
21251 char_u **fnamep; /* file name so far */
21252 char_u **bufp; /* buffer for allocated file name or NULL */
21253 int *fnamelen; /* length of fnamep */
21254{
21255 int valid = 0;
21256 char_u *tail;
21257 char_u *s, *p, *pbuf;
21258 char_u dirname[MAXPATHL];
21259 int c;
21260 int has_fullname = 0;
21261#ifdef WIN3264
21262 int has_shortname = 0;
21263#endif
21264
21265repeat:
21266 /* ":p" - full path/file_name */
21267 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21268 {
21269 has_fullname = 1;
21270
21271 valid |= VALID_PATH;
21272 *usedlen += 2;
21273
21274 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21275 if ((*fnamep)[0] == '~'
21276#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21277 && ((*fnamep)[1] == '/'
21278# ifdef BACKSLASH_IN_FILENAME
21279 || (*fnamep)[1] == '\\'
21280# endif
21281 || (*fnamep)[1] == NUL)
21282
21283#endif
21284 )
21285 {
21286 *fnamep = expand_env_save(*fnamep);
21287 vim_free(*bufp); /* free any allocated file name */
21288 *bufp = *fnamep;
21289 if (*fnamep == NULL)
21290 return -1;
21291 }
21292
21293 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021294 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 {
21296 if (vim_ispathsep(*p)
21297 && p[1] == '.'
21298 && (p[2] == NUL
21299 || vim_ispathsep(p[2])
21300 || (p[2] == '.'
21301 && (p[3] == NUL || vim_ispathsep(p[3])))))
21302 break;
21303 }
21304
21305 /* FullName_save() is slow, don't use it when not needed. */
21306 if (*p != NUL || !vim_isAbsName(*fnamep))
21307 {
21308 *fnamep = FullName_save(*fnamep, *p != NUL);
21309 vim_free(*bufp); /* free any allocated file name */
21310 *bufp = *fnamep;
21311 if (*fnamep == NULL)
21312 return -1;
21313 }
21314
21315 /* Append a path separator to a directory. */
21316 if (mch_isdir(*fnamep))
21317 {
21318 /* Make room for one or two extra characters. */
21319 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21320 vim_free(*bufp); /* free any allocated file name */
21321 *bufp = *fnamep;
21322 if (*fnamep == NULL)
21323 return -1;
21324 add_pathsep(*fnamep);
21325 }
21326 }
21327
21328 /* ":." - path relative to the current directory */
21329 /* ":~" - path relative to the home directory */
21330 /* ":8" - shortname path - postponed till after */
21331 while (src[*usedlen] == ':'
21332 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21333 {
21334 *usedlen += 2;
21335 if (c == '8')
21336 {
21337#ifdef WIN3264
21338 has_shortname = 1; /* Postpone this. */
21339#endif
21340 continue;
21341 }
21342 pbuf = NULL;
21343 /* Need full path first (use expand_env() to remove a "~/") */
21344 if (!has_fullname)
21345 {
21346 if (c == '.' && **fnamep == '~')
21347 p = pbuf = expand_env_save(*fnamep);
21348 else
21349 p = pbuf = FullName_save(*fnamep, FALSE);
21350 }
21351 else
21352 p = *fnamep;
21353
21354 has_fullname = 0;
21355
21356 if (p != NULL)
21357 {
21358 if (c == '.')
21359 {
21360 mch_dirname(dirname, MAXPATHL);
21361 s = shorten_fname(p, dirname);
21362 if (s != NULL)
21363 {
21364 *fnamep = s;
21365 if (pbuf != NULL)
21366 {
21367 vim_free(*bufp); /* free any allocated file name */
21368 *bufp = pbuf;
21369 pbuf = NULL;
21370 }
21371 }
21372 }
21373 else
21374 {
21375 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21376 /* Only replace it when it starts with '~' */
21377 if (*dirname == '~')
21378 {
21379 s = vim_strsave(dirname);
21380 if (s != NULL)
21381 {
21382 *fnamep = s;
21383 vim_free(*bufp);
21384 *bufp = s;
21385 }
21386 }
21387 }
21388 vim_free(pbuf);
21389 }
21390 }
21391
21392 tail = gettail(*fnamep);
21393 *fnamelen = (int)STRLEN(*fnamep);
21394
21395 /* ":h" - head, remove "/file_name", can be repeated */
21396 /* Don't remove the first "/" or "c:\" */
21397 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21398 {
21399 valid |= VALID_HEAD;
21400 *usedlen += 2;
21401 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021402 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021403 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 *fnamelen = (int)(tail - *fnamep);
21405#ifdef VMS
21406 if (*fnamelen > 0)
21407 *fnamelen += 1; /* the path separator is part of the path */
21408#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021409 if (*fnamelen == 0)
21410 {
21411 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21412 p = vim_strsave((char_u *)".");
21413 if (p == NULL)
21414 return -1;
21415 vim_free(*bufp);
21416 *bufp = *fnamep = tail = p;
21417 *fnamelen = 1;
21418 }
21419 else
21420 {
21421 while (tail > s && !after_pathsep(s, tail))
21422 mb_ptr_back(*fnamep, tail);
21423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 }
21425
21426 /* ":8" - shortname */
21427 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21428 {
21429 *usedlen += 2;
21430#ifdef WIN3264
21431 has_shortname = 1;
21432#endif
21433 }
21434
21435#ifdef WIN3264
21436 /* Check shortname after we have done 'heads' and before we do 'tails'
21437 */
21438 if (has_shortname)
21439 {
21440 pbuf = NULL;
21441 /* Copy the string if it is shortened by :h */
21442 if (*fnamelen < (int)STRLEN(*fnamep))
21443 {
21444 p = vim_strnsave(*fnamep, *fnamelen);
21445 if (p == 0)
21446 return -1;
21447 vim_free(*bufp);
21448 *bufp = *fnamep = p;
21449 }
21450
21451 /* Split into two implementations - makes it easier. First is where
21452 * there isn't a full name already, second is where there is.
21453 */
21454 if (!has_fullname && !vim_isAbsName(*fnamep))
21455 {
21456 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21457 return -1;
21458 }
21459 else
21460 {
21461 int l;
21462
21463 /* Simple case, already have the full-name
21464 * Nearly always shorter, so try first time. */
21465 l = *fnamelen;
21466 if (!get_short_pathname(fnamep, bufp, &l))
21467 return -1;
21468
21469 if (l == 0)
21470 {
21471 /* Couldn't find the filename.. search the paths.
21472 */
21473 l = *fnamelen;
21474 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21475 return -1;
21476 }
21477 *fnamelen = l;
21478 }
21479 }
21480#endif /* WIN3264 */
21481
21482 /* ":t" - tail, just the basename */
21483 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21484 {
21485 *usedlen += 2;
21486 *fnamelen -= (int)(tail - *fnamep);
21487 *fnamep = tail;
21488 }
21489
21490 /* ":e" - extension, can be repeated */
21491 /* ":r" - root, without extension, can be repeated */
21492 while (src[*usedlen] == ':'
21493 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21494 {
21495 /* find a '.' in the tail:
21496 * - for second :e: before the current fname
21497 * - otherwise: The last '.'
21498 */
21499 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21500 s = *fnamep - 2;
21501 else
21502 s = *fnamep + *fnamelen - 1;
21503 for ( ; s > tail; --s)
21504 if (s[0] == '.')
21505 break;
21506 if (src[*usedlen + 1] == 'e') /* :e */
21507 {
21508 if (s > tail)
21509 {
21510 *fnamelen += (int)(*fnamep - (s + 1));
21511 *fnamep = s + 1;
21512#ifdef VMS
21513 /* cut version from the extension */
21514 s = *fnamep + *fnamelen - 1;
21515 for ( ; s > *fnamep; --s)
21516 if (s[0] == ';')
21517 break;
21518 if (s > *fnamep)
21519 *fnamelen = s - *fnamep;
21520#endif
21521 }
21522 else if (*fnamep <= tail)
21523 *fnamelen = 0;
21524 }
21525 else /* :r */
21526 {
21527 if (s > tail) /* remove one extension */
21528 *fnamelen = (int)(s - *fnamep);
21529 }
21530 *usedlen += 2;
21531 }
21532
21533 /* ":s?pat?foo?" - substitute */
21534 /* ":gs?pat?foo?" - global substitute */
21535 if (src[*usedlen] == ':'
21536 && (src[*usedlen + 1] == 's'
21537 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21538 {
21539 char_u *str;
21540 char_u *pat;
21541 char_u *sub;
21542 int sep;
21543 char_u *flags;
21544 int didit = FALSE;
21545
21546 flags = (char_u *)"";
21547 s = src + *usedlen + 2;
21548 if (src[*usedlen + 1] == 'g')
21549 {
21550 flags = (char_u *)"g";
21551 ++s;
21552 }
21553
21554 sep = *s++;
21555 if (sep)
21556 {
21557 /* find end of pattern */
21558 p = vim_strchr(s, sep);
21559 if (p != NULL)
21560 {
21561 pat = vim_strnsave(s, (int)(p - s));
21562 if (pat != NULL)
21563 {
21564 s = p + 1;
21565 /* find end of substitution */
21566 p = vim_strchr(s, sep);
21567 if (p != NULL)
21568 {
21569 sub = vim_strnsave(s, (int)(p - s));
21570 str = vim_strnsave(*fnamep, *fnamelen);
21571 if (sub != NULL && str != NULL)
21572 {
21573 *usedlen = (int)(p + 1 - src);
21574 s = do_string_sub(str, pat, sub, flags);
21575 if (s != NULL)
21576 {
21577 *fnamep = s;
21578 *fnamelen = (int)STRLEN(s);
21579 vim_free(*bufp);
21580 *bufp = s;
21581 didit = TRUE;
21582 }
21583 }
21584 vim_free(sub);
21585 vim_free(str);
21586 }
21587 vim_free(pat);
21588 }
21589 }
21590 /* after using ":s", repeat all the modifiers */
21591 if (didit)
21592 goto repeat;
21593 }
21594 }
21595
21596 return valid;
21597}
21598
21599/*
21600 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21601 * "flags" can be "g" to do a global substitute.
21602 * Returns an allocated string, NULL for error.
21603 */
21604 char_u *
21605do_string_sub(str, pat, sub, flags)
21606 char_u *str;
21607 char_u *pat;
21608 char_u *sub;
21609 char_u *flags;
21610{
21611 int sublen;
21612 regmatch_T regmatch;
21613 int i;
21614 int do_all;
21615 char_u *tail;
21616 garray_T ga;
21617 char_u *ret;
21618 char_u *save_cpo;
21619
21620 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21621 save_cpo = p_cpo;
21622 p_cpo = (char_u *)"";
21623
21624 ga_init2(&ga, 1, 200);
21625
21626 do_all = (flags[0] == 'g');
21627
21628 regmatch.rm_ic = p_ic;
21629 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21630 if (regmatch.regprog != NULL)
21631 {
21632 tail = str;
21633 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21634 {
21635 /*
21636 * Get some space for a temporary buffer to do the substitution
21637 * into. It will contain:
21638 * - The text up to where the match is.
21639 * - The substituted text.
21640 * - The text after the match.
21641 */
21642 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21643 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21644 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21645 {
21646 ga_clear(&ga);
21647 break;
21648 }
21649
21650 /* copy the text up to where the match is */
21651 i = (int)(regmatch.startp[0] - tail);
21652 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21653 /* add the substituted text */
21654 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21655 + ga.ga_len + i, TRUE, TRUE, FALSE);
21656 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 /* avoid getting stuck on a match with an empty string */
21658 if (tail == regmatch.endp[0])
21659 {
21660 if (*tail == NUL)
21661 break;
21662 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21663 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 }
21665 else
21666 {
21667 tail = regmatch.endp[0];
21668 if (*tail == NUL)
21669 break;
21670 }
21671 if (!do_all)
21672 break;
21673 }
21674
21675 if (ga.ga_data != NULL)
21676 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21677
21678 vim_free(regmatch.regprog);
21679 }
21680
21681 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21682 ga_clear(&ga);
21683 p_cpo = save_cpo;
21684
21685 return ret;
21686}
21687
21688#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */