blob: bcf9b79d9ca0dedc1e309788e8088069c4dbf8f8 [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 Moolenaar18081e32008-02-20 19:11:07 +0000535static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000536static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000537static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000540static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000541static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000548static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000562static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000568static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000584static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000585static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000586static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000588static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000592#ifdef vim_mkdir
593static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
594#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000598static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000600static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000601static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000603static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000604static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000617static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000619static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000626static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000627static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000628static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000629static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000631static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000633static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000636static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000637static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000640static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641#ifdef HAVE_STRFTIME
642static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
643#endif
644static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000655static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000657static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000658static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000659static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000660static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000661static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000663static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000677static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000680static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000682static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000683static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static int get_env_len __ARGS((char_u **arg));
685static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000686static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000687static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
688#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
689#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
690 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000691static 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 +0000692static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000693static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000694static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
695static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static typval_T *alloc_tv __ARGS((void));
697static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void init_tv __ARGS((typval_T *varp));
699static long get_tv_number __ARGS((typval_T *varp));
700static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000701static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static char_u *get_tv_string __ARGS((typval_T *varp));
703static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000704static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000706static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
708static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
709static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000710static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
711static 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 +0000712static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
713static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000714static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000715static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000717static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
719static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
720static int eval_fname_script __ARGS((char_u *p));
721static int eval_fname_sid __ARGS((char_u *p));
722static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static ufunc_T *find_func __ARGS((char_u *name));
724static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000725static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000726#ifdef FEAT_PROFILE
727static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000728static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
729static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
730static int
731# ifdef __BORLANDC__
732 _RTLENTRYF
733# endif
734 prof_total_cmp __ARGS((const void *s1, const void *s2));
735static int
736# ifdef __BORLANDC__
737 _RTLENTRYF
738# endif
739 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000741static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000742static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000743static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void func_free __ARGS((ufunc_T *fp));
745static void func_unref __ARGS((char_u *name));
746static void func_ref __ARGS((char_u *name));
747static 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));
748static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000749static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
750static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000751static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000752static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000753static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000755/* Character used as separated in autoload function/variable names. */
756#define AUTOLOAD_CHAR '#'
757
Bram Moolenaar33570922005-01-25 22:26:29 +0000758/*
759 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000760 */
761 void
762eval_init()
763{
Bram Moolenaar33570922005-01-25 22:26:29 +0000764 int i;
765 struct vimvar *p;
766
767 init_var_dict(&globvardict, &globvars_var);
768 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000769 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000770 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000771
772 for (i = 0; i < VV_LEN; ++i)
773 {
774 p = &vimvars[i];
775 STRCPY(p->vv_di.di_key, p->vv_name);
776 if (p->vv_flags & VV_RO)
777 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
778 else if (p->vv_flags & VV_RO_SBX)
779 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
780 else
781 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000782
783 /* add to v: scope dict, unless the value is not always available */
784 if (p->vv_type != VAR_UNKNOWN)
785 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000787 /* add to compat scope dict */
788 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000789 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000790}
791
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000792#if defined(EXITFREE) || defined(PROTO)
793 void
794eval_clear()
795{
796 int i;
797 struct vimvar *p;
798
799 for (i = 0; i < VV_LEN; ++i)
800 {
801 p = &vimvars[i];
802 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000803 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000804 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000805 p->vv_di.di_tv.vval.v_string = NULL;
806 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000807 }
808 hash_clear(&vimvarht);
809 hash_clear(&compat_hashtab);
810
811 /* script-local variables */
812 for (i = 1; i <= ga_scripts.ga_len; ++i)
813 vars_clear(&SCRIPT_VARS(i));
814 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000815 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000816
817 /* global variables */
818 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000820 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000821 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000822 hash_clear(&func_hashtab);
823
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000824 /* autoloaded script names */
825 ga_clear_strings(&ga_loaded);
826
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000827 /* unreferenced lists and dicts */
828 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000829}
830#endif
831
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * Return the name of the executed function.
834 */
835 char_u *
836func_name(cookie)
837 void *cookie;
838{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000839 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841
842/*
843 * Return the address holding the next breakpoint line for a funccall cookie.
844 */
845 linenr_T *
846func_breakpoint(cookie)
847 void *cookie;
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
852/*
853 * Return the address holding the debug tick for a funccall cookie.
854 */
855 int *
856func_dbg_tick(cookie)
857 void *cookie;
858{
Bram Moolenaar33570922005-01-25 22:26:29 +0000859 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860}
861
862/*
863 * Return the nesting level for a funccall cookie.
864 */
865 int
866func_level(cookie)
867 void *cookie;
868{
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870}
871
872/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000873funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875/*
876 * Return TRUE when a function was ended by a ":return" command.
877 */
878 int
879current_func_returned()
880{
881 return current_funccal->returned;
882}
883
884
885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 * Set an internal variable to a string value. Creates the variable if it does
887 * not already exist.
888 */
889 void
890set_internal_string_var(name, value)
891 char_u *name;
892 char_u *value;
893{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000894 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 val = vim_strsave(value);
898 if (val != NULL)
899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000900 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000901 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000903 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000904 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
906 }
907}
908
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000909static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000910static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000911static char_u *redir_endp = NULL;
912static char_u *redir_varname = NULL;
913
914/*
915 * Start recording command output to a variable
916 * Returns OK if successfully completed the setup. FAIL otherwise.
917 */
918 int
919var_redir_start(name, append)
920 char_u *name;
921 int append; /* append to an existing variable */
922{
923 int save_emsg;
924 int err;
925 typval_T tv;
926
927 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000928 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000929 {
930 EMSG(_(e_invarg));
931 return FAIL;
932 }
933
934 redir_varname = vim_strsave(name);
935 if (redir_varname == NULL)
936 return FAIL;
937
938 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
939 if (redir_lval == NULL)
940 {
941 var_redir_stop();
942 return FAIL;
943 }
944
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000945 /* The output is stored in growarray "redir_ga" until redirection ends. */
946 ga_init2(&redir_ga, (int)sizeof(char), 500);
947
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000948 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000949 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
950 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000951 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
952 {
953 if (redir_endp != NULL && *redir_endp != NUL)
954 /* Trailing characters are present after the variable name */
955 EMSG(_(e_trailing));
956 else
957 EMSG(_(e_invarg));
958 var_redir_stop();
959 return FAIL;
960 }
961
962 /* check if we can write to the variable: set it to or append an empty
963 * string */
964 save_emsg = did_emsg;
965 did_emsg = FALSE;
966 tv.v_type = VAR_STRING;
967 tv.vval.v_string = (char_u *)"";
968 if (append)
969 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
970 else
971 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
972 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000973 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000974 if (err)
975 {
976 var_redir_stop();
977 return FAIL;
978 }
979 if (redir_lval->ll_newkey != NULL)
980 {
981 /* Dictionary item was created, don't do it again. */
982 vim_free(redir_lval->ll_newkey);
983 redir_lval->ll_newkey = NULL;
984 }
985
986 return OK;
987}
988
989/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000990 * Append "value[value_len]" to the variable set by var_redir_start().
991 * The actual appending is postponed until redirection ends, because the value
992 * appended may in fact be the string we write to, changing it may cause freed
993 * memory to be used:
994 * :redir => foo
995 * :let foo
996 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000997 */
998 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000999var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001000 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001001 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001002{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001003 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001004
1005 if (redir_lval == NULL)
1006 return;
1007
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001008 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001009 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001011 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001013 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001014 {
1015 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001016 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017 }
1018 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020}
1021
1022/*
1023 * Stop redirecting command output to a variable.
1024 */
1025 void
1026var_redir_stop()
1027{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001028 typval_T tv;
1029
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001030 if (redir_lval != NULL)
1031 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001032 /* Append the trailing NUL. */
1033 ga_append(&redir_ga, NUL);
1034
1035 /* Assign the text to the variable. */
1036 tv.v_type = VAR_STRING;
1037 tv.vval.v_string = redir_ga.ga_data;
1038 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1039 vim_free(tv.vval.v_string);
1040
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 clear_lval(redir_lval);
1042 vim_free(redir_lval);
1043 redir_lval = NULL;
1044 }
1045 vim_free(redir_varname);
1046 redir_varname = NULL;
1047}
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049# if defined(FEAT_MBYTE) || defined(PROTO)
1050 int
1051eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1052 char_u *enc_from;
1053 char_u *enc_to;
1054 char_u *fname_from;
1055 char_u *fname_to;
1056{
1057 int err = FALSE;
1058
1059 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1060 set_vim_var_string(VV_CC_TO, enc_to, -1);
1061 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1062 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1063 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1064 err = TRUE;
1065 set_vim_var_string(VV_CC_FROM, NULL, -1);
1066 set_vim_var_string(VV_CC_TO, NULL, -1);
1067 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1068 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1069
1070 if (err)
1071 return FAIL;
1072 return OK;
1073}
1074# endif
1075
1076# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1077 int
1078eval_printexpr(fname, args)
1079 char_u *fname;
1080 char_u *args;
1081{
1082 int err = FALSE;
1083
1084 set_vim_var_string(VV_FNAME_IN, fname, -1);
1085 set_vim_var_string(VV_CMDARG, args, -1);
1086 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1087 err = TRUE;
1088 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1089 set_vim_var_string(VV_CMDARG, NULL, -1);
1090
1091 if (err)
1092 {
1093 mch_remove(fname);
1094 return FAIL;
1095 }
1096 return OK;
1097}
1098# endif
1099
1100# if defined(FEAT_DIFF) || defined(PROTO)
1101 void
1102eval_diff(origfile, newfile, outfile)
1103 char_u *origfile;
1104 char_u *newfile;
1105 char_u *outfile;
1106{
1107 int err = FALSE;
1108
1109 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1110 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1111 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1112 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1113 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1114 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1115 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1116}
1117
1118 void
1119eval_patch(origfile, difffile, outfile)
1120 char_u *origfile;
1121 char_u *difffile;
1122 char_u *outfile;
1123{
1124 int err;
1125
1126 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1127 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1128 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1129 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1130 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1131 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1132 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1133}
1134# endif
1135
1136/*
1137 * Top level evaluation function, returning a boolean.
1138 * Sets "error" to TRUE if there was an error.
1139 * Return TRUE or FALSE.
1140 */
1141 int
1142eval_to_bool(arg, error, nextcmd, skip)
1143 char_u *arg;
1144 int *error;
1145 char_u **nextcmd;
1146 int skip; /* only parse, don't execute */
1147{
Bram Moolenaar33570922005-01-25 22:26:29 +00001148 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 int retval = FALSE;
1150
1151 if (skip)
1152 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 else
1156 {
1157 *error = FALSE;
1158 if (!skip)
1159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001160 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001161 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163 }
1164 if (skip)
1165 --emsg_skip;
1166
1167 return retval;
1168}
1169
1170/*
1171 * Top level evaluation function, returning a string. If "skip" is TRUE,
1172 * only parsing to "nextcmd" is done, without reporting errors. Return
1173 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1174 */
1175 char_u *
1176eval_to_string_skip(arg, nextcmd, skip)
1177 char_u *arg;
1178 char_u **nextcmd;
1179 int skip; /* only parse, don't execute */
1180{
Bram Moolenaar33570922005-01-25 22:26:29 +00001181 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 char_u *retval;
1183
1184 if (skip)
1185 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001186 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 retval = NULL;
1188 else
1189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 retval = vim_strsave(get_tv_string(&tv));
1191 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193 if (skip)
1194 --emsg_skip;
1195
1196 return retval;
1197}
1198
1199/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001200 * Skip over an expression at "*pp".
1201 * Return FAIL for an error, OK otherwise.
1202 */
1203 int
1204skip_expr(pp)
1205 char_u **pp;
1206{
Bram Moolenaar33570922005-01-25 22:26:29 +00001207 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001208
1209 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001211}
1212
1213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 * Top level evaluation function, returning a string.
1215 * Return pointer to allocated memory, or NULL for failure.
1216 */
1217 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001218eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 char_u *arg;
1220 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001221 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222{
Bram Moolenaar33570922005-01-25 22:26:29 +00001223 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001225 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001227 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 retval = NULL;
1229 else
1230 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001231 if (dolist && tv.v_type == VAR_LIST)
1232 {
1233 ga_init2(&ga, (int)sizeof(char), 80);
1234 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1235 ga_append(&ga, NUL);
1236 retval = (char_u *)ga.ga_data;
1237 }
1238 else
1239 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001240 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242
1243 return retval;
1244}
1245
1246/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001247 * Call eval_to_string() without using current local variables and using
1248 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 */
1250 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001251eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 char_u *arg;
1253 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001254 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255{
1256 char_u *retval;
1257 void *save_funccalp;
1258
1259 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001260 if (use_sandbox)
1261 ++sandbox;
1262 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001263 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001264 if (use_sandbox)
1265 --sandbox;
1266 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 restore_funccal(save_funccalp);
1268 return retval;
1269}
1270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271/*
1272 * Top level evaluation function, returning a number.
1273 * Evaluates "expr" silently.
1274 * Returns -1 for an error.
1275 */
1276 int
1277eval_to_number(expr)
1278 char_u *expr;
1279{
Bram Moolenaar33570922005-01-25 22:26:29 +00001280 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001282 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
1284 ++emsg_off;
1285
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001286 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 retval = -1;
1288 else
1289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001290 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001291 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293 --emsg_off;
1294
1295 return retval;
1296}
1297
Bram Moolenaara40058a2005-07-11 22:42:07 +00001298/*
1299 * Prepare v: variable "idx" to be used.
1300 * Save the current typeval in "save_tv".
1301 * When not used yet add the variable to the v: hashtable.
1302 */
1303 static void
1304prepare_vimvar(idx, save_tv)
1305 int idx;
1306 typval_T *save_tv;
1307{
1308 *save_tv = vimvars[idx].vv_tv;
1309 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1310 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1311}
1312
1313/*
1314 * Restore v: variable "idx" to typeval "save_tv".
1315 * When no longer defined, remove the variable from the v: hashtable.
1316 */
1317 static void
1318restore_vimvar(idx, save_tv)
1319 int idx;
1320 typval_T *save_tv;
1321{
1322 hashitem_T *hi;
1323
Bram Moolenaara40058a2005-07-11 22:42:07 +00001324 vimvars[idx].vv_tv = *save_tv;
1325 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1326 {
1327 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1328 if (HASHITEM_EMPTY(hi))
1329 EMSG2(_(e_intern2), "restore_vimvar()");
1330 else
1331 hash_remove(&vimvarht, hi);
1332 }
1333}
1334
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001335#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001336/*
1337 * Evaluate an expression to a list with suggestions.
1338 * For the "expr:" part of 'spellsuggest'.
1339 */
1340 list_T *
1341eval_spell_expr(badword, expr)
1342 char_u *badword;
1343 char_u *expr;
1344{
1345 typval_T save_val;
1346 typval_T rettv;
1347 list_T *list = NULL;
1348 char_u *p = skipwhite(expr);
1349
1350 /* Set "v:val" to the bad word. */
1351 prepare_vimvar(VV_VAL, &save_val);
1352 vimvars[VV_VAL].vv_type = VAR_STRING;
1353 vimvars[VV_VAL].vv_str = badword;
1354 if (p_verbose == 0)
1355 ++emsg_off;
1356
1357 if (eval1(&p, &rettv, TRUE) == OK)
1358 {
1359 if (rettv.v_type != VAR_LIST)
1360 clear_tv(&rettv);
1361 else
1362 list = rettv.vval.v_list;
1363 }
1364
1365 if (p_verbose == 0)
1366 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001367 restore_vimvar(VV_VAL, &save_val);
1368
1369 return list;
1370}
1371
1372/*
1373 * "list" is supposed to contain two items: a word and a number. Return the
1374 * word in "pp" and the number as the return value.
1375 * Return -1 if anything isn't right.
1376 * Used to get the good word and score from the eval_spell_expr() result.
1377 */
1378 int
1379get_spellword(list, pp)
1380 list_T *list;
1381 char_u **pp;
1382{
1383 listitem_T *li;
1384
1385 li = list->lv_first;
1386 if (li == NULL)
1387 return -1;
1388 *pp = get_tv_string(&li->li_tv);
1389
1390 li = li->li_next;
1391 if (li == NULL)
1392 return -1;
1393 return get_tv_number(&li->li_tv);
1394}
1395#endif
1396
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001397/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001398 * Top level evaluation function.
1399 * Returns an allocated typval_T with the result.
1400 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001401 */
1402 typval_T *
1403eval_expr(arg, nextcmd)
1404 char_u *arg;
1405 char_u **nextcmd;
1406{
1407 typval_T *tv;
1408
1409 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001410 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001411 {
1412 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001413 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001414 }
1415
1416 return tv;
1417}
1418
1419
Bram Moolenaar4f688582007-07-24 12:34:30 +00001420#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1421 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001423 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001427 static int
1428call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 char_u *func;
1430 int argc;
1431 char_u **argv;
1432 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
Bram Moolenaar33570922005-01-25 22:26:29 +00001435 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 long n;
1437 int len;
1438 int i;
1439 int doesrange;
1440 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001443 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
1447 for (i = 0; i < argc; i++)
1448 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001449 /* Pass a NULL or empty argument as an empty string */
1450 if (argv[i] == NULL || *argv[i] == NUL)
1451 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001452 argvars[i].v_type = VAR_STRING;
1453 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001454 continue;
1455 }
1456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 /* Recognize a number argument, the others must be strings. */
1458 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1459 if (len != 0 && len == (int)STRLEN(argv[i]))
1460 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001461 argvars[i].v_type = VAR_NUMBER;
1462 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
1464 else
1465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001466 argvars[i].v_type = VAR_STRING;
1467 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
1469 }
1470
1471 if (safe)
1472 {
1473 save_funccalp = save_funccal();
1474 ++sandbox;
1475 }
1476
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1478 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (safe)
1482 {
1483 --sandbox;
1484 restore_funccal(save_funccalp);
1485 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001486 vim_free(argvars);
1487
1488 if (ret == FAIL)
1489 clear_tv(rettv);
1490
1491 return ret;
1492}
1493
Bram Moolenaar4f688582007-07-24 12:34:30 +00001494# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001495/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001496 * Call vimL function "func" and return the result as a string.
1497 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001498 * Uses argv[argc] for the function arguments.
1499 */
1500 void *
1501call_func_retstr(func, argc, argv, safe)
1502 char_u *func;
1503 int argc;
1504 char_u **argv;
1505 int safe; /* use the sandbox */
1506{
1507 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001508 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001509
1510 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1511 return NULL;
1512
1513 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001514 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 return retval;
1516}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001517# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001518
Bram Moolenaar4f688582007-07-24 12:34:30 +00001519# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001520/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001521 * Call vimL function "func" and return the result as a number.
1522 * Returns -1 when calling the function fails.
1523 * Uses argv[argc] for the function arguments.
1524 */
1525 long
1526call_func_retnr(func, argc, argv, safe)
1527 char_u *func;
1528 int argc;
1529 char_u **argv;
1530 int safe; /* use the sandbox */
1531{
1532 typval_T rettv;
1533 long retval;
1534
1535 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1536 return -1;
1537
1538 retval = get_tv_number_chk(&rettv, NULL);
1539 clear_tv(&rettv);
1540 return retval;
1541}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001542# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001543
1544/*
1545 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001546 * Uses argv[argc] for the function arguments.
1547 */
1548 void *
1549call_func_retlist(func, argc, argv, safe)
1550 char_u *func;
1551 int argc;
1552 char_u **argv;
1553 int safe; /* use the sandbox */
1554{
1555 typval_T rettv;
1556
1557 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1558 return NULL;
1559
1560 if (rettv.v_type != VAR_LIST)
1561 {
1562 clear_tv(&rettv);
1563 return NULL;
1564 }
1565
1566 return rettv.vval.v_list;
1567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568#endif
1569
Bram Moolenaar4f688582007-07-24 12:34:30 +00001570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571/*
1572 * Save the current function call pointer, and set it to NULL.
1573 * Used when executing autocommands and for ":source".
1574 */
1575 void *
1576save_funccal()
1577{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001578 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 current_funccal = NULL;
1581 return (void *)fc;
1582}
1583
1584 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001585restore_funccal(vfc)
1586 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001588 funccall_T *fc = (funccall_T *)vfc;
1589
1590 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591}
1592
Bram Moolenaar05159a02005-02-26 23:04:13 +00001593#if defined(FEAT_PROFILE) || defined(PROTO)
1594/*
1595 * Prepare profiling for entering a child or something else that is not
1596 * counted for the script/function itself.
1597 * Should always be called in pair with prof_child_exit().
1598 */
1599 void
1600prof_child_enter(tm)
1601 proftime_T *tm; /* place to store waittime */
1602{
1603 funccall_T *fc = current_funccal;
1604
1605 if (fc != NULL && fc->func->uf_profiling)
1606 profile_start(&fc->prof_child);
1607 script_prof_save(tm);
1608}
1609
1610/*
1611 * Take care of time spent in a child.
1612 * Should always be called after prof_child_enter().
1613 */
1614 void
1615prof_child_exit(tm)
1616 proftime_T *tm; /* where waittime was stored */
1617{
1618 funccall_T *fc = current_funccal;
1619
1620 if (fc != NULL && fc->func->uf_profiling)
1621 {
1622 profile_end(&fc->prof_child);
1623 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1624 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1625 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1626 }
1627 script_prof_restore(tm);
1628}
1629#endif
1630
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#ifdef FEAT_FOLDING
1633/*
1634 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1635 * it in "*cp". Doesn't give error messages.
1636 */
1637 int
1638eval_foldexpr(arg, cp)
1639 char_u *arg;
1640 int *cp;
1641{
Bram Moolenaar33570922005-01-25 22:26:29 +00001642 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 int retval;
1644 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001645 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1646 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001649 if (use_sandbox)
1650 ++sandbox;
1651 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 retval = 0;
1655 else
1656 {
1657 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 if (tv.v_type == VAR_NUMBER)
1659 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001660 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 retval = 0;
1662 else
1663 {
1664 /* If the result is a string, check if there is a non-digit before
1665 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001666 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if (!VIM_ISDIGIT(*s) && *s != '-')
1668 *cp = *s++;
1669 retval = atol((char *)s);
1670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 }
1673 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001674 if (use_sandbox)
1675 --sandbox;
1676 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 return retval;
1679}
1680#endif
1681
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683 * ":let" list all variable values
1684 * ":let var1 var2" list variable values
1685 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001686 * ":let var += expr" assignment command.
1687 * ":let var -= expr" assignment command.
1688 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
1691 void
1692ex_let(eap)
1693 exarg_T *eap;
1694{
1695 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001697 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699 int var_count = 0;
1700 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001701 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001702 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001703 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaardb552d602006-03-23 22:59:57 +00001705 argend = skip_var_list(arg, &var_count, &semicolon);
1706 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001708 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1709 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001710 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001713 /*
1714 * ":let" without "=": list variables
1715 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 if (*arg == '[')
1717 EMSG(_(e_invarg));
1718 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001719 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001720 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001723 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001724 list_glob_vars(&first);
1725 list_buf_vars(&first);
1726 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001727#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001728 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001729#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001730 list_script_vars(&first);
1731 list_func_vars(&first);
1732 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 eap->nextcmd = check_nextcmd(arg);
1735 }
1736 else
1737 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 op[0] = '=';
1739 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001740 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741 {
1742 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1743 op[0] = expr[-1]; /* +=, -= or .= */
1744 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (eap->skip)
1748 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 if (eap->skip)
1751 {
1752 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001753 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 --emsg_skip;
1755 }
1756 else if (i != FAIL)
1757 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762 }
1763}
1764
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765/*
1766 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1767 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001768 * When "nextchars" is not NULL it points to a string with characters that
1769 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1770 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 * Returns OK or FAIL;
1772 */
1773 static int
1774ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1775 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001776 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 int copy; /* copy values from "tv", don't move */
1778 int semicolon; /* from skip_var_list() */
1779 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001780 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781{
1782 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001785 listitem_T *item;
1786 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787
1788 if (*arg != '[')
1789 {
1790 /*
1791 * ":let var = expr" or ":for var in list"
1792 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 return FAIL;
1795 return OK;
1796 }
1797
1798 /*
1799 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1800 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001801 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 {
1803 EMSG(_(e_listreq));
1804 return FAIL;
1805 }
1806
1807 i = list_len(l);
1808 if (semicolon == 0 && var_count < i)
1809 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001810 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 return FAIL;
1812 }
1813 if (var_count - semicolon > i)
1814 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001815 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 return FAIL;
1817 }
1818
1819 item = l->lv_first;
1820 while (*arg != ']')
1821 {
1822 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001823 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 item = item->li_next;
1825 if (arg == NULL)
1826 return FAIL;
1827
1828 arg = skipwhite(arg);
1829 if (*arg == ';')
1830 {
1831 /* Put the rest of the list (may be empty) in the var after ';'.
1832 * Create a new list for this. */
1833 l = list_alloc();
1834 if (l == NULL)
1835 return FAIL;
1836 while (item != NULL)
1837 {
1838 list_append_tv(l, &item->li_tv);
1839 item = item->li_next;
1840 }
1841
1842 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001843 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 ltv.vval.v_list = l;
1845 l->lv_refcount = 1;
1846
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1848 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 clear_tv(&ltv);
1850 if (arg == NULL)
1851 return FAIL;
1852 break;
1853 }
1854 else if (*arg != ',' && *arg != ']')
1855 {
1856 EMSG2(_(e_intern2), "ex_let_vars()");
1857 return FAIL;
1858 }
1859 }
1860
1861 return OK;
1862}
1863
1864/*
1865 * Skip over assignable variable "var" or list of variables "[var, var]".
1866 * Used for ":let varvar = expr" and ":for varvar in expr".
1867 * For "[var, var]" increment "*var_count" for each variable.
1868 * for "[var, var; var]" set "semicolon".
1869 * Return NULL for an error.
1870 */
1871 static char_u *
1872skip_var_list(arg, var_count, semicolon)
1873 char_u *arg;
1874 int *var_count;
1875 int *semicolon;
1876{
1877 char_u *p, *s;
1878
1879 if (*arg == '[')
1880 {
1881 /* "[var, var]": find the matching ']'. */
1882 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001883 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 {
1885 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1886 s = skip_var_one(p);
1887 if (s == p)
1888 {
1889 EMSG2(_(e_invarg2), p);
1890 return NULL;
1891 }
1892 ++*var_count;
1893
1894 p = skipwhite(s);
1895 if (*p == ']')
1896 break;
1897 else if (*p == ';')
1898 {
1899 if (*semicolon == 1)
1900 {
1901 EMSG(_("Double ; in list of variables"));
1902 return NULL;
1903 }
1904 *semicolon = 1;
1905 }
1906 else if (*p != ',')
1907 {
1908 EMSG2(_(e_invarg2), p);
1909 return NULL;
1910 }
1911 }
1912 return p + 1;
1913 }
1914 else
1915 return skip_var_one(arg);
1916}
1917
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001918/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001919 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001920 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 static char_u *
1923skip_var_one(arg)
1924 char_u *arg;
1925{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001926 if (*arg == '@' && arg[1] != NUL)
1927 return arg + 2;
1928 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1929 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930}
1931
Bram Moolenaara7043832005-01-21 11:56:39 +00001932/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 * List variables for hashtab "ht" with prefix "prefix".
1934 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001935 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001937list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001939 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001941 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001942{
Bram Moolenaar33570922005-01-25 22:26:29 +00001943 hashitem_T *hi;
1944 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001945 int todo;
1946
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001947 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001948 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1949 {
1950 if (!HASHITEM_EMPTY(hi))
1951 {
1952 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001953 di = HI2DI(hi);
1954 if (empty || di->di_tv.v_type != VAR_STRING
1955 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001956 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 }
1958 }
1959}
1960
1961/*
1962 * List global variables.
1963 */
1964 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965list_glob_vars(first)
1966 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001967{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001968 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001969}
1970
1971/*
1972 * List buffer variables.
1973 */
1974 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001975list_buf_vars(first)
1976 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001977{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001978 char_u numbuf[NUMBUFLEN];
1979
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001980 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1981 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982
1983 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001984 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1985 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001986}
1987
1988/*
1989 * List window variables.
1990 */
1991 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001992list_win_vars(first)
1993 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001994{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001995 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1996 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001997}
1998
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001999#ifdef FEAT_WINDOWS
2000/*
2001 * List tab page variables.
2002 */
2003 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002004list_tab_vars(first)
2005 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002006{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002007 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2008 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002009}
2010#endif
2011
Bram Moolenaara7043832005-01-21 11:56:39 +00002012/*
2013 * List Vim variables.
2014 */
2015 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002016list_vim_vars(first)
2017 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002019 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020}
2021
2022/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002023 * List script-local variables, if there is a script.
2024 */
2025 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002026list_script_vars(first)
2027 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002028{
2029 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002030 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2031 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002032}
2033
2034/*
2035 * List function variables, if there is a function.
2036 */
2037 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002038list_func_vars(first)
2039 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002040{
2041 if (current_funccal != NULL)
2042 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002043 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002044}
2045
2046/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 * List variables in "arg".
2048 */
2049 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002050list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 exarg_T *eap;
2052 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002053 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054{
2055 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058 char_u *name_start;
2059 char_u *arg_subsc;
2060 char_u *tofree;
2061 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062
2063 while (!ends_excmd(*arg) && !got_int)
2064 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002067 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002068 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2069 {
2070 emsg_severe = TRUE;
2071 EMSG(_(e_trailing));
2072 break;
2073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002077 /* get_name_len() takes care of expanding curly braces */
2078 name_start = name = arg;
2079 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2080 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082 /* This is mainly to keep test 49 working: when expanding
2083 * curly braces fails overrule the exception error message. */
2084 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002085 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002086 emsg_severe = TRUE;
2087 EMSG2(_(e_invarg2), arg);
2088 break;
2089 }
2090 error = TRUE;
2091 }
2092 else
2093 {
2094 if (tofree != NULL)
2095 name = tofree;
2096 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 else
2099 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 /* handle d.key, l[idx], f(expr) */
2101 arg_subsc = arg;
2102 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002108 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 case 'g': list_glob_vars(first); break;
2111 case 'b': list_buf_vars(first); break;
2112 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002113#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002115#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 case 'v': list_vim_vars(first); break;
2117 case 's': list_script_vars(first); break;
2118 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 default:
2120 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 }
2123 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124 {
2125 char_u numbuf[NUMBUFLEN];
2126 char_u *tf;
2127 int c;
2128 char_u *s;
2129
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002130 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002131 c = *arg;
2132 *arg = NUL;
2133 list_one_var_a((char_u *)"",
2134 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 tv.v_type,
2136 s == NULL ? (char_u *)"" : s,
2137 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002138 *arg = c;
2139 vim_free(tf);
2140 }
2141 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 }
2144 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145
2146 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002148
2149 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 }
2151
2152 return arg;
2153}
2154
2155/*
2156 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2157 * Returns a pointer to the char just after the var name.
2158 * Returns NULL if there is an error.
2159 */
2160 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002163 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002165 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167{
2168 int c1;
2169 char_u *name;
2170 char_u *p;
2171 char_u *arg_end = NULL;
2172 int len;
2173 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175
2176 /*
2177 * ":let $VAR = expr": Set environment variable.
2178 */
2179 if (*arg == '$')
2180 {
2181 /* Find the end of the name. */
2182 ++arg;
2183 name = arg;
2184 len = get_env_len(&arg);
2185 if (len == 0)
2186 EMSG2(_(e_invarg2), name - 1);
2187 else
2188 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189 if (op != NULL && (*op == '+' || *op == '-'))
2190 EMSG2(_(e_letwrong), op);
2191 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002192 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 EMSG(_(e_letunexp));
2194 else
2195 {
2196 c1 = name[len];
2197 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002198 p = get_tv_string_chk(tv);
2199 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002200 {
2201 int mustfree = FALSE;
2202 char_u *s = vim_getenv(name, &mustfree);
2203
2204 if (s != NULL)
2205 {
2206 p = tofree = concat_str(s, p);
2207 if (mustfree)
2208 vim_free(s);
2209 }
2210 }
2211 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002212 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002213 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002214 if (STRICMP(name, "HOME") == 0)
2215 init_homedir();
2216 else if (didset_vim && STRICMP(name, "VIM") == 0)
2217 didset_vim = FALSE;
2218 else if (didset_vimruntime
2219 && STRICMP(name, "VIMRUNTIME") == 0)
2220 didset_vimruntime = FALSE;
2221 arg_end = arg;
2222 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002224 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 }
2226 }
2227 }
2228
2229 /*
2230 * ":let &option = expr": Set option value.
2231 * ":let &l:option = expr": Set local option value.
2232 * ":let &g:option = expr": Set global option value.
2233 */
2234 else if (*arg == '&')
2235 {
2236 /* Find the end of the name. */
2237 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002238 if (p == NULL || (endchars != NULL
2239 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 EMSG(_(e_letunexp));
2241 else
2242 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002243 long n;
2244 int opt_type;
2245 long numval;
2246 char_u *stringval = NULL;
2247 char_u *s;
2248
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 c1 = *p;
2250 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002251
2252 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002253 s = get_tv_string_chk(tv); /* != NULL if number or string */
2254 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002255 {
2256 opt_type = get_option_value(arg, &numval,
2257 &stringval, opt_flags);
2258 if ((opt_type == 1 && *op == '.')
2259 || (opt_type == 0 && *op != '.'))
2260 EMSG2(_(e_letwrong), op);
2261 else
2262 {
2263 if (opt_type == 1) /* number */
2264 {
2265 if (*op == '+')
2266 n = numval + n;
2267 else
2268 n = numval - n;
2269 }
2270 else if (opt_type == 0 && stringval != NULL) /* string */
2271 {
2272 s = concat_str(stringval, s);
2273 vim_free(stringval);
2274 stringval = s;
2275 }
2276 }
2277 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 if (s != NULL)
2279 {
2280 set_option_value(arg, n, s, opt_flags);
2281 arg_end = p;
2282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002284 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286 }
2287
2288 /*
2289 * ":let @r = expr": Set register contents.
2290 */
2291 else if (*arg == '@')
2292 {
2293 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002294 if (op != NULL && (*op == '+' || *op == '-'))
2295 EMSG2(_(e_letwrong), op);
2296 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002297 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 EMSG(_(e_letunexp));
2299 else
2300 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002301 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 char_u *s;
2303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002304 p = get_tv_string_chk(tv);
2305 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002307 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 if (s != NULL)
2309 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002310 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002311 vim_free(s);
2312 }
2313 }
2314 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002315 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002317 arg_end = arg + 1;
2318 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002319 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
2321 }
2322
2323 /*
2324 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002327 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002329 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002331 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2335 EMSG(_(e_letunexp));
2336 else
2337 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 arg_end = p;
2340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 }
2344
2345 else
2346 EMSG2(_(e_invarg2), arg);
2347
2348 return arg_end;
2349}
2350
2351/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2353 */
2354 static int
2355check_changedtick(arg)
2356 char_u *arg;
2357{
2358 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2359 {
2360 EMSG2(_(e_readonlyvar), arg);
2361 return TRUE;
2362 }
2363 return FALSE;
2364}
2365
2366/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 * Get an lval: variable, Dict item or List item that can be assigned a value
2368 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2369 * "name.key", "name.key[expr]" etc.
2370 * Indexing only works if "name" is an existing List or Dictionary.
2371 * "name" points to the start of the name.
2372 * If "rettv" is not NULL it points to the value to be assigned.
2373 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2374 * wrong; must end in space or cmd separator.
2375 *
2376 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002377 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 */
2380 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002381get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002383 typval_T *rettv;
2384 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 int unlet;
2386 int skip;
2387 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002388 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 char_u *expr_start, *expr_end;
2392 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002393 dictitem_T *v;
2394 typval_T var1;
2395 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002397 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002400 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002403 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404
2405 if (skip)
2406 {
2407 /* When skipping just find the end of the name. */
2408 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002409 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 }
2411
2412 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002413 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 if (expr_start != NULL)
2415 {
2416 /* Don't expand the name when we already know there is an error. */
2417 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2418 && *p != '[' && *p != '.')
2419 {
2420 EMSG(_(e_trailing));
2421 return NULL;
2422 }
2423
2424 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2425 if (lp->ll_exp_name == NULL)
2426 {
2427 /* Report an invalid expression in braces, unless the
2428 * expression evaluation has been cancelled due to an
2429 * aborting error, an interrupt, or an exception. */
2430 if (!aborting() && !quiet)
2431 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002432 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 EMSG2(_(e_invarg2), name);
2434 return NULL;
2435 }
2436 }
2437 lp->ll_name = lp->ll_exp_name;
2438 }
2439 else
2440 lp->ll_name = name;
2441
2442 /* Without [idx] or .key we are done. */
2443 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2444 return p;
2445
2446 cc = *p;
2447 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002448 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 if (v == NULL && !quiet)
2450 EMSG2(_(e_undefvar), lp->ll_name);
2451 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 if (v == NULL)
2453 return NULL;
2454
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 /*
2456 * Loop until no more [idx] or .key is following.
2457 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002458 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2462 && !(lp->ll_tv->v_type == VAR_DICT
2463 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 if (!quiet)
2466 EMSG(_("E689: Can only index a List or Dictionary"));
2467 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (!quiet)
2472 EMSG(_("E708: [:] must come last"));
2473 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002475
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 len = -1;
2477 if (*p == '.')
2478 {
2479 key = p + 1;
2480 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2481 ;
2482 if (len == 0)
2483 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (!quiet)
2485 EMSG(_(e_emptykey));
2486 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 }
2488 p = key + len;
2489 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002490 else
2491 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002493 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 if (*p == ':')
2495 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002496 else
2497 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 empty1 = FALSE;
2499 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 if (get_tv_string_chk(&var1) == NULL)
2502 {
2503 /* not a number or string */
2504 clear_tv(&var1);
2505 return NULL;
2506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 }
2508
2509 /* Optionally get the second index [ :expr]. */
2510 if (*p == ':')
2511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516 if (!empty1)
2517 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (rettv != NULL && (rettv->v_type != VAR_LIST
2521 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (!quiet)
2524 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 if (!empty1)
2526 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 }
2529 p = skipwhite(p + 1);
2530 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 else
2533 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2536 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 if (!empty1)
2538 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002541 if (get_tv_string_chk(&var2) == NULL)
2542 {
2543 /* not a number or string */
2544 if (!empty1)
2545 clear_tv(&var1);
2546 clear_tv(&var2);
2547 return NULL;
2548 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002551 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002554
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 if (!quiet)
2558 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 if (!empty1)
2560 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 }
2565
2566 /* Skip to past ']'. */
2567 ++p;
2568 }
2569
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 {
2572 if (len == -1)
2573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002575 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 if (*key == NUL)
2577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 if (!quiet)
2579 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 }
2583 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002584 lp->ll_list = NULL;
2585 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002589 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002593 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 if (len == -1)
2595 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 }
2598 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 if (len == -1)
2603 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 p = NULL;
2606 break;
2607 }
2608 if (len == -1)
2609 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 }
2612 else
2613 {
2614 /*
2615 * Get the number and item for the only or first index of the List.
2616 */
2617 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 else
2620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002621 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 clear_tv(&var1);
2623 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002624 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 lp->ll_list = lp->ll_tv->vval.v_list;
2626 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2627 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002629 if (lp->ll_n1 < 0)
2630 {
2631 lp->ll_n1 = 0;
2632 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2633 }
2634 }
2635 if (lp->ll_li == NULL)
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641
2642 /*
2643 * May need to find the item or absolute index for the second
2644 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 * When no index given: "lp->ll_empty2" is TRUE.
2646 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002650 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 }
2659
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2661 if (lp->ll_n1 < 0)
2662 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2663 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 }
2666
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
2670
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return p;
2672}
2673
2674/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002675 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 */
2677 static void
2678clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002679 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680{
2681 vim_free(lp->ll_exp_name);
2682 vim_free(lp->ll_newkey);
2683}
2684
2685/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002686 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 */
2690 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002691set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002692 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002694 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002696 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697{
2698 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002699 listitem_T *ri;
2700 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701
2702 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 cc = *endp;
2707 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708 if (op != NULL && *op != '=')
2709 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002710 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002711
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002712 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002713 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002714 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 {
2716 if (tv_op(&tv, rettv, op) == OK)
2717 set_var(lp->ll_name, &tv, FALSE);
2718 clear_tv(&tv);
2719 }
2720 }
2721 else
2722 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002724 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002726 else if (tv_check_lock(lp->ll_newkey == NULL
2727 ? lp->ll_tv->v_lock
2728 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2729 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 else if (lp->ll_range)
2731 {
2732 /*
2733 * Assign the List values to the list items.
2734 */
2735 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 if (op != NULL && *op != '=')
2738 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2739 else
2740 {
2741 clear_tv(&lp->ll_li->li_tv);
2742 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2743 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 ri = ri->li_next;
2745 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2746 break;
2747 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002750 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 ri = NULL;
2753 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002755 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 lp->ll_li = lp->ll_li->li_next;
2757 ++lp->ll_n1;
2758 }
2759 if (ri != NULL)
2760 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002761 else if (lp->ll_empty2
2762 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 : lp->ll_n1 != lp->ll_n2)
2764 EMSG(_("E711: List value has not enough items"));
2765 }
2766 else
2767 {
2768 /*
2769 * Assign to a List or Dictionary item.
2770 */
2771 if (lp->ll_newkey != NULL)
2772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002773 if (op != NULL && *op != '=')
2774 {
2775 EMSG2(_(e_letwrong), op);
2776 return;
2777 }
2778
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002780 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (di == NULL)
2782 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002783 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2784 {
2785 vim_free(di);
2786 return;
2787 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002789 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002790 else if (op != NULL && *op != '=')
2791 {
2792 tv_op(lp->ll_tv, rettv, op);
2793 return;
2794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 /*
2799 * Assign the value to the variable or list item.
2800 */
2801 if (copy)
2802 copy_tv(rettv, lp->ll_tv);
2803 else
2804 {
2805 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002806 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002808 }
2809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002810}
2811
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002812/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2814 * Returns OK or FAIL.
2815 */
2816 static int
2817tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002818 typval_T *tv1;
2819 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 char_u *op;
2821{
2822 long n;
2823 char_u numbuf[NUMBUFLEN];
2824 char_u *s;
2825
2826 /* Can't do anything with a Funcref or a Dict on the right. */
2827 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2828 {
2829 switch (tv1->v_type)
2830 {
2831 case VAR_DICT:
2832 case VAR_FUNC:
2833 break;
2834
2835 case VAR_LIST:
2836 if (*op != '+' || tv2->v_type != VAR_LIST)
2837 break;
2838 /* List += List */
2839 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2840 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2841 return OK;
2842
2843 case VAR_NUMBER:
2844 case VAR_STRING:
2845 if (tv2->v_type == VAR_LIST)
2846 break;
2847 if (*op == '+' || *op == '-')
2848 {
2849 /* nr += nr or nr -= nr*/
2850 n = get_tv_number(tv1);
2851 if (*op == '+')
2852 n += get_tv_number(tv2);
2853 else
2854 n -= get_tv_number(tv2);
2855 clear_tv(tv1);
2856 tv1->v_type = VAR_NUMBER;
2857 tv1->vval.v_number = n;
2858 }
2859 else
2860 {
2861 /* str .= str */
2862 s = get_tv_string(tv1);
2863 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2864 clear_tv(tv1);
2865 tv1->v_type = VAR_STRING;
2866 tv1->vval.v_string = s;
2867 }
2868 return OK;
2869 }
2870 }
2871
2872 EMSG2(_(e_letwrong), op);
2873 return FAIL;
2874}
2875
2876/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002877 * Add a watcher to a list.
2878 */
2879 static void
2880list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 list_T *l;
2882 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883{
2884 lw->lw_next = l->lv_watch;
2885 l->lv_watch = lw;
2886}
2887
2888/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002889 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002890 * No warning when it isn't found...
2891 */
2892 static void
2893list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 list_T *l;
2895 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002896{
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002898
2899 lwp = &l->lv_watch;
2900 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2901 {
2902 if (lw == lwrem)
2903 {
2904 *lwp = lw->lw_next;
2905 break;
2906 }
2907 lwp = &lw->lw_next;
2908 }
2909}
2910
2911/*
2912 * Just before removing an item from a list: advance watchers to the next
2913 * item.
2914 */
2915 static void
2916list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 list_T *l;
2918 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919{
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002921
2922 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2923 if (lw->lw_item == item)
2924 lw->lw_item = item->li_next;
2925}
2926
2927/*
2928 * Evaluate the expression used in a ":for var in expr" command.
2929 * "arg" points to "var".
2930 * Set "*errp" to TRUE for an error, FALSE otherwise;
2931 * Return a pointer that holds the info. Null when there is an error.
2932 */
2933 void *
2934eval_for_line(arg, errp, nextcmdp, skip)
2935 char_u *arg;
2936 int *errp;
2937 char_u **nextcmdp;
2938 int skip;
2939{
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002942 typval_T tv;
2943 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002944
2945 *errp = TRUE; /* default: there is an error */
2946
Bram Moolenaar33570922005-01-25 22:26:29 +00002947 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002948 if (fi == NULL)
2949 return NULL;
2950
2951 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2952 if (expr == NULL)
2953 return fi;
2954
2955 expr = skipwhite(expr);
2956 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2957 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002958 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 return fi;
2960 }
2961
2962 if (skip)
2963 ++emsg_skip;
2964 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2965 {
2966 *errp = FALSE;
2967 if (!skip)
2968 {
2969 l = tv.vval.v_list;
2970 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002971 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002973 clear_tv(&tv);
2974 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 else
2976 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002977 /* No need to increment the refcount, it's already set for the
2978 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 fi->fi_list = l;
2980 list_add_watch(l, &fi->fi_lw);
2981 fi->fi_lw.lw_item = l->lv_first;
2982 }
2983 }
2984 }
2985 if (skip)
2986 --emsg_skip;
2987
2988 return fi;
2989}
2990
2991/*
2992 * Use the first item in a ":for" list. Advance to the next.
2993 * Assign the values to the variable (list). "arg" points to the first one.
2994 * Return TRUE when a valid item was found, FALSE when at end of list or
2995 * something wrong.
2996 */
2997 int
2998next_for_item(fi_void, arg)
2999 void *fi_void;
3000 char_u *arg;
3001{
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003004 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005
3006 item = fi->fi_lw.lw_item;
3007 if (item == NULL)
3008 result = FALSE;
3009 else
3010 {
3011 fi->fi_lw.lw_item = item->li_next;
3012 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3013 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3014 }
3015 return result;
3016}
3017
3018/*
3019 * Free the structure used to store info used by ":for".
3020 */
3021 void
3022free_for_info(fi_void)
3023 void *fi_void;
3024{
Bram Moolenaar33570922005-01-25 22:26:29 +00003025 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003026
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003027 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003028 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003029 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003030 list_unref(fi->fi_list);
3031 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003032 vim_free(fi);
3033}
3034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3036
3037 void
3038set_context_for_expression(xp, arg, cmdidx)
3039 expand_T *xp;
3040 char_u *arg;
3041 cmdidx_T cmdidx;
3042{
3043 int got_eq = FALSE;
3044 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003047 if (cmdidx == CMD_let)
3048 {
3049 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003050 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003051 {
3052 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003053 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003054 {
3055 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003056 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003057 if (vim_iswhite(*p))
3058 break;
3059 }
3060 return;
3061 }
3062 }
3063 else
3064 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3065 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 while ((xp->xp_pattern = vim_strpbrk(arg,
3067 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3068 {
3069 c = *xp->xp_pattern;
3070 if (c == '&')
3071 {
3072 c = xp->xp_pattern[1];
3073 if (c == '&')
3074 {
3075 ++xp->xp_pattern;
3076 xp->xp_context = cmdidx != CMD_let || got_eq
3077 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3078 }
3079 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003082 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3083 xp->xp_pattern += 2;
3084
3085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
3087 else if (c == '$')
3088 {
3089 /* environment variable */
3090 xp->xp_context = EXPAND_ENV_VARS;
3091 }
3092 else if (c == '=')
3093 {
3094 got_eq = TRUE;
3095 xp->xp_context = EXPAND_EXPRESSION;
3096 }
3097 else if (c == '<'
3098 && xp->xp_context == EXPAND_FUNCTIONS
3099 && vim_strchr(xp->xp_pattern, '(') == NULL)
3100 {
3101 /* Function name can start with "<SNR>" */
3102 break;
3103 }
3104 else if (cmdidx != CMD_let || got_eq)
3105 {
3106 if (c == '"') /* string */
3107 {
3108 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3109 if (c == '\\' && xp->xp_pattern[1] != NUL)
3110 ++xp->xp_pattern;
3111 xp->xp_context = EXPAND_NOTHING;
3112 }
3113 else if (c == '\'') /* literal string */
3114 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003115 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3117 /* skip */ ;
3118 xp->xp_context = EXPAND_NOTHING;
3119 }
3120 else if (c == '|')
3121 {
3122 if (xp->xp_pattern[1] == '|')
3123 {
3124 ++xp->xp_pattern;
3125 xp->xp_context = EXPAND_EXPRESSION;
3126 }
3127 else
3128 xp->xp_context = EXPAND_COMMANDS;
3129 }
3130 else
3131 xp->xp_context = EXPAND_EXPRESSION;
3132 }
3133 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 /* Doesn't look like something valid, expand as an expression
3135 * anyway. */
3136 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 arg = xp->xp_pattern;
3138 if (*arg != NUL)
3139 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3140 /* skip */ ;
3141 }
3142 xp->xp_pattern = arg;
3143}
3144
3145#endif /* FEAT_CMDL_COMPL */
3146
3147/*
3148 * ":1,25call func(arg1, arg2)" function call.
3149 */
3150 void
3151ex_call(eap)
3152 exarg_T *eap;
3153{
3154 char_u *arg = eap->arg;
3155 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003157 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003159 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 linenr_T lnum;
3161 int doesrange;
3162 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003165 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003166 if (fudi.fd_newkey != NULL)
3167 {
3168 /* Still need to give an error message for missing key. */
3169 EMSG2(_(e_dictkey), fudi.fd_newkey);
3170 vim_free(fudi.fd_newkey);
3171 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003172 if (tofree == NULL)
3173 return;
3174
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003175 /* Increase refcount on dictionary, it could get deleted when evaluating
3176 * the arguments. */
3177 if (fudi.fd_dict != NULL)
3178 ++fudi.fd_dict->dv_refcount;
3179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003181 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003182 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaar532c7802005-01-27 14:44:31 +00003184 /* Skip white space to allow ":call func ()". Not good, but required for
3185 * backward compatibility. */
3186 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003187 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 if (*startarg != '(')
3190 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003191 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 goto end;
3193 }
3194
3195 /*
3196 * When skipping, evaluate the function once, to find the end of the
3197 * arguments.
3198 * When the function takes a range, this is discovered after the first
3199 * call, and the loop is broken.
3200 */
3201 if (eap->skip)
3202 {
3203 ++emsg_skip;
3204 lnum = eap->line2; /* do it once, also with an invalid range */
3205 }
3206 else
3207 lnum = eap->line1;
3208 for ( ; lnum <= eap->line2; ++lnum)
3209 {
3210 if (!eap->skip && eap->addr_count > 0)
3211 {
3212 curwin->w_cursor.lnum = lnum;
3213 curwin->w_cursor.col = 0;
3214 }
3215 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003216 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003217 eap->line1, eap->line2, &doesrange,
3218 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
3220 failed = TRUE;
3221 break;
3222 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003223
3224 /* Handle a function returning a Funcref, Dictionary or List. */
3225 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3226 {
3227 failed = TRUE;
3228 break;
3229 }
3230
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003231 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (doesrange || eap->skip)
3233 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003236 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003237 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003238 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (aborting())
3240 break;
3241 }
3242 if (eap->skip)
3243 --emsg_skip;
3244
3245 if (!failed)
3246 {
3247 /* Check for trailing illegal characters and a following command. */
3248 if (!ends_excmd(*arg))
3249 {
3250 emsg_severe = TRUE;
3251 EMSG(_(e_trailing));
3252 }
3253 else
3254 eap->nextcmd = check_nextcmd(arg);
3255 }
3256
3257end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003258 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003259 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260}
3261
3262/*
3263 * ":unlet[!] var1 ... " command.
3264 */
3265 void
3266ex_unlet(eap)
3267 exarg_T *eap;
3268{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003269 ex_unletlock(eap, eap->arg, 0);
3270}
3271
3272/*
3273 * ":lockvar" and ":unlockvar" commands
3274 */
3275 void
3276ex_lockvar(eap)
3277 exarg_T *eap;
3278{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003280 int deep = 2;
3281
3282 if (eap->forceit)
3283 deep = -1;
3284 else if (vim_isdigit(*arg))
3285 {
3286 deep = getdigits(&arg);
3287 arg = skipwhite(arg);
3288 }
3289
3290 ex_unletlock(eap, arg, deep);
3291}
3292
3293/*
3294 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3295 */
3296 static void
3297ex_unletlock(eap, argstart, deep)
3298 exarg_T *eap;
3299 char_u *argstart;
3300 int deep;
3301{
3302 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003305 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 do
3308 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003309 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003310 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3311 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003312 if (lv.ll_name == NULL)
3313 error = TRUE; /* error but continue parsing */
3314 if (name_end == NULL || (!vim_iswhite(*name_end)
3315 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003317 if (name_end != NULL)
3318 {
3319 emsg_severe = TRUE;
3320 EMSG(_(e_trailing));
3321 }
3322 if (!(eap->skip || error))
3323 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 break;
3325 }
3326
3327 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003328 {
3329 if (eap->cmdidx == CMD_unlet)
3330 {
3331 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3332 error = TRUE;
3333 }
3334 else
3335 {
3336 if (do_lock_var(&lv, name_end, deep,
3337 eap->cmdidx == CMD_lockvar) == FAIL)
3338 error = TRUE;
3339 }
3340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003342 if (!eap->skip)
3343 clear_lval(&lv);
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 arg = skipwhite(name_end);
3346 } while (!ends_excmd(*arg));
3347
3348 eap->nextcmd = check_nextcmd(arg);
3349}
3350
Bram Moolenaar8c711452005-01-14 21:53:12 +00003351 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003352do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003353 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003354 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 int forceit;
3356{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003357 int ret = OK;
3358 int cc;
3359
3360 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003361 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003362 cc = *name_end;
3363 *name_end = NUL;
3364
3365 /* Normal name or expanded name. */
3366 if (check_changedtick(lp->ll_name))
3367 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003368 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003369 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003370 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003371 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003372 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3373 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003374 else if (lp->ll_range)
3375 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003376 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003377
3378 /* Delete a range of List items. */
3379 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3380 {
3381 li = lp->ll_li->li_next;
3382 listitem_remove(lp->ll_list, lp->ll_li);
3383 lp->ll_li = li;
3384 ++lp->ll_n1;
3385 }
3386 }
3387 else
3388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003389 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003390 /* unlet a List item. */
3391 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003392 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003393 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003394 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003395 }
3396
3397 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003398}
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
3401 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003402 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 */
3404 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003405do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003407 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
Bram Moolenaar33570922005-01-25 22:26:29 +00003409 hashtab_T *ht;
3410 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003411 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003412 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaar33570922005-01-25 22:26:29 +00003414 ht = find_var_ht(name, &varname);
3415 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003417 hi = hash_find(ht, varname);
3418 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003419 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003420 di = HI2DI(hi);
3421 if (var_check_fixed(di->di_flags, name)
3422 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003423 return FAIL;
3424 delete_var(ht, hi);
3425 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003428 if (forceit)
3429 return OK;
3430 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 return FAIL;
3432}
3433
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003434/*
3435 * Lock or unlock variable indicated by "lp".
3436 * "deep" is the levels to go (-1 for unlimited);
3437 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3438 */
3439 static int
3440do_lock_var(lp, name_end, deep, lock)
3441 lval_T *lp;
3442 char_u *name_end;
3443 int deep;
3444 int lock;
3445{
3446 int ret = OK;
3447 int cc;
3448 dictitem_T *di;
3449
3450 if (deep == 0) /* nothing to do */
3451 return OK;
3452
3453 if (lp->ll_tv == NULL)
3454 {
3455 cc = *name_end;
3456 *name_end = NUL;
3457
3458 /* Normal name or expanded name. */
3459 if (check_changedtick(lp->ll_name))
3460 ret = FAIL;
3461 else
3462 {
3463 di = find_var(lp->ll_name, NULL);
3464 if (di == NULL)
3465 ret = FAIL;
3466 else
3467 {
3468 if (lock)
3469 di->di_flags |= DI_FLAGS_LOCK;
3470 else
3471 di->di_flags &= ~DI_FLAGS_LOCK;
3472 item_lock(&di->di_tv, deep, lock);
3473 }
3474 }
3475 *name_end = cc;
3476 }
3477 else if (lp->ll_range)
3478 {
3479 listitem_T *li = lp->ll_li;
3480
3481 /* (un)lock a range of List items. */
3482 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3483 {
3484 item_lock(&li->li_tv, deep, lock);
3485 li = li->li_next;
3486 ++lp->ll_n1;
3487 }
3488 }
3489 else if (lp->ll_list != NULL)
3490 /* (un)lock a List item. */
3491 item_lock(&lp->ll_li->li_tv, deep, lock);
3492 else
3493 /* un(lock) a Dictionary item. */
3494 item_lock(&lp->ll_di->di_tv, deep, lock);
3495
3496 return ret;
3497}
3498
3499/*
3500 * Lock or unlock an item. "deep" is nr of levels to go.
3501 */
3502 static void
3503item_lock(tv, deep, lock)
3504 typval_T *tv;
3505 int deep;
3506 int lock;
3507{
3508 static int recurse = 0;
3509 list_T *l;
3510 listitem_T *li;
3511 dict_T *d;
3512 hashitem_T *hi;
3513 int todo;
3514
3515 if (recurse >= DICT_MAXNEST)
3516 {
3517 EMSG(_("E743: variable nested too deep for (un)lock"));
3518 return;
3519 }
3520 if (deep == 0)
3521 return;
3522 ++recurse;
3523
3524 /* lock/unlock the item itself */
3525 if (lock)
3526 tv->v_lock |= VAR_LOCKED;
3527 else
3528 tv->v_lock &= ~VAR_LOCKED;
3529
3530 switch (tv->v_type)
3531 {
3532 case VAR_LIST:
3533 if ((l = tv->vval.v_list) != NULL)
3534 {
3535 if (lock)
3536 l->lv_lock |= VAR_LOCKED;
3537 else
3538 l->lv_lock &= ~VAR_LOCKED;
3539 if (deep < 0 || deep > 1)
3540 /* recursive: lock/unlock the items the List contains */
3541 for (li = l->lv_first; li != NULL; li = li->li_next)
3542 item_lock(&li->li_tv, deep - 1, lock);
3543 }
3544 break;
3545 case VAR_DICT:
3546 if ((d = tv->vval.v_dict) != NULL)
3547 {
3548 if (lock)
3549 d->dv_lock |= VAR_LOCKED;
3550 else
3551 d->dv_lock &= ~VAR_LOCKED;
3552 if (deep < 0 || deep > 1)
3553 {
3554 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003555 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003556 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3557 {
3558 if (!HASHITEM_EMPTY(hi))
3559 {
3560 --todo;
3561 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3562 }
3563 }
3564 }
3565 }
3566 }
3567 --recurse;
3568}
3569
Bram Moolenaara40058a2005-07-11 22:42:07 +00003570/*
3571 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3572 * it refers to a List or Dictionary that is locked.
3573 */
3574 static int
3575tv_islocked(tv)
3576 typval_T *tv;
3577{
3578 return (tv->v_lock & VAR_LOCKED)
3579 || (tv->v_type == VAR_LIST
3580 && tv->vval.v_list != NULL
3581 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3582 || (tv->v_type == VAR_DICT
3583 && tv->vval.v_dict != NULL
3584 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3585}
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3588/*
3589 * Delete all "menutrans_" variables.
3590 */
3591 void
3592del_menutrans_vars()
3593{
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003595 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003598 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003599 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003600 {
3601 if (!HASHITEM_EMPTY(hi))
3602 {
3603 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3605 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003606 }
3607 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609}
3610#endif
3611
3612#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3613
3614/*
3615 * Local string buffer for the next two functions to store a variable name
3616 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3617 * get_user_var_name().
3618 */
3619
3620static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3621
3622static char_u *varnamebuf = NULL;
3623static int varnamebuflen = 0;
3624
3625/*
3626 * Function to concatenate a prefix and a variable name.
3627 */
3628 static char_u *
3629cat_prefix_varname(prefix, name)
3630 int prefix;
3631 char_u *name;
3632{
3633 int len;
3634
3635 len = (int)STRLEN(name) + 3;
3636 if (len > varnamebuflen)
3637 {
3638 vim_free(varnamebuf);
3639 len += 10; /* some additional space */
3640 varnamebuf = alloc(len);
3641 if (varnamebuf == NULL)
3642 {
3643 varnamebuflen = 0;
3644 return NULL;
3645 }
3646 varnamebuflen = len;
3647 }
3648 *varnamebuf = prefix;
3649 varnamebuf[1] = ':';
3650 STRCPY(varnamebuf + 2, name);
3651 return varnamebuf;
3652}
3653
3654/*
3655 * Function given to ExpandGeneric() to obtain the list of user defined
3656 * (global/buffer/window/built-in) variable names.
3657 */
3658/*ARGSUSED*/
3659 char_u *
3660get_user_var_name(xp, idx)
3661 expand_T *xp;
3662 int idx;
3663{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003664 static long_u gdone;
3665 static long_u bdone;
3666 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003667#ifdef FEAT_WINDOWS
3668 static long_u tdone;
3669#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003670 static int vidx;
3671 static hashitem_T *hi;
3672 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
3674 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003675 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003676 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003677#ifdef FEAT_WINDOWS
3678 tdone = 0;
3679#endif
3680 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003681
3682 /* Global variables */
3683 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003685 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003687 else
3688 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003689 while (HASHITEM_EMPTY(hi))
3690 ++hi;
3691 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3692 return cat_prefix_varname('g', hi->hi_key);
3693 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003695
3696 /* b: variables */
3697 ht = &curbuf->b_vars.dv_hashtab;
3698 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003700 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003701 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003702 else
3703 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003704 while (HASHITEM_EMPTY(hi))
3705 ++hi;
3706 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003708 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003710 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 return (char_u *)"b:changedtick";
3712 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003713
3714 /* w: variables */
3715 ht = &curwin->w_vars.dv_hashtab;
3716 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003718 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003719 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003720 else
3721 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003722 while (HASHITEM_EMPTY(hi))
3723 ++hi;
3724 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003726
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003727#ifdef FEAT_WINDOWS
3728 /* t: variables */
3729 ht = &curtab->tp_vars.dv_hashtab;
3730 if (tdone < ht->ht_used)
3731 {
3732 if (tdone++ == 0)
3733 hi = ht->ht_array;
3734 else
3735 ++hi;
3736 while (HASHITEM_EMPTY(hi))
3737 ++hi;
3738 return cat_prefix_varname('t', hi->hi_key);
3739 }
3740#endif
3741
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 /* v: variables */
3743 if (vidx < VV_LEN)
3744 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
3746 vim_free(varnamebuf);
3747 varnamebuf = NULL;
3748 varnamebuflen = 0;
3749 return NULL;
3750}
3751
3752#endif /* FEAT_CMDL_COMPL */
3753
3754/*
3755 * types for expressions.
3756 */
3757typedef enum
3758{
3759 TYPE_UNKNOWN = 0
3760 , TYPE_EQUAL /* == */
3761 , TYPE_NEQUAL /* != */
3762 , TYPE_GREATER /* > */
3763 , TYPE_GEQUAL /* >= */
3764 , TYPE_SMALLER /* < */
3765 , TYPE_SEQUAL /* <= */
3766 , TYPE_MATCH /* =~ */
3767 , TYPE_NOMATCH /* !~ */
3768} exptype_T;
3769
3770/*
3771 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003772 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3774 */
3775
3776/*
3777 * Handle zero level expression.
3778 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003779 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003780 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 * Return OK or FAIL.
3782 */
3783 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 char_u **nextcmd;
3788 int evaluate;
3789{
3790 int ret;
3791 char_u *p;
3792
3793 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (ret == FAIL || !ends_excmd(*p))
3796 {
3797 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003798 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 /*
3800 * Report the invalid expression unless the expression evaluation has
3801 * been cancelled due to an aborting error, an interrupt, or an
3802 * exception.
3803 */
3804 if (!aborting())
3805 EMSG2(_(e_invexpr2), arg);
3806 ret = FAIL;
3807 }
3808 if (nextcmd != NULL)
3809 *nextcmd = check_nextcmd(p);
3810
3811 return ret;
3812}
3813
3814/*
3815 * Handle top level expression:
3816 * expr1 ? expr0 : expr0
3817 *
3818 * "arg" must point to the first non-white of the expression.
3819 * "arg" is advanced to the next non-white after the recognized expression.
3820 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003821 * Note: "rettv.v_lock" is not set.
3822 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 * Return OK or FAIL.
3824 */
3825 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int evaluate;
3830{
3831 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003832 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 /*
3835 * Get the first variable.
3836 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 return FAIL;
3839
3840 if ((*arg)[0] == '?')
3841 {
3842 result = FALSE;
3843 if (evaluate)
3844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003845 int error = FALSE;
3846
3847 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003849 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003850 if (error)
3851 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
3853
3854 /*
3855 * Get the second variable.
3856 */
3857 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003858 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 return FAIL;
3860
3861 /*
3862 * Check for the ":".
3863 */
3864 if ((*arg)[0] != ':')
3865 {
3866 EMSG(_("E109: Missing ':' after '?'"));
3867 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return FAIL;
3870 }
3871
3872 /*
3873 * Get the third variable.
3874 */
3875 *arg = skipwhite(*arg + 1);
3876 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3877 {
3878 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return FAIL;
3881 }
3882 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003883 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885
3886 return OK;
3887}
3888
3889/*
3890 * Handle first level expression:
3891 * expr2 || expr2 || expr2 logical OR
3892 *
3893 * "arg" must point to the first non-white of the expression.
3894 * "arg" is advanced to the next non-white after the recognized expression.
3895 *
3896 * Return OK or FAIL.
3897 */
3898 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 int evaluate;
3903{
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 long result;
3906 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003907 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
3909 /*
3910 * Get the first variable.
3911 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003912 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 return FAIL;
3914
3915 /*
3916 * Repeat until there is no following "||".
3917 */
3918 first = TRUE;
3919 result = FALSE;
3920 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3921 {
3922 if (evaluate && first)
3923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003924 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003927 if (error)
3928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 first = FALSE;
3930 }
3931
3932 /*
3933 * Get the second variable.
3934 */
3935 *arg = skipwhite(*arg + 2);
3936 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3937 return FAIL;
3938
3939 /*
3940 * Compute the result.
3941 */
3942 if (evaluate && !result)
3943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003944 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003946 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003947 if (error)
3948 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 if (evaluate)
3951 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003952 rettv->v_type = VAR_NUMBER;
3953 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955 }
3956
3957 return OK;
3958}
3959
3960/*
3961 * Handle second level expression:
3962 * expr3 && expr3 && expr3 logical AND
3963 *
3964 * "arg" must point to the first non-white of the expression.
3965 * "arg" is advanced to the next non-white after the recognized expression.
3966 *
3967 * Return OK or FAIL.
3968 */
3969 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003970eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 int evaluate;
3974{
Bram Moolenaar33570922005-01-25 22:26:29 +00003975 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 long result;
3977 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003978 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980 /*
3981 * Get the first variable.
3982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003983 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 return FAIL;
3985
3986 /*
3987 * Repeat until there is no following "&&".
3988 */
3989 first = TRUE;
3990 result = TRUE;
3991 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3992 {
3993 if (evaluate && first)
3994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003995 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003998 if (error)
3999 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 first = FALSE;
4001 }
4002
4003 /*
4004 * Get the second variable.
4005 */
4006 *arg = skipwhite(*arg + 2);
4007 if (eval4(arg, &var2, evaluate && result) == FAIL)
4008 return FAIL;
4009
4010 /*
4011 * Compute the result.
4012 */
4013 if (evaluate && result)
4014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004017 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004018 if (error)
4019 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021 if (evaluate)
4022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 rettv->v_type = VAR_NUMBER;
4024 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026 }
4027
4028 return OK;
4029}
4030
4031/*
4032 * Handle third level expression:
4033 * var1 == var2
4034 * var1 =~ var2
4035 * var1 != var2
4036 * var1 !~ var2
4037 * var1 > var2
4038 * var1 >= var2
4039 * var1 < var2
4040 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004041 * var1 is var2
4042 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 *
4044 * "arg" must point to the first non-white of the expression.
4045 * "arg" is advanced to the next non-white after the recognized expression.
4046 *
4047 * Return OK or FAIL.
4048 */
4049 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004050eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 int evaluate;
4054{
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u *p;
4057 int i;
4058 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004059 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 int len = 2;
4061 long n1, n2;
4062 char_u *s1, *s2;
4063 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4064 regmatch_T regmatch;
4065 int ic;
4066 char_u *save_cpo;
4067
4068 /*
4069 * Get the first variable.
4070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004071 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 return FAIL;
4073
4074 p = *arg;
4075 switch (p[0])
4076 {
4077 case '=': if (p[1] == '=')
4078 type = TYPE_EQUAL;
4079 else if (p[1] == '~')
4080 type = TYPE_MATCH;
4081 break;
4082 case '!': if (p[1] == '=')
4083 type = TYPE_NEQUAL;
4084 else if (p[1] == '~')
4085 type = TYPE_NOMATCH;
4086 break;
4087 case '>': if (p[1] != '=')
4088 {
4089 type = TYPE_GREATER;
4090 len = 1;
4091 }
4092 else
4093 type = TYPE_GEQUAL;
4094 break;
4095 case '<': if (p[1] != '=')
4096 {
4097 type = TYPE_SMALLER;
4098 len = 1;
4099 }
4100 else
4101 type = TYPE_SEQUAL;
4102 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004103 case 'i': if (p[1] == 's')
4104 {
4105 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4106 len = 5;
4107 if (!vim_isIDc(p[len]))
4108 {
4109 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4110 type_is = TRUE;
4111 }
4112 }
4113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115
4116 /*
4117 * If there is a comparitive operator, use it.
4118 */
4119 if (type != TYPE_UNKNOWN)
4120 {
4121 /* extra question mark appended: ignore case */
4122 if (p[len] == '?')
4123 {
4124 ic = TRUE;
4125 ++len;
4126 }
4127 /* extra '#' appended: match case */
4128 else if (p[len] == '#')
4129 {
4130 ic = FALSE;
4131 ++len;
4132 }
4133 /* nothing appened: use 'ignorecase' */
4134 else
4135 ic = p_ic;
4136
4137 /*
4138 * Get the second variable.
4139 */
4140 *arg = skipwhite(p + len);
4141 if (eval5(arg, &var2, evaluate) == FAIL)
4142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return FAIL;
4145 }
4146
4147 if (evaluate)
4148 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004149 if (type_is && rettv->v_type != var2.v_type)
4150 {
4151 /* For "is" a different type always means FALSE, for "notis"
4152 * it means TRUE. */
4153 n1 = (type == TYPE_NEQUAL);
4154 }
4155 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4156 {
4157 if (type_is)
4158 {
4159 n1 = (rettv->v_type == var2.v_type
4160 && rettv->vval.v_list == var2.vval.v_list);
4161 if (type == TYPE_NEQUAL)
4162 n1 = !n1;
4163 }
4164 else if (rettv->v_type != var2.v_type
4165 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4166 {
4167 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004168 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004169 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004170 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004171 clear_tv(rettv);
4172 clear_tv(&var2);
4173 return FAIL;
4174 }
4175 else
4176 {
4177 /* Compare two Lists for being equal or unequal. */
4178 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4179 if (type == TYPE_NEQUAL)
4180 n1 = !n1;
4181 }
4182 }
4183
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004184 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4185 {
4186 if (type_is)
4187 {
4188 n1 = (rettv->v_type == var2.v_type
4189 && rettv->vval.v_dict == var2.vval.v_dict);
4190 if (type == TYPE_NEQUAL)
4191 n1 = !n1;
4192 }
4193 else if (rettv->v_type != var2.v_type
4194 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4195 {
4196 if (rettv->v_type != var2.v_type)
4197 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4198 else
4199 EMSG(_("E736: Invalid operation for Dictionary"));
4200 clear_tv(rettv);
4201 clear_tv(&var2);
4202 return FAIL;
4203 }
4204 else
4205 {
4206 /* Compare two Dictionaries for being equal or unequal. */
4207 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4208 if (type == TYPE_NEQUAL)
4209 n1 = !n1;
4210 }
4211 }
4212
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004213 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4214 {
4215 if (rettv->v_type != var2.v_type
4216 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4217 {
4218 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004219 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004221 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004222 clear_tv(rettv);
4223 clear_tv(&var2);
4224 return FAIL;
4225 }
4226 else
4227 {
4228 /* Compare two Funcrefs for being equal or unequal. */
4229 if (rettv->vval.v_string == NULL
4230 || var2.vval.v_string == NULL)
4231 n1 = FALSE;
4232 else
4233 n1 = STRCMP(rettv->vval.v_string,
4234 var2.vval.v_string) == 0;
4235 if (type == TYPE_NEQUAL)
4236 n1 = !n1;
4237 }
4238 }
4239
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 /*
4241 * If one of the two variables is a number, compare as a number.
4242 * When using "=~" or "!~", always compare as string.
4243 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004244 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 n1 = get_tv_number(rettv);
4248 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 switch (type)
4250 {
4251 case TYPE_EQUAL: n1 = (n1 == n2); break;
4252 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4253 case TYPE_GREATER: n1 = (n1 > n2); break;
4254 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4255 case TYPE_SMALLER: n1 = (n1 < n2); break;
4256 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4257 case TYPE_UNKNOWN:
4258 case TYPE_MATCH:
4259 case TYPE_NOMATCH: break; /* avoid gcc warning */
4260 }
4261 }
4262 else
4263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 s1 = get_tv_string_buf(rettv, buf1);
4265 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4267 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4268 else
4269 i = 0;
4270 n1 = FALSE;
4271 switch (type)
4272 {
4273 case TYPE_EQUAL: n1 = (i == 0); break;
4274 case TYPE_NEQUAL: n1 = (i != 0); break;
4275 case TYPE_GREATER: n1 = (i > 0); break;
4276 case TYPE_GEQUAL: n1 = (i >= 0); break;
4277 case TYPE_SMALLER: n1 = (i < 0); break;
4278 case TYPE_SEQUAL: n1 = (i <= 0); break;
4279
4280 case TYPE_MATCH:
4281 case TYPE_NOMATCH:
4282 /* avoid 'l' flag in 'cpoptions' */
4283 save_cpo = p_cpo;
4284 p_cpo = (char_u *)"";
4285 regmatch.regprog = vim_regcomp(s2,
4286 RE_MAGIC + RE_STRING);
4287 regmatch.rm_ic = ic;
4288 if (regmatch.regprog != NULL)
4289 {
4290 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4291 vim_free(regmatch.regprog);
4292 if (type == TYPE_NOMATCH)
4293 n1 = !n1;
4294 }
4295 p_cpo = save_cpo;
4296 break;
4297
4298 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4299 }
4300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 clear_tv(rettv);
4302 clear_tv(&var2);
4303 rettv->v_type = VAR_NUMBER;
4304 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 }
4307
4308 return OK;
4309}
4310
4311/*
4312 * Handle fourth level expression:
4313 * + number addition
4314 * - number subtraction
4315 * . string concatenation
4316 *
4317 * "arg" must point to the first non-white of the expression.
4318 * "arg" is advanced to the next non-white after the recognized expression.
4319 *
4320 * Return OK or FAIL.
4321 */
4322 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int evaluate;
4327{
Bram Moolenaar33570922005-01-25 22:26:29 +00004328 typval_T var2;
4329 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 int op;
4331 long n1, n2;
4332 char_u *s1, *s2;
4333 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4334 char_u *p;
4335
4336 /*
4337 * Get the first variable.
4338 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 return FAIL;
4341
4342 /*
4343 * Repeat computing, until no '+', '-' or '.' is following.
4344 */
4345 for (;;)
4346 {
4347 op = **arg;
4348 if (op != '+' && op != '-' && op != '.')
4349 break;
4350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (op != '+' || rettv->v_type != VAR_LIST)
4352 {
4353 /* For "list + ...", an illegal use of the first operand as
4354 * a number cannot be determined before evaluating the 2nd
4355 * operand: if this is also a list, all is ok.
4356 * For "something . ...", "something - ..." or "non-list + ...",
4357 * we know that the first operand needs to be a string or number
4358 * without evaluating the 2nd operand. So check before to avoid
4359 * side effects after an error. */
4360 if (evaluate && get_tv_string_chk(rettv) == NULL)
4361 {
4362 clear_tv(rettv);
4363 return FAIL;
4364 }
4365 }
4366
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 /*
4368 * Get the second variable.
4369 */
4370 *arg = skipwhite(*arg + 1);
4371 if (eval6(arg, &var2, evaluate) == FAIL)
4372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004373 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 return FAIL;
4375 }
4376
4377 if (evaluate)
4378 {
4379 /*
4380 * Compute the result.
4381 */
4382 if (op == '.')
4383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004384 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4385 s2 = get_tv_string_buf_chk(&var2, buf2);
4386 if (s2 == NULL) /* type error ? */
4387 {
4388 clear_tv(rettv);
4389 clear_tv(&var2);
4390 return FAIL;
4391 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004392 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 clear_tv(rettv);
4394 rettv->v_type = VAR_STRING;
4395 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004397 else if (op == '+' && rettv->v_type == VAR_LIST
4398 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 {
4400 /* concatenate Lists */
4401 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4402 &var3) == FAIL)
4403 {
4404 clear_tv(rettv);
4405 clear_tv(&var2);
4406 return FAIL;
4407 }
4408 clear_tv(rettv);
4409 *rettv = var3;
4410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 else
4412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004413 int error = FALSE;
4414
4415 n1 = get_tv_number_chk(rettv, &error);
4416 if (error)
4417 {
4418 /* This can only happen for "list + non-list".
4419 * For "non-list + ..." or "something - ...", we returned
4420 * before evaluating the 2nd operand. */
4421 clear_tv(rettv);
4422 return FAIL;
4423 }
4424 n2 = get_tv_number_chk(&var2, &error);
4425 if (error)
4426 {
4427 clear_tv(rettv);
4428 clear_tv(&var2);
4429 return FAIL;
4430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 if (op == '+')
4433 n1 = n1 + n2;
4434 else
4435 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 rettv->v_type = VAR_NUMBER;
4437 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004439 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 }
4442 return OK;
4443}
4444
4445/*
4446 * Handle fifth level expression:
4447 * * number multiplication
4448 * / number division
4449 * % number modulo
4450 *
4451 * "arg" must point to the first non-white of the expression.
4452 * "arg" is advanced to the next non-white after the recognized expression.
4453 *
4454 * Return OK or FAIL.
4455 */
4456 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 int evaluate;
4461{
Bram Moolenaar33570922005-01-25 22:26:29 +00004462 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 int op;
4464 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004465 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 /*
4468 * Get the first variable.
4469 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 return FAIL;
4472
4473 /*
4474 * Repeat computing, until no '*', '/' or '%' is following.
4475 */
4476 for (;;)
4477 {
4478 op = **arg;
4479 if (op != '*' && op != '/' && op != '%')
4480 break;
4481
4482 if (evaluate)
4483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004484 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004485 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004486 if (error)
4487 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 else
4490 n1 = 0;
4491
4492 /*
4493 * Get the second variable.
4494 */
4495 *arg = skipwhite(*arg + 1);
4496 if (eval7(arg, &var2, evaluate) == FAIL)
4497 return FAIL;
4498
4499 if (evaluate)
4500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004501 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004502 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004503 if (error)
4504 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
4506 /*
4507 * Compute the result.
4508 */
4509 if (op == '*')
4510 n1 = n1 * n2;
4511 else if (op == '/')
4512 {
4513 if (n2 == 0) /* give an error message? */
4514 n1 = 0x7fffffffL;
4515 else
4516 n1 = n1 / n2;
4517 }
4518 else
4519 {
4520 if (n2 == 0) /* give an error message? */
4521 n1 = 0;
4522 else
4523 n1 = n1 % n2;
4524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004525 rettv->v_type = VAR_NUMBER;
4526 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 }
4529
4530 return OK;
4531}
4532
4533/*
4534 * Handle sixth level expression:
4535 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004536 * "string" string constant
4537 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 * &option-name option value
4539 * @r register contents
4540 * identifier variable value
4541 * function() function call
4542 * $VAR environment variable
4543 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004544 * [expr, expr] List
4545 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 *
4547 * Also handle:
4548 * ! in front logical NOT
4549 * - in front unary minus
4550 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 * trailing [] subscript in String or List
4552 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 *
4554 * "arg" must point to the first non-white of the expression.
4555 * "arg" is advanced to the next non-white after the recognized expression.
4556 *
4557 * Return OK or FAIL.
4558 */
4559 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004560eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 int evaluate;
4564{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 long n;
4566 int len;
4567 char_u *s;
4568 int val;
4569 char_u *start_leader, *end_leader;
4570 int ret = OK;
4571 char_u *alias;
4572
4573 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004575 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
4579 /*
4580 * Skip '!' and '-' characters. They are handled later.
4581 */
4582 start_leader = *arg;
4583 while (**arg == '!' || **arg == '-' || **arg == '+')
4584 *arg = skipwhite(*arg + 1);
4585 end_leader = *arg;
4586
4587 switch (**arg)
4588 {
4589 /*
4590 * Number constant.
4591 */
4592 case '0':
4593 case '1':
4594 case '2':
4595 case '3':
4596 case '4':
4597 case '5':
4598 case '6':
4599 case '7':
4600 case '8':
4601 case '9':
4602 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4603 *arg += len;
4604 if (evaluate)
4605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004606 rettv->v_type = VAR_NUMBER;
4607 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609 break;
4610
4611 /*
4612 * String constant: "string".
4613 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 break;
4616
4617 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004618 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 break;
4622
4623 /*
4624 * List: [expr, expr]
4625 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 break;
4628
4629 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630 * Dictionary: {key: val, key: val}
4631 */
4632 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4633 break;
4634
4635 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004636 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004638 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 break;
4640
4641 /*
4642 * Environment variable: $VAR.
4643 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 break;
4646
4647 /*
4648 * Register contents: @r.
4649 */
4650 case '@': ++*arg;
4651 if (evaluate)
4652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004653 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004654 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 if (**arg != NUL)
4657 ++*arg;
4658 break;
4659
4660 /*
4661 * nested expression: (expression).
4662 */
4663 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (**arg == ')')
4666 ++*arg;
4667 else if (ret == OK)
4668 {
4669 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 ret = FAIL;
4672 }
4673 break;
4674
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 break;
4677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678
4679 if (ret == NOTDONE)
4680 {
4681 /*
4682 * Must be a variable or function name.
4683 * Can also be a curly-braces kind of name: {expr}.
4684 */
4685 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004686 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 if (alias != NULL)
4688 s = alias;
4689
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004690 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691 ret = FAIL;
4692 else
4693 {
4694 if (**arg == '(') /* recursive! */
4695 {
4696 /* If "s" is the name of a variable of type VAR_FUNC
4697 * use its contents. */
4698 s = deref_func_name(s, &len);
4699
4700 /* Invoke the function. */
4701 ret = get_func_tv(s, len, rettv, arg,
4702 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004703 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 /* Stop the expression evaluation when immediately
4705 * aborting on error, or when an interrupt occurred or
4706 * an exception was thrown but not caught. */
4707 if (aborting())
4708 {
4709 if (ret == OK)
4710 clear_tv(rettv);
4711 ret = FAIL;
4712 }
4713 }
4714 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004715 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004716 else
4717 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718 }
4719
4720 if (alias != NULL)
4721 vim_free(alias);
4722 }
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 *arg = skipwhite(*arg);
4725
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004726 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4727 * expr(expr). */
4728 if (ret == OK)
4729 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
4731 /*
4732 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4733 */
4734 if (ret == OK && evaluate && end_leader > start_leader)
4735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004736 int error = FALSE;
4737
4738 val = get_tv_number_chk(rettv, &error);
4739 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 clear_tv(rettv);
4742 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 else
4745 {
4746 while (end_leader > start_leader)
4747 {
4748 --end_leader;
4749 if (*end_leader == '!')
4750 val = !val;
4751 else if (*end_leader == '-')
4752 val = -val;
4753 }
4754 clear_tv(rettv);
4755 rettv->v_type = VAR_NUMBER;
4756 rettv->vval.v_number = val;
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
4759
4760 return ret;
4761}
4762
4763/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004764 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4765 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4767 */
4768 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004769eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004771 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004773 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774{
4775 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004776 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004778 long len = -1;
4779 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004785 if (verbose)
4786 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 return FAIL;
4788 }
4789
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004792 /*
4793 * dict.name
4794 */
4795 key = *arg + 1;
4796 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4797 ;
4798 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 }
4802 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804 /*
4805 * something[idx]
4806 *
4807 * Get the (first) variable from inside the [].
4808 */
4809 *arg = skipwhite(*arg + 1);
4810 if (**arg == ':')
4811 empty1 = TRUE;
4812 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4813 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4815 {
4816 /* not a number or string */
4817 clear_tv(&var1);
4818 return FAIL;
4819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820
4821 /*
4822 * Get the second variable from inside the [:].
4823 */
4824 if (**arg == ':')
4825 {
4826 range = TRUE;
4827 *arg = skipwhite(*arg + 1);
4828 if (**arg == ']')
4829 empty2 = TRUE;
4830 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004832 if (!empty1)
4833 clear_tv(&var1);
4834 return FAIL;
4835 }
4836 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4837 {
4838 /* not a number or string */
4839 if (!empty1)
4840 clear_tv(&var1);
4841 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004842 return FAIL;
4843 }
4844 }
4845
4846 /* Check for the ']'. */
4847 if (**arg != ']')
4848 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004849 if (verbose)
4850 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004851 clear_tv(&var1);
4852 if (range)
4853 clear_tv(&var2);
4854 return FAIL;
4855 }
4856 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004857 }
4858
4859 if (evaluate)
4860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004861 n1 = 0;
4862 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 n1 = get_tv_number(&var1);
4865 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004866 }
4867 if (range)
4868 {
4869 if (empty2)
4870 n2 = -1;
4871 else
4872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 n2 = get_tv_number(&var2);
4874 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 }
4876 }
4877
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 {
4880 case VAR_NUMBER:
4881 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883 len = (long)STRLEN(s);
4884 if (range)
4885 {
4886 /* The resulting variable is a substring. If the indexes
4887 * are out of range the result is empty. */
4888 if (n1 < 0)
4889 {
4890 n1 = len + n1;
4891 if (n1 < 0)
4892 n1 = 0;
4893 }
4894 if (n2 < 0)
4895 n2 = len + n2;
4896 else if (n2 >= len)
4897 n2 = len;
4898 if (n1 >= len || n2 < 0 || n1 > n2)
4899 s = NULL;
4900 else
4901 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4902 }
4903 else
4904 {
4905 /* The resulting variable is a string of a single
4906 * character. If the index is too big or negative the
4907 * result is empty. */
4908 if (n1 >= len || n1 < 0)
4909 s = NULL;
4910 else
4911 s = vim_strnsave(s + n1, 1);
4912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 clear_tv(rettv);
4914 rettv->v_type = VAR_STRING;
4915 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916 break;
4917
4918 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004919 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004920 if (n1 < 0)
4921 n1 = len + n1;
4922 if (!empty1 && (n1 < 0 || n1 >= len))
4923 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004924 /* For a range we allow invalid values and return an empty
4925 * list. A list index out of range is an error. */
4926 if (!range)
4927 {
4928 if (verbose)
4929 EMSGN(_(e_listidx), n1);
4930 return FAIL;
4931 }
4932 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004933 }
4934 if (range)
4935 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004936 list_T *l;
4937 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004938
4939 if (n2 < 0)
4940 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004941 else if (n2 >= len)
4942 n2 = len - 1;
4943 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004944 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 l = list_alloc();
4946 if (l == NULL)
4947 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004948 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004949 n1 <= n2; ++n1)
4950 {
4951 if (list_append_tv(l, &item->li_tv) == FAIL)
4952 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004953 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954 return FAIL;
4955 }
4956 item = item->li_next;
4957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004958 clear_tv(rettv);
4959 rettv->v_type = VAR_LIST;
4960 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004961 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004962 }
4963 else
4964 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004965 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004966 clear_tv(rettv);
4967 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004968 }
4969 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970
4971 case VAR_DICT:
4972 if (range)
4973 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004974 if (verbose)
4975 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 if (len == -1)
4977 clear_tv(&var1);
4978 return FAIL;
4979 }
4980 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004981 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982
4983 if (len == -1)
4984 {
4985 key = get_tv_string(&var1);
4986 if (*key == NUL)
4987 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004988 if (verbose)
4989 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 clear_tv(&var1);
4991 return FAIL;
4992 }
4993 }
4994
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004995 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004997 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004998 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 if (len == -1)
5000 clear_tv(&var1);
5001 if (item == NULL)
5002 return FAIL;
5003
5004 copy_tv(&item->di_tv, &var1);
5005 clear_tv(rettv);
5006 *rettv = var1;
5007 }
5008 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005009 }
5010 }
5011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005012 return OK;
5013}
5014
5015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 * Get an option value.
5017 * "arg" points to the '&' or '+' before the option name.
5018 * "arg" is advanced to character after the option name.
5019 * Return OK or FAIL.
5020 */
5021 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005022get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005024 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 int evaluate;
5026{
5027 char_u *option_end;
5028 long numval;
5029 char_u *stringval;
5030 int opt_type;
5031 int c;
5032 int working = (**arg == '+'); /* has("+option") */
5033 int ret = OK;
5034 int opt_flags;
5035
5036 /*
5037 * Isolate the option name and find its value.
5038 */
5039 option_end = find_option_end(arg, &opt_flags);
5040 if (option_end == NULL)
5041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005042 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 EMSG2(_("E112: Option name missing: %s"), *arg);
5044 return FAIL;
5045 }
5046
5047 if (!evaluate)
5048 {
5049 *arg = option_end;
5050 return OK;
5051 }
5052
5053 c = *option_end;
5054 *option_end = NUL;
5055 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
5058 if (opt_type == -3) /* invalid name */
5059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 EMSG2(_("E113: Unknown option: %s"), *arg);
5062 ret = FAIL;
5063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 if (opt_type == -2) /* hidden string option */
5067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->v_type = VAR_STRING;
5069 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 else if (opt_type == -1) /* hidden number option */
5072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 rettv->v_type = VAR_NUMBER;
5074 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 else if (opt_type == 1) /* number option */
5077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 rettv->v_type = VAR_NUMBER;
5079 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 else /* string option */
5082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 rettv->v_type = VAR_STRING;
5084 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 }
5087 else if (working && (opt_type == -2 || opt_type == -1))
5088 ret = FAIL;
5089
5090 *option_end = c; /* put back for error messages */
5091 *arg = option_end;
5092
5093 return ret;
5094}
5095
5096/*
5097 * Allocate a variable for a string constant.
5098 * Return OK or FAIL.
5099 */
5100 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 int evaluate;
5105{
5106 char_u *p;
5107 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 int extra = 0;
5109
5110 /*
5111 * Find the end of the string, skipping backslashed characters.
5112 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
5115 if (*p == '\\' && p[1] != NUL)
5116 {
5117 ++p;
5118 /* A "\<x>" form occupies at least 4 characters, and produces up
5119 * to 6 characters: reserve space for 2 extra */
5120 if (*p == '<')
5121 extra += 2;
5122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124
5125 if (*p != '"')
5126 {
5127 EMSG2(_("E114: Missing quote: %s"), *arg);
5128 return FAIL;
5129 }
5130
5131 /* If only parsing, set *arg and return here */
5132 if (!evaluate)
5133 {
5134 *arg = p + 1;
5135 return OK;
5136 }
5137
5138 /*
5139 * Copy the string into allocated memory, handling backslashed
5140 * characters.
5141 */
5142 name = alloc((unsigned)(p - *arg + extra));
5143 if (name == NULL)
5144 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 rettv->v_type = VAR_STRING;
5146 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
5150 if (*p == '\\')
5151 {
5152 switch (*++p)
5153 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 case 'b': *name++ = BS; ++p; break;
5155 case 'e': *name++ = ESC; ++p; break;
5156 case 'f': *name++ = FF; ++p; break;
5157 case 'n': *name++ = NL; ++p; break;
5158 case 'r': *name++ = CAR; ++p; break;
5159 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160
5161 case 'X': /* hex: "\x1", "\x12" */
5162 case 'x':
5163 case 'u': /* Unicode: "\u0023" */
5164 case 'U':
5165 if (vim_isxdigit(p[1]))
5166 {
5167 int n, nr;
5168 int c = toupper(*p);
5169
5170 if (c == 'X')
5171 n = 2;
5172 else
5173 n = 4;
5174 nr = 0;
5175 while (--n >= 0 && vim_isxdigit(p[1]))
5176 {
5177 ++p;
5178 nr = (nr << 4) + hex2nr(*p);
5179 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#ifdef FEAT_MBYTE
5182 /* For "\u" store the number according to
5183 * 'encoding'. */
5184 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 else
5187#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 break;
5191
5192 /* octal: "\1", "\12", "\123" */
5193 case '0':
5194 case '1':
5195 case '2':
5196 case '3':
5197 case '4':
5198 case '5':
5199 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 case '7': *name = *p++ - '0';
5201 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 *name = (*name << 3) + *p++ - '0';
5204 if (*p >= '0' && *p <= '7')
5205 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 break;
5209
5210 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (extra != 0)
5213 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216 }
5217 /* FALLTHROUGH */
5218
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 break;
5221 }
5222 }
5223 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 *arg = p + 1;
5229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 return OK;
5231}
5232
5233/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 * Return OK or FAIL.
5236 */
5237 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 int evaluate;
5242{
5243 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005244 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005245 int reduce = 0;
5246
5247 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 */
5250 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005253 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005255 break;
5256 ++reduce;
5257 ++p;
5258 }
5259 }
5260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005264 return FAIL;
5265 }
5266
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005268 if (!evaluate)
5269 {
5270 *arg = p + 1;
5271 return OK;
5272 }
5273
5274 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005276 */
5277 str = alloc((unsigned)((p - *arg) - reduce));
5278 if (str == NULL)
5279 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 rettv->v_type = VAR_STRING;
5281 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005282
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005284 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005286 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005288 break;
5289 ++p;
5290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005292 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005294 *arg = p + 1;
5295
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005296 return OK;
5297}
5298
5299/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 * Allocate a variable for a List and fill it from "*arg".
5301 * Return OK or FAIL.
5302 */
5303 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005304get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005306 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 int evaluate;
5308{
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 list_T *l = NULL;
5310 typval_T tv;
5311 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312
5313 if (evaluate)
5314 {
5315 l = list_alloc();
5316 if (l == NULL)
5317 return FAIL;
5318 }
5319
5320 *arg = skipwhite(*arg + 1);
5321 while (**arg != ']' && **arg != NUL)
5322 {
5323 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5324 goto failret;
5325 if (evaluate)
5326 {
5327 item = listitem_alloc();
5328 if (item != NULL)
5329 {
5330 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005331 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 list_append(l, item);
5333 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005334 else
5335 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 }
5337
5338 if (**arg == ']')
5339 break;
5340 if (**arg != ',')
5341 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 goto failret;
5344 }
5345 *arg = skipwhite(*arg + 1);
5346 }
5347
5348 if (**arg != ']')
5349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351failret:
5352 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005353 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 return FAIL;
5355 }
5356
5357 *arg = skipwhite(*arg + 1);
5358 if (evaluate)
5359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 rettv->v_type = VAR_LIST;
5361 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 ++l->lv_refcount;
5363 }
5364
5365 return OK;
5366}
5367
5368/*
5369 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005370 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005372 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373list_alloc()
5374{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005375 list_T *l;
5376
5377 l = (list_T *)alloc_clear(sizeof(list_T));
5378 if (l != NULL)
5379 {
5380 /* Prepend the list to the list of lists for garbage collection. */
5381 if (first_list != NULL)
5382 first_list->lv_used_prev = l;
5383 l->lv_used_prev = NULL;
5384 l->lv_used_next = first_list;
5385 first_list = l;
5386 }
5387 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388}
5389
5390/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005391 * Allocate an empty list for a return value.
5392 * Returns OK or FAIL.
5393 */
5394 static int
5395rettv_list_alloc(rettv)
5396 typval_T *rettv;
5397{
5398 list_T *l = list_alloc();
5399
5400 if (l == NULL)
5401 return FAIL;
5402
5403 rettv->vval.v_list = l;
5404 rettv->v_type = VAR_LIST;
5405 ++l->lv_refcount;
5406 return OK;
5407}
5408
5409/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 * Unreference a list: decrement the reference count and free it when it
5411 * becomes zero.
5412 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005413 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005417 if (l != NULL && --l->lv_refcount <= 0)
5418 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419}
5420
5421/*
5422 * Free a list, including all items it points to.
5423 * Ignores the reference count.
5424 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005425 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005426list_free(l, recurse)
5427 list_T *l;
5428 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429{
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005432 /* Remove the list from the list of lists for garbage collection. */
5433 if (l->lv_used_prev == NULL)
5434 first_list = l->lv_used_next;
5435 else
5436 l->lv_used_prev->lv_used_next = l->lv_used_next;
5437 if (l->lv_used_next != NULL)
5438 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5439
Bram Moolenaard9fba312005-06-26 22:34:35 +00005440 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005442 /* Remove the item before deleting it. */
5443 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005444 if (recurse || (item->li_tv.v_type != VAR_LIST
5445 && item->li_tv.v_type != VAR_DICT))
5446 clear_tv(&item->li_tv);
5447 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 }
5449 vim_free(l);
5450}
5451
5452/*
5453 * Allocate a list item.
5454 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456listitem_alloc()
5457{
Bram Moolenaar33570922005-01-25 22:26:29 +00005458 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459}
5460
5461/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005462 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 */
5464 static void
5465listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005466 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 vim_free(item);
5470}
5471
5472/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005473 * Remove a list item from a List and free it. Also clears the value.
5474 */
5475 static void
5476listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 list_T *l;
5478 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005479{
5480 list_remove(l, item, item);
5481 listitem_free(item);
5482}
5483
5484/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 * Get the number of items in a list.
5486 */
5487 static long
5488list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005489 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 if (l == NULL)
5492 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005493 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494}
5495
5496/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005497 * Return TRUE when two lists have exactly the same values.
5498 */
5499 static int
5500list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 list_T *l1;
5502 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005503 int ic; /* ignore case for strings */
5504{
Bram Moolenaar33570922005-01-25 22:26:29 +00005505 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005506
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005507 if (l1 == l2)
5508 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005509 if (list_len(l1) != list_len(l2))
5510 return FALSE;
5511
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005512 for (item1 = l1->lv_first, item2 = l2->lv_first;
5513 item1 != NULL && item2 != NULL;
5514 item1 = item1->li_next, item2 = item2->li_next)
5515 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5516 return FALSE;
5517 return item1 == NULL && item2 == NULL;
5518}
5519
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005520#if defined(FEAT_PYTHON) || defined(PROTO)
5521/*
5522 * Return the dictitem that an entry in a hashtable points to.
5523 */
5524 dictitem_T *
5525dict_lookup(hi)
5526 hashitem_T *hi;
5527{
5528 return HI2DI(hi);
5529}
5530#endif
5531
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005532/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005533 * Return TRUE when two dictionaries have exactly the same key/values.
5534 */
5535 static int
5536dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 dict_T *d1;
5538 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005539 int ic; /* ignore case for strings */
5540{
Bram Moolenaar33570922005-01-25 22:26:29 +00005541 hashitem_T *hi;
5542 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005543 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005544
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005545 if (d1 == d2)
5546 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005547 if (dict_len(d1) != dict_len(d2))
5548 return FALSE;
5549
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005550 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005553 if (!HASHITEM_EMPTY(hi))
5554 {
5555 item2 = dict_find(d2, hi->hi_key, -1);
5556 if (item2 == NULL)
5557 return FALSE;
5558 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5559 return FALSE;
5560 --todo;
5561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005562 }
5563 return TRUE;
5564}
5565
5566/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005567 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005568 * Compares the items just like "==" would compare them, but strings and
5569 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005570 */
5571 static int
5572tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 typval_T *tv1;
5574 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005575 int ic; /* ignore case */
5576{
5577 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005578 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005579 static int recursive = 0; /* cach recursive loops */
5580 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005581
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005582 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005583 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005584 /* Catch lists and dicts that have an endless loop by limiting
5585 * recursiveness to 1000. We guess they are equal then. */
5586 if (recursive >= 1000)
5587 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005588
5589 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005590 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005591 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005592 ++recursive;
5593 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5594 --recursive;
5595 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005596
5597 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005598 ++recursive;
5599 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5600 --recursive;
5601 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005602
5603 case VAR_FUNC:
5604 return (tv1->vval.v_string != NULL
5605 && tv2->vval.v_string != NULL
5606 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5607
5608 case VAR_NUMBER:
5609 return tv1->vval.v_number == tv2->vval.v_number;
5610
5611 case VAR_STRING:
5612 s1 = get_tv_string_buf(tv1, buf1);
5613 s2 = get_tv_string_buf(tv2, buf2);
5614 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005615 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005616
5617 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005618 return TRUE;
5619}
5620
5621/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 * Locate item with index "n" in list "l" and return it.
5623 * A negative index is counted from the end; -1 is the last item.
5624 * Returns NULL when "n" is out of range.
5625 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005626 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005628 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005629 long n;
5630{
Bram Moolenaar33570922005-01-25 22:26:29 +00005631 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 long idx;
5633
5634 if (l == NULL)
5635 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005636
5637 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005638 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005639 n = l->lv_len + n;
5640
5641 /* Check for index out of range. */
5642 if (n < 0 || n >= l->lv_len)
5643 return NULL;
5644
5645 /* When there is a cached index may start search from there. */
5646 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005648 if (n < l->lv_idx / 2)
5649 {
5650 /* closest to the start of the list */
5651 item = l->lv_first;
5652 idx = 0;
5653 }
5654 else if (n > (l->lv_idx + l->lv_len) / 2)
5655 {
5656 /* closest to the end of the list */
5657 item = l->lv_last;
5658 idx = l->lv_len - 1;
5659 }
5660 else
5661 {
5662 /* closest to the cached index */
5663 item = l->lv_idx_item;
5664 idx = l->lv_idx;
5665 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666 }
5667 else
5668 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005669 if (n < l->lv_len / 2)
5670 {
5671 /* closest to the start of the list */
5672 item = l->lv_first;
5673 idx = 0;
5674 }
5675 else
5676 {
5677 /* closest to the end of the list */
5678 item = l->lv_last;
5679 idx = l->lv_len - 1;
5680 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005682
5683 while (n > idx)
5684 {
5685 /* search forward */
5686 item = item->li_next;
5687 ++idx;
5688 }
5689 while (n < idx)
5690 {
5691 /* search backward */
5692 item = item->li_prev;
5693 --idx;
5694 }
5695
5696 /* cache the used index */
5697 l->lv_idx = idx;
5698 l->lv_idx_item = item;
5699
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700 return item;
5701}
5702
5703/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005704 * Get list item "l[idx]" as a number.
5705 */
5706 static long
5707list_find_nr(l, idx, errorp)
5708 list_T *l;
5709 long idx;
5710 int *errorp; /* set to TRUE when something wrong */
5711{
5712 listitem_T *li;
5713
5714 li = list_find(l, idx);
5715 if (li == NULL)
5716 {
5717 if (errorp != NULL)
5718 *errorp = TRUE;
5719 return -1L;
5720 }
5721 return get_tv_number_chk(&li->li_tv, errorp);
5722}
5723
5724/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005725 * Locate "item" list "l" and return its index.
5726 * Returns -1 when "item" is not in the list.
5727 */
5728 static long
5729list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 list_T *l;
5731 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005732{
5733 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005735
5736 if (l == NULL)
5737 return -1;
5738 idx = 0;
5739 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5740 ++idx;
5741 if (li == NULL)
5742 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005743 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005744}
5745
5746/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747 * Append item "item" to the end of list "l".
5748 */
5749 static void
5750list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 list_T *l;
5752 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753{
5754 if (l->lv_last == NULL)
5755 {
5756 /* empty list */
5757 l->lv_first = item;
5758 l->lv_last = item;
5759 item->li_prev = NULL;
5760 }
5761 else
5762 {
5763 l->lv_last->li_next = item;
5764 item->li_prev = l->lv_last;
5765 l->lv_last = item;
5766 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005767 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 item->li_next = NULL;
5769}
5770
5771/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005773 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774 */
5775 static int
5776list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005777 list_T *l;
5778 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005780 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781
Bram Moolenaar05159a02005-02-26 23:04:13 +00005782 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005784 copy_tv(tv, &li->li_tv);
5785 list_append(l, li);
5786 return OK;
5787}
5788
5789/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005790 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005791 * Return FAIL when out of memory.
5792 */
5793 int
5794list_append_dict(list, dict)
5795 list_T *list;
5796 dict_T *dict;
5797{
5798 listitem_T *li = listitem_alloc();
5799
5800 if (li == NULL)
5801 return FAIL;
5802 li->li_tv.v_type = VAR_DICT;
5803 li->li_tv.v_lock = 0;
5804 li->li_tv.vval.v_dict = dict;
5805 list_append(list, li);
5806 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 return OK;
5808}
5809
5810/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005811 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005812 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005813 * Returns FAIL when out of memory.
5814 */
5815 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005816list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005817 list_T *l;
5818 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005819 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005820{
5821 listitem_T *li = listitem_alloc();
5822
5823 if (li == NULL)
5824 return FAIL;
5825 list_append(l, li);
5826 li->li_tv.v_type = VAR_STRING;
5827 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005828 if (str == NULL)
5829 li->li_tv.vval.v_string = NULL;
5830 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005831 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005832 return FAIL;
5833 return OK;
5834}
5835
5836/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005837 * Append "n" to list "l".
5838 * Returns FAIL when out of memory.
5839 */
5840 static int
5841list_append_number(l, n)
5842 list_T *l;
5843 varnumber_T n;
5844{
5845 listitem_T *li;
5846
5847 li = listitem_alloc();
5848 if (li == NULL)
5849 return FAIL;
5850 li->li_tv.v_type = VAR_NUMBER;
5851 li->li_tv.v_lock = 0;
5852 li->li_tv.vval.v_number = n;
5853 list_append(l, li);
5854 return OK;
5855}
5856
5857/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005858 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005859 * If "item" is NULL append at the end.
5860 * Return FAIL when out of memory.
5861 */
5862 static int
5863list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 list_T *l;
5865 typval_T *tv;
5866 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005867{
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005869
5870 if (ni == NULL)
5871 return FAIL;
5872 copy_tv(tv, &ni->li_tv);
5873 if (item == NULL)
5874 /* Append new item at end of list. */
5875 list_append(l, ni);
5876 else
5877 {
5878 /* Insert new item before existing item. */
5879 ni->li_prev = item->li_prev;
5880 ni->li_next = item;
5881 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005882 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005883 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005884 ++l->lv_idx;
5885 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005886 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005887 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005888 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005889 l->lv_idx_item = NULL;
5890 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005891 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005892 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005893 }
5894 return OK;
5895}
5896
5897/*
5898 * Extend "l1" with "l2".
5899 * If "bef" is NULL append at the end, otherwise insert before this item.
5900 * Returns FAIL when out of memory.
5901 */
5902 static int
5903list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 list_T *l1;
5905 list_T *l2;
5906 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005907{
Bram Moolenaar33570922005-01-25 22:26:29 +00005908 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005909
5910 for (item = l2->lv_first; item != NULL; item = item->li_next)
5911 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5912 return FAIL;
5913 return OK;
5914}
5915
5916/*
5917 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5918 * Return FAIL when out of memory.
5919 */
5920 static int
5921list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 list_T *l1;
5923 list_T *l2;
5924 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005925{
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005927
5928 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005929 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005930 if (l == NULL)
5931 return FAIL;
5932 tv->v_type = VAR_LIST;
5933 tv->vval.v_list = l;
5934
5935 /* append all items from the second list */
5936 return list_extend(l, l2, NULL);
5937}
5938
5939/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005940 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 * Returns NULL when out of memory.
5944 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005946list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005949 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950{
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 list_T *copy;
5952 listitem_T *item;
5953 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954
5955 if (orig == NULL)
5956 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957
5958 copy = list_alloc();
5959 if (copy != NULL)
5960 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005961 if (copyID != 0)
5962 {
5963 /* Do this before adding the items, because one of the items may
5964 * refer back to this list. */
5965 orig->lv_copyID = copyID;
5966 orig->lv_copylist = copy;
5967 }
5968 for (item = orig->lv_first; item != NULL && !got_int;
5969 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 {
5971 ni = listitem_alloc();
5972 if (ni == NULL)
5973 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005974 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 {
5976 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5977 {
5978 vim_free(ni);
5979 break;
5980 }
5981 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005983 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 list_append(copy, ni);
5985 }
5986 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005987 if (item != NULL)
5988 {
5989 list_unref(copy);
5990 copy = NULL;
5991 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 }
5993
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 return copy;
5995}
5996
5997/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005999 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006002list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 list_T *l;
6004 listitem_T *item;
6005 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006{
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 /* notify watchers */
6010 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006012 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013 list_fix_watch(l, ip);
6014 if (ip == item2)
6015 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006017
6018 if (item2->li_next == NULL)
6019 l->lv_last = item->li_prev;
6020 else
6021 item2->li_next->li_prev = item->li_prev;
6022 if (item->li_prev == NULL)
6023 l->lv_first = item2->li_next;
6024 else
6025 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006026 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027}
6028
6029/*
6030 * Return an allocated string with the string representation of a list.
6031 * May return NULL.
6032 */
6033 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006034list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037{
6038 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039
6040 if (tv->vval.v_list == NULL)
6041 return NULL;
6042 ga_init2(&ga, (int)sizeof(char), 80);
6043 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006044 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006045 {
6046 vim_free(ga.ga_data);
6047 return NULL;
6048 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 ga_append(&ga, ']');
6050 ga_append(&ga, NUL);
6051 return (char_u *)ga.ga_data;
6052}
6053
6054/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006055 * Join list "l" into a string in "*gap", using separator "sep".
6056 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006057 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006058 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006059 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006060list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006061 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006063 char_u *sep;
6064 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006065 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006066{
6067 int first = TRUE;
6068 char_u *tofree;
6069 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006070 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006071 char_u *s;
6072
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006073 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006074 {
6075 if (first)
6076 first = FALSE;
6077 else
6078 ga_concat(gap, sep);
6079
6080 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006081 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006082 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006083 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006084 if (s != NULL)
6085 ga_concat(gap, s);
6086 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006087 if (s == NULL)
6088 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006089 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006090 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006091}
6092
6093/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006094 * Garbage collection for lists and dictionaries.
6095 *
6096 * We use reference counts to be able to free most items right away when they
6097 * are no longer used. But for composite items it's possible that it becomes
6098 * unused while the reference count is > 0: When there is a recursive
6099 * reference. Example:
6100 * :let l = [1, 2, 3]
6101 * :let d = {9: l}
6102 * :let l[1] = d
6103 *
6104 * Since this is quite unusual we handle this with garbage collection: every
6105 * once in a while find out which lists and dicts are not referenced from any
6106 * variable.
6107 *
6108 * Here is a good reference text about garbage collection (refers to Python
6109 * but it applies to all reference-counting mechanisms):
6110 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006111 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006112
6113/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006114 * Do garbage collection for lists and dicts.
6115 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006116 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006117 int
6118garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006119{
6120 dict_T *dd;
6121 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006122 int copyID = ++current_copyID;
6123 buf_T *buf;
6124 win_T *wp;
6125 int i;
6126 funccall_T *fc;
6127 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006128#ifdef FEAT_WINDOWS
6129 tabpage_T *tp;
6130#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006131
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006132 /* Only do this once. */
6133 want_garbage_collect = FALSE;
6134 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006135 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006136
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006137 /*
6138 * 1. Go through all accessible variables and mark all lists and dicts
6139 * with copyID.
6140 */
6141 /* script-local variables */
6142 for (i = 1; i <= ga_scripts.ga_len; ++i)
6143 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6144
6145 /* buffer-local variables */
6146 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6147 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6148
6149 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006150 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006151 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6152
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006153#ifdef FEAT_WINDOWS
6154 /* tabpage-local variables */
6155 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6156 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6157#endif
6158
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006159 /* global variables */
6160 set_ref_in_ht(&globvarht, copyID);
6161
6162 /* function-local variables */
6163 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6164 {
6165 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6166 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6167 }
6168
6169 /*
6170 * 2. Go through the list of dicts and free items without the copyID.
6171 */
6172 for (dd = first_dict; dd != NULL; )
6173 if (dd->dv_copyID != copyID)
6174 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006175 /* Free the Dictionary and ordinary items it contains, but don't
6176 * recurse into Lists and Dictionaries, they will be in the list
6177 * of dicts or list of lists. */
6178 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006179 did_free = TRUE;
6180
6181 /* restart, next dict may also have been freed */
6182 dd = first_dict;
6183 }
6184 else
6185 dd = dd->dv_used_next;
6186
6187 /*
6188 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006189 * But don't free a list that has a watcher (used in a for loop), these
6190 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006191 */
6192 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006193 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006194 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006195 /* Free the List and ordinary items it contains, but don't recurse
6196 * into Lists and Dictionaries, they will be in the list of dicts
6197 * or list of lists. */
6198 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006199 did_free = TRUE;
6200
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006201 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006202 ll = first_list;
6203 }
6204 else
6205 ll = ll->lv_used_next;
6206
6207 return did_free;
6208}
6209
6210/*
6211 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6212 */
6213 static void
6214set_ref_in_ht(ht, copyID)
6215 hashtab_T *ht;
6216 int copyID;
6217{
6218 int todo;
6219 hashitem_T *hi;
6220
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006221 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006222 for (hi = ht->ht_array; todo > 0; ++hi)
6223 if (!HASHITEM_EMPTY(hi))
6224 {
6225 --todo;
6226 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6227 }
6228}
6229
6230/*
6231 * Mark all lists and dicts referenced through list "l" with "copyID".
6232 */
6233 static void
6234set_ref_in_list(l, copyID)
6235 list_T *l;
6236 int copyID;
6237{
6238 listitem_T *li;
6239
6240 for (li = l->lv_first; li != NULL; li = li->li_next)
6241 set_ref_in_item(&li->li_tv, copyID);
6242}
6243
6244/*
6245 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6246 */
6247 static void
6248set_ref_in_item(tv, copyID)
6249 typval_T *tv;
6250 int copyID;
6251{
6252 dict_T *dd;
6253 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006254
6255 switch (tv->v_type)
6256 {
6257 case VAR_DICT:
6258 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006259 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006260 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006261 /* Didn't see this dict yet. */
6262 dd->dv_copyID = copyID;
6263 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006264 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006265 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006266
6267 case VAR_LIST:
6268 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006269 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006270 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006271 /* Didn't see this list yet. */
6272 ll->lv_copyID = copyID;
6273 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006274 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006275 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006276 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006277 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006278}
6279
6280/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006281 * Allocate an empty header for a dictionary.
6282 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006283 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006284dict_alloc()
6285{
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006289 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006290 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006291 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006292 if (first_dict != NULL)
6293 first_dict->dv_used_prev = d;
6294 d->dv_used_next = first_dict;
6295 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006296 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006297
Bram Moolenaar33570922005-01-25 22:26:29 +00006298 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006299 d->dv_lock = 0;
6300 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006301 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006302 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006303 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006304}
6305
6306/*
6307 * Unreference a Dictionary: decrement the reference count and free it when it
6308 * becomes zero.
6309 */
6310 static void
6311dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006314 if (d != NULL && --d->dv_refcount <= 0)
6315 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316}
6317
6318/*
6319 * Free a Dictionary, including all items it contains.
6320 * Ignores the reference count.
6321 */
6322 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006323dict_free(d, recurse)
6324 dict_T *d;
6325 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006326{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006327 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006328 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006329 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006331 /* Remove the dict from the list of dicts for garbage collection. */
6332 if (d->dv_used_prev == NULL)
6333 first_dict = d->dv_used_next;
6334 else
6335 d->dv_used_prev->dv_used_next = d->dv_used_next;
6336 if (d->dv_used_next != NULL)
6337 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6338
6339 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006340 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006341 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006343 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344 if (!HASHITEM_EMPTY(hi))
6345 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006346 /* Remove the item before deleting it, just in case there is
6347 * something recursive causing trouble. */
6348 di = HI2DI(hi);
6349 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006350 if (recurse || (di->di_tv.v_type != VAR_LIST
6351 && di->di_tv.v_type != VAR_DICT))
6352 clear_tv(&di->di_tv);
6353 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006354 --todo;
6355 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006356 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006358 vim_free(d);
6359}
6360
6361/*
6362 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006363 * The "key" is copied to the new item.
6364 * Note that the value of the item "di_tv" still needs to be initialized!
6365 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006366 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006368dictitem_alloc(key)
6369 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006370{
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006373 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006375 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006376 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006377 di->di_flags = 0;
6378 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006380}
6381
6382/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383 * Make a copy of a Dictionary item.
6384 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388{
Bram Moolenaar33570922005-01-25 22:26:29 +00006389 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006391 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6392 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393 if (di != NULL)
6394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006395 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006396 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006397 copy_tv(&org->di_tv, &di->di_tv);
6398 }
6399 return di;
6400}
6401
6402/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006403 * Remove item "item" from Dictionary "dict" and free it.
6404 */
6405 static void
6406dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 dict_T *dict;
6408 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006409{
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006411
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006413 if (HASHITEM_EMPTY(hi))
6414 EMSG2(_(e_intern2), "dictitem_remove()");
6415 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417 dictitem_free(item);
6418}
6419
6420/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421 * Free a dict item. Also clears the value.
6422 */
6423 static void
6424dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427 clear_tv(&item->di_tv);
6428 vim_free(item);
6429}
6430
6431/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6433 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006435 * Returns NULL when out of memory.
6436 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006437 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006440 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006441 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006442{
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 dict_T *copy;
6444 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006445 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006447
6448 if (orig == NULL)
6449 return NULL;
6450
6451 copy = dict_alloc();
6452 if (copy != NULL)
6453 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006454 if (copyID != 0)
6455 {
6456 orig->dv_copyID = copyID;
6457 orig->dv_copydict = copy;
6458 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006459 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006460 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006462 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006463 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006464 --todo;
6465
6466 di = dictitem_alloc(hi->hi_key);
6467 if (di == NULL)
6468 break;
6469 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006470 {
6471 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6472 copyID) == FAIL)
6473 {
6474 vim_free(di);
6475 break;
6476 }
6477 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006478 else
6479 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6480 if (dict_add(copy, di) == FAIL)
6481 {
6482 dictitem_free(di);
6483 break;
6484 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006487
Bram Moolenaare9a41262005-01-15 22:18:47 +00006488 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006489 if (todo > 0)
6490 {
6491 dict_unref(copy);
6492 copy = NULL;
6493 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 }
6495
6496 return copy;
6497}
6498
6499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006503 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 dict_T *d;
6506 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507{
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006509}
6510
Bram Moolenaar8c711452005-01-14 21:53:12 +00006511/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006512 * Add a number or string entry to dictionary "d".
6513 * When "str" is NULL use number "nr", otherwise use "str".
6514 * Returns FAIL when out of memory and when key already exists.
6515 */
6516 int
6517dict_add_nr_str(d, key, nr, str)
6518 dict_T *d;
6519 char *key;
6520 long nr;
6521 char_u *str;
6522{
6523 dictitem_T *item;
6524
6525 item = dictitem_alloc((char_u *)key);
6526 if (item == NULL)
6527 return FAIL;
6528 item->di_tv.v_lock = 0;
6529 if (str == NULL)
6530 {
6531 item->di_tv.v_type = VAR_NUMBER;
6532 item->di_tv.vval.v_number = nr;
6533 }
6534 else
6535 {
6536 item->di_tv.v_type = VAR_STRING;
6537 item->di_tv.vval.v_string = vim_strsave(str);
6538 }
6539 if (dict_add(d, item) == FAIL)
6540 {
6541 dictitem_free(item);
6542 return FAIL;
6543 }
6544 return OK;
6545}
6546
6547/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548 * Get the number of items in a Dictionary.
6549 */
6550 static long
6551dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 if (d == NULL)
6555 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006556 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557}
6558
6559/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006560 * Find item "key[len]" in Dictionary "d".
6561 * If "len" is negative use strlen(key).
6562 * Returns NULL when not found.
6563 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006565dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006567 char_u *key;
6568 int len;
6569{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006570#define AKEYLEN 200
6571 char_u buf[AKEYLEN];
6572 char_u *akey;
6573 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006575
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006576 if (len < 0)
6577 akey = key;
6578 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006579 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006580 tofree = akey = vim_strnsave(key, len);
6581 if (akey == NULL)
6582 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006583 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006584 else
6585 {
6586 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006587 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006588 akey = buf;
6589 }
6590
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006592 vim_free(tofree);
6593 if (HASHITEM_EMPTY(hi))
6594 return NULL;
6595 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006596}
6597
6598/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006599 * Get a string item from a dictionary.
6600 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006601 * Returns NULL if the entry doesn't exist or out of memory.
6602 */
6603 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006604get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006605 dict_T *d;
6606 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006607 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006608{
6609 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006610 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006611
6612 di = dict_find(d, key, -1);
6613 if (di == NULL)
6614 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006615 s = get_tv_string(&di->di_tv);
6616 if (save && s != NULL)
6617 s = vim_strsave(s);
6618 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006619}
6620
6621/*
6622 * Get a number item from a dictionary.
6623 * Returns 0 if the entry doesn't exist or out of memory.
6624 */
6625 long
6626get_dict_number(d, key)
6627 dict_T *d;
6628 char_u *key;
6629{
6630 dictitem_T *di;
6631
6632 di = dict_find(d, key, -1);
6633 if (di == NULL)
6634 return 0;
6635 return get_tv_number(&di->di_tv);
6636}
6637
6638/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006639 * Return an allocated string with the string representation of a Dictionary.
6640 * May return NULL.
6641 */
6642 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006643dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006644 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006645 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006646{
6647 garray_T ga;
6648 int first = TRUE;
6649 char_u *tofree;
6650 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006652 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006653 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006654 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006655
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006656 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006657 return NULL;
6658 ga_init2(&ga, (int)sizeof(char), 80);
6659 ga_append(&ga, '{');
6660
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006661 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006662 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006663 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006664 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006665 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006666 --todo;
6667
6668 if (first)
6669 first = FALSE;
6670 else
6671 ga_concat(&ga, (char_u *)", ");
6672
6673 tofree = string_quote(hi->hi_key, FALSE);
6674 if (tofree != NULL)
6675 {
6676 ga_concat(&ga, tofree);
6677 vim_free(tofree);
6678 }
6679 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006680 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006681 if (s != NULL)
6682 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006684 if (s == NULL)
6685 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006687 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006688 if (todo > 0)
6689 {
6690 vim_free(ga.ga_data);
6691 return NULL;
6692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006693
6694 ga_append(&ga, '}');
6695 ga_append(&ga, NUL);
6696 return (char_u *)ga.ga_data;
6697}
6698
6699/*
6700 * Allocate a variable for a Dictionary and fill it from "*arg".
6701 * Return OK or FAIL. Returns NOTDONE for {expr}.
6702 */
6703 static int
6704get_dict_tv(arg, rettv, evaluate)
6705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006706 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707 int evaluate;
6708{
Bram Moolenaar33570922005-01-25 22:26:29 +00006709 dict_T *d = NULL;
6710 typval_T tvkey;
6711 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00006712 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006713 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006714 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006715 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006716
6717 /*
6718 * First check if it's not a curly-braces thing: {expr}.
6719 * Must do this without evaluating, otherwise a function may be called
6720 * twice. Unfortunately this means we need to call eval1() twice for the
6721 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006722 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006723 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006724 if (*start != '}')
6725 {
6726 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6727 return FAIL;
6728 if (*start == '}')
6729 return NOTDONE;
6730 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006731
6732 if (evaluate)
6733 {
6734 d = dict_alloc();
6735 if (d == NULL)
6736 return FAIL;
6737 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006738 tvkey.v_type = VAR_UNKNOWN;
6739 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006740
6741 *arg = skipwhite(*arg + 1);
6742 while (**arg != '}' && **arg != NUL)
6743 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006744 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006745 goto failret;
6746 if (**arg != ':')
6747 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006748 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006749 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006750 goto failret;
6751 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006752 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006753 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006754 key = get_tv_string_buf_chk(&tvkey, buf);
6755 if (key == NULL || *key == NUL)
6756 {
6757 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6758 if (key != NULL)
6759 EMSG(_(e_emptykey));
6760 clear_tv(&tvkey);
6761 goto failret;
6762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006764
6765 *arg = skipwhite(*arg + 1);
6766 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6767 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006768 if (evaluate)
6769 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770 goto failret;
6771 }
6772 if (evaluate)
6773 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006774 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006775 if (item != NULL)
6776 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006777 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006778 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006779 clear_tv(&tv);
6780 goto failret;
6781 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006782 item = dictitem_alloc(key);
6783 clear_tv(&tvkey);
6784 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006786 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006787 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006788 if (dict_add(d, item) == FAIL)
6789 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006790 }
6791 }
6792
6793 if (**arg == '}')
6794 break;
6795 if (**arg != ',')
6796 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006797 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006798 goto failret;
6799 }
6800 *arg = skipwhite(*arg + 1);
6801 }
6802
6803 if (**arg != '}')
6804 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006805 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006806failret:
6807 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006808 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006809 return FAIL;
6810 }
6811
6812 *arg = skipwhite(*arg + 1);
6813 if (evaluate)
6814 {
6815 rettv->v_type = VAR_DICT;
6816 rettv->vval.v_dict = d;
6817 ++d->dv_refcount;
6818 }
6819
6820 return OK;
6821}
6822
Bram Moolenaar8c711452005-01-14 21:53:12 +00006823/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 * Return a string with the string representation of a variable.
6825 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006826 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006827 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006828 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006829 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006830 */
6831 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006832echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006833 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006835 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006836 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006837{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006838 static int recurse = 0;
6839 char_u *r = NULL;
6840
Bram Moolenaar33570922005-01-25 22:26:29 +00006841 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006842 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006843 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006844 *tofree = NULL;
6845 return NULL;
6846 }
6847 ++recurse;
6848
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849 switch (tv->v_type)
6850 {
6851 case VAR_FUNC:
6852 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006853 r = tv->vval.v_string;
6854 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006856 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006857 if (tv->vval.v_list == NULL)
6858 {
6859 *tofree = NULL;
6860 r = NULL;
6861 }
6862 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6863 {
6864 *tofree = NULL;
6865 r = (char_u *)"[...]";
6866 }
6867 else
6868 {
6869 tv->vval.v_list->lv_copyID = copyID;
6870 *tofree = list2string(tv, copyID);
6871 r = *tofree;
6872 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006873 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006874
Bram Moolenaar8c711452005-01-14 21:53:12 +00006875 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006876 if (tv->vval.v_dict == NULL)
6877 {
6878 *tofree = NULL;
6879 r = NULL;
6880 }
6881 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6882 {
6883 *tofree = NULL;
6884 r = (char_u *)"{...}";
6885 }
6886 else
6887 {
6888 tv->vval.v_dict->dv_copyID = copyID;
6889 *tofree = dict2string(tv, copyID);
6890 r = *tofree;
6891 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006892 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006894 case VAR_STRING:
6895 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006896 *tofree = NULL;
6897 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006898 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006899
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006900 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006903 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006904
6905 --recurse;
6906 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006907}
6908
6909/*
6910 * Return a string with the string representation of a variable.
6911 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6912 * "numbuf" is used for a number.
6913 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006914 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006915 */
6916 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006917tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006919 char_u **tofree;
6920 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006921 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006922{
6923 switch (tv->v_type)
6924 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006925 case VAR_FUNC:
6926 *tofree = string_quote(tv->vval.v_string, TRUE);
6927 return *tofree;
6928 case VAR_STRING:
6929 *tofree = string_quote(tv->vval.v_string, FALSE);
6930 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006931 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006932 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006933 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006934 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006935 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006936 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006937 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006938 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006939}
6940
6941/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 * Return string "str" in ' quotes, doubling ' characters.
6943 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006944 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006945 */
6946 static char_u *
6947string_quote(str, function)
6948 char_u *str;
6949 int function;
6950{
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006952 char_u *p, *r, *s;
6953
Bram Moolenaar33570922005-01-25 22:26:29 +00006954 len = (function ? 13 : 3);
6955 if (str != NULL)
6956 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006957 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 for (p = str; *p != NUL; mb_ptr_adv(p))
6959 if (*p == '\'')
6960 ++len;
6961 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006962 s = r = alloc(len);
6963 if (r != NULL)
6964 {
6965 if (function)
6966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006967 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006968 r += 10;
6969 }
6970 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006971 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 if (str != NULL)
6973 for (p = str; *p != NUL; )
6974 {
6975 if (*p == '\'')
6976 *r++ = '\'';
6977 MB_COPY_CHAR(p, r);
6978 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006979 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006980 if (function)
6981 *r++ = ')';
6982 *r++ = NUL;
6983 }
6984 return s;
6985}
6986
6987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 * Get the value of an environment variable.
6989 * "arg" is pointing to the '$'. It is advanced to after the name.
6990 * If the environment variable was not set, silently assume it is empty.
6991 * Always return OK.
6992 */
6993 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006994get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 int evaluate;
6998{
6999 char_u *string = NULL;
7000 int len;
7001 int cc;
7002 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007003 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 ++*arg;
7006 name = *arg;
7007 len = get_env_len(arg);
7008 if (evaluate)
7009 {
7010 if (len != 0)
7011 {
7012 cc = name[len];
7013 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007014 /* first try vim_getenv(), fast for normal environment vars */
7015 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007017 {
7018 if (!mustfree)
7019 string = vim_strsave(string);
7020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 else
7022 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007023 if (mustfree)
7024 vim_free(string);
7025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 /* next try expanding things like $VIM and ${HOME} */
7027 string = expand_env_save(name - 1);
7028 if (string != NULL && *string == '$')
7029 {
7030 vim_free(string);
7031 string = NULL;
7032 }
7033 }
7034 name[len] = cc;
7035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007036 rettv->v_type = VAR_STRING;
7037 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039
7040 return OK;
7041}
7042
7043/*
7044 * Array with names and number of arguments of all internal functions
7045 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7046 */
7047static struct fst
7048{
7049 char *f_name; /* function name */
7050 char f_min_argc; /* minimal number of arguments */
7051 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007052 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007053 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054} functions[] =
7055{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007056 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {"append", 2, 2, f_append},
7058 {"argc", 0, 0, f_argc},
7059 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007060 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007062 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {"bufexists", 1, 1, f_bufexists},
7064 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7065 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7066 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7067 {"buflisted", 1, 1, f_buflisted},
7068 {"bufloaded", 1, 1, f_bufloaded},
7069 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007070 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {"bufwinnr", 1, 1, f_bufwinnr},
7072 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007073 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007074 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007075 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"char2nr", 1, 1, f_char2nr},
7077 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007078 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007080#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007081 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007082 {"complete_add", 1, 1, f_complete_add},
7083 {"complete_check", 0, 0, f_complete_check},
7084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007086 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007087 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007089 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007090 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {"delete", 1, 1, f_delete},
7092 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007093 {"diff_filler", 1, 1, f_diff_filler},
7094 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007095 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007097 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {"eventhandler", 0, 0, f_eventhandler},
7099 {"executable", 1, 1, f_executable},
7100 {"exists", 1, 1, f_exists},
7101 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007102 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007103 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7105 {"filereadable", 1, 1, f_filereadable},
7106 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007107 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007108 {"finddir", 1, 3, f_finddir},
7109 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {"fnamemodify", 2, 2, f_fnamemodify},
7111 {"foldclosed", 1, 1, f_foldclosed},
7112 {"foldclosedend", 1, 1, f_foldclosedend},
7113 {"foldlevel", 1, 1, f_foldlevel},
7114 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007115 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007117 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007118 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007119 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007120 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {"getbufvar", 2, 2, f_getbufvar},
7122 {"getchar", 0, 1, f_getchar},
7123 {"getcharmod", 0, 0, f_getcharmod},
7124 {"getcmdline", 0, 0, f_getcmdline},
7125 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007126 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007128 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007129 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {"getfsize", 1, 1, f_getfsize},
7131 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007132 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007133 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007134 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007135 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007136 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007137 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007138 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007139 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007141 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {"getwinposx", 0, 0, f_getwinposx},
7143 {"getwinposy", 0, 0, f_getwinposy},
7144 {"getwinvar", 2, 2, f_getwinvar},
7145 {"glob", 1, 1, f_glob},
7146 {"globpath", 2, 2, f_globpath},
7147 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007149 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007150 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7152 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7153 {"histadd", 2, 2, f_histadd},
7154 {"histdel", 1, 2, f_histdel},
7155 {"histget", 1, 2, f_histget},
7156 {"histnr", 1, 1, f_histnr},
7157 {"hlID", 1, 1, f_hlID},
7158 {"hlexists", 1, 1, f_hlexists},
7159 {"hostname", 0, 0, f_hostname},
7160 {"iconv", 3, 3, f_iconv},
7161 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007162 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007163 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007165 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {"inputrestore", 0, 0, f_inputrestore},
7167 {"inputsave", 0, 0, f_inputsave},
7168 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007169 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007171 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007173 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007176 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {"libcall", 3, 3, f_libcall},
7178 {"libcallnr", 3, 3, f_libcallnr},
7179 {"line", 1, 1, f_line},
7180 {"line2byte", 1, 1, f_line2byte},
7181 {"lispindent", 1, 1, f_lispindent},
7182 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007183 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007184 {"maparg", 1, 3, f_maparg},
7185 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007186 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007187 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007188 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007189 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007190 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007191 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007192 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007193 {"max", 1, 1, f_max},
7194 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007195#ifdef vim_mkdir
7196 {"mkdir", 1, 3, f_mkdir},
7197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {"mode", 0, 0, f_mode},
7199 {"nextnonblank", 1, 1, f_nextnonblank},
7200 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007201 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007203 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007204 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007206 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007207 {"reltime", 0, 2, f_reltime},
7208 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {"remote_expr", 2, 3, f_remote_expr},
7210 {"remote_foreground", 1, 1, f_remote_foreground},
7211 {"remote_peek", 1, 2, f_remote_peek},
7212 {"remote_read", 1, 1, f_remote_read},
7213 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007214 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007216 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007218 {"reverse", 1, 1, f_reverse},
Bram Moolenaar76929292008-01-06 19:07:36 +00007219 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007220 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007221 {"searchpair", 3, 7, f_searchpair},
7222 {"searchpairpos", 3, 7, f_searchpairpos},
7223 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {"server2client", 2, 2, f_server2client},
7225 {"serverlist", 0, 0, f_serverlist},
7226 {"setbufvar", 3, 3, f_setbufvar},
7227 {"setcmdpos", 1, 1, f_setcmdpos},
7228 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007229 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007230 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007231 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007232 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007234 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007236 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007238 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007239 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007240 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007241 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007242 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007243 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244#ifdef HAVE_STRFTIME
7245 {"strftime", 1, 2, f_strftime},
7246#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007248 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {"strlen", 1, 1, f_strlen},
7250 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007251 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {"strtrans", 1, 1, f_strtrans},
7253 {"submatch", 1, 1, f_submatch},
7254 {"substitute", 4, 4, f_substitute},
7255 {"synID", 3, 3, f_synID},
7256 {"synIDattr", 2, 3, f_synIDattr},
7257 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007258 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007259 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007260 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007261 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007262 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007263 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007264 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007266 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {"tolower", 1, 1, f_tolower},
7268 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007269 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {"virtcol", 1, 1, f_virtcol},
7273 {"visualmode", 0, 1, f_visualmode},
7274 {"winbufnr", 1, 1, f_winbufnr},
7275 {"wincol", 0, 0, f_wincol},
7276 {"winheight", 1, 1, f_winheight},
7277 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007278 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007280 {"winrestview", 1, 1, f_winrestview},
7281 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007283 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284};
7285
7286#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7287
7288/*
7289 * Function given to ExpandGeneric() to obtain the list of internal
7290 * or user defined function names.
7291 */
7292 char_u *
7293get_function_name(xp, idx)
7294 expand_T *xp;
7295 int idx;
7296{
7297 static int intidx = -1;
7298 char_u *name;
7299
7300 if (idx == 0)
7301 intidx = -1;
7302 if (intidx < 0)
7303 {
7304 name = get_user_func_name(xp, idx);
7305 if (name != NULL)
7306 return name;
7307 }
7308 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7309 {
7310 STRCPY(IObuff, functions[intidx].f_name);
7311 STRCAT(IObuff, "(");
7312 if (functions[intidx].f_max_argc == 0)
7313 STRCAT(IObuff, ")");
7314 return IObuff;
7315 }
7316
7317 return NULL;
7318}
7319
7320/*
7321 * Function given to ExpandGeneric() to obtain the list of internal or
7322 * user defined variable or function names.
7323 */
7324/*ARGSUSED*/
7325 char_u *
7326get_expr_name(xp, idx)
7327 expand_T *xp;
7328 int idx;
7329{
7330 static int intidx = -1;
7331 char_u *name;
7332
7333 if (idx == 0)
7334 intidx = -1;
7335 if (intidx < 0)
7336 {
7337 name = get_function_name(xp, idx);
7338 if (name != NULL)
7339 return name;
7340 }
7341 return get_user_var_name(xp, ++intidx);
7342}
7343
7344#endif /* FEAT_CMDL_COMPL */
7345
7346/*
7347 * Find internal function in table above.
7348 * Return index, or -1 if not found
7349 */
7350 static int
7351find_internal_func(name)
7352 char_u *name; /* name of the function */
7353{
7354 int first = 0;
7355 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7356 int cmp;
7357 int x;
7358
7359 /*
7360 * Find the function name in the table. Binary search.
7361 */
7362 while (first <= last)
7363 {
7364 x = first + ((unsigned)(last - first) >> 1);
7365 cmp = STRCMP(name, functions[x].f_name);
7366 if (cmp < 0)
7367 last = x - 1;
7368 else if (cmp > 0)
7369 first = x + 1;
7370 else
7371 return x;
7372 }
7373 return -1;
7374}
7375
7376/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007377 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7378 * name it contains, otherwise return "name".
7379 */
7380 static char_u *
7381deref_func_name(name, lenp)
7382 char_u *name;
7383 int *lenp;
7384{
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 int cc;
7387
7388 cc = name[*lenp];
7389 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007390 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007393 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007395 {
7396 *lenp = 0;
7397 return (char_u *)""; /* just in case */
7398 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007399 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401 }
7402
7403 return name;
7404}
7405
7406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * Allocate a variable for the result of a function.
7408 * Return OK or FAIL.
7409 */
7410 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7412 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 char_u *name; /* name of the function */
7414 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 char_u **arg; /* argument, pointing to the '(' */
7417 linenr_T firstline; /* first line of range */
7418 linenr_T lastline; /* last line of range */
7419 int *doesrange; /* return: function handled range */
7420 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
7423 char_u *argp;
7424 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007425 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 int argcount = 0; /* number of arguments found */
7427
7428 /*
7429 * Get the arguments.
7430 */
7431 argp = *arg;
7432 while (argcount < MAX_FUNC_ARGS)
7433 {
7434 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7435 if (*argp == ')' || *argp == ',' || *argp == NUL)
7436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7438 {
7439 ret = FAIL;
7440 break;
7441 }
7442 ++argcount;
7443 if (*argp != ',')
7444 break;
7445 }
7446 if (*argp == ')')
7447 ++argp;
7448 else
7449 ret = FAIL;
7450
7451 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007452 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 {
7456 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007457 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
7462 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007463 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
7465 *arg = skipwhite(argp);
7466 return ret;
7467}
7468
7469
7470/*
7471 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007472 * Return OK when the function can't be called, FAIL otherwise.
7473 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 */
7475 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 char_u *name; /* name of the function */
7479 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007482 typval_T *argvars; /* vars for arguments, must have "argcount"
7483 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 linenr_T firstline; /* first line of range */
7485 linenr_T lastline; /* last line of range */
7486 int *doesrange; /* return: function handled range */
7487 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491#define ERROR_UNKNOWN 0
7492#define ERROR_TOOMANY 1
7493#define ERROR_TOOFEW 2
7494#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495#define ERROR_DICT 4
7496#define ERROR_NONE 5
7497#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 int error = ERROR_NONE;
7499 int i;
7500 int llen;
7501 ufunc_T *fp;
7502 int cc;
7503#define FLEN_FIXED 40
7504 char_u fname_buf[FLEN_FIXED + 1];
7505 char_u *fname;
7506
7507 /*
7508 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7509 * Change <SNR>123_name() to K_SNR 123_name().
7510 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7511 */
7512 cc = name[len];
7513 name[len] = NUL;
7514 llen = eval_fname_script(name);
7515 if (llen > 0)
7516 {
7517 fname_buf[0] = K_SPECIAL;
7518 fname_buf[1] = KS_EXTRA;
7519 fname_buf[2] = (int)KE_SNR;
7520 i = 3;
7521 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7522 {
7523 if (current_SID <= 0)
7524 error = ERROR_SCRIPT;
7525 else
7526 {
7527 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7528 i = (int)STRLEN(fname_buf);
7529 }
7530 }
7531 if (i + STRLEN(name + llen) < FLEN_FIXED)
7532 {
7533 STRCPY(fname_buf + i, name + llen);
7534 fname = fname_buf;
7535 }
7536 else
7537 {
7538 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7539 if (fname == NULL)
7540 error = ERROR_OTHER;
7541 else
7542 {
7543 mch_memmove(fname, fname_buf, (size_t)i);
7544 STRCPY(fname + i, name + llen);
7545 }
7546 }
7547 }
7548 else
7549 fname = name;
7550
7551 *doesrange = FALSE;
7552
7553
7554 /* execute the function if no errors detected and executing */
7555 if (evaluate && error == ERROR_NONE)
7556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 error = ERROR_UNKNOWN;
7559
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007560 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {
7562 /*
7563 * User defined function.
7564 */
7565 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007568 /* Trigger FuncUndefined event, may load the function. */
7569 if (fp == NULL
7570 && apply_autocmds(EVENT_FUNCUNDEFINED,
7571 fname, fname, TRUE, NULL)
7572 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007574 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 fp = find_func(fname);
7576 }
7577#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007578 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007579 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007580 {
7581 /* loaded a package, search for the function again */
7582 fp = find_func(fname);
7583 }
7584
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 if (fp != NULL)
7586 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007587 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007589 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007591 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007593 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007594 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 else
7596 {
7597 /*
7598 * Call the user function.
7599 * Save and restore search patterns, script variables and
7600 * redo buffer.
7601 */
7602 save_search_patterns();
7603 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007604 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007605 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007606 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007607 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7608 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7609 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007610 /* Function was unreferenced while being used, free it
7611 * now. */
7612 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 restoreRedobuff();
7614 restore_search_patterns();
7615 error = ERROR_NONE;
7616 }
7617 }
7618 }
7619 else
7620 {
7621 /*
7622 * Find the function name in the table, call its implementation.
7623 */
7624 i = find_internal_func(fname);
7625 if (i >= 0)
7626 {
7627 if (argcount < functions[i].f_min_argc)
7628 error = ERROR_TOOFEW;
7629 else if (argcount > functions[i].f_max_argc)
7630 error = ERROR_TOOMANY;
7631 else
7632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007634 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 error = ERROR_NONE;
7636 }
7637 }
7638 }
7639 /*
7640 * The function call (or "FuncUndefined" autocommand sequence) might
7641 * have been aborted by an error, an interrupt, or an explicitly thrown
7642 * exception that has not been caught so far. This situation can be
7643 * tested for by calling aborting(). For an error in an internal
7644 * function or for the "E132" error in call_user_func(), however, the
7645 * throw point at which the "force_abort" flag (temporarily reset by
7646 * emsg()) is normally updated has not been reached yet. We need to
7647 * update that flag first to make aborting() reliable.
7648 */
7649 update_force_abort();
7650 }
7651 if (error == ERROR_NONE)
7652 ret = OK;
7653
7654 /*
7655 * Report an error unless the argument evaluation or function call has been
7656 * cancelled due to an aborting error, an interrupt, or an exception.
7657 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 if (!aborting())
7659 {
7660 switch (error)
7661 {
7662 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007663 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 break;
7665 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007666 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 break;
7668 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007669 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 name);
7671 break;
7672 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007673 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 name);
7675 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007677 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007678 name);
7679 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 }
7681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
7683 name[len] = cc;
7684 if (fname != name && fname != fname_buf)
7685 vim_free(fname);
7686
7687 return ret;
7688}
7689
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007690/*
7691 * Give an error message with a function name. Handle <SNR> things.
7692 */
7693 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007694emsg_funcname(ermsg, name)
7695 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007696 char_u *name;
7697{
7698 char_u *p;
7699
7700 if (*name == K_SPECIAL)
7701 p = concat_str((char_u *)"<SNR>", name + 3);
7702 else
7703 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007704 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007705 if (p != name)
7706 vim_free(p);
7707}
7708
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709/*********************************************
7710 * Implementation of the built-in functions
7711 */
7712
7713/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007714 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 */
7716 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007717f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 typval_T *argvars;
7719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007724 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007726 if ((l = argvars[0].vval.v_list) != NULL
7727 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7728 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007729 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730 }
7731 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007732 EMSG(_(e_listreq));
7733}
7734
7735/*
7736 * "append(lnum, string/list)" function
7737 */
7738 static void
7739f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 typval_T *argvars;
7741 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007742{
7743 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007744 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 list_T *l = NULL;
7746 listitem_T *li = NULL;
7747 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007748 long added = 0;
7749
Bram Moolenaar0d660222005-01-07 21:51:51 +00007750 lnum = get_tv_lnum(argvars);
7751 if (lnum >= 0
7752 && lnum <= curbuf->b_ml.ml_line_count
7753 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007754 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007755 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007757 l = argvars[1].vval.v_list;
7758 if (l == NULL)
7759 return;
7760 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007763 for (;;)
7764 {
7765 if (l == NULL)
7766 tv = &argvars[1]; /* append a string */
7767 else if (li == NULL)
7768 break; /* end of list */
7769 else
7770 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007771 line = get_tv_string_chk(tv);
7772 if (line == NULL) /* type error */
7773 {
7774 rettv->vval.v_number = 1; /* Failed */
7775 break;
7776 }
7777 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007778 ++added;
7779 if (l == NULL)
7780 break;
7781 li = li->li_next;
7782 }
7783
7784 appended_lines_mark(lnum, added);
7785 if (curwin->w_cursor.lnum > lnum)
7786 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007788 else
7789 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
7791
7792/*
7793 * "argc()" function
7794 */
7795/* ARGSUSED */
7796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 typval_T *argvars;
7799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802}
7803
7804/*
7805 * "argidx()" function
7806 */
7807/* ARGSUSED */
7808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007809f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 typval_T *argvars;
7811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007813 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814}
7815
7816/*
7817 * "argv(nr)" function
7818 */
7819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007821 typval_T *argvars;
7822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 int idx;
7825
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007826 if (argvars[0].v_type != VAR_UNKNOWN)
7827 {
7828 idx = get_tv_number_chk(&argvars[0], NULL);
7829 if (idx >= 0 && idx < ARGCOUNT)
7830 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7831 else
7832 rettv->vval.v_string = NULL;
7833 rettv->v_type = VAR_STRING;
7834 }
7835 else if (rettv_list_alloc(rettv) == OK)
7836 for (idx = 0; idx < ARGCOUNT; ++idx)
7837 list_append_string(rettv->vval.v_list,
7838 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839}
7840
7841/*
7842 * "browse(save, title, initdir, default)" function
7843 */
7844/* ARGSUSED */
7845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 typval_T *argvars;
7848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850#ifdef FEAT_BROWSE
7851 int save;
7852 char_u *title;
7853 char_u *initdir;
7854 char_u *defname;
7855 char_u buf[NUMBUFLEN];
7856 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007857 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007859 save = get_tv_number_chk(&argvars[0], &error);
7860 title = get_tv_string_chk(&argvars[1]);
7861 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7862 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007864 if (error || title == NULL || initdir == NULL || defname == NULL)
7865 rettv->vval.v_string = NULL;
7866 else
7867 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007868 do_browse(save ? BROWSE_SAVE : 0,
7869 title, defname, NULL, initdir, NULL, curbuf);
7870#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007872#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007874}
7875
7876/*
7877 * "browsedir(title, initdir)" function
7878 */
7879/* ARGSUSED */
7880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 typval_T *argvars;
7883 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007884{
7885#ifdef FEAT_BROWSE
7886 char_u *title;
7887 char_u *initdir;
7888 char_u buf[NUMBUFLEN];
7889
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007890 title = get_tv_string_chk(&argvars[0]);
7891 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007893 if (title == NULL || initdir == NULL)
7894 rettv->vval.v_string = NULL;
7895 else
7896 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007897 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007899 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007901 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902}
7903
Bram Moolenaar33570922005-01-25 22:26:29 +00007904static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007905
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906/*
7907 * Find a buffer by number or exact name.
7908 */
7909 static buf_T *
7910find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912{
7913 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 if (avar->v_type == VAR_NUMBER)
7916 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007917 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007920 if (buf == NULL)
7921 {
7922 /* No full path name match, try a match with a URL or a "nofile"
7923 * buffer, these don't use the full path. */
7924 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7925 if (buf->b_fname != NULL
7926 && (path_with_url(buf->b_fname)
7927#ifdef FEAT_QUICKFIX
7928 || bt_nofile(buf)
7929#endif
7930 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007931 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007932 break;
7933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 }
7935 return buf;
7936}
7937
7938/*
7939 * "bufexists(expr)" function
7940 */
7941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 typval_T *argvars;
7944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007946 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947}
7948
7949/*
7950 * "buflisted(expr)" function
7951 */
7952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 typval_T *argvars;
7955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956{
7957 buf_T *buf;
7958
7959 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961}
7962
7963/*
7964 * "bufloaded(expr)" function
7965 */
7966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007967f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007968 typval_T *argvars;
7969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
7971 buf_T *buf;
7972
7973 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007974 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975}
7976
Bram Moolenaar33570922005-01-25 22:26:29 +00007977static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007978
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979/*
7980 * Get buffer by number or pattern.
7981 */
7982 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 int save_magic;
7988 char_u *save_cpo;
7989 buf_T *buf;
7990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007991 if (tv->v_type == VAR_NUMBER)
7992 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007993 if (tv->v_type != VAR_STRING)
7994 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 if (name == NULL || *name == NUL)
7996 return curbuf;
7997 if (name[0] == '$' && name[1] == NUL)
7998 return lastbuf;
7999
8000 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8001 save_magic = p_magic;
8002 p_magic = TRUE;
8003 save_cpo = p_cpo;
8004 p_cpo = (char_u *)"";
8005
8006 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8007 TRUE, FALSE));
8008
8009 p_magic = save_magic;
8010 p_cpo = save_cpo;
8011
8012 /* If not found, try expanding the name, like done for bufexists(). */
8013 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008014 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015
8016 return buf;
8017}
8018
8019/*
8020 * "bufname(expr)" function
8021 */
8022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008024 typval_T *argvars;
8025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 buf_T *buf;
8028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008029 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 buf = get_buf_tv(&argvars[0]);
8032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008034 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 --emsg_off;
8038}
8039
8040/*
8041 * "bufnr(expr)" function
8042 */
8043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 typval_T *argvars;
8046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008049 int error = FALSE;
8050 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008052 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008055 --emsg_off;
8056
8057 /* If the buffer isn't found and the second argument is not zero create a
8058 * new buffer. */
8059 if (buf == NULL
8060 && argvars[1].v_type != VAR_UNKNOWN
8061 && get_tv_number_chk(&argvars[1], &error) != 0
8062 && !error
8063 && (name = get_tv_string_chk(&argvars[0])) != NULL
8064 && !error)
8065 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008068 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071}
8072
8073/*
8074 * "bufwinnr(nr)" function
8075 */
8076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008077f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008078 typval_T *argvars;
8079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
8081#ifdef FEAT_WINDOWS
8082 win_T *wp;
8083 int winnr = 0;
8084#endif
8085 buf_T *buf;
8086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008087 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008089 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090#ifdef FEAT_WINDOWS
8091 for (wp = firstwin; wp; wp = wp->w_next)
8092 {
8093 ++winnr;
8094 if (wp->w_buffer == buf)
8095 break;
8096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100#endif
8101 --emsg_off;
8102}
8103
8104/*
8105 * "byte2line(byte)" function
8106 */
8107/*ARGSUSED*/
8108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 typval_T *argvars;
8111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
8113#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#else
8116 long boff = 0;
8117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008118 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008122 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 (linenr_T)0, &boff);
8124#endif
8125}
8126
8127/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008128 * "byteidx()" function
8129 */
8130/*ARGSUSED*/
8131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 typval_T *argvars;
8134 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008135{
8136#ifdef FEAT_MBYTE
8137 char_u *t;
8138#endif
8139 char_u *str;
8140 long idx;
8141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008142 str = get_tv_string_chk(&argvars[0]);
8143 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008144 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008145 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008146 return;
8147
8148#ifdef FEAT_MBYTE
8149 t = str;
8150 for ( ; idx > 0; idx--)
8151 {
8152 if (*t == NUL) /* EOL reached */
8153 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008154 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008155 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008156 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008157#else
8158 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008159 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008160#endif
8161}
8162
8163/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008164 * "call(func, arglist)" function
8165 */
8166 static void
8167f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008168 typval_T *argvars;
8169 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170{
8171 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008172 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008173 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008175 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008177
8178 rettv->vval.v_number = 0;
8179 if (argvars[1].v_type != VAR_LIST)
8180 {
8181 EMSG(_(e_listreq));
8182 return;
8183 }
8184 if (argvars[1].vval.v_list == NULL)
8185 return;
8186
8187 if (argvars[0].v_type == VAR_FUNC)
8188 func = argvars[0].vval.v_string;
8189 else
8190 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008191 if (*func == NUL)
8192 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008193
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 if (argvars[2].v_type != VAR_UNKNOWN)
8195 {
8196 if (argvars[2].v_type != VAR_DICT)
8197 {
8198 EMSG(_(e_dictreq));
8199 return;
8200 }
8201 selfdict = argvars[2].vval.v_dict;
8202 }
8203
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008204 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8205 item = item->li_next)
8206 {
8207 if (argc == MAX_FUNC_ARGS)
8208 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008209 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008210 break;
8211 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008212 /* Make a copy of each argument. This is needed to be able to set
8213 * v_lock to VAR_FIXED in the copy without changing the original list.
8214 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008215 copy_tv(&item->li_tv, &argv[argc++]);
8216 }
8217
8218 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008219 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008220 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8221 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008222
8223 /* Free the arguments. */
8224 while (argc > 0)
8225 clear_tv(&argv[--argc]);
8226}
8227
8228/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008229 * "changenr()" function
8230 */
8231/*ARGSUSED*/
8232 static void
8233f_changenr(argvars, rettv)
8234 typval_T *argvars;
8235 typval_T *rettv;
8236{
8237 rettv->vval.v_number = curbuf->b_u_seq_cur;
8238}
8239
8240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 * "char2nr(string)" function
8242 */
8243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008245 typval_T *argvars;
8246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
8248#ifdef FEAT_MBYTE
8249 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008250 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 else
8252#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254}
8255
8256/*
8257 * "cindent(lnum)" function
8258 */
8259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008260f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008261 typval_T *argvars;
8262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
8264#ifdef FEAT_CINDENT
8265 pos_T pos;
8266 linenr_T lnum;
8267
8268 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008269 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8271 {
8272 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 curwin->w_cursor = pos;
8275 }
8276 else
8277#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279}
8280
8281/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008282 * "clearmatches()" function
8283 */
8284/*ARGSUSED*/
8285 static void
8286f_clearmatches(argvars, rettv)
8287 typval_T *argvars;
8288 typval_T *rettv;
8289{
8290#ifdef FEAT_SEARCH_EXTRA
8291 clear_matches(curwin);
8292#endif
8293}
8294
8295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 * "col(string)" function
8297 */
8298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 typval_T *argvars;
8301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 colnr_T col = 0;
8304 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008305 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008307 fp = var2fpos(&argvars[0], FALSE, &fnum);
8308 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
8310 if (fp->col == MAXCOL)
8311 {
8312 /* '> can be MAXCOL, get the length of the line then */
8313 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008314 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 else
8316 col = MAXCOL;
8317 }
8318 else
8319 {
8320 col = fp->col + 1;
8321#ifdef FEAT_VIRTUALEDIT
8322 /* col(".") when the cursor is on the NUL at the end of the line
8323 * because of "coladd" can be seen as an extra column. */
8324 if (virtual_active() && fp == &curwin->w_cursor)
8325 {
8326 char_u *p = ml_get_cursor();
8327
8328 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8329 curwin->w_virtcol - curwin->w_cursor.coladd))
8330 {
8331# ifdef FEAT_MBYTE
8332 int l;
8333
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008334 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 col += l;
8336# else
8337 if (*p != NUL && p[1] == NUL)
8338 ++col;
8339# endif
8340 }
8341 }
8342#endif
8343 }
8344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008345 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
8347
Bram Moolenaar572cb562005-08-05 21:35:02 +00008348#if defined(FEAT_INS_EXPAND)
8349/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008350 * "complete()" function
8351 */
8352/*ARGSUSED*/
8353 static void
8354f_complete(argvars, rettv)
8355 typval_T *argvars;
8356 typval_T *rettv;
8357{
8358 int startcol;
8359
8360 if ((State & INSERT) == 0)
8361 {
8362 EMSG(_("E785: complete() can only be used in Insert mode"));
8363 return;
8364 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008365
8366 /* Check for undo allowed here, because if something was already inserted
8367 * the line was already saved for undo and this check isn't done. */
8368 if (!undo_allowed())
8369 return;
8370
Bram Moolenaarade00832006-03-10 21:46:58 +00008371 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8372 {
8373 EMSG(_(e_invarg));
8374 return;
8375 }
8376
8377 startcol = get_tv_number_chk(&argvars[0], NULL);
8378 if (startcol <= 0)
8379 return;
8380
8381 set_completion(startcol - 1, argvars[1].vval.v_list);
8382}
8383
8384/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008385 * "complete_add()" function
8386 */
8387/*ARGSUSED*/
8388 static void
8389f_complete_add(argvars, rettv)
8390 typval_T *argvars;
8391 typval_T *rettv;
8392{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008393 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008394}
8395
8396/*
8397 * "complete_check()" function
8398 */
8399/*ARGSUSED*/
8400 static void
8401f_complete_check(argvars, rettv)
8402 typval_T *argvars;
8403 typval_T *rettv;
8404{
8405 int saved = RedrawingDisabled;
8406
8407 RedrawingDisabled = 0;
8408 ins_compl_check_keys(0);
8409 rettv->vval.v_number = compl_interrupted;
8410 RedrawingDisabled = saved;
8411}
8412#endif
8413
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414/*
8415 * "confirm(message, buttons[, default [, type]])" function
8416 */
8417/*ARGSUSED*/
8418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008419f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008420 typval_T *argvars;
8421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
8423#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8424 char_u *message;
8425 char_u *buttons = NULL;
8426 char_u buf[NUMBUFLEN];
8427 char_u buf2[NUMBUFLEN];
8428 int def = 1;
8429 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008430 char_u *typestr;
8431 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008433 message = get_tv_string_chk(&argvars[0]);
8434 if (message == NULL)
8435 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008436 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8439 if (buttons == NULL)
8440 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008441 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008443 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008444 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008446 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8447 if (typestr == NULL)
8448 error = TRUE;
8449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 switch (TOUPPER_ASC(*typestr))
8452 {
8453 case 'E': type = VIM_ERROR; break;
8454 case 'Q': type = VIM_QUESTION; break;
8455 case 'I': type = VIM_INFO; break;
8456 case 'W': type = VIM_WARNING; break;
8457 case 'G': type = VIM_GENERIC; break;
8458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 }
8460 }
8461 }
8462 }
8463
8464 if (buttons == NULL || *buttons == NUL)
8465 buttons = (char_u *)_("&Ok");
8466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008467 if (error)
8468 rettv->vval.v_number = 0;
8469 else
8470 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 def, NULL);
8472#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008473 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474#endif
8475}
8476
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477/*
8478 * "copy()" function
8479 */
8480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008481f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 typval_T *argvars;
8483 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008485 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487
8488/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008489 * "count()" function
8490 */
8491 static void
8492f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 typval_T *argvars;
8494 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008495{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008496 long n = 0;
8497 int ic = FALSE;
8498
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008500 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 listitem_T *li;
8502 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008504
Bram Moolenaare9a41262005-01-15 22:18:47 +00008505 if ((l = argvars[0].vval.v_list) != NULL)
8506 {
8507 li = l->lv_first;
8508 if (argvars[2].v_type != VAR_UNKNOWN)
8509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510 int error = FALSE;
8511
8512 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008513 if (argvars[3].v_type != VAR_UNKNOWN)
8514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008515 idx = get_tv_number_chk(&argvars[3], &error);
8516 if (!error)
8517 {
8518 li = list_find(l, idx);
8519 if (li == NULL)
8520 EMSGN(_(e_listidx), idx);
8521 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523 if (error)
8524 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008525 }
8526
8527 for ( ; li != NULL; li = li->li_next)
8528 if (tv_equal(&li->li_tv, &argvars[1], ic))
8529 ++n;
8530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008531 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532 else if (argvars[0].v_type == VAR_DICT)
8533 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008534 int todo;
8535 dict_T *d;
8536 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008537
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008538 if ((d = argvars[0].vval.v_dict) != NULL)
8539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008540 int error = FALSE;
8541
Bram Moolenaare9a41262005-01-15 22:18:47 +00008542 if (argvars[2].v_type != VAR_UNKNOWN)
8543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008544 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008545 if (argvars[3].v_type != VAR_UNKNOWN)
8546 EMSG(_(e_invarg));
8547 }
8548
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008549 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008551 {
8552 if (!HASHITEM_EMPTY(hi))
8553 {
8554 --todo;
8555 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8556 ++n;
8557 }
8558 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008559 }
8560 }
8561 else
8562 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008563 rettv->vval.v_number = n;
8564}
8565
8566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8568 *
8569 * Checks the existence of a cscope connection.
8570 */
8571/*ARGSUSED*/
8572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008574 typval_T *argvars;
8575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577#ifdef FEAT_CSCOPE
8578 int num = 0;
8579 char_u *dbpath = NULL;
8580 char_u *prepend = NULL;
8581 char_u buf[NUMBUFLEN];
8582
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008583 if (argvars[0].v_type != VAR_UNKNOWN
8584 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 num = (int)get_tv_number(&argvars[0]);
8587 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008588 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 }
8591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#endif
8596}
8597
8598/*
8599 * "cursor(lnum, col)" function
8600 *
8601 * Moves the cursor to the specified line and column
8602 */
8603/*ARGSUSED*/
8604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008605f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 typval_T *argvars;
8607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008610#ifdef FEAT_VIRTUALEDIT
8611 long coladd = 0;
8612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
Bram Moolenaara5525202006-03-02 22:52:09 +00008614 if (argvars[1].v_type == VAR_UNKNOWN)
8615 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008616 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008617
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008618 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008619 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008620 line = pos.lnum;
8621 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008622#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008623 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008624#endif
8625 }
8626 else
8627 {
8628 line = get_tv_lnum(argvars);
8629 col = get_tv_number_chk(&argvars[1], NULL);
8630#ifdef FEAT_VIRTUALEDIT
8631 if (argvars[2].v_type != VAR_UNKNOWN)
8632 coladd = get_tv_number_chk(&argvars[2], NULL);
8633#endif
8634 }
8635 if (line < 0 || col < 0
8636#ifdef FEAT_VIRTUALEDIT
8637 || coladd < 0
8638#endif
8639 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 if (line > 0)
8642 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (col > 0)
8644 curwin->w_cursor.col = col - 1;
8645#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008646 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647#endif
8648
8649 /* Make sure the cursor is in a valid position. */
8650 check_cursor();
8651#ifdef FEAT_MBYTE
8652 /* Correct cursor for multi-byte character. */
8653 if (has_mbyte)
8654 mb_adjust_cursor();
8655#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008656
8657 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658}
8659
8660/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 */
8663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 typval_T *argvars;
8666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008668 int noref = 0;
8669
8670 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008671 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008672 if (noref < 0 || noref > 1)
8673 EMSG(_(e_invarg));
8674 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008675 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676}
8677
8678/*
8679 * "delete()" function
8680 */
8681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 typval_T *argvars;
8684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690}
8691
8692/*
8693 * "did_filetype()" function
8694 */
8695/*ARGSUSED*/
8696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008698 typval_T *argvars;
8699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705#endif
8706}
8707
8708/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008709 * "diff_filler()" function
8710 */
8711/*ARGSUSED*/
8712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 typval_T *argvars;
8715 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008716{
8717#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008718 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008719#endif
8720}
8721
8722/*
8723 * "diff_hlID()" function
8724 */
8725/*ARGSUSED*/
8726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 typval_T *argvars;
8729 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008730{
8731#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008732 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008733 static linenr_T prev_lnum = 0;
8734 static int changedtick = 0;
8735 static int fnum = 0;
8736 static int change_start = 0;
8737 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00008738 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008739 int filler_lines;
8740 int col;
8741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008742 if (lnum < 0) /* ignore type error in {lnum} arg */
8743 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008744 if (lnum != prev_lnum
8745 || changedtick != curbuf->b_changedtick
8746 || fnum != curbuf->b_fnum)
8747 {
8748 /* New line, buffer, change: need to get the values. */
8749 filler_lines = diff_check(curwin, lnum);
8750 if (filler_lines < 0)
8751 {
8752 if (filler_lines == -1)
8753 {
8754 change_start = MAXCOL;
8755 change_end = -1;
8756 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8757 hlID = HLF_ADD; /* added line */
8758 else
8759 hlID = HLF_CHD; /* changed line */
8760 }
8761 else
8762 hlID = HLF_ADD; /* added line */
8763 }
8764 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008765 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008766 prev_lnum = lnum;
8767 changedtick = curbuf->b_changedtick;
8768 fnum = curbuf->b_fnum;
8769 }
8770
8771 if (hlID == HLF_CHD || hlID == HLF_TXD)
8772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008774 if (col >= change_start && col <= change_end)
8775 hlID = HLF_TXD; /* changed text */
8776 else
8777 hlID = HLF_CHD; /* changed line */
8778 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008779 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008780#endif
8781}
8782
8783/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008784 * "empty({expr})" function
8785 */
8786 static void
8787f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008788 typval_T *argvars;
8789 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008790{
8791 int n;
8792
8793 switch (argvars[0].v_type)
8794 {
8795 case VAR_STRING:
8796 case VAR_FUNC:
8797 n = argvars[0].vval.v_string == NULL
8798 || *argvars[0].vval.v_string == NUL;
8799 break;
8800 case VAR_NUMBER:
8801 n = argvars[0].vval.v_number == 0;
8802 break;
8803 case VAR_LIST:
8804 n = argvars[0].vval.v_list == NULL
8805 || argvars[0].vval.v_list->lv_first == NULL;
8806 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008807 case VAR_DICT:
8808 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008810 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008811 default:
8812 EMSG2(_(e_intern2), "f_empty()");
8813 n = 0;
8814 }
8815
8816 rettv->vval.v_number = n;
8817}
8818
8819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 * "escape({string}, {chars})" function
8821 */
8822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *argvars;
8825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
8827 char_u buf[NUMBUFLEN];
8828
Bram Moolenaar758711c2005-02-02 23:11:38 +00008829 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8830 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
8834/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008835 * "eval()" function
8836 */
8837/*ARGSUSED*/
8838 static void
8839f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008840 typval_T *argvars;
8841 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008842{
8843 char_u *s;
8844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008845 s = get_tv_string_chk(&argvars[0]);
8846 if (s != NULL)
8847 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008849 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8850 {
8851 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008852 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008853 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008854 else if (*s != NUL)
8855 EMSG(_(e_trailing));
8856}
8857
8858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 * "eventhandler()" function
8860 */
8861/*ARGSUSED*/
8862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 typval_T *argvars;
8865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868}
8869
8870/*
8871 * "executable()" function
8872 */
8873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008875 typval_T *argvars;
8876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879}
8880
8881/*
8882 * "exists()" function
8883 */
8884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008885f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008886 typval_T *argvars;
8887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 char_u *p;
8890 char_u *name;
8891 int n = FALSE;
8892 int len = 0;
8893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 if (*p == '$') /* environment variable */
8896 {
8897 /* first try "normal" environment variables (fast) */
8898 if (mch_getenv(p + 1) != NULL)
8899 n = TRUE;
8900 else
8901 {
8902 /* try expanding things like $VIM and ${HOME} */
8903 p = expand_env_save(p);
8904 if (p != NULL && *p != '$')
8905 n = TRUE;
8906 vim_free(p);
8907 }
8908 }
8909 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008912 if (*skipwhite(p) != NUL)
8913 n = FALSE; /* trailing garbage */
8914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 else if (*p == '*') /* internal or user defined function */
8916 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008917 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919 else if (*p == ':')
8920 {
8921 n = cmd_exists(p + 1);
8922 }
8923 else if (*p == '#')
8924 {
8925#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008926 if (p[1] == '#')
8927 n = autocmd_supported(p + 2);
8928 else
8929 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930#endif
8931 }
8932 else /* internal variable */
8933 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008934 char_u *tofree;
8935 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008937 /* get_name_len() takes care of expanding curly braces */
8938 name = p;
8939 len = get_name_len(&p, &tofree, TRUE, FALSE);
8940 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008942 if (tofree != NULL)
8943 name = tofree;
8944 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8945 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008947 /* handle d.key, l[idx], f(expr) */
8948 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8949 if (n)
8950 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 }
8952 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008953 if (*p != NUL)
8954 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008956 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960}
8961
8962/*
8963 * "expand()" function
8964 */
8965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008967 typval_T *argvars;
8968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970 char_u *s;
8971 int len;
8972 char_u *errormsg;
8973 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8974 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 rettv->v_type = VAR_STRING;
8978 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 if (*s == '%' || *s == '#' || *s == '<')
8980 {
8981 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008982 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 --emsg_off;
8984 }
8985 else
8986 {
8987 /* When the optional second argument is non-zero, don't remove matches
8988 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008989 if (argvars[1].v_type != VAR_UNKNOWN
8990 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 if (!error)
8993 {
8994 ExpandInit(&xpc);
8995 xpc.xp_context = EXPAND_FILES;
8996 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008997 }
8998 else
8999 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 }
9001}
9002
9003/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009004 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009005 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009006 */
9007 static void
9008f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 typval_T *argvars;
9010 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009011{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009012 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 list_T *l1, *l2;
9016 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009017 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009018 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009019
Bram Moolenaare9a41262005-01-15 22:18:47 +00009020 l1 = argvars[0].vval.v_list;
9021 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009022 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9023 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009024 {
9025 if (argvars[2].v_type != VAR_UNKNOWN)
9026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009027 before = get_tv_number_chk(&argvars[2], &error);
9028 if (error)
9029 return; /* type error; errmsg already given */
9030
Bram Moolenaar758711c2005-02-02 23:11:38 +00009031 if (before == l1->lv_len)
9032 item = NULL;
9033 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009034 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009035 item = list_find(l1, before);
9036 if (item == NULL)
9037 {
9038 EMSGN(_(e_listidx), before);
9039 return;
9040 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 }
9042 }
9043 else
9044 item = NULL;
9045 list_extend(l1, l2, item);
9046
Bram Moolenaare9a41262005-01-15 22:18:47 +00009047 copy_tv(&argvars[0], rettv);
9048 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009049 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009050 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9051 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 dict_T *d1, *d2;
9053 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009054 char_u *action;
9055 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009056 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009057 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009058
9059 d1 = argvars[0].vval.v_dict;
9060 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009061 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9062 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009063 {
9064 /* Check the third argument. */
9065 if (argvars[2].v_type != VAR_UNKNOWN)
9066 {
9067 static char *(av[]) = {"keep", "force", "error"};
9068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 action = get_tv_string_chk(&argvars[2]);
9070 if (action == NULL)
9071 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009072 for (i = 0; i < 3; ++i)
9073 if (STRCMP(action, av[i]) == 0)
9074 break;
9075 if (i == 3)
9076 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009077 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009078 return;
9079 }
9080 }
9081 else
9082 action = (char_u *)"force";
9083
9084 /* Go over all entries in the second dict and add them to the
9085 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009086 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009087 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009089 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009091 --todo;
9092 di1 = dict_find(d1, hi2->hi_key, -1);
9093 if (di1 == NULL)
9094 {
9095 di1 = dictitem_copy(HI2DI(hi2));
9096 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9097 dictitem_free(di1);
9098 }
9099 else if (*action == 'e')
9100 {
9101 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9102 break;
9103 }
9104 else if (*action == 'f')
9105 {
9106 clear_tv(&di1->di_tv);
9107 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9108 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009109 }
9110 }
9111
Bram Moolenaare9a41262005-01-15 22:18:47 +00009112 copy_tv(&argvars[0], rettv);
9113 }
9114 }
9115 else
9116 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009117}
9118
9119/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009120 * "feedkeys()" function
9121 */
9122/*ARGSUSED*/
9123 static void
9124f_feedkeys(argvars, rettv)
9125 typval_T *argvars;
9126 typval_T *rettv;
9127{
9128 int remap = TRUE;
9129 char_u *keys, *flags;
9130 char_u nbuf[NUMBUFLEN];
9131 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009132 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009133
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009134 /* This is not allowed in the sandbox. If the commands would still be
9135 * executed in the sandbox it would be OK, but it probably happens later,
9136 * when "sandbox" is no longer set. */
9137 if (check_secure())
9138 return;
9139
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009140 rettv->vval.v_number = 0;
9141 keys = get_tv_string(&argvars[0]);
9142 if (*keys != NUL)
9143 {
9144 if (argvars[1].v_type != VAR_UNKNOWN)
9145 {
9146 flags = get_tv_string_buf(&argvars[1], nbuf);
9147 for ( ; *flags != NUL; ++flags)
9148 {
9149 switch (*flags)
9150 {
9151 case 'n': remap = FALSE; break;
9152 case 'm': remap = TRUE; break;
9153 case 't': typed = TRUE; break;
9154 }
9155 }
9156 }
9157
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009158 /* Need to escape K_SPECIAL and CSI before putting the string in the
9159 * typeahead buffer. */
9160 keys_esc = vim_strsave_escape_csi(keys);
9161 if (keys_esc != NULL)
9162 {
9163 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009164 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009165 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009166 if (vgetc_busy)
9167 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009168 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009169 }
9170}
9171
9172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 * "filereadable()" function
9174 */
9175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009177 typval_T *argvars;
9178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
9180 FILE *fd;
9181 char_u *p;
9182 int n;
9183
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9186 {
9187 n = TRUE;
9188 fclose(fd);
9189 }
9190 else
9191 n = FALSE;
9192
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
9195
9196/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009197 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 * rights to write into.
9199 */
9200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009202 typval_T *argvars;
9203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009205 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206}
9207
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009208static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009209
9210 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009211findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009212 typval_T *argvars;
9213 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009214 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009215{
9216#ifdef FEAT_SEARCHPATH
9217 char_u *fname;
9218 char_u *fresult = NULL;
9219 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9220 char_u *p;
9221 char_u pathbuf[NUMBUFLEN];
9222 int count = 1;
9223 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009224 int error = FALSE;
9225#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009226
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009227 rettv->vval.v_string = NULL;
9228 rettv->v_type = VAR_STRING;
9229
9230#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009232
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009233 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009235 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9236 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009237 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 else
9239 {
9240 if (*p != NUL)
9241 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009244 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009246 }
9247
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009248 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9249 error = TRUE;
9250
9251 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 do
9254 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009255 if (rettv->v_type == VAR_STRING)
9256 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009257 fresult = find_file_in_path_option(first ? fname : NULL,
9258 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009259 0, first, path,
9260 find_what,
9261 curbuf->b_ffname,
9262 find_what == FINDFILE_DIR
9263 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009265
9266 if (fresult != NULL && rettv->v_type == VAR_LIST)
9267 list_append_string(rettv->vval.v_list, fresult, -1);
9268
9269 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009270 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009271
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009272 if (rettv->v_type == VAR_STRING)
9273 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009274#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009275}
9276
Bram Moolenaar33570922005-01-25 22:26:29 +00009277static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9278static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009279
9280/*
9281 * Implementation of map() and filter().
9282 */
9283 static void
9284filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009285 typval_T *argvars;
9286 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009287 int map;
9288{
9289 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009291 listitem_T *li, *nli;
9292 list_T *l = NULL;
9293 dictitem_T *di;
9294 hashtab_T *ht;
9295 hashitem_T *hi;
9296 dict_T *d = NULL;
9297 typval_T save_val;
9298 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009299 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009300 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009301 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009302 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009303
9304 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009305 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009306 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009307 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009308 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009309 return;
9310 }
9311 else if (argvars[0].v_type == VAR_DICT)
9312 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009313 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009314 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009315 return;
9316 }
9317 else
9318 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009319 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009320 return;
9321 }
9322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 expr = get_tv_string_buf_chk(&argvars[1], buf);
9324 /* On type errors, the preceding call has already displayed an error
9325 * message. Avoid a misleading error message for an empty string that
9326 * was not passed as argument. */
9327 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 prepare_vimvar(VV_VAL, &save_val);
9330 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009331
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009332 /* We reset "did_emsg" to be able to detect whether an error
9333 * occurred during evaluation of the expression. */
9334 save_did_emsg = did_emsg;
9335 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009339 prepare_vimvar(VV_KEY, &save_key);
9340 vimvars[VV_KEY].vv_type = VAR_STRING;
9341
9342 ht = &d->dv_hashtab;
9343 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009344 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009347 if (!HASHITEM_EMPTY(hi))
9348 {
9349 --todo;
9350 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009351 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009352 break;
9353 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009354 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009355 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 break;
9357 if (!map && rem)
9358 dictitem_remove(d, di);
9359 clear_tv(&vimvars[VV_KEY].vv_tv);
9360 }
9361 }
9362 hash_unlock(ht);
9363
9364 restore_vimvar(VV_KEY, &save_key);
9365 }
9366 else
9367 {
9368 for (li = l->lv_first; li != NULL; li = nli)
9369 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009370 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009371 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009372 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009373 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009374 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009375 break;
9376 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009377 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009378 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009379 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009382
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009383 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009385
9386 copy_tv(&argvars[0], rettv);
9387}
9388
9389 static int
9390filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 char_u *expr;
9393 int map;
9394 int *remp;
9395{
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009397 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009398 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009399
Bram Moolenaar33570922005-01-25 22:26:29 +00009400 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009401 s = expr;
9402 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009403 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009404 if (*s != NUL) /* check for trailing chars after expr */
9405 {
9406 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009407 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009408 }
9409 if (map)
9410 {
9411 /* map(): replace the list item value */
9412 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009413 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009414 *tv = rettv;
9415 }
9416 else
9417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009418 int error = FALSE;
9419
Bram Moolenaare9a41262005-01-15 22:18:47 +00009420 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009422 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 /* On type error, nothing has been removed; return FAIL to stop the
9424 * loop. The error message was given by get_tv_number_chk(). */
9425 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009426 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009427 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009428 retval = OK;
9429theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009430 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009431 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009432}
9433
9434/*
9435 * "filter()" function
9436 */
9437 static void
9438f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 typval_T *argvars;
9440 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009441{
9442 filter_map(argvars, rettv, FALSE);
9443}
9444
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009445/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009446 * "finddir({fname}[, {path}[, {count}]])" function
9447 */
9448 static void
9449f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009450 typval_T *argvars;
9451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009452{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009453 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009454}
9455
9456/*
9457 * "findfile({fname}[, {path}[, {count}]])" function
9458 */
9459 static void
9460f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009461 typval_T *argvars;
9462 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009463{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009464 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465}
9466
9467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 * "fnamemodify({fname}, {mods})" function
9469 */
9470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009472 typval_T *argvars;
9473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 char_u *fname;
9476 char_u *mods;
9477 int usedlen = 0;
9478 int len;
9479 char_u *fbuf = NULL;
9480 char_u buf[NUMBUFLEN];
9481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009482 fname = get_tv_string_chk(&argvars[0]);
9483 mods = get_tv_string_buf_chk(&argvars[1], buf);
9484 if (fname == NULL || mods == NULL)
9485 fname = NULL;
9486 else
9487 {
9488 len = (int)STRLEN(fname);
9489 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 vim_free(fbuf);
9498}
9499
Bram Moolenaar33570922005-01-25 22:26:29 +00009500static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502/*
9503 * "foldclosed()" function
9504 */
9505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009507 typval_T *argvars;
9508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 int end;
9510{
9511#ifdef FEAT_FOLDING
9512 linenr_T lnum;
9513 linenr_T first, last;
9514
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9517 {
9518 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9519 {
9520 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 return;
9525 }
9526 }
9527#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529}
9530
9531/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009532 * "foldclosed()" function
9533 */
9534 static void
9535f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009536 typval_T *argvars;
9537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009538{
9539 foldclosed_both(argvars, rettv, FALSE);
9540}
9541
9542/*
9543 * "foldclosedend()" function
9544 */
9545 static void
9546f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009547 typval_T *argvars;
9548 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009549{
9550 foldclosed_both(argvars, rettv, TRUE);
9551}
9552
9553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 * "foldlevel()" function
9555 */
9556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009558 typval_T *argvars;
9559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560{
9561#ifdef FEAT_FOLDING
9562 linenr_T lnum;
9563
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 else
9568#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570}
9571
9572/*
9573 * "foldtext()" function
9574 */
9575/*ARGSUSED*/
9576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009578 typval_T *argvars;
9579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581#ifdef FEAT_FOLDING
9582 linenr_T lnum;
9583 char_u *s;
9584 char_u *r;
9585 int len;
9586 char *txt;
9587#endif
9588
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 rettv->v_type = VAR_STRING;
9590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009592 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9593 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9594 <= curbuf->b_ml.ml_line_count
9595 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 {
9597 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009598 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9599 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
9601 if (!linewhite(lnum))
9602 break;
9603 ++lnum;
9604 }
9605
9606 /* Find interesting text in this line. */
9607 s = skipwhite(ml_get(lnum));
9608 /* skip C comment-start */
9609 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009612 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009614 {
9615 s = skipwhite(ml_get(lnum + 1));
9616 if (*s == '*')
9617 s = skipwhite(s + 1);
9618 }
9619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 txt = _("+-%s%3ld lines: ");
9621 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 + 20 /* for %3ld */
9624 + STRLEN(s))); /* concatenated */
9625 if (r != NULL)
9626 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009627 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9628 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9629 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 len = (int)STRLEN(r);
9631 STRCAT(r, s);
9632 /* remove 'foldmarker' and 'commentstring' */
9633 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 }
9636 }
9637#endif
9638}
9639
9640/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009641 * "foldtextresult(lnum)" function
9642 */
9643/*ARGSUSED*/
9644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009646 typval_T *argvars;
9647 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009648{
9649#ifdef FEAT_FOLDING
9650 linenr_T lnum;
9651 char_u *text;
9652 char_u buf[51];
9653 foldinfo_T foldinfo;
9654 int fold_count;
9655#endif
9656
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 rettv->v_type = VAR_STRING;
9658 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009659#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009661 /* treat illegal types and illegal string values for {lnum} the same */
9662 if (lnum < 0)
9663 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009664 fold_count = foldedCount(curwin, lnum, &foldinfo);
9665 if (fold_count > 0)
9666 {
9667 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9668 &foldinfo, buf);
9669 if (text == buf)
9670 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009672 }
9673#endif
9674}
9675
9676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 * "foreground()" function
9678 */
9679/*ARGSUSED*/
9680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009682 typval_T *argvars;
9683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686#ifdef FEAT_GUI
9687 if (gui.in_use)
9688 gui_mch_set_foreground();
9689#else
9690# ifdef WIN32
9691 win32_set_foreground();
9692# endif
9693#endif
9694}
9695
9696/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009697 * "function()" function
9698 */
9699/*ARGSUSED*/
9700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009702 typval_T *argvars;
9703 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009704{
9705 char_u *s;
9706
Bram Moolenaara7043832005-01-21 11:56:39 +00009707 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009709 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009710 EMSG2(_(e_invarg2), s);
9711 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009712 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009713 else
9714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->vval.v_string = vim_strsave(s);
9716 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009717 }
9718}
9719
9720/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009721 * "garbagecollect()" function
9722 */
9723/*ARGSUSED*/
9724 static void
9725f_garbagecollect(argvars, rettv)
9726 typval_T *argvars;
9727 typval_T *rettv;
9728{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009729 /* This is postponed until we are back at the toplevel, because we may be
9730 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9731 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00009732
9733 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
9734 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009735}
9736
9737/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009738 * "get()" function
9739 */
9740 static void
9741f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009742 typval_T *argvars;
9743 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009744{
Bram Moolenaar33570922005-01-25 22:26:29 +00009745 listitem_T *li;
9746 list_T *l;
9747 dictitem_T *di;
9748 dict_T *d;
9749 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009750
Bram Moolenaare9a41262005-01-15 22:18:47 +00009751 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009752 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009753 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 int error = FALSE;
9756
9757 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9758 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009759 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009760 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009762 else if (argvars[0].v_type == VAR_DICT)
9763 {
9764 if ((d = argvars[0].vval.v_dict) != NULL)
9765 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009766 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009767 if (di != NULL)
9768 tv = &di->di_tv;
9769 }
9770 }
9771 else
9772 EMSG2(_(e_listdictarg), "get()");
9773
9774 if (tv == NULL)
9775 {
9776 if (argvars[2].v_type == VAR_UNKNOWN)
9777 rettv->vval.v_number = 0;
9778 else
9779 copy_tv(&argvars[2], rettv);
9780 }
9781 else
9782 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009783}
9784
Bram Moolenaar342337a2005-07-21 21:11:17 +00009785static 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 +00009786
9787/*
9788 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009789 * Return a range (from start to end) of lines in rettv from the specified
9790 * buffer.
9791 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009792 */
9793 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009794get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009795 buf_T *buf;
9796 linenr_T start;
9797 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009798 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009799 typval_T *rettv;
9800{
9801 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009802
Bram Moolenaar342337a2005-07-21 21:11:17 +00009803 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009804 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009805 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009806 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009807 }
9808 else
9809 rettv->vval.v_number = 0;
9810
9811 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9812 return;
9813
9814 if (!retlist)
9815 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009816 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9817 p = ml_get_buf(buf, start, FALSE);
9818 else
9819 p = (char_u *)"";
9820
9821 rettv->v_type = VAR_STRING;
9822 rettv->vval.v_string = vim_strsave(p);
9823 }
9824 else
9825 {
9826 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009827 return;
9828
9829 if (start < 1)
9830 start = 1;
9831 if (end > buf->b_ml.ml_line_count)
9832 end = buf->b_ml.ml_line_count;
9833 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009834 if (list_append_string(rettv->vval.v_list,
9835 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009836 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009837 }
9838}
9839
9840/*
9841 * "getbufline()" function
9842 */
9843 static void
9844f_getbufline(argvars, rettv)
9845 typval_T *argvars;
9846 typval_T *rettv;
9847{
9848 linenr_T lnum;
9849 linenr_T end;
9850 buf_T *buf;
9851
9852 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9853 ++emsg_off;
9854 buf = get_buf_tv(&argvars[0]);
9855 --emsg_off;
9856
Bram Moolenaar661b1822005-07-28 22:36:45 +00009857 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009858 if (argvars[2].v_type == VAR_UNKNOWN)
9859 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009860 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009861 end = get_tv_lnum_buf(&argvars[2], buf);
9862
Bram Moolenaar342337a2005-07-21 21:11:17 +00009863 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009864}
9865
Bram Moolenaar0d660222005-01-07 21:51:51 +00009866/*
9867 * "getbufvar()" function
9868 */
9869 static void
9870f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009871 typval_T *argvars;
9872 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009873{
9874 buf_T *buf;
9875 buf_T *save_curbuf;
9876 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009877 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9880 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009881 ++emsg_off;
9882 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009883
9884 rettv->v_type = VAR_STRING;
9885 rettv->vval.v_string = NULL;
9886
9887 if (buf != NULL && varname != NULL)
9888 {
9889 if (*varname == '&') /* buffer-local-option */
9890 {
9891 /* set curbuf to be our buf, temporarily */
9892 save_curbuf = curbuf;
9893 curbuf = buf;
9894
9895 get_option_tv(&varname, rettv, TRUE);
9896
9897 /* restore previous notion of curbuf */
9898 curbuf = save_curbuf;
9899 }
9900 else
9901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009902 if (*varname == NUL)
9903 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9904 * scope prefix before the NUL byte is required by
9905 * find_var_in_ht(). */
9906 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009907 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009908 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009909 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009910 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009911 }
9912 }
9913
9914 --emsg_off;
9915}
9916
9917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 * "getchar()" function
9919 */
9920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 typval_T *argvars;
9923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009926 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009928 /* Position the cursor. Needed after a message that ends in a space. */
9929 windgoto(msg_row, msg_col);
9930
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 ++no_mapping;
9932 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009933 for (;;)
9934 {
9935 if (argvars[0].v_type == VAR_UNKNOWN)
9936 /* getchar(): blocking wait. */
9937 n = safe_vgetc();
9938 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9939 /* getchar(1): only check if char avail */
9940 n = vpeekc();
9941 else if (error || vpeekc() == NUL)
9942 /* illegal argument or getchar(0) and no char avail: return zero */
9943 n = 0;
9944 else
9945 /* getchar(0) and char avail: return char */
9946 n = safe_vgetc();
9947 if (n == K_IGNORE)
9948 continue;
9949 break;
9950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 --no_mapping;
9952 --allow_keys;
9953
Bram Moolenaar219b8702006-11-01 14:32:36 +00009954 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9955 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9956 vimvars[VV_MOUSE_COL].vv_nr = 0;
9957
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009958 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 if (IS_SPECIAL(n) || mod_mask != 0)
9960 {
9961 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9962 int i = 0;
9963
9964 /* Turn a special key into three bytes, plus modifier. */
9965 if (mod_mask != 0)
9966 {
9967 temp[i++] = K_SPECIAL;
9968 temp[i++] = KS_MODIFIER;
9969 temp[i++] = mod_mask;
9970 }
9971 if (IS_SPECIAL(n))
9972 {
9973 temp[i++] = K_SPECIAL;
9974 temp[i++] = K_SECOND(n);
9975 temp[i++] = K_THIRD(n);
9976 }
9977#ifdef FEAT_MBYTE
9978 else if (has_mbyte)
9979 i += (*mb_char2bytes)(n, temp + i);
9980#endif
9981 else
9982 temp[i++] = n;
9983 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984 rettv->v_type = VAR_STRING;
9985 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009986
9987#ifdef FEAT_MOUSE
9988 if (n == K_LEFTMOUSE
9989 || n == K_LEFTMOUSE_NM
9990 || n == K_LEFTDRAG
9991 || n == K_LEFTRELEASE
9992 || n == K_LEFTRELEASE_NM
9993 || n == K_MIDDLEMOUSE
9994 || n == K_MIDDLEDRAG
9995 || n == K_MIDDLERELEASE
9996 || n == K_RIGHTMOUSE
9997 || n == K_RIGHTDRAG
9998 || n == K_RIGHTRELEASE
9999 || n == K_X1MOUSE
10000 || n == K_X1DRAG
10001 || n == K_X1RELEASE
10002 || n == K_X2MOUSE
10003 || n == K_X2DRAG
10004 || n == K_X2RELEASE
10005 || n == K_MOUSEDOWN
10006 || n == K_MOUSEUP)
10007 {
10008 int row = mouse_row;
10009 int col = mouse_col;
10010 win_T *win;
10011 linenr_T lnum;
10012# ifdef FEAT_WINDOWS
10013 win_T *wp;
10014# endif
10015 int n = 1;
10016
10017 if (row >= 0 && col >= 0)
10018 {
10019 /* Find the window at the mouse coordinates and compute the
10020 * text position. */
10021 win = mouse_find_win(&row, &col);
10022 (void)mouse_comp_pos(win, &row, &col, &lnum);
10023# ifdef FEAT_WINDOWS
10024 for (wp = firstwin; wp != win; wp = wp->w_next)
10025 ++n;
10026# endif
10027 vimvars[VV_MOUSE_WIN].vv_nr = n;
10028 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10029 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10030 }
10031 }
10032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 }
10034}
10035
10036/*
10037 * "getcharmod()" function
10038 */
10039/*ARGSUSED*/
10040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010041f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010042 typval_T *argvars;
10043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * "getcmdline()" function
10050 */
10051/*ARGSUSED*/
10052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010054 typval_T *argvars;
10055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010057 rettv->v_type = VAR_STRING;
10058 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059}
10060
10061/*
10062 * "getcmdpos()" function
10063 */
10064/*ARGSUSED*/
10065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010067 typval_T *argvars;
10068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071}
10072
10073/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010074 * "getcmdtype()" function
10075 */
10076/*ARGSUSED*/
10077 static void
10078f_getcmdtype(argvars, rettv)
10079 typval_T *argvars;
10080 typval_T *rettv;
10081{
10082 rettv->v_type = VAR_STRING;
10083 rettv->vval.v_string = alloc(2);
10084 if (rettv->vval.v_string != NULL)
10085 {
10086 rettv->vval.v_string[0] = get_cmdline_type();
10087 rettv->vval.v_string[1] = NUL;
10088 }
10089}
10090
10091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 * "getcwd()" function
10093 */
10094/*ARGSUSED*/
10095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010097 typval_T *argvars;
10098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099{
10100 char_u cwd[MAXPATHL];
10101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 else
10106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010109 if (rettv->vval.v_string != NULL)
10110 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111#endif
10112 }
10113}
10114
10115/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010116 * "getfontname()" function
10117 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010118/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010121 typval_T *argvars;
10122 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010123{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->v_type = VAR_STRING;
10125 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010126#ifdef FEAT_GUI
10127 if (gui.in_use)
10128 {
10129 GuiFont font;
10130 char_u *name = NULL;
10131
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010132 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010133 {
10134 /* Get the "Normal" font. Either the name saved by
10135 * hl_set_font_name() or from the font ID. */
10136 font = gui.norm_font;
10137 name = hl_get_font_name();
10138 }
10139 else
10140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010142 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10143 return;
10144 font = gui_mch_get_font(name, FALSE);
10145 if (font == NOFONT)
10146 return; /* Invalid font name, return empty string. */
10147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010149 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010150 gui_mch_free_font(font);
10151 }
10152#endif
10153}
10154
10155/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010156 * "getfperm({fname})" function
10157 */
10158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010160 typval_T *argvars;
10161 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010162{
10163 char_u *fname;
10164 struct stat st;
10165 char_u *perm = NULL;
10166 char_u flags[] = "rwx";
10167 int i;
10168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010172 if (mch_stat((char *)fname, &st) >= 0)
10173 {
10174 perm = vim_strsave((char_u *)"---------");
10175 if (perm != NULL)
10176 {
10177 for (i = 0; i < 9; i++)
10178 {
10179 if (st.st_mode & (1 << (8 - i)))
10180 perm[i] = flags[i % 3];
10181 }
10182 }
10183 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010185}
10186
10187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 * "getfsize({fname})" function
10189 */
10190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010192 typval_T *argvars;
10193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194{
10195 char_u *fname;
10196 struct stat st;
10197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010198 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
10202 if (mch_stat((char *)fname, &st) >= 0)
10203 {
10204 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010209
10210 /* non-perfect check for overflow */
10211 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10212 rettv->vval.v_number = -2;
10213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 }
10215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217}
10218
10219/*
10220 * "getftime({fname})" function
10221 */
10222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 typval_T *argvars;
10225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 char_u *fname;
10228 struct stat st;
10229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010230 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231
10232 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010235 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236}
10237
10238/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010239 * "getftype({fname})" function
10240 */
10241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010242f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 typval_T *argvars;
10244 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010245{
10246 char_u *fname;
10247 struct stat st;
10248 char_u *type = NULL;
10249 char *t;
10250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010254 if (mch_lstat((char *)fname, &st) >= 0)
10255 {
10256#ifdef S_ISREG
10257 if (S_ISREG(st.st_mode))
10258 t = "file";
10259 else if (S_ISDIR(st.st_mode))
10260 t = "dir";
10261# ifdef S_ISLNK
10262 else if (S_ISLNK(st.st_mode))
10263 t = "link";
10264# endif
10265# ifdef S_ISBLK
10266 else if (S_ISBLK(st.st_mode))
10267 t = "bdev";
10268# endif
10269# ifdef S_ISCHR
10270 else if (S_ISCHR(st.st_mode))
10271 t = "cdev";
10272# endif
10273# ifdef S_ISFIFO
10274 else if (S_ISFIFO(st.st_mode))
10275 t = "fifo";
10276# endif
10277# ifdef S_ISSOCK
10278 else if (S_ISSOCK(st.st_mode))
10279 t = "fifo";
10280# endif
10281 else
10282 t = "other";
10283#else
10284# ifdef S_IFMT
10285 switch (st.st_mode & S_IFMT)
10286 {
10287 case S_IFREG: t = "file"; break;
10288 case S_IFDIR: t = "dir"; break;
10289# ifdef S_IFLNK
10290 case S_IFLNK: t = "link"; break;
10291# endif
10292# ifdef S_IFBLK
10293 case S_IFBLK: t = "bdev"; break;
10294# endif
10295# ifdef S_IFCHR
10296 case S_IFCHR: t = "cdev"; break;
10297# endif
10298# ifdef S_IFIFO
10299 case S_IFIFO: t = "fifo"; break;
10300# endif
10301# ifdef S_IFSOCK
10302 case S_IFSOCK: t = "socket"; break;
10303# endif
10304 default: t = "other";
10305 }
10306# else
10307 if (mch_isdir(fname))
10308 t = "dir";
10309 else
10310 t = "file";
10311# endif
10312#endif
10313 type = vim_strsave((char_u *)t);
10314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010316}
10317
10318/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010319 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010320 */
10321 static void
10322f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010323 typval_T *argvars;
10324 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010325{
10326 linenr_T lnum;
10327 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010328 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010329
10330 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010331 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010332 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010333 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010334 retlist = FALSE;
10335 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010336 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010337 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010338 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010339 retlist = TRUE;
10340 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010341
Bram Moolenaar342337a2005-07-21 21:11:17 +000010342 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010343}
10344
10345/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010346 * "getmatches()" function
10347 */
10348/*ARGSUSED*/
10349 static void
10350f_getmatches(argvars, rettv)
10351 typval_T *argvars;
10352 typval_T *rettv;
10353{
10354#ifdef FEAT_SEARCH_EXTRA
10355 dict_T *dict;
10356 matchitem_T *cur = curwin->w_match_head;
10357
10358 rettv->vval.v_number = 0;
10359
10360 if (rettv_list_alloc(rettv) == OK)
10361 {
10362 while (cur != NULL)
10363 {
10364 dict = dict_alloc();
10365 if (dict == NULL)
10366 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010367 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10368 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10369 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10370 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10371 list_append_dict(rettv->vval.v_list, dict);
10372 cur = cur->next;
10373 }
10374 }
10375#endif
10376}
10377
10378/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000010379 * "getpid()" function
10380 */
10381/*ARGSUSED*/
10382 static void
10383f_getpid(argvars, rettv)
10384 typval_T *argvars;
10385 typval_T *rettv;
10386{
10387 rettv->vval.v_number = mch_get_pid();
10388}
10389
10390/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010391 * "getpos(string)" function
10392 */
10393 static void
10394f_getpos(argvars, rettv)
10395 typval_T *argvars;
10396 typval_T *rettv;
10397{
10398 pos_T *fp;
10399 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010400 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010401
10402 if (rettv_list_alloc(rettv) == OK)
10403 {
10404 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010405 fp = var2fpos(&argvars[0], TRUE, &fnum);
10406 if (fnum != -1)
10407 list_append_number(l, (varnumber_T)fnum);
10408 else
10409 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010410 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10411 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010412 list_append_number(l, (fp != NULL)
10413 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010414 : (varnumber_T)0);
10415 list_append_number(l,
10416#ifdef FEAT_VIRTUALEDIT
10417 (fp != NULL) ? (varnumber_T)fp->coladd :
10418#endif
10419 (varnumber_T)0);
10420 }
10421 else
10422 rettv->vval.v_number = FALSE;
10423}
10424
10425/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010426 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010427 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010428/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010429 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010430f_getqflist(argvars, rettv)
10431 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010432 typval_T *rettv;
10433{
10434#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010435 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010436#endif
10437
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010438 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010439#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010440 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010441 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010442 wp = NULL;
10443 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10444 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010445 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010446 if (wp == NULL)
10447 return;
10448 }
10449
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010450 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010451 }
10452#endif
10453}
10454
10455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 * "getreg()" function
10457 */
10458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010460 typval_T *argvars;
10461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463 char_u *strregname;
10464 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010465 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010468 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010470 strregname = get_tv_string_chk(&argvars[0]);
10471 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010472 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010476 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 regname = (strregname == NULL ? '"' : *strregname);
10478 if (regname == 0)
10479 regname = '"';
10480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 rettv->vval.v_string = error ? NULL :
10483 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484}
10485
10486/*
10487 * "getregtype()" function
10488 */
10489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010490f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010491 typval_T *argvars;
10492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493{
10494 char_u *strregname;
10495 int regname;
10496 char_u buf[NUMBUFLEN + 2];
10497 long reglen = 0;
10498
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010499 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010500 {
10501 strregname = get_tv_string_chk(&argvars[0]);
10502 if (strregname == NULL) /* type error; errmsg already given */
10503 {
10504 rettv->v_type = VAR_STRING;
10505 rettv->vval.v_string = NULL;
10506 return;
10507 }
10508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 else
10510 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512
10513 regname = (strregname == NULL ? '"' : *strregname);
10514 if (regname == 0)
10515 regname = '"';
10516
10517 buf[0] = NUL;
10518 buf[1] = NUL;
10519 switch (get_reg_type(regname, &reglen))
10520 {
10521 case MLINE: buf[0] = 'V'; break;
10522 case MCHAR: buf[0] = 'v'; break;
10523#ifdef FEAT_VISUAL
10524 case MBLOCK:
10525 buf[0] = Ctrl_V;
10526 sprintf((char *)buf + 1, "%ld", reglen + 1);
10527 break;
10528#endif
10529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 rettv->v_type = VAR_STRING;
10531 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532}
10533
10534/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010535 * "gettabwinvar()" function
10536 */
10537 static void
10538f_gettabwinvar(argvars, rettv)
10539 typval_T *argvars;
10540 typval_T *rettv;
10541{
10542 getwinvar(argvars, rettv, 1);
10543}
10544
10545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 * "getwinposx()" function
10547 */
10548/*ARGSUSED*/
10549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010551 typval_T *argvars;
10552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555#ifdef FEAT_GUI
10556 if (gui.in_use)
10557 {
10558 int x, y;
10559
10560 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 }
10563#endif
10564}
10565
10566/*
10567 * "getwinposy()" function
10568 */
10569/*ARGSUSED*/
10570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010571f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010572 typval_T *argvars;
10573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576#ifdef FEAT_GUI
10577 if (gui.in_use)
10578 {
10579 int x, y;
10580
10581 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 }
10584#endif
10585}
10586
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010587/*
10588 * Find window specifed by "vp" in tabpage "tp".
10589 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010590 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010591find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010592 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010593 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010594{
10595#ifdef FEAT_WINDOWS
10596 win_T *wp;
10597#endif
10598 int nr;
10599
10600 nr = get_tv_number_chk(vp, NULL);
10601
10602#ifdef FEAT_WINDOWS
10603 if (nr < 0)
10604 return NULL;
10605 if (nr == 0)
10606 return curwin;
10607
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010608 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10609 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010610 if (--nr <= 0)
10611 break;
10612 return wp;
10613#else
10614 if (nr == 0 || nr == 1)
10615 return curwin;
10616 return NULL;
10617#endif
10618}
10619
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620/*
10621 * "getwinvar()" function
10622 */
10623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 typval_T *argvars;
10626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010628 getwinvar(argvars, rettv, 0);
10629}
10630
10631/*
10632 * getwinvar() and gettabwinvar()
10633 */
10634 static void
10635getwinvar(argvars, rettv, off)
10636 typval_T *argvars;
10637 typval_T *rettv;
10638 int off; /* 1 for gettabwinvar() */
10639{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 win_T *win, *oldcurwin;
10641 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010643 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010645#ifdef FEAT_WINDOWS
10646 if (off == 1)
10647 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10648 else
10649 tp = curtab;
10650#endif
10651 win = find_win_by_nr(&argvars[off], tp);
10652 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010653 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010655 rettv->v_type = VAR_STRING;
10656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657
10658 if (win != NULL && varname != NULL)
10659 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010660 /* Set curwin to be our win, temporarily. Also set curbuf, so
10661 * that we can get buffer-local options. */
10662 oldcurwin = curwin;
10663 curwin = win;
10664 curbuf = win->w_buffer;
10665
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 else
10669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 if (*varname == NUL)
10671 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10672 * scope prefix before the NUL byte is required by
10673 * find_var_in_ht(). */
10674 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010676 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010678 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010680
10681 /* restore previous notion of curwin */
10682 curwin = oldcurwin;
10683 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 }
10685
10686 --emsg_off;
10687}
10688
10689/*
10690 * "glob()" function
10691 */
10692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693f_glob(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 expand_T xpc;
10698
10699 ExpandInit(&xpc);
10700 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701 rettv->v_type = VAR_STRING;
10702 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704}
10705
10706/*
10707 * "globpath()" function
10708 */
10709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010711 typval_T *argvars;
10712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713{
10714 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010715 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010718 if (file == NULL)
10719 rettv->vval.v_string = NULL;
10720 else
10721 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722}
10723
10724/*
10725 * "has()" function
10726 */
10727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010729 typval_T *argvars;
10730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731{
10732 int i;
10733 char_u *name;
10734 int n = FALSE;
10735 static char *(has_list[]) =
10736 {
10737#ifdef AMIGA
10738 "amiga",
10739# ifdef FEAT_ARP
10740 "arp",
10741# endif
10742#endif
10743#ifdef __BEOS__
10744 "beos",
10745#endif
10746#ifdef MSDOS
10747# ifdef DJGPP
10748 "dos32",
10749# else
10750 "dos16",
10751# endif
10752#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010753#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 "mac",
10755#endif
10756#if defined(MACOS_X_UNIX)
10757 "macunix",
10758#endif
10759#ifdef OS2
10760 "os2",
10761#endif
10762#ifdef __QNX__
10763 "qnx",
10764#endif
10765#ifdef RISCOS
10766 "riscos",
10767#endif
10768#ifdef UNIX
10769 "unix",
10770#endif
10771#ifdef VMS
10772 "vms",
10773#endif
10774#ifdef WIN16
10775 "win16",
10776#endif
10777#ifdef WIN32
10778 "win32",
10779#endif
10780#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10781 "win32unix",
10782#endif
10783#ifdef WIN64
10784 "win64",
10785#endif
10786#ifdef EBCDIC
10787 "ebcdic",
10788#endif
10789#ifndef CASE_INSENSITIVE_FILENAME
10790 "fname_case",
10791#endif
10792#ifdef FEAT_ARABIC
10793 "arabic",
10794#endif
10795#ifdef FEAT_AUTOCMD
10796 "autocmd",
10797#endif
10798#ifdef FEAT_BEVAL
10799 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010800# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10801 "balloon_multiline",
10802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803#endif
10804#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10805 "builtin_terms",
10806# ifdef ALL_BUILTIN_TCAPS
10807 "all_builtin_terms",
10808# endif
10809#endif
10810#ifdef FEAT_BYTEOFF
10811 "byte_offset",
10812#endif
10813#ifdef FEAT_CINDENT
10814 "cindent",
10815#endif
10816#ifdef FEAT_CLIENTSERVER
10817 "clientserver",
10818#endif
10819#ifdef FEAT_CLIPBOARD
10820 "clipboard",
10821#endif
10822#ifdef FEAT_CMDL_COMPL
10823 "cmdline_compl",
10824#endif
10825#ifdef FEAT_CMDHIST
10826 "cmdline_hist",
10827#endif
10828#ifdef FEAT_COMMENTS
10829 "comments",
10830#endif
10831#ifdef FEAT_CRYPT
10832 "cryptv",
10833#endif
10834#ifdef FEAT_CSCOPE
10835 "cscope",
10836#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010837#ifdef CURSOR_SHAPE
10838 "cursorshape",
10839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840#ifdef DEBUG
10841 "debug",
10842#endif
10843#ifdef FEAT_CON_DIALOG
10844 "dialog_con",
10845#endif
10846#ifdef FEAT_GUI_DIALOG
10847 "dialog_gui",
10848#endif
10849#ifdef FEAT_DIFF
10850 "diff",
10851#endif
10852#ifdef FEAT_DIGRAPHS
10853 "digraphs",
10854#endif
10855#ifdef FEAT_DND
10856 "dnd",
10857#endif
10858#ifdef FEAT_EMACS_TAGS
10859 "emacs_tags",
10860#endif
10861 "eval", /* always present, of course! */
10862#ifdef FEAT_EX_EXTRA
10863 "ex_extra",
10864#endif
10865#ifdef FEAT_SEARCH_EXTRA
10866 "extra_search",
10867#endif
10868#ifdef FEAT_FKMAP
10869 "farsi",
10870#endif
10871#ifdef FEAT_SEARCHPATH
10872 "file_in_path",
10873#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010874#if defined(UNIX) && !defined(USE_SYSTEM)
10875 "filterpipe",
10876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877#ifdef FEAT_FIND_ID
10878 "find_in_path",
10879#endif
10880#ifdef FEAT_FOLDING
10881 "folding",
10882#endif
10883#ifdef FEAT_FOOTER
10884 "footer",
10885#endif
10886#if !defined(USE_SYSTEM) && defined(UNIX)
10887 "fork",
10888#endif
10889#ifdef FEAT_GETTEXT
10890 "gettext",
10891#endif
10892#ifdef FEAT_GUI
10893 "gui",
10894#endif
10895#ifdef FEAT_GUI_ATHENA
10896# ifdef FEAT_GUI_NEXTAW
10897 "gui_neXtaw",
10898# else
10899 "gui_athena",
10900# endif
10901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902#ifdef FEAT_GUI_GTK
10903 "gui_gtk",
10904# ifdef HAVE_GTK2
10905 "gui_gtk2",
10906# endif
10907#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000010908#ifdef FEAT_GUI_GNOME
10909 "gui_gnome",
10910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911#ifdef FEAT_GUI_MAC
10912 "gui_mac",
10913#endif
10914#ifdef FEAT_GUI_MOTIF
10915 "gui_motif",
10916#endif
10917#ifdef FEAT_GUI_PHOTON
10918 "gui_photon",
10919#endif
10920#ifdef FEAT_GUI_W16
10921 "gui_win16",
10922#endif
10923#ifdef FEAT_GUI_W32
10924 "gui_win32",
10925#endif
10926#ifdef FEAT_HANGULIN
10927 "hangul_input",
10928#endif
10929#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10930 "iconv",
10931#endif
10932#ifdef FEAT_INS_EXPAND
10933 "insert_expand",
10934#endif
10935#ifdef FEAT_JUMPLIST
10936 "jumplist",
10937#endif
10938#ifdef FEAT_KEYMAP
10939 "keymap",
10940#endif
10941#ifdef FEAT_LANGMAP
10942 "langmap",
10943#endif
10944#ifdef FEAT_LIBCALL
10945 "libcall",
10946#endif
10947#ifdef FEAT_LINEBREAK
10948 "linebreak",
10949#endif
10950#ifdef FEAT_LISP
10951 "lispindent",
10952#endif
10953#ifdef FEAT_LISTCMDS
10954 "listcmds",
10955#endif
10956#ifdef FEAT_LOCALMAP
10957 "localmap",
10958#endif
10959#ifdef FEAT_MENU
10960 "menu",
10961#endif
10962#ifdef FEAT_SESSION
10963 "mksession",
10964#endif
10965#ifdef FEAT_MODIFY_FNAME
10966 "modify_fname",
10967#endif
10968#ifdef FEAT_MOUSE
10969 "mouse",
10970#endif
10971#ifdef FEAT_MOUSESHAPE
10972 "mouseshape",
10973#endif
10974#if defined(UNIX) || defined(VMS)
10975# ifdef FEAT_MOUSE_DEC
10976 "mouse_dec",
10977# endif
10978# ifdef FEAT_MOUSE_GPM
10979 "mouse_gpm",
10980# endif
10981# ifdef FEAT_MOUSE_JSB
10982 "mouse_jsbterm",
10983# endif
10984# ifdef FEAT_MOUSE_NET
10985 "mouse_netterm",
10986# endif
10987# ifdef FEAT_MOUSE_PTERM
10988 "mouse_pterm",
10989# endif
10990# ifdef FEAT_MOUSE_XTERM
10991 "mouse_xterm",
10992# endif
10993#endif
10994#ifdef FEAT_MBYTE
10995 "multi_byte",
10996#endif
10997#ifdef FEAT_MBYTE_IME
10998 "multi_byte_ime",
10999#endif
11000#ifdef FEAT_MULTI_LANG
11001 "multi_lang",
11002#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011003#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011004#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011005 "mzscheme",
11006#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008#ifdef FEAT_OLE
11009 "ole",
11010#endif
11011#ifdef FEAT_OSFILETYPE
11012 "osfiletype",
11013#endif
11014#ifdef FEAT_PATH_EXTRA
11015 "path_extra",
11016#endif
11017#ifdef FEAT_PERL
11018#ifndef DYNAMIC_PERL
11019 "perl",
11020#endif
11021#endif
11022#ifdef FEAT_PYTHON
11023#ifndef DYNAMIC_PYTHON
11024 "python",
11025#endif
11026#endif
11027#ifdef FEAT_POSTSCRIPT
11028 "postscript",
11029#endif
11030#ifdef FEAT_PRINTER
11031 "printer",
11032#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011033#ifdef FEAT_PROFILE
11034 "profile",
11035#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011036#ifdef FEAT_RELTIME
11037 "reltime",
11038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039#ifdef FEAT_QUICKFIX
11040 "quickfix",
11041#endif
11042#ifdef FEAT_RIGHTLEFT
11043 "rightleft",
11044#endif
11045#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11046 "ruby",
11047#endif
11048#ifdef FEAT_SCROLLBIND
11049 "scrollbind",
11050#endif
11051#ifdef FEAT_CMDL_INFO
11052 "showcmd",
11053 "cmdline_info",
11054#endif
11055#ifdef FEAT_SIGNS
11056 "signs",
11057#endif
11058#ifdef FEAT_SMARTINDENT
11059 "smartindent",
11060#endif
11061#ifdef FEAT_SNIFF
11062 "sniff",
11063#endif
11064#ifdef FEAT_STL_OPT
11065 "statusline",
11066#endif
11067#ifdef FEAT_SUN_WORKSHOP
11068 "sun_workshop",
11069#endif
11070#ifdef FEAT_NETBEANS_INTG
11071 "netbeans_intg",
11072#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011073#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011074 "spell",
11075#endif
11076#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 "syntax",
11078#endif
11079#if defined(USE_SYSTEM) || !defined(UNIX)
11080 "system",
11081#endif
11082#ifdef FEAT_TAG_BINS
11083 "tag_binary",
11084#endif
11085#ifdef FEAT_TAG_OLDSTATIC
11086 "tag_old_static",
11087#endif
11088#ifdef FEAT_TAG_ANYWHITE
11089 "tag_any_white",
11090#endif
11091#ifdef FEAT_TCL
11092# ifndef DYNAMIC_TCL
11093 "tcl",
11094# endif
11095#endif
11096#ifdef TERMINFO
11097 "terminfo",
11098#endif
11099#ifdef FEAT_TERMRESPONSE
11100 "termresponse",
11101#endif
11102#ifdef FEAT_TEXTOBJ
11103 "textobjects",
11104#endif
11105#ifdef HAVE_TGETENT
11106 "tgetent",
11107#endif
11108#ifdef FEAT_TITLE
11109 "title",
11110#endif
11111#ifdef FEAT_TOOLBAR
11112 "toolbar",
11113#endif
11114#ifdef FEAT_USR_CMDS
11115 "user-commands", /* was accidentally included in 5.4 */
11116 "user_commands",
11117#endif
11118#ifdef FEAT_VIMINFO
11119 "viminfo",
11120#endif
11121#ifdef FEAT_VERTSPLIT
11122 "vertsplit",
11123#endif
11124#ifdef FEAT_VIRTUALEDIT
11125 "virtualedit",
11126#endif
11127#ifdef FEAT_VISUAL
11128 "visual",
11129#endif
11130#ifdef FEAT_VISUALEXTRA
11131 "visualextra",
11132#endif
11133#ifdef FEAT_VREPLACE
11134 "vreplace",
11135#endif
11136#ifdef FEAT_WILDIGN
11137 "wildignore",
11138#endif
11139#ifdef FEAT_WILDMENU
11140 "wildmenu",
11141#endif
11142#ifdef FEAT_WINDOWS
11143 "windows",
11144#endif
11145#ifdef FEAT_WAK
11146 "winaltkeys",
11147#endif
11148#ifdef FEAT_WRITEBACKUP
11149 "writebackup",
11150#endif
11151#ifdef FEAT_XIM
11152 "xim",
11153#endif
11154#ifdef FEAT_XFONTSET
11155 "xfontset",
11156#endif
11157#ifdef USE_XSMP
11158 "xsmp",
11159#endif
11160#ifdef USE_XSMP_INTERACT
11161 "xsmp_interact",
11162#endif
11163#ifdef FEAT_XCLIPBOARD
11164 "xterm_clipboard",
11165#endif
11166#ifdef FEAT_XTERM_SAVE
11167 "xterm_save",
11168#endif
11169#if defined(UNIX) && defined(FEAT_X11)
11170 "X11",
11171#endif
11172 NULL
11173 };
11174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 for (i = 0; has_list[i] != NULL; ++i)
11177 if (STRICMP(name, has_list[i]) == 0)
11178 {
11179 n = TRUE;
11180 break;
11181 }
11182
11183 if (n == FALSE)
11184 {
11185 if (STRNICMP(name, "patch", 5) == 0)
11186 n = has_patch(atoi((char *)name + 5));
11187 else if (STRICMP(name, "vim_starting") == 0)
11188 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011189#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11190 else if (STRICMP(name, "balloon_multiline") == 0)
11191 n = multiline_balloon_available();
11192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193#ifdef DYNAMIC_TCL
11194 else if (STRICMP(name, "tcl") == 0)
11195 n = tcl_enabled(FALSE);
11196#endif
11197#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11198 else if (STRICMP(name, "iconv") == 0)
11199 n = iconv_enabled(FALSE);
11200#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011201#ifdef DYNAMIC_MZSCHEME
11202 else if (STRICMP(name, "mzscheme") == 0)
11203 n = mzscheme_enabled(FALSE);
11204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205#ifdef DYNAMIC_RUBY
11206 else if (STRICMP(name, "ruby") == 0)
11207 n = ruby_enabled(FALSE);
11208#endif
11209#ifdef DYNAMIC_PYTHON
11210 else if (STRICMP(name, "python") == 0)
11211 n = python_enabled(FALSE);
11212#endif
11213#ifdef DYNAMIC_PERL
11214 else if (STRICMP(name, "perl") == 0)
11215 n = perl_enabled(FALSE);
11216#endif
11217#ifdef FEAT_GUI
11218 else if (STRICMP(name, "gui_running") == 0)
11219 n = (gui.in_use || gui.starting);
11220# ifdef FEAT_GUI_W32
11221 else if (STRICMP(name, "gui_win32s") == 0)
11222 n = gui_is_win32s();
11223# endif
11224# ifdef FEAT_BROWSE
11225 else if (STRICMP(name, "browse") == 0)
11226 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11227# endif
11228#endif
11229#ifdef FEAT_SYN_HL
11230 else if (STRICMP(name, "syntax_items") == 0)
11231 n = syntax_present(curbuf);
11232#endif
11233#if defined(WIN3264)
11234 else if (STRICMP(name, "win95") == 0)
11235 n = mch_windows95();
11236#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011237#ifdef FEAT_NETBEANS_INTG
11238 else if (STRICMP(name, "netbeans_enabled") == 0)
11239 n = usingNetbeans;
11240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 }
11242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244}
11245
11246/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011247 * "has_key()" function
11248 */
11249 static void
11250f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011251 typval_T *argvars;
11252 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011253{
11254 rettv->vval.v_number = 0;
11255 if (argvars[0].v_type != VAR_DICT)
11256 {
11257 EMSG(_(e_dictreq));
11258 return;
11259 }
11260 if (argvars[0].vval.v_dict == NULL)
11261 return;
11262
11263 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011264 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011265}
11266
11267/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011268 * "haslocaldir()" function
11269 */
11270/*ARGSUSED*/
11271 static void
11272f_haslocaldir(argvars, rettv)
11273 typval_T *argvars;
11274 typval_T *rettv;
11275{
11276 rettv->vval.v_number = (curwin->w_localdir != NULL);
11277}
11278
11279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 * "hasmapto()" function
11281 */
11282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
11287 char_u *name;
11288 char_u *mode;
11289 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011290 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011292 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011293 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 mode = (char_u *)"nvo";
11295 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011298 if (argvars[2].v_type != VAR_UNKNOWN)
11299 abbr = get_tv_number(&argvars[2]);
11300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301
Bram Moolenaar2c932302006-03-18 21:42:09 +000011302 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306}
11307
11308/*
11309 * "histadd()" function
11310 */
11311/*ARGSUSED*/
11312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011314 typval_T *argvars;
11315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316{
11317#ifdef FEAT_CMDHIST
11318 int histype;
11319 char_u *str;
11320 char_u buf[NUMBUFLEN];
11321#endif
11322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 if (check_restricted() || check_secure())
11325 return;
11326#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11328 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 if (histype >= 0)
11330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 if (*str != NUL)
11333 {
11334 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 return;
11337 }
11338 }
11339#endif
11340}
11341
11342/*
11343 * "histdel()" function
11344 */
11345/*ARGSUSED*/
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 typval_T *argvars;
11349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350{
11351#ifdef FEAT_CMDHIST
11352 int n;
11353 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011354 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011356 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11357 if (str == NULL)
11358 n = 0;
11359 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011361 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011362 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 else
11367 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 get_tv_string_buf(&argvars[1], buf));
11370 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373#endif
11374}
11375
11376/*
11377 * "histget()" function
11378 */
11379/*ARGSUSED*/
11380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011382 typval_T *argvars;
11383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384{
11385#ifdef FEAT_CMDHIST
11386 int type;
11387 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11391 if (str == NULL)
11392 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011394 {
11395 type = get_histtype(str);
11396 if (argvars[1].v_type == VAR_UNKNOWN)
11397 idx = get_history_idx(type);
11398 else
11399 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11400 /* -1 on type error */
11401 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407}
11408
11409/*
11410 * "histnr()" function
11411 */
11412/*ARGSUSED*/
11413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011415 typval_T *argvars;
11416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417{
11418 int i;
11419
11420#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 char_u *history = get_tv_string_chk(&argvars[0]);
11422
11423 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 if (i >= HIST_CMD && i < HIST_COUNT)
11425 i = get_history_idx(i);
11426 else
11427#endif
11428 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430}
11431
11432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 * "highlightID(name)" function
11434 */
11435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *argvars;
11438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441}
11442
11443/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011444 * "highlight_exists()" function
11445 */
11446 static void
11447f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *argvars;
11449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011450{
11451 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11452}
11453
11454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 * "hostname()" function
11456 */
11457/*ARGSUSED*/
11458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011460 typval_T *argvars;
11461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462{
11463 char_u hostname[256];
11464
11465 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->v_type = VAR_STRING;
11467 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468}
11469
11470/*
11471 * iconv() function
11472 */
11473/*ARGSUSED*/
11474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011476 typval_T *argvars;
11477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478{
11479#ifdef FEAT_MBYTE
11480 char_u buf1[NUMBUFLEN];
11481 char_u buf2[NUMBUFLEN];
11482 char_u *from, *to, *str;
11483 vimconv_T vimconv;
11484#endif
11485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 rettv->v_type = VAR_STRING;
11487 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488
11489#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 str = get_tv_string(&argvars[0]);
11491 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11492 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 vimconv.vc_type = CONV_NONE;
11494 convert_setup(&vimconv, from, to);
11495
11496 /* If the encodings are equal, no conversion needed. */
11497 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501
11502 convert_setup(&vimconv, NULL, NULL);
11503 vim_free(from);
11504 vim_free(to);
11505#endif
11506}
11507
11508/*
11509 * "indent()" function
11510 */
11511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 typval_T *argvars;
11514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515{
11516 linenr_T lnum;
11517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523}
11524
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011525/*
11526 * "index()" function
11527 */
11528 static void
11529f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011530 typval_T *argvars;
11531 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011532{
Bram Moolenaar33570922005-01-25 22:26:29 +000011533 list_T *l;
11534 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011535 long idx = 0;
11536 int ic = FALSE;
11537
11538 rettv->vval.v_number = -1;
11539 if (argvars[0].v_type != VAR_LIST)
11540 {
11541 EMSG(_(e_listreq));
11542 return;
11543 }
11544 l = argvars[0].vval.v_list;
11545 if (l != NULL)
11546 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011547 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011548 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 int error = FALSE;
11551
Bram Moolenaar758711c2005-02-02 23:11:38 +000011552 /* Start at specified item. Use the cached index that list_find()
11553 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011555 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011556 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011557 ic = get_tv_number_chk(&argvars[3], &error);
11558 if (error)
11559 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011560 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011561
Bram Moolenaar758711c2005-02-02 23:11:38 +000011562 for ( ; item != NULL; item = item->li_next, ++idx)
11563 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011564 {
11565 rettv->vval.v_number = idx;
11566 break;
11567 }
11568 }
11569}
11570
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571static int inputsecret_flag = 0;
11572
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011573static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11574
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011576 * This function is used by f_input() and f_inputdialog() functions. The third
11577 * argument to f_input() specifies the type of completion to use at the
11578 * prompt. The third argument to f_inputdialog() specifies the value to return
11579 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 */
11581 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011582get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011583 typval_T *argvars;
11584 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011585 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011587 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 char_u *p = NULL;
11589 int c;
11590 char_u buf[NUMBUFLEN];
11591 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011593 int xp_type = EXPAND_NOTHING;
11594 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598
11599#ifdef NO_CONSOLE_INPUT
11600 /* While starting up, there is no place to enter text. */
11601 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603#endif
11604
11605 cmd_silent = FALSE; /* Want to see the prompt. */
11606 if (prompt != NULL)
11607 {
11608 /* Only the part of the message after the last NL is considered as
11609 * prompt for the command line */
11610 p = vim_strrchr(prompt, '\n');
11611 if (p == NULL)
11612 p = prompt;
11613 else
11614 {
11615 ++p;
11616 c = *p;
11617 *p = NUL;
11618 msg_start();
11619 msg_clr_eos();
11620 msg_puts_attr(prompt, echo_attr);
11621 msg_didout = FALSE;
11622 msg_starthere();
11623 *p = c;
11624 }
11625 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011627 if (argvars[1].v_type != VAR_UNKNOWN)
11628 {
11629 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11630 if (defstr != NULL)
11631 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011633 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011634 {
11635 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011636 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011637 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011638
Bram Moolenaar4463f292005-09-25 22:20:24 +000011639 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011640
Bram Moolenaar4463f292005-09-25 22:20:24 +000011641 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11642 if (xp_name == NULL)
11643 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011644
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011645 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011646
Bram Moolenaar4463f292005-09-25 22:20:24 +000011647 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11648 &xp_arg) == FAIL)
11649 return;
11650 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011651 }
11652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011653 if (defstr != NULL)
11654 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011655 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11656 xp_type, xp_arg);
11657
11658 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011660 /* since the user typed this, no need to wait for return */
11661 need_wait_return = FALSE;
11662 msg_didout = FALSE;
11663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 cmd_silent = cmd_silent_save;
11665}
11666
11667/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011668 * "input()" function
11669 * Also handles inputsecret() when inputsecret is set.
11670 */
11671 static void
11672f_input(argvars, rettv)
11673 typval_T *argvars;
11674 typval_T *rettv;
11675{
11676 get_user_input(argvars, rettv, FALSE);
11677}
11678
11679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 * "inputdialog()" function
11681 */
11682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *argvars;
11685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686{
11687#if defined(FEAT_GUI_TEXTDIALOG)
11688 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11689 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11690 {
11691 char_u *message;
11692 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 message = get_tv_string_chk(&argvars[0]);
11696 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011697 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011698 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 else
11700 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701 if (message != NULL && defstr != NULL
11702 && do_dialog(VIM_QUESTION, NULL, message,
11703 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 else
11706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 if (message != NULL && defstr != NULL
11708 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011709 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011710 rettv->vval.v_string = vim_strsave(
11711 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 }
11717 else
11718#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011719 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720}
11721
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011722/*
11723 * "inputlist()" function
11724 */
11725 static void
11726f_inputlist(argvars, rettv)
11727 typval_T *argvars;
11728 typval_T *rettv;
11729{
11730 listitem_T *li;
11731 int selected;
11732 int mouse_used;
11733
11734 rettv->vval.v_number = 0;
11735#ifdef NO_CONSOLE_INPUT
11736 /* While starting up, there is no place to enter text. */
11737 if (no_console_input())
11738 return;
11739#endif
11740 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11741 {
11742 EMSG2(_(e_listarg), "inputlist()");
11743 return;
11744 }
11745
11746 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011747 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011748 lines_left = Rows; /* avoid more prompt */
11749 msg_scroll = TRUE;
11750 msg_clr_eos();
11751
11752 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11753 {
11754 msg_puts(get_tv_string(&li->li_tv));
11755 msg_putchar('\n');
11756 }
11757
11758 /* Ask for choice. */
11759 selected = prompt_for_number(&mouse_used);
11760 if (mouse_used)
11761 selected -= lines_left;
11762
11763 rettv->vval.v_number = selected;
11764}
11765
11766
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11768
11769/*
11770 * "inputrestore()" function
11771 */
11772/*ARGSUSED*/
11773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011774f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011775 typval_T *argvars;
11776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777{
11778 if (ga_userinput.ga_len > 0)
11779 {
11780 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11782 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 }
11785 else if (p_verbose > 1)
11786 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011787 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 }
11790}
11791
11792/*
11793 * "inputsave()" function
11794 */
11795/*ARGSUSED*/
11796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011798 typval_T *argvars;
11799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800{
11801 /* Add an entry to the stack of typehead storage. */
11802 if (ga_grow(&ga_userinput, 1) == OK)
11803 {
11804 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11805 + ga_userinput.ga_len);
11806 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 }
11809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811}
11812
11813/*
11814 * "inputsecret()" function
11815 */
11816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011818 typval_T *argvars;
11819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820{
11821 ++cmdline_star;
11822 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011823 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 --cmdline_star;
11825 --inputsecret_flag;
11826}
11827
11828/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011829 * "insert()" function
11830 */
11831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011833 typval_T *argvars;
11834 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011835{
11836 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011837 listitem_T *item;
11838 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011839 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011840
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011841 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011842 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011843 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011844 else if ((l = argvars[0].vval.v_list) != NULL
11845 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011846 {
11847 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 before = get_tv_number_chk(&argvars[2], &error);
11849 if (error)
11850 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011851
Bram Moolenaar758711c2005-02-02 23:11:38 +000011852 if (before == l->lv_len)
11853 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011854 else
11855 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011856 item = list_find(l, before);
11857 if (item == NULL)
11858 {
11859 EMSGN(_(e_listidx), before);
11860 l = NULL;
11861 }
11862 }
11863 if (l != NULL)
11864 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011865 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011866 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011867 }
11868 }
11869}
11870
11871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 * "isdirectory()" function
11873 */
11874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011876 typval_T *argvars;
11877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880}
11881
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011882/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011883 * "islocked()" function
11884 */
11885 static void
11886f_islocked(argvars, rettv)
11887 typval_T *argvars;
11888 typval_T *rettv;
11889{
11890 lval_T lv;
11891 char_u *end;
11892 dictitem_T *di;
11893
11894 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011895 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11896 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011897 if (end != NULL && lv.ll_name != NULL)
11898 {
11899 if (*end != NUL)
11900 EMSG(_(e_trailing));
11901 else
11902 {
11903 if (lv.ll_tv == NULL)
11904 {
11905 if (check_changedtick(lv.ll_name))
11906 rettv->vval.v_number = 1; /* always locked */
11907 else
11908 {
11909 di = find_var(lv.ll_name, NULL);
11910 if (di != NULL)
11911 {
11912 /* Consider a variable locked when:
11913 * 1. the variable itself is locked
11914 * 2. the value of the variable is locked.
11915 * 3. the List or Dict value is locked.
11916 */
11917 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11918 || tv_islocked(&di->di_tv));
11919 }
11920 }
11921 }
11922 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011923 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011924 else if (lv.ll_newkey != NULL)
11925 EMSG2(_(e_dictkey), lv.ll_newkey);
11926 else if (lv.ll_list != NULL)
11927 /* List item. */
11928 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11929 else
11930 /* Dictionary item. */
11931 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11932 }
11933 }
11934
11935 clear_lval(&lv);
11936}
11937
Bram Moolenaar33570922005-01-25 22:26:29 +000011938static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011939
11940/*
11941 * Turn a dict into a list:
11942 * "what" == 0: list of keys
11943 * "what" == 1: list of values
11944 * "what" == 2: list of items
11945 */
11946 static void
11947dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 typval_T *argvars;
11949 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011950 int what;
11951{
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 list_T *l2;
11953 dictitem_T *di;
11954 hashitem_T *hi;
11955 listitem_T *li;
11956 listitem_T *li2;
11957 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011958 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011959
11960 rettv->vval.v_number = 0;
11961 if (argvars[0].v_type != VAR_DICT)
11962 {
11963 EMSG(_(e_dictreq));
11964 return;
11965 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011966 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011967 return;
11968
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011969 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011970 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011971
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011972 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011973 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011975 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011976 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011977 --todo;
11978 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011979
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011980 li = listitem_alloc();
11981 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011982 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011983 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011984
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011985 if (what == 0)
11986 {
11987 /* keys() */
11988 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011989 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011990 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11991 }
11992 else if (what == 1)
11993 {
11994 /* values() */
11995 copy_tv(&di->di_tv, &li->li_tv);
11996 }
11997 else
11998 {
11999 /* items() */
12000 l2 = list_alloc();
12001 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012002 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012003 li->li_tv.vval.v_list = l2;
12004 if (l2 == NULL)
12005 break;
12006 ++l2->lv_refcount;
12007
12008 li2 = listitem_alloc();
12009 if (li2 == NULL)
12010 break;
12011 list_append(l2, li2);
12012 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012013 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012014 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12015
12016 li2 = listitem_alloc();
12017 if (li2 == NULL)
12018 break;
12019 list_append(l2, li2);
12020 copy_tv(&di->di_tv, &li2->li_tv);
12021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012022 }
12023 }
12024}
12025
12026/*
12027 * "items(dict)" function
12028 */
12029 static void
12030f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012031 typval_T *argvars;
12032 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012033{
12034 dict_list(argvars, rettv, 2);
12035}
12036
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012038 * "join()" function
12039 */
12040 static void
12041f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 typval_T *argvars;
12043 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012044{
12045 garray_T ga;
12046 char_u *sep;
12047
12048 rettv->vval.v_number = 0;
12049 if (argvars[0].v_type != VAR_LIST)
12050 {
12051 EMSG(_(e_listreq));
12052 return;
12053 }
12054 if (argvars[0].vval.v_list == NULL)
12055 return;
12056 if (argvars[1].v_type == VAR_UNKNOWN)
12057 sep = (char_u *)" ";
12058 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012060
12061 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012062
12063 if (sep != NULL)
12064 {
12065 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012066 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 ga_append(&ga, NUL);
12068 rettv->vval.v_string = (char_u *)ga.ga_data;
12069 }
12070 else
12071 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012072}
12073
12074/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012075 * "keys()" function
12076 */
12077 static void
12078f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012079 typval_T *argvars;
12080 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012081{
12082 dict_list(argvars, rettv, 0);
12083}
12084
12085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 * "last_buffer_nr()" function.
12087 */
12088/*ARGSUSED*/
12089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012091 typval_T *argvars;
12092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093{
12094 int n = 0;
12095 buf_T *buf;
12096
12097 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12098 if (n < buf->b_fnum)
12099 n = buf->b_fnum;
12100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012102}
12103
12104/*
12105 * "len()" function
12106 */
12107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012109 typval_T *argvars;
12110 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012111{
12112 switch (argvars[0].v_type)
12113 {
12114 case VAR_STRING:
12115 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->vval.v_number = (varnumber_T)STRLEN(
12117 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012118 break;
12119 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012121 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012122 case VAR_DICT:
12123 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12124 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012125 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012126 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012127 break;
12128 }
12129}
12130
Bram Moolenaar33570922005-01-25 22:26:29 +000012131static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012132
12133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012135 typval_T *argvars;
12136 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012137 int type;
12138{
12139#ifdef FEAT_LIBCALL
12140 char_u *string_in;
12141 char_u **string_result;
12142 int nr_result;
12143#endif
12144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012145 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012146 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012148 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012150
12151 if (check_restricted() || check_secure())
12152 return;
12153
12154#ifdef FEAT_LIBCALL
12155 /* The first two args must be strings, otherwise its meaningless */
12156 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12157 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012158 string_in = NULL;
12159 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012160 string_in = argvars[2].vval.v_string;
12161 if (type == VAR_NUMBER)
12162 string_result = NULL;
12163 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012165 if (mch_libcall(argvars[0].vval.v_string,
12166 argvars[1].vval.v_string,
12167 string_in,
12168 argvars[2].vval.v_number,
12169 string_result,
12170 &nr_result) == OK
12171 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012173 }
12174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175}
12176
12177/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012178 * "libcall()" function
12179 */
12180 static void
12181f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012182 typval_T *argvars;
12183 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012184{
12185 libcall_common(argvars, rettv, VAR_STRING);
12186}
12187
12188/*
12189 * "libcallnr()" function
12190 */
12191 static void
12192f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012193 typval_T *argvars;
12194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012195{
12196 libcall_common(argvars, rettv, VAR_NUMBER);
12197}
12198
12199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 * "line(string)" function
12201 */
12202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012203f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012204 typval_T *argvars;
12205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206{
12207 linenr_T lnum = 0;
12208 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012209 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012211 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 if (fp != NULL)
12213 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215}
12216
12217/*
12218 * "line2byte(lnum)" function
12219 */
12220/*ARGSUSED*/
12221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012222f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012223 typval_T *argvars;
12224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225{
12226#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228#else
12229 linenr_T lnum;
12230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12236 if (rettv->vval.v_number >= 0)
12237 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238#endif
12239}
12240
12241/*
12242 * "lispindent(lnum)" function
12243 */
12244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012246 typval_T *argvars;
12247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248{
12249#ifdef FEAT_LISP
12250 pos_T pos;
12251 linenr_T lnum;
12252
12253 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12256 {
12257 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 curwin->w_cursor = pos;
12260 }
12261 else
12262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012263 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264}
12265
12266/*
12267 * "localtime()" function
12268 */
12269/*ARGSUSED*/
12270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012271f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012272 typval_T *argvars;
12273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276}
12277
Bram Moolenaar33570922005-01-25 22:26:29 +000012278static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279
12280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012281get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012282 typval_T *argvars;
12283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 int exact;
12285{
12286 char_u *keys;
12287 char_u *which;
12288 char_u buf[NUMBUFLEN];
12289 char_u *keys_buf = NULL;
12290 char_u *rhs;
12291 int mode;
12292 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012293 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294
12295 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296 rettv->v_type = VAR_STRING;
12297 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 if (*keys == NUL)
12301 return;
12302
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012303 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012305 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012306 if (argvars[2].v_type != VAR_UNKNOWN)
12307 abbr = get_tv_number(&argvars[2]);
12308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 else
12310 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012311 if (which == NULL)
12312 return;
12313
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 mode = get_map_mode(&which, 0);
12315
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012316 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012317 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 vim_free(keys_buf);
12319 if (rhs != NULL)
12320 {
12321 ga_init(&ga);
12322 ga.ga_itemsize = 1;
12323 ga.ga_growsize = 40;
12324
12325 while (*rhs != NUL)
12326 ga_concat(&ga, str2special(&rhs, FALSE));
12327
12328 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 }
12331}
12332
12333/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012334 * "map()" function
12335 */
12336 static void
12337f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012338 typval_T *argvars;
12339 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012340{
12341 filter_map(argvars, rettv, TRUE);
12342}
12343
12344/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012345 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 */
12347 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012348f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012349 typval_T *argvars;
12350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012352 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353}
12354
12355/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012356 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 */
12358 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012359f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012360 typval_T *argvars;
12361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012363 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364}
12365
Bram Moolenaar33570922005-01-25 22:26:29 +000012366static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367
12368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012370 typval_T *argvars;
12371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 int type;
12373{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012374 char_u *str = NULL;
12375 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 char_u *pat;
12377 regmatch_T regmatch;
12378 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012379 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 char_u *save_cpo;
12381 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012382 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012383 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012384 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012385 list_T *l = NULL;
12386 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012387 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012388 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389
12390 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12391 save_cpo = p_cpo;
12392 p_cpo = (char_u *)"";
12393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012394 rettv->vval.v_number = -1;
12395 if (type == 3)
12396 {
12397 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012398 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012399 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012400 }
12401 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403 rettv->v_type = VAR_STRING;
12404 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012407 if (argvars[0].v_type == VAR_LIST)
12408 {
12409 if ((l = argvars[0].vval.v_list) == NULL)
12410 goto theend;
12411 li = l->lv_first;
12412 }
12413 else
12414 expr = str = get_tv_string(&argvars[0]);
12415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012416 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12417 if (pat == NULL)
12418 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012419
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012420 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012422 int error = FALSE;
12423
12424 start = get_tv_number_chk(&argvars[2], &error);
12425 if (error)
12426 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012427 if (l != NULL)
12428 {
12429 li = list_find(l, start);
12430 if (li == NULL)
12431 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012432 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012433 }
12434 else
12435 {
12436 if (start < 0)
12437 start = 0;
12438 if (start > (long)STRLEN(str))
12439 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012440 /* When "count" argument is there ignore matches before "start",
12441 * otherwise skip part of the string. Differs when pattern is "^"
12442 * or "\<". */
12443 if (argvars[3].v_type != VAR_UNKNOWN)
12444 startcol = start;
12445 else
12446 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012447 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012448
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012449 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012450 nth = get_tv_number_chk(&argvars[3], &error);
12451 if (error)
12452 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 }
12454
12455 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12456 if (regmatch.regprog != NULL)
12457 {
12458 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012459
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012460 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012461 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012462 if (l != NULL)
12463 {
12464 if (li == NULL)
12465 {
12466 match = FALSE;
12467 break;
12468 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012469 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012470 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012471 if (str == NULL)
12472 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012473 }
12474
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012475 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012476
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012477 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012478 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012479 if (l == NULL && !match)
12480 break;
12481
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012482 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012483 if (l != NULL)
12484 {
12485 li = li->li_next;
12486 ++idx;
12487 }
12488 else
12489 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012490#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012491 startcol = (colnr_T)(regmatch.startp[0]
12492 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012493#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012494 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012495#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012496 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012497 }
12498
12499 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012501 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012502 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012503 int i;
12504
12505 /* return list with matched string and submatches */
12506 for (i = 0; i < NSUBEXP; ++i)
12507 {
12508 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012509 {
12510 if (list_append_string(rettv->vval.v_list,
12511 (char_u *)"", 0) == FAIL)
12512 break;
12513 }
12514 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012515 regmatch.startp[i],
12516 (int)(regmatch.endp[i] - regmatch.startp[i]))
12517 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012518 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012519 }
12520 }
12521 else if (type == 2)
12522 {
12523 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012524 if (l != NULL)
12525 copy_tv(&li->li_tv, rettv);
12526 else
12527 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012529 }
12530 else if (l != NULL)
12531 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 else
12533 {
12534 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536 (varnumber_T)(regmatch.startp[0] - str);
12537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012538 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012540 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541 }
12542 }
12543 vim_free(regmatch.regprog);
12544 }
12545
12546theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012547 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548 p_cpo = save_cpo;
12549}
12550
12551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012552 * "match()" function
12553 */
12554 static void
12555f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 typval_T *argvars;
12557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558{
12559 find_some_match(argvars, rettv, 1);
12560}
12561
12562/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012563 * "matchadd()" function
12564 */
12565 static void
12566f_matchadd(argvars, rettv)
12567 typval_T *argvars;
12568 typval_T *rettv;
12569{
12570#ifdef FEAT_SEARCH_EXTRA
12571 char_u buf[NUMBUFLEN];
12572 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12573 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12574 int prio = 10; /* default priority */
12575 int id = -1;
12576 int error = FALSE;
12577
12578 rettv->vval.v_number = -1;
12579
12580 if (grp == NULL || pat == NULL)
12581 return;
12582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012583 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012584 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012585 if (argvars[3].v_type != VAR_UNKNOWN)
12586 id = get_tv_number_chk(&argvars[3], &error);
12587 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012588 if (error == TRUE)
12589 return;
12590 if (id >= 1 && id <= 3)
12591 {
12592 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12593 return;
12594 }
12595
12596 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12597#endif
12598}
12599
12600/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012601 * "matcharg()" function
12602 */
12603 static void
12604f_matcharg(argvars, rettv)
12605 typval_T *argvars;
12606 typval_T *rettv;
12607{
12608 if (rettv_list_alloc(rettv) == OK)
12609 {
12610#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012611 int id = get_tv_number(&argvars[0]);
12612 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012613
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012614 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012615 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012616 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12617 {
12618 list_append_string(rettv->vval.v_list,
12619 syn_id2name(m->hlg_id), -1);
12620 list_append_string(rettv->vval.v_list, m->pattern, -1);
12621 }
12622 else
12623 {
12624 list_append_string(rettv->vval.v_list, NUL, -1);
12625 list_append_string(rettv->vval.v_list, NUL, -1);
12626 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012627 }
12628#endif
12629 }
12630}
12631
12632/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012633 * "matchdelete()" function
12634 */
12635 static void
12636f_matchdelete(argvars, rettv)
12637 typval_T *argvars;
12638 typval_T *rettv;
12639{
12640#ifdef FEAT_SEARCH_EXTRA
12641 rettv->vval.v_number = match_delete(curwin,
12642 (int)get_tv_number(&argvars[0]), TRUE);
12643#endif
12644}
12645
12646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012647 * "matchend()" function
12648 */
12649 static void
12650f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012651 typval_T *argvars;
12652 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012653{
12654 find_some_match(argvars, rettv, 0);
12655}
12656
12657/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012658 * "matchlist()" function
12659 */
12660 static void
12661f_matchlist(argvars, rettv)
12662 typval_T *argvars;
12663 typval_T *rettv;
12664{
12665 find_some_match(argvars, rettv, 3);
12666}
12667
12668/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012669 * "matchstr()" function
12670 */
12671 static void
12672f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012673 typval_T *argvars;
12674 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012675{
12676 find_some_match(argvars, rettv, 2);
12677}
12678
Bram Moolenaar33570922005-01-25 22:26:29 +000012679static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012680
12681 static void
12682max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012683 typval_T *argvars;
12684 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012685 int domax;
12686{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012687 long n = 0;
12688 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012690
12691 if (argvars[0].v_type == VAR_LIST)
12692 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 list_T *l;
12694 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012695
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012696 l = argvars[0].vval.v_list;
12697 if (l != NULL)
12698 {
12699 li = l->lv_first;
12700 if (li != NULL)
12701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012702 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012703 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012704 {
12705 li = li->li_next;
12706 if (li == NULL)
12707 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012708 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012709 if (domax ? i > n : i < n)
12710 n = i;
12711 }
12712 }
12713 }
12714 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012715 else if (argvars[0].v_type == VAR_DICT)
12716 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012717 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012718 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012719 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012720 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012721
12722 d = argvars[0].vval.v_dict;
12723 if (d != NULL)
12724 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012725 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012726 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012727 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012728 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012729 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012730 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012732 if (first)
12733 {
12734 n = i;
12735 first = FALSE;
12736 }
12737 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012738 n = i;
12739 }
12740 }
12741 }
12742 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012743 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012744 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012746}
12747
12748/*
12749 * "max()" function
12750 */
12751 static void
12752f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012753 typval_T *argvars;
12754 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012755{
12756 max_min(argvars, rettv, TRUE);
12757}
12758
12759/*
12760 * "min()" function
12761 */
12762 static void
12763f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *argvars;
12765 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012766{
12767 max_min(argvars, rettv, FALSE);
12768}
12769
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012770static int mkdir_recurse __ARGS((char_u *dir, int prot));
12771
12772/*
12773 * Create the directory in which "dir" is located, and higher levels when
12774 * needed.
12775 */
12776 static int
12777mkdir_recurse(dir, prot)
12778 char_u *dir;
12779 int prot;
12780{
12781 char_u *p;
12782 char_u *updir;
12783 int r = FAIL;
12784
12785 /* Get end of directory name in "dir".
12786 * We're done when it's "/" or "c:/". */
12787 p = gettail_sep(dir);
12788 if (p <= get_past_head(dir))
12789 return OK;
12790
12791 /* If the directory exists we're done. Otherwise: create it.*/
12792 updir = vim_strnsave(dir, (int)(p - dir));
12793 if (updir == NULL)
12794 return FAIL;
12795 if (mch_isdir(updir))
12796 r = OK;
12797 else if (mkdir_recurse(updir, prot) == OK)
12798 r = vim_mkdir_emsg(updir, prot);
12799 vim_free(updir);
12800 return r;
12801}
12802
12803#ifdef vim_mkdir
12804/*
12805 * "mkdir()" function
12806 */
12807 static void
12808f_mkdir(argvars, rettv)
12809 typval_T *argvars;
12810 typval_T *rettv;
12811{
12812 char_u *dir;
12813 char_u buf[NUMBUFLEN];
12814 int prot = 0755;
12815
12816 rettv->vval.v_number = FAIL;
12817 if (check_restricted() || check_secure())
12818 return;
12819
12820 dir = get_tv_string_buf(&argvars[0], buf);
12821 if (argvars[1].v_type != VAR_UNKNOWN)
12822 {
12823 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 prot = get_tv_number_chk(&argvars[2], NULL);
12825 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012826 mkdir_recurse(dir, prot);
12827 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012828 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012829}
12830#endif
12831
Bram Moolenaar0d660222005-01-07 21:51:51 +000012832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 * "mode()" function
12834 */
12835/*ARGSUSED*/
12836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 typval_T *argvars;
12839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840{
12841 char_u buf[2];
12842
12843#ifdef FEAT_VISUAL
12844 if (VIsual_active)
12845 {
12846 if (VIsual_select)
12847 buf[0] = VIsual_mode + 's' - 'v';
12848 else
12849 buf[0] = VIsual_mode;
12850 }
12851 else
12852#endif
12853 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12854 buf[0] = 'r';
12855 else if (State & INSERT)
12856 {
12857 if (State & REPLACE_FLAG)
12858 buf[0] = 'R';
12859 else
12860 buf[0] = 'i';
12861 }
12862 else if (State & CMDLINE)
12863 buf[0] = 'c';
12864 else
12865 buf[0] = 'n';
12866
12867 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 rettv->vval.v_string = vim_strsave(buf);
12869 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870}
12871
12872/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012873 * "nextnonblank()" function
12874 */
12875 static void
12876f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 typval_T *argvars;
12878 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012879{
12880 linenr_T lnum;
12881
12882 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012884 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012885 {
12886 lnum = 0;
12887 break;
12888 }
12889 if (*skipwhite(ml_get(lnum)) != NUL)
12890 break;
12891 }
12892 rettv->vval.v_number = lnum;
12893}
12894
12895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 * "nr2char()" function
12897 */
12898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012900 typval_T *argvars;
12901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902{
12903 char_u buf[NUMBUFLEN];
12904
12905#ifdef FEAT_MBYTE
12906 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 else
12909#endif
12910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 buf[1] = NUL;
12913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 rettv->v_type = VAR_STRING;
12915 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012916}
12917
12918/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012919 * "pathshorten()" function
12920 */
12921 static void
12922f_pathshorten(argvars, rettv)
12923 typval_T *argvars;
12924 typval_T *rettv;
12925{
12926 char_u *p;
12927
12928 rettv->v_type = VAR_STRING;
12929 p = get_tv_string_chk(&argvars[0]);
12930 if (p == NULL)
12931 rettv->vval.v_string = NULL;
12932 else
12933 {
12934 p = vim_strsave(p);
12935 rettv->vval.v_string = p;
12936 if (p != NULL)
12937 shorten_dir(p);
12938 }
12939}
12940
12941/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012942 * "prevnonblank()" function
12943 */
12944 static void
12945f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012946 typval_T *argvars;
12947 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012948{
12949 linenr_T lnum;
12950
12951 lnum = get_tv_lnum(argvars);
12952 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12953 lnum = 0;
12954 else
12955 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12956 --lnum;
12957 rettv->vval.v_number = lnum;
12958}
12959
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012960#ifdef HAVE_STDARG_H
12961/* This dummy va_list is here because:
12962 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12963 * - locally in the function results in a "used before set" warning
12964 * - using va_start() to initialize it gives "function with fixed args" error */
12965static va_list ap;
12966#endif
12967
Bram Moolenaar8c711452005-01-14 21:53:12 +000012968/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012969 * "printf()" function
12970 */
12971 static void
12972f_printf(argvars, rettv)
12973 typval_T *argvars;
12974 typval_T *rettv;
12975{
12976 rettv->v_type = VAR_STRING;
12977 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012978#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012979 {
12980 char_u buf[NUMBUFLEN];
12981 int len;
12982 char_u *s;
12983 int saved_did_emsg = did_emsg;
12984 char *fmt;
12985
12986 /* Get the required length, allocate the buffer and do it for real. */
12987 did_emsg = FALSE;
12988 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012989 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012990 if (!did_emsg)
12991 {
12992 s = alloc(len + 1);
12993 if (s != NULL)
12994 {
12995 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012996 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012997 }
12998 }
12999 did_emsg |= saved_did_emsg;
13000 }
13001#endif
13002}
13003
13004/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013005 * "pumvisible()" function
13006 */
13007/*ARGSUSED*/
13008 static void
13009f_pumvisible(argvars, rettv)
13010 typval_T *argvars;
13011 typval_T *rettv;
13012{
13013 rettv->vval.v_number = 0;
13014#ifdef FEAT_INS_EXPAND
13015 if (pum_visible())
13016 rettv->vval.v_number = 1;
13017#endif
13018}
13019
13020/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013021 * "range()" function
13022 */
13023 static void
13024f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013025 typval_T *argvars;
13026 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013027{
13028 long start;
13029 long end;
13030 long stride = 1;
13031 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013034 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013035 if (argvars[1].v_type == VAR_UNKNOWN)
13036 {
13037 end = start - 1;
13038 start = 0;
13039 }
13040 else
13041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013042 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013043 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013044 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013045 }
13046
13047 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013048 if (error)
13049 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013050 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013051 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013052 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013053 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013054 else
13055 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013056 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013057 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013058 if (list_append_number(rettv->vval.v_list,
13059 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013060 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013061 }
13062}
13063
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013064/*
13065 * "readfile()" function
13066 */
13067 static void
13068f_readfile(argvars, rettv)
13069 typval_T *argvars;
13070 typval_T *rettv;
13071{
13072 int binary = FALSE;
13073 char_u *fname;
13074 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013075 listitem_T *li;
13076#define FREAD_SIZE 200 /* optimized for text lines */
13077 char_u buf[FREAD_SIZE];
13078 int readlen; /* size of last fread() */
13079 int buflen; /* nr of valid chars in buf[] */
13080 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13081 int tolist; /* first byte in buf[] still to be put in list */
13082 int chop; /* how many CR to chop off */
13083 char_u *prev = NULL; /* previously read bytes, if any */
13084 int prevlen = 0; /* length of "prev" if not NULL */
13085 char_u *s;
13086 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013087 long maxline = MAXLNUM;
13088 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013090 if (argvars[1].v_type != VAR_UNKNOWN)
13091 {
13092 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13093 binary = TRUE;
13094 if (argvars[2].v_type != VAR_UNKNOWN)
13095 maxline = get_tv_number(&argvars[2]);
13096 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013097
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013098 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013099 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013100
13101 /* Always open the file in binary mode, library functions have a mind of
13102 * their own about CR-LF conversion. */
13103 fname = get_tv_string(&argvars[0]);
13104 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13105 {
13106 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13107 return;
13108 }
13109
13110 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013111 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013112 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013113 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013114 buflen = filtd + readlen;
13115 tolist = 0;
13116 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13117 {
13118 if (buf[filtd] == '\n' || readlen <= 0)
13119 {
13120 /* Only when in binary mode add an empty list item when the
13121 * last line ends in a '\n'. */
13122 if (!binary && readlen == 0 && filtd == 0)
13123 break;
13124
13125 /* Found end-of-line or end-of-file: add a text line to the
13126 * list. */
13127 chop = 0;
13128 if (!binary)
13129 while (filtd - chop - 1 >= tolist
13130 && buf[filtd - chop - 1] == '\r')
13131 ++chop;
13132 len = filtd - tolist - chop;
13133 if (prev == NULL)
13134 s = vim_strnsave(buf + tolist, len);
13135 else
13136 {
13137 s = alloc((unsigned)(prevlen + len + 1));
13138 if (s != NULL)
13139 {
13140 mch_memmove(s, prev, prevlen);
13141 vim_free(prev);
13142 prev = NULL;
13143 mch_memmove(s + prevlen, buf + tolist, len);
13144 s[prevlen + len] = NUL;
13145 }
13146 }
13147 tolist = filtd + 1;
13148
13149 li = listitem_alloc();
13150 if (li == NULL)
13151 {
13152 vim_free(s);
13153 break;
13154 }
13155 li->li_tv.v_type = VAR_STRING;
13156 li->li_tv.v_lock = 0;
13157 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013158 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013159
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013160 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013161 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013162 if (readlen <= 0)
13163 break;
13164 }
13165 else if (buf[filtd] == NUL)
13166 buf[filtd] = '\n';
13167 }
13168 if (readlen <= 0)
13169 break;
13170
13171 if (tolist == 0)
13172 {
13173 /* "buf" is full, need to move text to an allocated buffer */
13174 if (prev == NULL)
13175 {
13176 prev = vim_strnsave(buf, buflen);
13177 prevlen = buflen;
13178 }
13179 else
13180 {
13181 s = alloc((unsigned)(prevlen + buflen));
13182 if (s != NULL)
13183 {
13184 mch_memmove(s, prev, prevlen);
13185 mch_memmove(s + prevlen, buf, buflen);
13186 vim_free(prev);
13187 prev = s;
13188 prevlen += buflen;
13189 }
13190 }
13191 filtd = 0;
13192 }
13193 else
13194 {
13195 mch_memmove(buf, buf + tolist, buflen - tolist);
13196 filtd -= tolist;
13197 }
13198 }
13199
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013200 /*
13201 * For a negative line count use only the lines at the end of the file,
13202 * free the rest.
13203 */
13204 if (maxline < 0)
13205 while (cnt > -maxline)
13206 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013207 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013208 --cnt;
13209 }
13210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013211 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013212 fclose(fd);
13213}
13214
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013215#if defined(FEAT_RELTIME)
13216static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13217
13218/*
13219 * Convert a List to proftime_T.
13220 * Return FAIL when there is something wrong.
13221 */
13222 static int
13223list2proftime(arg, tm)
13224 typval_T *arg;
13225 proftime_T *tm;
13226{
13227 long n1, n2;
13228 int error = FALSE;
13229
13230 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13231 || arg->vval.v_list->lv_len != 2)
13232 return FAIL;
13233 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13234 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13235# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013236 tm->HighPart = n1;
13237 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013238# else
13239 tm->tv_sec = n1;
13240 tm->tv_usec = n2;
13241# endif
13242 return error ? FAIL : OK;
13243}
13244#endif /* FEAT_RELTIME */
13245
13246/*
13247 * "reltime()" function
13248 */
13249 static void
13250f_reltime(argvars, rettv)
13251 typval_T *argvars;
13252 typval_T *rettv;
13253{
13254#ifdef FEAT_RELTIME
13255 proftime_T res;
13256 proftime_T start;
13257
13258 if (argvars[0].v_type == VAR_UNKNOWN)
13259 {
13260 /* No arguments: get current time. */
13261 profile_start(&res);
13262 }
13263 else if (argvars[1].v_type == VAR_UNKNOWN)
13264 {
13265 if (list2proftime(&argvars[0], &res) == FAIL)
13266 return;
13267 profile_end(&res);
13268 }
13269 else
13270 {
13271 /* Two arguments: compute the difference. */
13272 if (list2proftime(&argvars[0], &start) == FAIL
13273 || list2proftime(&argvars[1], &res) == FAIL)
13274 return;
13275 profile_sub(&res, &start);
13276 }
13277
13278 if (rettv_list_alloc(rettv) == OK)
13279 {
13280 long n1, n2;
13281
13282# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013283 n1 = res.HighPart;
13284 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013285# else
13286 n1 = res.tv_sec;
13287 n2 = res.tv_usec;
13288# endif
13289 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13290 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13291 }
13292#endif
13293}
13294
13295/*
13296 * "reltimestr()" function
13297 */
13298 static void
13299f_reltimestr(argvars, rettv)
13300 typval_T *argvars;
13301 typval_T *rettv;
13302{
13303#ifdef FEAT_RELTIME
13304 proftime_T tm;
13305#endif
13306
13307 rettv->v_type = VAR_STRING;
13308 rettv->vval.v_string = NULL;
13309#ifdef FEAT_RELTIME
13310 if (list2proftime(&argvars[0], &tm) == OK)
13311 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13312#endif
13313}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013314
Bram Moolenaar0d660222005-01-07 21:51:51 +000013315#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13316static void make_connection __ARGS((void));
13317static int check_connection __ARGS((void));
13318
13319 static void
13320make_connection()
13321{
13322 if (X_DISPLAY == NULL
13323# ifdef FEAT_GUI
13324 && !gui.in_use
13325# endif
13326 )
13327 {
13328 x_force_connect = TRUE;
13329 setup_term_clip();
13330 x_force_connect = FALSE;
13331 }
13332}
13333
13334 static int
13335check_connection()
13336{
13337 make_connection();
13338 if (X_DISPLAY == NULL)
13339 {
13340 EMSG(_("E240: No connection to Vim server"));
13341 return FAIL;
13342 }
13343 return OK;
13344}
13345#endif
13346
13347#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013348static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013349
13350 static void
13351remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013352 typval_T *argvars;
13353 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013354 int expr;
13355{
13356 char_u *server_name;
13357 char_u *keys;
13358 char_u *r = NULL;
13359 char_u buf[NUMBUFLEN];
13360# ifdef WIN32
13361 HWND w;
13362# else
13363 Window w;
13364# endif
13365
13366 if (check_restricted() || check_secure())
13367 return;
13368
13369# ifdef FEAT_X11
13370 if (check_connection() == FAIL)
13371 return;
13372# endif
13373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013374 server_name = get_tv_string_chk(&argvars[0]);
13375 if (server_name == NULL)
13376 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013377 keys = get_tv_string_buf(&argvars[1], buf);
13378# ifdef WIN32
13379 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13380# else
13381 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13382 < 0)
13383# endif
13384 {
13385 if (r != NULL)
13386 EMSG(r); /* sending worked but evaluation failed */
13387 else
13388 EMSG2(_("E241: Unable to send to %s"), server_name);
13389 return;
13390 }
13391
13392 rettv->vval.v_string = r;
13393
13394 if (argvars[2].v_type != VAR_UNKNOWN)
13395 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013396 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013397 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013398 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013399
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013400 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013401 v.di_tv.v_type = VAR_STRING;
13402 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 idvar = get_tv_string_chk(&argvars[2]);
13404 if (idvar != NULL)
13405 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013407 }
13408}
13409#endif
13410
13411/*
13412 * "remote_expr()" function
13413 */
13414/*ARGSUSED*/
13415 static void
13416f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 typval_T *argvars;
13418 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013419{
13420 rettv->v_type = VAR_STRING;
13421 rettv->vval.v_string = NULL;
13422#ifdef FEAT_CLIENTSERVER
13423 remote_common(argvars, rettv, TRUE);
13424#endif
13425}
13426
13427/*
13428 * "remote_foreground()" function
13429 */
13430/*ARGSUSED*/
13431 static void
13432f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013433 typval_T *argvars;
13434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013435{
13436 rettv->vval.v_number = 0;
13437#ifdef FEAT_CLIENTSERVER
13438# ifdef WIN32
13439 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 {
13441 char_u *server_name = get_tv_string_chk(&argvars[0]);
13442
13443 if (server_name != NULL)
13444 serverForeground(server_name);
13445 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013446# else
13447 /* Send a foreground() expression to the server. */
13448 argvars[1].v_type = VAR_STRING;
13449 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13450 argvars[2].v_type = VAR_UNKNOWN;
13451 remote_common(argvars, rettv, TRUE);
13452 vim_free(argvars[1].vval.v_string);
13453# endif
13454#endif
13455}
13456
13457/*ARGSUSED*/
13458 static void
13459f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013460 typval_T *argvars;
13461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013462{
13463#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013464 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013465 char_u *s = NULL;
13466# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013467 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013468# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013469 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013470
13471 if (check_restricted() || check_secure())
13472 {
13473 rettv->vval.v_number = -1;
13474 return;
13475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013476 serverid = get_tv_string_chk(&argvars[0]);
13477 if (serverid == NULL)
13478 {
13479 rettv->vval.v_number = -1;
13480 return; /* type error; errmsg already given */
13481 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013482# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013483 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013484 if (n == 0)
13485 rettv->vval.v_number = -1;
13486 else
13487 {
13488 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13489 rettv->vval.v_number = (s != NULL);
13490 }
13491# else
13492 rettv->vval.v_number = 0;
13493 if (check_connection() == FAIL)
13494 return;
13495
13496 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013498# endif
13499
13500 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013502 char_u *retvar;
13503
Bram Moolenaar33570922005-01-25 22:26:29 +000013504 v.di_tv.v_type = VAR_STRING;
13505 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 retvar = get_tv_string_chk(&argvars[1]);
13507 if (retvar != NULL)
13508 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013509 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013510 }
13511#else
13512 rettv->vval.v_number = -1;
13513#endif
13514}
13515
13516/*ARGSUSED*/
13517 static void
13518f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013519 typval_T *argvars;
13520 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013521{
13522 char_u *r = NULL;
13523
13524#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013525 char_u *serverid = get_tv_string_chk(&argvars[0]);
13526
13527 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013528 {
13529# ifdef WIN32
13530 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013531 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013532
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013533 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013534 if (n != 0)
13535 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13536 if (r == NULL)
13537# else
13538 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013539 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013540# endif
13541 EMSG(_("E277: Unable to read a server reply"));
13542 }
13543#endif
13544 rettv->v_type = VAR_STRING;
13545 rettv->vval.v_string = r;
13546}
13547
13548/*
13549 * "remote_send()" function
13550 */
13551/*ARGSUSED*/
13552 static void
13553f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013554 typval_T *argvars;
13555 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013556{
13557 rettv->v_type = VAR_STRING;
13558 rettv->vval.v_string = NULL;
13559#ifdef FEAT_CLIENTSERVER
13560 remote_common(argvars, rettv, FALSE);
13561#endif
13562}
13563
13564/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013565 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013566 */
13567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013571{
Bram Moolenaar33570922005-01-25 22:26:29 +000013572 list_T *l;
13573 listitem_T *item, *item2;
13574 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013575 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013576 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013577 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013578 dict_T *d;
13579 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013580
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013581 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013582 if (argvars[0].v_type == VAR_DICT)
13583 {
13584 if (argvars[2].v_type != VAR_UNKNOWN)
13585 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013586 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013587 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013589 key = get_tv_string_chk(&argvars[1]);
13590 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013592 di = dict_find(d, key, -1);
13593 if (di == NULL)
13594 EMSG2(_(e_dictkey), key);
13595 else
13596 {
13597 *rettv = di->di_tv;
13598 init_tv(&di->di_tv);
13599 dictitem_remove(d, di);
13600 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013602 }
13603 }
13604 else if (argvars[0].v_type != VAR_LIST)
13605 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013606 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013607 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013609 int error = FALSE;
13610
13611 idx = get_tv_number_chk(&argvars[1], &error);
13612 if (error)
13613 ; /* type error: do nothing, errmsg already given */
13614 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013615 EMSGN(_(e_listidx), idx);
13616 else
13617 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013618 if (argvars[2].v_type == VAR_UNKNOWN)
13619 {
13620 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013621 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013622 *rettv = item->li_tv;
13623 vim_free(item);
13624 }
13625 else
13626 {
13627 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 end = get_tv_number_chk(&argvars[2], &error);
13629 if (error)
13630 ; /* type error: do nothing */
13631 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013632 EMSGN(_(e_listidx), end);
13633 else
13634 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013635 int cnt = 0;
13636
13637 for (li = item; li != NULL; li = li->li_next)
13638 {
13639 ++cnt;
13640 if (li == item2)
13641 break;
13642 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013643 if (li == NULL) /* didn't find "item2" after "item" */
13644 EMSG(_(e_invrange));
13645 else
13646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013647 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013648 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013649 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013650 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013651 l->lv_first = item;
13652 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013653 item->li_prev = NULL;
13654 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013655 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013656 }
13657 }
13658 }
13659 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013660 }
13661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662}
13663
13664/*
13665 * "rename({from}, {to})" function
13666 */
13667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013669 typval_T *argvars;
13670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671{
13672 char_u buf[NUMBUFLEN];
13673
13674 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013675 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13678 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679}
13680
13681/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013682 * "repeat()" function
13683 */
13684/*ARGSUSED*/
13685 static void
13686f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013687 typval_T *argvars;
13688 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013689{
13690 char_u *p;
13691 int n;
13692 int slen;
13693 int len;
13694 char_u *r;
13695 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013696
13697 n = get_tv_number(&argvars[1]);
13698 if (argvars[0].v_type == VAR_LIST)
13699 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013700 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013701 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013702 if (list_extend(rettv->vval.v_list,
13703 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013704 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013705 }
13706 else
13707 {
13708 p = get_tv_string(&argvars[0]);
13709 rettv->v_type = VAR_STRING;
13710 rettv->vval.v_string = NULL;
13711
13712 slen = (int)STRLEN(p);
13713 len = slen * n;
13714 if (len <= 0)
13715 return;
13716
13717 r = alloc(len + 1);
13718 if (r != NULL)
13719 {
13720 for (i = 0; i < n; i++)
13721 mch_memmove(r + i * slen, p, (size_t)slen);
13722 r[len] = NUL;
13723 }
13724
13725 rettv->vval.v_string = r;
13726 }
13727}
13728
13729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 * "resolve()" function
13731 */
13732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 typval_T *argvars;
13735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736{
13737 char_u *p;
13738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740#ifdef FEAT_SHORTCUT
13741 {
13742 char_u *v = NULL;
13743
13744 v = mch_resolve_shortcut(p);
13745 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 }
13750#else
13751# ifdef HAVE_READLINK
13752 {
13753 char_u buf[MAXPATHL + 1];
13754 char_u *cpy;
13755 int len;
13756 char_u *remain = NULL;
13757 char_u *q;
13758 int is_relative_to_current = FALSE;
13759 int has_trailing_pathsep = FALSE;
13760 int limit = 100;
13761
13762 p = vim_strsave(p);
13763
13764 if (p[0] == '.' && (vim_ispathsep(p[1])
13765 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13766 is_relative_to_current = TRUE;
13767
13768 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013769 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 has_trailing_pathsep = TRUE;
13771
13772 q = getnextcomp(p);
13773 if (*q != NUL)
13774 {
13775 /* Separate the first path component in "p", and keep the
13776 * remainder (beginning with the path separator). */
13777 remain = vim_strsave(q - 1);
13778 q[-1] = NUL;
13779 }
13780
13781 for (;;)
13782 {
13783 for (;;)
13784 {
13785 len = readlink((char *)p, (char *)buf, MAXPATHL);
13786 if (len <= 0)
13787 break;
13788 buf[len] = NUL;
13789
13790 if (limit-- == 0)
13791 {
13792 vim_free(p);
13793 vim_free(remain);
13794 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 goto fail;
13797 }
13798
13799 /* Ensure that the result will have a trailing path separator
13800 * if the argument has one. */
13801 if (remain == NULL && has_trailing_pathsep)
13802 add_pathsep(buf);
13803
13804 /* Separate the first path component in the link value and
13805 * concatenate the remainders. */
13806 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13807 if (*q != NUL)
13808 {
13809 if (remain == NULL)
13810 remain = vim_strsave(q - 1);
13811 else
13812 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013813 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 if (cpy != NULL)
13815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 vim_free(remain);
13817 remain = cpy;
13818 }
13819 }
13820 q[-1] = NUL;
13821 }
13822
13823 q = gettail(p);
13824 if (q > p && *q == NUL)
13825 {
13826 /* Ignore trailing path separator. */
13827 q[-1] = NUL;
13828 q = gettail(p);
13829 }
13830 if (q > p && !mch_isFullName(buf))
13831 {
13832 /* symlink is relative to directory of argument */
13833 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13834 if (cpy != NULL)
13835 {
13836 STRCPY(cpy, p);
13837 STRCPY(gettail(cpy), buf);
13838 vim_free(p);
13839 p = cpy;
13840 }
13841 }
13842 else
13843 {
13844 vim_free(p);
13845 p = vim_strsave(buf);
13846 }
13847 }
13848
13849 if (remain == NULL)
13850 break;
13851
13852 /* Append the first path component of "remain" to "p". */
13853 q = getnextcomp(remain + 1);
13854 len = q - remain - (*q != NUL);
13855 cpy = vim_strnsave(p, STRLEN(p) + len);
13856 if (cpy != NULL)
13857 {
13858 STRNCAT(cpy, remain, len);
13859 vim_free(p);
13860 p = cpy;
13861 }
13862 /* Shorten "remain". */
13863 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013864 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 else
13866 {
13867 vim_free(remain);
13868 remain = NULL;
13869 }
13870 }
13871
13872 /* If the result is a relative path name, make it explicitly relative to
13873 * the current directory if and only if the argument had this form. */
13874 if (!vim_ispathsep(*p))
13875 {
13876 if (is_relative_to_current
13877 && *p != NUL
13878 && !(p[0] == '.'
13879 && (p[1] == NUL
13880 || vim_ispathsep(p[1])
13881 || (p[1] == '.'
13882 && (p[2] == NUL
13883 || vim_ispathsep(p[2]))))))
13884 {
13885 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013886 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 if (cpy != NULL)
13888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 vim_free(p);
13890 p = cpy;
13891 }
13892 }
13893 else if (!is_relative_to_current)
13894 {
13895 /* Strip leading "./". */
13896 q = p;
13897 while (q[0] == '.' && vim_ispathsep(q[1]))
13898 q += 2;
13899 if (q > p)
13900 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13901 }
13902 }
13903
13904 /* Ensure that the result will have no trailing path separator
13905 * if the argument had none. But keep "/" or "//". */
13906 if (!has_trailing_pathsep)
13907 {
13908 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013909 if (after_pathsep(p, q))
13910 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 }
13912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 }
13915# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013916 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917# endif
13918#endif
13919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921
13922#ifdef HAVE_READLINK
13923fail:
13924#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013925 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926}
13927
13928/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013929 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 */
13931 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013932f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013933 typval_T *argvars;
13934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935{
Bram Moolenaar33570922005-01-25 22:26:29 +000013936 list_T *l;
13937 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938
Bram Moolenaar0d660222005-01-07 21:51:51 +000013939 rettv->vval.v_number = 0;
13940 if (argvars[0].v_type != VAR_LIST)
13941 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013942 else if ((l = argvars[0].vval.v_list) != NULL
13943 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013944 {
13945 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013946 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013947 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013948 while (li != NULL)
13949 {
13950 ni = li->li_prev;
13951 list_append(l, li);
13952 li = ni;
13953 }
13954 rettv->vval.v_list = l;
13955 rettv->v_type = VAR_LIST;
13956 ++l->lv_refcount;
13957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958}
13959
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013960#define SP_NOMOVE 0x01 /* don't move cursor */
13961#define SP_REPEAT 0x02 /* repeat to find outer pair */
13962#define SP_RETCOUNT 0x04 /* return matchcount */
13963#define SP_SETPCMARK 0x08 /* set previous context mark */
13964#define SP_START 0x10 /* accept match at start position */
13965#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13966#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013967
Bram Moolenaar33570922005-01-25 22:26:29 +000013968static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013969
13970/*
13971 * Get flags for a search function.
13972 * Possibly sets "p_ws".
13973 * Returns BACKWARD, FORWARD or zero (for an error).
13974 */
13975 static int
13976get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013977 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013978 int *flagsp;
13979{
13980 int dir = FORWARD;
13981 char_u *flags;
13982 char_u nbuf[NUMBUFLEN];
13983 int mask;
13984
13985 if (varp->v_type != VAR_UNKNOWN)
13986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 flags = get_tv_string_buf_chk(varp, nbuf);
13988 if (flags == NULL)
13989 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013990 while (*flags != NUL)
13991 {
13992 switch (*flags)
13993 {
13994 case 'b': dir = BACKWARD; break;
13995 case 'w': p_ws = TRUE; break;
13996 case 'W': p_ws = FALSE; break;
13997 default: mask = 0;
13998 if (flagsp != NULL)
13999 switch (*flags)
14000 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014001 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014002 case 'e': mask = SP_END; break;
14003 case 'm': mask = SP_RETCOUNT; break;
14004 case 'n': mask = SP_NOMOVE; break;
14005 case 'p': mask = SP_SUBPAT; break;
14006 case 'r': mask = SP_REPEAT; break;
14007 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014008 }
14009 if (mask == 0)
14010 {
14011 EMSG2(_(e_invarg2), flags);
14012 dir = 0;
14013 }
14014 else
14015 *flagsp |= mask;
14016 }
14017 if (dir == 0)
14018 break;
14019 ++flags;
14020 }
14021 }
14022 return dir;
14023}
14024
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014026 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014028 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014029search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014030 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014031 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014032 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014034 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 char_u *pat;
14036 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014037 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 int save_p_ws = p_ws;
14039 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014040 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014041 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014042 proftime_T tm;
14043#ifdef FEAT_RELTIME
14044 long time_limit = 0;
14045#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014046 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014047 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014049 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014050 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014051 if (dir == 0)
14052 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014053 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014054 if (flags & SP_START)
14055 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014056 if (flags & SP_END)
14057 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014058
Bram Moolenaar76929292008-01-06 19:07:36 +000014059 /* Optional arguments: line number to stop searching and timeout. */
14060 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014061 {
14062 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14063 if (lnum_stop < 0)
14064 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014065#ifdef FEAT_RELTIME
14066 if (argvars[3].v_type != VAR_UNKNOWN)
14067 {
14068 time_limit = get_tv_number_chk(&argvars[3], NULL);
14069 if (time_limit < 0)
14070 goto theend;
14071 }
14072#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014073 }
14074
Bram Moolenaar76929292008-01-06 19:07:36 +000014075#ifdef FEAT_RELTIME
14076 /* Set the time limit, if there is one. */
14077 profile_setlimit(time_limit, &tm);
14078#endif
14079
Bram Moolenaar231334e2005-07-25 20:46:57 +000014080 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014081 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014082 * Check to make sure only those flags are set.
14083 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14084 * flags cannot be set. Check for that condition also.
14085 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014086 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014087 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014089 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014090 goto theend;
14091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014093 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014094 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014095 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014096 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014098 if (flags & SP_SUBPAT)
14099 retval = subpatnum;
14100 else
14101 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014102 if (flags & SP_SETPCMARK)
14103 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014105 if (match_pos != NULL)
14106 {
14107 /* Store the match cursor position */
14108 match_pos->lnum = pos.lnum;
14109 match_pos->col = pos.col + 1;
14110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111 /* "/$" will put the cursor after the end of the line, may need to
14112 * correct that here */
14113 check_cursor();
14114 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014115
14116 /* If 'n' flag is used: restore cursor position. */
14117 if (flags & SP_NOMOVE)
14118 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014119 else
14120 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014121theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014123
14124 return retval;
14125}
14126
14127/*
14128 * "search()" function
14129 */
14130 static void
14131f_search(argvars, rettv)
14132 typval_T *argvars;
14133 typval_T *rettv;
14134{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014135 int flags = 0;
14136
14137 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138}
14139
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014141 * "searchdecl()" function
14142 */
14143 static void
14144f_searchdecl(argvars, rettv)
14145 typval_T *argvars;
14146 typval_T *rettv;
14147{
14148 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014149 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014150 int error = FALSE;
14151 char_u *name;
14152
14153 rettv->vval.v_number = 1; /* default: FAIL */
14154
14155 name = get_tv_string_chk(&argvars[0]);
14156 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014157 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014158 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014159 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14160 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14161 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014162 if (!error && name != NULL)
14163 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014164 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014165}
14166
14167/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014168 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014170 static int
14171searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014172 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014173 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174{
14175 char_u *spat, *mpat, *epat;
14176 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 int dir;
14179 int flags = 0;
14180 char_u nbuf1[NUMBUFLEN];
14181 char_u nbuf2[NUMBUFLEN];
14182 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014183 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014184 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014185 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188 spat = get_tv_string_chk(&argvars[0]);
14189 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14190 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14191 if (spat == NULL || mpat == NULL || epat == NULL)
14192 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 /* Handle the optional fourth argument: flags */
14195 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014196 if (dir == 0)
14197 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014198
14199 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014200 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14201 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014202 if ((flags & (SP_END | SP_SUBPAT)) != 0
14203 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014204 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014205 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014206 goto theend;
14207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014209 /* Using 'r' implies 'W', otherwise it doesn't work. */
14210 if (flags & SP_REPEAT)
14211 p_ws = FALSE;
14212
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014213 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014214 if (argvars[3].v_type == VAR_UNKNOWN
14215 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 skip = (char_u *)"";
14217 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014219 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014220 if (argvars[5].v_type != VAR_UNKNOWN)
14221 {
14222 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14223 if (lnum_stop < 0)
14224 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014225#ifdef FEAT_RELTIME
14226 if (argvars[6].v_type != VAR_UNKNOWN)
14227 {
14228 time_limit = get_tv_number_chk(&argvars[6], NULL);
14229 if (time_limit < 0)
14230 goto theend;
14231 }
14232#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014233 }
14234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 if (skip == NULL)
14236 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014238 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014239 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014240
14241theend:
14242 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014243
14244 return retval;
14245}
14246
14247/*
14248 * "searchpair()" function
14249 */
14250 static void
14251f_searchpair(argvars, rettv)
14252 typval_T *argvars;
14253 typval_T *rettv;
14254{
14255 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14256}
14257
14258/*
14259 * "searchpairpos()" function
14260 */
14261 static void
14262f_searchpairpos(argvars, rettv)
14263 typval_T *argvars;
14264 typval_T *rettv;
14265{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014266 pos_T match_pos;
14267 int lnum = 0;
14268 int col = 0;
14269
14270 rettv->vval.v_number = 0;
14271
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014272 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014273 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014274
14275 if (searchpair_cmn(argvars, &match_pos) > 0)
14276 {
14277 lnum = match_pos.lnum;
14278 col = match_pos.col;
14279 }
14280
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014281 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14282 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014283}
14284
14285/*
14286 * Search for a start/middle/end thing.
14287 * Used by searchpair(), see its documentation for the details.
14288 * Returns 0 or -1 for no match,
14289 */
14290 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014291do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14292 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014293 char_u *spat; /* start pattern */
14294 char_u *mpat; /* middle pattern */
14295 char_u *epat; /* end pattern */
14296 int dir; /* BACKWARD or FORWARD */
14297 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014298 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014299 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014300 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014301 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014302{
14303 char_u *save_cpo;
14304 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14305 long retval = 0;
14306 pos_T pos;
14307 pos_T firstpos;
14308 pos_T foundpos;
14309 pos_T save_cursor;
14310 pos_T save_pos;
14311 int n;
14312 int r;
14313 int nest = 1;
14314 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014315 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014316 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014317
14318 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14319 save_cpo = p_cpo;
14320 p_cpo = (char_u *)"";
14321
Bram Moolenaar76929292008-01-06 19:07:36 +000014322#ifdef FEAT_RELTIME
14323 /* Set the time limit, if there is one. */
14324 profile_setlimit(time_limit, &tm);
14325#endif
14326
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014327 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14328 * start/middle/end (pat3, for the top pair). */
14329 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14330 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14331 if (pat2 == NULL || pat3 == NULL)
14332 goto theend;
14333 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14334 if (*mpat == NUL)
14335 STRCPY(pat3, pat2);
14336 else
14337 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14338 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014339 if (flags & SP_START)
14340 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014341
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 save_cursor = curwin->w_cursor;
14343 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014344 clearpos(&firstpos);
14345 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 pat = pat3;
14347 for (;;)
14348 {
14349 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014350 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14352 /* didn't find it or found the first match again: FAIL */
14353 break;
14354
14355 if (firstpos.lnum == 0)
14356 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014357 if (equalpos(pos, foundpos))
14358 {
14359 /* Found the same position again. Can happen with a pattern that
14360 * has "\zs" at the end and searching backwards. Advance one
14361 * character and try again. */
14362 if (dir == BACKWARD)
14363 decl(&pos);
14364 else
14365 incl(&pos);
14366 }
14367 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014369 /* clear the start flag to avoid getting stuck here */
14370 options &= ~SEARCH_START;
14371
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 /* If the skip pattern matches, ignore this match. */
14373 if (*skip != NUL)
14374 {
14375 save_pos = curwin->w_cursor;
14376 curwin->w_cursor = pos;
14377 r = eval_to_bool(skip, &err, NULL, FALSE);
14378 curwin->w_cursor = save_pos;
14379 if (err)
14380 {
14381 /* Evaluating {skip} caused an error, break here. */
14382 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014383 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 break;
14385 }
14386 if (r)
14387 continue;
14388 }
14389
14390 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14391 {
14392 /* Found end when searching backwards or start when searching
14393 * forward: nested pair. */
14394 ++nest;
14395 pat = pat2; /* nested, don't search for middle */
14396 }
14397 else
14398 {
14399 /* Found end when searching forward or start when searching
14400 * backward: end of (nested) pair; or found middle in outer pair. */
14401 if (--nest == 1)
14402 pat = pat3; /* outer level, search for middle */
14403 }
14404
14405 if (nest == 0)
14406 {
14407 /* Found the match: return matchcount or line number. */
14408 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014409 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014411 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014412 if (flags & SP_SETPCMARK)
14413 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 curwin->w_cursor = pos;
14415 if (!(flags & SP_REPEAT))
14416 break;
14417 nest = 1; /* search for next unmatched */
14418 }
14419 }
14420
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014421 if (match_pos != NULL)
14422 {
14423 /* Store the match cursor position */
14424 match_pos->lnum = curwin->w_cursor.lnum;
14425 match_pos->col = curwin->w_cursor.col + 1;
14426 }
14427
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014429 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 curwin->w_cursor = save_cursor;
14431
14432theend:
14433 vim_free(pat2);
14434 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014436
14437 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438}
14439
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014440/*
14441 * "searchpos()" function
14442 */
14443 static void
14444f_searchpos(argvars, rettv)
14445 typval_T *argvars;
14446 typval_T *rettv;
14447{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014448 pos_T match_pos;
14449 int lnum = 0;
14450 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014451 int n;
14452 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014453
14454 rettv->vval.v_number = 0;
14455
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014456 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014457 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014458
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014459 n = search_cmn(argvars, &match_pos, &flags);
14460 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014461 {
14462 lnum = match_pos.lnum;
14463 col = match_pos.col;
14464 }
14465
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014466 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14467 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014468 if (flags & SP_SUBPAT)
14469 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014470}
14471
14472
Bram Moolenaar0d660222005-01-07 21:51:51 +000014473/*ARGSUSED*/
14474 static void
14475f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014476 typval_T *argvars;
14477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014479#ifdef FEAT_CLIENTSERVER
14480 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014481 char_u *server = get_tv_string_chk(&argvars[0]);
14482 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483
Bram Moolenaar0d660222005-01-07 21:51:51 +000014484 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 if (server == NULL || reply == NULL)
14486 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014487 if (check_restricted() || check_secure())
14488 return;
14489# ifdef FEAT_X11
14490 if (check_connection() == FAIL)
14491 return;
14492# endif
14493
14494 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496 EMSG(_("E258: Unable to send to client"));
14497 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014499 rettv->vval.v_number = 0;
14500#else
14501 rettv->vval.v_number = -1;
14502#endif
14503}
14504
14505/*ARGSUSED*/
14506 static void
14507f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014508 typval_T *argvars;
14509 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014510{
14511 char_u *r = NULL;
14512
14513#ifdef FEAT_CLIENTSERVER
14514# ifdef WIN32
14515 r = serverGetVimNames();
14516# else
14517 make_connection();
14518 if (X_DISPLAY != NULL)
14519 r = serverGetVimNames(X_DISPLAY);
14520# endif
14521#endif
14522 rettv->v_type = VAR_STRING;
14523 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524}
14525
14526/*
14527 * "setbufvar()" function
14528 */
14529/*ARGSUSED*/
14530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014531f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014532 typval_T *argvars;
14533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534{
14535 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014538 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 char_u nbuf[NUMBUFLEN];
14540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014541 rettv->vval.v_number = 0;
14542
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 if (check_restricted() || check_secure())
14544 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014545 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14546 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 varp = &argvars[2];
14549
14550 if (buf != NULL && varname != NULL && varp != NULL)
14551 {
14552 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554
14555 if (*varname == '&')
14556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557 long numval;
14558 char_u *strval;
14559 int error = FALSE;
14560
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 numval = get_tv_number_chk(varp, &error);
14563 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014564 if (!error && strval != NULL)
14565 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 }
14567 else
14568 {
14569 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14570 if (bufvarname != NULL)
14571 {
14572 STRCPY(bufvarname, "b:");
14573 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014574 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 vim_free(bufvarname);
14576 }
14577 }
14578
14579 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582}
14583
14584/*
14585 * "setcmdpos()" function
14586 */
14587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014589 typval_T *argvars;
14590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014592 int pos = (int)get_tv_number(&argvars[0]) - 1;
14593
14594 if (pos >= 0)
14595 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596}
14597
14598/*
14599 * "setline()" function
14600 */
14601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014602f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014603 typval_T *argvars;
14604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605{
14606 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014607 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014608 list_T *l = NULL;
14609 listitem_T *li = NULL;
14610 long added = 0;
14611 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014613 lnum = get_tv_lnum(&argvars[0]);
14614 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014616 l = argvars[1].vval.v_list;
14617 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014619 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014620 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014621
14622 rettv->vval.v_number = 0; /* OK */
14623 for (;;)
14624 {
14625 if (l != NULL)
14626 {
14627 /* list argument, get next string */
14628 if (li == NULL)
14629 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014631 li = li->li_next;
14632 }
14633
14634 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014635 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014636 break;
14637 if (lnum <= curbuf->b_ml.ml_line_count)
14638 {
14639 /* existing line, replace it */
14640 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14641 {
14642 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014643 if (lnum == curwin->w_cursor.lnum)
14644 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014645 rettv->vval.v_number = 0; /* OK */
14646 }
14647 }
14648 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14649 {
14650 /* lnum is one past the last line, append the line */
14651 ++added;
14652 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14653 rettv->vval.v_number = 0; /* OK */
14654 }
14655
14656 if (l == NULL) /* only one string argument */
14657 break;
14658 ++lnum;
14659 }
14660
14661 if (added > 0)
14662 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663}
14664
14665/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014666 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014667 */
14668/*ARGSUSED*/
14669 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014670set_qf_ll_list(wp, list_arg, action_arg, rettv)
14671 win_T *wp;
14672 typval_T *list_arg;
14673 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014674 typval_T *rettv;
14675{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014676#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014677 char_u *act;
14678 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014679#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014680
Bram Moolenaar2641f772005-03-25 21:58:17 +000014681 rettv->vval.v_number = -1;
14682
14683#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014684 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014685 EMSG(_(e_listreq));
14686 else
14687 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014688 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014689
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014690 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014691 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014692 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014693 if (act == NULL)
14694 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014695 if (*act == 'a' || *act == 'r')
14696 action = *act;
14697 }
14698
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014699 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014700 rettv->vval.v_number = 0;
14701 }
14702#endif
14703}
14704
14705/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014706 * "setloclist()" function
14707 */
14708/*ARGSUSED*/
14709 static void
14710f_setloclist(argvars, rettv)
14711 typval_T *argvars;
14712 typval_T *rettv;
14713{
14714 win_T *win;
14715
14716 rettv->vval.v_number = -1;
14717
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014718 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014719 if (win != NULL)
14720 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14721}
14722
14723/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014724 * "setmatches()" function
14725 */
14726 static void
14727f_setmatches(argvars, rettv)
14728 typval_T *argvars;
14729 typval_T *rettv;
14730{
14731#ifdef FEAT_SEARCH_EXTRA
14732 list_T *l;
14733 listitem_T *li;
14734 dict_T *d;
14735
14736 rettv->vval.v_number = -1;
14737 if (argvars[0].v_type != VAR_LIST)
14738 {
14739 EMSG(_(e_listreq));
14740 return;
14741 }
14742 if ((l = argvars[0].vval.v_list) != NULL)
14743 {
14744
14745 /* To some extent make sure that we are dealing with a list from
14746 * "getmatches()". */
14747 li = l->lv_first;
14748 while (li != NULL)
14749 {
14750 if (li->li_tv.v_type != VAR_DICT
14751 || (d = li->li_tv.vval.v_dict) == NULL)
14752 {
14753 EMSG(_(e_invarg));
14754 return;
14755 }
14756 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14757 && dict_find(d, (char_u *)"pattern", -1) != NULL
14758 && dict_find(d, (char_u *)"priority", -1) != NULL
14759 && dict_find(d, (char_u *)"id", -1) != NULL))
14760 {
14761 EMSG(_(e_invarg));
14762 return;
14763 }
14764 li = li->li_next;
14765 }
14766
14767 clear_matches(curwin);
14768 li = l->lv_first;
14769 while (li != NULL)
14770 {
14771 d = li->li_tv.vval.v_dict;
14772 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14773 get_dict_string(d, (char_u *)"pattern", FALSE),
14774 (int)get_dict_number(d, (char_u *)"priority"),
14775 (int)get_dict_number(d, (char_u *)"id"));
14776 li = li->li_next;
14777 }
14778 rettv->vval.v_number = 0;
14779 }
14780#endif
14781}
14782
14783/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014784 * "setpos()" function
14785 */
14786/*ARGSUSED*/
14787 static void
14788f_setpos(argvars, rettv)
14789 typval_T *argvars;
14790 typval_T *rettv;
14791{
14792 pos_T pos;
14793 int fnum;
14794 char_u *name;
14795
Bram Moolenaar08250432008-02-13 11:42:46 +000014796 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014797 name = get_tv_string_chk(argvars);
14798 if (name != NULL)
14799 {
14800 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14801 {
14802 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000014803 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014804 {
Bram Moolenaar08250432008-02-13 11:42:46 +000014805 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014806 if (fnum == curbuf->b_fnum)
14807 {
14808 curwin->w_cursor = pos;
14809 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000014810 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014811 }
14812 else
14813 EMSG(_(e_invarg));
14814 }
Bram Moolenaar08250432008-02-13 11:42:46 +000014815 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
14816 {
14817 /* set mark */
14818 if (setmark_pos(name[1], &pos, fnum) == OK)
14819 rettv->vval.v_number = 0;
14820 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014821 else
14822 EMSG(_(e_invarg));
14823 }
14824 }
14825}
14826
14827/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014828 * "setqflist()" function
14829 */
14830/*ARGSUSED*/
14831 static void
14832f_setqflist(argvars, rettv)
14833 typval_T *argvars;
14834 typval_T *rettv;
14835{
14836 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14837}
14838
14839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 * "setreg()" function
14841 */
14842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014843f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014844 typval_T *argvars;
14845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846{
14847 int regname;
14848 char_u *strregname;
14849 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014850 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 int append;
14852 char_u yank_type;
14853 long block_len;
14854
14855 block_len = -1;
14856 yank_type = MAUTO;
14857 append = FALSE;
14858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014859 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014860 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014862 if (strregname == NULL)
14863 return; /* type error; errmsg already given */
14864 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865 if (regname == 0 || regname == '@')
14866 regname = '"';
14867 else if (regname == '=')
14868 return;
14869
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014870 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014872 stropt = get_tv_string_chk(&argvars[2]);
14873 if (stropt == NULL)
14874 return; /* type error */
14875 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 switch (*stropt)
14877 {
14878 case 'a': case 'A': /* append */
14879 append = TRUE;
14880 break;
14881 case 'v': case 'c': /* character-wise selection */
14882 yank_type = MCHAR;
14883 break;
14884 case 'V': case 'l': /* line-wise selection */
14885 yank_type = MLINE;
14886 break;
14887#ifdef FEAT_VISUAL
14888 case 'b': case Ctrl_V: /* block-wise selection */
14889 yank_type = MBLOCK;
14890 if (VIM_ISDIGIT(stropt[1]))
14891 {
14892 ++stropt;
14893 block_len = getdigits(&stropt) - 1;
14894 --stropt;
14895 }
14896 break;
14897#endif
14898 }
14899 }
14900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014901 strval = get_tv_string_chk(&argvars[1]);
14902 if (strval != NULL)
14903 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014905 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906}
14907
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014908/*
14909 * "settabwinvar()" function
14910 */
14911 static void
14912f_settabwinvar(argvars, rettv)
14913 typval_T *argvars;
14914 typval_T *rettv;
14915{
14916 setwinvar(argvars, rettv, 1);
14917}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918
14919/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014920 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014924 typval_T *argvars;
14925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014927 setwinvar(argvars, rettv, 0);
14928}
14929
14930/*
14931 * "setwinvar()" and "settabwinvar()" functions
14932 */
14933 static void
14934setwinvar(argvars, rettv, off)
14935 typval_T *argvars;
14936 typval_T *rettv;
14937 int off;
14938{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 win_T *win;
14940#ifdef FEAT_WINDOWS
14941 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014942 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943#endif
14944 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014947 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014949 rettv->vval.v_number = 0;
14950
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 if (check_restricted() || check_secure())
14952 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014953
14954#ifdef FEAT_WINDOWS
14955 if (off == 1)
14956 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14957 else
14958 tp = curtab;
14959#endif
14960 win = find_win_by_nr(&argvars[off], tp);
14961 varname = get_tv_string_chk(&argvars[off + 1]);
14962 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963
14964 if (win != NULL && varname != NULL && varp != NULL)
14965 {
14966#ifdef FEAT_WINDOWS
14967 /* set curwin to be our win, temporarily */
14968 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014969 save_curtab = curtab;
14970 goto_tabpage_tp(tp);
14971 if (!win_valid(win))
14972 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973 curwin = win;
14974 curbuf = curwin->w_buffer;
14975#endif
14976
14977 if (*varname == '&')
14978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014979 long numval;
14980 char_u *strval;
14981 int error = FALSE;
14982
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014984 numval = get_tv_number_chk(varp, &error);
14985 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014986 if (!error && strval != NULL)
14987 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 }
14989 else
14990 {
14991 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14992 if (winvarname != NULL)
14993 {
14994 STRCPY(winvarname, "w:");
14995 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014996 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 vim_free(winvarname);
14998 }
14999 }
15000
15001#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015002 /* Restore current tabpage and window, if still valid (autocomands can
15003 * make them invalid). */
15004 if (valid_tabpage(save_curtab))
15005 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 if (win_valid(save_curwin))
15007 {
15008 curwin = save_curwin;
15009 curbuf = curwin->w_buffer;
15010 }
15011#endif
15012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013}
15014
15015/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015016 * "shellescape({string})" function
15017 */
15018 static void
15019f_shellescape(argvars, rettv)
15020 typval_T *argvars;
15021 typval_T *rettv;
15022{
15023 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
15024 rettv->v_type = VAR_STRING;
15025}
15026
15027/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015028 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 */
15030 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015031f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 typval_T *argvars;
15033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015035 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037 p = get_tv_string(&argvars[0]);
15038 rettv->vval.v_string = vim_strsave(p);
15039 simplify_filename(rettv->vval.v_string); /* simplify in place */
15040 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041}
15042
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043static int
15044#ifdef __BORLANDC__
15045 _RTLENTRYF
15046#endif
15047 item_compare __ARGS((const void *s1, const void *s2));
15048static int
15049#ifdef __BORLANDC__
15050 _RTLENTRYF
15051#endif
15052 item_compare2 __ARGS((const void *s1, const void *s2));
15053
15054static int item_compare_ic;
15055static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015056static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057#define ITEM_COMPARE_FAIL 999
15058
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015062 static int
15063#ifdef __BORLANDC__
15064_RTLENTRYF
15065#endif
15066item_compare(s1, s2)
15067 const void *s1;
15068 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015070 char_u *p1, *p2;
15071 char_u *tofree1, *tofree2;
15072 int res;
15073 char_u numbuf1[NUMBUFLEN];
15074 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015076 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15077 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015078 if (p1 == NULL)
15079 p1 = (char_u *)"";
15080 if (p2 == NULL)
15081 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 if (item_compare_ic)
15083 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 res = STRCMP(p1, p2);
15086 vim_free(tofree1);
15087 vim_free(tofree2);
15088 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089}
15090
15091 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092#ifdef __BORLANDC__
15093_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095item_compare2(s1, s2)
15096 const void *s1;
15097 const void *s2;
15098{
15099 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015100 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015101 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015104 /* shortcut after failure in previous call; compare all items equal */
15105 if (item_compare_func_err)
15106 return 0;
15107
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015108 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15109 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015110 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15111 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112
15113 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015114 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015115 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015116 clear_tv(&argv[0]);
15117 clear_tv(&argv[1]);
15118
15119 if (res == FAIL)
15120 res = ITEM_COMPARE_FAIL;
15121 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015122 /* return value has wrong type */
15123 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15124 if (item_compare_func_err)
15125 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015126 clear_tv(&rettv);
15127 return res;
15128}
15129
15130/*
15131 * "sort({list})" function
15132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 typval_T *argvars;
15136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137{
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 list_T *l;
15139 listitem_T *li;
15140 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015141 long len;
15142 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144 rettv->vval.v_number = 0;
15145 if (argvars[0].v_type != VAR_LIST)
15146 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 else
15148 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015150 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015151 return;
15152 rettv->vval.v_list = l;
15153 rettv->v_type = VAR_LIST;
15154 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 len = list_len(l);
15157 if (len <= 1)
15158 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160 item_compare_ic = FALSE;
15161 item_compare_func = NULL;
15162 if (argvars[1].v_type != VAR_UNKNOWN)
15163 {
15164 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015165 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166 else
15167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015168 int error = FALSE;
15169
15170 i = get_tv_number_chk(&argvars[1], &error);
15171 if (error)
15172 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173 if (i == 1)
15174 item_compare_ic = TRUE;
15175 else
15176 item_compare_func = get_tv_string(&argvars[1]);
15177 }
15178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015181 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015182 if (ptrs == NULL)
15183 return;
15184 i = 0;
15185 for (li = l->lv_first; li != NULL; li = li->li_next)
15186 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015188 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015189 /* test the compare function */
15190 if (item_compare_func != NULL
15191 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15192 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015193 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015195 {
15196 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015197 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198 item_compare_func == NULL ? item_compare : item_compare2);
15199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015200 if (!item_compare_func_err)
15201 {
15202 /* Clear the List and append the items in the sorted order. */
15203 l->lv_first = l->lv_last = NULL;
15204 l->lv_len = 0;
15205 for (i = 0; i < len; ++i)
15206 list_append(l, ptrs[i]);
15207 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015208 }
15209
15210 vim_free(ptrs);
15211 }
15212}
15213
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015214/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015215 * "soundfold({word})" function
15216 */
15217 static void
15218f_soundfold(argvars, rettv)
15219 typval_T *argvars;
15220 typval_T *rettv;
15221{
15222 char_u *s;
15223
15224 rettv->v_type = VAR_STRING;
15225 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015226#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015227 rettv->vval.v_string = eval_soundfold(s);
15228#else
15229 rettv->vval.v_string = vim_strsave(s);
15230#endif
15231}
15232
15233/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015234 * "spellbadword()" function
15235 */
15236/* ARGSUSED */
15237 static void
15238f_spellbadword(argvars, rettv)
15239 typval_T *argvars;
15240 typval_T *rettv;
15241{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015242 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015243 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015244 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015245
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015246 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015247 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015248
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015249#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015250 if (argvars[0].v_type == VAR_UNKNOWN)
15251 {
15252 /* Find the start and length of the badly spelled word. */
15253 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15254 if (len != 0)
15255 word = ml_get_cursor();
15256 }
15257 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15258 {
15259 char_u *str = get_tv_string_chk(&argvars[0]);
15260 int capcol = -1;
15261
15262 if (str != NULL)
15263 {
15264 /* Check the argument for spelling. */
15265 while (*str != NUL)
15266 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015267 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015268 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015269 {
15270 word = str;
15271 break;
15272 }
15273 str += len;
15274 }
15275 }
15276 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015277#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015278
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015279 list_append_string(rettv->vval.v_list, word, len);
15280 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015281 attr == HLF_SPB ? "bad" :
15282 attr == HLF_SPR ? "rare" :
15283 attr == HLF_SPL ? "local" :
15284 attr == HLF_SPC ? "caps" :
15285 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015286}
15287
15288/*
15289 * "spellsuggest()" function
15290 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015291/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015292 static void
15293f_spellsuggest(argvars, rettv)
15294 typval_T *argvars;
15295 typval_T *rettv;
15296{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015297#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015298 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015299 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015300 int maxcount;
15301 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015302 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015303 listitem_T *li;
15304 int need_capital = FALSE;
15305#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015306
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015307 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015308 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015309
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015310#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015311 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15312 {
15313 str = get_tv_string(&argvars[0]);
15314 if (argvars[1].v_type != VAR_UNKNOWN)
15315 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015316 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015317 if (maxcount <= 0)
15318 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015319 if (argvars[2].v_type != VAR_UNKNOWN)
15320 {
15321 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15322 if (typeerr)
15323 return;
15324 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015325 }
15326 else
15327 maxcount = 25;
15328
Bram Moolenaar4770d092006-01-12 23:22:24 +000015329 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015330
15331 for (i = 0; i < ga.ga_len; ++i)
15332 {
15333 str = ((char_u **)ga.ga_data)[i];
15334
15335 li = listitem_alloc();
15336 if (li == NULL)
15337 vim_free(str);
15338 else
15339 {
15340 li->li_tv.v_type = VAR_STRING;
15341 li->li_tv.v_lock = 0;
15342 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015343 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015344 }
15345 }
15346 ga_clear(&ga);
15347 }
15348#endif
15349}
15350
Bram Moolenaar0d660222005-01-07 21:51:51 +000015351 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015352f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015353 typval_T *argvars;
15354 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015355{
15356 char_u *str;
15357 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015358 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015359 regmatch_T regmatch;
15360 char_u patbuf[NUMBUFLEN];
15361 char_u *save_cpo;
15362 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015363 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015364 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015365 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015366
15367 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15368 save_cpo = p_cpo;
15369 p_cpo = (char_u *)"";
15370
15371 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015372 if (argvars[1].v_type != VAR_UNKNOWN)
15373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015374 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15375 if (pat == NULL)
15376 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015377 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015378 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015379 }
15380 if (pat == NULL || *pat == NUL)
15381 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015382
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015383 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015385 if (typeerr)
15386 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387
Bram Moolenaar0d660222005-01-07 21:51:51 +000015388 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15389 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015391 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015392 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015393 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015394 if (*str == NUL)
15395 match = FALSE; /* empty item at the end */
15396 else
15397 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015398 if (match)
15399 end = regmatch.startp[0];
15400 else
15401 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015402 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15403 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015404 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015405 if (list_append_string(rettv->vval.v_list, str,
15406 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015407 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015408 }
15409 if (!match)
15410 break;
15411 /* Advance to just after the match. */
15412 if (regmatch.endp[0] > str)
15413 col = 0;
15414 else
15415 {
15416 /* Don't get stuck at the same match. */
15417#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015418 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015419#else
15420 col = 1;
15421#endif
15422 }
15423 str = regmatch.endp[0];
15424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425
Bram Moolenaar0d660222005-01-07 21:51:51 +000015426 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428
Bram Moolenaar0d660222005-01-07 21:51:51 +000015429 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430}
15431
Bram Moolenaar2c932302006-03-18 21:42:09 +000015432/*
15433 * "str2nr()" function
15434 */
15435 static void
15436f_str2nr(argvars, rettv)
15437 typval_T *argvars;
15438 typval_T *rettv;
15439{
15440 int base = 10;
15441 char_u *p;
15442 long n;
15443
15444 if (argvars[1].v_type != VAR_UNKNOWN)
15445 {
15446 base = get_tv_number(&argvars[1]);
15447 if (base != 8 && base != 10 && base != 16)
15448 {
15449 EMSG(_(e_invarg));
15450 return;
15451 }
15452 }
15453
15454 p = skipwhite(get_tv_string(&argvars[0]));
15455 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15456 rettv->vval.v_number = n;
15457}
15458
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459#ifdef HAVE_STRFTIME
15460/*
15461 * "strftime({format}[, {time}])" function
15462 */
15463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015464f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 typval_T *argvars;
15466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467{
15468 char_u result_buf[256];
15469 struct tm *curtime;
15470 time_t seconds;
15471 char_u *p;
15472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015473 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015475 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015476 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477 seconds = time(NULL);
15478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015479 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 curtime = localtime(&seconds);
15481 /* MSVC returns NULL for an invalid value of seconds. */
15482 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015483 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 else
15485 {
15486# ifdef FEAT_MBYTE
15487 vimconv_T conv;
15488 char_u *enc;
15489
15490 conv.vc_type = CONV_NONE;
15491 enc = enc_locale();
15492 convert_setup(&conv, p_enc, enc);
15493 if (conv.vc_type != CONV_NONE)
15494 p = string_convert(&conv, p, NULL);
15495# endif
15496 if (p != NULL)
15497 (void)strftime((char *)result_buf, sizeof(result_buf),
15498 (char *)p, curtime);
15499 else
15500 result_buf[0] = NUL;
15501
15502# ifdef FEAT_MBYTE
15503 if (conv.vc_type != CONV_NONE)
15504 vim_free(p);
15505 convert_setup(&conv, enc, p_enc);
15506 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015507 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508 else
15509# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015510 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511
15512# ifdef FEAT_MBYTE
15513 /* Release conversion descriptors */
15514 convert_setup(&conv, NULL, NULL);
15515 vim_free(enc);
15516# endif
15517 }
15518}
15519#endif
15520
15521/*
15522 * "stridx()" function
15523 */
15524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015525f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015526 typval_T *argvars;
15527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528{
15529 char_u buf[NUMBUFLEN];
15530 char_u *needle;
15531 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015532 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015534 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015536 needle = get_tv_string_chk(&argvars[1]);
15537 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015538 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015539 if (needle == NULL || haystack == NULL)
15540 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541
Bram Moolenaar33570922005-01-25 22:26:29 +000015542 if (argvars[2].v_type != VAR_UNKNOWN)
15543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015544 int error = FALSE;
15545
15546 start_idx = get_tv_number_chk(&argvars[2], &error);
15547 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015548 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015549 if (start_idx >= 0)
15550 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015551 }
15552
15553 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15554 if (pos != NULL)
15555 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556}
15557
15558/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015559 * "string()" function
15560 */
15561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015563 typval_T *argvars;
15564 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015565{
15566 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015567 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015570 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015571 /* Make a copy if we have a value but it's not in allocate memory. */
15572 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574}
15575
15576/*
15577 * "strlen()" function
15578 */
15579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015580f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015581 typval_T *argvars;
15582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015584 rettv->vval.v_number = (varnumber_T)(STRLEN(
15585 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586}
15587
15588/*
15589 * "strpart()" function
15590 */
15591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015593 typval_T *argvars;
15594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595{
15596 char_u *p;
15597 int n;
15598 int len;
15599 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015600 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015602 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 slen = (int)STRLEN(p);
15604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015605 n = get_tv_number_chk(&argvars[1], &error);
15606 if (error)
15607 len = 0;
15608 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015609 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 else
15611 len = slen - n; /* default len: all bytes that are available. */
15612
15613 /*
15614 * Only return the overlap between the specified part and the actual
15615 * string.
15616 */
15617 if (n < 0)
15618 {
15619 len += n;
15620 n = 0;
15621 }
15622 else if (n > slen)
15623 n = slen;
15624 if (len < 0)
15625 len = 0;
15626 else if (n + len > slen)
15627 len = slen - n;
15628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015629 rettv->v_type = VAR_STRING;
15630 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631}
15632
15633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015634 * "strridx()" function
15635 */
15636 static void
15637f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015638 typval_T *argvars;
15639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640{
15641 char_u buf[NUMBUFLEN];
15642 char_u *needle;
15643 char_u *haystack;
15644 char_u *rest;
15645 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015646 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015648 needle = get_tv_string_chk(&argvars[1]);
15649 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015650
15651 rettv->vval.v_number = -1;
15652 if (needle == NULL || haystack == NULL)
15653 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015654
15655 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015656 if (argvars[2].v_type != VAR_UNKNOWN)
15657 {
15658 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015659 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015660 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015661 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015662 }
15663 else
15664 end_idx = haystack_len;
15665
Bram Moolenaar0d660222005-01-07 21:51:51 +000015666 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015667 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015668 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015669 lastmatch = haystack + end_idx;
15670 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015671 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015672 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015673 for (rest = haystack; *rest != '\0'; ++rest)
15674 {
15675 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015676 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677 break;
15678 lastmatch = rest;
15679 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015680 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015681
15682 if (lastmatch == NULL)
15683 rettv->vval.v_number = -1;
15684 else
15685 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15686}
15687
15688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689 * "strtrans()" function
15690 */
15691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015692f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015693 typval_T *argvars;
15694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015696 rettv->v_type = VAR_STRING;
15697 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698}
15699
15700/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015701 * "submatch()" function
15702 */
15703 static void
15704f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015705 typval_T *argvars;
15706 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015707{
15708 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015709 rettv->vval.v_string =
15710 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015711}
15712
15713/*
15714 * "substitute()" function
15715 */
15716 static void
15717f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 typval_T *argvars;
15719 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015720{
15721 char_u patbuf[NUMBUFLEN];
15722 char_u subbuf[NUMBUFLEN];
15723 char_u flagsbuf[NUMBUFLEN];
15724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015725 char_u *str = get_tv_string_chk(&argvars[0]);
15726 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15727 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15728 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15729
Bram Moolenaar0d660222005-01-07 21:51:51 +000015730 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015731 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15732 rettv->vval.v_string = NULL;
15733 else
15734 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015735}
15736
15737/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015738 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 */
15740/*ARGSUSED*/
15741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015742f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015743 typval_T *argvars;
15744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745{
15746 int id = 0;
15747#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015748 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 long col;
15750 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015751 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015753 lnum = get_tv_lnum(argvars); /* -1 on type error */
15754 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15755 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015757 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015758 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015759 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760#endif
15761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763}
15764
15765/*
15766 * "synIDattr(id, what [, mode])" function
15767 */
15768/*ARGSUSED*/
15769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015770f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015771 typval_T *argvars;
15772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773{
15774 char_u *p = NULL;
15775#ifdef FEAT_SYN_HL
15776 int id;
15777 char_u *what;
15778 char_u *mode;
15779 char_u modebuf[NUMBUFLEN];
15780 int modec;
15781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015782 id = get_tv_number(&argvars[0]);
15783 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015784 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 modec = TOLOWER_ASC(mode[0]);
15788 if (modec != 't' && modec != 'c'
15789#ifdef FEAT_GUI
15790 && modec != 'g'
15791#endif
15792 )
15793 modec = 0; /* replace invalid with current */
15794 }
15795 else
15796 {
15797#ifdef FEAT_GUI
15798 if (gui.in_use)
15799 modec = 'g';
15800 else
15801#endif
15802 if (t_colors > 1)
15803 modec = 'c';
15804 else
15805 modec = 't';
15806 }
15807
15808
15809 switch (TOLOWER_ASC(what[0]))
15810 {
15811 case 'b':
15812 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15813 p = highlight_color(id, what, modec);
15814 else /* bold */
15815 p = highlight_has_attr(id, HL_BOLD, modec);
15816 break;
15817
15818 case 'f': /* fg[#] */
15819 p = highlight_color(id, what, modec);
15820 break;
15821
15822 case 'i':
15823 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15824 p = highlight_has_attr(id, HL_INVERSE, modec);
15825 else /* italic */
15826 p = highlight_has_attr(id, HL_ITALIC, modec);
15827 break;
15828
15829 case 'n': /* name */
15830 p = get_highlight_name(NULL, id - 1);
15831 break;
15832
15833 case 'r': /* reverse */
15834 p = highlight_has_attr(id, HL_INVERSE, modec);
15835 break;
15836
15837 case 's': /* standout */
15838 p = highlight_has_attr(id, HL_STANDOUT, modec);
15839 break;
15840
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015841 case 'u':
15842 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15843 /* underline */
15844 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15845 else
15846 /* undercurl */
15847 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 break;
15849 }
15850
15851 if (p != NULL)
15852 p = vim_strsave(p);
15853#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015854 rettv->v_type = VAR_STRING;
15855 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856}
15857
15858/*
15859 * "synIDtrans(id)" function
15860 */
15861/*ARGSUSED*/
15862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015863f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015864 typval_T *argvars;
15865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866{
15867 int id;
15868
15869#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015870 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871
15872 if (id > 0)
15873 id = syn_get_final_id(id);
15874 else
15875#endif
15876 id = 0;
15877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015878 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879}
15880
15881/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015882 * "synstack(lnum, col)" function
15883 */
15884/*ARGSUSED*/
15885 static void
15886f_synstack(argvars, rettv)
15887 typval_T *argvars;
15888 typval_T *rettv;
15889{
15890#ifdef FEAT_SYN_HL
15891 long lnum;
15892 long col;
15893 int i;
15894 int id;
15895#endif
15896
15897 rettv->v_type = VAR_LIST;
15898 rettv->vval.v_list = NULL;
15899
15900#ifdef FEAT_SYN_HL
15901 lnum = get_tv_lnum(argvars); /* -1 on type error */
15902 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15903
15904 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
15905 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
15906 && rettv_list_alloc(rettv) != FAIL)
15907 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015908 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015909 for (i = 0; ; ++i)
15910 {
15911 id = syn_get_stack_item(i);
15912 if (id < 0)
15913 break;
15914 if (list_append_number(rettv->vval.v_list, id) == FAIL)
15915 break;
15916 }
15917 }
15918#endif
15919}
15920
15921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 * "system()" function
15923 */
15924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015925f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015926 typval_T *argvars;
15927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015929 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015931 char_u *infile = NULL;
15932 char_u buf[NUMBUFLEN];
15933 int err = FALSE;
15934 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015936 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015937 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015938
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015939 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015940 {
15941 /*
15942 * Write the string to a temp file, to be used for input of the shell
15943 * command.
15944 */
15945 if ((infile = vim_tempname('i')) == NULL)
15946 {
15947 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015948 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015949 }
15950
15951 fd = mch_fopen((char *)infile, WRITEBIN);
15952 if (fd == NULL)
15953 {
15954 EMSG2(_(e_notopen), infile);
15955 goto done;
15956 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015957 p = get_tv_string_buf_chk(&argvars[1], buf);
15958 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015959 {
15960 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015961 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015962 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015963 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15964 err = TRUE;
15965 if (fclose(fd) != 0)
15966 err = TRUE;
15967 if (err)
15968 {
15969 EMSG(_("E677: Error writing temp file"));
15970 goto done;
15971 }
15972 }
15973
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015974 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15975 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015976
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977#ifdef USE_CR
15978 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015979 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980 {
15981 char_u *s;
15982
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015983 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 {
15985 if (*s == CAR)
15986 *s = NL;
15987 }
15988 }
15989#else
15990# ifdef USE_CRNL
15991 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015992 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 {
15994 char_u *s, *d;
15995
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015996 d = res;
15997 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 {
15999 if (s[0] == CAR && s[1] == NL)
16000 ++s;
16001 *d++ = *s;
16002 }
16003 *d = NUL;
16004 }
16005# endif
16006#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016007
16008done:
16009 if (infile != NULL)
16010 {
16011 mch_remove(infile);
16012 vim_free(infile);
16013 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016014 rettv->v_type = VAR_STRING;
16015 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016}
16017
16018/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016019 * "tabpagebuflist()" function
16020 */
16021/* ARGSUSED */
16022 static void
16023f_tabpagebuflist(argvars, rettv)
16024 typval_T *argvars;
16025 typval_T *rettv;
16026{
16027#ifndef FEAT_WINDOWS
16028 rettv->vval.v_number = 0;
16029#else
16030 tabpage_T *tp;
16031 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016032
16033 if (argvars[0].v_type == VAR_UNKNOWN)
16034 wp = firstwin;
16035 else
16036 {
16037 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16038 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016039 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016040 }
16041 if (wp == NULL)
16042 rettv->vval.v_number = 0;
16043 else
16044 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016045 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016046 rettv->vval.v_number = 0;
16047 else
16048 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016049 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016050 if (list_append_number(rettv->vval.v_list,
16051 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016052 break;
16053 }
16054 }
16055#endif
16056}
16057
16058
16059/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016060 * "tabpagenr()" function
16061 */
16062/* ARGSUSED */
16063 static void
16064f_tabpagenr(argvars, rettv)
16065 typval_T *argvars;
16066 typval_T *rettv;
16067{
16068 int nr = 1;
16069#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016070 char_u *arg;
16071
16072 if (argvars[0].v_type != VAR_UNKNOWN)
16073 {
16074 arg = get_tv_string_chk(&argvars[0]);
16075 nr = 0;
16076 if (arg != NULL)
16077 {
16078 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016079 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016080 else
16081 EMSG2(_(e_invexpr2), arg);
16082 }
16083 }
16084 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016085 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016086#endif
16087 rettv->vval.v_number = nr;
16088}
16089
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016090
16091#ifdef FEAT_WINDOWS
16092static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16093
16094/*
16095 * Common code for tabpagewinnr() and winnr().
16096 */
16097 static int
16098get_winnr(tp, argvar)
16099 tabpage_T *tp;
16100 typval_T *argvar;
16101{
16102 win_T *twin;
16103 int nr = 1;
16104 win_T *wp;
16105 char_u *arg;
16106
16107 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16108 if (argvar->v_type != VAR_UNKNOWN)
16109 {
16110 arg = get_tv_string_chk(argvar);
16111 if (arg == NULL)
16112 nr = 0; /* type error; errmsg already given */
16113 else if (STRCMP(arg, "$") == 0)
16114 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16115 else if (STRCMP(arg, "#") == 0)
16116 {
16117 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16118 if (twin == NULL)
16119 nr = 0;
16120 }
16121 else
16122 {
16123 EMSG2(_(e_invexpr2), arg);
16124 nr = 0;
16125 }
16126 }
16127
16128 if (nr > 0)
16129 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16130 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016131 {
16132 if (wp == NULL)
16133 {
16134 /* didn't find it in this tabpage */
16135 nr = 0;
16136 break;
16137 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016138 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016139 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016140 return nr;
16141}
16142#endif
16143
16144/*
16145 * "tabpagewinnr()" function
16146 */
16147/* ARGSUSED */
16148 static void
16149f_tabpagewinnr(argvars, rettv)
16150 typval_T *argvars;
16151 typval_T *rettv;
16152{
16153 int nr = 1;
16154#ifdef FEAT_WINDOWS
16155 tabpage_T *tp;
16156
16157 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16158 if (tp == NULL)
16159 nr = 0;
16160 else
16161 nr = get_winnr(tp, &argvars[1]);
16162#endif
16163 rettv->vval.v_number = nr;
16164}
16165
16166
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016167/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016168 * "tagfiles()" function
16169 */
16170/*ARGSUSED*/
16171 static void
16172f_tagfiles(argvars, rettv)
16173 typval_T *argvars;
16174 typval_T *rettv;
16175{
16176 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016177 tagname_T tn;
16178 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016179
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016180 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016181 {
16182 rettv->vval.v_number = 0;
16183 return;
16184 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016185
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016186 for (first = TRUE; ; first = FALSE)
16187 if (get_tagfname(&tn, first, fname) == FAIL
16188 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016189 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016190 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016191}
16192
16193/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016194 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016195 */
16196 static void
16197f_taglist(argvars, rettv)
16198 typval_T *argvars;
16199 typval_T *rettv;
16200{
16201 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016202
16203 tag_pattern = get_tv_string(&argvars[0]);
16204
16205 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016206 if (*tag_pattern == NUL)
16207 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016208
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016209 if (rettv_list_alloc(rettv) == OK)
16210 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016211}
16212
16213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214 * "tempname()" function
16215 */
16216/*ARGSUSED*/
16217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016218f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016219 typval_T *argvars;
16220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221{
16222 static int x = 'A';
16223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016224 rettv->v_type = VAR_STRING;
16225 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226
16227 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16228 * names. Skip 'I' and 'O', they are used for shell redirection. */
16229 do
16230 {
16231 if (x == 'Z')
16232 x = '0';
16233 else if (x == '9')
16234 x = 'A';
16235 else
16236 {
16237#ifdef EBCDIC
16238 if (x == 'I')
16239 x = 'J';
16240 else if (x == 'R')
16241 x = 'S';
16242 else
16243#endif
16244 ++x;
16245 }
16246 } while (x == 'I' || x == 'O');
16247}
16248
16249/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016250 * "test(list)" function: Just checking the walls...
16251 */
16252/*ARGSUSED*/
16253 static void
16254f_test(argvars, rettv)
16255 typval_T *argvars;
16256 typval_T *rettv;
16257{
16258 /* Used for unit testing. Change the code below to your liking. */
16259#if 0
16260 listitem_T *li;
16261 list_T *l;
16262 char_u *bad, *good;
16263
16264 if (argvars[0].v_type != VAR_LIST)
16265 return;
16266 l = argvars[0].vval.v_list;
16267 if (l == NULL)
16268 return;
16269 li = l->lv_first;
16270 if (li == NULL)
16271 return;
16272 bad = get_tv_string(&li->li_tv);
16273 li = li->li_next;
16274 if (li == NULL)
16275 return;
16276 good = get_tv_string(&li->li_tv);
16277 rettv->vval.v_number = test_edit_score(bad, good);
16278#endif
16279}
16280
16281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 * "tolower(string)" function
16283 */
16284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016285f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 typval_T *argvars;
16287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288{
16289 char_u *p;
16290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016291 p = vim_strsave(get_tv_string(&argvars[0]));
16292 rettv->v_type = VAR_STRING;
16293 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294
16295 if (p != NULL)
16296 while (*p != NUL)
16297 {
16298#ifdef FEAT_MBYTE
16299 int l;
16300
16301 if (enc_utf8)
16302 {
16303 int c, lc;
16304
16305 c = utf_ptr2char(p);
16306 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016307 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 /* TODO: reallocate string when byte count changes. */
16309 if (utf_char2len(lc) == l)
16310 utf_char2bytes(lc, p);
16311 p += l;
16312 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016313 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 p += l; /* skip multi-byte character */
16315 else
16316#endif
16317 {
16318 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16319 ++p;
16320 }
16321 }
16322}
16323
16324/*
16325 * "toupper(string)" function
16326 */
16327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016328f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016329 typval_T *argvars;
16330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016332 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334}
16335
16336/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016337 * "tr(string, fromstr, tostr)" function
16338 */
16339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016340f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016341 typval_T *argvars;
16342 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016343{
16344 char_u *instr;
16345 char_u *fromstr;
16346 char_u *tostr;
16347 char_u *p;
16348#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016349 int inlen;
16350 int fromlen;
16351 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016352 int idx;
16353 char_u *cpstr;
16354 int cplen;
16355 int first = TRUE;
16356#endif
16357 char_u buf[NUMBUFLEN];
16358 char_u buf2[NUMBUFLEN];
16359 garray_T ga;
16360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016361 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016362 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16363 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016364
16365 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016366 rettv->v_type = VAR_STRING;
16367 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368 if (fromstr == NULL || tostr == NULL)
16369 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016370 ga_init2(&ga, (int)sizeof(char), 80);
16371
16372#ifdef FEAT_MBYTE
16373 if (!has_mbyte)
16374#endif
16375 /* not multi-byte: fromstr and tostr must be the same length */
16376 if (STRLEN(fromstr) != STRLEN(tostr))
16377 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016378#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016379error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016380#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016381 EMSG2(_(e_invarg2), fromstr);
16382 ga_clear(&ga);
16383 return;
16384 }
16385
16386 /* fromstr and tostr have to contain the same number of chars */
16387 while (*instr != NUL)
16388 {
16389#ifdef FEAT_MBYTE
16390 if (has_mbyte)
16391 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016392 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016393 cpstr = instr;
16394 cplen = inlen;
16395 idx = 0;
16396 for (p = fromstr; *p != NUL; p += fromlen)
16397 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016398 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016399 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16400 {
16401 for (p = tostr; *p != NUL; p += tolen)
16402 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016403 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016404 if (idx-- == 0)
16405 {
16406 cplen = tolen;
16407 cpstr = p;
16408 break;
16409 }
16410 }
16411 if (*p == NUL) /* tostr is shorter than fromstr */
16412 goto error;
16413 break;
16414 }
16415 ++idx;
16416 }
16417
16418 if (first && cpstr == instr)
16419 {
16420 /* Check that fromstr and tostr have the same number of
16421 * (multi-byte) characters. Done only once when a character
16422 * of instr doesn't appear in fromstr. */
16423 first = FALSE;
16424 for (p = tostr; *p != NUL; p += tolen)
16425 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016426 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016427 --idx;
16428 }
16429 if (idx != 0)
16430 goto error;
16431 }
16432
16433 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016434 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016435 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016436
16437 instr += inlen;
16438 }
16439 else
16440#endif
16441 {
16442 /* When not using multi-byte chars we can do it faster. */
16443 p = vim_strchr(fromstr, *instr);
16444 if (p != NULL)
16445 ga_append(&ga, tostr[p - fromstr]);
16446 else
16447 ga_append(&ga, *instr);
16448 ++instr;
16449 }
16450 }
16451
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016452 /* add a terminating NUL */
16453 ga_grow(&ga, 1);
16454 ga_append(&ga, NUL);
16455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016456 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016457}
16458
16459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 * "type(expr)" function
16461 */
16462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016463f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016464 typval_T *argvars;
16465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016467 int n;
16468
16469 switch (argvars[0].v_type)
16470 {
16471 case VAR_NUMBER: n = 0; break;
16472 case VAR_STRING: n = 1; break;
16473 case VAR_FUNC: n = 2; break;
16474 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016475 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016476 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16477 }
16478 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479}
16480
16481/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016482 * "values(dict)" function
16483 */
16484 static void
16485f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016486 typval_T *argvars;
16487 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016488{
16489 dict_list(argvars, rettv, 1);
16490}
16491
16492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 * "virtcol(string)" function
16494 */
16495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016497 typval_T *argvars;
16498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499{
16500 colnr_T vcol = 0;
16501 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016502 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016504 fp = var2fpos(&argvars[0], FALSE, &fnum);
16505 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16506 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507 {
16508 getvvcol(curwin, fp, NULL, NULL, &vcol);
16509 ++vcol;
16510 }
16511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016512 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513}
16514
16515/*
16516 * "visualmode()" function
16517 */
16518/*ARGSUSED*/
16519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016520f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016521 typval_T *argvars;
16522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523{
16524#ifdef FEAT_VISUAL
16525 char_u str[2];
16526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016527 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 str[0] = curbuf->b_visual_mode_eval;
16529 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016530 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531
16532 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016533 if ((argvars[0].v_type == VAR_NUMBER
16534 && argvars[0].vval.v_number != 0)
16535 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016536 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 curbuf->b_visual_mode_eval = NUL;
16538#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016539 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540#endif
16541}
16542
16543/*
16544 * "winbufnr(nr)" function
16545 */
16546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016547f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016548 typval_T *argvars;
16549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550{
16551 win_T *wp;
16552
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016553 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016557 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558}
16559
16560/*
16561 * "wincol()" function
16562 */
16563/*ARGSUSED*/
16564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016565f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016566 typval_T *argvars;
16567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568{
16569 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016570 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571}
16572
16573/*
16574 * "winheight(nr)" function
16575 */
16576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016577f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016578 typval_T *argvars;
16579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580{
16581 win_T *wp;
16582
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016583 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016585 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016587 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588}
16589
16590/*
16591 * "winline()" function
16592 */
16593/*ARGSUSED*/
16594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016595f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 typval_T *argvars;
16597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598{
16599 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016600 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601}
16602
16603/*
16604 * "winnr()" function
16605 */
16606/* ARGSUSED */
16607 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016608f_winnr(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 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016613
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016615 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016617 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618}
16619
16620/*
16621 * "winrestcmd()" function
16622 */
16623/* ARGSUSED */
16624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016625f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016626 typval_T *argvars;
16627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628{
16629#ifdef FEAT_WINDOWS
16630 win_T *wp;
16631 int winnr = 1;
16632 garray_T ga;
16633 char_u buf[50];
16634
16635 ga_init2(&ga, (int)sizeof(char), 70);
16636 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16637 {
16638 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16639 ga_concat(&ga, buf);
16640# ifdef FEAT_VERTSPLIT
16641 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16642 ga_concat(&ga, buf);
16643# endif
16644 ++winnr;
16645 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016646 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016648 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016650 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016652 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653}
16654
16655/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016656 * "winrestview()" function
16657 */
16658/* ARGSUSED */
16659 static void
16660f_winrestview(argvars, rettv)
16661 typval_T *argvars;
16662 typval_T *rettv;
16663{
16664 dict_T *dict;
16665
16666 if (argvars[0].v_type != VAR_DICT
16667 || (dict = argvars[0].vval.v_dict) == NULL)
16668 EMSG(_(e_invarg));
16669 else
16670 {
16671 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16672 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16673#ifdef FEAT_VIRTUALEDIT
16674 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16675#endif
16676 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016677 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016678
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016679 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016680#ifdef FEAT_DIFF
16681 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16682#endif
16683 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16684 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16685
16686 check_cursor();
16687 changed_cline_bef_curs();
16688 invalidate_botline();
16689 redraw_later(VALID);
16690
16691 if (curwin->w_topline == 0)
16692 curwin->w_topline = 1;
16693 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16694 curwin->w_topline = curbuf->b_ml.ml_line_count;
16695#ifdef FEAT_DIFF
16696 check_topfill(curwin, TRUE);
16697#endif
16698 }
16699}
16700
16701/*
16702 * "winsaveview()" function
16703 */
16704/* ARGSUSED */
16705 static void
16706f_winsaveview(argvars, rettv)
16707 typval_T *argvars;
16708 typval_T *rettv;
16709{
16710 dict_T *dict;
16711
16712 dict = dict_alloc();
16713 if (dict == NULL)
16714 return;
16715 rettv->v_type = VAR_DICT;
16716 rettv->vval.v_dict = dict;
16717 ++dict->dv_refcount;
16718
16719 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16720 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16721#ifdef FEAT_VIRTUALEDIT
16722 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16723#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016724 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016725 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16726
16727 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16728#ifdef FEAT_DIFF
16729 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16730#endif
16731 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16732 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16733}
16734
16735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 * "winwidth(nr)" function
16737 */
16738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016739f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016740 typval_T *argvars;
16741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742{
16743 win_T *wp;
16744
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016745 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016747 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 else
16749#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016750 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016752 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753#endif
16754}
16755
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016757 * "writefile()" function
16758 */
16759 static void
16760f_writefile(argvars, rettv)
16761 typval_T *argvars;
16762 typval_T *rettv;
16763{
16764 int binary = FALSE;
16765 char_u *fname;
16766 FILE *fd;
16767 listitem_T *li;
16768 char_u *s;
16769 int ret = 0;
16770 int c;
16771
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016772 if (check_restricted() || check_secure())
16773 return;
16774
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016775 if (argvars[0].v_type != VAR_LIST)
16776 {
16777 EMSG2(_(e_listarg), "writefile()");
16778 return;
16779 }
16780 if (argvars[0].vval.v_list == NULL)
16781 return;
16782
16783 if (argvars[2].v_type != VAR_UNKNOWN
16784 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16785 binary = TRUE;
16786
16787 /* Always open the file in binary mode, library functions have a mind of
16788 * their own about CR-LF conversion. */
16789 fname = get_tv_string(&argvars[1]);
16790 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16791 {
16792 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16793 ret = -1;
16794 }
16795 else
16796 {
16797 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16798 li = li->li_next)
16799 {
16800 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16801 {
16802 if (*s == '\n')
16803 c = putc(NUL, fd);
16804 else
16805 c = putc(*s, fd);
16806 if (c == EOF)
16807 {
16808 ret = -1;
16809 break;
16810 }
16811 }
16812 if (!binary || li->li_next != NULL)
16813 if (putc('\n', fd) == EOF)
16814 {
16815 ret = -1;
16816 break;
16817 }
16818 if (ret < 0)
16819 {
16820 EMSG(_(e_write));
16821 break;
16822 }
16823 }
16824 fclose(fd);
16825 }
16826
16827 rettv->vval.v_number = ret;
16828}
16829
16830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016832 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 */
16834 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016835var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016836 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016837 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016838 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016840 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016842 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843
Bram Moolenaara5525202006-03-02 22:52:09 +000016844 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016845 if (varp->v_type == VAR_LIST)
16846 {
16847 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016848 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016849 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016850 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016851
16852 l = varp->vval.v_list;
16853 if (l == NULL)
16854 return NULL;
16855
16856 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016857 pos.lnum = list_find_nr(l, 0L, &error);
16858 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016859 return NULL; /* invalid line number */
16860
16861 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016862 pos.col = list_find_nr(l, 1L, &error);
16863 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016864 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016865 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016866
16867 /* We accept "$" for the column number: last column. */
16868 li = list_find(l, 1L);
16869 if (li != NULL && li->li_tv.v_type == VAR_STRING
16870 && li->li_tv.vval.v_string != NULL
16871 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16872 pos.col = len + 1;
16873
Bram Moolenaara5525202006-03-02 22:52:09 +000016874 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016875 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016876 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016877 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016878
Bram Moolenaara5525202006-03-02 22:52:09 +000016879#ifdef FEAT_VIRTUALEDIT
16880 /* Get the virtual offset. Defaults to zero. */
16881 pos.coladd = list_find_nr(l, 2L, &error);
16882 if (error)
16883 pos.coladd = 0;
16884#endif
16885
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016886 return &pos;
16887 }
16888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016889 name = get_tv_string_chk(varp);
16890 if (name == NULL)
16891 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892 if (name[0] == '.') /* cursor */
16893 return &curwin->w_cursor;
16894 if (name[0] == '\'') /* mark */
16895 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016896 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16898 return NULL;
16899 return pp;
16900 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016901
16902#ifdef FEAT_VIRTUALEDIT
16903 pos.coladd = 0;
16904#endif
16905
Bram Moolenaar477933c2007-07-17 14:32:23 +000016906 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016907 {
16908 pos.col = 0;
16909 if (name[1] == '0') /* "w0": first visible line */
16910 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016911 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016912 pos.lnum = curwin->w_topline;
16913 return &pos;
16914 }
16915 else if (name[1] == '$') /* "w$": last visible line */
16916 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016917 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016918 pos.lnum = curwin->w_botline - 1;
16919 return &pos;
16920 }
16921 }
16922 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016924 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 {
16926 pos.lnum = curbuf->b_ml.ml_line_count;
16927 pos.col = 0;
16928 }
16929 else
16930 {
16931 pos.lnum = curwin->w_cursor.lnum;
16932 pos.col = (colnr_T)STRLEN(ml_get_curline());
16933 }
16934 return &pos;
16935 }
16936 return NULL;
16937}
16938
16939/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016940 * Convert list in "arg" into a position and optional file number.
16941 * When "fnump" is NULL there is no file number, only 3 items.
16942 * Note that the column is passed on as-is, the caller may want to decrement
16943 * it to use 1 for the first column.
16944 * Return FAIL when conversion is not possible, doesn't check the position for
16945 * validity.
16946 */
16947 static int
16948list2fpos(arg, posp, fnump)
16949 typval_T *arg;
16950 pos_T *posp;
16951 int *fnump;
16952{
16953 list_T *l = arg->vval.v_list;
16954 long i = 0;
16955 long n;
16956
Bram Moolenaarbde35262006-07-23 20:12:24 +000016957 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16958 * when "fnump" isn't NULL and "coladd" is optional. */
16959 if (arg->v_type != VAR_LIST
16960 || l == NULL
16961 || l->lv_len < (fnump == NULL ? 2 : 3)
16962 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016963 return FAIL;
16964
16965 if (fnump != NULL)
16966 {
16967 n = list_find_nr(l, i++, NULL); /* fnum */
16968 if (n < 0)
16969 return FAIL;
16970 if (n == 0)
16971 n = curbuf->b_fnum; /* current buffer */
16972 *fnump = n;
16973 }
16974
16975 n = list_find_nr(l, i++, NULL); /* lnum */
16976 if (n < 0)
16977 return FAIL;
16978 posp->lnum = n;
16979
16980 n = list_find_nr(l, i++, NULL); /* col */
16981 if (n < 0)
16982 return FAIL;
16983 posp->col = n;
16984
16985#ifdef FEAT_VIRTUALEDIT
16986 n = list_find_nr(l, i, NULL);
16987 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016988 posp->coladd = 0;
16989 else
16990 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016991#endif
16992
16993 return OK;
16994}
16995
16996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 * Get the length of an environment variable name.
16998 * Advance "arg" to the first character after the name.
16999 * Return 0 for error.
17000 */
17001 static int
17002get_env_len(arg)
17003 char_u **arg;
17004{
17005 char_u *p;
17006 int len;
17007
17008 for (p = *arg; vim_isIDc(*p); ++p)
17009 ;
17010 if (p == *arg) /* no name found */
17011 return 0;
17012
17013 len = (int)(p - *arg);
17014 *arg = p;
17015 return len;
17016}
17017
17018/*
17019 * Get the length of the name of a function or internal variable.
17020 * "arg" is advanced to the first non-white character after the name.
17021 * Return 0 if something is wrong.
17022 */
17023 static int
17024get_id_len(arg)
17025 char_u **arg;
17026{
17027 char_u *p;
17028 int len;
17029
17030 /* Find the end of the name. */
17031 for (p = *arg; eval_isnamec(*p); ++p)
17032 ;
17033 if (p == *arg) /* no name found */
17034 return 0;
17035
17036 len = (int)(p - *arg);
17037 *arg = skipwhite(p);
17038
17039 return len;
17040}
17041
17042/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017043 * Get the length of the name of a variable or function.
17044 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017046 * Return -1 if curly braces expansion failed.
17047 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 * If the name contains 'magic' {}'s, expand them and return the
17049 * expanded name in an allocated string via 'alias' - caller must free.
17050 */
17051 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017052get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 char_u **arg;
17054 char_u **alias;
17055 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017056 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057{
17058 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 char_u *p;
17060 char_u *expr_start;
17061 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062
17063 *alias = NULL; /* default to no alias */
17064
17065 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17066 && (*arg)[2] == (int)KE_SNR)
17067 {
17068 /* hard coded <SNR>, already translated */
17069 *arg += 3;
17070 return get_id_len(arg) + 3;
17071 }
17072 len = eval_fname_script(*arg);
17073 if (len > 0)
17074 {
17075 /* literal "<SID>", "s:" or "<SNR>" */
17076 *arg += len;
17077 }
17078
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017080 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017082 p = find_name_end(*arg, &expr_start, &expr_end,
17083 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 if (expr_start != NULL)
17085 {
17086 char_u *temp_string;
17087
17088 if (!evaluate)
17089 {
17090 len += (int)(p - *arg);
17091 *arg = skipwhite(p);
17092 return len;
17093 }
17094
17095 /*
17096 * Include any <SID> etc in the expanded string:
17097 * Thus the -len here.
17098 */
17099 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17100 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017101 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 *alias = temp_string;
17103 *arg = skipwhite(p);
17104 return (int)STRLEN(temp_string);
17105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106
17107 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017108 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 EMSG2(_(e_invexpr2), *arg);
17110
17111 return len;
17112}
17113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017114/*
17115 * Find the end of a variable or function name, taking care of magic braces.
17116 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17117 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017118 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119 * Return a pointer to just after the name. Equal to "arg" if there is no
17120 * valid name.
17121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017123find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 char_u *arg;
17125 char_u **expr_start;
17126 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017127 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017129 int mb_nest = 0;
17130 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 char_u *p;
17132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017135 *expr_start = NULL;
17136 *expr_end = NULL;
17137 }
17138
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017139 /* Quick check for valid starting character. */
17140 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17141 return arg;
17142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017143 for (p = arg; *p != NUL
17144 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017145 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017146 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017147 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017148 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017149 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017150 if (*p == '\'')
17151 {
17152 /* skip over 'string' to avoid counting [ and ] inside it. */
17153 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17154 ;
17155 if (*p == NUL)
17156 break;
17157 }
17158 else if (*p == '"')
17159 {
17160 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17161 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17162 if (*p == '\\' && p[1] != NUL)
17163 ++p;
17164 if (*p == NUL)
17165 break;
17166 }
17167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017168 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017170 if (*p == '[')
17171 ++br_nest;
17172 else if (*p == ']')
17173 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017178 if (*p == '{')
17179 {
17180 mb_nest++;
17181 if (expr_start != NULL && *expr_start == NULL)
17182 *expr_start = p;
17183 }
17184 else if (*p == '}')
17185 {
17186 mb_nest--;
17187 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17188 *expr_end = p;
17189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 }
17192
17193 return p;
17194}
17195
17196/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017197 * Expands out the 'magic' {}'s in a variable/function name.
17198 * Note that this can call itself recursively, to deal with
17199 * constructs like foo{bar}{baz}{bam}
17200 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17201 * "in_start" ^
17202 * "expr_start" ^
17203 * "expr_end" ^
17204 * "in_end" ^
17205 *
17206 * Returns a new allocated string, which the caller must free.
17207 * Returns NULL for failure.
17208 */
17209 static char_u *
17210make_expanded_name(in_start, expr_start, expr_end, in_end)
17211 char_u *in_start;
17212 char_u *expr_start;
17213 char_u *expr_end;
17214 char_u *in_end;
17215{
17216 char_u c1;
17217 char_u *retval = NULL;
17218 char_u *temp_result;
17219 char_u *nextcmd = NULL;
17220
17221 if (expr_end == NULL || in_end == NULL)
17222 return NULL;
17223 *expr_start = NUL;
17224 *expr_end = NUL;
17225 c1 = *in_end;
17226 *in_end = NUL;
17227
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017228 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017229 if (temp_result != NULL && nextcmd == NULL)
17230 {
17231 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17232 + (in_end - expr_end) + 1));
17233 if (retval != NULL)
17234 {
17235 STRCPY(retval, in_start);
17236 STRCAT(retval, temp_result);
17237 STRCAT(retval, expr_end + 1);
17238 }
17239 }
17240 vim_free(temp_result);
17241
17242 *in_end = c1; /* put char back for error messages */
17243 *expr_start = '{';
17244 *expr_end = '}';
17245
17246 if (retval != NULL)
17247 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017248 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017249 if (expr_start != NULL)
17250 {
17251 /* Further expansion! */
17252 temp_result = make_expanded_name(retval, expr_start,
17253 expr_end, temp_result);
17254 vim_free(retval);
17255 retval = temp_result;
17256 }
17257 }
17258
17259 return retval;
17260}
17261
17262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017264 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265 */
17266 static int
17267eval_isnamec(c)
17268 int c;
17269{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017270 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17271}
17272
17273/*
17274 * Return TRUE if character "c" can be used as the first character in a
17275 * variable or function name (excluding '{' and '}').
17276 */
17277 static int
17278eval_isnamec1(c)
17279 int c;
17280{
17281 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282}
17283
17284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 * Set number v: variable to "val".
17286 */
17287 void
17288set_vim_var_nr(idx, val)
17289 int idx;
17290 long val;
17291{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017292 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293}
17294
17295/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017296 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 */
17298 long
17299get_vim_var_nr(idx)
17300 int idx;
17301{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017302 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303}
17304
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017305#if defined(FEAT_AUTOCMD) || defined(PROTO)
17306/*
17307 * Get string v: variable value. Uses a static buffer, can only be used once.
17308 */
17309 char_u *
17310get_vim_var_str(idx)
17311 int idx;
17312{
17313 return get_tv_string(&vimvars[idx].vv_tv);
17314}
17315#endif
17316
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317/*
17318 * Set v:count, v:count1 and v:prevcount.
17319 */
17320 void
17321set_vcount(count, count1)
17322 long count;
17323 long count1;
17324{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017325 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17326 vimvars[VV_COUNT].vv_nr = count;
17327 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328}
17329
17330/*
17331 * Set string v: variable to a copy of "val".
17332 */
17333 void
17334set_vim_var_string(idx, val, len)
17335 int idx;
17336 char_u *val;
17337 int len; /* length of "val" to use or -1 (whole string) */
17338{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017339 /* Need to do this (at least) once, since we can't initialize a union.
17340 * Will always be invoked when "v:progname" is set. */
17341 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17342
Bram Moolenaare9a41262005-01-15 22:18:47 +000017343 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017345 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017347 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017349 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350}
17351
17352/*
17353 * Set v:register if needed.
17354 */
17355 void
17356set_reg_var(c)
17357 int c;
17358{
17359 char_u regname;
17360
17361 if (c == 0 || c == ' ')
17362 regname = '"';
17363 else
17364 regname = c;
17365 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017366 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 set_vim_var_string(VV_REG, &regname, 1);
17368}
17369
17370/*
17371 * Get or set v:exception. 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:exception! Does not
17374 * take care of memory allocations.
17375 */
17376 char_u *
17377v_exception(oldval)
17378 char_u *oldval;
17379{
17380 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017381 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382
Bram Moolenaare9a41262005-01-15 22:18:47 +000017383 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 return NULL;
17385}
17386
17387/*
17388 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17389 * Otherwise, restore the value to "oldval" and return NULL.
17390 * Must always be called in pairs to save and restore v:throwpoint! Does not
17391 * take care of memory allocations.
17392 */
17393 char_u *
17394v_throwpoint(oldval)
17395 char_u *oldval;
17396{
17397 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017398 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399
Bram Moolenaare9a41262005-01-15 22:18:47 +000017400 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 return NULL;
17402}
17403
17404#if defined(FEAT_AUTOCMD) || defined(PROTO)
17405/*
17406 * Set v:cmdarg.
17407 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17408 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17409 * Must always be called in pairs!
17410 */
17411 char_u *
17412set_cmdarg(eap, oldarg)
17413 exarg_T *eap;
17414 char_u *oldarg;
17415{
17416 char_u *oldval;
17417 char_u *newval;
17418 unsigned len;
17419
Bram Moolenaare9a41262005-01-15 22:18:47 +000017420 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017421 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017423 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017424 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017425 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 }
17427
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017428 if (eap->force_bin == FORCE_BIN)
17429 len = 6;
17430 else if (eap->force_bin == FORCE_NOBIN)
17431 len = 8;
17432 else
17433 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017434
17435 if (eap->read_edit)
17436 len += 7;
17437
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017438 if (eap->force_ff != 0)
17439 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17440# ifdef FEAT_MBYTE
17441 if (eap->force_enc != 0)
17442 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017443 if (eap->bad_char != 0)
17444 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017445# endif
17446
17447 newval = alloc(len + 1);
17448 if (newval == NULL)
17449 return NULL;
17450
17451 if (eap->force_bin == FORCE_BIN)
17452 sprintf((char *)newval, " ++bin");
17453 else if (eap->force_bin == FORCE_NOBIN)
17454 sprintf((char *)newval, " ++nobin");
17455 else
17456 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017457
17458 if (eap->read_edit)
17459 STRCAT(newval, " ++edit");
17460
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017461 if (eap->force_ff != 0)
17462 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17463 eap->cmd + eap->force_ff);
17464# ifdef FEAT_MBYTE
17465 if (eap->force_enc != 0)
17466 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17467 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017468 if (eap->bad_char != 0)
17469 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17470 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017471# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017472 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017473 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474}
17475#endif
17476
17477/*
17478 * Get the value of internal variable "name".
17479 * Return OK or FAIL.
17480 */
17481 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017482get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 char_u *name;
17484 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017485 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017486 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487{
17488 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017489 typval_T *tv = NULL;
17490 typval_T atv;
17491 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493
17494 /* truncate the name, so that we can use strcmp() */
17495 cc = name[len];
17496 name[len] = NUL;
17497
17498 /*
17499 * Check for "b:changedtick".
17500 */
17501 if (STRCMP(name, "b:changedtick") == 0)
17502 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017503 atv.v_type = VAR_NUMBER;
17504 atv.vval.v_number = curbuf->b_changedtick;
17505 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 }
17507
17508 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 * Check for user-defined variables.
17510 */
17511 else
17512 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017513 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017515 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 }
17517
Bram Moolenaare9a41262005-01-15 22:18:47 +000017518 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017520 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 ret = FAIL;
17523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017524 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017525 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526
17527 name[len] = cc;
17528
17529 return ret;
17530}
17531
17532/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017533 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17534 * Also handle function call with Funcref variable: func(expr)
17535 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17536 */
17537 static int
17538handle_subscript(arg, rettv, evaluate, verbose)
17539 char_u **arg;
17540 typval_T *rettv;
17541 int evaluate; /* do more than finding the end */
17542 int verbose; /* give error messages */
17543{
17544 int ret = OK;
17545 dict_T *selfdict = NULL;
17546 char_u *s;
17547 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017548 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017549
17550 while (ret == OK
17551 && (**arg == '['
17552 || (**arg == '.' && rettv->v_type == VAR_DICT)
17553 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17554 && !vim_iswhite(*(*arg - 1)))
17555 {
17556 if (**arg == '(')
17557 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017558 /* need to copy the funcref so that we can clear rettv */
17559 functv = *rettv;
17560 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017561
17562 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017563 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017564 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017565 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17566 &len, evaluate, selfdict);
17567
17568 /* Clear the funcref afterwards, so that deleting it while
17569 * evaluating the arguments is possible (see test55). */
17570 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017571
17572 /* Stop the expression evaluation when immediately aborting on
17573 * error, or when an interrupt occurred or an exception was thrown
17574 * but not caught. */
17575 if (aborting())
17576 {
17577 if (ret == OK)
17578 clear_tv(rettv);
17579 ret = FAIL;
17580 }
17581 dict_unref(selfdict);
17582 selfdict = NULL;
17583 }
17584 else /* **arg == '[' || **arg == '.' */
17585 {
17586 dict_unref(selfdict);
17587 if (rettv->v_type == VAR_DICT)
17588 {
17589 selfdict = rettv->vval.v_dict;
17590 if (selfdict != NULL)
17591 ++selfdict->dv_refcount;
17592 }
17593 else
17594 selfdict = NULL;
17595 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17596 {
17597 clear_tv(rettv);
17598 ret = FAIL;
17599 }
17600 }
17601 }
17602 dict_unref(selfdict);
17603 return ret;
17604}
17605
17606/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017607 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17608 * value).
17609 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017610 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017611alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017612{
Bram Moolenaar33570922005-01-25 22:26:29 +000017613 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017614}
17615
17616/*
17617 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 * The string "s" must have been allocated, it is consumed.
17619 * Return NULL for out of memory, the variable otherwise.
17620 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017621 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017622alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 char_u *s;
17624{
Bram Moolenaar33570922005-01-25 22:26:29 +000017625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627 rettv = alloc_tv();
17628 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630 rettv->v_type = VAR_STRING;
17631 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 }
17633 else
17634 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017635 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636}
17637
17638/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017639 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017641 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017642free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017643 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644{
17645 if (varp != NULL)
17646 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017647 switch (varp->v_type)
17648 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017649 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017650 func_unref(varp->vval.v_string);
17651 /*FALLTHROUGH*/
17652 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017653 vim_free(varp->vval.v_string);
17654 break;
17655 case VAR_LIST:
17656 list_unref(varp->vval.v_list);
17657 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017658 case VAR_DICT:
17659 dict_unref(varp->vval.v_dict);
17660 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017661 case VAR_NUMBER:
17662 case VAR_UNKNOWN:
17663 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017664 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017665 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017666 break;
17667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 vim_free(varp);
17669 }
17670}
17671
17672/*
17673 * Free the memory for a variable value and set the value to NULL or 0.
17674 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017675 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017677 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678{
17679 if (varp != NULL)
17680 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017681 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017683 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017684 func_unref(varp->vval.v_string);
17685 /*FALLTHROUGH*/
17686 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017687 vim_free(varp->vval.v_string);
17688 varp->vval.v_string = NULL;
17689 break;
17690 case VAR_LIST:
17691 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017692 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017693 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017694 case VAR_DICT:
17695 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017696 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017697 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017698 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017699 varp->vval.v_number = 0;
17700 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017701 case VAR_UNKNOWN:
17702 break;
17703 default:
17704 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017706 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 }
17708}
17709
17710/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017711 * Set the value of a variable to NULL without freeing items.
17712 */
17713 static void
17714init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017715 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017716{
17717 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017718 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719}
17720
17721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 * Get the number value of a variable.
17723 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017724 * For incompatible types, return 0.
17725 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17726 * caller of incompatible types: it sets *denote to TRUE if "denote"
17727 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 */
17729 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017730get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017731 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017733 int error = FALSE;
17734
17735 return get_tv_number_chk(varp, &error); /* return 0L on error */
17736}
17737
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017738 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017739get_tv_number_chk(varp, denote)
17740 typval_T *varp;
17741 int *denote;
17742{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017743 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017745 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017747 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017748 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017749 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017750 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017751 break;
17752 case VAR_STRING:
17753 if (varp->vval.v_string != NULL)
17754 vim_str2nr(varp->vval.v_string, NULL, NULL,
17755 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017756 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017757 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017758 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017759 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017760 case VAR_DICT:
17761 EMSG(_("E728: Using a Dictionary as a number"));
17762 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017763 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017764 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017767 if (denote == NULL) /* useful for values that must be unsigned */
17768 n = -1;
17769 else
17770 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017771 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772}
17773
17774/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017775 * Get the lnum from the first argument.
17776 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017777 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 */
17779 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017780get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017781 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782{
Bram Moolenaar33570922005-01-25 22:26:29 +000017783 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 linenr_T lnum;
17785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017786 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 if (lnum == 0) /* no valid number, try using line() */
17788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017789 rettv.v_type = VAR_NUMBER;
17790 f_line(argvars, &rettv);
17791 lnum = rettv.vval.v_number;
17792 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 }
17794 return lnum;
17795}
17796
17797/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017798 * Get the lnum from the first argument.
17799 * Also accepts "$", then "buf" is used.
17800 * Returns 0 on error.
17801 */
17802 static linenr_T
17803get_tv_lnum_buf(argvars, buf)
17804 typval_T *argvars;
17805 buf_T *buf;
17806{
17807 if (argvars[0].v_type == VAR_STRING
17808 && argvars[0].vval.v_string != NULL
17809 && argvars[0].vval.v_string[0] == '$'
17810 && buf != NULL)
17811 return buf->b_ml.ml_line_count;
17812 return get_tv_number_chk(&argvars[0], NULL);
17813}
17814
17815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 * Get the string value of a variable.
17817 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017818 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17819 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 * If the String variable has never been set, return an empty string.
17821 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017822 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17823 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 */
17825 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017826get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017827 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828{
17829 static char_u mybuf[NUMBUFLEN];
17830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017831 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832}
17833
17834 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017835get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017836 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017837 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017839 char_u *res = get_tv_string_buf_chk(varp, buf);
17840
17841 return res != NULL ? res : (char_u *)"";
17842}
17843
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017844 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017845get_tv_string_chk(varp)
17846 typval_T *varp;
17847{
17848 static char_u mybuf[NUMBUFLEN];
17849
17850 return get_tv_string_buf_chk(varp, mybuf);
17851}
17852
17853 static char_u *
17854get_tv_string_buf_chk(varp, buf)
17855 typval_T *varp;
17856 char_u *buf;
17857{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017858 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017860 case VAR_NUMBER:
17861 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17862 return buf;
17863 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017864 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017865 break;
17866 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017867 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017868 break;
17869 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017870 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017871 break;
17872 case VAR_STRING:
17873 if (varp->vval.v_string != NULL)
17874 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017875 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017876 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017877 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017880 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881}
17882
17883/*
17884 * Find variable "name" in the list of variables.
17885 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017886 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017887 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017888 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017890 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017891find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017896 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897
Bram Moolenaara7043832005-01-21 11:56:39 +000017898 ht = find_var_ht(name, &varname);
17899 if (htp != NULL)
17900 *htp = ht;
17901 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017903 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904}
17905
17906/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017907 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017908 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017910 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017911find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017912 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017913 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017914 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017915{
Bram Moolenaar33570922005-01-25 22:26:29 +000017916 hashitem_T *hi;
17917
17918 if (*varname == NUL)
17919 {
17920 /* Must be something like "s:", otherwise "ht" would be NULL. */
17921 switch (varname[-2])
17922 {
17923 case 's': return &SCRIPT_SV(current_SID).sv_var;
17924 case 'g': return &globvars_var;
17925 case 'v': return &vimvars_var;
17926 case 'b': return &curbuf->b_bufvar;
17927 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017928#ifdef FEAT_WINDOWS
17929 case 't': return &curtab->tp_winvar;
17930#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017931 case 'l': return current_funccal == NULL
17932 ? NULL : &current_funccal->l_vars_var;
17933 case 'a': return current_funccal == NULL
17934 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017935 }
17936 return NULL;
17937 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017938
17939 hi = hash_find(ht, varname);
17940 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017941 {
17942 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017943 * worked find the variable again. Don't auto-load a script if it was
17944 * loaded already, otherwise it would be loaded every time when
17945 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017946 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017947 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017948 hi = hash_find(ht, varname);
17949 if (HASHITEM_EMPTY(hi))
17950 return NULL;
17951 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017952 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017953}
17954
17955/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017956 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017957 * Set "varname" to the start of name without ':'.
17958 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017959 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017960find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 char_u *name;
17962 char_u **varname;
17963{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017964 hashitem_T *hi;
17965
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966 if (name[1] != ':')
17967 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017968 /* The name must not start with a colon or #. */
17969 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 return NULL;
17971 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017972
17973 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017974 hi = hash_find(&compat_hashtab, name);
17975 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017976 return &compat_hashtab;
17977
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017979 return &globvarht; /* global variable */
17980 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981 }
17982 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017983 if (*name == 'g') /* global variable */
17984 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017985 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17986 */
17987 if (vim_strchr(name + 2, ':') != NULL
17988 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017989 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017991 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017993 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017994#ifdef FEAT_WINDOWS
17995 if (*name == 't') /* tab page variable */
17996 return &curtab->tp_vars.dv_hashtab;
17997#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017998 if (*name == 'v') /* v: variable */
17999 return &vimvarht;
18000 if (*name == 'a' && current_funccal != NULL) /* function argument */
18001 return &current_funccal->l_avars.dv_hashtab;
18002 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18003 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 if (*name == 's' /* script variable */
18005 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18006 return &SCRIPT_VARS(current_SID);
18007 return NULL;
18008}
18009
18010/*
18011 * Get the string value of a (global/local) variable.
18012 * Returns NULL when it doesn't exist.
18013 */
18014 char_u *
18015get_var_value(name)
18016 char_u *name;
18017{
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019
Bram Moolenaara7043832005-01-21 11:56:39 +000018020 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 if (v == NULL)
18022 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018023 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024}
18025
18026/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018027 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 * sourcing this script and when executing functions defined in the script.
18029 */
18030 void
18031new_script_vars(id)
18032 scid_T id;
18033{
Bram Moolenaara7043832005-01-21 11:56:39 +000018034 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018035 hashtab_T *ht;
18036 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018037
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18039 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018040 /* Re-allocating ga_data means that an ht_array pointing to
18041 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018042 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018043 for (i = 1; i <= ga_scripts.ga_len; ++i)
18044 {
18045 ht = &SCRIPT_VARS(i);
18046 if (ht->ht_mask == HT_INIT_SIZE - 1)
18047 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 sv = &SCRIPT_SV(i);
18049 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018050 }
18051
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 while (ga_scripts.ga_len < id)
18053 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018054 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18055 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 }
18058 }
18059}
18060
18061/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018062 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18063 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 */
18065 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018066init_var_dict(dict, dict_var)
18067 dict_T *dict;
18068 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069{
Bram Moolenaar33570922005-01-25 22:26:29 +000018070 hash_init(&dict->dv_hashtab);
18071 dict->dv_refcount = 99999;
18072 dict_var->di_tv.vval.v_dict = dict;
18073 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018074 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018075 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18076 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077}
18078
18079/*
18080 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018081 * Frees all allocated variables and the value they contain.
18082 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 */
18084 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018085vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018086 hashtab_T *ht;
18087{
18088 vars_clear_ext(ht, TRUE);
18089}
18090
18091/*
18092 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18093 */
18094 static void
18095vars_clear_ext(ht, free_val)
18096 hashtab_T *ht;
18097 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098{
Bram Moolenaara7043832005-01-21 11:56:39 +000018099 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 hashitem_T *hi;
18101 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102
Bram Moolenaar33570922005-01-25 22:26:29 +000018103 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018104 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018105 for (hi = ht->ht_array; todo > 0; ++hi)
18106 {
18107 if (!HASHITEM_EMPTY(hi))
18108 {
18109 --todo;
18110
Bram Moolenaar33570922005-01-25 22:26:29 +000018111 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018112 * ht_array might change then. hash_clear() takes care of it
18113 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018114 v = HI2DI(hi);
18115 if (free_val)
18116 clear_tv(&v->di_tv);
18117 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18118 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018119 }
18120 }
18121 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018122 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123}
18124
Bram Moolenaara7043832005-01-21 11:56:39 +000018125/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018126 * Delete a variable from hashtab "ht" at item "hi".
18127 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018130delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018131 hashtab_T *ht;
18132 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133{
Bram Moolenaar33570922005-01-25 22:26:29 +000018134 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018135
18136 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018137 clear_tv(&di->di_tv);
18138 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139}
18140
18141/*
18142 * List the value of one internal variable.
18143 */
18144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018145list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018146 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018148 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018150 char_u *tofree;
18151 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018152 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018153
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018154 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018155 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018156 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018157 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158}
18159
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018161list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 char_u *prefix;
18163 char_u *name;
18164 int type;
18165 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018166 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167{
Bram Moolenaar31859182007-08-14 20:41:13 +000018168 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18169 msg_start();
18170 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 if (name != NULL) /* "a:" vars don't have a name stored */
18172 msg_puts(name);
18173 msg_putchar(' ');
18174 msg_advance(22);
18175 if (type == VAR_NUMBER)
18176 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018177 else if (type == VAR_FUNC)
18178 msg_putchar('*');
18179 else if (type == VAR_LIST)
18180 {
18181 msg_putchar('[');
18182 if (*string == '[')
18183 ++string;
18184 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018185 else if (type == VAR_DICT)
18186 {
18187 msg_putchar('{');
18188 if (*string == '{')
18189 ++string;
18190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 else
18192 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018193
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018195
18196 if (type == VAR_FUNC)
18197 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018198 if (*first)
18199 {
18200 msg_clr_eos();
18201 *first = FALSE;
18202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203}
18204
18205/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018206 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 * If the variable already exists, the value is updated.
18208 * Otherwise the variable is created.
18209 */
18210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018211set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018213 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018214 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215{
Bram Moolenaar33570922005-01-25 22:26:29 +000018216 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018218 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018219 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018221 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018222 {
18223 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18224 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18225 ? name[2] : name[0]))
18226 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018227 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018228 return;
18229 }
18230 if (function_exists(name))
18231 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018232 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018233 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018234 return;
18235 }
18236 }
18237
Bram Moolenaara7043832005-01-21 11:56:39 +000018238 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018239 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018240 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018241 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018242 return;
18243 }
18244
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018245 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018246 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018248 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018249 if (var_check_ro(v->di_flags, name)
18250 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018251 return;
18252 if (v->di_tv.v_type != tv->v_type
18253 && !((v->di_tv.v_type == VAR_STRING
18254 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018255 && (tv->v_type == VAR_STRING
18256 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018257 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018258 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018259 return;
18260 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018261
18262 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018263 * Handle setting internal v: variables separately: we don't change
18264 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018265 */
18266 if (ht == &vimvarht)
18267 {
18268 if (v->di_tv.v_type == VAR_STRING)
18269 {
18270 vim_free(v->di_tv.vval.v_string);
18271 if (copy || tv->v_type != VAR_STRING)
18272 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18273 else
18274 {
18275 /* Take over the string to avoid an extra alloc/free. */
18276 v->di_tv.vval.v_string = tv->vval.v_string;
18277 tv->vval.v_string = NULL;
18278 }
18279 }
18280 else if (v->di_tv.v_type != VAR_NUMBER)
18281 EMSG2(_(e_intern2), "set_var()");
18282 else
18283 v->di_tv.vval.v_number = get_tv_number(tv);
18284 return;
18285 }
18286
18287 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 }
18289 else /* add a new variable */
18290 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018291 /* Can't add "v:" variable. */
18292 if (ht == &vimvarht)
18293 {
18294 EMSG2(_(e_illvar), name);
18295 return;
18296 }
18297
Bram Moolenaar92124a32005-06-17 22:03:40 +000018298 /* Make sure the variable name is valid. */
18299 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018300 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18301 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018302 {
18303 EMSG2(_(e_illvar), varname);
18304 return;
18305 }
18306
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018307 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18308 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018309 if (v == NULL)
18310 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018311 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018312 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018314 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 return;
18316 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018317 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018320 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018321 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018322 else
18323 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018324 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018325 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018326 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328}
18329
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018330/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018331 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018332 * Also give an error message.
18333 */
18334 static int
18335var_check_ro(flags, name)
18336 int flags;
18337 char_u *name;
18338{
18339 if (flags & DI_FLAGS_RO)
18340 {
18341 EMSG2(_(e_readonlyvar), name);
18342 return TRUE;
18343 }
18344 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18345 {
18346 EMSG2(_(e_readonlysbx), name);
18347 return TRUE;
18348 }
18349 return FALSE;
18350}
18351
18352/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018353 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18354 * Also give an error message.
18355 */
18356 static int
18357var_check_fixed(flags, name)
18358 int flags;
18359 char_u *name;
18360{
18361 if (flags & DI_FLAGS_FIX)
18362 {
18363 EMSG2(_("E795: Cannot delete variable %s"), name);
18364 return TRUE;
18365 }
18366 return FALSE;
18367}
18368
18369/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018370 * Return TRUE if typeval "tv" is set to be locked (immutable).
18371 * Also give an error message, using "name".
18372 */
18373 static int
18374tv_check_lock(lock, name)
18375 int lock;
18376 char_u *name;
18377{
18378 if (lock & VAR_LOCKED)
18379 {
18380 EMSG2(_("E741: Value is locked: %s"),
18381 name == NULL ? (char_u *)_("Unknown") : name);
18382 return TRUE;
18383 }
18384 if (lock & VAR_FIXED)
18385 {
18386 EMSG2(_("E742: Cannot change value of %s"),
18387 name == NULL ? (char_u *)_("Unknown") : name);
18388 return TRUE;
18389 }
18390 return FALSE;
18391}
18392
18393/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018394 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018395 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018396 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018399copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018400 typval_T *from;
18401 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018403 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018404 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018405 switch (from->v_type)
18406 {
18407 case VAR_NUMBER:
18408 to->vval.v_number = from->vval.v_number;
18409 break;
18410 case VAR_STRING:
18411 case VAR_FUNC:
18412 if (from->vval.v_string == NULL)
18413 to->vval.v_string = NULL;
18414 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018416 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018417 if (from->v_type == VAR_FUNC)
18418 func_ref(to->vval.v_string);
18419 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018420 break;
18421 case VAR_LIST:
18422 if (from->vval.v_list == NULL)
18423 to->vval.v_list = NULL;
18424 else
18425 {
18426 to->vval.v_list = from->vval.v_list;
18427 ++to->vval.v_list->lv_refcount;
18428 }
18429 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018430 case VAR_DICT:
18431 if (from->vval.v_dict == NULL)
18432 to->vval.v_dict = NULL;
18433 else
18434 {
18435 to->vval.v_dict = from->vval.v_dict;
18436 ++to->vval.v_dict->dv_refcount;
18437 }
18438 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018439 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018440 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018441 break;
18442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443}
18444
18445/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018446 * Make a copy of an item.
18447 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018448 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18449 * reference to an already copied list/dict can be used.
18450 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018451 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018452 static int
18453item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018454 typval_T *from;
18455 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018456 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018457 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018458{
18459 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018460 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018461
Bram Moolenaar33570922005-01-25 22:26:29 +000018462 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018463 {
18464 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018465 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018466 }
18467 ++recurse;
18468
18469 switch (from->v_type)
18470 {
18471 case VAR_NUMBER:
18472 case VAR_STRING:
18473 case VAR_FUNC:
18474 copy_tv(from, to);
18475 break;
18476 case VAR_LIST:
18477 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018478 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018479 if (from->vval.v_list == NULL)
18480 to->vval.v_list = NULL;
18481 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18482 {
18483 /* use the copy made earlier */
18484 to->vval.v_list = from->vval.v_list->lv_copylist;
18485 ++to->vval.v_list->lv_refcount;
18486 }
18487 else
18488 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18489 if (to->vval.v_list == NULL)
18490 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018491 break;
18492 case VAR_DICT:
18493 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018494 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018495 if (from->vval.v_dict == NULL)
18496 to->vval.v_dict = NULL;
18497 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18498 {
18499 /* use the copy made earlier */
18500 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18501 ++to->vval.v_dict->dv_refcount;
18502 }
18503 else
18504 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18505 if (to->vval.v_dict == NULL)
18506 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018507 break;
18508 default:
18509 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018510 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018511 }
18512 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018513 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018514}
18515
18516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 * ":echo expr1 ..." print each argument separated with a space, add a
18518 * newline at the end.
18519 * ":echon expr1 ..." print each argument plain.
18520 */
18521 void
18522ex_echo(eap)
18523 exarg_T *eap;
18524{
18525 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018526 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018527 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528 char_u *p;
18529 int needclr = TRUE;
18530 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018531 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532
18533 if (eap->skip)
18534 ++emsg_skip;
18535 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18536 {
18537 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018538 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 {
18540 /*
18541 * Report the invalid expression unless the expression evaluation
18542 * has been cancelled due to an aborting error, an interrupt, or an
18543 * exception.
18544 */
18545 if (!aborting())
18546 EMSG2(_(e_invexpr2), p);
18547 break;
18548 }
18549 if (!eap->skip)
18550 {
18551 if (atstart)
18552 {
18553 atstart = FALSE;
18554 /* Call msg_start() after eval1(), evaluating the expression
18555 * may cause a message to appear. */
18556 if (eap->cmdidx == CMD_echo)
18557 msg_start();
18558 }
18559 else if (eap->cmdidx == CMD_echo)
18560 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018561 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018562 if (p != NULL)
18563 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018565 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018567 if (*p != TAB && needclr)
18568 {
18569 /* remove any text still there from the command */
18570 msg_clr_eos();
18571 needclr = FALSE;
18572 }
18573 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 }
18575 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018576 {
18577#ifdef FEAT_MBYTE
18578 if (has_mbyte)
18579 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018580 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018581
18582 (void)msg_outtrans_len_attr(p, i, echo_attr);
18583 p += i - 1;
18584 }
18585 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018587 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018590 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018592 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 arg = skipwhite(arg);
18594 }
18595 eap->nextcmd = check_nextcmd(arg);
18596
18597 if (eap->skip)
18598 --emsg_skip;
18599 else
18600 {
18601 /* remove text that may still be there from the command */
18602 if (needclr)
18603 msg_clr_eos();
18604 if (eap->cmdidx == CMD_echo)
18605 msg_end();
18606 }
18607}
18608
18609/*
18610 * ":echohl {name}".
18611 */
18612 void
18613ex_echohl(eap)
18614 exarg_T *eap;
18615{
18616 int id;
18617
18618 id = syn_name2id(eap->arg);
18619 if (id == 0)
18620 echo_attr = 0;
18621 else
18622 echo_attr = syn_id2attr(id);
18623}
18624
18625/*
18626 * ":execute expr1 ..." execute the result of an expression.
18627 * ":echomsg expr1 ..." Print a message
18628 * ":echoerr expr1 ..." Print an error
18629 * Each gets spaces around each argument and a newline at the end for
18630 * echo commands
18631 */
18632 void
18633ex_execute(eap)
18634 exarg_T *eap;
18635{
18636 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018637 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 int ret = OK;
18639 char_u *p;
18640 garray_T ga;
18641 int len;
18642 int save_did_emsg;
18643
18644 ga_init2(&ga, 1, 80);
18645
18646 if (eap->skip)
18647 ++emsg_skip;
18648 while (*arg != NUL && *arg != '|' && *arg != '\n')
18649 {
18650 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018651 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 {
18653 /*
18654 * Report the invalid expression unless the expression evaluation
18655 * has been cancelled due to an aborting error, an interrupt, or an
18656 * exception.
18657 */
18658 if (!aborting())
18659 EMSG2(_(e_invexpr2), p);
18660 ret = FAIL;
18661 break;
18662 }
18663
18664 if (!eap->skip)
18665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 len = (int)STRLEN(p);
18668 if (ga_grow(&ga, len + 2) == FAIL)
18669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018670 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 ret = FAIL;
18672 break;
18673 }
18674 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 ga.ga_len += len;
18678 }
18679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018680 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 arg = skipwhite(arg);
18682 }
18683
18684 if (ret != FAIL && ga.ga_data != NULL)
18685 {
18686 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018689 out_flush();
18690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 else if (eap->cmdidx == CMD_echoerr)
18692 {
18693 /* We don't want to abort following commands, restore did_emsg. */
18694 save_did_emsg = did_emsg;
18695 EMSG((char_u *)ga.ga_data);
18696 if (!force_abort)
18697 did_emsg = save_did_emsg;
18698 }
18699 else if (eap->cmdidx == CMD_execute)
18700 do_cmdline((char_u *)ga.ga_data,
18701 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18702 }
18703
18704 ga_clear(&ga);
18705
18706 if (eap->skip)
18707 --emsg_skip;
18708
18709 eap->nextcmd = check_nextcmd(arg);
18710}
18711
18712/*
18713 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18714 * "arg" points to the "&" or '+' when called, to "option" when returning.
18715 * Returns NULL when no option name found. Otherwise pointer to the char
18716 * after the option name.
18717 */
18718 static char_u *
18719find_option_end(arg, opt_flags)
18720 char_u **arg;
18721 int *opt_flags;
18722{
18723 char_u *p = *arg;
18724
18725 ++p;
18726 if (*p == 'g' && p[1] == ':')
18727 {
18728 *opt_flags = OPT_GLOBAL;
18729 p += 2;
18730 }
18731 else if (*p == 'l' && p[1] == ':')
18732 {
18733 *opt_flags = OPT_LOCAL;
18734 p += 2;
18735 }
18736 else
18737 *opt_flags = 0;
18738
18739 if (!ASCII_ISALPHA(*p))
18740 return NULL;
18741 *arg = p;
18742
18743 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18744 p += 4; /* termcap option */
18745 else
18746 while (ASCII_ISALPHA(*p))
18747 ++p;
18748 return p;
18749}
18750
18751/*
18752 * ":function"
18753 */
18754 void
18755ex_function(eap)
18756 exarg_T *eap;
18757{
18758 char_u *theline;
18759 int j;
18760 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 char_u *name = NULL;
18763 char_u *p;
18764 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018765 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 garray_T newargs;
18767 garray_T newlines;
18768 int varargs = FALSE;
18769 int mustend = FALSE;
18770 int flags = 0;
18771 ufunc_T *fp;
18772 int indent;
18773 int nesting;
18774 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018775 dictitem_T *v;
18776 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018777 static int func_nr = 0; /* number for nameless function */
18778 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018779 hashtab_T *ht;
18780 int todo;
18781 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018782 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783
18784 /*
18785 * ":function" without argument: list functions.
18786 */
18787 if (ends_excmd(*eap->arg))
18788 {
18789 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018790 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018791 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018792 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018793 {
18794 if (!HASHITEM_EMPTY(hi))
18795 {
18796 --todo;
18797 fp = HI2UF(hi);
18798 if (!isdigit(*fp->uf_name))
18799 list_func_head(fp, FALSE);
18800 }
18801 }
18802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 eap->nextcmd = check_nextcmd(eap->arg);
18804 return;
18805 }
18806
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018807 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018808 * ":function /pat": list functions matching pattern.
18809 */
18810 if (*eap->arg == '/')
18811 {
18812 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18813 if (!eap->skip)
18814 {
18815 regmatch_T regmatch;
18816
18817 c = *p;
18818 *p = NUL;
18819 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18820 *p = c;
18821 if (regmatch.regprog != NULL)
18822 {
18823 regmatch.rm_ic = p_ic;
18824
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018825 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018826 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18827 {
18828 if (!HASHITEM_EMPTY(hi))
18829 {
18830 --todo;
18831 fp = HI2UF(hi);
18832 if (!isdigit(*fp->uf_name)
18833 && vim_regexec(&regmatch, fp->uf_name, 0))
18834 list_func_head(fp, FALSE);
18835 }
18836 }
18837 }
18838 }
18839 if (*p == '/')
18840 ++p;
18841 eap->nextcmd = check_nextcmd(p);
18842 return;
18843 }
18844
18845 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018846 * Get the function name. There are these situations:
18847 * func normal function name
18848 * "name" == func, "fudi.fd_dict" == NULL
18849 * dict.func new dictionary entry
18850 * "name" == NULL, "fudi.fd_dict" set,
18851 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18852 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018853 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018854 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18855 * dict.func existing dict entry that's not a Funcref
18856 * "name" == NULL, "fudi.fd_dict" set,
18857 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018860 name = trans_function_name(&p, eap->skip, 0, &fudi);
18861 paren = (vim_strchr(p, '(') != NULL);
18862 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 {
18864 /*
18865 * Return on an invalid expression in braces, unless the expression
18866 * evaluation has been cancelled due to an aborting error, an
18867 * interrupt, or an exception.
18868 */
18869 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018870 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018871 if (!eap->skip && fudi.fd_newkey != NULL)
18872 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018873 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 else
18877 eap->skip = TRUE;
18878 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018879
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 /* An error in a function call during evaluation of an expression in magic
18881 * braces should not cause the function not to be defined. */
18882 saved_did_emsg = did_emsg;
18883 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884
18885 /*
18886 * ":function func" with only function name: list function.
18887 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018888 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889 {
18890 if (!ends_excmd(*skipwhite(p)))
18891 {
18892 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018893 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894 }
18895 eap->nextcmd = check_nextcmd(p);
18896 if (eap->nextcmd != NULL)
18897 *p = NUL;
18898 if (!eap->skip && !got_int)
18899 {
18900 fp = find_func(name);
18901 if (fp != NULL)
18902 {
18903 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018904 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018906 if (FUNCLINE(fp, j) == NULL)
18907 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 msg_putchar('\n');
18909 msg_outnum((long)(j + 1));
18910 if (j < 9)
18911 msg_putchar(' ');
18912 if (j < 99)
18913 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018914 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 out_flush(); /* show a line at a time */
18916 ui_breakcheck();
18917 }
18918 if (!got_int)
18919 {
18920 msg_putchar('\n');
18921 msg_puts((char_u *)" endfunction");
18922 }
18923 }
18924 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018925 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018927 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 }
18929
18930 /*
18931 * ":function name(arg1, arg2)" Define function.
18932 */
18933 p = skipwhite(p);
18934 if (*p != '(')
18935 {
18936 if (!eap->skip)
18937 {
18938 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018939 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 }
18941 /* attempt to continue by skipping some text */
18942 if (vim_strchr(p, '(') != NULL)
18943 p = vim_strchr(p, '(');
18944 }
18945 p = skipwhite(p + 1);
18946
18947 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18948 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18949
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018950 if (!eap->skip)
18951 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018952 /* Check the name of the function. Unless it's a dictionary function
18953 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018954 if (name != NULL)
18955 arg = name;
18956 else
18957 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018958 if (arg != NULL && (fudi.fd_di == NULL
18959 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018960 {
18961 if (*arg == K_SPECIAL)
18962 j = 3;
18963 else
18964 j = 0;
18965 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18966 : eval_isnamec(arg[j])))
18967 ++j;
18968 if (arg[j] != NUL)
18969 emsg_funcname(_(e_invarg2), arg);
18970 }
18971 }
18972
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 /*
18974 * Isolate the arguments: "arg1, arg2, ...)"
18975 */
18976 while (*p != ')')
18977 {
18978 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18979 {
18980 varargs = TRUE;
18981 p += 3;
18982 mustend = TRUE;
18983 }
18984 else
18985 {
18986 arg = p;
18987 while (ASCII_ISALNUM(*p) || *p == '_')
18988 ++p;
18989 if (arg == p || isdigit(*arg)
18990 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18991 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18992 {
18993 if (!eap->skip)
18994 EMSG2(_("E125: Illegal argument: %s"), arg);
18995 break;
18996 }
18997 if (ga_grow(&newargs, 1) == FAIL)
18998 goto erret;
18999 c = *p;
19000 *p = NUL;
19001 arg = vim_strsave(arg);
19002 if (arg == NULL)
19003 goto erret;
19004 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19005 *p = c;
19006 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 if (*p == ',')
19008 ++p;
19009 else
19010 mustend = TRUE;
19011 }
19012 p = skipwhite(p);
19013 if (mustend && *p != ')')
19014 {
19015 if (!eap->skip)
19016 EMSG2(_(e_invarg2), eap->arg);
19017 break;
19018 }
19019 }
19020 ++p; /* skip the ')' */
19021
Bram Moolenaare9a41262005-01-15 22:18:47 +000019022 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 for (;;)
19024 {
19025 p = skipwhite(p);
19026 if (STRNCMP(p, "range", 5) == 0)
19027 {
19028 flags |= FC_RANGE;
19029 p += 5;
19030 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019031 else if (STRNCMP(p, "dict", 4) == 0)
19032 {
19033 flags |= FC_DICT;
19034 p += 4;
19035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 else if (STRNCMP(p, "abort", 5) == 0)
19037 {
19038 flags |= FC_ABORT;
19039 p += 5;
19040 }
19041 else
19042 break;
19043 }
19044
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019045 /* When there is a line break use what follows for the function body.
19046 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19047 if (*p == '\n')
19048 line_arg = p + 1;
19049 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 EMSG(_(e_trailing));
19051
19052 /*
19053 * Read the body of the function, until ":endfunction" is found.
19054 */
19055 if (KeyTyped)
19056 {
19057 /* Check if the function already exists, don't let the user type the
19058 * whole function before telling him it doesn't work! For a script we
19059 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019060 if (!eap->skip && !eap->forceit)
19061 {
19062 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19063 EMSG(_(e_funcdict));
19064 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019065 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019068 if (!eap->skip && did_emsg)
19069 goto erret;
19070
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 msg_putchar('\n'); /* don't overwrite the function name */
19072 cmdline_row = msg_row;
19073 }
19074
19075 indent = 2;
19076 nesting = 0;
19077 for (;;)
19078 {
19079 msg_scroll = TRUE;
19080 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019081 sourcing_lnum_off = sourcing_lnum;
19082
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019083 if (line_arg != NULL)
19084 {
19085 /* Use eap->arg, split up in parts by line breaks. */
19086 theline = line_arg;
19087 p = vim_strchr(theline, '\n');
19088 if (p == NULL)
19089 line_arg += STRLEN(line_arg);
19090 else
19091 {
19092 *p = NUL;
19093 line_arg = p + 1;
19094 }
19095 }
19096 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 theline = getcmdline(':', 0L, indent);
19098 else
19099 theline = eap->getline(':', eap->cookie, indent);
19100 if (KeyTyped)
19101 lines_left = Rows - 1;
19102 if (theline == NULL)
19103 {
19104 EMSG(_("E126: Missing :endfunction"));
19105 goto erret;
19106 }
19107
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019108 /* Detect line continuation: sourcing_lnum increased more than one. */
19109 if (sourcing_lnum > sourcing_lnum_off + 1)
19110 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19111 else
19112 sourcing_lnum_off = 0;
19113
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 if (skip_until != NULL)
19115 {
19116 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19117 * don't check for ":endfunc". */
19118 if (STRCMP(theline, skip_until) == 0)
19119 {
19120 vim_free(skip_until);
19121 skip_until = NULL;
19122 }
19123 }
19124 else
19125 {
19126 /* skip ':' and blanks*/
19127 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19128 ;
19129
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019130 /* Check for "endfunction". */
19131 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019133 if (line_arg == NULL)
19134 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 break;
19136 }
19137
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019138 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 * at "end". */
19140 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19141 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019142 else if (STRNCMP(p, "if", 2) == 0
19143 || STRNCMP(p, "wh", 2) == 0
19144 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 || STRNCMP(p, "try", 3) == 0)
19146 indent += 2;
19147
19148 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019149 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019151 if (*p == '!')
19152 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 p += eval_fname_script(p);
19154 if (ASCII_ISALPHA(*p))
19155 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019156 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 if (*skipwhite(p) == '(')
19158 {
19159 ++nesting;
19160 indent += 2;
19161 }
19162 }
19163 }
19164
19165 /* Check for ":append" or ":insert". */
19166 p = skip_range(p, NULL);
19167 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19168 || (p[0] == 'i'
19169 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19170 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19171 skip_until = vim_strsave((char_u *)".");
19172
19173 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19174 arg = skipwhite(skiptowhite(p));
19175 if (arg[0] == '<' && arg[1] =='<'
19176 && ((p[0] == 'p' && p[1] == 'y'
19177 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19178 || (p[0] == 'p' && p[1] == 'e'
19179 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19180 || (p[0] == 't' && p[1] == 'c'
19181 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19182 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19183 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019184 || (p[0] == 'm' && p[1] == 'z'
19185 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186 ))
19187 {
19188 /* ":python <<" continues until a dot, like ":append" */
19189 p = skipwhite(arg + 2);
19190 if (*p == NUL)
19191 skip_until = vim_strsave((char_u *)".");
19192 else
19193 skip_until = vim_strsave(p);
19194 }
19195 }
19196
19197 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019198 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019199 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019200 if (line_arg == NULL)
19201 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019203 }
19204
19205 /* Copy the line to newly allocated memory. get_one_sourceline()
19206 * allocates 250 bytes per line, this saves 80% on average. The cost
19207 * is an extra alloc/free. */
19208 p = vim_strsave(theline);
19209 if (p != NULL)
19210 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019211 if (line_arg == NULL)
19212 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019213 theline = p;
19214 }
19215
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019216 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19217
19218 /* Add NULL lines for continuation lines, so that the line count is
19219 * equal to the index in the growarray. */
19220 while (sourcing_lnum_off-- > 0)
19221 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019222
19223 /* Check for end of eap->arg. */
19224 if (line_arg != NULL && *line_arg == NUL)
19225 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 }
19227
19228 /* Don't define the function when skipping commands or when an error was
19229 * detected. */
19230 if (eap->skip || did_emsg)
19231 goto erret;
19232
19233 /*
19234 * If there are no errors, add the function
19235 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019236 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019237 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019238 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019239 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019240 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019241 emsg_funcname("E707: Function name conflicts with variable: %s",
19242 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019243 goto erret;
19244 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019245
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019246 fp = find_func(name);
19247 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019249 if (!eap->forceit)
19250 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019251 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019252 goto erret;
19253 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019254 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019255 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019256 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019257 name);
19258 goto erret;
19259 }
19260 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019261 ga_clear_strings(&(fp->uf_args));
19262 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019263 vim_free(name);
19264 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 }
19267 else
19268 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019269 char numbuf[20];
19270
19271 fp = NULL;
19272 if (fudi.fd_newkey == NULL && !eap->forceit)
19273 {
19274 EMSG(_(e_funcdict));
19275 goto erret;
19276 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019277 if (fudi.fd_di == NULL)
19278 {
19279 /* Can't add a function to a locked dictionary */
19280 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19281 goto erret;
19282 }
19283 /* Can't change an existing function if it is locked */
19284 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19285 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019286
19287 /* Give the function a sequential number. Can only be used with a
19288 * Funcref! */
19289 vim_free(name);
19290 sprintf(numbuf, "%d", ++func_nr);
19291 name = vim_strsave((char_u *)numbuf);
19292 if (name == NULL)
19293 goto erret;
19294 }
19295
19296 if (fp == NULL)
19297 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019298 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019299 {
19300 int slen, plen;
19301 char_u *scriptname;
19302
19303 /* Check that the autoload name matches the script name. */
19304 j = FAIL;
19305 if (sourcing_name != NULL)
19306 {
19307 scriptname = autoload_name(name);
19308 if (scriptname != NULL)
19309 {
19310 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019311 plen = (int)STRLEN(p);
19312 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019313 if (slen > plen && fnamecmp(p,
19314 sourcing_name + slen - plen) == 0)
19315 j = OK;
19316 vim_free(scriptname);
19317 }
19318 }
19319 if (j == FAIL)
19320 {
19321 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19322 goto erret;
19323 }
19324 }
19325
19326 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 if (fp == NULL)
19328 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019329
19330 if (fudi.fd_dict != NULL)
19331 {
19332 if (fudi.fd_di == NULL)
19333 {
19334 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019335 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019336 if (fudi.fd_di == NULL)
19337 {
19338 vim_free(fp);
19339 goto erret;
19340 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019341 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19342 {
19343 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019344 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019345 goto erret;
19346 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019347 }
19348 else
19349 /* overwrite existing dict entry */
19350 clear_tv(&fudi.fd_di->di_tv);
19351 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019352 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019353 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019354 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019355
19356 /* behave like "dict" was used */
19357 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019358 }
19359
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019361 STRCPY(fp->uf_name, name);
19362 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019364 fp->uf_args = newargs;
19365 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019366#ifdef FEAT_PROFILE
19367 fp->uf_tml_count = NULL;
19368 fp->uf_tml_total = NULL;
19369 fp->uf_tml_self = NULL;
19370 fp->uf_profiling = FALSE;
19371 if (prof_def_func())
19372 func_do_profile(fp);
19373#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019374 fp->uf_varargs = varargs;
19375 fp->uf_flags = flags;
19376 fp->uf_calls = 0;
19377 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019378 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379
19380erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 ga_clear_strings(&newargs);
19382 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019383ret_free:
19384 vim_free(skip_until);
19385 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388}
19389
19390/*
19391 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019392 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019394 * flags:
19395 * TFN_INT: internal function name OK
19396 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 * Advances "pp" to just after the function name (if no error).
19398 */
19399 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019400trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 char_u **pp;
19402 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019403 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019404 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019406 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 char_u *start;
19408 char_u *end;
19409 int lead;
19410 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019412 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019413
19414 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019415 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019417
19418 /* Check for hard coded <SNR>: already translated function ID (from a user
19419 * command). */
19420 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19421 && (*pp)[2] == (int)KE_SNR)
19422 {
19423 *pp += 3;
19424 len = get_id_len(pp) + 3;
19425 return vim_strnsave(start, len);
19426 }
19427
19428 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19429 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019431 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019433
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019434 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19435 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019436 if (end == start)
19437 {
19438 if (!skip)
19439 EMSG(_("E129: Function name required"));
19440 goto theend;
19441 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019442 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019443 {
19444 /*
19445 * Report an invalid expression in braces, unless the expression
19446 * evaluation has been cancelled due to an aborting error, an
19447 * interrupt, or an exception.
19448 */
19449 if (!aborting())
19450 {
19451 if (end != NULL)
19452 EMSG2(_(e_invarg2), start);
19453 }
19454 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019455 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019456 goto theend;
19457 }
19458
19459 if (lv.ll_tv != NULL)
19460 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019461 if (fdp != NULL)
19462 {
19463 fdp->fd_dict = lv.ll_dict;
19464 fdp->fd_newkey = lv.ll_newkey;
19465 lv.ll_newkey = NULL;
19466 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019467 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019468 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19469 {
19470 name = vim_strsave(lv.ll_tv->vval.v_string);
19471 *pp = end;
19472 }
19473 else
19474 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019475 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19476 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019477 EMSG(_(e_funcref));
19478 else
19479 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019480 name = NULL;
19481 }
19482 goto theend;
19483 }
19484
19485 if (lv.ll_name == NULL)
19486 {
19487 /* Error found, but continue after the function name. */
19488 *pp = end;
19489 goto theend;
19490 }
19491
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019492 /* Check if the name is a Funcref. If so, use the value. */
19493 if (lv.ll_exp_name != NULL)
19494 {
19495 len = (int)STRLEN(lv.ll_exp_name);
19496 name = deref_func_name(lv.ll_exp_name, &len);
19497 if (name == lv.ll_exp_name)
19498 name = NULL;
19499 }
19500 else
19501 {
19502 len = (int)(end - *pp);
19503 name = deref_func_name(*pp, &len);
19504 if (name == *pp)
19505 name = NULL;
19506 }
19507 if (name != NULL)
19508 {
19509 name = vim_strsave(name);
19510 *pp = end;
19511 goto theend;
19512 }
19513
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019514 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019515 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019516 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019517 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19518 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19519 {
19520 /* When there was "s:" already or the name expanded to get a
19521 * leading "s:" then remove it. */
19522 lv.ll_name += 2;
19523 len -= 2;
19524 lead = 2;
19525 }
19526 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019527 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019528 {
19529 if (lead == 2) /* skip over "s:" */
19530 lv.ll_name += 2;
19531 len = (int)(end - lv.ll_name);
19532 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019533
19534 /*
19535 * Copy the function name to allocated memory.
19536 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19537 * Accept <SNR>123_name() outside a script.
19538 */
19539 if (skip)
19540 lead = 0; /* do nothing */
19541 else if (lead > 0)
19542 {
19543 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019544 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19545 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019546 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019547 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019548 if (current_SID <= 0)
19549 {
19550 EMSG(_(e_usingsid));
19551 goto theend;
19552 }
19553 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19554 lead += (int)STRLEN(sid_buf);
19555 }
19556 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019557 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019558 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019559 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019560 goto theend;
19561 }
19562 name = alloc((unsigned)(len + lead + 1));
19563 if (name != NULL)
19564 {
19565 if (lead > 0)
19566 {
19567 name[0] = K_SPECIAL;
19568 name[1] = KS_EXTRA;
19569 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019570 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019571 STRCPY(name + 3, sid_buf);
19572 }
19573 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19574 name[len + lead] = NUL;
19575 }
19576 *pp = end;
19577
19578theend:
19579 clear_lval(&lv);
19580 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581}
19582
19583/*
19584 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19585 * Return 2 if "p" starts with "s:".
19586 * Return 0 otherwise.
19587 */
19588 static int
19589eval_fname_script(p)
19590 char_u *p;
19591{
19592 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19593 || STRNICMP(p + 1, "SNR>", 4) == 0))
19594 return 5;
19595 if (p[0] == 's' && p[1] == ':')
19596 return 2;
19597 return 0;
19598}
19599
19600/*
19601 * Return TRUE if "p" starts with "<SID>" or "s:".
19602 * Only works if eval_fname_script() returned non-zero for "p"!
19603 */
19604 static int
19605eval_fname_sid(p)
19606 char_u *p;
19607{
19608 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19609}
19610
19611/*
19612 * List the head of the function: "name(arg1, arg2)".
19613 */
19614 static void
19615list_func_head(fp, indent)
19616 ufunc_T *fp;
19617 int indent;
19618{
19619 int j;
19620
19621 msg_start();
19622 if (indent)
19623 MSG_PUTS(" ");
19624 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019625 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 {
19627 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019628 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 }
19630 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019631 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019633 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 {
19635 if (j)
19636 MSG_PUTS(", ");
19637 msg_puts(FUNCARG(fp, j));
19638 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019639 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 {
19641 if (j)
19642 MSG_PUTS(", ");
19643 MSG_PUTS("...");
19644 }
19645 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019646 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019647 if (p_verbose > 0)
19648 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649}
19650
19651/*
19652 * Find a function by name, return pointer to it in ufuncs.
19653 * Return NULL for unknown function.
19654 */
19655 static ufunc_T *
19656find_func(name)
19657 char_u *name;
19658{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019659 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019661 hi = hash_find(&func_hashtab, name);
19662 if (!HASHITEM_EMPTY(hi))
19663 return HI2UF(hi);
19664 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665}
19666
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019667#if defined(EXITFREE) || defined(PROTO)
19668 void
19669free_all_functions()
19670{
19671 hashitem_T *hi;
19672
19673 /* Need to start all over every time, because func_free() may change the
19674 * hash table. */
19675 while (func_hashtab.ht_used > 0)
19676 for (hi = func_hashtab.ht_array; ; ++hi)
19677 if (!HASHITEM_EMPTY(hi))
19678 {
19679 func_free(HI2UF(hi));
19680 break;
19681 }
19682}
19683#endif
19684
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019685/*
19686 * Return TRUE if a function "name" exists.
19687 */
19688 static int
19689function_exists(name)
19690 char_u *name;
19691{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019692 char_u *nm = name;
19693 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019694 int n = FALSE;
19695
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019696 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019697 nm = skipwhite(nm);
19698
19699 /* Only accept "funcname", "funcname ", "funcname (..." and
19700 * "funcname(...", not "funcname!...". */
19701 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019702 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019703 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019704 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019705 else
19706 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019707 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019708 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019709 return n;
19710}
19711
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019712/*
19713 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019714 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019715 */
19716 static int
19717builtin_function(name)
19718 char_u *name;
19719{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019720 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19721 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019722}
19723
Bram Moolenaar05159a02005-02-26 23:04:13 +000019724#if defined(FEAT_PROFILE) || defined(PROTO)
19725/*
19726 * Start profiling function "fp".
19727 */
19728 static void
19729func_do_profile(fp)
19730 ufunc_T *fp;
19731{
19732 fp->uf_tm_count = 0;
19733 profile_zero(&fp->uf_tm_self);
19734 profile_zero(&fp->uf_tm_total);
19735 if (fp->uf_tml_count == NULL)
19736 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19737 (sizeof(int) * fp->uf_lines.ga_len));
19738 if (fp->uf_tml_total == NULL)
19739 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19740 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19741 if (fp->uf_tml_self == NULL)
19742 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19743 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19744 fp->uf_tml_idx = -1;
19745 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19746 || fp->uf_tml_self == NULL)
19747 return; /* out of memory */
19748
19749 fp->uf_profiling = TRUE;
19750}
19751
19752/*
19753 * Dump the profiling results for all functions in file "fd".
19754 */
19755 void
19756func_dump_profile(fd)
19757 FILE *fd;
19758{
19759 hashitem_T *hi;
19760 int todo;
19761 ufunc_T *fp;
19762 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019763 ufunc_T **sorttab;
19764 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019765
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019766 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019767 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19768
Bram Moolenaar05159a02005-02-26 23:04:13 +000019769 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19770 {
19771 if (!HASHITEM_EMPTY(hi))
19772 {
19773 --todo;
19774 fp = HI2UF(hi);
19775 if (fp->uf_profiling)
19776 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019777 if (sorttab != NULL)
19778 sorttab[st_len++] = fp;
19779
Bram Moolenaar05159a02005-02-26 23:04:13 +000019780 if (fp->uf_name[0] == K_SPECIAL)
19781 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19782 else
19783 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19784 if (fp->uf_tm_count == 1)
19785 fprintf(fd, "Called 1 time\n");
19786 else
19787 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19788 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19789 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19790 fprintf(fd, "\n");
19791 fprintf(fd, "count total (s) self (s)\n");
19792
19793 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19794 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019795 if (FUNCLINE(fp, i) == NULL)
19796 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019797 prof_func_line(fd, fp->uf_tml_count[i],
19798 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019799 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19800 }
19801 fprintf(fd, "\n");
19802 }
19803 }
19804 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019805
19806 if (sorttab != NULL && st_len > 0)
19807 {
19808 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19809 prof_total_cmp);
19810 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19811 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19812 prof_self_cmp);
19813 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19814 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019815}
Bram Moolenaar73830342005-02-28 22:48:19 +000019816
19817 static void
19818prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19819 FILE *fd;
19820 ufunc_T **sorttab;
19821 int st_len;
19822 char *title;
19823 int prefer_self; /* when equal print only self time */
19824{
19825 int i;
19826 ufunc_T *fp;
19827
19828 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19829 fprintf(fd, "count total (s) self (s) function\n");
19830 for (i = 0; i < 20 && i < st_len; ++i)
19831 {
19832 fp = sorttab[i];
19833 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19834 prefer_self);
19835 if (fp->uf_name[0] == K_SPECIAL)
19836 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19837 else
19838 fprintf(fd, " %s()\n", fp->uf_name);
19839 }
19840 fprintf(fd, "\n");
19841}
19842
19843/*
19844 * Print the count and times for one function or function line.
19845 */
19846 static void
19847prof_func_line(fd, count, total, self, prefer_self)
19848 FILE *fd;
19849 int count;
19850 proftime_T *total;
19851 proftime_T *self;
19852 int prefer_self; /* when equal print only self time */
19853{
19854 if (count > 0)
19855 {
19856 fprintf(fd, "%5d ", count);
19857 if (prefer_self && profile_equal(total, self))
19858 fprintf(fd, " ");
19859 else
19860 fprintf(fd, "%s ", profile_msg(total));
19861 if (!prefer_self && profile_equal(total, self))
19862 fprintf(fd, " ");
19863 else
19864 fprintf(fd, "%s ", profile_msg(self));
19865 }
19866 else
19867 fprintf(fd, " ");
19868}
19869
19870/*
19871 * Compare function for total time sorting.
19872 */
19873 static int
19874#ifdef __BORLANDC__
19875_RTLENTRYF
19876#endif
19877prof_total_cmp(s1, s2)
19878 const void *s1;
19879 const void *s2;
19880{
19881 ufunc_T *p1, *p2;
19882
19883 p1 = *(ufunc_T **)s1;
19884 p2 = *(ufunc_T **)s2;
19885 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19886}
19887
19888/*
19889 * Compare function for self time sorting.
19890 */
19891 static int
19892#ifdef __BORLANDC__
19893_RTLENTRYF
19894#endif
19895prof_self_cmp(s1, s2)
19896 const void *s1;
19897 const void *s2;
19898{
19899 ufunc_T *p1, *p2;
19900
19901 p1 = *(ufunc_T **)s1;
19902 p2 = *(ufunc_T **)s2;
19903 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19904}
19905
Bram Moolenaar05159a02005-02-26 23:04:13 +000019906#endif
19907
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019908/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019909 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019910 * Return TRUE if a package was loaded.
19911 */
19912 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019913script_autoload(name, reload)
19914 char_u *name;
19915 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019916{
19917 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019918 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019919 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019920 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019921
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019922 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019923 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019924 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019925 return FALSE;
19926
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019927 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019928
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019929 /* Find the name in the list of previously loaded package names. Skip
19930 * "autoload/", it's always the same. */
19931 for (i = 0; i < ga_loaded.ga_len; ++i)
19932 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19933 break;
19934 if (!reload && i < ga_loaded.ga_len)
19935 ret = FALSE; /* was loaded already */
19936 else
19937 {
19938 /* Remember the name if it wasn't loaded already. */
19939 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19940 {
19941 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19942 tofree = NULL;
19943 }
19944
19945 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019946 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019947 ret = TRUE;
19948 }
19949
19950 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019951 return ret;
19952}
19953
19954/*
19955 * Return the autoload script name for a function or variable name.
19956 * Returns NULL when out of memory.
19957 */
19958 static char_u *
19959autoload_name(name)
19960 char_u *name;
19961{
19962 char_u *p;
19963 char_u *scriptname;
19964
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019965 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019966 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19967 if (scriptname == NULL)
19968 return FALSE;
19969 STRCPY(scriptname, "autoload/");
19970 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019971 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019972 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019973 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019974 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019975 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019976}
19977
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19979
19980/*
19981 * Function given to ExpandGeneric() to obtain the list of user defined
19982 * function names.
19983 */
19984 char_u *
19985get_user_func_name(xp, idx)
19986 expand_T *xp;
19987 int idx;
19988{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019989 static long_u done;
19990 static hashitem_T *hi;
19991 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992
19993 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019995 done = 0;
19996 hi = func_hashtab.ht_array;
19997 }
19998 if (done < func_hashtab.ht_used)
19999 {
20000 if (done++ > 0)
20001 ++hi;
20002 while (HASHITEM_EMPTY(hi))
20003 ++hi;
20004 fp = HI2UF(hi);
20005
20006 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20007 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008
20009 cat_func_name(IObuff, fp);
20010 if (xp->xp_context != EXPAND_USER_FUNC)
20011 {
20012 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020013 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 STRCAT(IObuff, ")");
20015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 return IObuff;
20017 }
20018 return NULL;
20019}
20020
20021#endif /* FEAT_CMDL_COMPL */
20022
20023/*
20024 * Copy the function name of "fp" to buffer "buf".
20025 * "buf" must be able to hold the function name plus three bytes.
20026 * Takes care of script-local function names.
20027 */
20028 static void
20029cat_func_name(buf, fp)
20030 char_u *buf;
20031 ufunc_T *fp;
20032{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020033 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 {
20035 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020036 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 }
20038 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020039 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040}
20041
20042/*
20043 * ":delfunction {name}"
20044 */
20045 void
20046ex_delfunction(eap)
20047 exarg_T *eap;
20048{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020049 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 char_u *p;
20051 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020052 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053
20054 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020055 name = trans_function_name(&p, eap->skip, 0, &fudi);
20056 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020058 {
20059 if (fudi.fd_dict != NULL && !eap->skip)
20060 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 if (!ends_excmd(*skipwhite(p)))
20064 {
20065 vim_free(name);
20066 EMSG(_(e_trailing));
20067 return;
20068 }
20069 eap->nextcmd = check_nextcmd(p);
20070 if (eap->nextcmd != NULL)
20071 *p = NUL;
20072
20073 if (!eap->skip)
20074 fp = find_func(name);
20075 vim_free(name);
20076
20077 if (!eap->skip)
20078 {
20079 if (fp == NULL)
20080 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020081 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 return;
20083 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020084 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 {
20086 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20087 return;
20088 }
20089
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020090 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020092 /* Delete the dict item that refers to the function, it will
20093 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020094 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020096 else
20097 func_free(fp);
20098 }
20099}
20100
20101/*
20102 * Free a function and remove it from the list of functions.
20103 */
20104 static void
20105func_free(fp)
20106 ufunc_T *fp;
20107{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020108 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020109
20110 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020111 ga_clear_strings(&(fp->uf_args));
20112 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020113#ifdef FEAT_PROFILE
20114 vim_free(fp->uf_tml_count);
20115 vim_free(fp->uf_tml_total);
20116 vim_free(fp->uf_tml_self);
20117#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020118
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020119 /* remove the function from the function hashtable */
20120 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20121 if (HASHITEM_EMPTY(hi))
20122 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020123 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020124 hash_remove(&func_hashtab, hi);
20125
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020126 vim_free(fp);
20127}
20128
20129/*
20130 * Unreference a Function: decrement the reference count and free it when it
20131 * becomes zero. Only for numbered functions.
20132 */
20133 static void
20134func_unref(name)
20135 char_u *name;
20136{
20137 ufunc_T *fp;
20138
20139 if (name != NULL && isdigit(*name))
20140 {
20141 fp = find_func(name);
20142 if (fp == NULL)
20143 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020144 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020145 {
20146 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020147 * when "uf_calls" becomes zero. */
20148 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020149 func_free(fp);
20150 }
20151 }
20152}
20153
20154/*
20155 * Count a reference to a Function.
20156 */
20157 static void
20158func_ref(name)
20159 char_u *name;
20160{
20161 ufunc_T *fp;
20162
20163 if (name != NULL && isdigit(*name))
20164 {
20165 fp = find_func(name);
20166 if (fp == NULL)
20167 EMSG2(_(e_intern2), "func_ref()");
20168 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020169 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 }
20171}
20172
20173/*
20174 * Call a user function.
20175 */
20176 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020177call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 ufunc_T *fp; /* pointer to function */
20179 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020180 typval_T *argvars; /* arguments */
20181 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 linenr_T firstline; /* first line of range */
20183 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020184 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185{
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 char_u *save_sourcing_name;
20187 linenr_T save_sourcing_lnum;
20188 scid_T save_current_SID;
20189 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020190 int save_did_emsg;
20191 static int depth = 0;
20192 dictitem_T *v;
20193 int fixvar_idx = 0; /* index in fixvar[] */
20194 int i;
20195 int ai;
20196 char_u numbuf[NUMBUFLEN];
20197 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020198#ifdef FEAT_PROFILE
20199 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020200 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202
20203 /* If depth of calling is getting too high, don't execute the function */
20204 if (depth >= p_mfd)
20205 {
20206 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020207 rettv->v_type = VAR_NUMBER;
20208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 return;
20210 }
20211 ++depth;
20212
20213 line_breakcheck(); /* check for CTRL-C hit */
20214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020215 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020216 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 fc.rettv = rettv;
20219 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 fc.linenr = 0;
20221 fc.returned = FALSE;
20222 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020224 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 fc.dbg_tick = debug_tick;
20226
Bram Moolenaar33570922005-01-25 22:26:29 +000020227 /*
20228 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20229 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20230 * each argument variable and saves a lot of time.
20231 */
20232 /*
20233 * Init l: variables.
20234 */
20235 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020236 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020237 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020238 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20239 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020240 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020241 name = v->di_key;
20242 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20244 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20245 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020246 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020247 v->di_tv.vval.v_dict = selfdict;
20248 ++selfdict->dv_refcount;
20249 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020250
Bram Moolenaar33570922005-01-25 22:26:29 +000020251 /*
20252 * Init a: variables.
20253 * Set a:0 to "argcount".
20254 * Set a:000 to a list with room for the "..." arguments.
20255 */
20256 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20257 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020258 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 v = &fc.fixvar[fixvar_idx++].var;
20260 STRCPY(v->di_key, "000");
20261 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20262 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20263 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020264 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 v->di_tv.vval.v_list = &fc.l_varlist;
20266 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20267 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020268 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020269
20270 /*
20271 * Set a:firstline to "firstline" and a:lastline to "lastline".
20272 * Set a:name to named arguments.
20273 * Set a:N to the "..." arguments.
20274 */
20275 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20276 (varnumber_T)firstline);
20277 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20278 (varnumber_T)lastline);
20279 for (i = 0; i < argcount; ++i)
20280 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020281 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020282 if (ai < 0)
20283 /* named argument a:name */
20284 name = FUNCARG(fp, i);
20285 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020286 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020287 /* "..." argument a:1, a:2, etc. */
20288 sprintf((char *)numbuf, "%d", ai + 1);
20289 name = numbuf;
20290 }
20291 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20292 {
20293 v = &fc.fixvar[fixvar_idx++].var;
20294 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20295 }
20296 else
20297 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020298 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20299 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 if (v == NULL)
20301 break;
20302 v->di_flags = DI_FLAGS_RO;
20303 }
20304 STRCPY(v->di_key, name);
20305 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20306
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020307 /* Note: the values are copied directly to avoid alloc/free.
20308 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020309 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020310 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020311
20312 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20313 {
20314 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20315 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020316 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020317 }
20318 }
20319
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 /* Don't redraw while executing the function. */
20321 ++RedrawingDisabled;
20322 save_sourcing_name = sourcing_name;
20323 save_sourcing_lnum = sourcing_lnum;
20324 sourcing_lnum = 1;
20325 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020326 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 if (sourcing_name != NULL)
20328 {
20329 if (save_sourcing_name != NULL
20330 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20331 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20332 else
20333 STRCPY(sourcing_name, "function ");
20334 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20335
20336 if (p_verbose >= 12)
20337 {
20338 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020339 verbose_enter_scroll();
20340
Bram Moolenaar555b2802005-05-19 21:08:39 +000020341 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 if (p_verbose >= 14)
20343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020345 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020346 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020347 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348
20349 msg_puts((char_u *)"(");
20350 for (i = 0; i < argcount; ++i)
20351 {
20352 if (i > 0)
20353 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020354 if (argvars[i].v_type == VAR_NUMBER)
20355 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 else
20357 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020358 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20359 if (s != NULL)
20360 {
20361 trunc_string(s, buf, MSG_BUF_CLEN);
20362 msg_puts(buf);
20363 vim_free(tofree);
20364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 }
20366 }
20367 msg_puts((char_u *)")");
20368 }
20369 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020370
20371 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 --no_wait_return;
20373 }
20374 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020375#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020376 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020377 {
20378 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20379 func_do_profile(fp);
20380 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020381 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020382 {
20383 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020384 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020385 profile_zero(&fp->uf_tm_children);
20386 }
20387 script_prof_save(&wait_start);
20388 }
20389#endif
20390
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020392 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 save_did_emsg = did_emsg;
20394 did_emsg = FALSE;
20395
20396 /* call do_cmdline() to execute the lines */
20397 do_cmdline(NULL, get_func_line, (void *)&fc,
20398 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20399
20400 --RedrawingDisabled;
20401
20402 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020403 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020405 clear_tv(rettv);
20406 rettv->v_type = VAR_NUMBER;
20407 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 }
20409
Bram Moolenaar05159a02005-02-26 23:04:13 +000020410#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020411 if (do_profiling == PROF_YES && (fp->uf_profiling
20412 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020413 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020414 profile_end(&call_start);
20415 profile_sub_wait(&wait_start, &call_start);
20416 profile_add(&fp->uf_tm_total, &call_start);
20417 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020418 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020419 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020420 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20421 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020422 }
20423 }
20424#endif
20425
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 /* when being verbose, mention the return value */
20427 if (p_verbose >= 12)
20428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020430 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020433 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020434 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020435 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20436 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020439 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020440 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020441 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020442 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020443
Bram Moolenaar555b2802005-05-19 21:08:39 +000020444 /* The value may be very long. Skip the middle part, so that we
20445 * have some idea how it starts and ends. smsg() would always
20446 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020447 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20448 if (s != NULL)
20449 {
20450 trunc_string(s, buf, MSG_BUF_CLEN);
20451 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20452 vim_free(tofree);
20453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 }
20455 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020456
20457 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 --no_wait_return;
20459 }
20460
20461 vim_free(sourcing_name);
20462 sourcing_name = save_sourcing_name;
20463 sourcing_lnum = save_sourcing_lnum;
20464 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020465#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020466 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020467 script_prof_restore(&wait_start);
20468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469
20470 if (p_verbose >= 12 && sourcing_name != NULL)
20471 {
20472 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020473 verbose_enter_scroll();
20474
Bram Moolenaar555b2802005-05-19 21:08:39 +000020475 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020477
20478 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 --no_wait_return;
20480 }
20481
20482 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020483 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484
Bram Moolenaar33570922005-01-25 22:26:29 +000020485 /* The a: variables typevals were not alloced, only free the allocated
20486 * variables. */
20487 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20488
20489 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490 --depth;
20491}
20492
20493/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020494 * Add a number variable "name" to dict "dp" with value "nr".
20495 */
20496 static void
20497add_nr_var(dp, v, name, nr)
20498 dict_T *dp;
20499 dictitem_T *v;
20500 char *name;
20501 varnumber_T nr;
20502{
20503 STRCPY(v->di_key, name);
20504 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20505 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20506 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020507 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020508 v->di_tv.vval.v_number = nr;
20509}
20510
20511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 * ":return [expr]"
20513 */
20514 void
20515ex_return(eap)
20516 exarg_T *eap;
20517{
20518 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020519 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 int returning = FALSE;
20521
20522 if (current_funccal == NULL)
20523 {
20524 EMSG(_("E133: :return not inside a function"));
20525 return;
20526 }
20527
20528 if (eap->skip)
20529 ++emsg_skip;
20530
20531 eap->nextcmd = NULL;
20532 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020533 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 {
20535 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020536 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 }
20540 /* It's safer to return also on error. */
20541 else if (!eap->skip)
20542 {
20543 /*
20544 * Return unless the expression evaluation has been cancelled due to an
20545 * aborting error, an interrupt, or an exception.
20546 */
20547 if (!aborting())
20548 returning = do_return(eap, FALSE, TRUE, NULL);
20549 }
20550
20551 /* When skipping or the return gets pending, advance to the next command
20552 * in this line (!returning). Otherwise, ignore the rest of the line.
20553 * Following lines will be ignored by get_func_line(). */
20554 if (returning)
20555 eap->nextcmd = NULL;
20556 else if (eap->nextcmd == NULL) /* no argument */
20557 eap->nextcmd = check_nextcmd(arg);
20558
20559 if (eap->skip)
20560 --emsg_skip;
20561}
20562
20563/*
20564 * Return from a function. Possibly makes the return pending. Also called
20565 * for a pending return at the ":endtry" or after returning from an extra
20566 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020567 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020568 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 * FALSE when the return gets pending.
20570 */
20571 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020572do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 exarg_T *eap;
20574 int reanimate;
20575 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020576 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577{
20578 int idx;
20579 struct condstack *cstack = eap->cstack;
20580
20581 if (reanimate)
20582 /* Undo the return. */
20583 current_funccal->returned = FALSE;
20584
20585 /*
20586 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20587 * not in its finally clause (which then is to be executed next) is found.
20588 * In this case, make the ":return" pending for execution at the ":endtry".
20589 * Otherwise, return normally.
20590 */
20591 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20592 if (idx >= 0)
20593 {
20594 cstack->cs_pending[idx] = CSTP_RETURN;
20595
20596 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020597 /* A pending return again gets pending. "rettv" points to an
20598 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020600 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 else
20602 {
20603 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020604 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020606 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020608 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 {
20610 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020612 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 else
20614 EMSG(_(e_outofmem));
20615 }
20616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020617 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618
20619 if (reanimate)
20620 {
20621 /* The pending return value could be overwritten by a ":return"
20622 * without argument in a finally clause; reset the default
20623 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020624 current_funccal->rettv->v_type = VAR_NUMBER;
20625 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 }
20627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020628 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 }
20630 else
20631 {
20632 current_funccal->returned = TRUE;
20633
20634 /* If the return is carried out now, store the return value. For
20635 * a return immediately after reanimation, the value is already
20636 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020637 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020639 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020640 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020642 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 }
20644 }
20645
20646 return idx < 0;
20647}
20648
20649/*
20650 * Free the variable with a pending return value.
20651 */
20652 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020653discard_pending_return(rettv)
20654 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655{
Bram Moolenaar33570922005-01-25 22:26:29 +000020656 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657}
20658
20659/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020660 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 * is an allocated string. Used by report_pending() for verbose messages.
20662 */
20663 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020664get_return_cmd(rettv)
20665 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020667 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020668 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020669 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020671 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020672 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020673 if (s == NULL)
20674 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020675
20676 STRCPY(IObuff, ":return ");
20677 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20678 if (STRLEN(s) + 8 >= IOSIZE)
20679 STRCPY(IObuff + IOSIZE - 4, "...");
20680 vim_free(tofree);
20681 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682}
20683
20684/*
20685 * Get next function line.
20686 * Called by do_cmdline() to get the next line.
20687 * Returns allocated string, or NULL for end of function.
20688 */
20689/* ARGSUSED */
20690 char_u *
20691get_func_line(c, cookie, indent)
20692 int c; /* not used */
20693 void *cookie;
20694 int indent; /* not used */
20695{
Bram Moolenaar33570922005-01-25 22:26:29 +000020696 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020697 ufunc_T *fp = fcp->func;
20698 char_u *retval;
20699 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700
20701 /* If breakpoints have been added/deleted need to check for it. */
20702 if (fcp->dbg_tick != debug_tick)
20703 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020704 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705 sourcing_lnum);
20706 fcp->dbg_tick = debug_tick;
20707 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020708#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020709 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020710 func_line_end(cookie);
20711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712
Bram Moolenaar05159a02005-02-26 23:04:13 +000020713 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020714 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20715 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 retval = NULL;
20717 else
20718 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020719 /* Skip NULL lines (continuation lines). */
20720 while (fcp->linenr < gap->ga_len
20721 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20722 ++fcp->linenr;
20723 if (fcp->linenr >= gap->ga_len)
20724 retval = NULL;
20725 else
20726 {
20727 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20728 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020729#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020730 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020731 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020732#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 }
20735
20736 /* Did we encounter a breakpoint? */
20737 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20738 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020739 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020741 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742 sourcing_lnum);
20743 fcp->dbg_tick = debug_tick;
20744 }
20745
20746 return retval;
20747}
20748
Bram Moolenaar05159a02005-02-26 23:04:13 +000020749#if defined(FEAT_PROFILE) || defined(PROTO)
20750/*
20751 * Called when starting to read a function line.
20752 * "sourcing_lnum" must be correct!
20753 * When skipping lines it may not actually be executed, but we won't find out
20754 * until later and we need to store the time now.
20755 */
20756 void
20757func_line_start(cookie)
20758 void *cookie;
20759{
20760 funccall_T *fcp = (funccall_T *)cookie;
20761 ufunc_T *fp = fcp->func;
20762
20763 if (fp->uf_profiling && sourcing_lnum >= 1
20764 && sourcing_lnum <= fp->uf_lines.ga_len)
20765 {
20766 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020767 /* Skip continuation lines. */
20768 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20769 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020770 fp->uf_tml_execed = FALSE;
20771 profile_start(&fp->uf_tml_start);
20772 profile_zero(&fp->uf_tml_children);
20773 profile_get_wait(&fp->uf_tml_wait);
20774 }
20775}
20776
20777/*
20778 * Called when actually executing a function line.
20779 */
20780 void
20781func_line_exec(cookie)
20782 void *cookie;
20783{
20784 funccall_T *fcp = (funccall_T *)cookie;
20785 ufunc_T *fp = fcp->func;
20786
20787 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20788 fp->uf_tml_execed = TRUE;
20789}
20790
20791/*
20792 * Called when done with a function line.
20793 */
20794 void
20795func_line_end(cookie)
20796 void *cookie;
20797{
20798 funccall_T *fcp = (funccall_T *)cookie;
20799 ufunc_T *fp = fcp->func;
20800
20801 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20802 {
20803 if (fp->uf_tml_execed)
20804 {
20805 ++fp->uf_tml_count[fp->uf_tml_idx];
20806 profile_end(&fp->uf_tml_start);
20807 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020808 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020809 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20810 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020811 }
20812 fp->uf_tml_idx = -1;
20813 }
20814}
20815#endif
20816
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817/*
20818 * Return TRUE if the currently active function should be ended, because a
20819 * return was encountered or an error occured. Used inside a ":while".
20820 */
20821 int
20822func_has_ended(cookie)
20823 void *cookie;
20824{
Bram Moolenaar33570922005-01-25 22:26:29 +000020825 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826
20827 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20828 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020829 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830 || fcp->returned);
20831}
20832
20833/*
20834 * return TRUE if cookie indicates a function which "abort"s on errors.
20835 */
20836 int
20837func_has_abort(cookie)
20838 void *cookie;
20839{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020840 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841}
20842
20843#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20844typedef enum
20845{
20846 VAR_FLAVOUR_DEFAULT,
20847 VAR_FLAVOUR_SESSION,
20848 VAR_FLAVOUR_VIMINFO
20849} var_flavour_T;
20850
20851static var_flavour_T var_flavour __ARGS((char_u *varname));
20852
20853 static var_flavour_T
20854var_flavour(varname)
20855 char_u *varname;
20856{
20857 char_u *p = varname;
20858
20859 if (ASCII_ISUPPER(*p))
20860 {
20861 while (*(++p))
20862 if (ASCII_ISLOWER(*p))
20863 return VAR_FLAVOUR_SESSION;
20864 return VAR_FLAVOUR_VIMINFO;
20865 }
20866 else
20867 return VAR_FLAVOUR_DEFAULT;
20868}
20869#endif
20870
20871#if defined(FEAT_VIMINFO) || defined(PROTO)
20872/*
20873 * Restore global vars that start with a capital from the viminfo file
20874 */
20875 int
20876read_viminfo_varlist(virp, writing)
20877 vir_T *virp;
20878 int writing;
20879{
20880 char_u *tab;
20881 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020882 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883
20884 if (!writing && (find_viminfo_parameter('!') != NULL))
20885 {
20886 tab = vim_strchr(virp->vir_line + 1, '\t');
20887 if (tab != NULL)
20888 {
20889 *tab++ = '\0'; /* isolate the variable name */
20890 if (*tab == 'S') /* string var */
20891 is_string = TRUE;
20892
20893 tab = vim_strchr(tab, '\t');
20894 if (tab != NULL)
20895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 if (is_string)
20897 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020898 tv.v_type = VAR_STRING;
20899 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 }
20902 else
20903 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020904 tv.v_type = VAR_NUMBER;
20905 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020907 set_var(virp->vir_line + 1, &tv, FALSE);
20908 if (is_string)
20909 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 }
20911 }
20912 }
20913
20914 return viminfo_readline(virp);
20915}
20916
20917/*
20918 * Write global vars that start with a capital to the viminfo file
20919 */
20920 void
20921write_viminfo_varlist(fp)
20922 FILE *fp;
20923{
Bram Moolenaar33570922005-01-25 22:26:29 +000020924 hashitem_T *hi;
20925 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020926 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020927 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020928 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020929 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020930 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931
20932 if (find_viminfo_parameter('!') == NULL)
20933 return;
20934
20935 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020936
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020937 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020938 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020940 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020942 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020943 this_var = HI2DI(hi);
20944 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020945 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020946 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020947 {
20948 case VAR_STRING: s = "STR"; break;
20949 case VAR_NUMBER: s = "NUM"; break;
20950 default: continue;
20951 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020952 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020953 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020954 if (p != NULL)
20955 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020956 vim_free(tofree);
20957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 }
20959 }
20960}
20961#endif
20962
20963#if defined(FEAT_SESSION) || defined(PROTO)
20964 int
20965store_session_globals(fd)
20966 FILE *fd;
20967{
Bram Moolenaar33570922005-01-25 22:26:29 +000020968 hashitem_T *hi;
20969 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020970 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 char_u *p, *t;
20972
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020973 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020974 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020976 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020978 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020979 this_var = HI2DI(hi);
20980 if ((this_var->di_tv.v_type == VAR_NUMBER
20981 || this_var->di_tv.v_type == VAR_STRING)
20982 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020983 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020984 /* Escape special characters with a backslash. Turn a LF and
20985 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020986 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020987 (char_u *)"\\\"\n\r");
20988 if (p == NULL) /* out of memory */
20989 break;
20990 for (t = p; *t != NUL; ++t)
20991 if (*t == '\n')
20992 *t = 'n';
20993 else if (*t == '\r')
20994 *t = 'r';
20995 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020996 this_var->di_key,
20997 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20998 : ' ',
20999 p,
21000 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21001 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021002 || put_eol(fd) == FAIL)
21003 {
21004 vim_free(p);
21005 return FAIL;
21006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 vim_free(p);
21008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 }
21010 }
21011 return OK;
21012}
21013#endif
21014
Bram Moolenaar661b1822005-07-28 22:36:45 +000021015/*
21016 * Display script name where an item was last set.
21017 * Should only be invoked when 'verbose' is non-zero.
21018 */
21019 void
21020last_set_msg(scriptID)
21021 scid_T scriptID;
21022{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021023 char_u *p;
21024
Bram Moolenaar661b1822005-07-28 22:36:45 +000021025 if (scriptID != 0)
21026 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021027 p = home_replace_save(NULL, get_scriptname(scriptID));
21028 if (p != NULL)
21029 {
21030 verbose_enter();
21031 MSG_PUTS(_("\n\tLast set from "));
21032 MSG_PUTS(p);
21033 vim_free(p);
21034 verbose_leave();
21035 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021036 }
21037}
21038
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039#endif /* FEAT_EVAL */
21040
21041#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21042
21043
21044#ifdef WIN3264
21045/*
21046 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21047 */
21048static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21049static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21050static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21051
21052/*
21053 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021054 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 */
21056 static int
21057get_short_pathname(fnamep, bufp, fnamelen)
21058 char_u **fnamep;
21059 char_u **bufp;
21060 int *fnamelen;
21061{
21062 int l,len;
21063 char_u *newbuf;
21064
21065 len = *fnamelen;
21066
21067 l = GetShortPathName(*fnamep, *fnamep, len);
21068 if (l > len - 1)
21069 {
21070 /* If that doesn't work (not enough space), then save the string
21071 * and try again with a new buffer big enough
21072 */
21073 newbuf = vim_strnsave(*fnamep, l);
21074 if (newbuf == NULL)
21075 return 0;
21076
21077 vim_free(*bufp);
21078 *fnamep = *bufp = newbuf;
21079
21080 l = GetShortPathName(*fnamep,*fnamep,l+1);
21081
21082 /* Really should always succeed, as the buffer is big enough */
21083 }
21084
21085 *fnamelen = l;
21086 return 1;
21087}
21088
21089/*
21090 * Create a short path name. Returns the length of the buffer it needs.
21091 * Doesn't copy over the end of the buffer passed in.
21092 */
21093 static int
21094shortpath_for_invalid_fname(fname, bufp, fnamelen)
21095 char_u **fname;
21096 char_u **bufp;
21097 int *fnamelen;
21098{
21099 char_u *s, *p, *pbuf2, *pbuf3;
21100 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021101 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102
21103 /* Make a copy */
21104 len2 = *fnamelen;
21105 pbuf2 = vim_strnsave(*fname, len2);
21106 pbuf3 = NULL;
21107
21108 s = pbuf2 + len2 - 1; /* Find the end */
21109 slen = 1;
21110 plen = len2;
21111
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021112 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 {
21114 --s;
21115 ++slen;
21116 --plen;
21117 }
21118
21119 do
21120 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021121 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021122 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 {
21124 --s;
21125 ++slen;
21126 --plen;
21127 }
21128 if (s <= pbuf2)
21129 break;
21130
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021131 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 ch = *s;
21133 *s = 0; /* get_short_pathname requires a null-terminated string */
21134
21135 /* Try it in situ */
21136 p = pbuf2;
21137 if (!get_short_pathname(&p, &pbuf3, &plen))
21138 {
21139 vim_free(pbuf2);
21140 return -1;
21141 }
21142 *s = ch; /* Preserve the string */
21143 } while (plen == 0);
21144
21145 if (plen > 0)
21146 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021147 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 *fnamelen = len = plen + slen;
21149 vim_free(*bufp);
21150 if (len > len2)
21151 {
21152 /* If there's not enough space in the currently allocated string,
21153 * then copy it to a buffer big enough.
21154 */
21155 *fname= *bufp = vim_strnsave(p, len);
21156 if (*fname == NULL)
21157 return -1;
21158 }
21159 else
21160 {
21161 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21162 *fname = *bufp = pbuf2;
21163 if (p != pbuf2)
21164 strncpy(*fname, p, plen);
21165 pbuf2 = NULL;
21166 }
21167 /* Concat the next bit */
21168 strncpy(*fname + plen, s, slen);
21169 (*fname)[len] = '\0';
21170 }
21171 vim_free(pbuf3);
21172 vim_free(pbuf2);
21173 return 0;
21174}
21175
21176/*
21177 * Get a pathname for a partial path.
21178 */
21179 static int
21180shortpath_for_partial(fnamep, bufp, fnamelen)
21181 char_u **fnamep;
21182 char_u **bufp;
21183 int *fnamelen;
21184{
21185 int sepcount, len, tflen;
21186 char_u *p;
21187 char_u *pbuf, *tfname;
21188 int hasTilde;
21189
21190 /* Count up the path seperators from the RHS.. so we know which part
21191 * of the path to return.
21192 */
21193 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021194 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 if (vim_ispathsep(*p))
21196 ++sepcount;
21197
21198 /* Need full path first (use expand_env() to remove a "~/") */
21199 hasTilde = (**fnamep == '~');
21200 if (hasTilde)
21201 pbuf = tfname = expand_env_save(*fnamep);
21202 else
21203 pbuf = tfname = FullName_save(*fnamep, FALSE);
21204
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021205 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206
21207 if (!get_short_pathname(&tfname, &pbuf, &len))
21208 return -1;
21209
21210 if (len == 0)
21211 {
21212 /* Don't have a valid filename, so shorten the rest of the
21213 * path if we can. This CAN give us invalid 8.3 filenames, but
21214 * there's not a lot of point in guessing what it might be.
21215 */
21216 len = tflen;
21217 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21218 return -1;
21219 }
21220
21221 /* Count the paths backward to find the beginning of the desired string. */
21222 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021223 {
21224#ifdef FEAT_MBYTE
21225 if (has_mbyte)
21226 p -= mb_head_off(tfname, p);
21227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 if (vim_ispathsep(*p))
21229 {
21230 if (sepcount == 0 || (hasTilde && sepcount == 1))
21231 break;
21232 else
21233 sepcount --;
21234 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236 if (hasTilde)
21237 {
21238 --p;
21239 if (p >= tfname)
21240 *p = '~';
21241 else
21242 return -1;
21243 }
21244 else
21245 ++p;
21246
21247 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21248 vim_free(*bufp);
21249 *fnamelen = (int)STRLEN(p);
21250 *bufp = pbuf;
21251 *fnamep = p;
21252
21253 return 0;
21254}
21255#endif /* WIN3264 */
21256
21257/*
21258 * Adjust a filename, according to a string of modifiers.
21259 * *fnamep must be NUL terminated when called. When returning, the length is
21260 * determined by *fnamelen.
21261 * Returns valid flags.
21262 * When there is an error, *fnamep is set to NULL.
21263 */
21264 int
21265modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21266 char_u *src; /* string with modifiers */
21267 int *usedlen; /* characters after src that are used */
21268 char_u **fnamep; /* file name so far */
21269 char_u **bufp; /* buffer for allocated file name or NULL */
21270 int *fnamelen; /* length of fnamep */
21271{
21272 int valid = 0;
21273 char_u *tail;
21274 char_u *s, *p, *pbuf;
21275 char_u dirname[MAXPATHL];
21276 int c;
21277 int has_fullname = 0;
21278#ifdef WIN3264
21279 int has_shortname = 0;
21280#endif
21281
21282repeat:
21283 /* ":p" - full path/file_name */
21284 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21285 {
21286 has_fullname = 1;
21287
21288 valid |= VALID_PATH;
21289 *usedlen += 2;
21290
21291 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21292 if ((*fnamep)[0] == '~'
21293#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21294 && ((*fnamep)[1] == '/'
21295# ifdef BACKSLASH_IN_FILENAME
21296 || (*fnamep)[1] == '\\'
21297# endif
21298 || (*fnamep)[1] == NUL)
21299
21300#endif
21301 )
21302 {
21303 *fnamep = expand_env_save(*fnamep);
21304 vim_free(*bufp); /* free any allocated file name */
21305 *bufp = *fnamep;
21306 if (*fnamep == NULL)
21307 return -1;
21308 }
21309
21310 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021311 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 {
21313 if (vim_ispathsep(*p)
21314 && p[1] == '.'
21315 && (p[2] == NUL
21316 || vim_ispathsep(p[2])
21317 || (p[2] == '.'
21318 && (p[3] == NUL || vim_ispathsep(p[3])))))
21319 break;
21320 }
21321
21322 /* FullName_save() is slow, don't use it when not needed. */
21323 if (*p != NUL || !vim_isAbsName(*fnamep))
21324 {
21325 *fnamep = FullName_save(*fnamep, *p != NUL);
21326 vim_free(*bufp); /* free any allocated file name */
21327 *bufp = *fnamep;
21328 if (*fnamep == NULL)
21329 return -1;
21330 }
21331
21332 /* Append a path separator to a directory. */
21333 if (mch_isdir(*fnamep))
21334 {
21335 /* Make room for one or two extra characters. */
21336 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21337 vim_free(*bufp); /* free any allocated file name */
21338 *bufp = *fnamep;
21339 if (*fnamep == NULL)
21340 return -1;
21341 add_pathsep(*fnamep);
21342 }
21343 }
21344
21345 /* ":." - path relative to the current directory */
21346 /* ":~" - path relative to the home directory */
21347 /* ":8" - shortname path - postponed till after */
21348 while (src[*usedlen] == ':'
21349 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21350 {
21351 *usedlen += 2;
21352 if (c == '8')
21353 {
21354#ifdef WIN3264
21355 has_shortname = 1; /* Postpone this. */
21356#endif
21357 continue;
21358 }
21359 pbuf = NULL;
21360 /* Need full path first (use expand_env() to remove a "~/") */
21361 if (!has_fullname)
21362 {
21363 if (c == '.' && **fnamep == '~')
21364 p = pbuf = expand_env_save(*fnamep);
21365 else
21366 p = pbuf = FullName_save(*fnamep, FALSE);
21367 }
21368 else
21369 p = *fnamep;
21370
21371 has_fullname = 0;
21372
21373 if (p != NULL)
21374 {
21375 if (c == '.')
21376 {
21377 mch_dirname(dirname, MAXPATHL);
21378 s = shorten_fname(p, dirname);
21379 if (s != NULL)
21380 {
21381 *fnamep = s;
21382 if (pbuf != NULL)
21383 {
21384 vim_free(*bufp); /* free any allocated file name */
21385 *bufp = pbuf;
21386 pbuf = NULL;
21387 }
21388 }
21389 }
21390 else
21391 {
21392 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21393 /* Only replace it when it starts with '~' */
21394 if (*dirname == '~')
21395 {
21396 s = vim_strsave(dirname);
21397 if (s != NULL)
21398 {
21399 *fnamep = s;
21400 vim_free(*bufp);
21401 *bufp = s;
21402 }
21403 }
21404 }
21405 vim_free(pbuf);
21406 }
21407 }
21408
21409 tail = gettail(*fnamep);
21410 *fnamelen = (int)STRLEN(*fnamep);
21411
21412 /* ":h" - head, remove "/file_name", can be repeated */
21413 /* Don't remove the first "/" or "c:\" */
21414 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21415 {
21416 valid |= VALID_HEAD;
21417 *usedlen += 2;
21418 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021419 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021420 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 *fnamelen = (int)(tail - *fnamep);
21422#ifdef VMS
21423 if (*fnamelen > 0)
21424 *fnamelen += 1; /* the path separator is part of the path */
21425#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021426 if (*fnamelen == 0)
21427 {
21428 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21429 p = vim_strsave((char_u *)".");
21430 if (p == NULL)
21431 return -1;
21432 vim_free(*bufp);
21433 *bufp = *fnamep = tail = p;
21434 *fnamelen = 1;
21435 }
21436 else
21437 {
21438 while (tail > s && !after_pathsep(s, tail))
21439 mb_ptr_back(*fnamep, tail);
21440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 }
21442
21443 /* ":8" - shortname */
21444 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21445 {
21446 *usedlen += 2;
21447#ifdef WIN3264
21448 has_shortname = 1;
21449#endif
21450 }
21451
21452#ifdef WIN3264
21453 /* Check shortname after we have done 'heads' and before we do 'tails'
21454 */
21455 if (has_shortname)
21456 {
21457 pbuf = NULL;
21458 /* Copy the string if it is shortened by :h */
21459 if (*fnamelen < (int)STRLEN(*fnamep))
21460 {
21461 p = vim_strnsave(*fnamep, *fnamelen);
21462 if (p == 0)
21463 return -1;
21464 vim_free(*bufp);
21465 *bufp = *fnamep = p;
21466 }
21467
21468 /* Split into two implementations - makes it easier. First is where
21469 * there isn't a full name already, second is where there is.
21470 */
21471 if (!has_fullname && !vim_isAbsName(*fnamep))
21472 {
21473 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21474 return -1;
21475 }
21476 else
21477 {
21478 int l;
21479
21480 /* Simple case, already have the full-name
21481 * Nearly always shorter, so try first time. */
21482 l = *fnamelen;
21483 if (!get_short_pathname(fnamep, bufp, &l))
21484 return -1;
21485
21486 if (l == 0)
21487 {
21488 /* Couldn't find the filename.. search the paths.
21489 */
21490 l = *fnamelen;
21491 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21492 return -1;
21493 }
21494 *fnamelen = l;
21495 }
21496 }
21497#endif /* WIN3264 */
21498
21499 /* ":t" - tail, just the basename */
21500 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21501 {
21502 *usedlen += 2;
21503 *fnamelen -= (int)(tail - *fnamep);
21504 *fnamep = tail;
21505 }
21506
21507 /* ":e" - extension, can be repeated */
21508 /* ":r" - root, without extension, can be repeated */
21509 while (src[*usedlen] == ':'
21510 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21511 {
21512 /* find a '.' in the tail:
21513 * - for second :e: before the current fname
21514 * - otherwise: The last '.'
21515 */
21516 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21517 s = *fnamep - 2;
21518 else
21519 s = *fnamep + *fnamelen - 1;
21520 for ( ; s > tail; --s)
21521 if (s[0] == '.')
21522 break;
21523 if (src[*usedlen + 1] == 'e') /* :e */
21524 {
21525 if (s > tail)
21526 {
21527 *fnamelen += (int)(*fnamep - (s + 1));
21528 *fnamep = s + 1;
21529#ifdef VMS
21530 /* cut version from the extension */
21531 s = *fnamep + *fnamelen - 1;
21532 for ( ; s > *fnamep; --s)
21533 if (s[0] == ';')
21534 break;
21535 if (s > *fnamep)
21536 *fnamelen = s - *fnamep;
21537#endif
21538 }
21539 else if (*fnamep <= tail)
21540 *fnamelen = 0;
21541 }
21542 else /* :r */
21543 {
21544 if (s > tail) /* remove one extension */
21545 *fnamelen = (int)(s - *fnamep);
21546 }
21547 *usedlen += 2;
21548 }
21549
21550 /* ":s?pat?foo?" - substitute */
21551 /* ":gs?pat?foo?" - global substitute */
21552 if (src[*usedlen] == ':'
21553 && (src[*usedlen + 1] == 's'
21554 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21555 {
21556 char_u *str;
21557 char_u *pat;
21558 char_u *sub;
21559 int sep;
21560 char_u *flags;
21561 int didit = FALSE;
21562
21563 flags = (char_u *)"";
21564 s = src + *usedlen + 2;
21565 if (src[*usedlen + 1] == 'g')
21566 {
21567 flags = (char_u *)"g";
21568 ++s;
21569 }
21570
21571 sep = *s++;
21572 if (sep)
21573 {
21574 /* find end of pattern */
21575 p = vim_strchr(s, sep);
21576 if (p != NULL)
21577 {
21578 pat = vim_strnsave(s, (int)(p - s));
21579 if (pat != NULL)
21580 {
21581 s = p + 1;
21582 /* find end of substitution */
21583 p = vim_strchr(s, sep);
21584 if (p != NULL)
21585 {
21586 sub = vim_strnsave(s, (int)(p - s));
21587 str = vim_strnsave(*fnamep, *fnamelen);
21588 if (sub != NULL && str != NULL)
21589 {
21590 *usedlen = (int)(p + 1 - src);
21591 s = do_string_sub(str, pat, sub, flags);
21592 if (s != NULL)
21593 {
21594 *fnamep = s;
21595 *fnamelen = (int)STRLEN(s);
21596 vim_free(*bufp);
21597 *bufp = s;
21598 didit = TRUE;
21599 }
21600 }
21601 vim_free(sub);
21602 vim_free(str);
21603 }
21604 vim_free(pat);
21605 }
21606 }
21607 /* after using ":s", repeat all the modifiers */
21608 if (didit)
21609 goto repeat;
21610 }
21611 }
21612
21613 return valid;
21614}
21615
21616/*
21617 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21618 * "flags" can be "g" to do a global substitute.
21619 * Returns an allocated string, NULL for error.
21620 */
21621 char_u *
21622do_string_sub(str, pat, sub, flags)
21623 char_u *str;
21624 char_u *pat;
21625 char_u *sub;
21626 char_u *flags;
21627{
21628 int sublen;
21629 regmatch_T regmatch;
21630 int i;
21631 int do_all;
21632 char_u *tail;
21633 garray_T ga;
21634 char_u *ret;
21635 char_u *save_cpo;
21636
21637 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21638 save_cpo = p_cpo;
21639 p_cpo = (char_u *)"";
21640
21641 ga_init2(&ga, 1, 200);
21642
21643 do_all = (flags[0] == 'g');
21644
21645 regmatch.rm_ic = p_ic;
21646 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21647 if (regmatch.regprog != NULL)
21648 {
21649 tail = str;
21650 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21651 {
21652 /*
21653 * Get some space for a temporary buffer to do the substitution
21654 * into. It will contain:
21655 * - The text up to where the match is.
21656 * - The substituted text.
21657 * - The text after the match.
21658 */
21659 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21660 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21661 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21662 {
21663 ga_clear(&ga);
21664 break;
21665 }
21666
21667 /* copy the text up to where the match is */
21668 i = (int)(regmatch.startp[0] - tail);
21669 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21670 /* add the substituted text */
21671 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21672 + ga.ga_len + i, TRUE, TRUE, FALSE);
21673 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 /* avoid getting stuck on a match with an empty string */
21675 if (tail == regmatch.endp[0])
21676 {
21677 if (*tail == NUL)
21678 break;
21679 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21680 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 }
21682 else
21683 {
21684 tail = regmatch.endp[0];
21685 if (*tail == NUL)
21686 break;
21687 }
21688 if (!do_all)
21689 break;
21690 }
21691
21692 if (ga.ga_data != NULL)
21693 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21694
21695 vim_free(regmatch.regprog);
21696 }
21697
21698 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21699 ga_clear(&ga);
21700 p_cpo = save_cpo;
21701
21702 return ret;
21703}
21704
21705#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */