blob: 740b8dd41ff5d230b989114436ddc08fca3bf74f [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 Moolenaar33570922005-01-25 22:26:29 +0000348};
349
350/* shorthand */
351#define vv_type vv_di.di_tv.v_type
352#define vv_nr vv_di.di_tv.vval.v_number
353#define vv_str vv_di.di_tv.vval.v_string
354#define vv_tv vv_di.di_tv
355
356/*
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
359 */
360static dict_T vimvardict;
361static dictitem_T vimvars_var;
362#define vimvarht vimvardict.dv_hashtab
363
Bram Moolenaara40058a2005-07-11 22:42:07 +0000364static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
365static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
366#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
368#endif
369static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
370static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
371static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
373static void list_glob_vars __ARGS((int *first));
374static void list_buf_vars __ARGS((int *first));
375static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000377static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000378#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_vim_vars __ARGS((int *first));
380static void list_script_vars __ARGS((int *first));
381static void list_func_vars __ARGS((int *first));
382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
384static int check_changedtick __ARGS((char_u *arg));
385static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
386static void clear_lval __ARGS((lval_T *lp));
387static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
388static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
389static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
390static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
391static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
392static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
393static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
394static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
395static void item_lock __ARGS((typval_T *tv, int deep, int lock));
396static int tv_islocked __ARGS((typval_T *tv));
397
Bram Moolenaar33570922005-01-25 22:26:29 +0000398static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
399static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000407static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000412static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *listitem_alloc __ARGS((void));
414static void listitem_free __ARGS((listitem_T *item));
415static void listitem_remove __ARGS((list_T *l, listitem_T *item));
416static long list_len __ARGS((list_T *l));
417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000421static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void list_append __ARGS((list_T *l, listitem_T *item));
424static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_string __ARGS((list_T *l, char_u *str, int len));
426static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000434static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
435static void set_ref_in_list __ARGS((list_T *l, int copyID));
436static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000438static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_alloc __ARGS((char_u *key));
440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
442static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int dict_add __ARGS((dict_T *d, dictitem_T *item));
445static long dict_len __ARGS((dict_T *d));
446static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
450static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static char_u *string_quote __ARGS((char_u *str, int function));
452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
454static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
455static 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));
456static 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 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
459static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000475static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000478static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000480#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000481static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000482static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
490static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000503static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000517static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000519static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000525static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000533static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000534static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000535static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000538static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000546static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000560static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000566static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000582static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000583static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000584static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000586static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000590#ifdef vim_mkdir
591static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
592#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000596static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000598static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000599static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000601static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000602static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000604static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000615static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000617static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000624static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000626static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000627static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000629static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000631static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000634static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000638static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639#ifdef HAVE_STRFTIME
640static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
642static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000654static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000655static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000656static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000657static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000658static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000660static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000674static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000677static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000679static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000680static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static int get_env_len __ARGS((char_u **arg));
682static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000683static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000684static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
685#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
686#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
687 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000688static 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 +0000689static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000690static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000691static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
692static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static typval_T *alloc_tv __ARGS((void));
694static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void init_tv __ARGS((typval_T *varp));
696static long get_tv_number __ARGS((typval_T *varp));
697static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000698static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static char_u *get_tv_string __ARGS((typval_T *varp));
700static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000701static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000703static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
705static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
706static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000707static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
708static 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 +0000709static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
710static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000711static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000712static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000714static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
716static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
717static int eval_fname_script __ARGS((char_u *p));
718static int eval_fname_sid __ARGS((char_u *p));
719static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static ufunc_T *find_func __ARGS((char_u *name));
721static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000722static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000723#ifdef FEAT_PROFILE
724static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000725static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
726static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
727static int
728# ifdef __BORLANDC__
729 _RTLENTRYF
730# endif
731 prof_total_cmp __ARGS((const void *s1, const void *s2));
732static int
733# ifdef __BORLANDC__
734 _RTLENTRYF
735# endif
736 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000737#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000738static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000739static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000740static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void func_free __ARGS((ufunc_T *fp));
742static void func_unref __ARGS((char_u *name));
743static void func_ref __ARGS((char_u *name));
744static 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));
745static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000746static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
747static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000748static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000749static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000750static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000752/* Character used as separated in autoload function/variable names. */
753#define AUTOLOAD_CHAR '#'
754
Bram Moolenaar33570922005-01-25 22:26:29 +0000755/*
756 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000757 */
758 void
759eval_init()
760{
Bram Moolenaar33570922005-01-25 22:26:29 +0000761 int i;
762 struct vimvar *p;
763
764 init_var_dict(&globvardict, &globvars_var);
765 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000766 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000767 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768
769 for (i = 0; i < VV_LEN; ++i)
770 {
771 p = &vimvars[i];
772 STRCPY(p->vv_di.di_key, p->vv_name);
773 if (p->vv_flags & VV_RO)
774 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
775 else if (p->vv_flags & VV_RO_SBX)
776 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
777 else
778 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779
780 /* add to v: scope dict, unless the value is not always available */
781 if (p->vv_type != VAR_UNKNOWN)
782 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000784 /* add to compat scope dict */
785 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000787}
788
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000789#if defined(EXITFREE) || defined(PROTO)
790 void
791eval_clear()
792{
793 int i;
794 struct vimvar *p;
795
796 for (i = 0; i < VV_LEN; ++i)
797 {
798 p = &vimvars[i];
799 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000800 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000801 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000802 p->vv_di.di_tv.vval.v_string = NULL;
803 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000804 }
805 hash_clear(&vimvarht);
806 hash_clear(&compat_hashtab);
807
808 /* script-local variables */
809 for (i = 1; i <= ga_scripts.ga_len; ++i)
810 vars_clear(&SCRIPT_VARS(i));
811 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000812 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000813
814 /* global variables */
815 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000816
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000817 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000818 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 hash_clear(&func_hashtab);
820
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000821 /* autoloaded script names */
822 ga_clear_strings(&ga_loaded);
823
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000824 /* unreferenced lists and dicts */
825 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000826}
827#endif
828
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * Return the name of the executed function.
831 */
832 char_u *
833func_name(cookie)
834 void *cookie;
835{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000836 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837}
838
839/*
840 * Return the address holding the next breakpoint line for a funccall cookie.
841 */
842 linenr_T *
843func_breakpoint(cookie)
844 void *cookie;
845{
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847}
848
849/*
850 * Return the address holding the debug tick for a funccall cookie.
851 */
852 int *
853func_dbg_tick(cookie)
854 void *cookie;
855{
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
859/*
860 * Return the nesting level for a funccall cookie.
861 */
862 int
863func_level(cookie)
864 void *cookie;
865{
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867}
868
869/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000870funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872/*
873 * Return TRUE when a function was ended by a ":return" command.
874 */
875 int
876current_func_returned()
877{
878 return current_funccal->returned;
879}
880
881
882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * Set an internal variable to a string value. Creates the variable if it does
884 * not already exist.
885 */
886 void
887set_internal_string_var(name, value)
888 char_u *name;
889 char_u *value;
890{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000891 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894 val = vim_strsave(value);
895 if (val != NULL)
896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000897 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000898 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000900 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000901 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903 }
904}
905
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000906static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000907static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908static char_u *redir_endp = NULL;
909static char_u *redir_varname = NULL;
910
911/*
912 * Start recording command output to a variable
913 * Returns OK if successfully completed the setup. FAIL otherwise.
914 */
915 int
916var_redir_start(name, append)
917 char_u *name;
918 int append; /* append to an existing variable */
919{
920 int save_emsg;
921 int err;
922 typval_T tv;
923
924 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000925 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000926 {
927 EMSG(_(e_invarg));
928 return FAIL;
929 }
930
931 redir_varname = vim_strsave(name);
932 if (redir_varname == NULL)
933 return FAIL;
934
935 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
936 if (redir_lval == NULL)
937 {
938 var_redir_stop();
939 return FAIL;
940 }
941
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000942 /* The output is stored in growarray "redir_ga" until redirection ends. */
943 ga_init2(&redir_ga, (int)sizeof(char), 500);
944
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000946 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
947 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000948 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
949 {
950 if (redir_endp != NULL && *redir_endp != NUL)
951 /* Trailing characters are present after the variable name */
952 EMSG(_(e_trailing));
953 else
954 EMSG(_(e_invarg));
955 var_redir_stop();
956 return FAIL;
957 }
958
959 /* check if we can write to the variable: set it to or append an empty
960 * string */
961 save_emsg = did_emsg;
962 did_emsg = FALSE;
963 tv.v_type = VAR_STRING;
964 tv.vval.v_string = (char_u *)"";
965 if (append)
966 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
967 else
968 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
969 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000970 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000971 if (err)
972 {
973 var_redir_stop();
974 return FAIL;
975 }
976 if (redir_lval->ll_newkey != NULL)
977 {
978 /* Dictionary item was created, don't do it again. */
979 vim_free(redir_lval->ll_newkey);
980 redir_lval->ll_newkey = NULL;
981 }
982
983 return OK;
984}
985
986/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000987 * Append "value[value_len]" to the variable set by var_redir_start().
988 * The actual appending is postponed until redirection ends, because the value
989 * appended may in fact be the string we write to, changing it may cause freed
990 * memory to be used:
991 * :redir => foo
992 * :let foo
993 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994 */
995 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000996var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000997 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000998 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001000 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001
1002 if (redir_lval == NULL)
1003 return;
1004
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001005 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001006 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001008 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001010 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001011 {
1012 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001013 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001014 }
1015 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017}
1018
1019/*
1020 * Stop redirecting command output to a variable.
1021 */
1022 void
1023var_redir_stop()
1024{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001025 typval_T tv;
1026
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027 if (redir_lval != NULL)
1028 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001029 /* Append the trailing NUL. */
1030 ga_append(&redir_ga, NUL);
1031
1032 /* Assign the text to the variable. */
1033 tv.v_type = VAR_STRING;
1034 tv.vval.v_string = redir_ga.ga_data;
1035 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1036 vim_free(tv.vval.v_string);
1037
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 clear_lval(redir_lval);
1039 vim_free(redir_lval);
1040 redir_lval = NULL;
1041 }
1042 vim_free(redir_varname);
1043 redir_varname = NULL;
1044}
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046# if defined(FEAT_MBYTE) || defined(PROTO)
1047 int
1048eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1049 char_u *enc_from;
1050 char_u *enc_to;
1051 char_u *fname_from;
1052 char_u *fname_to;
1053{
1054 int err = FALSE;
1055
1056 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1057 set_vim_var_string(VV_CC_TO, enc_to, -1);
1058 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1059 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1060 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1061 err = TRUE;
1062 set_vim_var_string(VV_CC_FROM, NULL, -1);
1063 set_vim_var_string(VV_CC_TO, NULL, -1);
1064 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1065 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1066
1067 if (err)
1068 return FAIL;
1069 return OK;
1070}
1071# endif
1072
1073# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1074 int
1075eval_printexpr(fname, args)
1076 char_u *fname;
1077 char_u *args;
1078{
1079 int err = FALSE;
1080
1081 set_vim_var_string(VV_FNAME_IN, fname, -1);
1082 set_vim_var_string(VV_CMDARG, args, -1);
1083 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1084 err = TRUE;
1085 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1086 set_vim_var_string(VV_CMDARG, NULL, -1);
1087
1088 if (err)
1089 {
1090 mch_remove(fname);
1091 return FAIL;
1092 }
1093 return OK;
1094}
1095# endif
1096
1097# if defined(FEAT_DIFF) || defined(PROTO)
1098 void
1099eval_diff(origfile, newfile, outfile)
1100 char_u *origfile;
1101 char_u *newfile;
1102 char_u *outfile;
1103{
1104 int err = FALSE;
1105
1106 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1107 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1108 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1109 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1112 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1113}
1114
1115 void
1116eval_patch(origfile, difffile, outfile)
1117 char_u *origfile;
1118 char_u *difffile;
1119 char_u *outfile;
1120{
1121 int err;
1122
1123 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1124 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1125 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1126 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1127 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1128 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1129 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1130}
1131# endif
1132
1133/*
1134 * Top level evaluation function, returning a boolean.
1135 * Sets "error" to TRUE if there was an error.
1136 * Return TRUE or FALSE.
1137 */
1138 int
1139eval_to_bool(arg, error, nextcmd, skip)
1140 char_u *arg;
1141 int *error;
1142 char_u **nextcmd;
1143 int skip; /* only parse, don't execute */
1144{
Bram Moolenaar33570922005-01-25 22:26:29 +00001145 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 int retval = FALSE;
1147
1148 if (skip)
1149 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 else
1153 {
1154 *error = FALSE;
1155 if (!skip)
1156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001157 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160 }
1161 if (skip)
1162 --emsg_skip;
1163
1164 return retval;
1165}
1166
1167/*
1168 * Top level evaluation function, returning a string. If "skip" is TRUE,
1169 * only parsing to "nextcmd" is done, without reporting errors. Return
1170 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1171 */
1172 char_u *
1173eval_to_string_skip(arg, nextcmd, skip)
1174 char_u *arg;
1175 char_u **nextcmd;
1176 int skip; /* only parse, don't execute */
1177{
Bram Moolenaar33570922005-01-25 22:26:29 +00001178 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 char_u *retval;
1180
1181 if (skip)
1182 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 retval = NULL;
1185 else
1186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187 retval = vim_strsave(get_tv_string(&tv));
1188 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 if (skip)
1191 --emsg_skip;
1192
1193 return retval;
1194}
1195
1196/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001197 * Skip over an expression at "*pp".
1198 * Return FAIL for an error, OK otherwise.
1199 */
1200 int
1201skip_expr(pp)
1202 char_u **pp;
1203{
Bram Moolenaar33570922005-01-25 22:26:29 +00001204 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001205
1206 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001208}
1209
1210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 * Top level evaluation function, returning a string.
1212 * Return pointer to allocated memory, or NULL for failure.
1213 */
1214 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001215eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *arg;
1217 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001218 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001222 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 retval = NULL;
1226 else
1227 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001228 if (dolist && tv.v_type == VAR_LIST)
1229 {
1230 ga_init2(&ga, (int)sizeof(char), 80);
1231 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1232 ga_append(&ga, NUL);
1233 retval = (char_u *)ga.ga_data;
1234 }
1235 else
1236 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239
1240 return retval;
1241}
1242
1243/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001244 * Call eval_to_string() without using current local variables and using
1245 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 */
1247 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001248eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 char_u *arg;
1250 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001251 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 char_u *retval;
1254 void *save_funccalp;
1255
1256 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001257 if (use_sandbox)
1258 ++sandbox;
1259 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001260 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001261 if (use_sandbox)
1262 --sandbox;
1263 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 restore_funccal(save_funccalp);
1265 return retval;
1266}
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268/*
1269 * Top level evaluation function, returning a number.
1270 * Evaluates "expr" silently.
1271 * Returns -1 for an error.
1272 */
1273 int
1274eval_to_number(expr)
1275 char_u *expr;
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001279 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 ++emsg_off;
1282
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 retval = -1;
1285 else
1286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001287 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 --emsg_off;
1291
1292 return retval;
1293}
1294
Bram Moolenaara40058a2005-07-11 22:42:07 +00001295/*
1296 * Prepare v: variable "idx" to be used.
1297 * Save the current typeval in "save_tv".
1298 * When not used yet add the variable to the v: hashtable.
1299 */
1300 static void
1301prepare_vimvar(idx, save_tv)
1302 int idx;
1303 typval_T *save_tv;
1304{
1305 *save_tv = vimvars[idx].vv_tv;
1306 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1307 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1308}
1309
1310/*
1311 * Restore v: variable "idx" to typeval "save_tv".
1312 * When no longer defined, remove the variable from the v: hashtable.
1313 */
1314 static void
1315restore_vimvar(idx, save_tv)
1316 int idx;
1317 typval_T *save_tv;
1318{
1319 hashitem_T *hi;
1320
Bram Moolenaara40058a2005-07-11 22:42:07 +00001321 vimvars[idx].vv_tv = *save_tv;
1322 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1323 {
1324 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1325 if (HASHITEM_EMPTY(hi))
1326 EMSG2(_(e_intern2), "restore_vimvar()");
1327 else
1328 hash_remove(&vimvarht, hi);
1329 }
1330}
1331
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001332#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001333/*
1334 * Evaluate an expression to a list with suggestions.
1335 * For the "expr:" part of 'spellsuggest'.
1336 */
1337 list_T *
1338eval_spell_expr(badword, expr)
1339 char_u *badword;
1340 char_u *expr;
1341{
1342 typval_T save_val;
1343 typval_T rettv;
1344 list_T *list = NULL;
1345 char_u *p = skipwhite(expr);
1346
1347 /* Set "v:val" to the bad word. */
1348 prepare_vimvar(VV_VAL, &save_val);
1349 vimvars[VV_VAL].vv_type = VAR_STRING;
1350 vimvars[VV_VAL].vv_str = badword;
1351 if (p_verbose == 0)
1352 ++emsg_off;
1353
1354 if (eval1(&p, &rettv, TRUE) == OK)
1355 {
1356 if (rettv.v_type != VAR_LIST)
1357 clear_tv(&rettv);
1358 else
1359 list = rettv.vval.v_list;
1360 }
1361
1362 if (p_verbose == 0)
1363 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001364 restore_vimvar(VV_VAL, &save_val);
1365
1366 return list;
1367}
1368
1369/*
1370 * "list" is supposed to contain two items: a word and a number. Return the
1371 * word in "pp" and the number as the return value.
1372 * Return -1 if anything isn't right.
1373 * Used to get the good word and score from the eval_spell_expr() result.
1374 */
1375 int
1376get_spellword(list, pp)
1377 list_T *list;
1378 char_u **pp;
1379{
1380 listitem_T *li;
1381
1382 li = list->lv_first;
1383 if (li == NULL)
1384 return -1;
1385 *pp = get_tv_string(&li->li_tv);
1386
1387 li = li->li_next;
1388 if (li == NULL)
1389 return -1;
1390 return get_tv_number(&li->li_tv);
1391}
1392#endif
1393
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001394/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001395 * Top level evaluation function.
1396 * Returns an allocated typval_T with the result.
1397 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001398 */
1399 typval_T *
1400eval_expr(arg, nextcmd)
1401 char_u *arg;
1402 char_u **nextcmd;
1403{
1404 typval_T *tv;
1405
1406 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001407 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001408 {
1409 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001410 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001411 }
1412
1413 return tv;
1414}
1415
1416
Bram Moolenaar4f688582007-07-24 12:34:30 +00001417#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1418 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 static int
1425call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 char_u *func;
1427 int argc;
1428 char_u **argv;
1429 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 long n;
1434 int len;
1435 int i;
1436 int doesrange;
1437 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001440 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
1444 for (i = 0; i < argc; i++)
1445 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001446 /* Pass a NULL or empty argument as an empty string */
1447 if (argv[i] == NULL || *argv[i] == NUL)
1448 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001449 argvars[i].v_type = VAR_STRING;
1450 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001451 continue;
1452 }
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 /* Recognize a number argument, the others must be strings. */
1455 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1456 if (len != 0 && len == (int)STRLEN(argv[i]))
1457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001458 argvars[i].v_type = VAR_NUMBER;
1459 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461 else
1462 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001463 argvars[i].v_type = VAR_STRING;
1464 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
1466 }
1467
1468 if (safe)
1469 {
1470 save_funccalp = save_funccal();
1471 ++sandbox;
1472 }
1473
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001474 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1475 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (safe)
1479 {
1480 --sandbox;
1481 restore_funccal(save_funccalp);
1482 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001483 vim_free(argvars);
1484
1485 if (ret == FAIL)
1486 clear_tv(rettv);
1487
1488 return ret;
1489}
1490
Bram Moolenaar4f688582007-07-24 12:34:30 +00001491# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001492/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001493 * Call vimL function "func" and return the result as a string.
1494 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001495 * Uses argv[argc] for the function arguments.
1496 */
1497 void *
1498call_func_retstr(func, argc, argv, safe)
1499 char_u *func;
1500 int argc;
1501 char_u **argv;
1502 int safe; /* use the sandbox */
1503{
1504 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001505 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001506
1507 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1508 return NULL;
1509
1510 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001511 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 return retval;
1513}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001514# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001515
Bram Moolenaar4f688582007-07-24 12:34:30 +00001516# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001517/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001518 * Call vimL function "func" and return the result as a number.
1519 * Returns -1 when calling the function fails.
1520 * Uses argv[argc] for the function arguments.
1521 */
1522 long
1523call_func_retnr(func, argc, argv, safe)
1524 char_u *func;
1525 int argc;
1526 char_u **argv;
1527 int safe; /* use the sandbox */
1528{
1529 typval_T rettv;
1530 long retval;
1531
1532 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1533 return -1;
1534
1535 retval = get_tv_number_chk(&rettv, NULL);
1536 clear_tv(&rettv);
1537 return retval;
1538}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001539# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001540
1541/*
1542 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001543 * Uses argv[argc] for the function arguments.
1544 */
1545 void *
1546call_func_retlist(func, argc, argv, safe)
1547 char_u *func;
1548 int argc;
1549 char_u **argv;
1550 int safe; /* use the sandbox */
1551{
1552 typval_T rettv;
1553
1554 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1555 return NULL;
1556
1557 if (rettv.v_type != VAR_LIST)
1558 {
1559 clear_tv(&rettv);
1560 return NULL;
1561 }
1562
1563 return rettv.vval.v_list;
1564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#endif
1566
Bram Moolenaar4f688582007-07-24 12:34:30 +00001567
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568/*
1569 * Save the current function call pointer, and set it to NULL.
1570 * Used when executing autocommands and for ":source".
1571 */
1572 void *
1573save_funccal()
1574{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001575 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 current_funccal = NULL;
1578 return (void *)fc;
1579}
1580
1581 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001582restore_funccal(vfc)
1583 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001585 funccall_T *fc = (funccall_T *)vfc;
1586
1587 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588}
1589
Bram Moolenaar05159a02005-02-26 23:04:13 +00001590#if defined(FEAT_PROFILE) || defined(PROTO)
1591/*
1592 * Prepare profiling for entering a child or something else that is not
1593 * counted for the script/function itself.
1594 * Should always be called in pair with prof_child_exit().
1595 */
1596 void
1597prof_child_enter(tm)
1598 proftime_T *tm; /* place to store waittime */
1599{
1600 funccall_T *fc = current_funccal;
1601
1602 if (fc != NULL && fc->func->uf_profiling)
1603 profile_start(&fc->prof_child);
1604 script_prof_save(tm);
1605}
1606
1607/*
1608 * Take care of time spent in a child.
1609 * Should always be called after prof_child_enter().
1610 */
1611 void
1612prof_child_exit(tm)
1613 proftime_T *tm; /* where waittime was stored */
1614{
1615 funccall_T *fc = current_funccal;
1616
1617 if (fc != NULL && fc->func->uf_profiling)
1618 {
1619 profile_end(&fc->prof_child);
1620 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1621 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1622 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1623 }
1624 script_prof_restore(tm);
1625}
1626#endif
1627
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629#ifdef FEAT_FOLDING
1630/*
1631 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1632 * it in "*cp". Doesn't give error messages.
1633 */
1634 int
1635eval_foldexpr(arg, cp)
1636 char_u *arg;
1637 int *cp;
1638{
Bram Moolenaar33570922005-01-25 22:26:29 +00001639 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 int retval;
1641 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001642 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1643 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001646 if (use_sandbox)
1647 ++sandbox;
1648 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 retval = 0;
1652 else
1653 {
1654 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 if (tv.v_type == VAR_NUMBER)
1656 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001657 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 retval = 0;
1659 else
1660 {
1661 /* If the result is a string, check if there is a non-digit before
1662 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (!VIM_ISDIGIT(*s) && *s != '-')
1665 *cp = *s++;
1666 retval = atol((char *)s);
1667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001671 if (use_sandbox)
1672 --sandbox;
1673 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675 return retval;
1676}
1677#endif
1678
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 * ":let" list all variable values
1681 * ":let var1 var2" list variable values
1682 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 * ":let var += expr" assignment command.
1684 * ":let var -= expr" assignment command.
1685 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001686 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
1688 void
1689ex_let(eap)
1690 exarg_T *eap;
1691{
1692 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 int var_count = 0;
1697 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001699 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001700 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaardb552d602006-03-23 22:59:57 +00001702 argend = skip_var_list(arg, &var_count, &semicolon);
1703 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001705 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1706 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001707 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001710 /*
1711 * ":let" without "=": list variables
1712 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 if (*arg == '[')
1714 EMSG(_(e_invarg));
1715 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001717 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001721 list_glob_vars(&first);
1722 list_buf_vars(&first);
1723 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001724#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001725 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001726#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001727 list_script_vars(&first);
1728 list_func_vars(&first);
1729 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 eap->nextcmd = check_nextcmd(arg);
1732 }
1733 else
1734 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 op[0] = '=';
1736 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001737 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 {
1739 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1740 op[0] = expr[-1]; /* +=, -= or .= */
1741 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (eap->skip)
1745 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (eap->skip)
1748 {
1749 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 --emsg_skip;
1752 }
1753 else if (i != FAIL)
1754 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759 }
1760}
1761
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762/*
1763 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1764 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 * When "nextchars" is not NULL it points to a string with characters that
1766 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1767 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 * Returns OK or FAIL;
1769 */
1770 static int
1771ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1772 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001773 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 int copy; /* copy values from "tv", don't move */
1775 int semicolon; /* from skip_var_list() */
1776 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778{
1779 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 listitem_T *item;
1783 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784
1785 if (*arg != '[')
1786 {
1787 /*
1788 * ":let var = expr" or ":for var in list"
1789 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001790 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 return FAIL;
1792 return OK;
1793 }
1794
1795 /*
1796 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1797 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001798 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 {
1800 EMSG(_(e_listreq));
1801 return FAIL;
1802 }
1803
1804 i = list_len(l);
1805 if (semicolon == 0 && var_count < i)
1806 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001807 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808 return FAIL;
1809 }
1810 if (var_count - semicolon > i)
1811 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001812 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 return FAIL;
1814 }
1815
1816 item = l->lv_first;
1817 while (*arg != ']')
1818 {
1819 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 item = item->li_next;
1822 if (arg == NULL)
1823 return FAIL;
1824
1825 arg = skipwhite(arg);
1826 if (*arg == ';')
1827 {
1828 /* Put the rest of the list (may be empty) in the var after ';'.
1829 * Create a new list for this. */
1830 l = list_alloc();
1831 if (l == NULL)
1832 return FAIL;
1833 while (item != NULL)
1834 {
1835 list_append_tv(l, &item->li_tv);
1836 item = item->li_next;
1837 }
1838
1839 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001840 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 ltv.vval.v_list = l;
1842 l->lv_refcount = 1;
1843
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1845 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 clear_tv(&ltv);
1847 if (arg == NULL)
1848 return FAIL;
1849 break;
1850 }
1851 else if (*arg != ',' && *arg != ']')
1852 {
1853 EMSG2(_(e_intern2), "ex_let_vars()");
1854 return FAIL;
1855 }
1856 }
1857
1858 return OK;
1859}
1860
1861/*
1862 * Skip over assignable variable "var" or list of variables "[var, var]".
1863 * Used for ":let varvar = expr" and ":for varvar in expr".
1864 * For "[var, var]" increment "*var_count" for each variable.
1865 * for "[var, var; var]" set "semicolon".
1866 * Return NULL for an error.
1867 */
1868 static char_u *
1869skip_var_list(arg, var_count, semicolon)
1870 char_u *arg;
1871 int *var_count;
1872 int *semicolon;
1873{
1874 char_u *p, *s;
1875
1876 if (*arg == '[')
1877 {
1878 /* "[var, var]": find the matching ']'. */
1879 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001880 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 {
1882 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1883 s = skip_var_one(p);
1884 if (s == p)
1885 {
1886 EMSG2(_(e_invarg2), p);
1887 return NULL;
1888 }
1889 ++*var_count;
1890
1891 p = skipwhite(s);
1892 if (*p == ']')
1893 break;
1894 else if (*p == ';')
1895 {
1896 if (*semicolon == 1)
1897 {
1898 EMSG(_("Double ; in list of variables"));
1899 return NULL;
1900 }
1901 *semicolon = 1;
1902 }
1903 else if (*p != ',')
1904 {
1905 EMSG2(_(e_invarg2), p);
1906 return NULL;
1907 }
1908 }
1909 return p + 1;
1910 }
1911 else
1912 return skip_var_one(arg);
1913}
1914
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001915/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001916 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001917 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001918 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 static char_u *
1920skip_var_one(arg)
1921 char_u *arg;
1922{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001923 if (*arg == '@' && arg[1] != NUL)
1924 return arg + 2;
1925 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1926 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927}
1928
Bram Moolenaara7043832005-01-21 11:56:39 +00001929/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 * List variables for hashtab "ht" with prefix "prefix".
1931 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001932 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001934list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001938 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001939{
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 hashitem_T *hi;
1941 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001942 int todo;
1943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001944 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001945 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1946 {
1947 if (!HASHITEM_EMPTY(hi))
1948 {
1949 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001950 di = HI2DI(hi);
1951 if (empty || di->di_tv.v_type != VAR_STRING
1952 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001953 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001954 }
1955 }
1956}
1957
1958/*
1959 * List global variables.
1960 */
1961 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001962list_glob_vars(first)
1963 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001964{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001966}
1967
1968/*
1969 * List buffer variables.
1970 */
1971 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001972list_buf_vars(first)
1973 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001974{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001975 char_u numbuf[NUMBUFLEN];
1976
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001977 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1978 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001979
1980 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001981 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1982 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001983}
1984
1985/*
1986 * List window variables.
1987 */
1988 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001989list_win_vars(first)
1990 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001991{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001992 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1993 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001994}
1995
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001996#ifdef FEAT_WINDOWS
1997/*
1998 * List tab page variables.
1999 */
2000 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002001list_tab_vars(first)
2002 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002003{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002004 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2005 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002006}
2007#endif
2008
Bram Moolenaara7043832005-01-21 11:56:39 +00002009/*
2010 * List Vim variables.
2011 */
2012 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002013list_vim_vars(first)
2014 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002016 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017}
2018
2019/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002020 * List script-local variables, if there is a script.
2021 */
2022 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002023list_script_vars(first)
2024 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002025{
2026 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002027 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2028 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002029}
2030
2031/*
2032 * List function variables, if there is a function.
2033 */
2034 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002035list_func_vars(first)
2036 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002037{
2038 if (current_funccal != NULL)
2039 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002040 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002041}
2042
2043/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 * List variables in "arg".
2045 */
2046 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002047list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 exarg_T *eap;
2049 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002050 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051{
2052 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 char_u *name_start;
2056 char_u *arg_subsc;
2057 char_u *tofree;
2058 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059
2060 while (!ends_excmd(*arg) && !got_int)
2061 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002064 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2066 {
2067 emsg_severe = TRUE;
2068 EMSG(_(e_trailing));
2069 break;
2070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 /* get_name_len() takes care of expanding curly braces */
2075 name_start = name = arg;
2076 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2077 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002079 /* This is mainly to keep test 49 working: when expanding
2080 * curly braces fails overrule the exception error message. */
2081 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002082 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 emsg_severe = TRUE;
2084 EMSG2(_(e_invarg2), arg);
2085 break;
2086 }
2087 error = TRUE;
2088 }
2089 else
2090 {
2091 if (tofree != NULL)
2092 name = tofree;
2093 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 else
2096 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097 /* handle d.key, l[idx], f(expr) */
2098 arg_subsc = arg;
2099 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002100 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002105 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107 case 'g': list_glob_vars(first); break;
2108 case 'b': list_buf_vars(first); break;
2109 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002110#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002112#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 case 'v': list_vim_vars(first); break;
2114 case 's': list_script_vars(first); break;
2115 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116 default:
2117 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002118 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 }
2120 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121 {
2122 char_u numbuf[NUMBUFLEN];
2123 char_u *tf;
2124 int c;
2125 char_u *s;
2126
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002127 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002128 c = *arg;
2129 *arg = NUL;
2130 list_one_var_a((char_u *)"",
2131 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 tv.v_type,
2133 s == NULL ? (char_u *)"" : s,
2134 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135 *arg = c;
2136 vim_free(tf);
2137 }
2138 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 }
2141 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142
2143 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002144 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145
2146 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 }
2148
2149 return arg;
2150}
2151
2152/*
2153 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2154 * Returns a pointer to the char just after the var name.
2155 * Returns NULL if there is an error.
2156 */
2157 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002158ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164{
2165 int c1;
2166 char_u *name;
2167 char_u *p;
2168 char_u *arg_end = NULL;
2169 int len;
2170 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172
2173 /*
2174 * ":let $VAR = expr": Set environment variable.
2175 */
2176 if (*arg == '$')
2177 {
2178 /* Find the end of the name. */
2179 ++arg;
2180 name = arg;
2181 len = get_env_len(&arg);
2182 if (len == 0)
2183 EMSG2(_(e_invarg2), name - 1);
2184 else
2185 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 if (op != NULL && (*op == '+' || *op == '-'))
2187 EMSG2(_(e_letwrong), op);
2188 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002189 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 EMSG(_(e_letunexp));
2191 else
2192 {
2193 c1 = name[len];
2194 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002195 p = get_tv_string_chk(tv);
2196 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002197 {
2198 int mustfree = FALSE;
2199 char_u *s = vim_getenv(name, &mustfree);
2200
2201 if (s != NULL)
2202 {
2203 p = tofree = concat_str(s, p);
2204 if (mustfree)
2205 vim_free(s);
2206 }
2207 }
2208 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 if (STRICMP(name, "HOME") == 0)
2212 init_homedir();
2213 else if (didset_vim && STRICMP(name, "VIM") == 0)
2214 didset_vim = FALSE;
2215 else if (didset_vimruntime
2216 && STRICMP(name, "VIMRUNTIME") == 0)
2217 didset_vimruntime = FALSE;
2218 arg_end = arg;
2219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002221 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
2223 }
2224 }
2225
2226 /*
2227 * ":let &option = expr": Set option value.
2228 * ":let &l:option = expr": Set local option value.
2229 * ":let &g:option = expr": Set global option value.
2230 */
2231 else if (*arg == '&')
2232 {
2233 /* Find the end of the name. */
2234 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002235 if (p == NULL || (endchars != NULL
2236 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 EMSG(_(e_letunexp));
2238 else
2239 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240 long n;
2241 int opt_type;
2242 long numval;
2243 char_u *stringval = NULL;
2244 char_u *s;
2245
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 c1 = *p;
2247 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248
2249 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002250 s = get_tv_string_chk(tv); /* != NULL if number or string */
2251 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002252 {
2253 opt_type = get_option_value(arg, &numval,
2254 &stringval, opt_flags);
2255 if ((opt_type == 1 && *op == '.')
2256 || (opt_type == 0 && *op != '.'))
2257 EMSG2(_(e_letwrong), op);
2258 else
2259 {
2260 if (opt_type == 1) /* number */
2261 {
2262 if (*op == '+')
2263 n = numval + n;
2264 else
2265 n = numval - n;
2266 }
2267 else if (opt_type == 0 && stringval != NULL) /* string */
2268 {
2269 s = concat_str(stringval, s);
2270 vim_free(stringval);
2271 stringval = s;
2272 }
2273 }
2274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002275 if (s != NULL)
2276 {
2277 set_option_value(arg, n, s, opt_flags);
2278 arg_end = p;
2279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002281 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283 }
2284
2285 /*
2286 * ":let @r = expr": Set register contents.
2287 */
2288 else if (*arg == '@')
2289 {
2290 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002291 if (op != NULL && (*op == '+' || *op == '-'))
2292 EMSG2(_(e_letwrong), op);
2293 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002294 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 EMSG(_(e_letunexp));
2296 else
2297 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002298 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 char_u *s;
2300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002301 p = get_tv_string_chk(tv);
2302 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002304 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305 if (s != NULL)
2306 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002307 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 vim_free(s);
2309 }
2310 }
2311 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002312 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002313 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 arg_end = arg + 1;
2315 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002316 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318 }
2319
2320 /*
2321 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002324 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002328 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2332 EMSG(_(e_letunexp));
2333 else
2334 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 arg_end = p;
2337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
2341
2342 else
2343 EMSG2(_(e_invarg2), arg);
2344
2345 return arg_end;
2346}
2347
2348/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2350 */
2351 static int
2352check_changedtick(arg)
2353 char_u *arg;
2354{
2355 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2356 {
2357 EMSG2(_(e_readonlyvar), arg);
2358 return TRUE;
2359 }
2360 return FALSE;
2361}
2362
2363/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 * Get an lval: variable, Dict item or List item that can be assigned a value
2365 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2366 * "name.key", "name.key[expr]" etc.
2367 * Indexing only works if "name" is an existing List or Dictionary.
2368 * "name" points to the start of the name.
2369 * If "rettv" is not NULL it points to the value to be assigned.
2370 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2371 * wrong; must end in space or cmd separator.
2372 *
2373 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002374 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 */
2377 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002378get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002380 typval_T *rettv;
2381 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 int unlet;
2383 int skip;
2384 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002385 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 char_u *expr_start, *expr_end;
2389 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002390 dictitem_T *v;
2391 typval_T var1;
2392 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002394 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002397 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002400 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401
2402 if (skip)
2403 {
2404 /* When skipping just find the end of the name. */
2405 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002406 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 }
2408
2409 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002410 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (expr_start != NULL)
2412 {
2413 /* Don't expand the name when we already know there is an error. */
2414 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2415 && *p != '[' && *p != '.')
2416 {
2417 EMSG(_(e_trailing));
2418 return NULL;
2419 }
2420
2421 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2422 if (lp->ll_exp_name == NULL)
2423 {
2424 /* Report an invalid expression in braces, unless the
2425 * expression evaluation has been cancelled due to an
2426 * aborting error, an interrupt, or an exception. */
2427 if (!aborting() && !quiet)
2428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002429 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 EMSG2(_(e_invarg2), name);
2431 return NULL;
2432 }
2433 }
2434 lp->ll_name = lp->ll_exp_name;
2435 }
2436 else
2437 lp->ll_name = name;
2438
2439 /* Without [idx] or .key we are done. */
2440 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2441 return p;
2442
2443 cc = *p;
2444 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002445 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (v == NULL && !quiet)
2447 EMSG2(_(e_undefvar), lp->ll_name);
2448 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 if (v == NULL)
2450 return NULL;
2451
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 /*
2453 * Loop until no more [idx] or .key is following.
2454 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002455 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2459 && !(lp->ll_tv->v_type == VAR_DICT
2460 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (!quiet)
2463 EMSG(_("E689: Can only index a List or Dictionary"));
2464 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (!quiet)
2469 EMSG(_("E708: [:] must come last"));
2470 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002472
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 len = -1;
2474 if (*p == '.')
2475 {
2476 key = p + 1;
2477 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2478 ;
2479 if (len == 0)
2480 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 if (!quiet)
2482 EMSG(_(e_emptykey));
2483 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 }
2485 p = key + len;
2486 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002487 else
2488 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002490 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 if (*p == ':')
2492 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002493 else
2494 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 empty1 = FALSE;
2496 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 if (get_tv_string_chk(&var1) == NULL)
2499 {
2500 /* not a number or string */
2501 clear_tv(&var1);
2502 return NULL;
2503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
2505
2506 /* Optionally get the second index [ :expr]. */
2507 if (*p == ':')
2508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 if (!empty1)
2514 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 if (rettv != NULL && (rettv->v_type != VAR_LIST
2518 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (!quiet)
2521 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 if (!empty1)
2523 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 }
2526 p = skipwhite(p + 1);
2527 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 else
2530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2533 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 if (!empty1)
2535 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002538 if (get_tv_string_chk(&var2) == NULL)
2539 {
2540 /* not a number or string */
2541 if (!empty1)
2542 clear_tv(&var1);
2543 clear_tv(&var2);
2544 return NULL;
2545 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002548 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002551
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (!quiet)
2555 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 if (!empty1)
2557 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 }
2562
2563 /* Skip to past ']'. */
2564 ++p;
2565 }
2566
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 {
2569 if (len == -1)
2570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002572 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 if (*key == NUL)
2574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (!quiet)
2576 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 }
2580 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 lp->ll_list = NULL;
2582 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002583 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002590 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 if (len == -1)
2592 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 }
2595 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 if (len == -1)
2600 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 p = NULL;
2603 break;
2604 }
2605 if (len == -1)
2606 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 }
2609 else
2610 {
2611 /*
2612 * Get the number and item for the only or first index of the List.
2613 */
2614 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 else
2617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002618 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 clear_tv(&var1);
2620 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 lp->ll_list = lp->ll_tv->vval.v_list;
2623 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2624 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002626 if (lp->ll_n1 < 0)
2627 {
2628 lp->ll_n1 = 0;
2629 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2630 }
2631 }
2632 if (lp->ll_li == NULL)
2633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 }
2638
2639 /*
2640 * May need to find the item or absolute index for the second
2641 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 * When no index given: "lp->ll_empty2" is TRUE.
2643 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002647 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2658 if (lp->ll_n1 < 0)
2659 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2660 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 }
2663
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
2667
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return p;
2669}
2670
2671/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002672 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 */
2674 static void
2675clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002676 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677{
2678 vim_free(lp->ll_exp_name);
2679 vim_free(lp->ll_newkey);
2680}
2681
2682/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002683 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 */
2687 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002689 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002691 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694{
2695 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002696 listitem_T *ri;
2697 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698
2699 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 cc = *endp;
2704 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 if (op != NULL && *op != '=')
2706 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002707 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002709 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002710 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002711 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002712 {
2713 if (tv_op(&tv, rettv, op) == OK)
2714 set_var(lp->ll_name, &tv, FALSE);
2715 clear_tv(&tv);
2716 }
2717 }
2718 else
2719 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002721 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002723 else if (tv_check_lock(lp->ll_newkey == NULL
2724 ? lp->ll_tv->v_lock
2725 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2726 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 else if (lp->ll_range)
2728 {
2729 /*
2730 * Assign the List values to the list items.
2731 */
2732 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 if (op != NULL && *op != '=')
2735 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2736 else
2737 {
2738 clear_tv(&lp->ll_li->li_tv);
2739 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 ri = ri->li_next;
2742 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2743 break;
2744 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002747 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 ri = NULL;
2750 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_li = lp->ll_li->li_next;
2754 ++lp->ll_n1;
2755 }
2756 if (ri != NULL)
2757 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002758 else if (lp->ll_empty2
2759 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 : lp->ll_n1 != lp->ll_n2)
2761 EMSG(_("E711: List value has not enough items"));
2762 }
2763 else
2764 {
2765 /*
2766 * Assign to a List or Dictionary item.
2767 */
2768 if (lp->ll_newkey != NULL)
2769 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002770 if (op != NULL && *op != '=')
2771 {
2772 EMSG2(_(e_letwrong), op);
2773 return;
2774 }
2775
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002777 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (di == NULL)
2779 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002780 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2781 {
2782 vim_free(di);
2783 return;
2784 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787 else if (op != NULL && *op != '=')
2788 {
2789 tv_op(lp->ll_tv, rettv, op);
2790 return;
2791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002792 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /*
2796 * Assign the value to the variable or list item.
2797 */
2798 if (copy)
2799 copy_tv(rettv, lp->ll_tv);
2800 else
2801 {
2802 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002803 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002805 }
2806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807}
2808
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002809/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002810 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2811 * Returns OK or FAIL.
2812 */
2813 static int
2814tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002815 typval_T *tv1;
2816 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 char_u *op;
2818{
2819 long n;
2820 char_u numbuf[NUMBUFLEN];
2821 char_u *s;
2822
2823 /* Can't do anything with a Funcref or a Dict on the right. */
2824 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2825 {
2826 switch (tv1->v_type)
2827 {
2828 case VAR_DICT:
2829 case VAR_FUNC:
2830 break;
2831
2832 case VAR_LIST:
2833 if (*op != '+' || tv2->v_type != VAR_LIST)
2834 break;
2835 /* List += List */
2836 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2837 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2838 return OK;
2839
2840 case VAR_NUMBER:
2841 case VAR_STRING:
2842 if (tv2->v_type == VAR_LIST)
2843 break;
2844 if (*op == '+' || *op == '-')
2845 {
2846 /* nr += nr or nr -= nr*/
2847 n = get_tv_number(tv1);
2848 if (*op == '+')
2849 n += get_tv_number(tv2);
2850 else
2851 n -= get_tv_number(tv2);
2852 clear_tv(tv1);
2853 tv1->v_type = VAR_NUMBER;
2854 tv1->vval.v_number = n;
2855 }
2856 else
2857 {
2858 /* str .= str */
2859 s = get_tv_string(tv1);
2860 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2861 clear_tv(tv1);
2862 tv1->v_type = VAR_STRING;
2863 tv1->vval.v_string = s;
2864 }
2865 return OK;
2866 }
2867 }
2868
2869 EMSG2(_(e_letwrong), op);
2870 return FAIL;
2871}
2872
2873/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874 * Add a watcher to a list.
2875 */
2876 static void
2877list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 list_T *l;
2879 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002880{
2881 lw->lw_next = l->lv_watch;
2882 l->lv_watch = lw;
2883}
2884
2885/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002886 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002887 * No warning when it isn't found...
2888 */
2889 static void
2890list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 list_T *l;
2892 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002893{
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895
2896 lwp = &l->lv_watch;
2897 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2898 {
2899 if (lw == lwrem)
2900 {
2901 *lwp = lw->lw_next;
2902 break;
2903 }
2904 lwp = &lw->lw_next;
2905 }
2906}
2907
2908/*
2909 * Just before removing an item from a list: advance watchers to the next
2910 * item.
2911 */
2912 static void
2913list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 list_T *l;
2915 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916{
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918
2919 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2920 if (lw->lw_item == item)
2921 lw->lw_item = item->li_next;
2922}
2923
2924/*
2925 * Evaluate the expression used in a ":for var in expr" command.
2926 * "arg" points to "var".
2927 * Set "*errp" to TRUE for an error, FALSE otherwise;
2928 * Return a pointer that holds the info. Null when there is an error.
2929 */
2930 void *
2931eval_for_line(arg, errp, nextcmdp, skip)
2932 char_u *arg;
2933 int *errp;
2934 char_u **nextcmdp;
2935 int skip;
2936{
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002938 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 typval_T tv;
2940 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941
2942 *errp = TRUE; /* default: there is an error */
2943
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002945 if (fi == NULL)
2946 return NULL;
2947
2948 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2949 if (expr == NULL)
2950 return fi;
2951
2952 expr = skipwhite(expr);
2953 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2954 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002955 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002956 return fi;
2957 }
2958
2959 if (skip)
2960 ++emsg_skip;
2961 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2962 {
2963 *errp = FALSE;
2964 if (!skip)
2965 {
2966 l = tv.vval.v_list;
2967 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002968 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002970 clear_tv(&tv);
2971 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 else
2973 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002974 /* No need to increment the refcount, it's already set for the
2975 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976 fi->fi_list = l;
2977 list_add_watch(l, &fi->fi_lw);
2978 fi->fi_lw.lw_item = l->lv_first;
2979 }
2980 }
2981 }
2982 if (skip)
2983 --emsg_skip;
2984
2985 return fi;
2986}
2987
2988/*
2989 * Use the first item in a ":for" list. Advance to the next.
2990 * Assign the values to the variable (list). "arg" points to the first one.
2991 * Return TRUE when a valid item was found, FALSE when at end of list or
2992 * something wrong.
2993 */
2994 int
2995next_for_item(fi_void, arg)
2996 void *fi_void;
2997 char_u *arg;
2998{
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003001 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002
3003 item = fi->fi_lw.lw_item;
3004 if (item == NULL)
3005 result = FALSE;
3006 else
3007 {
3008 fi->fi_lw.lw_item = item->li_next;
3009 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3010 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3011 }
3012 return result;
3013}
3014
3015/*
3016 * Free the structure used to store info used by ":for".
3017 */
3018 void
3019free_for_info(fi_void)
3020 void *fi_void;
3021{
Bram Moolenaar33570922005-01-25 22:26:29 +00003022 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003023
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003024 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003025 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003026 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003027 list_unref(fi->fi_list);
3028 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003029 vim_free(fi);
3030}
3031
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3033
3034 void
3035set_context_for_expression(xp, arg, cmdidx)
3036 expand_T *xp;
3037 char_u *arg;
3038 cmdidx_T cmdidx;
3039{
3040 int got_eq = FALSE;
3041 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003042 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044 if (cmdidx == CMD_let)
3045 {
3046 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003047 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048 {
3049 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003050 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003051 {
3052 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003053 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003054 if (vim_iswhite(*p))
3055 break;
3056 }
3057 return;
3058 }
3059 }
3060 else
3061 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3062 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 while ((xp->xp_pattern = vim_strpbrk(arg,
3064 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3065 {
3066 c = *xp->xp_pattern;
3067 if (c == '&')
3068 {
3069 c = xp->xp_pattern[1];
3070 if (c == '&')
3071 {
3072 ++xp->xp_pattern;
3073 xp->xp_context = cmdidx != CMD_let || got_eq
3074 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3075 }
3076 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003079 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3080 xp->xp_pattern += 2;
3081
3082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
3084 else if (c == '$')
3085 {
3086 /* environment variable */
3087 xp->xp_context = EXPAND_ENV_VARS;
3088 }
3089 else if (c == '=')
3090 {
3091 got_eq = TRUE;
3092 xp->xp_context = EXPAND_EXPRESSION;
3093 }
3094 else if (c == '<'
3095 && xp->xp_context == EXPAND_FUNCTIONS
3096 && vim_strchr(xp->xp_pattern, '(') == NULL)
3097 {
3098 /* Function name can start with "<SNR>" */
3099 break;
3100 }
3101 else if (cmdidx != CMD_let || got_eq)
3102 {
3103 if (c == '"') /* string */
3104 {
3105 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3106 if (c == '\\' && xp->xp_pattern[1] != NUL)
3107 ++xp->xp_pattern;
3108 xp->xp_context = EXPAND_NOTHING;
3109 }
3110 else if (c == '\'') /* literal string */
3111 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003112 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3114 /* skip */ ;
3115 xp->xp_context = EXPAND_NOTHING;
3116 }
3117 else if (c == '|')
3118 {
3119 if (xp->xp_pattern[1] == '|')
3120 {
3121 ++xp->xp_pattern;
3122 xp->xp_context = EXPAND_EXPRESSION;
3123 }
3124 else
3125 xp->xp_context = EXPAND_COMMANDS;
3126 }
3127 else
3128 xp->xp_context = EXPAND_EXPRESSION;
3129 }
3130 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131 /* Doesn't look like something valid, expand as an expression
3132 * anyway. */
3133 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 arg = xp->xp_pattern;
3135 if (*arg != NUL)
3136 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3137 /* skip */ ;
3138 }
3139 xp->xp_pattern = arg;
3140}
3141
3142#endif /* FEAT_CMDL_COMPL */
3143
3144/*
3145 * ":1,25call func(arg1, arg2)" function call.
3146 */
3147 void
3148ex_call(eap)
3149 exarg_T *eap;
3150{
3151 char_u *arg = eap->arg;
3152 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 linenr_T lnum;
3158 int doesrange;
3159 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003162 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003163 if (fudi.fd_newkey != NULL)
3164 {
3165 /* Still need to give an error message for missing key. */
3166 EMSG2(_(e_dictkey), fudi.fd_newkey);
3167 vim_free(fudi.fd_newkey);
3168 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003169 if (tofree == NULL)
3170 return;
3171
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003172 /* Increase refcount on dictionary, it could get deleted when evaluating
3173 * the arguments. */
3174 if (fudi.fd_dict != NULL)
3175 ++fudi.fd_dict->dv_refcount;
3176
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003177 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003178 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaar532c7802005-01-27 14:44:31 +00003181 /* Skip white space to allow ":call func ()". Not good, but required for
3182 * backward compatibility. */
3183 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003184 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186 if (*startarg != '(')
3187 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003188 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 goto end;
3190 }
3191
3192 /*
3193 * When skipping, evaluate the function once, to find the end of the
3194 * arguments.
3195 * When the function takes a range, this is discovered after the first
3196 * call, and the loop is broken.
3197 */
3198 if (eap->skip)
3199 {
3200 ++emsg_skip;
3201 lnum = eap->line2; /* do it once, also with an invalid range */
3202 }
3203 else
3204 lnum = eap->line1;
3205 for ( ; lnum <= eap->line2; ++lnum)
3206 {
3207 if (!eap->skip && eap->addr_count > 0)
3208 {
3209 curwin->w_cursor.lnum = lnum;
3210 curwin->w_cursor.col = 0;
3211 }
3212 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003213 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003214 eap->line1, eap->line2, &doesrange,
3215 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
3217 failed = TRUE;
3218 break;
3219 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003220
3221 /* Handle a function returning a Funcref, Dictionary or List. */
3222 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3223 {
3224 failed = TRUE;
3225 break;
3226 }
3227
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003228 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (doesrange || eap->skip)
3230 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003233 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003234 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003235 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (aborting())
3237 break;
3238 }
3239 if (eap->skip)
3240 --emsg_skip;
3241
3242 if (!failed)
3243 {
3244 /* Check for trailing illegal characters and a following command. */
3245 if (!ends_excmd(*arg))
3246 {
3247 emsg_severe = TRUE;
3248 EMSG(_(e_trailing));
3249 }
3250 else
3251 eap->nextcmd = check_nextcmd(arg);
3252 }
3253
3254end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003255 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003256 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
3258
3259/*
3260 * ":unlet[!] var1 ... " command.
3261 */
3262 void
3263ex_unlet(eap)
3264 exarg_T *eap;
3265{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266 ex_unletlock(eap, eap->arg, 0);
3267}
3268
3269/*
3270 * ":lockvar" and ":unlockvar" commands
3271 */
3272 void
3273ex_lockvar(eap)
3274 exarg_T *eap;
3275{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277 int deep = 2;
3278
3279 if (eap->forceit)
3280 deep = -1;
3281 else if (vim_isdigit(*arg))
3282 {
3283 deep = getdigits(&arg);
3284 arg = skipwhite(arg);
3285 }
3286
3287 ex_unletlock(eap, arg, deep);
3288}
3289
3290/*
3291 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3292 */
3293 static void
3294ex_unletlock(eap, argstart, deep)
3295 exarg_T *eap;
3296 char_u *argstart;
3297 int deep;
3298{
3299 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003302 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
3304 do
3305 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003307 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3308 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003309 if (lv.ll_name == NULL)
3310 error = TRUE; /* error but continue parsing */
3311 if (name_end == NULL || (!vim_iswhite(*name_end)
3312 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003314 if (name_end != NULL)
3315 {
3316 emsg_severe = TRUE;
3317 EMSG(_(e_trailing));
3318 }
3319 if (!(eap->skip || error))
3320 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 break;
3322 }
3323
3324 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003325 {
3326 if (eap->cmdidx == CMD_unlet)
3327 {
3328 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3329 error = TRUE;
3330 }
3331 else
3332 {
3333 if (do_lock_var(&lv, name_end, deep,
3334 eap->cmdidx == CMD_lockvar) == FAIL)
3335 error = TRUE;
3336 }
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003339 if (!eap->skip)
3340 clear_lval(&lv);
3341
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 arg = skipwhite(name_end);
3343 } while (!ends_excmd(*arg));
3344
3345 eap->nextcmd = check_nextcmd(arg);
3346}
3347
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003349do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003352 int forceit;
3353{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003354 int ret = OK;
3355 int cc;
3356
3357 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003358 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003359 cc = *name_end;
3360 *name_end = NUL;
3361
3362 /* Normal name or expanded name. */
3363 if (check_changedtick(lp->ll_name))
3364 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003365 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003366 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003367 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003368 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003369 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3370 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003371 else if (lp->ll_range)
3372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003374
3375 /* Delete a range of List items. */
3376 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3377 {
3378 li = lp->ll_li->li_next;
3379 listitem_remove(lp->ll_list, lp->ll_li);
3380 lp->ll_li = li;
3381 ++lp->ll_n1;
3382 }
3383 }
3384 else
3385 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003386 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003387 /* unlet a List item. */
3388 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003389 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003390 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003391 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003392 }
3393
3394 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003395}
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397/*
3398 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003399 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 */
3401 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003402do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003404 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaar33570922005-01-25 22:26:29 +00003406 hashtab_T *ht;
3407 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003408 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 ht = find_var_ht(name, &varname);
3411 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 hi = hash_find(ht, varname);
3414 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003415 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003416 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3417 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003418 if (var_check_ro(HI2DI(hi)->di_flags, name))
3419 return FAIL;
3420 delete_var(ht, hi);
3421 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003424 if (forceit)
3425 return OK;
3426 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 return FAIL;
3428}
3429
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003430/*
3431 * Lock or unlock variable indicated by "lp".
3432 * "deep" is the levels to go (-1 for unlimited);
3433 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3434 */
3435 static int
3436do_lock_var(lp, name_end, deep, lock)
3437 lval_T *lp;
3438 char_u *name_end;
3439 int deep;
3440 int lock;
3441{
3442 int ret = OK;
3443 int cc;
3444 dictitem_T *di;
3445
3446 if (deep == 0) /* nothing to do */
3447 return OK;
3448
3449 if (lp->ll_tv == NULL)
3450 {
3451 cc = *name_end;
3452 *name_end = NUL;
3453
3454 /* Normal name or expanded name. */
3455 if (check_changedtick(lp->ll_name))
3456 ret = FAIL;
3457 else
3458 {
3459 di = find_var(lp->ll_name, NULL);
3460 if (di == NULL)
3461 ret = FAIL;
3462 else
3463 {
3464 if (lock)
3465 di->di_flags |= DI_FLAGS_LOCK;
3466 else
3467 di->di_flags &= ~DI_FLAGS_LOCK;
3468 item_lock(&di->di_tv, deep, lock);
3469 }
3470 }
3471 *name_end = cc;
3472 }
3473 else if (lp->ll_range)
3474 {
3475 listitem_T *li = lp->ll_li;
3476
3477 /* (un)lock a range of List items. */
3478 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3479 {
3480 item_lock(&li->li_tv, deep, lock);
3481 li = li->li_next;
3482 ++lp->ll_n1;
3483 }
3484 }
3485 else if (lp->ll_list != NULL)
3486 /* (un)lock a List item. */
3487 item_lock(&lp->ll_li->li_tv, deep, lock);
3488 else
3489 /* un(lock) a Dictionary item. */
3490 item_lock(&lp->ll_di->di_tv, deep, lock);
3491
3492 return ret;
3493}
3494
3495/*
3496 * Lock or unlock an item. "deep" is nr of levels to go.
3497 */
3498 static void
3499item_lock(tv, deep, lock)
3500 typval_T *tv;
3501 int deep;
3502 int lock;
3503{
3504 static int recurse = 0;
3505 list_T *l;
3506 listitem_T *li;
3507 dict_T *d;
3508 hashitem_T *hi;
3509 int todo;
3510
3511 if (recurse >= DICT_MAXNEST)
3512 {
3513 EMSG(_("E743: variable nested too deep for (un)lock"));
3514 return;
3515 }
3516 if (deep == 0)
3517 return;
3518 ++recurse;
3519
3520 /* lock/unlock the item itself */
3521 if (lock)
3522 tv->v_lock |= VAR_LOCKED;
3523 else
3524 tv->v_lock &= ~VAR_LOCKED;
3525
3526 switch (tv->v_type)
3527 {
3528 case VAR_LIST:
3529 if ((l = tv->vval.v_list) != NULL)
3530 {
3531 if (lock)
3532 l->lv_lock |= VAR_LOCKED;
3533 else
3534 l->lv_lock &= ~VAR_LOCKED;
3535 if (deep < 0 || deep > 1)
3536 /* recursive: lock/unlock the items the List contains */
3537 for (li = l->lv_first; li != NULL; li = li->li_next)
3538 item_lock(&li->li_tv, deep - 1, lock);
3539 }
3540 break;
3541 case VAR_DICT:
3542 if ((d = tv->vval.v_dict) != NULL)
3543 {
3544 if (lock)
3545 d->dv_lock |= VAR_LOCKED;
3546 else
3547 d->dv_lock &= ~VAR_LOCKED;
3548 if (deep < 0 || deep > 1)
3549 {
3550 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003551 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3553 {
3554 if (!HASHITEM_EMPTY(hi))
3555 {
3556 --todo;
3557 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3558 }
3559 }
3560 }
3561 }
3562 }
3563 --recurse;
3564}
3565
Bram Moolenaara40058a2005-07-11 22:42:07 +00003566/*
3567 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3568 * it refers to a List or Dictionary that is locked.
3569 */
3570 static int
3571tv_islocked(tv)
3572 typval_T *tv;
3573{
3574 return (tv->v_lock & VAR_LOCKED)
3575 || (tv->v_type == VAR_LIST
3576 && tv->vval.v_list != NULL
3577 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3578 || (tv->v_type == VAR_DICT
3579 && tv->vval.v_dict != NULL
3580 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3581}
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3584/*
3585 * Delete all "menutrans_" variables.
3586 */
3587 void
3588del_menutrans_vars()
3589{
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003591 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003596 {
3597 if (!HASHITEM_EMPTY(hi))
3598 {
3599 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3601 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003602 }
3603 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605}
3606#endif
3607
3608#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3609
3610/*
3611 * Local string buffer for the next two functions to store a variable name
3612 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3613 * get_user_var_name().
3614 */
3615
3616static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3617
3618static char_u *varnamebuf = NULL;
3619static int varnamebuflen = 0;
3620
3621/*
3622 * Function to concatenate a prefix and a variable name.
3623 */
3624 static char_u *
3625cat_prefix_varname(prefix, name)
3626 int prefix;
3627 char_u *name;
3628{
3629 int len;
3630
3631 len = (int)STRLEN(name) + 3;
3632 if (len > varnamebuflen)
3633 {
3634 vim_free(varnamebuf);
3635 len += 10; /* some additional space */
3636 varnamebuf = alloc(len);
3637 if (varnamebuf == NULL)
3638 {
3639 varnamebuflen = 0;
3640 return NULL;
3641 }
3642 varnamebuflen = len;
3643 }
3644 *varnamebuf = prefix;
3645 varnamebuf[1] = ':';
3646 STRCPY(varnamebuf + 2, name);
3647 return varnamebuf;
3648}
3649
3650/*
3651 * Function given to ExpandGeneric() to obtain the list of user defined
3652 * (global/buffer/window/built-in) variable names.
3653 */
3654/*ARGSUSED*/
3655 char_u *
3656get_user_var_name(xp, idx)
3657 expand_T *xp;
3658 int idx;
3659{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003660 static long_u gdone;
3661 static long_u bdone;
3662 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003663#ifdef FEAT_WINDOWS
3664 static long_u tdone;
3665#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003666 static int vidx;
3667 static hashitem_T *hi;
3668 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003671 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003672 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003673#ifdef FEAT_WINDOWS
3674 tdone = 0;
3675#endif
3676 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003677
3678 /* Global variables */
3679 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003683 else
3684 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003685 while (HASHITEM_EMPTY(hi))
3686 ++hi;
3687 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3688 return cat_prefix_varname('g', hi->hi_key);
3689 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003691
3692 /* b: variables */
3693 ht = &curbuf->b_vars.dv_hashtab;
3694 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003696 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003697 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003698 else
3699 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003700 while (HASHITEM_EMPTY(hi))
3701 ++hi;
3702 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003704 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003706 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 return (char_u *)"b:changedtick";
3708 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003709
3710 /* w: variables */
3711 ht = &curwin->w_vars.dv_hashtab;
3712 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003714 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003716 else
3717 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003718 while (HASHITEM_EMPTY(hi))
3719 ++hi;
3720 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003722
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003723#ifdef FEAT_WINDOWS
3724 /* t: variables */
3725 ht = &curtab->tp_vars.dv_hashtab;
3726 if (tdone < ht->ht_used)
3727 {
3728 if (tdone++ == 0)
3729 hi = ht->ht_array;
3730 else
3731 ++hi;
3732 while (HASHITEM_EMPTY(hi))
3733 ++hi;
3734 return cat_prefix_varname('t', hi->hi_key);
3735 }
3736#endif
3737
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 /* v: variables */
3739 if (vidx < VV_LEN)
3740 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 vim_free(varnamebuf);
3743 varnamebuf = NULL;
3744 varnamebuflen = 0;
3745 return NULL;
3746}
3747
3748#endif /* FEAT_CMDL_COMPL */
3749
3750/*
3751 * types for expressions.
3752 */
3753typedef enum
3754{
3755 TYPE_UNKNOWN = 0
3756 , TYPE_EQUAL /* == */
3757 , TYPE_NEQUAL /* != */
3758 , TYPE_GREATER /* > */
3759 , TYPE_GEQUAL /* >= */
3760 , TYPE_SMALLER /* < */
3761 , TYPE_SEQUAL /* <= */
3762 , TYPE_MATCH /* =~ */
3763 , TYPE_NOMATCH /* !~ */
3764} exptype_T;
3765
3766/*
3767 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3770 */
3771
3772/*
3773 * Handle zero level expression.
3774 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003776 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 * Return OK or FAIL.
3778 */
3779 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003780eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 char_u **nextcmd;
3784 int evaluate;
3785{
3786 int ret;
3787 char_u *p;
3788
3789 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003790 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (ret == FAIL || !ends_excmd(*p))
3792 {
3793 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 /*
3796 * Report the invalid expression unless the expression evaluation has
3797 * been cancelled due to an aborting error, an interrupt, or an
3798 * exception.
3799 */
3800 if (!aborting())
3801 EMSG2(_(e_invexpr2), arg);
3802 ret = FAIL;
3803 }
3804 if (nextcmd != NULL)
3805 *nextcmd = check_nextcmd(p);
3806
3807 return ret;
3808}
3809
3810/*
3811 * Handle top level expression:
3812 * expr1 ? expr0 : expr0
3813 *
3814 * "arg" must point to the first non-white of the expression.
3815 * "arg" is advanced to the next non-white after the recognized expression.
3816 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003817 * Note: "rettv.v_lock" is not set.
3818 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 * Return OK or FAIL.
3820 */
3821 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 int evaluate;
3826{
3827 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830 /*
3831 * Get the first variable.
3832 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 return FAIL;
3835
3836 if ((*arg)[0] == '?')
3837 {
3838 result = FALSE;
3839 if (evaluate)
3840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003841 int error = FALSE;
3842
3843 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003845 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003846 if (error)
3847 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849
3850 /*
3851 * Get the second variable.
3852 */
3853 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return FAIL;
3856
3857 /*
3858 * Check for the ":".
3859 */
3860 if ((*arg)[0] != ':')
3861 {
3862 EMSG(_("E109: Missing ':' after '?'"));
3863 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 return FAIL;
3866 }
3867
3868 /*
3869 * Get the third variable.
3870 */
3871 *arg = skipwhite(*arg + 1);
3872 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3873 {
3874 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003875 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return FAIL;
3877 }
3878 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881
3882 return OK;
3883}
3884
3885/*
3886 * Handle first level expression:
3887 * expr2 || expr2 || expr2 logical OR
3888 *
3889 * "arg" must point to the first non-white of the expression.
3890 * "arg" is advanced to the next non-white after the recognized expression.
3891 *
3892 * Return OK or FAIL.
3893 */
3894 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003895eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 int evaluate;
3899{
Bram Moolenaar33570922005-01-25 22:26:29 +00003900 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 long result;
3902 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003903 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905 /*
3906 * Get the first variable.
3907 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003908 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return FAIL;
3910
3911 /*
3912 * Repeat until there is no following "||".
3913 */
3914 first = TRUE;
3915 result = FALSE;
3916 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3917 {
3918 if (evaluate && first)
3919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003920 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003922 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003923 if (error)
3924 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 first = FALSE;
3926 }
3927
3928 /*
3929 * Get the second variable.
3930 */
3931 *arg = skipwhite(*arg + 2);
3932 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3933 return FAIL;
3934
3935 /*
3936 * Compute the result.
3937 */
3938 if (evaluate && !result)
3939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003940 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (error)
3944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 if (evaluate)
3947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003948 rettv->v_type = VAR_NUMBER;
3949 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 }
3952
3953 return OK;
3954}
3955
3956/*
3957 * Handle second level expression:
3958 * expr3 && expr3 && expr3 logical AND
3959 *
3960 * "arg" must point to the first non-white of the expression.
3961 * "arg" is advanced to the next non-white after the recognized expression.
3962 *
3963 * Return OK or FAIL.
3964 */
3965 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003966eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 int evaluate;
3970{
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 long result;
3973 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003974 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976 /*
3977 * Get the first variable.
3978 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003979 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 return FAIL;
3981
3982 /*
3983 * Repeat until there is no following "&&".
3984 */
3985 first = TRUE;
3986 result = TRUE;
3987 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3988 {
3989 if (evaluate && first)
3990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003993 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003994 if (error)
3995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 first = FALSE;
3997 }
3998
3999 /*
4000 * Get the second variable.
4001 */
4002 *arg = skipwhite(*arg + 2);
4003 if (eval4(arg, &var2, evaluate && result) == FAIL)
4004 return FAIL;
4005
4006 /*
4007 * Compute the result.
4008 */
4009 if (evaluate && result)
4010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004011 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 if (error)
4015 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 if (evaluate)
4018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 rettv->v_type = VAR_NUMBER;
4020 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022 }
4023
4024 return OK;
4025}
4026
4027/*
4028 * Handle third level expression:
4029 * var1 == var2
4030 * var1 =~ var2
4031 * var1 != var2
4032 * var1 !~ var2
4033 * var1 > var2
4034 * var1 >= var2
4035 * var1 < var2
4036 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004037 * var1 is var2
4038 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 *
4040 * "arg" must point to the first non-white of the expression.
4041 * "arg" is advanced to the next non-white after the recognized expression.
4042 *
4043 * Return OK or FAIL.
4044 */
4045 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 int evaluate;
4050{
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 char_u *p;
4053 int i;
4054 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004055 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 int len = 2;
4057 long n1, n2;
4058 char_u *s1, *s2;
4059 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4060 regmatch_T regmatch;
4061 int ic;
4062 char_u *save_cpo;
4063
4064 /*
4065 * Get the first variable.
4066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 return FAIL;
4069
4070 p = *arg;
4071 switch (p[0])
4072 {
4073 case '=': if (p[1] == '=')
4074 type = TYPE_EQUAL;
4075 else if (p[1] == '~')
4076 type = TYPE_MATCH;
4077 break;
4078 case '!': if (p[1] == '=')
4079 type = TYPE_NEQUAL;
4080 else if (p[1] == '~')
4081 type = TYPE_NOMATCH;
4082 break;
4083 case '>': if (p[1] != '=')
4084 {
4085 type = TYPE_GREATER;
4086 len = 1;
4087 }
4088 else
4089 type = TYPE_GEQUAL;
4090 break;
4091 case '<': if (p[1] != '=')
4092 {
4093 type = TYPE_SMALLER;
4094 len = 1;
4095 }
4096 else
4097 type = TYPE_SEQUAL;
4098 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004099 case 'i': if (p[1] == 's')
4100 {
4101 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4102 len = 5;
4103 if (!vim_isIDc(p[len]))
4104 {
4105 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4106 type_is = TRUE;
4107 }
4108 }
4109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111
4112 /*
4113 * If there is a comparitive operator, use it.
4114 */
4115 if (type != TYPE_UNKNOWN)
4116 {
4117 /* extra question mark appended: ignore case */
4118 if (p[len] == '?')
4119 {
4120 ic = TRUE;
4121 ++len;
4122 }
4123 /* extra '#' appended: match case */
4124 else if (p[len] == '#')
4125 {
4126 ic = FALSE;
4127 ++len;
4128 }
4129 /* nothing appened: use 'ignorecase' */
4130 else
4131 ic = p_ic;
4132
4133 /*
4134 * Get the second variable.
4135 */
4136 *arg = skipwhite(p + len);
4137 if (eval5(arg, &var2, evaluate) == FAIL)
4138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 return FAIL;
4141 }
4142
4143 if (evaluate)
4144 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004145 if (type_is && rettv->v_type != var2.v_type)
4146 {
4147 /* For "is" a different type always means FALSE, for "notis"
4148 * it means TRUE. */
4149 n1 = (type == TYPE_NEQUAL);
4150 }
4151 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4152 {
4153 if (type_is)
4154 {
4155 n1 = (rettv->v_type == var2.v_type
4156 && rettv->vval.v_list == var2.vval.v_list);
4157 if (type == TYPE_NEQUAL)
4158 n1 = !n1;
4159 }
4160 else if (rettv->v_type != var2.v_type
4161 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4162 {
4163 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004164 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004165 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004166 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 clear_tv(rettv);
4168 clear_tv(&var2);
4169 return FAIL;
4170 }
4171 else
4172 {
4173 /* Compare two Lists for being equal or unequal. */
4174 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4175 if (type == TYPE_NEQUAL)
4176 n1 = !n1;
4177 }
4178 }
4179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004180 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4181 {
4182 if (type_is)
4183 {
4184 n1 = (rettv->v_type == var2.v_type
4185 && rettv->vval.v_dict == var2.vval.v_dict);
4186 if (type == TYPE_NEQUAL)
4187 n1 = !n1;
4188 }
4189 else if (rettv->v_type != var2.v_type
4190 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4191 {
4192 if (rettv->v_type != var2.v_type)
4193 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4194 else
4195 EMSG(_("E736: Invalid operation for Dictionary"));
4196 clear_tv(rettv);
4197 clear_tv(&var2);
4198 return FAIL;
4199 }
4200 else
4201 {
4202 /* Compare two Dictionaries for being equal or unequal. */
4203 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4204 if (type == TYPE_NEQUAL)
4205 n1 = !n1;
4206 }
4207 }
4208
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004209 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4210 {
4211 if (rettv->v_type != var2.v_type
4212 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4213 {
4214 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004215 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004217 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004218 clear_tv(rettv);
4219 clear_tv(&var2);
4220 return FAIL;
4221 }
4222 else
4223 {
4224 /* Compare two Funcrefs for being equal or unequal. */
4225 if (rettv->vval.v_string == NULL
4226 || var2.vval.v_string == NULL)
4227 n1 = FALSE;
4228 else
4229 n1 = STRCMP(rettv->vval.v_string,
4230 var2.vval.v_string) == 0;
4231 if (type == TYPE_NEQUAL)
4232 n1 = !n1;
4233 }
4234 }
4235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 /*
4237 * If one of the two variables is a number, compare as a number.
4238 * When using "=~" or "!~", always compare as string.
4239 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004240 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 n1 = get_tv_number(rettv);
4244 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 switch (type)
4246 {
4247 case TYPE_EQUAL: n1 = (n1 == n2); break;
4248 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4249 case TYPE_GREATER: n1 = (n1 > n2); break;
4250 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4251 case TYPE_SMALLER: n1 = (n1 < n2); break;
4252 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4253 case TYPE_UNKNOWN:
4254 case TYPE_MATCH:
4255 case TYPE_NOMATCH: break; /* avoid gcc warning */
4256 }
4257 }
4258 else
4259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 s1 = get_tv_string_buf(rettv, buf1);
4261 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4263 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4264 else
4265 i = 0;
4266 n1 = FALSE;
4267 switch (type)
4268 {
4269 case TYPE_EQUAL: n1 = (i == 0); break;
4270 case TYPE_NEQUAL: n1 = (i != 0); break;
4271 case TYPE_GREATER: n1 = (i > 0); break;
4272 case TYPE_GEQUAL: n1 = (i >= 0); break;
4273 case TYPE_SMALLER: n1 = (i < 0); break;
4274 case TYPE_SEQUAL: n1 = (i <= 0); break;
4275
4276 case TYPE_MATCH:
4277 case TYPE_NOMATCH:
4278 /* avoid 'l' flag in 'cpoptions' */
4279 save_cpo = p_cpo;
4280 p_cpo = (char_u *)"";
4281 regmatch.regprog = vim_regcomp(s2,
4282 RE_MAGIC + RE_STRING);
4283 regmatch.rm_ic = ic;
4284 if (regmatch.regprog != NULL)
4285 {
4286 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4287 vim_free(regmatch.regprog);
4288 if (type == TYPE_NOMATCH)
4289 n1 = !n1;
4290 }
4291 p_cpo = save_cpo;
4292 break;
4293
4294 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4295 }
4296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 clear_tv(rettv);
4298 clear_tv(&var2);
4299 rettv->v_type = VAR_NUMBER;
4300 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
4303
4304 return OK;
4305}
4306
4307/*
4308 * Handle fourth level expression:
4309 * + number addition
4310 * - number subtraction
4311 * . string concatenation
4312 *
4313 * "arg" must point to the first non-white of the expression.
4314 * "arg" is advanced to the next non-white after the recognized expression.
4315 *
4316 * Return OK or FAIL.
4317 */
4318 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 int evaluate;
4323{
Bram Moolenaar33570922005-01-25 22:26:29 +00004324 typval_T var2;
4325 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int op;
4327 long n1, n2;
4328 char_u *s1, *s2;
4329 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4330 char_u *p;
4331
4332 /*
4333 * Get the first variable.
4334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return FAIL;
4337
4338 /*
4339 * Repeat computing, until no '+', '-' or '.' is following.
4340 */
4341 for (;;)
4342 {
4343 op = **arg;
4344 if (op != '+' && op != '-' && op != '.')
4345 break;
4346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 if (op != '+' || rettv->v_type != VAR_LIST)
4348 {
4349 /* For "list + ...", an illegal use of the first operand as
4350 * a number cannot be determined before evaluating the 2nd
4351 * operand: if this is also a list, all is ok.
4352 * For "something . ...", "something - ..." or "non-list + ...",
4353 * we know that the first operand needs to be a string or number
4354 * without evaluating the 2nd operand. So check before to avoid
4355 * side effects after an error. */
4356 if (evaluate && get_tv_string_chk(rettv) == NULL)
4357 {
4358 clear_tv(rettv);
4359 return FAIL;
4360 }
4361 }
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 /*
4364 * Get the second variable.
4365 */
4366 *arg = skipwhite(*arg + 1);
4367 if (eval6(arg, &var2, evaluate) == FAIL)
4368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 return FAIL;
4371 }
4372
4373 if (evaluate)
4374 {
4375 /*
4376 * Compute the result.
4377 */
4378 if (op == '.')
4379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4381 s2 = get_tv_string_buf_chk(&var2, buf2);
4382 if (s2 == NULL) /* type error ? */
4383 {
4384 clear_tv(rettv);
4385 clear_tv(&var2);
4386 return FAIL;
4387 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004388 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389 clear_tv(rettv);
4390 rettv->v_type = VAR_STRING;
4391 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004393 else if (op == '+' && rettv->v_type == VAR_LIST
4394 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 {
4396 /* concatenate Lists */
4397 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4398 &var3) == FAIL)
4399 {
4400 clear_tv(rettv);
4401 clear_tv(&var2);
4402 return FAIL;
4403 }
4404 clear_tv(rettv);
4405 *rettv = var3;
4406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 else
4408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 int error = FALSE;
4410
4411 n1 = get_tv_number_chk(rettv, &error);
4412 if (error)
4413 {
4414 /* This can only happen for "list + non-list".
4415 * For "non-list + ..." or "something - ...", we returned
4416 * before evaluating the 2nd operand. */
4417 clear_tv(rettv);
4418 return FAIL;
4419 }
4420 n2 = get_tv_number_chk(&var2, &error);
4421 if (error)
4422 {
4423 clear_tv(rettv);
4424 clear_tv(&var2);
4425 return FAIL;
4426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004427 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 if (op == '+')
4429 n1 = n1 + n2;
4430 else
4431 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 rettv->v_type = VAR_NUMBER;
4433 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437 }
4438 return OK;
4439}
4440
4441/*
4442 * Handle fifth level expression:
4443 * * number multiplication
4444 * / number division
4445 * % number modulo
4446 *
4447 * "arg" must point to the first non-white of the expression.
4448 * "arg" is advanced to the next non-white after the recognized expression.
4449 *
4450 * Return OK or FAIL.
4451 */
4452 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004453eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 int evaluate;
4457{
Bram Moolenaar33570922005-01-25 22:26:29 +00004458 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 int op;
4460 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004461 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 /*
4464 * Get the first variable.
4465 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004466 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 return FAIL;
4468
4469 /*
4470 * Repeat computing, until no '*', '/' or '%' is following.
4471 */
4472 for (;;)
4473 {
4474 op = **arg;
4475 if (op != '*' && op != '/' && op != '%')
4476 break;
4477
4478 if (evaluate)
4479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004480 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 if (error)
4483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 else
4486 n1 = 0;
4487
4488 /*
4489 * Get the second variable.
4490 */
4491 *arg = skipwhite(*arg + 1);
4492 if (eval7(arg, &var2, evaluate) == FAIL)
4493 return FAIL;
4494
4495 if (evaluate)
4496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004497 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 if (error)
4500 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 /*
4503 * Compute the result.
4504 */
4505 if (op == '*')
4506 n1 = n1 * n2;
4507 else if (op == '/')
4508 {
4509 if (n2 == 0) /* give an error message? */
4510 n1 = 0x7fffffffL;
4511 else
4512 n1 = n1 / n2;
4513 }
4514 else
4515 {
4516 if (n2 == 0) /* give an error message? */
4517 n1 = 0;
4518 else
4519 n1 = n1 % n2;
4520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 rettv->v_type = VAR_NUMBER;
4522 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 }
4525
4526 return OK;
4527}
4528
4529/*
4530 * Handle sixth level expression:
4531 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004532 * "string" string constant
4533 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 * &option-name option value
4535 * @r register contents
4536 * identifier variable value
4537 * function() function call
4538 * $VAR environment variable
4539 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004540 * [expr, expr] List
4541 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 *
4543 * Also handle:
4544 * ! in front logical NOT
4545 * - in front unary minus
4546 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 * trailing [] subscript in String or List
4548 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 *
4550 * "arg" must point to the first non-white of the expression.
4551 * "arg" is advanced to the next non-white after the recognized expression.
4552 *
4553 * Return OK or FAIL.
4554 */
4555 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004556eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 int evaluate;
4560{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 long n;
4562 int len;
4563 char_u *s;
4564 int val;
4565 char_u *start_leader, *end_leader;
4566 int ret = OK;
4567 char_u *alias;
4568
4569 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004570 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004571 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 /*
4576 * Skip '!' and '-' characters. They are handled later.
4577 */
4578 start_leader = *arg;
4579 while (**arg == '!' || **arg == '-' || **arg == '+')
4580 *arg = skipwhite(*arg + 1);
4581 end_leader = *arg;
4582
4583 switch (**arg)
4584 {
4585 /*
4586 * Number constant.
4587 */
4588 case '0':
4589 case '1':
4590 case '2':
4591 case '3':
4592 case '4':
4593 case '5':
4594 case '6':
4595 case '7':
4596 case '8':
4597 case '9':
4598 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4599 *arg += len;
4600 if (evaluate)
4601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 rettv->v_type = VAR_NUMBER;
4603 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605 break;
4606
4607 /*
4608 * String constant: "string".
4609 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004610 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612
4613 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 break;
4618
4619 /*
4620 * List: [expr, expr]
4621 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
4624
4625 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 * Dictionary: {key: val, key: val}
4627 */
4628 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4629 break;
4630
4631 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004632 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004634 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 break;
4636
4637 /*
4638 * Environment variable: $VAR.
4639 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 break;
4642
4643 /*
4644 * Register contents: @r.
4645 */
4646 case '@': ++*arg;
4647 if (evaluate)
4648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004649 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004650 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 if (**arg != NUL)
4653 ++*arg;
4654 break;
4655
4656 /*
4657 * nested expression: (expression).
4658 */
4659 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (**arg == ')')
4662 ++*arg;
4663 else if (ret == OK)
4664 {
4665 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 ret = FAIL;
4668 }
4669 break;
4670
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 break;
4673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674
4675 if (ret == NOTDONE)
4676 {
4677 /*
4678 * Must be a variable or function name.
4679 * Can also be a curly-braces kind of name: {expr}.
4680 */
4681 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004682 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 if (alias != NULL)
4684 s = alias;
4685
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004686 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 ret = FAIL;
4688 else
4689 {
4690 if (**arg == '(') /* recursive! */
4691 {
4692 /* If "s" is the name of a variable of type VAR_FUNC
4693 * use its contents. */
4694 s = deref_func_name(s, &len);
4695
4696 /* Invoke the function. */
4697 ret = get_func_tv(s, len, rettv, arg,
4698 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004699 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 /* Stop the expression evaluation when immediately
4701 * aborting on error, or when an interrupt occurred or
4702 * an exception was thrown but not caught. */
4703 if (aborting())
4704 {
4705 if (ret == OK)
4706 clear_tv(rettv);
4707 ret = FAIL;
4708 }
4709 }
4710 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004711 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004712 else
4713 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 }
4715
4716 if (alias != NULL)
4717 vim_free(alias);
4718 }
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 *arg = skipwhite(*arg);
4721
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004722 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4723 * expr(expr). */
4724 if (ret == OK)
4725 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
4727 /*
4728 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4729 */
4730 if (ret == OK && evaluate && end_leader > start_leader)
4731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 int error = FALSE;
4733
4734 val = get_tv_number_chk(rettv, &error);
4735 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737 clear_tv(rettv);
4738 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 else
4741 {
4742 while (end_leader > start_leader)
4743 {
4744 --end_leader;
4745 if (*end_leader == '!')
4746 val = !val;
4747 else if (*end_leader == '-')
4748 val = -val;
4749 }
4750 clear_tv(rettv);
4751 rettv->v_type = VAR_NUMBER;
4752 rettv->vval.v_number = val;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
4755
4756 return ret;
4757}
4758
4759/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004760 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4761 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4763 */
4764 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004765eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004769 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770{
4771 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004772 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 long len = -1;
4775 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004781 if (verbose)
4782 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 return FAIL;
4784 }
4785
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 /*
4789 * dict.name
4790 */
4791 key = *arg + 1;
4792 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4793 ;
4794 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004795 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 }
4798 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 /*
4801 * something[idx]
4802 *
4803 * Get the (first) variable from inside the [].
4804 */
4805 *arg = skipwhite(*arg + 1);
4806 if (**arg == ':')
4807 empty1 = TRUE;
4808 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4809 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4811 {
4812 /* not a number or string */
4813 clear_tv(&var1);
4814 return FAIL;
4815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816
4817 /*
4818 * Get the second variable from inside the [:].
4819 */
4820 if (**arg == ':')
4821 {
4822 range = TRUE;
4823 *arg = skipwhite(*arg + 1);
4824 if (**arg == ']')
4825 empty2 = TRUE;
4826 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 if (!empty1)
4829 clear_tv(&var1);
4830 return FAIL;
4831 }
4832 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4833 {
4834 /* not a number or string */
4835 if (!empty1)
4836 clear_tv(&var1);
4837 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004838 return FAIL;
4839 }
4840 }
4841
4842 /* Check for the ']'. */
4843 if (**arg != ']')
4844 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004845 if (verbose)
4846 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 clear_tv(&var1);
4848 if (range)
4849 clear_tv(&var2);
4850 return FAIL;
4851 }
4852 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004853 }
4854
4855 if (evaluate)
4856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004857 n1 = 0;
4858 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 n1 = get_tv_number(&var1);
4861 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 }
4863 if (range)
4864 {
4865 if (empty2)
4866 n2 = -1;
4867 else
4868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 n2 = get_tv_number(&var2);
4870 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 }
4872 }
4873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 {
4876 case VAR_NUMBER:
4877 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 len = (long)STRLEN(s);
4880 if (range)
4881 {
4882 /* The resulting variable is a substring. If the indexes
4883 * are out of range the result is empty. */
4884 if (n1 < 0)
4885 {
4886 n1 = len + n1;
4887 if (n1 < 0)
4888 n1 = 0;
4889 }
4890 if (n2 < 0)
4891 n2 = len + n2;
4892 else if (n2 >= len)
4893 n2 = len;
4894 if (n1 >= len || n2 < 0 || n1 > n2)
4895 s = NULL;
4896 else
4897 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4898 }
4899 else
4900 {
4901 /* The resulting variable is a string of a single
4902 * character. If the index is too big or negative the
4903 * result is empty. */
4904 if (n1 >= len || n1 < 0)
4905 s = NULL;
4906 else
4907 s = vim_strnsave(s + n1, 1);
4908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 clear_tv(rettv);
4910 rettv->v_type = VAR_STRING;
4911 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004912 break;
4913
4914 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916 if (n1 < 0)
4917 n1 = len + n1;
4918 if (!empty1 && (n1 < 0 || n1 >= len))
4919 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004920 /* For a range we allow invalid values and return an empty
4921 * list. A list index out of range is an error. */
4922 if (!range)
4923 {
4924 if (verbose)
4925 EMSGN(_(e_listidx), n1);
4926 return FAIL;
4927 }
4928 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004929 }
4930 if (range)
4931 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004932 list_T *l;
4933 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934
4935 if (n2 < 0)
4936 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004937 else if (n2 >= len)
4938 n2 = len - 1;
4939 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004940 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004941 l = list_alloc();
4942 if (l == NULL)
4943 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004944 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 n1 <= n2; ++n1)
4946 {
4947 if (list_append_tv(l, &item->li_tv) == FAIL)
4948 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004949 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004950 return FAIL;
4951 }
4952 item = item->li_next;
4953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004954 clear_tv(rettv);
4955 rettv->v_type = VAR_LIST;
4956 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004957 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958 }
4959 else
4960 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004961 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004962 clear_tv(rettv);
4963 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004964 }
4965 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966
4967 case VAR_DICT:
4968 if (range)
4969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004970 if (verbose)
4971 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 if (len == -1)
4973 clear_tv(&var1);
4974 return FAIL;
4975 }
4976 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004977 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978
4979 if (len == -1)
4980 {
4981 key = get_tv_string(&var1);
4982 if (*key == NUL)
4983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004984 if (verbose)
4985 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 clear_tv(&var1);
4987 return FAIL;
4988 }
4989 }
4990
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004991 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004994 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 if (len == -1)
4996 clear_tv(&var1);
4997 if (item == NULL)
4998 return FAIL;
4999
5000 copy_tv(&item->di_tv, &var1);
5001 clear_tv(rettv);
5002 *rettv = var1;
5003 }
5004 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 }
5006 }
5007
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 return OK;
5009}
5010
5011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 * Get an option value.
5013 * "arg" points to the '&' or '+' before the option name.
5014 * "arg" is advanced to character after the option name.
5015 * Return OK or FAIL.
5016 */
5017 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005020 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 int evaluate;
5022{
5023 char_u *option_end;
5024 long numval;
5025 char_u *stringval;
5026 int opt_type;
5027 int c;
5028 int working = (**arg == '+'); /* has("+option") */
5029 int ret = OK;
5030 int opt_flags;
5031
5032 /*
5033 * Isolate the option name and find its value.
5034 */
5035 option_end = find_option_end(arg, &opt_flags);
5036 if (option_end == NULL)
5037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005038 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 EMSG2(_("E112: Option name missing: %s"), *arg);
5040 return FAIL;
5041 }
5042
5043 if (!evaluate)
5044 {
5045 *arg = option_end;
5046 return OK;
5047 }
5048
5049 c = *option_end;
5050 *option_end = NUL;
5051 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
5054 if (opt_type == -3) /* invalid name */
5055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 EMSG2(_("E113: Unknown option: %s"), *arg);
5058 ret = FAIL;
5059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 if (opt_type == -2) /* hidden string option */
5063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 rettv->v_type = VAR_STRING;
5065 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067 else if (opt_type == -1) /* hidden number option */
5068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 rettv->v_type = VAR_NUMBER;
5070 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 else if (opt_type == 1) /* number option */
5073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 rettv->v_type = VAR_NUMBER;
5075 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 else /* string option */
5078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 rettv->v_type = VAR_STRING;
5080 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 }
5083 else if (working && (opt_type == -2 || opt_type == -1))
5084 ret = FAIL;
5085
5086 *option_end = c; /* put back for error messages */
5087 *arg = option_end;
5088
5089 return ret;
5090}
5091
5092/*
5093 * Allocate a variable for a string constant.
5094 * Return OK or FAIL.
5095 */
5096 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 int evaluate;
5101{
5102 char_u *p;
5103 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 int extra = 0;
5105
5106 /*
5107 * Find the end of the string, skipping backslashed characters.
5108 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 if (*p == '\\' && p[1] != NUL)
5112 {
5113 ++p;
5114 /* A "\<x>" form occupies at least 4 characters, and produces up
5115 * to 6 characters: reserve space for 2 extra */
5116 if (*p == '<')
5117 extra += 2;
5118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120
5121 if (*p != '"')
5122 {
5123 EMSG2(_("E114: Missing quote: %s"), *arg);
5124 return FAIL;
5125 }
5126
5127 /* If only parsing, set *arg and return here */
5128 if (!evaluate)
5129 {
5130 *arg = p + 1;
5131 return OK;
5132 }
5133
5134 /*
5135 * Copy the string into allocated memory, handling backslashed
5136 * characters.
5137 */
5138 name = alloc((unsigned)(p - *arg + extra));
5139 if (name == NULL)
5140 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 rettv->v_type = VAR_STRING;
5142 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 if (*p == '\\')
5147 {
5148 switch (*++p)
5149 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 case 'b': *name++ = BS; ++p; break;
5151 case 'e': *name++ = ESC; ++p; break;
5152 case 'f': *name++ = FF; ++p; break;
5153 case 'n': *name++ = NL; ++p; break;
5154 case 'r': *name++ = CAR; ++p; break;
5155 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
5157 case 'X': /* hex: "\x1", "\x12" */
5158 case 'x':
5159 case 'u': /* Unicode: "\u0023" */
5160 case 'U':
5161 if (vim_isxdigit(p[1]))
5162 {
5163 int n, nr;
5164 int c = toupper(*p);
5165
5166 if (c == 'X')
5167 n = 2;
5168 else
5169 n = 4;
5170 nr = 0;
5171 while (--n >= 0 && vim_isxdigit(p[1]))
5172 {
5173 ++p;
5174 nr = (nr << 4) + hex2nr(*p);
5175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177#ifdef FEAT_MBYTE
5178 /* For "\u" store the number according to
5179 * 'encoding'. */
5180 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 else
5183#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187
5188 /* octal: "\1", "\12", "\123" */
5189 case '0':
5190 case '1':
5191 case '2':
5192 case '3':
5193 case '4':
5194 case '5':
5195 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 case '7': *name = *p++ - '0';
5197 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 *name = (*name << 3) + *p++ - '0';
5200 if (*p >= '0' && *p <= '7')
5201 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 break;
5205
5206 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (extra != 0)
5209 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212 }
5213 /* FALLTHROUGH */
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 break;
5217 }
5218 }
5219 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 *arg = p + 1;
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 return OK;
5227}
5228
5229/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 * Return OK or FAIL.
5232 */
5233 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005234get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 int evaluate;
5238{
5239 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005240 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 int reduce = 0;
5242
5243 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005245 */
5246 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5247 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005251 break;
5252 ++reduce;
5253 ++p;
5254 }
5255 }
5256
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005258 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005260 return FAIL;
5261 }
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005264 if (!evaluate)
5265 {
5266 *arg = p + 1;
5267 return OK;
5268 }
5269
5270 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005272 */
5273 str = alloc((unsigned)((p - *arg) - reduce));
5274 if (str == NULL)
5275 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 rettv->v_type = VAR_STRING;
5277 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005278
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005280 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005282 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005284 break;
5285 ++p;
5286 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005290 *arg = p + 1;
5291
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005292 return OK;
5293}
5294
5295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 * Allocate a variable for a List and fill it from "*arg".
5297 * Return OK or FAIL.
5298 */
5299 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005300get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005302 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 int evaluate;
5304{
Bram Moolenaar33570922005-01-25 22:26:29 +00005305 list_T *l = NULL;
5306 typval_T tv;
5307 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308
5309 if (evaluate)
5310 {
5311 l = list_alloc();
5312 if (l == NULL)
5313 return FAIL;
5314 }
5315
5316 *arg = skipwhite(*arg + 1);
5317 while (**arg != ']' && **arg != NUL)
5318 {
5319 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5320 goto failret;
5321 if (evaluate)
5322 {
5323 item = listitem_alloc();
5324 if (item != NULL)
5325 {
5326 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005327 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 list_append(l, item);
5329 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005330 else
5331 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 }
5333
5334 if (**arg == ']')
5335 break;
5336 if (**arg != ',')
5337 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005338 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 goto failret;
5340 }
5341 *arg = skipwhite(*arg + 1);
5342 }
5343
5344 if (**arg != ']')
5345 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347failret:
5348 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005349 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
5351 }
5352
5353 *arg = skipwhite(*arg + 1);
5354 if (evaluate)
5355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 rettv->v_type = VAR_LIST;
5357 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 ++l->lv_refcount;
5359 }
5360
5361 return OK;
5362}
5363
5364/*
5365 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005366 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005368 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369list_alloc()
5370{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005371 list_T *l;
5372
5373 l = (list_T *)alloc_clear(sizeof(list_T));
5374 if (l != NULL)
5375 {
5376 /* Prepend the list to the list of lists for garbage collection. */
5377 if (first_list != NULL)
5378 first_list->lv_used_prev = l;
5379 l->lv_used_prev = NULL;
5380 l->lv_used_next = first_list;
5381 first_list = l;
5382 }
5383 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384}
5385
5386/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005387 * Allocate an empty list for a return value.
5388 * Returns OK or FAIL.
5389 */
5390 static int
5391rettv_list_alloc(rettv)
5392 typval_T *rettv;
5393{
5394 list_T *l = list_alloc();
5395
5396 if (l == NULL)
5397 return FAIL;
5398
5399 rettv->vval.v_list = l;
5400 rettv->v_type = VAR_LIST;
5401 ++l->lv_refcount;
5402 return OK;
5403}
5404
5405/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 * Unreference a list: decrement the reference count and free it when it
5407 * becomes zero.
5408 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005409 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005413 if (l != NULL && --l->lv_refcount <= 0)
5414 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415}
5416
5417/*
5418 * Free a list, including all items it points to.
5419 * Ignores the reference count.
5420 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005421 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005422list_free(l, recurse)
5423 list_T *l;
5424 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425{
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005428 /* Remove the list from the list of lists for garbage collection. */
5429 if (l->lv_used_prev == NULL)
5430 first_list = l->lv_used_next;
5431 else
5432 l->lv_used_prev->lv_used_next = l->lv_used_next;
5433 if (l->lv_used_next != NULL)
5434 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5435
Bram Moolenaard9fba312005-06-26 22:34:35 +00005436 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005438 /* Remove the item before deleting it. */
5439 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005440 if (recurse || (item->li_tv.v_type != VAR_LIST
5441 && item->li_tv.v_type != VAR_DICT))
5442 clear_tv(&item->li_tv);
5443 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 vim_free(l);
5446}
5447
5448/*
5449 * Allocate a list item.
5450 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005451 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452listitem_alloc()
5453{
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455}
5456
5457/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005458 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 */
5460 static void
5461listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 vim_free(item);
5466}
5467
5468/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005469 * Remove a list item from a List and free it. Also clears the value.
5470 */
5471 static void
5472listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 list_T *l;
5474 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005475{
5476 list_remove(l, item, item);
5477 listitem_free(item);
5478}
5479
5480/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 * Get the number of items in a list.
5482 */
5483 static long
5484list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005485 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 if (l == NULL)
5488 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005489 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490}
5491
5492/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005493 * Return TRUE when two lists have exactly the same values.
5494 */
5495 static int
5496list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 list_T *l1;
5498 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005499 int ic; /* ignore case for strings */
5500{
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005502
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005503 if (l1 == l2)
5504 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005505 if (list_len(l1) != list_len(l2))
5506 return FALSE;
5507
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005508 for (item1 = l1->lv_first, item2 = l2->lv_first;
5509 item1 != NULL && item2 != NULL;
5510 item1 = item1->li_next, item2 = item2->li_next)
5511 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5512 return FALSE;
5513 return item1 == NULL && item2 == NULL;
5514}
5515
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005516#if defined(FEAT_PYTHON) || defined(PROTO)
5517/*
5518 * Return the dictitem that an entry in a hashtable points to.
5519 */
5520 dictitem_T *
5521dict_lookup(hi)
5522 hashitem_T *hi;
5523{
5524 return HI2DI(hi);
5525}
5526#endif
5527
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005528/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005529 * Return TRUE when two dictionaries have exactly the same key/values.
5530 */
5531 static int
5532dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005533 dict_T *d1;
5534 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005535 int ic; /* ignore case for strings */
5536{
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 hashitem_T *hi;
5538 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005539 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005540
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005541 if (d1 == d2)
5542 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005543 if (dict_len(d1) != dict_len(d2))
5544 return FALSE;
5545
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005546 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005547 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 if (!HASHITEM_EMPTY(hi))
5550 {
5551 item2 = dict_find(d2, hi->hi_key, -1);
5552 if (item2 == NULL)
5553 return FALSE;
5554 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5555 return FALSE;
5556 --todo;
5557 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005558 }
5559 return TRUE;
5560}
5561
5562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005563 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005564 * Compares the items just like "==" would compare them, but strings and
5565 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005566 */
5567 static int
5568tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005569 typval_T *tv1;
5570 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005571 int ic; /* ignore case */
5572{
5573 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005574 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005575 static int recursive = 0; /* cach recursive loops */
5576 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005577
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005578 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005579 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005580 /* Catch lists and dicts that have an endless loop by limiting
5581 * recursiveness to 1000. We guess they are equal then. */
5582 if (recursive >= 1000)
5583 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005584
5585 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005586 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005587 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005588 ++recursive;
5589 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5590 --recursive;
5591 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005592
5593 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005594 ++recursive;
5595 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5596 --recursive;
5597 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005598
5599 case VAR_FUNC:
5600 return (tv1->vval.v_string != NULL
5601 && tv2->vval.v_string != NULL
5602 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5603
5604 case VAR_NUMBER:
5605 return tv1->vval.v_number == tv2->vval.v_number;
5606
5607 case VAR_STRING:
5608 s1 = get_tv_string_buf(tv1, buf1);
5609 s2 = get_tv_string_buf(tv2, buf2);
5610 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005611 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005612
5613 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005614 return TRUE;
5615}
5616
5617/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 * Locate item with index "n" in list "l" and return it.
5619 * A negative index is counted from the end; -1 is the last item.
5620 * Returns NULL when "n" is out of range.
5621 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625 long n;
5626{
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 long idx;
5629
5630 if (l == NULL)
5631 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005632
5633 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005635 n = l->lv_len + n;
5636
5637 /* Check for index out of range. */
5638 if (n < 0 || n >= l->lv_len)
5639 return NULL;
5640
5641 /* When there is a cached index may start search from there. */
5642 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005643 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005644 if (n < l->lv_idx / 2)
5645 {
5646 /* closest to the start of the list */
5647 item = l->lv_first;
5648 idx = 0;
5649 }
5650 else if (n > (l->lv_idx + l->lv_len) / 2)
5651 {
5652 /* closest to the end of the list */
5653 item = l->lv_last;
5654 idx = l->lv_len - 1;
5655 }
5656 else
5657 {
5658 /* closest to the cached index */
5659 item = l->lv_idx_item;
5660 idx = l->lv_idx;
5661 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 }
5663 else
5664 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005665 if (n < l->lv_len / 2)
5666 {
5667 /* closest to the start of the list */
5668 item = l->lv_first;
5669 idx = 0;
5670 }
5671 else
5672 {
5673 /* closest to the end of the list */
5674 item = l->lv_last;
5675 idx = l->lv_len - 1;
5676 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005678
5679 while (n > idx)
5680 {
5681 /* search forward */
5682 item = item->li_next;
5683 ++idx;
5684 }
5685 while (n < idx)
5686 {
5687 /* search backward */
5688 item = item->li_prev;
5689 --idx;
5690 }
5691
5692 /* cache the used index */
5693 l->lv_idx = idx;
5694 l->lv_idx_item = item;
5695
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 return item;
5697}
5698
5699/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005700 * Get list item "l[idx]" as a number.
5701 */
5702 static long
5703list_find_nr(l, idx, errorp)
5704 list_T *l;
5705 long idx;
5706 int *errorp; /* set to TRUE when something wrong */
5707{
5708 listitem_T *li;
5709
5710 li = list_find(l, idx);
5711 if (li == NULL)
5712 {
5713 if (errorp != NULL)
5714 *errorp = TRUE;
5715 return -1L;
5716 }
5717 return get_tv_number_chk(&li->li_tv, errorp);
5718}
5719
5720/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005721 * Locate "item" list "l" and return its index.
5722 * Returns -1 when "item" is not in the list.
5723 */
5724 static long
5725list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 list_T *l;
5727 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005728{
5729 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005731
5732 if (l == NULL)
5733 return -1;
5734 idx = 0;
5735 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5736 ++idx;
5737 if (li == NULL)
5738 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005739 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005740}
5741
5742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743 * Append item "item" to the end of list "l".
5744 */
5745 static void
5746list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005747 list_T *l;
5748 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749{
5750 if (l->lv_last == NULL)
5751 {
5752 /* empty list */
5753 l->lv_first = item;
5754 l->lv_last = item;
5755 item->li_prev = NULL;
5756 }
5757 else
5758 {
5759 l->lv_last->li_next = item;
5760 item->li_prev = l->lv_last;
5761 l->lv_last = item;
5762 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005763 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 item->li_next = NULL;
5765}
5766
5767/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005769 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 */
5771 static int
5772list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005773 list_T *l;
5774 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005776 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777
Bram Moolenaar05159a02005-02-26 23:04:13 +00005778 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005780 copy_tv(tv, &li->li_tv);
5781 list_append(l, li);
5782 return OK;
5783}
5784
5785/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005786 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005787 * Return FAIL when out of memory.
5788 */
5789 int
5790list_append_dict(list, dict)
5791 list_T *list;
5792 dict_T *dict;
5793{
5794 listitem_T *li = listitem_alloc();
5795
5796 if (li == NULL)
5797 return FAIL;
5798 li->li_tv.v_type = VAR_DICT;
5799 li->li_tv.v_lock = 0;
5800 li->li_tv.vval.v_dict = dict;
5801 list_append(list, li);
5802 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 return OK;
5804}
5805
5806/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005807 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005808 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005809 * Returns FAIL when out of memory.
5810 */
5811 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005812list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005813 list_T *l;
5814 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005815 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005816{
5817 listitem_T *li = listitem_alloc();
5818
5819 if (li == NULL)
5820 return FAIL;
5821 list_append(l, li);
5822 li->li_tv.v_type = VAR_STRING;
5823 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005824 if (str == NULL)
5825 li->li_tv.vval.v_string = NULL;
5826 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005827 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005828 return FAIL;
5829 return OK;
5830}
5831
5832/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005833 * Append "n" to list "l".
5834 * Returns FAIL when out of memory.
5835 */
5836 static int
5837list_append_number(l, n)
5838 list_T *l;
5839 varnumber_T n;
5840{
5841 listitem_T *li;
5842
5843 li = listitem_alloc();
5844 if (li == NULL)
5845 return FAIL;
5846 li->li_tv.v_type = VAR_NUMBER;
5847 li->li_tv.v_lock = 0;
5848 li->li_tv.vval.v_number = n;
5849 list_append(l, li);
5850 return OK;
5851}
5852
5853/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005855 * If "item" is NULL append at the end.
5856 * Return FAIL when out of memory.
5857 */
5858 static int
5859list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 list_T *l;
5861 typval_T *tv;
5862 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863{
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005865
5866 if (ni == NULL)
5867 return FAIL;
5868 copy_tv(tv, &ni->li_tv);
5869 if (item == NULL)
5870 /* Append new item at end of list. */
5871 list_append(l, ni);
5872 else
5873 {
5874 /* Insert new item before existing item. */
5875 ni->li_prev = item->li_prev;
5876 ni->li_next = item;
5877 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005878 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005880 ++l->lv_idx;
5881 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005883 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005884 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005885 l->lv_idx_item = NULL;
5886 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005888 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889 }
5890 return OK;
5891}
5892
5893/*
5894 * Extend "l1" with "l2".
5895 * If "bef" is NULL append at the end, otherwise insert before this item.
5896 * Returns FAIL when out of memory.
5897 */
5898 static int
5899list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 list_T *l1;
5901 list_T *l2;
5902 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005903{
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005905
5906 for (item = l2->lv_first; item != NULL; item = item->li_next)
5907 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5908 return FAIL;
5909 return OK;
5910}
5911
5912/*
5913 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5914 * Return FAIL when out of memory.
5915 */
5916 static int
5917list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 list_T *l1;
5919 list_T *l2;
5920 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005921{
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923
5924 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005925 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926 if (l == NULL)
5927 return FAIL;
5928 tv->v_type = VAR_LIST;
5929 tv->vval.v_list = l;
5930
5931 /* append all items from the second list */
5932 return list_extend(l, l2, NULL);
5933}
5934
5935/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005938 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 * Returns NULL when out of memory.
5940 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005945 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946{
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 list_T *copy;
5948 listitem_T *item;
5949 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950
5951 if (orig == NULL)
5952 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953
5954 copy = list_alloc();
5955 if (copy != NULL)
5956 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005957 if (copyID != 0)
5958 {
5959 /* Do this before adding the items, because one of the items may
5960 * refer back to this list. */
5961 orig->lv_copyID = copyID;
5962 orig->lv_copylist = copy;
5963 }
5964 for (item = orig->lv_first; item != NULL && !got_int;
5965 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 {
5967 ni = listitem_alloc();
5968 if (ni == NULL)
5969 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005970 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005971 {
5972 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5973 {
5974 vim_free(ni);
5975 break;
5976 }
5977 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 list_append(copy, ni);
5981 }
5982 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005983 if (item != NULL)
5984 {
5985 list_unref(copy);
5986 copy = NULL;
5987 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 }
5989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 return copy;
5991}
5992
5993/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005994 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005995 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005998list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 list_T *l;
6000 listitem_T *item;
6001 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002{
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005 /* notify watchers */
6006 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006008 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 list_fix_watch(l, ip);
6010 if (ip == item2)
6011 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013
6014 if (item2->li_next == NULL)
6015 l->lv_last = item->li_prev;
6016 else
6017 item2->li_next->li_prev = item->li_prev;
6018 if (item->li_prev == NULL)
6019 l->lv_first = item2->li_next;
6020 else
6021 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006022 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023}
6024
6025/*
6026 * Return an allocated string with the string representation of a list.
6027 * May return NULL.
6028 */
6029 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006030list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033{
6034 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035
6036 if (tv->vval.v_list == NULL)
6037 return NULL;
6038 ga_init2(&ga, (int)sizeof(char), 80);
6039 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006040 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006041 {
6042 vim_free(ga.ga_data);
6043 return NULL;
6044 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 ga_append(&ga, ']');
6046 ga_append(&ga, NUL);
6047 return (char_u *)ga.ga_data;
6048}
6049
6050/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006051 * Join list "l" into a string in "*gap", using separator "sep".
6052 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006053 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006056list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006059 char_u *sep;
6060 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006061 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006062{
6063 int first = TRUE;
6064 char_u *tofree;
6065 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006067 char_u *s;
6068
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006069 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006070 {
6071 if (first)
6072 first = FALSE;
6073 else
6074 ga_concat(gap, sep);
6075
6076 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006077 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006078 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006079 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006080 if (s != NULL)
6081 ga_concat(gap, s);
6082 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006083 if (s == NULL)
6084 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006085 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006086 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087}
6088
6089/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006090 * Garbage collection for lists and dictionaries.
6091 *
6092 * We use reference counts to be able to free most items right away when they
6093 * are no longer used. But for composite items it's possible that it becomes
6094 * unused while the reference count is > 0: When there is a recursive
6095 * reference. Example:
6096 * :let l = [1, 2, 3]
6097 * :let d = {9: l}
6098 * :let l[1] = d
6099 *
6100 * Since this is quite unusual we handle this with garbage collection: every
6101 * once in a while find out which lists and dicts are not referenced from any
6102 * variable.
6103 *
6104 * Here is a good reference text about garbage collection (refers to Python
6105 * but it applies to all reference-counting mechanisms):
6106 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006107 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006108
6109/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 * Do garbage collection for lists and dicts.
6111 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006112 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 int
6114garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006115{
6116 dict_T *dd;
6117 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006118 int copyID = ++current_copyID;
6119 buf_T *buf;
6120 win_T *wp;
6121 int i;
6122 funccall_T *fc;
6123 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006124#ifdef FEAT_WINDOWS
6125 tabpage_T *tp;
6126#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006127
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006128 /* Only do this once. */
6129 want_garbage_collect = FALSE;
6130 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006131 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006132
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006133 /*
6134 * 1. Go through all accessible variables and mark all lists and dicts
6135 * with copyID.
6136 */
6137 /* script-local variables */
6138 for (i = 1; i <= ga_scripts.ga_len; ++i)
6139 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6140
6141 /* buffer-local variables */
6142 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6143 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6144
6145 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006146 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006147 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6148
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006149#ifdef FEAT_WINDOWS
6150 /* tabpage-local variables */
6151 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6152 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6153#endif
6154
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006155 /* global variables */
6156 set_ref_in_ht(&globvarht, copyID);
6157
6158 /* function-local variables */
6159 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6160 {
6161 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6162 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6163 }
6164
6165 /*
6166 * 2. Go through the list of dicts and free items without the copyID.
6167 */
6168 for (dd = first_dict; dd != NULL; )
6169 if (dd->dv_copyID != copyID)
6170 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006171 /* Free the Dictionary and ordinary items it contains, but don't
6172 * recurse into Lists and Dictionaries, they will be in the list
6173 * of dicts or list of lists. */
6174 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006175 did_free = TRUE;
6176
6177 /* restart, next dict may also have been freed */
6178 dd = first_dict;
6179 }
6180 else
6181 dd = dd->dv_used_next;
6182
6183 /*
6184 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006185 * But don't free a list that has a watcher (used in a for loop), these
6186 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006187 */
6188 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006189 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006190 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006191 /* Free the List and ordinary items it contains, but don't recurse
6192 * into Lists and Dictionaries, they will be in the list of dicts
6193 * or list of lists. */
6194 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006195 did_free = TRUE;
6196
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006197 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006198 ll = first_list;
6199 }
6200 else
6201 ll = ll->lv_used_next;
6202
6203 return did_free;
6204}
6205
6206/*
6207 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6208 */
6209 static void
6210set_ref_in_ht(ht, copyID)
6211 hashtab_T *ht;
6212 int copyID;
6213{
6214 int todo;
6215 hashitem_T *hi;
6216
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006217 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006218 for (hi = ht->ht_array; todo > 0; ++hi)
6219 if (!HASHITEM_EMPTY(hi))
6220 {
6221 --todo;
6222 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6223 }
6224}
6225
6226/*
6227 * Mark all lists and dicts referenced through list "l" with "copyID".
6228 */
6229 static void
6230set_ref_in_list(l, copyID)
6231 list_T *l;
6232 int copyID;
6233{
6234 listitem_T *li;
6235
6236 for (li = l->lv_first; li != NULL; li = li->li_next)
6237 set_ref_in_item(&li->li_tv, copyID);
6238}
6239
6240/*
6241 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6242 */
6243 static void
6244set_ref_in_item(tv, copyID)
6245 typval_T *tv;
6246 int copyID;
6247{
6248 dict_T *dd;
6249 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006250
6251 switch (tv->v_type)
6252 {
6253 case VAR_DICT:
6254 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006255 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006256 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006257 /* Didn't see this dict yet. */
6258 dd->dv_copyID = copyID;
6259 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006260 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006261 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006262
6263 case VAR_LIST:
6264 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006265 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006266 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006267 /* Didn't see this list yet. */
6268 ll->lv_copyID = copyID;
6269 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006270 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006271 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006272 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006273 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006274}
6275
6276/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006277 * Allocate an empty header for a dictionary.
6278 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006279 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006280dict_alloc()
6281{
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006283
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006286 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006287 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006288 if (first_dict != NULL)
6289 first_dict->dv_used_prev = d;
6290 d->dv_used_next = first_dict;
6291 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006292 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006293
Bram Moolenaar33570922005-01-25 22:26:29 +00006294 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006295 d->dv_lock = 0;
6296 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006297 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006298 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006300}
6301
6302/*
6303 * Unreference a Dictionary: decrement the reference count and free it when it
6304 * becomes zero.
6305 */
6306 static void
6307dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006310 if (d != NULL && --d->dv_refcount <= 0)
6311 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006312}
6313
6314/*
6315 * Free a Dictionary, including all items it contains.
6316 * Ignores the reference count.
6317 */
6318 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006319dict_free(d, recurse)
6320 dict_T *d;
6321 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006322{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006323 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006324 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006325 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006326
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006327 /* Remove the dict from the list of dicts for garbage collection. */
6328 if (d->dv_used_prev == NULL)
6329 first_dict = d->dv_used_next;
6330 else
6331 d->dv_used_prev->dv_used_next = d->dv_used_next;
6332 if (d->dv_used_next != NULL)
6333 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6334
6335 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006336 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006337 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006338 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006339 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006340 if (!HASHITEM_EMPTY(hi))
6341 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006342 /* Remove the item before deleting it, just in case there is
6343 * something recursive causing trouble. */
6344 di = HI2DI(hi);
6345 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006346 if (recurse || (di->di_tv.v_type != VAR_LIST
6347 && di->di_tv.v_type != VAR_DICT))
6348 clear_tv(&di->di_tv);
6349 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006350 --todo;
6351 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354 vim_free(d);
6355}
6356
6357/*
6358 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006359 * The "key" is copied to the new item.
6360 * Note that the value of the item "di_tv" still needs to be initialized!
6361 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006362 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006364dictitem_alloc(key)
6365 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006366{
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006369 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006370 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006373 di->di_flags = 0;
6374 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006376}
6377
6378/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006379 * Make a copy of a Dictionary item.
6380 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006382dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006384{
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006387 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6388 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006389 if (di != NULL)
6390 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006391 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006392 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393 copy_tv(&org->di_tv, &di->di_tv);
6394 }
6395 return di;
6396}
6397
6398/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006399 * Remove item "item" from Dictionary "dict" and free it.
6400 */
6401 static void
6402dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 dict_T *dict;
6404 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006405{
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006407
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006409 if (HASHITEM_EMPTY(hi))
6410 EMSG2(_(e_intern2), "dictitem_remove()");
6411 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006413 dictitem_free(item);
6414}
6415
6416/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417 * Free a dict item. Also clears the value.
6418 */
6419 static void
6420dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006422{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006423 clear_tv(&item->di_tv);
6424 vim_free(item);
6425}
6426
6427/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006428 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6429 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006430 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006431 * Returns NULL when out of memory.
6432 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006435 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006436 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006437 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438{
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 dict_T *copy;
6440 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006441 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006443
6444 if (orig == NULL)
6445 return NULL;
6446
6447 copy = dict_alloc();
6448 if (copy != NULL)
6449 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006450 if (copyID != 0)
6451 {
6452 orig->dv_copyID = copyID;
6453 orig->dv_copydict = copy;
6454 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006455 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006456 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006458 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006459 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006460 --todo;
6461
6462 di = dictitem_alloc(hi->hi_key);
6463 if (di == NULL)
6464 break;
6465 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006466 {
6467 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6468 copyID) == FAIL)
6469 {
6470 vim_free(di);
6471 break;
6472 }
6473 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006474 else
6475 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6476 if (dict_add(copy, di) == FAIL)
6477 {
6478 dictitem_free(di);
6479 break;
6480 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006483
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006485 if (todo > 0)
6486 {
6487 dict_unref(copy);
6488 copy = NULL;
6489 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490 }
6491
6492 return copy;
6493}
6494
6495/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006496 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006497 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006499 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 dict_T *d;
6502 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006503{
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505}
6506
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006508 * Add a number or string entry to dictionary "d".
6509 * When "str" is NULL use number "nr", otherwise use "str".
6510 * Returns FAIL when out of memory and when key already exists.
6511 */
6512 int
6513dict_add_nr_str(d, key, nr, str)
6514 dict_T *d;
6515 char *key;
6516 long nr;
6517 char_u *str;
6518{
6519 dictitem_T *item;
6520
6521 item = dictitem_alloc((char_u *)key);
6522 if (item == NULL)
6523 return FAIL;
6524 item->di_tv.v_lock = 0;
6525 if (str == NULL)
6526 {
6527 item->di_tv.v_type = VAR_NUMBER;
6528 item->di_tv.vval.v_number = nr;
6529 }
6530 else
6531 {
6532 item->di_tv.v_type = VAR_STRING;
6533 item->di_tv.vval.v_string = vim_strsave(str);
6534 }
6535 if (dict_add(d, item) == FAIL)
6536 {
6537 dictitem_free(item);
6538 return FAIL;
6539 }
6540 return OK;
6541}
6542
6543/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 * Get the number of items in a Dictionary.
6545 */
6546 static long
6547dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 if (d == NULL)
6551 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006552 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553}
6554
6555/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556 * Find item "key[len]" in Dictionary "d".
6557 * If "len" is negative use strlen(key).
6558 * Returns NULL when not found.
6559 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006561dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563 char_u *key;
6564 int len;
6565{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006566#define AKEYLEN 200
6567 char_u buf[AKEYLEN];
6568 char_u *akey;
6569 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006570 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006571
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006572 if (len < 0)
6573 akey = key;
6574 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006575 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006576 tofree = akey = vim_strnsave(key, len);
6577 if (akey == NULL)
6578 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006579 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006580 else
6581 {
6582 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006583 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006584 akey = buf;
6585 }
6586
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006588 vim_free(tofree);
6589 if (HASHITEM_EMPTY(hi))
6590 return NULL;
6591 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592}
6593
6594/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006595 * Get a string item from a dictionary.
6596 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006597 * Returns NULL if the entry doesn't exist or out of memory.
6598 */
6599 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006600get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006601 dict_T *d;
6602 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006603 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006604{
6605 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006606 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006607
6608 di = dict_find(d, key, -1);
6609 if (di == NULL)
6610 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006611 s = get_tv_string(&di->di_tv);
6612 if (save && s != NULL)
6613 s = vim_strsave(s);
6614 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006615}
6616
6617/*
6618 * Get a number item from a dictionary.
6619 * Returns 0 if the entry doesn't exist or out of memory.
6620 */
6621 long
6622get_dict_number(d, key)
6623 dict_T *d;
6624 char_u *key;
6625{
6626 dictitem_T *di;
6627
6628 di = dict_find(d, key, -1);
6629 if (di == NULL)
6630 return 0;
6631 return get_tv_number(&di->di_tv);
6632}
6633
6634/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006635 * Return an allocated string with the string representation of a Dictionary.
6636 * May return NULL.
6637 */
6638 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006639dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006640 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006641 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006642{
6643 garray_T ga;
6644 int first = TRUE;
6645 char_u *tofree;
6646 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006647 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006649 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006650 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006651
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006652 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006653 return NULL;
6654 ga_init2(&ga, (int)sizeof(char), 80);
6655 ga_append(&ga, '{');
6656
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006657 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006658 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006659 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006660 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006662 --todo;
6663
6664 if (first)
6665 first = FALSE;
6666 else
6667 ga_concat(&ga, (char_u *)", ");
6668
6669 tofree = string_quote(hi->hi_key, FALSE);
6670 if (tofree != NULL)
6671 {
6672 ga_concat(&ga, tofree);
6673 vim_free(tofree);
6674 }
6675 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006676 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006677 if (s != NULL)
6678 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006679 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006680 if (s == NULL)
6681 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006684 if (todo > 0)
6685 {
6686 vim_free(ga.ga_data);
6687 return NULL;
6688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006689
6690 ga_append(&ga, '}');
6691 ga_append(&ga, NUL);
6692 return (char_u *)ga.ga_data;
6693}
6694
6695/*
6696 * Allocate a variable for a Dictionary and fill it from "*arg".
6697 * Return OK or FAIL. Returns NOTDONE for {expr}.
6698 */
6699 static int
6700get_dict_tv(arg, rettv, evaluate)
6701 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006703 int evaluate;
6704{
Bram Moolenaar33570922005-01-25 22:26:29 +00006705 dict_T *d = NULL;
6706 typval_T tvkey;
6707 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00006708 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006709 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006710 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006711 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712
6713 /*
6714 * First check if it's not a curly-braces thing: {expr}.
6715 * Must do this without evaluating, otherwise a function may be called
6716 * twice. Unfortunately this means we need to call eval1() twice for the
6717 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006718 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006719 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006720 if (*start != '}')
6721 {
6722 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6723 return FAIL;
6724 if (*start == '}')
6725 return NOTDONE;
6726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006727
6728 if (evaluate)
6729 {
6730 d = dict_alloc();
6731 if (d == NULL)
6732 return FAIL;
6733 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006734 tvkey.v_type = VAR_UNKNOWN;
6735 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006736
6737 *arg = skipwhite(*arg + 1);
6738 while (**arg != '}' && **arg != NUL)
6739 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006740 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006741 goto failret;
6742 if (**arg != ':')
6743 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006744 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006745 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006746 goto failret;
6747 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006748 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006750 key = get_tv_string_buf_chk(&tvkey, buf);
6751 if (key == NULL || *key == NUL)
6752 {
6753 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6754 if (key != NULL)
6755 EMSG(_(e_emptykey));
6756 clear_tv(&tvkey);
6757 goto failret;
6758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006760
6761 *arg = skipwhite(*arg + 1);
6762 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6763 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006764 if (evaluate)
6765 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006766 goto failret;
6767 }
6768 if (evaluate)
6769 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006770 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006771 if (item != NULL)
6772 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006773 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006774 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006775 clear_tv(&tv);
6776 goto failret;
6777 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006778 item = dictitem_alloc(key);
6779 clear_tv(&tvkey);
6780 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006782 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006783 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006784 if (dict_add(d, item) == FAIL)
6785 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006786 }
6787 }
6788
6789 if (**arg == '}')
6790 break;
6791 if (**arg != ',')
6792 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006793 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006794 goto failret;
6795 }
6796 *arg = skipwhite(*arg + 1);
6797 }
6798
6799 if (**arg != '}')
6800 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006801 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006802failret:
6803 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006804 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006805 return FAIL;
6806 }
6807
6808 *arg = skipwhite(*arg + 1);
6809 if (evaluate)
6810 {
6811 rettv->v_type = VAR_DICT;
6812 rettv->vval.v_dict = d;
6813 ++d->dv_refcount;
6814 }
6815
6816 return OK;
6817}
6818
Bram Moolenaar8c711452005-01-14 21:53:12 +00006819/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006820 * Return a string with the string representation of a variable.
6821 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006822 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006824 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006825 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006826 */
6827 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006828echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006829 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006830 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006831 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006832 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006834 static int recurse = 0;
6835 char_u *r = NULL;
6836
Bram Moolenaar33570922005-01-25 22:26:29 +00006837 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006838 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006839 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006840 *tofree = NULL;
6841 return NULL;
6842 }
6843 ++recurse;
6844
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845 switch (tv->v_type)
6846 {
6847 case VAR_FUNC:
6848 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 r = tv->vval.v_string;
6850 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006851
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006852 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006853 if (tv->vval.v_list == NULL)
6854 {
6855 *tofree = NULL;
6856 r = NULL;
6857 }
6858 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6859 {
6860 *tofree = NULL;
6861 r = (char_u *)"[...]";
6862 }
6863 else
6864 {
6865 tv->vval.v_list->lv_copyID = copyID;
6866 *tofree = list2string(tv, copyID);
6867 r = *tofree;
6868 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006869 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006870
Bram Moolenaar8c711452005-01-14 21:53:12 +00006871 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006872 if (tv->vval.v_dict == NULL)
6873 {
6874 *tofree = NULL;
6875 r = NULL;
6876 }
6877 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6878 {
6879 *tofree = NULL;
6880 r = (char_u *)"{...}";
6881 }
6882 else
6883 {
6884 tv->vval.v_dict->dv_copyID = copyID;
6885 *tofree = dict2string(tv, copyID);
6886 r = *tofree;
6887 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006888 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006889
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006890 case VAR_STRING:
6891 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006892 *tofree = NULL;
6893 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006894 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006895
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006896 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006897 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006898 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006899 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900
6901 --recurse;
6902 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006903}
6904
6905/*
6906 * Return a string with the string representation of a variable.
6907 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6908 * "numbuf" is used for a number.
6909 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006910 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006911 */
6912 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006913tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006914 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006915 char_u **tofree;
6916 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006917 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006918{
6919 switch (tv->v_type)
6920 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006921 case VAR_FUNC:
6922 *tofree = string_quote(tv->vval.v_string, TRUE);
6923 return *tofree;
6924 case VAR_STRING:
6925 *tofree = string_quote(tv->vval.v_string, FALSE);
6926 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006927 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006928 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006929 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006930 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006931 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006932 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006933 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006934 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006935}
6936
6937/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006938 * Return string "str" in ' quotes, doubling ' characters.
6939 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006940 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006941 */
6942 static char_u *
6943string_quote(str, function)
6944 char_u *str;
6945 int function;
6946{
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006948 char_u *p, *r, *s;
6949
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 len = (function ? 13 : 3);
6951 if (str != NULL)
6952 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006953 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006954 for (p = str; *p != NUL; mb_ptr_adv(p))
6955 if (*p == '\'')
6956 ++len;
6957 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006958 s = r = alloc(len);
6959 if (r != NULL)
6960 {
6961 if (function)
6962 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006963 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006964 r += 10;
6965 }
6966 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006967 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 if (str != NULL)
6969 for (p = str; *p != NUL; )
6970 {
6971 if (*p == '\'')
6972 *r++ = '\'';
6973 MB_COPY_CHAR(p, r);
6974 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006975 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006976 if (function)
6977 *r++ = ')';
6978 *r++ = NUL;
6979 }
6980 return s;
6981}
6982
6983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 * Get the value of an environment variable.
6985 * "arg" is pointing to the '$'. It is advanced to after the name.
6986 * If the environment variable was not set, silently assume it is empty.
6987 * Always return OK.
6988 */
6989 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006990get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 int evaluate;
6994{
6995 char_u *string = NULL;
6996 int len;
6997 int cc;
6998 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006999 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001 ++*arg;
7002 name = *arg;
7003 len = get_env_len(arg);
7004 if (evaluate)
7005 {
7006 if (len != 0)
7007 {
7008 cc = name[len];
7009 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007010 /* first try vim_getenv(), fast for normal environment vars */
7011 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007013 {
7014 if (!mustfree)
7015 string = vim_strsave(string);
7016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 else
7018 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007019 if (mustfree)
7020 vim_free(string);
7021
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 /* next try expanding things like $VIM and ${HOME} */
7023 string = expand_env_save(name - 1);
7024 if (string != NULL && *string == '$')
7025 {
7026 vim_free(string);
7027 string = NULL;
7028 }
7029 }
7030 name[len] = cc;
7031 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007032 rettv->v_type = VAR_STRING;
7033 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 }
7035
7036 return OK;
7037}
7038
7039/*
7040 * Array with names and number of arguments of all internal functions
7041 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7042 */
7043static struct fst
7044{
7045 char *f_name; /* function name */
7046 char f_min_argc; /* minimal number of arguments */
7047 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007049 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050} functions[] =
7051{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007052 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {"append", 2, 2, f_append},
7054 {"argc", 0, 0, f_argc},
7055 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007056 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007058 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {"bufexists", 1, 1, f_bufexists},
7060 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7061 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7062 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7063 {"buflisted", 1, 1, f_buflisted},
7064 {"bufloaded", 1, 1, f_bufloaded},
7065 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007066 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {"bufwinnr", 1, 1, f_bufwinnr},
7068 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007069 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007070 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007071 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {"char2nr", 1, 1, f_char2nr},
7073 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007074 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007076#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007077 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007078 {"complete_add", 1, 1, f_complete_add},
7079 {"complete_check", 0, 0, f_complete_check},
7080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007082 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007083 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007085 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007086 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"delete", 1, 1, f_delete},
7088 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007089 {"diff_filler", 1, 1, f_diff_filler},
7090 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007091 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007093 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"eventhandler", 0, 0, f_eventhandler},
7095 {"executable", 1, 1, f_executable},
7096 {"exists", 1, 1, f_exists},
7097 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007098 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007099 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7101 {"filereadable", 1, 1, f_filereadable},
7102 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007103 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007104 {"finddir", 1, 3, f_finddir},
7105 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 {"fnamemodify", 2, 2, f_fnamemodify},
7107 {"foldclosed", 1, 1, f_foldclosed},
7108 {"foldclosedend", 1, 1, f_foldclosedend},
7109 {"foldlevel", 1, 1, f_foldlevel},
7110 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007111 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007113 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007114 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007115 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007116 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {"getbufvar", 2, 2, f_getbufvar},
7118 {"getchar", 0, 1, f_getchar},
7119 {"getcharmod", 0, 0, f_getcharmod},
7120 {"getcmdline", 0, 0, f_getcmdline},
7121 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007122 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007124 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007125 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {"getfsize", 1, 1, f_getfsize},
7127 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007128 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007129 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007130 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007131 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007132 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007133 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007134 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007136 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 {"getwinposx", 0, 0, f_getwinposx},
7138 {"getwinposy", 0, 0, f_getwinposy},
7139 {"getwinvar", 2, 2, f_getwinvar},
7140 {"glob", 1, 1, f_glob},
7141 {"globpath", 2, 2, f_globpath},
7142 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007144 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007145 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7147 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7148 {"histadd", 2, 2, f_histadd},
7149 {"histdel", 1, 2, f_histdel},
7150 {"histget", 1, 2, f_histget},
7151 {"histnr", 1, 1, f_histnr},
7152 {"hlID", 1, 1, f_hlID},
7153 {"hlexists", 1, 1, f_hlexists},
7154 {"hostname", 0, 0, f_hostname},
7155 {"iconv", 3, 3, f_iconv},
7156 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007157 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007158 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007160 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"inputrestore", 0, 0, f_inputrestore},
7162 {"inputsave", 0, 0, f_inputsave},
7163 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007164 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007166 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007167 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007168 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007171 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {"libcall", 3, 3, f_libcall},
7173 {"libcallnr", 3, 3, f_libcallnr},
7174 {"line", 1, 1, f_line},
7175 {"line2byte", 1, 1, f_line2byte},
7176 {"lispindent", 1, 1, f_lispindent},
7177 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007178 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007179 {"maparg", 1, 3, f_maparg},
7180 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007181 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007182 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007183 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007184 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007185 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007186 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007187 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007188 {"max", 1, 1, f_max},
7189 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007190#ifdef vim_mkdir
7191 {"mkdir", 1, 3, f_mkdir},
7192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {"mode", 0, 0, f_mode},
7194 {"nextnonblank", 1, 1, f_nextnonblank},
7195 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007196 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007198 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007199 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007201 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007202 {"reltime", 0, 2, f_reltime},
7203 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 {"remote_expr", 2, 3, f_remote_expr},
7205 {"remote_foreground", 1, 1, f_remote_foreground},
7206 {"remote_peek", 1, 2, f_remote_peek},
7207 {"remote_read", 1, 1, f_remote_read},
7208 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007209 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007211 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007213 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007214 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007215 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007216 {"searchpair", 3, 6, f_searchpair},
7217 {"searchpairpos", 3, 6, f_searchpairpos},
7218 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {"server2client", 2, 2, f_server2client},
7220 {"serverlist", 0, 0, f_serverlist},
7221 {"setbufvar", 3, 3, f_setbufvar},
7222 {"setcmdpos", 1, 1, f_setcmdpos},
7223 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007224 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007225 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007226 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007227 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007229 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007231 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007233 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007234 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007235 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007236 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007237 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007238 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239#ifdef HAVE_STRFTIME
7240 {"strftime", 1, 2, f_strftime},
7241#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007243 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 {"strlen", 1, 1, f_strlen},
7245 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007246 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {"strtrans", 1, 1, f_strtrans},
7248 {"submatch", 1, 1, f_submatch},
7249 {"substitute", 4, 4, f_substitute},
7250 {"synID", 3, 3, f_synID},
7251 {"synIDattr", 2, 3, f_synIDattr},
7252 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007253 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007254 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007255 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007256 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007257 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007258 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007260 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 {"tolower", 1, 1, f_tolower},
7262 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007263 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 {"virtcol", 1, 1, f_virtcol},
7267 {"visualmode", 0, 1, f_visualmode},
7268 {"winbufnr", 1, 1, f_winbufnr},
7269 {"wincol", 0, 0, f_wincol},
7270 {"winheight", 1, 1, f_winheight},
7271 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007272 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007274 {"winrestview", 1, 1, f_winrestview},
7275 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007277 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278};
7279
7280#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7281
7282/*
7283 * Function given to ExpandGeneric() to obtain the list of internal
7284 * or user defined function names.
7285 */
7286 char_u *
7287get_function_name(xp, idx)
7288 expand_T *xp;
7289 int idx;
7290{
7291 static int intidx = -1;
7292 char_u *name;
7293
7294 if (idx == 0)
7295 intidx = -1;
7296 if (intidx < 0)
7297 {
7298 name = get_user_func_name(xp, idx);
7299 if (name != NULL)
7300 return name;
7301 }
7302 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7303 {
7304 STRCPY(IObuff, functions[intidx].f_name);
7305 STRCAT(IObuff, "(");
7306 if (functions[intidx].f_max_argc == 0)
7307 STRCAT(IObuff, ")");
7308 return IObuff;
7309 }
7310
7311 return NULL;
7312}
7313
7314/*
7315 * Function given to ExpandGeneric() to obtain the list of internal or
7316 * user defined variable or function names.
7317 */
7318/*ARGSUSED*/
7319 char_u *
7320get_expr_name(xp, idx)
7321 expand_T *xp;
7322 int idx;
7323{
7324 static int intidx = -1;
7325 char_u *name;
7326
7327 if (idx == 0)
7328 intidx = -1;
7329 if (intidx < 0)
7330 {
7331 name = get_function_name(xp, idx);
7332 if (name != NULL)
7333 return name;
7334 }
7335 return get_user_var_name(xp, ++intidx);
7336}
7337
7338#endif /* FEAT_CMDL_COMPL */
7339
7340/*
7341 * Find internal function in table above.
7342 * Return index, or -1 if not found
7343 */
7344 static int
7345find_internal_func(name)
7346 char_u *name; /* name of the function */
7347{
7348 int first = 0;
7349 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7350 int cmp;
7351 int x;
7352
7353 /*
7354 * Find the function name in the table. Binary search.
7355 */
7356 while (first <= last)
7357 {
7358 x = first + ((unsigned)(last - first) >> 1);
7359 cmp = STRCMP(name, functions[x].f_name);
7360 if (cmp < 0)
7361 last = x - 1;
7362 else if (cmp > 0)
7363 first = x + 1;
7364 else
7365 return x;
7366 }
7367 return -1;
7368}
7369
7370/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007371 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7372 * name it contains, otherwise return "name".
7373 */
7374 static char_u *
7375deref_func_name(name, lenp)
7376 char_u *name;
7377 int *lenp;
7378{
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007380 int cc;
7381
7382 cc = name[*lenp];
7383 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007384 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007385 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007387 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007389 {
7390 *lenp = 0;
7391 return (char_u *)""; /* just in case */
7392 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007393 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007395 }
7396
7397 return name;
7398}
7399
7400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 * Allocate a variable for the result of a function.
7402 * Return OK or FAIL.
7403 */
7404 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7406 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 char_u *name; /* name of the function */
7408 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 char_u **arg; /* argument, pointing to the '(' */
7411 linenr_T firstline; /* first line of range */
7412 linenr_T lastline; /* last line of range */
7413 int *doesrange; /* return: function handled range */
7414 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
7417 char_u *argp;
7418 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007419 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 int argcount = 0; /* number of arguments found */
7421
7422 /*
7423 * Get the arguments.
7424 */
7425 argp = *arg;
7426 while (argcount < MAX_FUNC_ARGS)
7427 {
7428 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7429 if (*argp == ')' || *argp == ',' || *argp == NUL)
7430 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7432 {
7433 ret = FAIL;
7434 break;
7435 }
7436 ++argcount;
7437 if (*argp != ',')
7438 break;
7439 }
7440 if (*argp == ')')
7441 ++argp;
7442 else
7443 ret = FAIL;
7444
7445 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007446 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007447 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 {
7450 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007451 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007453 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
7456 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007457 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 *arg = skipwhite(argp);
7460 return ret;
7461}
7462
7463
7464/*
7465 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007466 * Return OK when the function can't be called, FAIL otherwise.
7467 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 */
7469 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007470call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007471 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 char_u *name; /* name of the function */
7473 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007476 typval_T *argvars; /* vars for arguments, must have "argcount"
7477 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 linenr_T firstline; /* first line of range */
7479 linenr_T lastline; /* last line of range */
7480 int *doesrange; /* return: function handled range */
7481 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
7484 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485#define ERROR_UNKNOWN 0
7486#define ERROR_TOOMANY 1
7487#define ERROR_TOOFEW 2
7488#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489#define ERROR_DICT 4
7490#define ERROR_NONE 5
7491#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 int error = ERROR_NONE;
7493 int i;
7494 int llen;
7495 ufunc_T *fp;
7496 int cc;
7497#define FLEN_FIXED 40
7498 char_u fname_buf[FLEN_FIXED + 1];
7499 char_u *fname;
7500
7501 /*
7502 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7503 * Change <SNR>123_name() to K_SNR 123_name().
7504 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7505 */
7506 cc = name[len];
7507 name[len] = NUL;
7508 llen = eval_fname_script(name);
7509 if (llen > 0)
7510 {
7511 fname_buf[0] = K_SPECIAL;
7512 fname_buf[1] = KS_EXTRA;
7513 fname_buf[2] = (int)KE_SNR;
7514 i = 3;
7515 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7516 {
7517 if (current_SID <= 0)
7518 error = ERROR_SCRIPT;
7519 else
7520 {
7521 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7522 i = (int)STRLEN(fname_buf);
7523 }
7524 }
7525 if (i + STRLEN(name + llen) < FLEN_FIXED)
7526 {
7527 STRCPY(fname_buf + i, name + llen);
7528 fname = fname_buf;
7529 }
7530 else
7531 {
7532 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7533 if (fname == NULL)
7534 error = ERROR_OTHER;
7535 else
7536 {
7537 mch_memmove(fname, fname_buf, (size_t)i);
7538 STRCPY(fname + i, name + llen);
7539 }
7540 }
7541 }
7542 else
7543 fname = name;
7544
7545 *doesrange = FALSE;
7546
7547
7548 /* execute the function if no errors detected and executing */
7549 if (evaluate && error == ERROR_NONE)
7550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 error = ERROR_UNKNOWN;
7553
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007554 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 {
7556 /*
7557 * User defined function.
7558 */
7559 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007560
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007562 /* Trigger FuncUndefined event, may load the function. */
7563 if (fp == NULL
7564 && apply_autocmds(EVENT_FUNCUNDEFINED,
7565 fname, fname, TRUE, NULL)
7566 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007568 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 fp = find_func(fname);
7570 }
7571#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007572 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007573 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007574 {
7575 /* loaded a package, search for the function again */
7576 fp = find_func(fname);
7577 }
7578
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 if (fp != NULL)
7580 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007581 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007583 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007585 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007587 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007588 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 else
7590 {
7591 /*
7592 * Call the user function.
7593 * Save and restore search patterns, script variables and
7594 * redo buffer.
7595 */
7596 save_search_patterns();
7597 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007598 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007599 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007600 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007601 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7602 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7603 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007604 /* Function was unreferenced while being used, free it
7605 * now. */
7606 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 restoreRedobuff();
7608 restore_search_patterns();
7609 error = ERROR_NONE;
7610 }
7611 }
7612 }
7613 else
7614 {
7615 /*
7616 * Find the function name in the table, call its implementation.
7617 */
7618 i = find_internal_func(fname);
7619 if (i >= 0)
7620 {
7621 if (argcount < functions[i].f_min_argc)
7622 error = ERROR_TOOFEW;
7623 else if (argcount > functions[i].f_max_argc)
7624 error = ERROR_TOOMANY;
7625 else
7626 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007627 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 error = ERROR_NONE;
7630 }
7631 }
7632 }
7633 /*
7634 * The function call (or "FuncUndefined" autocommand sequence) might
7635 * have been aborted by an error, an interrupt, or an explicitly thrown
7636 * exception that has not been caught so far. This situation can be
7637 * tested for by calling aborting(). For an error in an internal
7638 * function or for the "E132" error in call_user_func(), however, the
7639 * throw point at which the "force_abort" flag (temporarily reset by
7640 * emsg()) is normally updated has not been reached yet. We need to
7641 * update that flag first to make aborting() reliable.
7642 */
7643 update_force_abort();
7644 }
7645 if (error == ERROR_NONE)
7646 ret = OK;
7647
7648 /*
7649 * Report an error unless the argument evaluation or function call has been
7650 * cancelled due to an aborting error, an interrupt, or an exception.
7651 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 if (!aborting())
7653 {
7654 switch (error)
7655 {
7656 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007657 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 break;
7659 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007660 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 break;
7662 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007663 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 name);
7665 break;
7666 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007667 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 name);
7669 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007670 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007671 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007672 name);
7673 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 }
7675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676
7677 name[len] = cc;
7678 if (fname != name && fname != fname_buf)
7679 vim_free(fname);
7680
7681 return ret;
7682}
7683
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007684/*
7685 * Give an error message with a function name. Handle <SNR> things.
7686 */
7687 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007688emsg_funcname(ermsg, name)
7689 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007690 char_u *name;
7691{
7692 char_u *p;
7693
7694 if (*name == K_SPECIAL)
7695 p = concat_str((char_u *)"<SNR>", name + 3);
7696 else
7697 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007698 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007699 if (p != name)
7700 vim_free(p);
7701}
7702
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703/*********************************************
7704 * Implementation of the built-in functions
7705 */
7706
7707/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007708 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 */
7710 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007711f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 typval_T *argvars;
7713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714{
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007720 if ((l = argvars[0].vval.v_list) != NULL
7721 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7722 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007724 }
7725 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007726 EMSG(_(e_listreq));
7727}
7728
7729/*
7730 * "append(lnum, string/list)" function
7731 */
7732 static void
7733f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007734 typval_T *argvars;
7735 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007736{
7737 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007738 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 list_T *l = NULL;
7740 listitem_T *li = NULL;
7741 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007742 long added = 0;
7743
Bram Moolenaar0d660222005-01-07 21:51:51 +00007744 lnum = get_tv_lnum(argvars);
7745 if (lnum >= 0
7746 && lnum <= curbuf->b_ml.ml_line_count
7747 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007748 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007749 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007750 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007751 l = argvars[1].vval.v_list;
7752 if (l == NULL)
7753 return;
7754 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007755 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007756 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007757 for (;;)
7758 {
7759 if (l == NULL)
7760 tv = &argvars[1]; /* append a string */
7761 else if (li == NULL)
7762 break; /* end of list */
7763 else
7764 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007765 line = get_tv_string_chk(tv);
7766 if (line == NULL) /* type error */
7767 {
7768 rettv->vval.v_number = 1; /* Failed */
7769 break;
7770 }
7771 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007772 ++added;
7773 if (l == NULL)
7774 break;
7775 li = li->li_next;
7776 }
7777
7778 appended_lines_mark(lnum, added);
7779 if (curwin->w_cursor.lnum > lnum)
7780 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007782 else
7783 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784}
7785
7786/*
7787 * "argc()" function
7788 */
7789/* ARGSUSED */
7790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007791f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 typval_T *argvars;
7793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797
7798/*
7799 * "argidx()" function
7800 */
7801/* ARGSUSED */
7802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007803f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007804 typval_T *argvars;
7805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808}
7809
7810/*
7811 * "argv(nr)" function
7812 */
7813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007814f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 typval_T *argvars;
7816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817{
7818 int idx;
7819
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007820 if (argvars[0].v_type != VAR_UNKNOWN)
7821 {
7822 idx = get_tv_number_chk(&argvars[0], NULL);
7823 if (idx >= 0 && idx < ARGCOUNT)
7824 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7825 else
7826 rettv->vval.v_string = NULL;
7827 rettv->v_type = VAR_STRING;
7828 }
7829 else if (rettv_list_alloc(rettv) == OK)
7830 for (idx = 0; idx < ARGCOUNT; ++idx)
7831 list_append_string(rettv->vval.v_list,
7832 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833}
7834
7835/*
7836 * "browse(save, title, initdir, default)" function
7837 */
7838/* ARGSUSED */
7839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007841 typval_T *argvars;
7842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843{
7844#ifdef FEAT_BROWSE
7845 int save;
7846 char_u *title;
7847 char_u *initdir;
7848 char_u *defname;
7849 char_u buf[NUMBUFLEN];
7850 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007851 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007853 save = get_tv_number_chk(&argvars[0], &error);
7854 title = get_tv_string_chk(&argvars[1]);
7855 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7856 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007858 if (error || title == NULL || initdir == NULL || defname == NULL)
7859 rettv->vval.v_string = NULL;
7860 else
7861 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007862 do_browse(save ? BROWSE_SAVE : 0,
7863 title, defname, NULL, initdir, NULL, curbuf);
7864#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007866#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007867 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007868}
7869
7870/*
7871 * "browsedir(title, initdir)" function
7872 */
7873/* ARGSUSED */
7874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007876 typval_T *argvars;
7877 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007878{
7879#ifdef FEAT_BROWSE
7880 char_u *title;
7881 char_u *initdir;
7882 char_u buf[NUMBUFLEN];
7883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007884 title = get_tv_string_chk(&argvars[0]);
7885 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007887 if (title == NULL || initdir == NULL)
7888 rettv->vval.v_string = NULL;
7889 else
7890 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007891 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007893 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896}
7897
Bram Moolenaar33570922005-01-25 22:26:29 +00007898static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007899
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900/*
7901 * Find a buffer by number or exact name.
7902 */
7903 static buf_T *
7904find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007909 if (avar->v_type == VAR_NUMBER)
7910 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007911 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007914 if (buf == NULL)
7915 {
7916 /* No full path name match, try a match with a URL or a "nofile"
7917 * buffer, these don't use the full path. */
7918 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7919 if (buf->b_fname != NULL
7920 && (path_with_url(buf->b_fname)
7921#ifdef FEAT_QUICKFIX
7922 || bt_nofile(buf)
7923#endif
7924 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007925 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007926 break;
7927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
7929 return buf;
7930}
7931
7932/*
7933 * "bufexists(expr)" function
7934 */
7935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007936f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 typval_T *argvars;
7938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941}
7942
7943/*
7944 * "buflisted(expr)" function
7945 */
7946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007947f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 typval_T *argvars;
7949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
7951 buf_T *buf;
7952
7953 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007954 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955}
7956
7957/*
7958 * "bufloaded(expr)" function
7959 */
7960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 typval_T *argvars;
7963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 buf_T *buf;
7966
7967 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969}
7970
Bram Moolenaar33570922005-01-25 22:26:29 +00007971static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007972
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973/*
7974 * Get buffer by number or pattern.
7975 */
7976 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007978 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007980 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 int save_magic;
7982 char_u *save_cpo;
7983 buf_T *buf;
7984
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985 if (tv->v_type == VAR_NUMBER)
7986 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007987 if (tv->v_type != VAR_STRING)
7988 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 if (name == NULL || *name == NUL)
7990 return curbuf;
7991 if (name[0] == '$' && name[1] == NUL)
7992 return lastbuf;
7993
7994 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7995 save_magic = p_magic;
7996 p_magic = TRUE;
7997 save_cpo = p_cpo;
7998 p_cpo = (char_u *)"";
7999
8000 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8001 TRUE, FALSE));
8002
8003 p_magic = save_magic;
8004 p_cpo = save_cpo;
8005
8006 /* If not found, try expanding the name, like done for bufexists(). */
8007 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009
8010 return buf;
8011}
8012
8013/*
8014 * "bufname(expr)" function
8015 */
8016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008017f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008018 typval_T *argvars;
8019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020{
8021 buf_T *buf;
8022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008023 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008025 buf = get_buf_tv(&argvars[0]);
8026 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 --emsg_off;
8032}
8033
8034/*
8035 * "bufnr(expr)" function
8036 */
8037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 typval_T *argvars;
8040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041{
8042 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008043 int error = FALSE;
8044 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008048 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008049 --emsg_off;
8050
8051 /* If the buffer isn't found and the second argument is not zero create a
8052 * new buffer. */
8053 if (buf == NULL
8054 && argvars[1].v_type != VAR_UNKNOWN
8055 && get_tv_number_chk(&argvars[1], &error) != 0
8056 && !error
8057 && (name = get_tv_string_chk(&argvars[0])) != NULL
8058 && !error)
8059 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8060
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008062 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065}
8066
8067/*
8068 * "bufwinnr(nr)" function
8069 */
8070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008071f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 typval_T *argvars;
8073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
8075#ifdef FEAT_WINDOWS
8076 win_T *wp;
8077 int winnr = 0;
8078#endif
8079 buf_T *buf;
8080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084#ifdef FEAT_WINDOWS
8085 for (wp = firstwin; wp; wp = wp->w_next)
8086 {
8087 ++winnr;
8088 if (wp->w_buffer == buf)
8089 break;
8090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008091 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008093 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094#endif
8095 --emsg_off;
8096}
8097
8098/*
8099 * "byte2line(byte)" function
8100 */
8101/*ARGSUSED*/
8102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008103f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008104 typval_T *argvars;
8105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
8107#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008108 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109#else
8110 long boff = 0;
8111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008112 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 (linenr_T)0, &boff);
8118#endif
8119}
8120
8121/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008122 * "byteidx()" function
8123 */
8124/*ARGSUSED*/
8125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008126f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 typval_T *argvars;
8128 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008129{
8130#ifdef FEAT_MBYTE
8131 char_u *t;
8132#endif
8133 char_u *str;
8134 long idx;
8135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008136 str = get_tv_string_chk(&argvars[0]);
8137 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008138 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008139 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008140 return;
8141
8142#ifdef FEAT_MBYTE
8143 t = str;
8144 for ( ; idx > 0; idx--)
8145 {
8146 if (*t == NUL) /* EOL reached */
8147 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008148 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008149 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008150 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008151#else
8152 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008153 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008154#endif
8155}
8156
8157/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008158 * "call(func, arglist)" function
8159 */
8160 static void
8161f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 typval_T *argvars;
8163 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008164{
8165 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008166 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008167 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008168 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008169 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008170 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008171
8172 rettv->vval.v_number = 0;
8173 if (argvars[1].v_type != VAR_LIST)
8174 {
8175 EMSG(_(e_listreq));
8176 return;
8177 }
8178 if (argvars[1].vval.v_list == NULL)
8179 return;
8180
8181 if (argvars[0].v_type == VAR_FUNC)
8182 func = argvars[0].vval.v_string;
8183 else
8184 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008185 if (*func == NUL)
8186 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008187
Bram Moolenaare9a41262005-01-15 22:18:47 +00008188 if (argvars[2].v_type != VAR_UNKNOWN)
8189 {
8190 if (argvars[2].v_type != VAR_DICT)
8191 {
8192 EMSG(_(e_dictreq));
8193 return;
8194 }
8195 selfdict = argvars[2].vval.v_dict;
8196 }
8197
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008198 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8199 item = item->li_next)
8200 {
8201 if (argc == MAX_FUNC_ARGS)
8202 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008203 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008204 break;
8205 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008206 /* Make a copy of each argument. This is needed to be able to set
8207 * v_lock to VAR_FIXED in the copy without changing the original list.
8208 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008209 copy_tv(&item->li_tv, &argv[argc++]);
8210 }
8211
8212 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008213 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008214 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8215 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008216
8217 /* Free the arguments. */
8218 while (argc > 0)
8219 clear_tv(&argv[--argc]);
8220}
8221
8222/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008223 * "changenr()" function
8224 */
8225/*ARGSUSED*/
8226 static void
8227f_changenr(argvars, rettv)
8228 typval_T *argvars;
8229 typval_T *rettv;
8230{
8231 rettv->vval.v_number = curbuf->b_u_seq_cur;
8232}
8233
8234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 * "char2nr(string)" function
8236 */
8237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008238f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008239 typval_T *argvars;
8240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242#ifdef FEAT_MBYTE
8243 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008244 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 else
8246#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008247 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248}
8249
8250/*
8251 * "cindent(lnum)" function
8252 */
8253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008254f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008255 typval_T *argvars;
8256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
8258#ifdef FEAT_CINDENT
8259 pos_T pos;
8260 linenr_T lnum;
8261
8262 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008263 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8265 {
8266 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 curwin->w_cursor = pos;
8269 }
8270 else
8271#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008272 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273}
8274
8275/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008276 * "clearmatches()" function
8277 */
8278/*ARGSUSED*/
8279 static void
8280f_clearmatches(argvars, rettv)
8281 typval_T *argvars;
8282 typval_T *rettv;
8283{
8284#ifdef FEAT_SEARCH_EXTRA
8285 clear_matches(curwin);
8286#endif
8287}
8288
8289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 * "col(string)" function
8291 */
8292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 typval_T *argvars;
8295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
8297 colnr_T col = 0;
8298 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008299 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008301 fp = var2fpos(&argvars[0], FALSE, &fnum);
8302 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {
8304 if (fp->col == MAXCOL)
8305 {
8306 /* '> can be MAXCOL, get the length of the line then */
8307 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008308 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 else
8310 col = MAXCOL;
8311 }
8312 else
8313 {
8314 col = fp->col + 1;
8315#ifdef FEAT_VIRTUALEDIT
8316 /* col(".") when the cursor is on the NUL at the end of the line
8317 * because of "coladd" can be seen as an extra column. */
8318 if (virtual_active() && fp == &curwin->w_cursor)
8319 {
8320 char_u *p = ml_get_cursor();
8321
8322 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8323 curwin->w_virtcol - curwin->w_cursor.coladd))
8324 {
8325# ifdef FEAT_MBYTE
8326 int l;
8327
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008328 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 col += l;
8330# else
8331 if (*p != NUL && p[1] == NUL)
8332 ++col;
8333# endif
8334 }
8335 }
8336#endif
8337 }
8338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340}
8341
Bram Moolenaar572cb562005-08-05 21:35:02 +00008342#if defined(FEAT_INS_EXPAND)
8343/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008344 * "complete()" function
8345 */
8346/*ARGSUSED*/
8347 static void
8348f_complete(argvars, rettv)
8349 typval_T *argvars;
8350 typval_T *rettv;
8351{
8352 int startcol;
8353
8354 if ((State & INSERT) == 0)
8355 {
8356 EMSG(_("E785: complete() can only be used in Insert mode"));
8357 return;
8358 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008359
8360 /* Check for undo allowed here, because if something was already inserted
8361 * the line was already saved for undo and this check isn't done. */
8362 if (!undo_allowed())
8363 return;
8364
Bram Moolenaarade00832006-03-10 21:46:58 +00008365 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8366 {
8367 EMSG(_(e_invarg));
8368 return;
8369 }
8370
8371 startcol = get_tv_number_chk(&argvars[0], NULL);
8372 if (startcol <= 0)
8373 return;
8374
8375 set_completion(startcol - 1, argvars[1].vval.v_list);
8376}
8377
8378/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008379 * "complete_add()" function
8380 */
8381/*ARGSUSED*/
8382 static void
8383f_complete_add(argvars, rettv)
8384 typval_T *argvars;
8385 typval_T *rettv;
8386{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008387 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008388}
8389
8390/*
8391 * "complete_check()" function
8392 */
8393/*ARGSUSED*/
8394 static void
8395f_complete_check(argvars, rettv)
8396 typval_T *argvars;
8397 typval_T *rettv;
8398{
8399 int saved = RedrawingDisabled;
8400
8401 RedrawingDisabled = 0;
8402 ins_compl_check_keys(0);
8403 rettv->vval.v_number = compl_interrupted;
8404 RedrawingDisabled = saved;
8405}
8406#endif
8407
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408/*
8409 * "confirm(message, buttons[, default [, type]])" function
8410 */
8411/*ARGSUSED*/
8412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008413f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008414 typval_T *argvars;
8415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
8417#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8418 char_u *message;
8419 char_u *buttons = NULL;
8420 char_u buf[NUMBUFLEN];
8421 char_u buf2[NUMBUFLEN];
8422 int def = 1;
8423 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008424 char_u *typestr;
8425 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008427 message = get_tv_string_chk(&argvars[0]);
8428 if (message == NULL)
8429 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008430 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008432 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8433 if (buttons == NULL)
8434 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008435 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008437 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008438 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008440 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8441 if (typestr == NULL)
8442 error = TRUE;
8443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008445 switch (TOUPPER_ASC(*typestr))
8446 {
8447 case 'E': type = VIM_ERROR; break;
8448 case 'Q': type = VIM_QUESTION; break;
8449 case 'I': type = VIM_INFO; break;
8450 case 'W': type = VIM_WARNING; break;
8451 case 'G': type = VIM_GENERIC; break;
8452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 }
8454 }
8455 }
8456 }
8457
8458 if (buttons == NULL || *buttons == NUL)
8459 buttons = (char_u *)_("&Ok");
8460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008461 if (error)
8462 rettv->vval.v_number = 0;
8463 else
8464 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 def, NULL);
8466#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#endif
8469}
8470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008471/*
8472 * "copy()" function
8473 */
8474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008475f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008476 typval_T *argvars;
8477 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008478{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008479 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008480}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481
8482/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008483 * "count()" function
8484 */
8485 static void
8486f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008487 typval_T *argvars;
8488 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008489{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008490 long n = 0;
8491 int ic = FALSE;
8492
Bram Moolenaare9a41262005-01-15 22:18:47 +00008493 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008494 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 listitem_T *li;
8496 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008497 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008498
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499 if ((l = argvars[0].vval.v_list) != NULL)
8500 {
8501 li = l->lv_first;
8502 if (argvars[2].v_type != VAR_UNKNOWN)
8503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008504 int error = FALSE;
8505
8506 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008507 if (argvars[3].v_type != VAR_UNKNOWN)
8508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008509 idx = get_tv_number_chk(&argvars[3], &error);
8510 if (!error)
8511 {
8512 li = list_find(l, idx);
8513 if (li == NULL)
8514 EMSGN(_(e_listidx), idx);
8515 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008517 if (error)
8518 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 }
8520
8521 for ( ; li != NULL; li = li->li_next)
8522 if (tv_equal(&li->li_tv, &argvars[1], ic))
8523 ++n;
8524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008525 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008526 else if (argvars[0].v_type == VAR_DICT)
8527 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008528 int todo;
8529 dict_T *d;
8530 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008531
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008532 if ((d = argvars[0].vval.v_dict) != NULL)
8533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008534 int error = FALSE;
8535
Bram Moolenaare9a41262005-01-15 22:18:47 +00008536 if (argvars[2].v_type != VAR_UNKNOWN)
8537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008538 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539 if (argvars[3].v_type != VAR_UNKNOWN)
8540 EMSG(_(e_invarg));
8541 }
8542
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008543 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008544 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008545 {
8546 if (!HASHITEM_EMPTY(hi))
8547 {
8548 --todo;
8549 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8550 ++n;
8551 }
8552 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008553 }
8554 }
8555 else
8556 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008557 rettv->vval.v_number = n;
8558}
8559
8560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8562 *
8563 * Checks the existence of a cscope connection.
8564 */
8565/*ARGSUSED*/
8566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 typval_T *argvars;
8569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570{
8571#ifdef FEAT_CSCOPE
8572 int num = 0;
8573 char_u *dbpath = NULL;
8574 char_u *prepend = NULL;
8575 char_u buf[NUMBUFLEN];
8576
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008577 if (argvars[0].v_type != VAR_UNKNOWN
8578 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 num = (int)get_tv_number(&argvars[0]);
8581 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008583 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 }
8585
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589#endif
8590}
8591
8592/*
8593 * "cursor(lnum, col)" function
8594 *
8595 * Moves the cursor to the specified line and column
8596 */
8597/*ARGSUSED*/
8598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008599f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 typval_T *argvars;
8601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008604#ifdef FEAT_VIRTUALEDIT
8605 long coladd = 0;
8606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
Bram Moolenaara5525202006-03-02 22:52:09 +00008608 if (argvars[1].v_type == VAR_UNKNOWN)
8609 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008610 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008611
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008612 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008613 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008614 line = pos.lnum;
8615 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008616#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008617 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008618#endif
8619 }
8620 else
8621 {
8622 line = get_tv_lnum(argvars);
8623 col = get_tv_number_chk(&argvars[1], NULL);
8624#ifdef FEAT_VIRTUALEDIT
8625 if (argvars[2].v_type != VAR_UNKNOWN)
8626 coladd = get_tv_number_chk(&argvars[2], NULL);
8627#endif
8628 }
8629 if (line < 0 || col < 0
8630#ifdef FEAT_VIRTUALEDIT
8631 || coladd < 0
8632#endif
8633 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008634 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 if (line > 0)
8636 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 if (col > 0)
8638 curwin->w_cursor.col = col - 1;
8639#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008640 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641#endif
8642
8643 /* Make sure the cursor is in a valid position. */
8644 check_cursor();
8645#ifdef FEAT_MBYTE
8646 /* Correct cursor for multi-byte character. */
8647 if (has_mbyte)
8648 mb_adjust_cursor();
8649#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008650
8651 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652}
8653
8654/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008655 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 */
8657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 typval_T *argvars;
8660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008662 int noref = 0;
8663
8664 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008665 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008666 if (noref < 0 || noref > 1)
8667 EMSG(_(e_invarg));
8668 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008669 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670}
8671
8672/*
8673 * "delete()" function
8674 */
8675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008676f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008677 typval_T *argvars;
8678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
8680 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684}
8685
8686/*
8687 * "did_filetype()" function
8688 */
8689/*ARGSUSED*/
8690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008691f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 typval_T *argvars;
8693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
8695#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008696 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699#endif
8700}
8701
8702/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008703 * "diff_filler()" function
8704 */
8705/*ARGSUSED*/
8706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 typval_T *argvars;
8709 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008710{
8711#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008713#endif
8714}
8715
8716/*
8717 * "diff_hlID()" function
8718 */
8719/*ARGSUSED*/
8720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008722 typval_T *argvars;
8723 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008724{
8725#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008726 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008727 static linenr_T prev_lnum = 0;
8728 static int changedtick = 0;
8729 static int fnum = 0;
8730 static int change_start = 0;
8731 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008732 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008733 int filler_lines;
8734 int col;
8735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008736 if (lnum < 0) /* ignore type error in {lnum} arg */
8737 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008738 if (lnum != prev_lnum
8739 || changedtick != curbuf->b_changedtick
8740 || fnum != curbuf->b_fnum)
8741 {
8742 /* New line, buffer, change: need to get the values. */
8743 filler_lines = diff_check(curwin, lnum);
8744 if (filler_lines < 0)
8745 {
8746 if (filler_lines == -1)
8747 {
8748 change_start = MAXCOL;
8749 change_end = -1;
8750 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8751 hlID = HLF_ADD; /* added line */
8752 else
8753 hlID = HLF_CHD; /* changed line */
8754 }
8755 else
8756 hlID = HLF_ADD; /* added line */
8757 }
8758 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008759 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008760 prev_lnum = lnum;
8761 changedtick = curbuf->b_changedtick;
8762 fnum = curbuf->b_fnum;
8763 }
8764
8765 if (hlID == HLF_CHD || hlID == HLF_TXD)
8766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008767 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008768 if (col >= change_start && col <= change_end)
8769 hlID = HLF_TXD; /* changed text */
8770 else
8771 hlID = HLF_CHD; /* changed line */
8772 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008773 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008774#endif
8775}
8776
8777/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008778 * "empty({expr})" function
8779 */
8780 static void
8781f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008782 typval_T *argvars;
8783 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008784{
8785 int n;
8786
8787 switch (argvars[0].v_type)
8788 {
8789 case VAR_STRING:
8790 case VAR_FUNC:
8791 n = argvars[0].vval.v_string == NULL
8792 || *argvars[0].vval.v_string == NUL;
8793 break;
8794 case VAR_NUMBER:
8795 n = argvars[0].vval.v_number == 0;
8796 break;
8797 case VAR_LIST:
8798 n = argvars[0].vval.v_list == NULL
8799 || argvars[0].vval.v_list->lv_first == NULL;
8800 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008801 case VAR_DICT:
8802 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008804 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008805 default:
8806 EMSG2(_(e_intern2), "f_empty()");
8807 n = 0;
8808 }
8809
8810 rettv->vval.v_number = n;
8811}
8812
8813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 * "escape({string}, {chars})" function
8815 */
8816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008818 typval_T *argvars;
8819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 char_u buf[NUMBUFLEN];
8822
Bram Moolenaar758711c2005-02-02 23:11:38 +00008823 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8824 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008829 * "eval()" function
8830 */
8831/*ARGSUSED*/
8832 static void
8833f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008834 typval_T *argvars;
8835 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008836{
8837 char_u *s;
8838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 s = get_tv_string_chk(&argvars[0]);
8840 if (s != NULL)
8841 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008843 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8844 {
8845 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008846 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008847 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008848 else if (*s != NUL)
8849 EMSG(_(e_trailing));
8850}
8851
8852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 * "eventhandler()" function
8854 */
8855/*ARGSUSED*/
8856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008857f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008858 typval_T *argvars;
8859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862}
8863
8864/*
8865 * "executable()" function
8866 */
8867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008869 typval_T *argvars;
8870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873}
8874
8875/*
8876 * "exists()" function
8877 */
8878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 typval_T *argvars;
8881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
8883 char_u *p;
8884 char_u *name;
8885 int n = FALSE;
8886 int len = 0;
8887
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 if (*p == '$') /* environment variable */
8890 {
8891 /* first try "normal" environment variables (fast) */
8892 if (mch_getenv(p + 1) != NULL)
8893 n = TRUE;
8894 else
8895 {
8896 /* try expanding things like $VIM and ${HOME} */
8897 p = expand_env_save(p);
8898 if (p != NULL && *p != '$')
8899 n = TRUE;
8900 vim_free(p);
8901 }
8902 }
8903 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008906 if (*skipwhite(p) != NUL)
8907 n = FALSE; /* trailing garbage */
8908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 else if (*p == '*') /* internal or user defined function */
8910 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008911 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 }
8913 else if (*p == ':')
8914 {
8915 n = cmd_exists(p + 1);
8916 }
8917 else if (*p == '#')
8918 {
8919#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008920 if (p[1] == '#')
8921 n = autocmd_supported(p + 2);
8922 else
8923 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924#endif
8925 }
8926 else /* internal variable */
8927 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008928 char_u *tofree;
8929 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008931 /* get_name_len() takes care of expanding curly braces */
8932 name = p;
8933 len = get_name_len(&p, &tofree, TRUE, FALSE);
8934 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008936 if (tofree != NULL)
8937 name = tofree;
8938 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8939 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008941 /* handle d.key, l[idx], f(expr) */
8942 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8943 if (n)
8944 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 }
8946 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008947 if (*p != NUL)
8948 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008950 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 }
8952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954}
8955
8956/*
8957 * "expand()" function
8958 */
8959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008961 typval_T *argvars;
8962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963{
8964 char_u *s;
8965 int len;
8966 char_u *errormsg;
8967 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8968 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008969 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 rettv->v_type = VAR_STRING;
8972 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 if (*s == '%' || *s == '#' || *s == '<')
8974 {
8975 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008976 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 --emsg_off;
8978 }
8979 else
8980 {
8981 /* When the optional second argument is non-zero, don't remove matches
8982 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 if (argvars[1].v_type != VAR_UNKNOWN
8984 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008986 if (!error)
8987 {
8988 ExpandInit(&xpc);
8989 xpc.xp_context = EXPAND_FILES;
8990 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008991 }
8992 else
8993 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 }
8995}
8996
8997/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008998 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008999 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009000 */
9001 static void
9002f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *argvars;
9004 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009005{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009006 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009007 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009008 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 list_T *l1, *l2;
9010 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009011 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009012 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009013
Bram Moolenaare9a41262005-01-15 22:18:47 +00009014 l1 = argvars[0].vval.v_list;
9015 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009016 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9017 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009018 {
9019 if (argvars[2].v_type != VAR_UNKNOWN)
9020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 before = get_tv_number_chk(&argvars[2], &error);
9022 if (error)
9023 return; /* type error; errmsg already given */
9024
Bram Moolenaar758711c2005-02-02 23:11:38 +00009025 if (before == l1->lv_len)
9026 item = NULL;
9027 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009028 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009029 item = list_find(l1, before);
9030 if (item == NULL)
9031 {
9032 EMSGN(_(e_listidx), before);
9033 return;
9034 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009035 }
9036 }
9037 else
9038 item = NULL;
9039 list_extend(l1, l2, item);
9040
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 copy_tv(&argvars[0], rettv);
9042 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009043 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009044 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9045 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009046 dict_T *d1, *d2;
9047 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009048 char_u *action;
9049 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009050 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009051 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009052
9053 d1 = argvars[0].vval.v_dict;
9054 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009055 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9056 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009057 {
9058 /* Check the third argument. */
9059 if (argvars[2].v_type != VAR_UNKNOWN)
9060 {
9061 static char *(av[]) = {"keep", "force", "error"};
9062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009063 action = get_tv_string_chk(&argvars[2]);
9064 if (action == NULL)
9065 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009066 for (i = 0; i < 3; ++i)
9067 if (STRCMP(action, av[i]) == 0)
9068 break;
9069 if (i == 3)
9070 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009071 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009072 return;
9073 }
9074 }
9075 else
9076 action = (char_u *)"force";
9077
9078 /* Go over all entries in the second dict and add them to the
9079 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009080 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009081 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009082 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009083 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009085 --todo;
9086 di1 = dict_find(d1, hi2->hi_key, -1);
9087 if (di1 == NULL)
9088 {
9089 di1 = dictitem_copy(HI2DI(hi2));
9090 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9091 dictitem_free(di1);
9092 }
9093 else if (*action == 'e')
9094 {
9095 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9096 break;
9097 }
9098 else if (*action == 'f')
9099 {
9100 clear_tv(&di1->di_tv);
9101 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9102 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009103 }
9104 }
9105
Bram Moolenaare9a41262005-01-15 22:18:47 +00009106 copy_tv(&argvars[0], rettv);
9107 }
9108 }
9109 else
9110 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009111}
9112
9113/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009114 * "feedkeys()" function
9115 */
9116/*ARGSUSED*/
9117 static void
9118f_feedkeys(argvars, rettv)
9119 typval_T *argvars;
9120 typval_T *rettv;
9121{
9122 int remap = TRUE;
9123 char_u *keys, *flags;
9124 char_u nbuf[NUMBUFLEN];
9125 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009126 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009127
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009128 /* This is not allowed in the sandbox. If the commands would still be
9129 * executed in the sandbox it would be OK, but it probably happens later,
9130 * when "sandbox" is no longer set. */
9131 if (check_secure())
9132 return;
9133
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009134 rettv->vval.v_number = 0;
9135 keys = get_tv_string(&argvars[0]);
9136 if (*keys != NUL)
9137 {
9138 if (argvars[1].v_type != VAR_UNKNOWN)
9139 {
9140 flags = get_tv_string_buf(&argvars[1], nbuf);
9141 for ( ; *flags != NUL; ++flags)
9142 {
9143 switch (*flags)
9144 {
9145 case 'n': remap = FALSE; break;
9146 case 'm': remap = TRUE; break;
9147 case 't': typed = TRUE; break;
9148 }
9149 }
9150 }
9151
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009152 /* Need to escape K_SPECIAL and CSI before putting the string in the
9153 * typeahead buffer. */
9154 keys_esc = vim_strsave_escape_csi(keys);
9155 if (keys_esc != NULL)
9156 {
9157 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009158 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009159 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009160 if (vgetc_busy)
9161 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009162 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009163 }
9164}
9165
9166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 * "filereadable()" function
9168 */
9169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009171 typval_T *argvars;
9172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173{
9174 FILE *fd;
9175 char_u *p;
9176 int n;
9177
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9180 {
9181 n = TRUE;
9182 fclose(fd);
9183 }
9184 else
9185 n = FALSE;
9186
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188}
9189
9190/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009191 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 * rights to write into.
9193 */
9194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009196 typval_T *argvars;
9197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009199 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200}
9201
Bram Moolenaar33570922005-01-25 22:26:29 +00009202static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009203
9204 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009205findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009206 typval_T *argvars;
9207 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009208 int dir;
9209{
9210#ifdef FEAT_SEARCHPATH
9211 char_u *fname;
9212 char_u *fresult = NULL;
9213 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9214 char_u *p;
9215 char_u pathbuf[NUMBUFLEN];
9216 int count = 1;
9217 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009218 int error = FALSE;
9219#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009220
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009221 rettv->vval.v_string = NULL;
9222 rettv->v_type = VAR_STRING;
9223
9224#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009225 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009226
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009227 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009229 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9230 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009231 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009232 else
9233 {
9234 if (*p != NUL)
9235 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009238 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009240 }
9241
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009242 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9243 error = TRUE;
9244
9245 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009247 do
9248 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009249 if (rettv->v_type == VAR_STRING)
9250 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009251 fresult = find_file_in_path_option(first ? fname : NULL,
9252 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009253 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009254 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009256
9257 if (fresult != NULL && rettv->v_type == VAR_LIST)
9258 list_append_string(rettv->vval.v_list, fresult, -1);
9259
9260 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009262
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009263 if (rettv->v_type == VAR_STRING)
9264 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009265#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009266}
9267
Bram Moolenaar33570922005-01-25 22:26:29 +00009268static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9269static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009270
9271/*
9272 * Implementation of map() and filter().
9273 */
9274 static void
9275filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009276 typval_T *argvars;
9277 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009278 int map;
9279{
9280 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009281 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009282 listitem_T *li, *nli;
9283 list_T *l = NULL;
9284 dictitem_T *di;
9285 hashtab_T *ht;
9286 hashitem_T *hi;
9287 dict_T *d = NULL;
9288 typval_T save_val;
9289 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009291 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009292 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009293 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009294
9295 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009296 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009297 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009298 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009299 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009300 return;
9301 }
9302 else if (argvars[0].v_type == VAR_DICT)
9303 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009304 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009305 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009306 return;
9307 }
9308 else
9309 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009310 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009311 return;
9312 }
9313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009314 expr = get_tv_string_buf_chk(&argvars[1], buf);
9315 /* On type errors, the preceding call has already displayed an error
9316 * message. Avoid a misleading error message for an empty string that
9317 * was not passed as argument. */
9318 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009320 prepare_vimvar(VV_VAL, &save_val);
9321 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009322
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009323 /* We reset "did_emsg" to be able to detect whether an error
9324 * occurred during evaluation of the expression. */
9325 save_did_emsg = did_emsg;
9326 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009328 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009330 prepare_vimvar(VV_KEY, &save_key);
9331 vimvars[VV_KEY].vv_type = VAR_STRING;
9332
9333 ht = &d->dv_hashtab;
9334 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009335 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009336 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009338 if (!HASHITEM_EMPTY(hi))
9339 {
9340 --todo;
9341 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009342 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009343 break;
9344 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009345 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009346 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009347 break;
9348 if (!map && rem)
9349 dictitem_remove(d, di);
9350 clear_tv(&vimvars[VV_KEY].vv_tv);
9351 }
9352 }
9353 hash_unlock(ht);
9354
9355 restore_vimvar(VV_KEY, &save_key);
9356 }
9357 else
9358 {
9359 for (li = l->lv_first; li != NULL; li = nli)
9360 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009361 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009362 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009363 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009364 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009365 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009366 break;
9367 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009368 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009369 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009370 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009371
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009372 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009373
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009374 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009375 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009376
9377 copy_tv(&argvars[0], rettv);
9378}
9379
9380 static int
9381filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009382 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009383 char_u *expr;
9384 int map;
9385 int *remp;
9386{
Bram Moolenaar33570922005-01-25 22:26:29 +00009387 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009388 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009389 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009390
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 s = expr;
9393 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009394 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009395 if (*s != NUL) /* check for trailing chars after expr */
9396 {
9397 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009398 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009399 }
9400 if (map)
9401 {
9402 /* map(): replace the list item value */
9403 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009404 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009405 *tv = rettv;
9406 }
9407 else
9408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009409 int error = FALSE;
9410
Bram Moolenaare9a41262005-01-15 22:18:47 +00009411 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009413 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414 /* On type error, nothing has been removed; return FAIL to stop the
9415 * loop. The error message was given by get_tv_number_chk(). */
9416 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009417 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009418 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009419 retval = OK;
9420theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009421 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009422 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009423}
9424
9425/*
9426 * "filter()" function
9427 */
9428 static void
9429f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009430 typval_T *argvars;
9431 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009432{
9433 filter_map(argvars, rettv, FALSE);
9434}
9435
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009436/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009437 * "finddir({fname}[, {path}[, {count}]])" function
9438 */
9439 static void
9440f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009441 typval_T *argvars;
9442 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009443{
9444 findfilendir(argvars, rettv, TRUE);
9445}
9446
9447/*
9448 * "findfile({fname}[, {path}[, {count}]])" function
9449 */
9450 static void
9451f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009452 typval_T *argvars;
9453 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009454{
9455 findfilendir(argvars, rettv, FALSE);
9456}
9457
9458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 * "fnamemodify({fname}, {mods})" function
9460 */
9461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009463 typval_T *argvars;
9464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
9466 char_u *fname;
9467 char_u *mods;
9468 int usedlen = 0;
9469 int len;
9470 char_u *fbuf = NULL;
9471 char_u buf[NUMBUFLEN];
9472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473 fname = get_tv_string_chk(&argvars[0]);
9474 mods = get_tv_string_buf_chk(&argvars[1], buf);
9475 if (fname == NULL || mods == NULL)
9476 fname = NULL;
9477 else
9478 {
9479 len = (int)STRLEN(fname);
9480 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 vim_free(fbuf);
9489}
9490
Bram Moolenaar33570922005-01-25 22:26:29 +00009491static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492
9493/*
9494 * "foldclosed()" function
9495 */
9496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009498 typval_T *argvars;
9499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 int end;
9501{
9502#ifdef FEAT_FOLDING
9503 linenr_T lnum;
9504 linenr_T first, last;
9505
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9508 {
9509 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9510 {
9511 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 return;
9516 }
9517 }
9518#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520}
9521
9522/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009523 * "foldclosed()" function
9524 */
9525 static void
9526f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009527 typval_T *argvars;
9528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009529{
9530 foldclosed_both(argvars, rettv, FALSE);
9531}
9532
9533/*
9534 * "foldclosedend()" function
9535 */
9536 static void
9537f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009538 typval_T *argvars;
9539 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009540{
9541 foldclosed_both(argvars, rettv, TRUE);
9542}
9543
9544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 * "foldlevel()" function
9546 */
9547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009549 typval_T *argvars;
9550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
9552#ifdef FEAT_FOLDING
9553 linenr_T lnum;
9554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 else
9559#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561}
9562
9563/*
9564 * "foldtext()" function
9565 */
9566/*ARGSUSED*/
9567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009569 typval_T *argvars;
9570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
9572#ifdef FEAT_FOLDING
9573 linenr_T lnum;
9574 char_u *s;
9575 char_u *r;
9576 int len;
9577 char *txt;
9578#endif
9579
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580 rettv->v_type = VAR_STRING;
9581 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009583 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9584 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9585 <= curbuf->b_ml.ml_line_count
9586 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 {
9588 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009589 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9590 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 {
9592 if (!linewhite(lnum))
9593 break;
9594 ++lnum;
9595 }
9596
9597 /* Find interesting text in this line. */
9598 s = skipwhite(ml_get(lnum));
9599 /* skip C comment-start */
9600 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009603 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009604 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009605 {
9606 s = skipwhite(ml_get(lnum + 1));
9607 if (*s == '*')
9608 s = skipwhite(s + 1);
9609 }
9610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 txt = _("+-%s%3ld lines: ");
9612 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 + 20 /* for %3ld */
9615 + STRLEN(s))); /* concatenated */
9616 if (r != NULL)
9617 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009618 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9619 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9620 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 len = (int)STRLEN(r);
9622 STRCAT(r, s);
9623 /* remove 'foldmarker' and 'commentstring' */
9624 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 }
9627 }
9628#endif
9629}
9630
9631/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009632 * "foldtextresult(lnum)" function
9633 */
9634/*ARGSUSED*/
9635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009637 typval_T *argvars;
9638 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009639{
9640#ifdef FEAT_FOLDING
9641 linenr_T lnum;
9642 char_u *text;
9643 char_u buf[51];
9644 foldinfo_T foldinfo;
9645 int fold_count;
9646#endif
9647
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 rettv->v_type = VAR_STRING;
9649 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009650#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009652 /* treat illegal types and illegal string values for {lnum} the same */
9653 if (lnum < 0)
9654 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009655 fold_count = foldedCount(curwin, lnum, &foldinfo);
9656 if (fold_count > 0)
9657 {
9658 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9659 &foldinfo, buf);
9660 if (text == buf)
9661 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009663 }
9664#endif
9665}
9666
9667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 * "foreground()" function
9669 */
9670/*ARGSUSED*/
9671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009673 typval_T *argvars;
9674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677#ifdef FEAT_GUI
9678 if (gui.in_use)
9679 gui_mch_set_foreground();
9680#else
9681# ifdef WIN32
9682 win32_set_foreground();
9683# endif
9684#endif
9685}
9686
9687/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009688 * "function()" function
9689 */
9690/*ARGSUSED*/
9691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009693 typval_T *argvars;
9694 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009695{
9696 char_u *s;
9697
Bram Moolenaara7043832005-01-21 11:56:39 +00009698 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009700 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009701 EMSG2(_(e_invarg2), s);
9702 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009703 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009704 else
9705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_string = vim_strsave(s);
9707 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009708 }
9709}
9710
9711/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009712 * "garbagecollect()" function
9713 */
9714/*ARGSUSED*/
9715 static void
9716f_garbagecollect(argvars, rettv)
9717 typval_T *argvars;
9718 typval_T *rettv;
9719{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009720 /* This is postponed until we are back at the toplevel, because we may be
9721 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9722 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00009723
9724 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
9725 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009726}
9727
9728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009729 * "get()" function
9730 */
9731 static void
9732f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009733 typval_T *argvars;
9734 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009735{
Bram Moolenaar33570922005-01-25 22:26:29 +00009736 listitem_T *li;
9737 list_T *l;
9738 dictitem_T *di;
9739 dict_T *d;
9740 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009741
Bram Moolenaare9a41262005-01-15 22:18:47 +00009742 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009743 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009744 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009746 int error = FALSE;
9747
9748 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9749 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009750 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009751 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009752 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009753 else if (argvars[0].v_type == VAR_DICT)
9754 {
9755 if ((d = argvars[0].vval.v_dict) != NULL)
9756 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009757 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009758 if (di != NULL)
9759 tv = &di->di_tv;
9760 }
9761 }
9762 else
9763 EMSG2(_(e_listdictarg), "get()");
9764
9765 if (tv == NULL)
9766 {
9767 if (argvars[2].v_type == VAR_UNKNOWN)
9768 rettv->vval.v_number = 0;
9769 else
9770 copy_tv(&argvars[2], rettv);
9771 }
9772 else
9773 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009774}
9775
Bram Moolenaar342337a2005-07-21 21:11:17 +00009776static 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 +00009777
9778/*
9779 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009780 * Return a range (from start to end) of lines in rettv from the specified
9781 * buffer.
9782 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009783 */
9784 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009785get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009786 buf_T *buf;
9787 linenr_T start;
9788 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009789 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009790 typval_T *rettv;
9791{
9792 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009793
Bram Moolenaar342337a2005-07-21 21:11:17 +00009794 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009795 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009796 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009797 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009798 }
9799 else
9800 rettv->vval.v_number = 0;
9801
9802 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9803 return;
9804
9805 if (!retlist)
9806 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009807 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9808 p = ml_get_buf(buf, start, FALSE);
9809 else
9810 p = (char_u *)"";
9811
9812 rettv->v_type = VAR_STRING;
9813 rettv->vval.v_string = vim_strsave(p);
9814 }
9815 else
9816 {
9817 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009818 return;
9819
9820 if (start < 1)
9821 start = 1;
9822 if (end > buf->b_ml.ml_line_count)
9823 end = buf->b_ml.ml_line_count;
9824 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009825 if (list_append_string(rettv->vval.v_list,
9826 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009827 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009828 }
9829}
9830
9831/*
9832 * "getbufline()" function
9833 */
9834 static void
9835f_getbufline(argvars, rettv)
9836 typval_T *argvars;
9837 typval_T *rettv;
9838{
9839 linenr_T lnum;
9840 linenr_T end;
9841 buf_T *buf;
9842
9843 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9844 ++emsg_off;
9845 buf = get_buf_tv(&argvars[0]);
9846 --emsg_off;
9847
Bram Moolenaar661b1822005-07-28 22:36:45 +00009848 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009849 if (argvars[2].v_type == VAR_UNKNOWN)
9850 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009851 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009852 end = get_tv_lnum_buf(&argvars[2], buf);
9853
Bram Moolenaar342337a2005-07-21 21:11:17 +00009854 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009855}
9856
Bram Moolenaar0d660222005-01-07 21:51:51 +00009857/*
9858 * "getbufvar()" function
9859 */
9860 static void
9861f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009862 typval_T *argvars;
9863 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009864{
9865 buf_T *buf;
9866 buf_T *save_curbuf;
9867 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009868 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009870 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9871 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009872 ++emsg_off;
9873 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009874
9875 rettv->v_type = VAR_STRING;
9876 rettv->vval.v_string = NULL;
9877
9878 if (buf != NULL && varname != NULL)
9879 {
9880 if (*varname == '&') /* buffer-local-option */
9881 {
9882 /* set curbuf to be our buf, temporarily */
9883 save_curbuf = curbuf;
9884 curbuf = buf;
9885
9886 get_option_tv(&varname, rettv, TRUE);
9887
9888 /* restore previous notion of curbuf */
9889 curbuf = save_curbuf;
9890 }
9891 else
9892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 if (*varname == NUL)
9894 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9895 * scope prefix before the NUL byte is required by
9896 * find_var_in_ht(). */
9897 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009898 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009899 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009900 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009901 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009902 }
9903 }
9904
9905 --emsg_off;
9906}
9907
9908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 * "getchar()" function
9910 */
9911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009913 typval_T *argvars;
9914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009917 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009919 /* Position the cursor. Needed after a message that ends in a space. */
9920 windgoto(msg_row, msg_col);
9921
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 ++no_mapping;
9923 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009924 for (;;)
9925 {
9926 if (argvars[0].v_type == VAR_UNKNOWN)
9927 /* getchar(): blocking wait. */
9928 n = safe_vgetc();
9929 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9930 /* getchar(1): only check if char avail */
9931 n = vpeekc();
9932 else if (error || vpeekc() == NUL)
9933 /* illegal argument or getchar(0) and no char avail: return zero */
9934 n = 0;
9935 else
9936 /* getchar(0) and char avail: return char */
9937 n = safe_vgetc();
9938 if (n == K_IGNORE)
9939 continue;
9940 break;
9941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 --no_mapping;
9943 --allow_keys;
9944
Bram Moolenaar219b8702006-11-01 14:32:36 +00009945 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9946 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9947 vimvars[VV_MOUSE_COL].vv_nr = 0;
9948
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 if (IS_SPECIAL(n) || mod_mask != 0)
9951 {
9952 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9953 int i = 0;
9954
9955 /* Turn a special key into three bytes, plus modifier. */
9956 if (mod_mask != 0)
9957 {
9958 temp[i++] = K_SPECIAL;
9959 temp[i++] = KS_MODIFIER;
9960 temp[i++] = mod_mask;
9961 }
9962 if (IS_SPECIAL(n))
9963 {
9964 temp[i++] = K_SPECIAL;
9965 temp[i++] = K_SECOND(n);
9966 temp[i++] = K_THIRD(n);
9967 }
9968#ifdef FEAT_MBYTE
9969 else if (has_mbyte)
9970 i += (*mb_char2bytes)(n, temp + i);
9971#endif
9972 else
9973 temp[i++] = n;
9974 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 rettv->v_type = VAR_STRING;
9976 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009977
9978#ifdef FEAT_MOUSE
9979 if (n == K_LEFTMOUSE
9980 || n == K_LEFTMOUSE_NM
9981 || n == K_LEFTDRAG
9982 || n == K_LEFTRELEASE
9983 || n == K_LEFTRELEASE_NM
9984 || n == K_MIDDLEMOUSE
9985 || n == K_MIDDLEDRAG
9986 || n == K_MIDDLERELEASE
9987 || n == K_RIGHTMOUSE
9988 || n == K_RIGHTDRAG
9989 || n == K_RIGHTRELEASE
9990 || n == K_X1MOUSE
9991 || n == K_X1DRAG
9992 || n == K_X1RELEASE
9993 || n == K_X2MOUSE
9994 || n == K_X2DRAG
9995 || n == K_X2RELEASE
9996 || n == K_MOUSEDOWN
9997 || n == K_MOUSEUP)
9998 {
9999 int row = mouse_row;
10000 int col = mouse_col;
10001 win_T *win;
10002 linenr_T lnum;
10003# ifdef FEAT_WINDOWS
10004 win_T *wp;
10005# endif
10006 int n = 1;
10007
10008 if (row >= 0 && col >= 0)
10009 {
10010 /* Find the window at the mouse coordinates and compute the
10011 * text position. */
10012 win = mouse_find_win(&row, &col);
10013 (void)mouse_comp_pos(win, &row, &col, &lnum);
10014# ifdef FEAT_WINDOWS
10015 for (wp = firstwin; wp != win; wp = wp->w_next)
10016 ++n;
10017# endif
10018 vimvars[VV_MOUSE_WIN].vv_nr = n;
10019 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10020 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10021 }
10022 }
10023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025}
10026
10027/*
10028 * "getcharmod()" function
10029 */
10030/*ARGSUSED*/
10031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010033 typval_T *argvars;
10034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037}
10038
10039/*
10040 * "getcmdline()" function
10041 */
10042/*ARGSUSED*/
10043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010045 typval_T *argvars;
10046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->v_type = VAR_STRING;
10049 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050}
10051
10052/*
10053 * "getcmdpos()" function
10054 */
10055/*ARGSUSED*/
10056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010057f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010058 typval_T *argvars;
10059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062}
10063
10064/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010065 * "getcmdtype()" function
10066 */
10067/*ARGSUSED*/
10068 static void
10069f_getcmdtype(argvars, rettv)
10070 typval_T *argvars;
10071 typval_T *rettv;
10072{
10073 rettv->v_type = VAR_STRING;
10074 rettv->vval.v_string = alloc(2);
10075 if (rettv->vval.v_string != NULL)
10076 {
10077 rettv->vval.v_string[0] = get_cmdline_type();
10078 rettv->vval.v_string[1] = NUL;
10079 }
10080}
10081
10082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 * "getcwd()" function
10084 */
10085/*ARGSUSED*/
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
10091 char_u cwd[MAXPATHL];
10092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 else
10097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010100 if (rettv->vval.v_string != NULL)
10101 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102#endif
10103 }
10104}
10105
10106/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010107 * "getfontname()" function
10108 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010109/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010112 typval_T *argvars;
10113 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010114{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 rettv->v_type = VAR_STRING;
10116 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010117#ifdef FEAT_GUI
10118 if (gui.in_use)
10119 {
10120 GuiFont font;
10121 char_u *name = NULL;
10122
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010123 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010124 {
10125 /* Get the "Normal" font. Either the name saved by
10126 * hl_set_font_name() or from the font ID. */
10127 font = gui.norm_font;
10128 name = hl_get_font_name();
10129 }
10130 else
10131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010133 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10134 return;
10135 font = gui_mch_get_font(name, FALSE);
10136 if (font == NOFONT)
10137 return; /* Invalid font name, return empty string. */
10138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010140 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010141 gui_mch_free_font(font);
10142 }
10143#endif
10144}
10145
10146/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010147 * "getfperm({fname})" function
10148 */
10149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 typval_T *argvars;
10152 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010153{
10154 char_u *fname;
10155 struct stat st;
10156 char_u *perm = NULL;
10157 char_u flags[] = "rwx";
10158 int i;
10159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010163 if (mch_stat((char *)fname, &st) >= 0)
10164 {
10165 perm = vim_strsave((char_u *)"---------");
10166 if (perm != NULL)
10167 {
10168 for (i = 0; i < 9; i++)
10169 {
10170 if (st.st_mode & (1 << (8 - i)))
10171 perm[i] = flags[i % 3];
10172 }
10173 }
10174 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010175 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010176}
10177
10178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 * "getfsize({fname})" function
10180 */
10181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010183 typval_T *argvars;
10184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185{
10186 char_u *fname;
10187 struct stat st;
10188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
10193 if (mch_stat((char *)fname, &st) >= 0)
10194 {
10195 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010196 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010199 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010200
10201 /* non-perfect check for overflow */
10202 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10203 rettv->vval.v_number = -2;
10204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 }
10206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208}
10209
10210/*
10211 * "getftime({fname})" function
10212 */
10213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010214f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010215 typval_T *argvars;
10216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217{
10218 char_u *fname;
10219 struct stat st;
10220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
10223 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010224 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227}
10228
10229/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010230 * "getftype({fname})" function
10231 */
10232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010234 typval_T *argvars;
10235 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010236{
10237 char_u *fname;
10238 struct stat st;
10239 char_u *type = NULL;
10240 char *t;
10241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010242 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010244 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010245 if (mch_lstat((char *)fname, &st) >= 0)
10246 {
10247#ifdef S_ISREG
10248 if (S_ISREG(st.st_mode))
10249 t = "file";
10250 else if (S_ISDIR(st.st_mode))
10251 t = "dir";
10252# ifdef S_ISLNK
10253 else if (S_ISLNK(st.st_mode))
10254 t = "link";
10255# endif
10256# ifdef S_ISBLK
10257 else if (S_ISBLK(st.st_mode))
10258 t = "bdev";
10259# endif
10260# ifdef S_ISCHR
10261 else if (S_ISCHR(st.st_mode))
10262 t = "cdev";
10263# endif
10264# ifdef S_ISFIFO
10265 else if (S_ISFIFO(st.st_mode))
10266 t = "fifo";
10267# endif
10268# ifdef S_ISSOCK
10269 else if (S_ISSOCK(st.st_mode))
10270 t = "fifo";
10271# endif
10272 else
10273 t = "other";
10274#else
10275# ifdef S_IFMT
10276 switch (st.st_mode & S_IFMT)
10277 {
10278 case S_IFREG: t = "file"; break;
10279 case S_IFDIR: t = "dir"; break;
10280# ifdef S_IFLNK
10281 case S_IFLNK: t = "link"; break;
10282# endif
10283# ifdef S_IFBLK
10284 case S_IFBLK: t = "bdev"; break;
10285# endif
10286# ifdef S_IFCHR
10287 case S_IFCHR: t = "cdev"; break;
10288# endif
10289# ifdef S_IFIFO
10290 case S_IFIFO: t = "fifo"; break;
10291# endif
10292# ifdef S_IFSOCK
10293 case S_IFSOCK: t = "socket"; break;
10294# endif
10295 default: t = "other";
10296 }
10297# else
10298 if (mch_isdir(fname))
10299 t = "dir";
10300 else
10301 t = "file";
10302# endif
10303#endif
10304 type = vim_strsave((char_u *)t);
10305 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010306 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010307}
10308
10309/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010310 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010311 */
10312 static void
10313f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010314 typval_T *argvars;
10315 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010316{
10317 linenr_T lnum;
10318 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010319 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010320
10321 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010322 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010323 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010324 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010325 retlist = FALSE;
10326 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010327 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010328 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010329 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010330 retlist = TRUE;
10331 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010332
Bram Moolenaar342337a2005-07-21 21:11:17 +000010333 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010334}
10335
10336/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010337 * "getmatches()" function
10338 */
10339/*ARGSUSED*/
10340 static void
10341f_getmatches(argvars, rettv)
10342 typval_T *argvars;
10343 typval_T *rettv;
10344{
10345#ifdef FEAT_SEARCH_EXTRA
10346 dict_T *dict;
10347 matchitem_T *cur = curwin->w_match_head;
10348
10349 rettv->vval.v_number = 0;
10350
10351 if (rettv_list_alloc(rettv) == OK)
10352 {
10353 while (cur != NULL)
10354 {
10355 dict = dict_alloc();
10356 if (dict == NULL)
10357 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010358 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10359 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10360 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10361 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10362 list_append_dict(rettv->vval.v_list, dict);
10363 cur = cur->next;
10364 }
10365 }
10366#endif
10367}
10368
10369/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010370 * "getpos(string)" function
10371 */
10372 static void
10373f_getpos(argvars, rettv)
10374 typval_T *argvars;
10375 typval_T *rettv;
10376{
10377 pos_T *fp;
10378 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010379 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010380
10381 if (rettv_list_alloc(rettv) == OK)
10382 {
10383 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010384 fp = var2fpos(&argvars[0], TRUE, &fnum);
10385 if (fnum != -1)
10386 list_append_number(l, (varnumber_T)fnum);
10387 else
10388 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010389 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10390 : (varnumber_T)0);
10391 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10392 : (varnumber_T)0);
10393 list_append_number(l,
10394#ifdef FEAT_VIRTUALEDIT
10395 (fp != NULL) ? (varnumber_T)fp->coladd :
10396#endif
10397 (varnumber_T)0);
10398 }
10399 else
10400 rettv->vval.v_number = FALSE;
10401}
10402
10403/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010404 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010405 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010406/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010407 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010408f_getqflist(argvars, rettv)
10409 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010410 typval_T *rettv;
10411{
10412#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010413 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010414#endif
10415
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010416 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010417#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010418 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010419 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010420 wp = NULL;
10421 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10422 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010423 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010424 if (wp == NULL)
10425 return;
10426 }
10427
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010428 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010429 }
10430#endif
10431}
10432
10433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 * "getreg()" function
10435 */
10436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010437f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010438 typval_T *argvars;
10439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
10441 char_u *strregname;
10442 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010443 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010446 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 strregname = get_tv_string_chk(&argvars[0]);
10449 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010450 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 regname = (strregname == NULL ? '"' : *strregname);
10456 if (regname == 0)
10457 regname = '"';
10458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 rettv->vval.v_string = error ? NULL :
10461 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462}
10463
10464/*
10465 * "getregtype()" function
10466 */
10467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010469 typval_T *argvars;
10470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471{
10472 char_u *strregname;
10473 int regname;
10474 char_u buf[NUMBUFLEN + 2];
10475 long reglen = 0;
10476
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010477 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 {
10479 strregname = get_tv_string_chk(&argvars[0]);
10480 if (strregname == NULL) /* type error; errmsg already given */
10481 {
10482 rettv->v_type = VAR_STRING;
10483 rettv->vval.v_string = NULL;
10484 return;
10485 }
10486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 else
10488 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010489 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490
10491 regname = (strregname == NULL ? '"' : *strregname);
10492 if (regname == 0)
10493 regname = '"';
10494
10495 buf[0] = NUL;
10496 buf[1] = NUL;
10497 switch (get_reg_type(regname, &reglen))
10498 {
10499 case MLINE: buf[0] = 'V'; break;
10500 case MCHAR: buf[0] = 'v'; break;
10501#ifdef FEAT_VISUAL
10502 case MBLOCK:
10503 buf[0] = Ctrl_V;
10504 sprintf((char *)buf + 1, "%ld", reglen + 1);
10505 break;
10506#endif
10507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 rettv->v_type = VAR_STRING;
10509 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510}
10511
10512/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010513 * "gettabwinvar()" function
10514 */
10515 static void
10516f_gettabwinvar(argvars, rettv)
10517 typval_T *argvars;
10518 typval_T *rettv;
10519{
10520 getwinvar(argvars, rettv, 1);
10521}
10522
10523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 * "getwinposx()" function
10525 */
10526/*ARGSUSED*/
10527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010529 typval_T *argvars;
10530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533#ifdef FEAT_GUI
10534 if (gui.in_use)
10535 {
10536 int x, y;
10537
10538 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010539 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 }
10541#endif
10542}
10543
10544/*
10545 * "getwinposy()" function
10546 */
10547/*ARGSUSED*/
10548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010550 typval_T *argvars;
10551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554#ifdef FEAT_GUI
10555 if (gui.in_use)
10556 {
10557 int x, y;
10558
10559 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 }
10562#endif
10563}
10564
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010565/*
10566 * Find window specifed by "vp" in tabpage "tp".
10567 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010568 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010569find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010570 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010571 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010572{
10573#ifdef FEAT_WINDOWS
10574 win_T *wp;
10575#endif
10576 int nr;
10577
10578 nr = get_tv_number_chk(vp, NULL);
10579
10580#ifdef FEAT_WINDOWS
10581 if (nr < 0)
10582 return NULL;
10583 if (nr == 0)
10584 return curwin;
10585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010586 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10587 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010588 if (--nr <= 0)
10589 break;
10590 return wp;
10591#else
10592 if (nr == 0 || nr == 1)
10593 return curwin;
10594 return NULL;
10595#endif
10596}
10597
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598/*
10599 * "getwinvar()" function
10600 */
10601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010602f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010603 typval_T *argvars;
10604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010606 getwinvar(argvars, rettv, 0);
10607}
10608
10609/*
10610 * getwinvar() and gettabwinvar()
10611 */
10612 static void
10613getwinvar(argvars, rettv, off)
10614 typval_T *argvars;
10615 typval_T *rettv;
10616 int off; /* 1 for gettabwinvar() */
10617{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 win_T *win, *oldcurwin;
10619 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010620 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010621 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010623#ifdef FEAT_WINDOWS
10624 if (off == 1)
10625 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10626 else
10627 tp = curtab;
10628#endif
10629 win = find_win_by_nr(&argvars[off], tp);
10630 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010631 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633 rettv->v_type = VAR_STRING;
10634 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635
10636 if (win != NULL && varname != NULL)
10637 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010638 /* Set curwin to be our win, temporarily. Also set curbuf, so
10639 * that we can get buffer-local options. */
10640 oldcurwin = curwin;
10641 curwin = win;
10642 curbuf = win->w_buffer;
10643
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 else
10647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010648 if (*varname == NUL)
10649 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10650 * scope prefix before the NUL byte is required by
10651 * find_var_in_ht(). */
10652 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010654 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010656 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010658
10659 /* restore previous notion of curwin */
10660 curwin = oldcurwin;
10661 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 }
10663
10664 --emsg_off;
10665}
10666
10667/*
10668 * "glob()" function
10669 */
10670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010671f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010672 typval_T *argvars;
10673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674{
10675 expand_T xpc;
10676
10677 ExpandInit(&xpc);
10678 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 rettv->v_type = VAR_STRING;
10680 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682}
10683
10684/*
10685 * "globpath()" function
10686 */
10687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010689 typval_T *argvars;
10690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
10692 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010696 if (file == NULL)
10697 rettv->vval.v_string = NULL;
10698 else
10699 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700}
10701
10702/*
10703 * "has()" function
10704 */
10705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010707 typval_T *argvars;
10708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709{
10710 int i;
10711 char_u *name;
10712 int n = FALSE;
10713 static char *(has_list[]) =
10714 {
10715#ifdef AMIGA
10716 "amiga",
10717# ifdef FEAT_ARP
10718 "arp",
10719# endif
10720#endif
10721#ifdef __BEOS__
10722 "beos",
10723#endif
10724#ifdef MSDOS
10725# ifdef DJGPP
10726 "dos32",
10727# else
10728 "dos16",
10729# endif
10730#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010731#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 "mac",
10733#endif
10734#if defined(MACOS_X_UNIX)
10735 "macunix",
10736#endif
10737#ifdef OS2
10738 "os2",
10739#endif
10740#ifdef __QNX__
10741 "qnx",
10742#endif
10743#ifdef RISCOS
10744 "riscos",
10745#endif
10746#ifdef UNIX
10747 "unix",
10748#endif
10749#ifdef VMS
10750 "vms",
10751#endif
10752#ifdef WIN16
10753 "win16",
10754#endif
10755#ifdef WIN32
10756 "win32",
10757#endif
10758#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10759 "win32unix",
10760#endif
10761#ifdef WIN64
10762 "win64",
10763#endif
10764#ifdef EBCDIC
10765 "ebcdic",
10766#endif
10767#ifndef CASE_INSENSITIVE_FILENAME
10768 "fname_case",
10769#endif
10770#ifdef FEAT_ARABIC
10771 "arabic",
10772#endif
10773#ifdef FEAT_AUTOCMD
10774 "autocmd",
10775#endif
10776#ifdef FEAT_BEVAL
10777 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010778# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10779 "balloon_multiline",
10780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781#endif
10782#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10783 "builtin_terms",
10784# ifdef ALL_BUILTIN_TCAPS
10785 "all_builtin_terms",
10786# endif
10787#endif
10788#ifdef FEAT_BYTEOFF
10789 "byte_offset",
10790#endif
10791#ifdef FEAT_CINDENT
10792 "cindent",
10793#endif
10794#ifdef FEAT_CLIENTSERVER
10795 "clientserver",
10796#endif
10797#ifdef FEAT_CLIPBOARD
10798 "clipboard",
10799#endif
10800#ifdef FEAT_CMDL_COMPL
10801 "cmdline_compl",
10802#endif
10803#ifdef FEAT_CMDHIST
10804 "cmdline_hist",
10805#endif
10806#ifdef FEAT_COMMENTS
10807 "comments",
10808#endif
10809#ifdef FEAT_CRYPT
10810 "cryptv",
10811#endif
10812#ifdef FEAT_CSCOPE
10813 "cscope",
10814#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010815#ifdef CURSOR_SHAPE
10816 "cursorshape",
10817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818#ifdef DEBUG
10819 "debug",
10820#endif
10821#ifdef FEAT_CON_DIALOG
10822 "dialog_con",
10823#endif
10824#ifdef FEAT_GUI_DIALOG
10825 "dialog_gui",
10826#endif
10827#ifdef FEAT_DIFF
10828 "diff",
10829#endif
10830#ifdef FEAT_DIGRAPHS
10831 "digraphs",
10832#endif
10833#ifdef FEAT_DND
10834 "dnd",
10835#endif
10836#ifdef FEAT_EMACS_TAGS
10837 "emacs_tags",
10838#endif
10839 "eval", /* always present, of course! */
10840#ifdef FEAT_EX_EXTRA
10841 "ex_extra",
10842#endif
10843#ifdef FEAT_SEARCH_EXTRA
10844 "extra_search",
10845#endif
10846#ifdef FEAT_FKMAP
10847 "farsi",
10848#endif
10849#ifdef FEAT_SEARCHPATH
10850 "file_in_path",
10851#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010852#if defined(UNIX) && !defined(USE_SYSTEM)
10853 "filterpipe",
10854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855#ifdef FEAT_FIND_ID
10856 "find_in_path",
10857#endif
10858#ifdef FEAT_FOLDING
10859 "folding",
10860#endif
10861#ifdef FEAT_FOOTER
10862 "footer",
10863#endif
10864#if !defined(USE_SYSTEM) && defined(UNIX)
10865 "fork",
10866#endif
10867#ifdef FEAT_GETTEXT
10868 "gettext",
10869#endif
10870#ifdef FEAT_GUI
10871 "gui",
10872#endif
10873#ifdef FEAT_GUI_ATHENA
10874# ifdef FEAT_GUI_NEXTAW
10875 "gui_neXtaw",
10876# else
10877 "gui_athena",
10878# endif
10879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880#ifdef FEAT_GUI_GTK
10881 "gui_gtk",
10882# ifdef HAVE_GTK2
10883 "gui_gtk2",
10884# endif
10885#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000010886#ifdef FEAT_GUI_GNOME
10887 "gui_gnome",
10888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889#ifdef FEAT_GUI_MAC
10890 "gui_mac",
10891#endif
10892#ifdef FEAT_GUI_MOTIF
10893 "gui_motif",
10894#endif
10895#ifdef FEAT_GUI_PHOTON
10896 "gui_photon",
10897#endif
10898#ifdef FEAT_GUI_W16
10899 "gui_win16",
10900#endif
10901#ifdef FEAT_GUI_W32
10902 "gui_win32",
10903#endif
10904#ifdef FEAT_HANGULIN
10905 "hangul_input",
10906#endif
10907#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10908 "iconv",
10909#endif
10910#ifdef FEAT_INS_EXPAND
10911 "insert_expand",
10912#endif
10913#ifdef FEAT_JUMPLIST
10914 "jumplist",
10915#endif
10916#ifdef FEAT_KEYMAP
10917 "keymap",
10918#endif
10919#ifdef FEAT_LANGMAP
10920 "langmap",
10921#endif
10922#ifdef FEAT_LIBCALL
10923 "libcall",
10924#endif
10925#ifdef FEAT_LINEBREAK
10926 "linebreak",
10927#endif
10928#ifdef FEAT_LISP
10929 "lispindent",
10930#endif
10931#ifdef FEAT_LISTCMDS
10932 "listcmds",
10933#endif
10934#ifdef FEAT_LOCALMAP
10935 "localmap",
10936#endif
10937#ifdef FEAT_MENU
10938 "menu",
10939#endif
10940#ifdef FEAT_SESSION
10941 "mksession",
10942#endif
10943#ifdef FEAT_MODIFY_FNAME
10944 "modify_fname",
10945#endif
10946#ifdef FEAT_MOUSE
10947 "mouse",
10948#endif
10949#ifdef FEAT_MOUSESHAPE
10950 "mouseshape",
10951#endif
10952#if defined(UNIX) || defined(VMS)
10953# ifdef FEAT_MOUSE_DEC
10954 "mouse_dec",
10955# endif
10956# ifdef FEAT_MOUSE_GPM
10957 "mouse_gpm",
10958# endif
10959# ifdef FEAT_MOUSE_JSB
10960 "mouse_jsbterm",
10961# endif
10962# ifdef FEAT_MOUSE_NET
10963 "mouse_netterm",
10964# endif
10965# ifdef FEAT_MOUSE_PTERM
10966 "mouse_pterm",
10967# endif
10968# ifdef FEAT_MOUSE_XTERM
10969 "mouse_xterm",
10970# endif
10971#endif
10972#ifdef FEAT_MBYTE
10973 "multi_byte",
10974#endif
10975#ifdef FEAT_MBYTE_IME
10976 "multi_byte_ime",
10977#endif
10978#ifdef FEAT_MULTI_LANG
10979 "multi_lang",
10980#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010981#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010982#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010983 "mzscheme",
10984#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986#ifdef FEAT_OLE
10987 "ole",
10988#endif
10989#ifdef FEAT_OSFILETYPE
10990 "osfiletype",
10991#endif
10992#ifdef FEAT_PATH_EXTRA
10993 "path_extra",
10994#endif
10995#ifdef FEAT_PERL
10996#ifndef DYNAMIC_PERL
10997 "perl",
10998#endif
10999#endif
11000#ifdef FEAT_PYTHON
11001#ifndef DYNAMIC_PYTHON
11002 "python",
11003#endif
11004#endif
11005#ifdef FEAT_POSTSCRIPT
11006 "postscript",
11007#endif
11008#ifdef FEAT_PRINTER
11009 "printer",
11010#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011011#ifdef FEAT_PROFILE
11012 "profile",
11013#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011014#ifdef FEAT_RELTIME
11015 "reltime",
11016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017#ifdef FEAT_QUICKFIX
11018 "quickfix",
11019#endif
11020#ifdef FEAT_RIGHTLEFT
11021 "rightleft",
11022#endif
11023#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11024 "ruby",
11025#endif
11026#ifdef FEAT_SCROLLBIND
11027 "scrollbind",
11028#endif
11029#ifdef FEAT_CMDL_INFO
11030 "showcmd",
11031 "cmdline_info",
11032#endif
11033#ifdef FEAT_SIGNS
11034 "signs",
11035#endif
11036#ifdef FEAT_SMARTINDENT
11037 "smartindent",
11038#endif
11039#ifdef FEAT_SNIFF
11040 "sniff",
11041#endif
11042#ifdef FEAT_STL_OPT
11043 "statusline",
11044#endif
11045#ifdef FEAT_SUN_WORKSHOP
11046 "sun_workshop",
11047#endif
11048#ifdef FEAT_NETBEANS_INTG
11049 "netbeans_intg",
11050#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011051#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011052 "spell",
11053#endif
11054#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 "syntax",
11056#endif
11057#if defined(USE_SYSTEM) || !defined(UNIX)
11058 "system",
11059#endif
11060#ifdef FEAT_TAG_BINS
11061 "tag_binary",
11062#endif
11063#ifdef FEAT_TAG_OLDSTATIC
11064 "tag_old_static",
11065#endif
11066#ifdef FEAT_TAG_ANYWHITE
11067 "tag_any_white",
11068#endif
11069#ifdef FEAT_TCL
11070# ifndef DYNAMIC_TCL
11071 "tcl",
11072# endif
11073#endif
11074#ifdef TERMINFO
11075 "terminfo",
11076#endif
11077#ifdef FEAT_TERMRESPONSE
11078 "termresponse",
11079#endif
11080#ifdef FEAT_TEXTOBJ
11081 "textobjects",
11082#endif
11083#ifdef HAVE_TGETENT
11084 "tgetent",
11085#endif
11086#ifdef FEAT_TITLE
11087 "title",
11088#endif
11089#ifdef FEAT_TOOLBAR
11090 "toolbar",
11091#endif
11092#ifdef FEAT_USR_CMDS
11093 "user-commands", /* was accidentally included in 5.4 */
11094 "user_commands",
11095#endif
11096#ifdef FEAT_VIMINFO
11097 "viminfo",
11098#endif
11099#ifdef FEAT_VERTSPLIT
11100 "vertsplit",
11101#endif
11102#ifdef FEAT_VIRTUALEDIT
11103 "virtualedit",
11104#endif
11105#ifdef FEAT_VISUAL
11106 "visual",
11107#endif
11108#ifdef FEAT_VISUALEXTRA
11109 "visualextra",
11110#endif
11111#ifdef FEAT_VREPLACE
11112 "vreplace",
11113#endif
11114#ifdef FEAT_WILDIGN
11115 "wildignore",
11116#endif
11117#ifdef FEAT_WILDMENU
11118 "wildmenu",
11119#endif
11120#ifdef FEAT_WINDOWS
11121 "windows",
11122#endif
11123#ifdef FEAT_WAK
11124 "winaltkeys",
11125#endif
11126#ifdef FEAT_WRITEBACKUP
11127 "writebackup",
11128#endif
11129#ifdef FEAT_XIM
11130 "xim",
11131#endif
11132#ifdef FEAT_XFONTSET
11133 "xfontset",
11134#endif
11135#ifdef USE_XSMP
11136 "xsmp",
11137#endif
11138#ifdef USE_XSMP_INTERACT
11139 "xsmp_interact",
11140#endif
11141#ifdef FEAT_XCLIPBOARD
11142 "xterm_clipboard",
11143#endif
11144#ifdef FEAT_XTERM_SAVE
11145 "xterm_save",
11146#endif
11147#if defined(UNIX) && defined(FEAT_X11)
11148 "X11",
11149#endif
11150 NULL
11151 };
11152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 for (i = 0; has_list[i] != NULL; ++i)
11155 if (STRICMP(name, has_list[i]) == 0)
11156 {
11157 n = TRUE;
11158 break;
11159 }
11160
11161 if (n == FALSE)
11162 {
11163 if (STRNICMP(name, "patch", 5) == 0)
11164 n = has_patch(atoi((char *)name + 5));
11165 else if (STRICMP(name, "vim_starting") == 0)
11166 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011167#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11168 else if (STRICMP(name, "balloon_multiline") == 0)
11169 n = multiline_balloon_available();
11170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171#ifdef DYNAMIC_TCL
11172 else if (STRICMP(name, "tcl") == 0)
11173 n = tcl_enabled(FALSE);
11174#endif
11175#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11176 else if (STRICMP(name, "iconv") == 0)
11177 n = iconv_enabled(FALSE);
11178#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011179#ifdef DYNAMIC_MZSCHEME
11180 else if (STRICMP(name, "mzscheme") == 0)
11181 n = mzscheme_enabled(FALSE);
11182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183#ifdef DYNAMIC_RUBY
11184 else if (STRICMP(name, "ruby") == 0)
11185 n = ruby_enabled(FALSE);
11186#endif
11187#ifdef DYNAMIC_PYTHON
11188 else if (STRICMP(name, "python") == 0)
11189 n = python_enabled(FALSE);
11190#endif
11191#ifdef DYNAMIC_PERL
11192 else if (STRICMP(name, "perl") == 0)
11193 n = perl_enabled(FALSE);
11194#endif
11195#ifdef FEAT_GUI
11196 else if (STRICMP(name, "gui_running") == 0)
11197 n = (gui.in_use || gui.starting);
11198# ifdef FEAT_GUI_W32
11199 else if (STRICMP(name, "gui_win32s") == 0)
11200 n = gui_is_win32s();
11201# endif
11202# ifdef FEAT_BROWSE
11203 else if (STRICMP(name, "browse") == 0)
11204 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11205# endif
11206#endif
11207#ifdef FEAT_SYN_HL
11208 else if (STRICMP(name, "syntax_items") == 0)
11209 n = syntax_present(curbuf);
11210#endif
11211#if defined(WIN3264)
11212 else if (STRICMP(name, "win95") == 0)
11213 n = mch_windows95();
11214#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011215#ifdef FEAT_NETBEANS_INTG
11216 else if (STRICMP(name, "netbeans_enabled") == 0)
11217 n = usingNetbeans;
11218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 }
11220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222}
11223
11224/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011225 * "has_key()" function
11226 */
11227 static void
11228f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011229 typval_T *argvars;
11230 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231{
11232 rettv->vval.v_number = 0;
11233 if (argvars[0].v_type != VAR_DICT)
11234 {
11235 EMSG(_(e_dictreq));
11236 return;
11237 }
11238 if (argvars[0].vval.v_dict == NULL)
11239 return;
11240
11241 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011242 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011243}
11244
11245/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011246 * "haslocaldir()" function
11247 */
11248/*ARGSUSED*/
11249 static void
11250f_haslocaldir(argvars, rettv)
11251 typval_T *argvars;
11252 typval_T *rettv;
11253{
11254 rettv->vval.v_number = (curwin->w_localdir != NULL);
11255}
11256
11257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 * "hasmapto()" function
11259 */
11260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011262 typval_T *argvars;
11263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264{
11265 char_u *name;
11266 char_u *mode;
11267 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011268 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011271 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 mode = (char_u *)"nvo";
11273 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011276 if (argvars[2].v_type != VAR_UNKNOWN)
11277 abbr = get_tv_number(&argvars[2]);
11278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279
Bram Moolenaar2c932302006-03-18 21:42:09 +000011280 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284}
11285
11286/*
11287 * "histadd()" function
11288 */
11289/*ARGSUSED*/
11290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *argvars;
11293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294{
11295#ifdef FEAT_CMDHIST
11296 int histype;
11297 char_u *str;
11298 char_u buf[NUMBUFLEN];
11299#endif
11300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 if (check_restricted() || check_secure())
11303 return;
11304#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11306 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 if (histype >= 0)
11308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 if (*str != NUL)
11311 {
11312 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 return;
11315 }
11316 }
11317#endif
11318}
11319
11320/*
11321 * "histdel()" function
11322 */
11323/*ARGSUSED*/
11324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011325f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011326 typval_T *argvars;
11327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328{
11329#ifdef FEAT_CMDHIST
11330 int n;
11331 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011334 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11335 if (str == NULL)
11336 n = 0;
11337 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011340 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 else
11345 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011346 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 get_tv_string_buf(&argvars[1], buf));
11348 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351#endif
11352}
11353
11354/*
11355 * "histget()" function
11356 */
11357/*ARGSUSED*/
11358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011360 typval_T *argvars;
11361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362{
11363#ifdef FEAT_CMDHIST
11364 int type;
11365 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11369 if (str == NULL)
11370 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011372 {
11373 type = get_histtype(str);
11374 if (argvars[1].v_type == VAR_UNKNOWN)
11375 idx = get_history_idx(type);
11376 else
11377 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11378 /* -1 on type error */
11379 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385}
11386
11387/*
11388 * "histnr()" function
11389 */
11390/*ARGSUSED*/
11391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011393 typval_T *argvars;
11394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395{
11396 int i;
11397
11398#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011399 char_u *history = get_tv_string_chk(&argvars[0]);
11400
11401 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 if (i >= HIST_CMD && i < HIST_COUNT)
11403 i = get_history_idx(i);
11404 else
11405#endif
11406 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408}
11409
11410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 * "highlightID(name)" function
11412 */
11413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414f_hlID(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419}
11420
11421/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011422 * "highlight_exists()" function
11423 */
11424 static void
11425f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011426 typval_T *argvars;
11427 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011428{
11429 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11430}
11431
11432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 * "hostname()" function
11434 */
11435/*ARGSUSED*/
11436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011438 typval_T *argvars;
11439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440{
11441 char_u hostname[256];
11442
11443 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 rettv->v_type = VAR_STRING;
11445 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446}
11447
11448/*
11449 * iconv() function
11450 */
11451/*ARGSUSED*/
11452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 typval_T *argvars;
11455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456{
11457#ifdef FEAT_MBYTE
11458 char_u buf1[NUMBUFLEN];
11459 char_u buf2[NUMBUFLEN];
11460 char_u *from, *to, *str;
11461 vimconv_T vimconv;
11462#endif
11463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->v_type = VAR_STRING;
11465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466
11467#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 str = get_tv_string(&argvars[0]);
11469 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11470 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 vimconv.vc_type = CONV_NONE;
11472 convert_setup(&vimconv, from, to);
11473
11474 /* If the encodings are equal, no conversion needed. */
11475 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479
11480 convert_setup(&vimconv, NULL, NULL);
11481 vim_free(from);
11482 vim_free(to);
11483#endif
11484}
11485
11486/*
11487 * "indent()" function
11488 */
11489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011491 typval_T *argvars;
11492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493{
11494 linenr_T lnum;
11495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501}
11502
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011503/*
11504 * "index()" function
11505 */
11506 static void
11507f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011508 typval_T *argvars;
11509 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011510{
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 list_T *l;
11512 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011513 long idx = 0;
11514 int ic = FALSE;
11515
11516 rettv->vval.v_number = -1;
11517 if (argvars[0].v_type != VAR_LIST)
11518 {
11519 EMSG(_(e_listreq));
11520 return;
11521 }
11522 l = argvars[0].vval.v_list;
11523 if (l != NULL)
11524 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011525 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011526 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528 int error = FALSE;
11529
Bram Moolenaar758711c2005-02-02 23:11:38 +000011530 /* Start at specified item. Use the cached index that list_find()
11531 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011533 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011534 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011535 ic = get_tv_number_chk(&argvars[3], &error);
11536 if (error)
11537 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011538 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011539
Bram Moolenaar758711c2005-02-02 23:11:38 +000011540 for ( ; item != NULL; item = item->li_next, ++idx)
11541 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011542 {
11543 rettv->vval.v_number = idx;
11544 break;
11545 }
11546 }
11547}
11548
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549static int inputsecret_flag = 0;
11550
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011551static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11552
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011554 * This function is used by f_input() and f_inputdialog() functions. The third
11555 * argument to f_input() specifies the type of completion to use at the
11556 * prompt. The third argument to f_inputdialog() specifies the value to return
11557 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 */
11559 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011560get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011561 typval_T *argvars;
11562 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011563 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011565 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 char_u *p = NULL;
11567 int c;
11568 char_u buf[NUMBUFLEN];
11569 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011570 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011571 int xp_type = EXPAND_NOTHING;
11572 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576
11577#ifdef NO_CONSOLE_INPUT
11578 /* While starting up, there is no place to enter text. */
11579 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581#endif
11582
11583 cmd_silent = FALSE; /* Want to see the prompt. */
11584 if (prompt != NULL)
11585 {
11586 /* Only the part of the message after the last NL is considered as
11587 * prompt for the command line */
11588 p = vim_strrchr(prompt, '\n');
11589 if (p == NULL)
11590 p = prompt;
11591 else
11592 {
11593 ++p;
11594 c = *p;
11595 *p = NUL;
11596 msg_start();
11597 msg_clr_eos();
11598 msg_puts_attr(prompt, echo_attr);
11599 msg_didout = FALSE;
11600 msg_starthere();
11601 *p = c;
11602 }
11603 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 if (argvars[1].v_type != VAR_UNKNOWN)
11606 {
11607 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11608 if (defstr != NULL)
11609 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011611 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011612 {
11613 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011614 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011615 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011616
Bram Moolenaar4463f292005-09-25 22:20:24 +000011617 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011618
Bram Moolenaar4463f292005-09-25 22:20:24 +000011619 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11620 if (xp_name == NULL)
11621 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011622
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011623 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011624
Bram Moolenaar4463f292005-09-25 22:20:24 +000011625 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11626 &xp_arg) == FAIL)
11627 return;
11628 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011629 }
11630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011631 if (defstr != NULL)
11632 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011633 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11634 xp_type, xp_arg);
11635
11636 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011638 /* since the user typed this, no need to wait for return */
11639 need_wait_return = FALSE;
11640 msg_didout = FALSE;
11641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642 cmd_silent = cmd_silent_save;
11643}
11644
11645/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011646 * "input()" function
11647 * Also handles inputsecret() when inputsecret is set.
11648 */
11649 static void
11650f_input(argvars, rettv)
11651 typval_T *argvars;
11652 typval_T *rettv;
11653{
11654 get_user_input(argvars, rettv, FALSE);
11655}
11656
11657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 * "inputdialog()" function
11659 */
11660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011662 typval_T *argvars;
11663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664{
11665#if defined(FEAT_GUI_TEXTDIALOG)
11666 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11667 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11668 {
11669 char_u *message;
11670 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011671 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 message = get_tv_string_chk(&argvars[0]);
11674 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011675 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011676 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 else
11678 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 if (message != NULL && defstr != NULL
11680 && do_dialog(VIM_QUESTION, NULL, message,
11681 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 else
11684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011685 if (message != NULL && defstr != NULL
11686 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011687 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688 rettv->vval.v_string = vim_strsave(
11689 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011693 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 }
11695 else
11696#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011697 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698}
11699
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011700/*
11701 * "inputlist()" function
11702 */
11703 static void
11704f_inputlist(argvars, rettv)
11705 typval_T *argvars;
11706 typval_T *rettv;
11707{
11708 listitem_T *li;
11709 int selected;
11710 int mouse_used;
11711
11712 rettv->vval.v_number = 0;
11713#ifdef NO_CONSOLE_INPUT
11714 /* While starting up, there is no place to enter text. */
11715 if (no_console_input())
11716 return;
11717#endif
11718 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11719 {
11720 EMSG2(_(e_listarg), "inputlist()");
11721 return;
11722 }
11723
11724 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011725 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011726 lines_left = Rows; /* avoid more prompt */
11727 msg_scroll = TRUE;
11728 msg_clr_eos();
11729
11730 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11731 {
11732 msg_puts(get_tv_string(&li->li_tv));
11733 msg_putchar('\n');
11734 }
11735
11736 /* Ask for choice. */
11737 selected = prompt_for_number(&mouse_used);
11738 if (mouse_used)
11739 selected -= lines_left;
11740
11741 rettv->vval.v_number = selected;
11742}
11743
11744
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11746
11747/*
11748 * "inputrestore()" function
11749 */
11750/*ARGSUSED*/
11751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011753 typval_T *argvars;
11754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755{
11756 if (ga_userinput.ga_len > 0)
11757 {
11758 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11760 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 }
11763 else if (p_verbose > 1)
11764 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011765 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 }
11768}
11769
11770/*
11771 * "inputsave()" function
11772 */
11773/*ARGSUSED*/
11774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011775f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011776 typval_T *argvars;
11777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778{
11779 /* Add an entry to the stack of typehead storage. */
11780 if (ga_grow(&ga_userinput, 1) == OK)
11781 {
11782 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11783 + ga_userinput.ga_len);
11784 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 }
11787 else
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 * "inputsecret()" function
11793 */
11794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011796 typval_T *argvars;
11797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798{
11799 ++cmdline_star;
11800 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 --cmdline_star;
11803 --inputsecret_flag;
11804}
11805
11806/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011807 * "insert()" function
11808 */
11809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011811 typval_T *argvars;
11812 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011813{
11814 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011815 listitem_T *item;
11816 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011818
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011819 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011820 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011821 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011822 else if ((l = argvars[0].vval.v_list) != NULL
11823 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011824 {
11825 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826 before = get_tv_number_chk(&argvars[2], &error);
11827 if (error)
11828 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011829
Bram Moolenaar758711c2005-02-02 23:11:38 +000011830 if (before == l->lv_len)
11831 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011832 else
11833 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011834 item = list_find(l, before);
11835 if (item == NULL)
11836 {
11837 EMSGN(_(e_listidx), before);
11838 l = NULL;
11839 }
11840 }
11841 if (l != NULL)
11842 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011843 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011844 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011845 }
11846 }
11847}
11848
11849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 * "isdirectory()" function
11851 */
11852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011854 typval_T *argvars;
11855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858}
11859
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011860/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011861 * "islocked()" function
11862 */
11863 static void
11864f_islocked(argvars, rettv)
11865 typval_T *argvars;
11866 typval_T *rettv;
11867{
11868 lval_T lv;
11869 char_u *end;
11870 dictitem_T *di;
11871
11872 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011873 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11874 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011875 if (end != NULL && lv.ll_name != NULL)
11876 {
11877 if (*end != NUL)
11878 EMSG(_(e_trailing));
11879 else
11880 {
11881 if (lv.ll_tv == NULL)
11882 {
11883 if (check_changedtick(lv.ll_name))
11884 rettv->vval.v_number = 1; /* always locked */
11885 else
11886 {
11887 di = find_var(lv.ll_name, NULL);
11888 if (di != NULL)
11889 {
11890 /* Consider a variable locked when:
11891 * 1. the variable itself is locked
11892 * 2. the value of the variable is locked.
11893 * 3. the List or Dict value is locked.
11894 */
11895 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11896 || tv_islocked(&di->di_tv));
11897 }
11898 }
11899 }
11900 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011901 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011902 else if (lv.ll_newkey != NULL)
11903 EMSG2(_(e_dictkey), lv.ll_newkey);
11904 else if (lv.ll_list != NULL)
11905 /* List item. */
11906 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11907 else
11908 /* Dictionary item. */
11909 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11910 }
11911 }
11912
11913 clear_lval(&lv);
11914}
11915
Bram Moolenaar33570922005-01-25 22:26:29 +000011916static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011917
11918/*
11919 * Turn a dict into a list:
11920 * "what" == 0: list of keys
11921 * "what" == 1: list of values
11922 * "what" == 2: list of items
11923 */
11924 static void
11925dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011926 typval_T *argvars;
11927 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011928 int what;
11929{
Bram Moolenaar33570922005-01-25 22:26:29 +000011930 list_T *l2;
11931 dictitem_T *di;
11932 hashitem_T *hi;
11933 listitem_T *li;
11934 listitem_T *li2;
11935 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011936 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011937
11938 rettv->vval.v_number = 0;
11939 if (argvars[0].v_type != VAR_DICT)
11940 {
11941 EMSG(_(e_dictreq));
11942 return;
11943 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011944 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011945 return;
11946
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011947 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011948 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011949
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011950 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011951 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011953 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011954 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011955 --todo;
11956 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011957
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011958 li = listitem_alloc();
11959 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011960 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011961 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011962
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011963 if (what == 0)
11964 {
11965 /* keys() */
11966 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011967 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011968 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11969 }
11970 else if (what == 1)
11971 {
11972 /* values() */
11973 copy_tv(&di->di_tv, &li->li_tv);
11974 }
11975 else
11976 {
11977 /* items() */
11978 l2 = list_alloc();
11979 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011980 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011981 li->li_tv.vval.v_list = l2;
11982 if (l2 == NULL)
11983 break;
11984 ++l2->lv_refcount;
11985
11986 li2 = listitem_alloc();
11987 if (li2 == NULL)
11988 break;
11989 list_append(l2, li2);
11990 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011991 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011992 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11993
11994 li2 = listitem_alloc();
11995 if (li2 == NULL)
11996 break;
11997 list_append(l2, li2);
11998 copy_tv(&di->di_tv, &li2->li_tv);
11999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012000 }
12001 }
12002}
12003
12004/*
12005 * "items(dict)" function
12006 */
12007 static void
12008f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012009 typval_T *argvars;
12010 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012011{
12012 dict_list(argvars, rettv, 2);
12013}
12014
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012016 * "join()" function
12017 */
12018 static void
12019f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T *argvars;
12021 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012022{
12023 garray_T ga;
12024 char_u *sep;
12025
12026 rettv->vval.v_number = 0;
12027 if (argvars[0].v_type != VAR_LIST)
12028 {
12029 EMSG(_(e_listreq));
12030 return;
12031 }
12032 if (argvars[0].vval.v_list == NULL)
12033 return;
12034 if (argvars[1].v_type == VAR_UNKNOWN)
12035 sep = (char_u *)" ";
12036 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012037 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012038
12039 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012040
12041 if (sep != NULL)
12042 {
12043 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012044 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045 ga_append(&ga, NUL);
12046 rettv->vval.v_string = (char_u *)ga.ga_data;
12047 }
12048 else
12049 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012050}
12051
12052/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012053 * "keys()" function
12054 */
12055 static void
12056f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012057 typval_T *argvars;
12058 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012059{
12060 dict_list(argvars, rettv, 0);
12061}
12062
12063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 * "last_buffer_nr()" function.
12065 */
12066/*ARGSUSED*/
12067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012069 typval_T *argvars;
12070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071{
12072 int n = 0;
12073 buf_T *buf;
12074
12075 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12076 if (n < buf->b_fnum)
12077 n = buf->b_fnum;
12078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012080}
12081
12082/*
12083 * "len()" function
12084 */
12085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012087 typval_T *argvars;
12088 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012089{
12090 switch (argvars[0].v_type)
12091 {
12092 case VAR_STRING:
12093 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094 rettv->vval.v_number = (varnumber_T)STRLEN(
12095 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012096 break;
12097 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012098 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012099 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012100 case VAR_DICT:
12101 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12102 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012103 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012104 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012105 break;
12106 }
12107}
12108
Bram Moolenaar33570922005-01-25 22:26:29 +000012109static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012110
12111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012112libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012113 typval_T *argvars;
12114 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012115 int type;
12116{
12117#ifdef FEAT_LIBCALL
12118 char_u *string_in;
12119 char_u **string_result;
12120 int nr_result;
12121#endif
12122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012124 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012126 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012127 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012128
12129 if (check_restricted() || check_secure())
12130 return;
12131
12132#ifdef FEAT_LIBCALL
12133 /* The first two args must be strings, otherwise its meaningless */
12134 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12135 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012136 string_in = NULL;
12137 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012138 string_in = argvars[2].vval.v_string;
12139 if (type == VAR_NUMBER)
12140 string_result = NULL;
12141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012143 if (mch_libcall(argvars[0].vval.v_string,
12144 argvars[1].vval.v_string,
12145 string_in,
12146 argvars[2].vval.v_number,
12147 string_result,
12148 &nr_result) == OK
12149 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012151 }
12152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153}
12154
12155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012156 * "libcall()" function
12157 */
12158 static void
12159f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 typval_T *argvars;
12161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012162{
12163 libcall_common(argvars, rettv, VAR_STRING);
12164}
12165
12166/*
12167 * "libcallnr()" function
12168 */
12169 static void
12170f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012171 typval_T *argvars;
12172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012173{
12174 libcall_common(argvars, rettv, VAR_NUMBER);
12175}
12176
12177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 * "line(string)" function
12179 */
12180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012182 typval_T *argvars;
12183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184{
12185 linenr_T lnum = 0;
12186 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012187 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012189 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190 if (fp != NULL)
12191 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193}
12194
12195/*
12196 * "line2byte(lnum)" function
12197 */
12198/*ARGSUSED*/
12199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012201 typval_T *argvars;
12202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203{
12204#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206#else
12207 linenr_T lnum;
12208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012209 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012213 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12214 if (rettv->vval.v_number >= 0)
12215 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216#endif
12217}
12218
12219/*
12220 * "lispindent(lnum)" function
12221 */
12222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012223f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012224 typval_T *argvars;
12225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226{
12227#ifdef FEAT_LISP
12228 pos_T pos;
12229 linenr_T lnum;
12230
12231 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012232 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12234 {
12235 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012236 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237 curwin->w_cursor = pos;
12238 }
12239 else
12240#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242}
12243
12244/*
12245 * "localtime()" function
12246 */
12247/*ARGSUSED*/
12248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012250 typval_T *argvars;
12251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012253 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254}
12255
Bram Moolenaar33570922005-01-25 22:26:29 +000012256static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257
12258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012260 typval_T *argvars;
12261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 int exact;
12263{
12264 char_u *keys;
12265 char_u *which;
12266 char_u buf[NUMBUFLEN];
12267 char_u *keys_buf = NULL;
12268 char_u *rhs;
12269 int mode;
12270 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012271 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272
12273 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012274 rettv->v_type = VAR_STRING;
12275 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012277 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 if (*keys == NUL)
12279 return;
12280
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012281 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012283 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012284 if (argvars[2].v_type != VAR_UNKNOWN)
12285 abbr = get_tv_number(&argvars[2]);
12286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 else
12288 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012289 if (which == NULL)
12290 return;
12291
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 mode = get_map_mode(&which, 0);
12293
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012294 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012295 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 vim_free(keys_buf);
12297 if (rhs != NULL)
12298 {
12299 ga_init(&ga);
12300 ga.ga_itemsize = 1;
12301 ga.ga_growsize = 40;
12302
12303 while (*rhs != NUL)
12304 ga_concat(&ga, str2special(&rhs, FALSE));
12305
12306 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 }
12309}
12310
12311/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012312 * "map()" function
12313 */
12314 static void
12315f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012316 typval_T *argvars;
12317 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012318{
12319 filter_map(argvars, rettv, TRUE);
12320}
12321
12322/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012323 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 */
12325 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012326f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012327 typval_T *argvars;
12328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331}
12332
12333/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012334 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335 */
12336 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012337f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012338 typval_T *argvars;
12339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012341 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342}
12343
Bram Moolenaar33570922005-01-25 22:26:29 +000012344static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345
12346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012348 typval_T *argvars;
12349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 int type;
12351{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012352 char_u *str = NULL;
12353 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 char_u *pat;
12355 regmatch_T regmatch;
12356 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012357 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 char_u *save_cpo;
12359 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012360 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012361 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012362 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012363 list_T *l = NULL;
12364 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012365 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012366 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367
12368 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12369 save_cpo = p_cpo;
12370 p_cpo = (char_u *)"";
12371
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012372 rettv->vval.v_number = -1;
12373 if (type == 3)
12374 {
12375 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012376 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012377 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012378 }
12379 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012381 rettv->v_type = VAR_STRING;
12382 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012385 if (argvars[0].v_type == VAR_LIST)
12386 {
12387 if ((l = argvars[0].vval.v_list) == NULL)
12388 goto theend;
12389 li = l->lv_first;
12390 }
12391 else
12392 expr = str = get_tv_string(&argvars[0]);
12393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012394 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12395 if (pat == NULL)
12396 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012397
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012398 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012400 int error = FALSE;
12401
12402 start = get_tv_number_chk(&argvars[2], &error);
12403 if (error)
12404 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012405 if (l != NULL)
12406 {
12407 li = list_find(l, start);
12408 if (li == NULL)
12409 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012410 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012411 }
12412 else
12413 {
12414 if (start < 0)
12415 start = 0;
12416 if (start > (long)STRLEN(str))
12417 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012418 /* When "count" argument is there ignore matches before "start",
12419 * otherwise skip part of the string. Differs when pattern is "^"
12420 * or "\<". */
12421 if (argvars[3].v_type != VAR_UNKNOWN)
12422 startcol = start;
12423 else
12424 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012425 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012427 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012428 nth = get_tv_number_chk(&argvars[3], &error);
12429 if (error)
12430 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 }
12432
12433 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12434 if (regmatch.regprog != NULL)
12435 {
12436 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012437
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012438 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012439 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012440 if (l != NULL)
12441 {
12442 if (li == NULL)
12443 {
12444 match = FALSE;
12445 break;
12446 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012447 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012448 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012449 if (str == NULL)
12450 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012451 }
12452
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012453 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012454
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012455 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012456 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012457 if (l == NULL && !match)
12458 break;
12459
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012460 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012461 if (l != NULL)
12462 {
12463 li = li->li_next;
12464 ++idx;
12465 }
12466 else
12467 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012468#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012469 startcol = (colnr_T)(regmatch.startp[0]
12470 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012471#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012472 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012473#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012474 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012475 }
12476
12477 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012479 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012480 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012481 int i;
12482
12483 /* return list with matched string and submatches */
12484 for (i = 0; i < NSUBEXP; ++i)
12485 {
12486 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012487 {
12488 if (list_append_string(rettv->vval.v_list,
12489 (char_u *)"", 0) == FAIL)
12490 break;
12491 }
12492 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012493 regmatch.startp[i],
12494 (int)(regmatch.endp[i] - regmatch.startp[i]))
12495 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012496 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012497 }
12498 }
12499 else if (type == 2)
12500 {
12501 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012502 if (l != NULL)
12503 copy_tv(&li->li_tv, rettv);
12504 else
12505 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012507 }
12508 else if (l != NULL)
12509 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 else
12511 {
12512 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 (varnumber_T)(regmatch.startp[0] - str);
12515 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012516 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012518 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 }
12520 }
12521 vim_free(regmatch.regprog);
12522 }
12523
12524theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012525 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 p_cpo = save_cpo;
12527}
12528
12529/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012530 * "match()" function
12531 */
12532 static void
12533f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 typval_T *argvars;
12535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012536{
12537 find_some_match(argvars, rettv, 1);
12538}
12539
12540/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012541 * "matchadd()" function
12542 */
12543 static void
12544f_matchadd(argvars, rettv)
12545 typval_T *argvars;
12546 typval_T *rettv;
12547{
12548#ifdef FEAT_SEARCH_EXTRA
12549 char_u buf[NUMBUFLEN];
12550 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12551 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12552 int prio = 10; /* default priority */
12553 int id = -1;
12554 int error = FALSE;
12555
12556 rettv->vval.v_number = -1;
12557
12558 if (grp == NULL || pat == NULL)
12559 return;
12560 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012561 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012562 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012563 if (argvars[3].v_type != VAR_UNKNOWN)
12564 id = get_tv_number_chk(&argvars[3], &error);
12565 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012566 if (error == TRUE)
12567 return;
12568 if (id >= 1 && id <= 3)
12569 {
12570 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12571 return;
12572 }
12573
12574 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12575#endif
12576}
12577
12578/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012579 * "matcharg()" function
12580 */
12581 static void
12582f_matcharg(argvars, rettv)
12583 typval_T *argvars;
12584 typval_T *rettv;
12585{
12586 if (rettv_list_alloc(rettv) == OK)
12587 {
12588#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012589 int id = get_tv_number(&argvars[0]);
12590 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012591
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012592 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012593 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012594 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12595 {
12596 list_append_string(rettv->vval.v_list,
12597 syn_id2name(m->hlg_id), -1);
12598 list_append_string(rettv->vval.v_list, m->pattern, -1);
12599 }
12600 else
12601 {
12602 list_append_string(rettv->vval.v_list, NUL, -1);
12603 list_append_string(rettv->vval.v_list, NUL, -1);
12604 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012605 }
12606#endif
12607 }
12608}
12609
12610/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012611 * "matchdelete()" function
12612 */
12613 static void
12614f_matchdelete(argvars, rettv)
12615 typval_T *argvars;
12616 typval_T *rettv;
12617{
12618#ifdef FEAT_SEARCH_EXTRA
12619 rettv->vval.v_number = match_delete(curwin,
12620 (int)get_tv_number(&argvars[0]), TRUE);
12621#endif
12622}
12623
12624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012625 * "matchend()" function
12626 */
12627 static void
12628f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012629 typval_T *argvars;
12630 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631{
12632 find_some_match(argvars, rettv, 0);
12633}
12634
12635/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012636 * "matchlist()" function
12637 */
12638 static void
12639f_matchlist(argvars, rettv)
12640 typval_T *argvars;
12641 typval_T *rettv;
12642{
12643 find_some_match(argvars, rettv, 3);
12644}
12645
12646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012647 * "matchstr()" function
12648 */
12649 static void
12650f_matchstr(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, 2);
12655}
12656
Bram Moolenaar33570922005-01-25 22:26:29 +000012657static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012658
12659 static void
12660max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012661 typval_T *argvars;
12662 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012663 int domax;
12664{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012665 long n = 0;
12666 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012667 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012668
12669 if (argvars[0].v_type == VAR_LIST)
12670 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012671 list_T *l;
12672 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012673
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012674 l = argvars[0].vval.v_list;
12675 if (l != NULL)
12676 {
12677 li = l->lv_first;
12678 if (li != NULL)
12679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012680 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012681 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012682 {
12683 li = li->li_next;
12684 if (li == NULL)
12685 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012686 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012687 if (domax ? i > n : i < n)
12688 n = i;
12689 }
12690 }
12691 }
12692 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012693 else if (argvars[0].v_type == VAR_DICT)
12694 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012695 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012696 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012698 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012699
12700 d = argvars[0].vval.v_dict;
12701 if (d != NULL)
12702 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012703 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012704 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012705 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012706 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012707 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012708 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012710 if (first)
12711 {
12712 n = i;
12713 first = FALSE;
12714 }
12715 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012716 n = i;
12717 }
12718 }
12719 }
12720 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012721 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012722 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012724}
12725
12726/*
12727 * "max()" function
12728 */
12729 static void
12730f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012731 typval_T *argvars;
12732 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012733{
12734 max_min(argvars, rettv, TRUE);
12735}
12736
12737/*
12738 * "min()" function
12739 */
12740 static void
12741f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012742 typval_T *argvars;
12743 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012744{
12745 max_min(argvars, rettv, FALSE);
12746}
12747
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012748static int mkdir_recurse __ARGS((char_u *dir, int prot));
12749
12750/*
12751 * Create the directory in which "dir" is located, and higher levels when
12752 * needed.
12753 */
12754 static int
12755mkdir_recurse(dir, prot)
12756 char_u *dir;
12757 int prot;
12758{
12759 char_u *p;
12760 char_u *updir;
12761 int r = FAIL;
12762
12763 /* Get end of directory name in "dir".
12764 * We're done when it's "/" or "c:/". */
12765 p = gettail_sep(dir);
12766 if (p <= get_past_head(dir))
12767 return OK;
12768
12769 /* If the directory exists we're done. Otherwise: create it.*/
12770 updir = vim_strnsave(dir, (int)(p - dir));
12771 if (updir == NULL)
12772 return FAIL;
12773 if (mch_isdir(updir))
12774 r = OK;
12775 else if (mkdir_recurse(updir, prot) == OK)
12776 r = vim_mkdir_emsg(updir, prot);
12777 vim_free(updir);
12778 return r;
12779}
12780
12781#ifdef vim_mkdir
12782/*
12783 * "mkdir()" function
12784 */
12785 static void
12786f_mkdir(argvars, rettv)
12787 typval_T *argvars;
12788 typval_T *rettv;
12789{
12790 char_u *dir;
12791 char_u buf[NUMBUFLEN];
12792 int prot = 0755;
12793
12794 rettv->vval.v_number = FAIL;
12795 if (check_restricted() || check_secure())
12796 return;
12797
12798 dir = get_tv_string_buf(&argvars[0], buf);
12799 if (argvars[1].v_type != VAR_UNKNOWN)
12800 {
12801 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012802 prot = get_tv_number_chk(&argvars[2], NULL);
12803 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012804 mkdir_recurse(dir, prot);
12805 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012806 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012807}
12808#endif
12809
Bram Moolenaar0d660222005-01-07 21:51:51 +000012810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 * "mode()" function
12812 */
12813/*ARGSUSED*/
12814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012816 typval_T *argvars;
12817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818{
12819 char_u buf[2];
12820
12821#ifdef FEAT_VISUAL
12822 if (VIsual_active)
12823 {
12824 if (VIsual_select)
12825 buf[0] = VIsual_mode + 's' - 'v';
12826 else
12827 buf[0] = VIsual_mode;
12828 }
12829 else
12830#endif
12831 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12832 buf[0] = 'r';
12833 else if (State & INSERT)
12834 {
12835 if (State & REPLACE_FLAG)
12836 buf[0] = 'R';
12837 else
12838 buf[0] = 'i';
12839 }
12840 else if (State & CMDLINE)
12841 buf[0] = 'c';
12842 else
12843 buf[0] = 'n';
12844
12845 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->vval.v_string = vim_strsave(buf);
12847 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848}
12849
12850/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012851 * "nextnonblank()" function
12852 */
12853 static void
12854f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012855 typval_T *argvars;
12856 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012857{
12858 linenr_T lnum;
12859
12860 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012862 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012863 {
12864 lnum = 0;
12865 break;
12866 }
12867 if (*skipwhite(ml_get(lnum)) != NUL)
12868 break;
12869 }
12870 rettv->vval.v_number = lnum;
12871}
12872
12873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 * "nr2char()" function
12875 */
12876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012878 typval_T *argvars;
12879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880{
12881 char_u buf[NUMBUFLEN];
12882
12883#ifdef FEAT_MBYTE
12884 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 else
12887#endif
12888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890 buf[1] = NUL;
12891 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 rettv->v_type = VAR_STRING;
12893 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012894}
12895
12896/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012897 * "pathshorten()" function
12898 */
12899 static void
12900f_pathshorten(argvars, rettv)
12901 typval_T *argvars;
12902 typval_T *rettv;
12903{
12904 char_u *p;
12905
12906 rettv->v_type = VAR_STRING;
12907 p = get_tv_string_chk(&argvars[0]);
12908 if (p == NULL)
12909 rettv->vval.v_string = NULL;
12910 else
12911 {
12912 p = vim_strsave(p);
12913 rettv->vval.v_string = p;
12914 if (p != NULL)
12915 shorten_dir(p);
12916 }
12917}
12918
12919/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012920 * "prevnonblank()" function
12921 */
12922 static void
12923f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012924 typval_T *argvars;
12925 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012926{
12927 linenr_T lnum;
12928
12929 lnum = get_tv_lnum(argvars);
12930 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12931 lnum = 0;
12932 else
12933 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12934 --lnum;
12935 rettv->vval.v_number = lnum;
12936}
12937
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012938#ifdef HAVE_STDARG_H
12939/* This dummy va_list is here because:
12940 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12941 * - locally in the function results in a "used before set" warning
12942 * - using va_start() to initialize it gives "function with fixed args" error */
12943static va_list ap;
12944#endif
12945
Bram Moolenaar8c711452005-01-14 21:53:12 +000012946/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012947 * "printf()" function
12948 */
12949 static void
12950f_printf(argvars, rettv)
12951 typval_T *argvars;
12952 typval_T *rettv;
12953{
12954 rettv->v_type = VAR_STRING;
12955 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012956#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012957 {
12958 char_u buf[NUMBUFLEN];
12959 int len;
12960 char_u *s;
12961 int saved_did_emsg = did_emsg;
12962 char *fmt;
12963
12964 /* Get the required length, allocate the buffer and do it for real. */
12965 did_emsg = FALSE;
12966 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012967 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012968 if (!did_emsg)
12969 {
12970 s = alloc(len + 1);
12971 if (s != NULL)
12972 {
12973 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012974 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012975 }
12976 }
12977 did_emsg |= saved_did_emsg;
12978 }
12979#endif
12980}
12981
12982/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012983 * "pumvisible()" function
12984 */
12985/*ARGSUSED*/
12986 static void
12987f_pumvisible(argvars, rettv)
12988 typval_T *argvars;
12989 typval_T *rettv;
12990{
12991 rettv->vval.v_number = 0;
12992#ifdef FEAT_INS_EXPAND
12993 if (pum_visible())
12994 rettv->vval.v_number = 1;
12995#endif
12996}
12997
12998/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012999 * "range()" function
13000 */
13001 static void
13002f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013003 typval_T *argvars;
13004 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013005{
13006 long start;
13007 long end;
13008 long stride = 1;
13009 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013010 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013012 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013013 if (argvars[1].v_type == VAR_UNKNOWN)
13014 {
13015 end = start - 1;
13016 start = 0;
13017 }
13018 else
13019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013020 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013021 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013022 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013023 }
13024
13025 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013026 if (error)
13027 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013028 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013029 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013030 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013031 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013032 else
13033 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013034 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013035 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013036 if (list_append_number(rettv->vval.v_list,
13037 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013038 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013039 }
13040}
13041
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013042/*
13043 * "readfile()" function
13044 */
13045 static void
13046f_readfile(argvars, rettv)
13047 typval_T *argvars;
13048 typval_T *rettv;
13049{
13050 int binary = FALSE;
13051 char_u *fname;
13052 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013053 listitem_T *li;
13054#define FREAD_SIZE 200 /* optimized for text lines */
13055 char_u buf[FREAD_SIZE];
13056 int readlen; /* size of last fread() */
13057 int buflen; /* nr of valid chars in buf[] */
13058 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13059 int tolist; /* first byte in buf[] still to be put in list */
13060 int chop; /* how many CR to chop off */
13061 char_u *prev = NULL; /* previously read bytes, if any */
13062 int prevlen = 0; /* length of "prev" if not NULL */
13063 char_u *s;
13064 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013065 long maxline = MAXLNUM;
13066 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013067
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013068 if (argvars[1].v_type != VAR_UNKNOWN)
13069 {
13070 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13071 binary = TRUE;
13072 if (argvars[2].v_type != VAR_UNKNOWN)
13073 maxline = get_tv_number(&argvars[2]);
13074 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013075
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013076 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013077 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013078
13079 /* Always open the file in binary mode, library functions have a mind of
13080 * their own about CR-LF conversion. */
13081 fname = get_tv_string(&argvars[0]);
13082 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13083 {
13084 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13085 return;
13086 }
13087
13088 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013089 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013090 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013091 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013092 buflen = filtd + readlen;
13093 tolist = 0;
13094 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13095 {
13096 if (buf[filtd] == '\n' || readlen <= 0)
13097 {
13098 /* Only when in binary mode add an empty list item when the
13099 * last line ends in a '\n'. */
13100 if (!binary && readlen == 0 && filtd == 0)
13101 break;
13102
13103 /* Found end-of-line or end-of-file: add a text line to the
13104 * list. */
13105 chop = 0;
13106 if (!binary)
13107 while (filtd - chop - 1 >= tolist
13108 && buf[filtd - chop - 1] == '\r')
13109 ++chop;
13110 len = filtd - tolist - chop;
13111 if (prev == NULL)
13112 s = vim_strnsave(buf + tolist, len);
13113 else
13114 {
13115 s = alloc((unsigned)(prevlen + len + 1));
13116 if (s != NULL)
13117 {
13118 mch_memmove(s, prev, prevlen);
13119 vim_free(prev);
13120 prev = NULL;
13121 mch_memmove(s + prevlen, buf + tolist, len);
13122 s[prevlen + len] = NUL;
13123 }
13124 }
13125 tolist = filtd + 1;
13126
13127 li = listitem_alloc();
13128 if (li == NULL)
13129 {
13130 vim_free(s);
13131 break;
13132 }
13133 li->li_tv.v_type = VAR_STRING;
13134 li->li_tv.v_lock = 0;
13135 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013136 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013137
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013138 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013139 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013140 if (readlen <= 0)
13141 break;
13142 }
13143 else if (buf[filtd] == NUL)
13144 buf[filtd] = '\n';
13145 }
13146 if (readlen <= 0)
13147 break;
13148
13149 if (tolist == 0)
13150 {
13151 /* "buf" is full, need to move text to an allocated buffer */
13152 if (prev == NULL)
13153 {
13154 prev = vim_strnsave(buf, buflen);
13155 prevlen = buflen;
13156 }
13157 else
13158 {
13159 s = alloc((unsigned)(prevlen + buflen));
13160 if (s != NULL)
13161 {
13162 mch_memmove(s, prev, prevlen);
13163 mch_memmove(s + prevlen, buf, buflen);
13164 vim_free(prev);
13165 prev = s;
13166 prevlen += buflen;
13167 }
13168 }
13169 filtd = 0;
13170 }
13171 else
13172 {
13173 mch_memmove(buf, buf + tolist, buflen - tolist);
13174 filtd -= tolist;
13175 }
13176 }
13177
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013178 /*
13179 * For a negative line count use only the lines at the end of the file,
13180 * free the rest.
13181 */
13182 if (maxline < 0)
13183 while (cnt > -maxline)
13184 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013185 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013186 --cnt;
13187 }
13188
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013189 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013190 fclose(fd);
13191}
13192
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013193#if defined(FEAT_RELTIME)
13194static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13195
13196/*
13197 * Convert a List to proftime_T.
13198 * Return FAIL when there is something wrong.
13199 */
13200 static int
13201list2proftime(arg, tm)
13202 typval_T *arg;
13203 proftime_T *tm;
13204{
13205 long n1, n2;
13206 int error = FALSE;
13207
13208 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13209 || arg->vval.v_list->lv_len != 2)
13210 return FAIL;
13211 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13212 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13213# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013214 tm->HighPart = n1;
13215 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013216# else
13217 tm->tv_sec = n1;
13218 tm->tv_usec = n2;
13219# endif
13220 return error ? FAIL : OK;
13221}
13222#endif /* FEAT_RELTIME */
13223
13224/*
13225 * "reltime()" function
13226 */
13227 static void
13228f_reltime(argvars, rettv)
13229 typval_T *argvars;
13230 typval_T *rettv;
13231{
13232#ifdef FEAT_RELTIME
13233 proftime_T res;
13234 proftime_T start;
13235
13236 if (argvars[0].v_type == VAR_UNKNOWN)
13237 {
13238 /* No arguments: get current time. */
13239 profile_start(&res);
13240 }
13241 else if (argvars[1].v_type == VAR_UNKNOWN)
13242 {
13243 if (list2proftime(&argvars[0], &res) == FAIL)
13244 return;
13245 profile_end(&res);
13246 }
13247 else
13248 {
13249 /* Two arguments: compute the difference. */
13250 if (list2proftime(&argvars[0], &start) == FAIL
13251 || list2proftime(&argvars[1], &res) == FAIL)
13252 return;
13253 profile_sub(&res, &start);
13254 }
13255
13256 if (rettv_list_alloc(rettv) == OK)
13257 {
13258 long n1, n2;
13259
13260# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013261 n1 = res.HighPart;
13262 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013263# else
13264 n1 = res.tv_sec;
13265 n2 = res.tv_usec;
13266# endif
13267 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13268 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13269 }
13270#endif
13271}
13272
13273/*
13274 * "reltimestr()" function
13275 */
13276 static void
13277f_reltimestr(argvars, rettv)
13278 typval_T *argvars;
13279 typval_T *rettv;
13280{
13281#ifdef FEAT_RELTIME
13282 proftime_T tm;
13283#endif
13284
13285 rettv->v_type = VAR_STRING;
13286 rettv->vval.v_string = NULL;
13287#ifdef FEAT_RELTIME
13288 if (list2proftime(&argvars[0], &tm) == OK)
13289 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13290#endif
13291}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013292
Bram Moolenaar0d660222005-01-07 21:51:51 +000013293#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13294static void make_connection __ARGS((void));
13295static int check_connection __ARGS((void));
13296
13297 static void
13298make_connection()
13299{
13300 if (X_DISPLAY == NULL
13301# ifdef FEAT_GUI
13302 && !gui.in_use
13303# endif
13304 )
13305 {
13306 x_force_connect = TRUE;
13307 setup_term_clip();
13308 x_force_connect = FALSE;
13309 }
13310}
13311
13312 static int
13313check_connection()
13314{
13315 make_connection();
13316 if (X_DISPLAY == NULL)
13317 {
13318 EMSG(_("E240: No connection to Vim server"));
13319 return FAIL;
13320 }
13321 return OK;
13322}
13323#endif
13324
13325#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013326static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013327
13328 static void
13329remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013330 typval_T *argvars;
13331 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013332 int expr;
13333{
13334 char_u *server_name;
13335 char_u *keys;
13336 char_u *r = NULL;
13337 char_u buf[NUMBUFLEN];
13338# ifdef WIN32
13339 HWND w;
13340# else
13341 Window w;
13342# endif
13343
13344 if (check_restricted() || check_secure())
13345 return;
13346
13347# ifdef FEAT_X11
13348 if (check_connection() == FAIL)
13349 return;
13350# endif
13351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013352 server_name = get_tv_string_chk(&argvars[0]);
13353 if (server_name == NULL)
13354 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013355 keys = get_tv_string_buf(&argvars[1], buf);
13356# ifdef WIN32
13357 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13358# else
13359 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13360 < 0)
13361# endif
13362 {
13363 if (r != NULL)
13364 EMSG(r); /* sending worked but evaluation failed */
13365 else
13366 EMSG2(_("E241: Unable to send to %s"), server_name);
13367 return;
13368 }
13369
13370 rettv->vval.v_string = r;
13371
13372 if (argvars[2].v_type != VAR_UNKNOWN)
13373 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013374 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013375 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013376 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013377
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013378 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 v.di_tv.v_type = VAR_STRING;
13380 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013381 idvar = get_tv_string_chk(&argvars[2]);
13382 if (idvar != NULL)
13383 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013384 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013385 }
13386}
13387#endif
13388
13389/*
13390 * "remote_expr()" function
13391 */
13392/*ARGSUSED*/
13393 static void
13394f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013395 typval_T *argvars;
13396 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013397{
13398 rettv->v_type = VAR_STRING;
13399 rettv->vval.v_string = NULL;
13400#ifdef FEAT_CLIENTSERVER
13401 remote_common(argvars, rettv, TRUE);
13402#endif
13403}
13404
13405/*
13406 * "remote_foreground()" function
13407 */
13408/*ARGSUSED*/
13409 static void
13410f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013411 typval_T *argvars;
13412 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013413{
13414 rettv->vval.v_number = 0;
13415#ifdef FEAT_CLIENTSERVER
13416# ifdef WIN32
13417 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013418 {
13419 char_u *server_name = get_tv_string_chk(&argvars[0]);
13420
13421 if (server_name != NULL)
13422 serverForeground(server_name);
13423 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013424# else
13425 /* Send a foreground() expression to the server. */
13426 argvars[1].v_type = VAR_STRING;
13427 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13428 argvars[2].v_type = VAR_UNKNOWN;
13429 remote_common(argvars, rettv, TRUE);
13430 vim_free(argvars[1].vval.v_string);
13431# endif
13432#endif
13433}
13434
13435/*ARGSUSED*/
13436 static void
13437f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013438 typval_T *argvars;
13439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013440{
13441#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013443 char_u *s = NULL;
13444# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013445 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013446# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013447 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013448
13449 if (check_restricted() || check_secure())
13450 {
13451 rettv->vval.v_number = -1;
13452 return;
13453 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013454 serverid = get_tv_string_chk(&argvars[0]);
13455 if (serverid == NULL)
13456 {
13457 rettv->vval.v_number = -1;
13458 return; /* type error; errmsg already given */
13459 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013460# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013461 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013462 if (n == 0)
13463 rettv->vval.v_number = -1;
13464 else
13465 {
13466 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13467 rettv->vval.v_number = (s != NULL);
13468 }
13469# else
13470 rettv->vval.v_number = 0;
13471 if (check_connection() == FAIL)
13472 return;
13473
13474 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013475 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013476# endif
13477
13478 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013480 char_u *retvar;
13481
Bram Moolenaar33570922005-01-25 22:26:29 +000013482 v.di_tv.v_type = VAR_STRING;
13483 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013484 retvar = get_tv_string_chk(&argvars[1]);
13485 if (retvar != NULL)
13486 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013487 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013488 }
13489#else
13490 rettv->vval.v_number = -1;
13491#endif
13492}
13493
13494/*ARGSUSED*/
13495 static void
13496f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013497 typval_T *argvars;
13498 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013499{
13500 char_u *r = NULL;
13501
13502#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013503 char_u *serverid = get_tv_string_chk(&argvars[0]);
13504
13505 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013506 {
13507# ifdef WIN32
13508 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013509 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013510
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013511 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013512 if (n != 0)
13513 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13514 if (r == NULL)
13515# else
13516 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013517 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013518# endif
13519 EMSG(_("E277: Unable to read a server reply"));
13520 }
13521#endif
13522 rettv->v_type = VAR_STRING;
13523 rettv->vval.v_string = r;
13524}
13525
13526/*
13527 * "remote_send()" function
13528 */
13529/*ARGSUSED*/
13530 static void
13531f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 typval_T *argvars;
13533 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013534{
13535 rettv->v_type = VAR_STRING;
13536 rettv->vval.v_string = NULL;
13537#ifdef FEAT_CLIENTSERVER
13538 remote_common(argvars, rettv, FALSE);
13539#endif
13540}
13541
13542/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013543 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013544 */
13545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013547 typval_T *argvars;
13548 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013549{
Bram Moolenaar33570922005-01-25 22:26:29 +000013550 list_T *l;
13551 listitem_T *item, *item2;
13552 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013553 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013554 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013555 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 dict_T *d;
13557 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013558
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013559 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013560 if (argvars[0].v_type == VAR_DICT)
13561 {
13562 if (argvars[2].v_type != VAR_UNKNOWN)
13563 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013564 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013565 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013567 key = get_tv_string_chk(&argvars[1]);
13568 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 di = dict_find(d, key, -1);
13571 if (di == NULL)
13572 EMSG2(_(e_dictkey), key);
13573 else
13574 {
13575 *rettv = di->di_tv;
13576 init_tv(&di->di_tv);
13577 dictitem_remove(d, di);
13578 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013580 }
13581 }
13582 else if (argvars[0].v_type != VAR_LIST)
13583 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013584 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013585 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013587 int error = FALSE;
13588
13589 idx = get_tv_number_chk(&argvars[1], &error);
13590 if (error)
13591 ; /* type error: do nothing, errmsg already given */
13592 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013593 EMSGN(_(e_listidx), idx);
13594 else
13595 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013596 if (argvars[2].v_type == VAR_UNKNOWN)
13597 {
13598 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013599 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013600 *rettv = item->li_tv;
13601 vim_free(item);
13602 }
13603 else
13604 {
13605 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013606 end = get_tv_number_chk(&argvars[2], &error);
13607 if (error)
13608 ; /* type error: do nothing */
13609 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013610 EMSGN(_(e_listidx), end);
13611 else
13612 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013613 int cnt = 0;
13614
13615 for (li = item; li != NULL; li = li->li_next)
13616 {
13617 ++cnt;
13618 if (li == item2)
13619 break;
13620 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013621 if (li == NULL) /* didn't find "item2" after "item" */
13622 EMSG(_(e_invrange));
13623 else
13624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013625 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013626 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013627 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013628 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013629 l->lv_first = item;
13630 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013631 item->li_prev = NULL;
13632 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013633 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013634 }
13635 }
13636 }
13637 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013638 }
13639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640}
13641
13642/*
13643 * "rename({from}, {to})" function
13644 */
13645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013647 typval_T *argvars;
13648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649{
13650 char_u buf[NUMBUFLEN];
13651
13652 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13656 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657}
13658
13659/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013660 * "repeat()" function
13661 */
13662/*ARGSUSED*/
13663 static void
13664f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013665 typval_T *argvars;
13666 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013667{
13668 char_u *p;
13669 int n;
13670 int slen;
13671 int len;
13672 char_u *r;
13673 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013674
13675 n = get_tv_number(&argvars[1]);
13676 if (argvars[0].v_type == VAR_LIST)
13677 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013678 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013679 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013680 if (list_extend(rettv->vval.v_list,
13681 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013682 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013683 }
13684 else
13685 {
13686 p = get_tv_string(&argvars[0]);
13687 rettv->v_type = VAR_STRING;
13688 rettv->vval.v_string = NULL;
13689
13690 slen = (int)STRLEN(p);
13691 len = slen * n;
13692 if (len <= 0)
13693 return;
13694
13695 r = alloc(len + 1);
13696 if (r != NULL)
13697 {
13698 for (i = 0; i < n; i++)
13699 mch_memmove(r + i * slen, p, (size_t)slen);
13700 r[len] = NUL;
13701 }
13702
13703 rettv->vval.v_string = r;
13704 }
13705}
13706
13707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708 * "resolve()" function
13709 */
13710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013712 typval_T *argvars;
13713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714{
13715 char_u *p;
13716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013717 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718#ifdef FEAT_SHORTCUT
13719 {
13720 char_u *v = NULL;
13721
13722 v = mch_resolve_shortcut(p);
13723 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 }
13728#else
13729# ifdef HAVE_READLINK
13730 {
13731 char_u buf[MAXPATHL + 1];
13732 char_u *cpy;
13733 int len;
13734 char_u *remain = NULL;
13735 char_u *q;
13736 int is_relative_to_current = FALSE;
13737 int has_trailing_pathsep = FALSE;
13738 int limit = 100;
13739
13740 p = vim_strsave(p);
13741
13742 if (p[0] == '.' && (vim_ispathsep(p[1])
13743 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13744 is_relative_to_current = TRUE;
13745
13746 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013747 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 has_trailing_pathsep = TRUE;
13749
13750 q = getnextcomp(p);
13751 if (*q != NUL)
13752 {
13753 /* Separate the first path component in "p", and keep the
13754 * remainder (beginning with the path separator). */
13755 remain = vim_strsave(q - 1);
13756 q[-1] = NUL;
13757 }
13758
13759 for (;;)
13760 {
13761 for (;;)
13762 {
13763 len = readlink((char *)p, (char *)buf, MAXPATHL);
13764 if (len <= 0)
13765 break;
13766 buf[len] = NUL;
13767
13768 if (limit-- == 0)
13769 {
13770 vim_free(p);
13771 vim_free(remain);
13772 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013773 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 goto fail;
13775 }
13776
13777 /* Ensure that the result will have a trailing path separator
13778 * if the argument has one. */
13779 if (remain == NULL && has_trailing_pathsep)
13780 add_pathsep(buf);
13781
13782 /* Separate the first path component in the link value and
13783 * concatenate the remainders. */
13784 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13785 if (*q != NUL)
13786 {
13787 if (remain == NULL)
13788 remain = vim_strsave(q - 1);
13789 else
13790 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013791 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 if (cpy != NULL)
13793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 vim_free(remain);
13795 remain = cpy;
13796 }
13797 }
13798 q[-1] = NUL;
13799 }
13800
13801 q = gettail(p);
13802 if (q > p && *q == NUL)
13803 {
13804 /* Ignore trailing path separator. */
13805 q[-1] = NUL;
13806 q = gettail(p);
13807 }
13808 if (q > p && !mch_isFullName(buf))
13809 {
13810 /* symlink is relative to directory of argument */
13811 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13812 if (cpy != NULL)
13813 {
13814 STRCPY(cpy, p);
13815 STRCPY(gettail(cpy), buf);
13816 vim_free(p);
13817 p = cpy;
13818 }
13819 }
13820 else
13821 {
13822 vim_free(p);
13823 p = vim_strsave(buf);
13824 }
13825 }
13826
13827 if (remain == NULL)
13828 break;
13829
13830 /* Append the first path component of "remain" to "p". */
13831 q = getnextcomp(remain + 1);
13832 len = q - remain - (*q != NUL);
13833 cpy = vim_strnsave(p, STRLEN(p) + len);
13834 if (cpy != NULL)
13835 {
13836 STRNCAT(cpy, remain, len);
13837 vim_free(p);
13838 p = cpy;
13839 }
13840 /* Shorten "remain". */
13841 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013842 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 else
13844 {
13845 vim_free(remain);
13846 remain = NULL;
13847 }
13848 }
13849
13850 /* If the result is a relative path name, make it explicitly relative to
13851 * the current directory if and only if the argument had this form. */
13852 if (!vim_ispathsep(*p))
13853 {
13854 if (is_relative_to_current
13855 && *p != NUL
13856 && !(p[0] == '.'
13857 && (p[1] == NUL
13858 || vim_ispathsep(p[1])
13859 || (p[1] == '.'
13860 && (p[2] == NUL
13861 || vim_ispathsep(p[2]))))))
13862 {
13863 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013864 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 if (cpy != NULL)
13866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 vim_free(p);
13868 p = cpy;
13869 }
13870 }
13871 else if (!is_relative_to_current)
13872 {
13873 /* Strip leading "./". */
13874 q = p;
13875 while (q[0] == '.' && vim_ispathsep(q[1]))
13876 q += 2;
13877 if (q > p)
13878 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13879 }
13880 }
13881
13882 /* Ensure that the result will have no trailing path separator
13883 * if the argument had none. But keep "/" or "//". */
13884 if (!has_trailing_pathsep)
13885 {
13886 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013887 if (after_pathsep(p, q))
13888 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 }
13890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 }
13893# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895# endif
13896#endif
13897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013898 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
13900#ifdef HAVE_READLINK
13901fail:
13902#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013903 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904}
13905
13906/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013907 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 */
13909 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013910f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013911 typval_T *argvars;
13912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913{
Bram Moolenaar33570922005-01-25 22:26:29 +000013914 list_T *l;
13915 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916
Bram Moolenaar0d660222005-01-07 21:51:51 +000013917 rettv->vval.v_number = 0;
13918 if (argvars[0].v_type != VAR_LIST)
13919 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013920 else if ((l = argvars[0].vval.v_list) != NULL
13921 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013922 {
13923 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013924 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013925 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013926 while (li != NULL)
13927 {
13928 ni = li->li_prev;
13929 list_append(l, li);
13930 li = ni;
13931 }
13932 rettv->vval.v_list = l;
13933 rettv->v_type = VAR_LIST;
13934 ++l->lv_refcount;
13935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936}
13937
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013938#define SP_NOMOVE 0x01 /* don't move cursor */
13939#define SP_REPEAT 0x02 /* repeat to find outer pair */
13940#define SP_RETCOUNT 0x04 /* return matchcount */
13941#define SP_SETPCMARK 0x08 /* set previous context mark */
13942#define SP_START 0x10 /* accept match at start position */
13943#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13944#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013945
Bram Moolenaar33570922005-01-25 22:26:29 +000013946static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947
13948/*
13949 * Get flags for a search function.
13950 * Possibly sets "p_ws".
13951 * Returns BACKWARD, FORWARD or zero (for an error).
13952 */
13953 static int
13954get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013955 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956 int *flagsp;
13957{
13958 int dir = FORWARD;
13959 char_u *flags;
13960 char_u nbuf[NUMBUFLEN];
13961 int mask;
13962
13963 if (varp->v_type != VAR_UNKNOWN)
13964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013965 flags = get_tv_string_buf_chk(varp, nbuf);
13966 if (flags == NULL)
13967 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013968 while (*flags != NUL)
13969 {
13970 switch (*flags)
13971 {
13972 case 'b': dir = BACKWARD; break;
13973 case 'w': p_ws = TRUE; break;
13974 case 'W': p_ws = FALSE; break;
13975 default: mask = 0;
13976 if (flagsp != NULL)
13977 switch (*flags)
13978 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013979 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013980 case 'e': mask = SP_END; break;
13981 case 'm': mask = SP_RETCOUNT; break;
13982 case 'n': mask = SP_NOMOVE; break;
13983 case 'p': mask = SP_SUBPAT; break;
13984 case 'r': mask = SP_REPEAT; break;
13985 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013986 }
13987 if (mask == 0)
13988 {
13989 EMSG2(_(e_invarg2), flags);
13990 dir = 0;
13991 }
13992 else
13993 *flagsp |= mask;
13994 }
13995 if (dir == 0)
13996 break;
13997 ++flags;
13998 }
13999 }
14000 return dir;
14001}
14002
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014004 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014006 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014007search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014008 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014009 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014010 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014012 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 char_u *pat;
14014 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014015 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 int save_p_ws = p_ws;
14017 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014018 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014019 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014020 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014021 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014024 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014025 if (dir == 0)
14026 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014027 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014028 if (flags & SP_START)
14029 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014030 if (flags & SP_END)
14031 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014032
14033 /* Optional extra argument: line number to stop searching. */
14034 if (argvars[1].v_type != VAR_UNKNOWN
14035 && argvars[2].v_type != VAR_UNKNOWN)
14036 {
14037 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14038 if (lnum_stop < 0)
14039 goto theend;
14040 }
14041
Bram Moolenaar231334e2005-07-25 20:46:57 +000014042 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014043 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014044 * Check to make sure only those flags are set.
14045 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14046 * flags cannot be set. Check for that condition also.
14047 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014048 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014049 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014051 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014052 goto theend;
14053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014055 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014056 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
14057 options, RE_SEARCH, (linenr_T)lnum_stop);
14058 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014060 if (flags & SP_SUBPAT)
14061 retval = subpatnum;
14062 else
14063 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014064 if (flags & SP_SETPCMARK)
14065 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014067 if (match_pos != NULL)
14068 {
14069 /* Store the match cursor position */
14070 match_pos->lnum = pos.lnum;
14071 match_pos->col = pos.col + 1;
14072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 /* "/$" will put the cursor after the end of the line, may need to
14074 * correct that here */
14075 check_cursor();
14076 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014077
14078 /* If 'n' flag is used: restore cursor position. */
14079 if (flags & SP_NOMOVE)
14080 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014081 else
14082 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014083theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014085
14086 return retval;
14087}
14088
14089/*
14090 * "search()" function
14091 */
14092 static void
14093f_search(argvars, rettv)
14094 typval_T *argvars;
14095 typval_T *rettv;
14096{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014097 int flags = 0;
14098
14099 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100}
14101
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014103 * "searchdecl()" function
14104 */
14105 static void
14106f_searchdecl(argvars, rettv)
14107 typval_T *argvars;
14108 typval_T *rettv;
14109{
14110 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014111 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014112 int error = FALSE;
14113 char_u *name;
14114
14115 rettv->vval.v_number = 1; /* default: FAIL */
14116
14117 name = get_tv_string_chk(&argvars[0]);
14118 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014119 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014120 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014121 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14122 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14123 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014124 if (!error && name != NULL)
14125 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014126 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014127}
14128
14129/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014130 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014132 static int
14133searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014135 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136{
14137 char_u *spat, *mpat, *epat;
14138 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140 int dir;
14141 int flags = 0;
14142 char_u nbuf1[NUMBUFLEN];
14143 char_u nbuf2[NUMBUFLEN];
14144 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014145 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014146 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 spat = get_tv_string_chk(&argvars[0]);
14150 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14151 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14152 if (spat == NULL || mpat == NULL || epat == NULL)
14153 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 /* Handle the optional fourth argument: flags */
14156 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014157 if (dir == 0)
14158 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014159
14160 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014161 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14162 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014163 if ((flags & (SP_END | SP_SUBPAT)) != 0
14164 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014165 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014166 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014167 goto theend;
14168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014170 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014171 if (argvars[3].v_type == VAR_UNKNOWN
14172 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 skip = (char_u *)"";
14174 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014177 if (argvars[5].v_type != VAR_UNKNOWN)
14178 {
14179 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14180 if (lnum_stop < 0)
14181 goto theend;
14182 }
14183 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 if (skip == NULL)
14185 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014187 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14188 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014189
14190theend:
14191 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014192
14193 return retval;
14194}
14195
14196/*
14197 * "searchpair()" function
14198 */
14199 static void
14200f_searchpair(argvars, rettv)
14201 typval_T *argvars;
14202 typval_T *rettv;
14203{
14204 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14205}
14206
14207/*
14208 * "searchpairpos()" function
14209 */
14210 static void
14211f_searchpairpos(argvars, rettv)
14212 typval_T *argvars;
14213 typval_T *rettv;
14214{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014215 pos_T match_pos;
14216 int lnum = 0;
14217 int col = 0;
14218
14219 rettv->vval.v_number = 0;
14220
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014221 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014222 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014223
14224 if (searchpair_cmn(argvars, &match_pos) > 0)
14225 {
14226 lnum = match_pos.lnum;
14227 col = match_pos.col;
14228 }
14229
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014230 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14231 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014232}
14233
14234/*
14235 * Search for a start/middle/end thing.
14236 * Used by searchpair(), see its documentation for the details.
14237 * Returns 0 or -1 for no match,
14238 */
14239 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014240do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014241 char_u *spat; /* start pattern */
14242 char_u *mpat; /* middle pattern */
14243 char_u *epat; /* end pattern */
14244 int dir; /* BACKWARD or FORWARD */
14245 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014246 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014247 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014248 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014249{
14250 char_u *save_cpo;
14251 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14252 long retval = 0;
14253 pos_T pos;
14254 pos_T firstpos;
14255 pos_T foundpos;
14256 pos_T save_cursor;
14257 pos_T save_pos;
14258 int n;
14259 int r;
14260 int nest = 1;
14261 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014262 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014263
14264 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14265 save_cpo = p_cpo;
14266 p_cpo = (char_u *)"";
14267
14268 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14269 * start/middle/end (pat3, for the top pair). */
14270 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14271 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14272 if (pat2 == NULL || pat3 == NULL)
14273 goto theend;
14274 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14275 if (*mpat == NUL)
14276 STRCPY(pat3, pat2);
14277 else
14278 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14279 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014280 if (flags & SP_START)
14281 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014282
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 save_cursor = curwin->w_cursor;
14284 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014285 clearpos(&firstpos);
14286 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 pat = pat3;
14288 for (;;)
14289 {
14290 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014291 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14293 /* didn't find it or found the first match again: FAIL */
14294 break;
14295
14296 if (firstpos.lnum == 0)
14297 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014298 if (equalpos(pos, foundpos))
14299 {
14300 /* Found the same position again. Can happen with a pattern that
14301 * has "\zs" at the end and searching backwards. Advance one
14302 * character and try again. */
14303 if (dir == BACKWARD)
14304 decl(&pos);
14305 else
14306 incl(&pos);
14307 }
14308 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309
14310 /* If the skip pattern matches, ignore this match. */
14311 if (*skip != NUL)
14312 {
14313 save_pos = curwin->w_cursor;
14314 curwin->w_cursor = pos;
14315 r = eval_to_bool(skip, &err, NULL, FALSE);
14316 curwin->w_cursor = save_pos;
14317 if (err)
14318 {
14319 /* Evaluating {skip} caused an error, break here. */
14320 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014321 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 break;
14323 }
14324 if (r)
14325 continue;
14326 }
14327
14328 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14329 {
14330 /* Found end when searching backwards or start when searching
14331 * forward: nested pair. */
14332 ++nest;
14333 pat = pat2; /* nested, don't search for middle */
14334 }
14335 else
14336 {
14337 /* Found end when searching forward or start when searching
14338 * backward: end of (nested) pair; or found middle in outer pair. */
14339 if (--nest == 1)
14340 pat = pat3; /* outer level, search for middle */
14341 }
14342
14343 if (nest == 0)
14344 {
14345 /* Found the match: return matchcount or line number. */
14346 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014347 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014349 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014350 if (flags & SP_SETPCMARK)
14351 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 curwin->w_cursor = pos;
14353 if (!(flags & SP_REPEAT))
14354 break;
14355 nest = 1; /* search for next unmatched */
14356 }
14357 }
14358
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014359 if (match_pos != NULL)
14360 {
14361 /* Store the match cursor position */
14362 match_pos->lnum = curwin->w_cursor.lnum;
14363 match_pos->col = curwin->w_cursor.col + 1;
14364 }
14365
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014367 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 curwin->w_cursor = save_cursor;
14369
14370theend:
14371 vim_free(pat2);
14372 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014374
14375 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376}
14377
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014378/*
14379 * "searchpos()" function
14380 */
14381 static void
14382f_searchpos(argvars, rettv)
14383 typval_T *argvars;
14384 typval_T *rettv;
14385{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014386 pos_T match_pos;
14387 int lnum = 0;
14388 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014389 int n;
14390 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014391
14392 rettv->vval.v_number = 0;
14393
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014394 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014395 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014396
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014397 n = search_cmn(argvars, &match_pos, &flags);
14398 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014399 {
14400 lnum = match_pos.lnum;
14401 col = match_pos.col;
14402 }
14403
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014404 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14405 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014406 if (flags & SP_SUBPAT)
14407 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014408}
14409
14410
Bram Moolenaar0d660222005-01-07 21:51:51 +000014411/*ARGSUSED*/
14412 static void
14413f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014414 typval_T *argvars;
14415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014417#ifdef FEAT_CLIENTSERVER
14418 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 char_u *server = get_tv_string_chk(&argvars[0]);
14420 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421
Bram Moolenaar0d660222005-01-07 21:51:51 +000014422 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014423 if (server == NULL || reply == NULL)
14424 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425 if (check_restricted() || check_secure())
14426 return;
14427# ifdef FEAT_X11
14428 if (check_connection() == FAIL)
14429 return;
14430# endif
14431
14432 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434 EMSG(_("E258: Unable to send to client"));
14435 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014437 rettv->vval.v_number = 0;
14438#else
14439 rettv->vval.v_number = -1;
14440#endif
14441}
14442
14443/*ARGSUSED*/
14444 static void
14445f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014446 typval_T *argvars;
14447 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014448{
14449 char_u *r = NULL;
14450
14451#ifdef FEAT_CLIENTSERVER
14452# ifdef WIN32
14453 r = serverGetVimNames();
14454# else
14455 make_connection();
14456 if (X_DISPLAY != NULL)
14457 r = serverGetVimNames(X_DISPLAY);
14458# endif
14459#endif
14460 rettv->v_type = VAR_STRING;
14461 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462}
14463
14464/*
14465 * "setbufvar()" function
14466 */
14467/*ARGSUSED*/
14468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014470 typval_T *argvars;
14471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472{
14473 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014476 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 char_u nbuf[NUMBUFLEN];
14478
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014479 rettv->vval.v_number = 0;
14480
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 if (check_restricted() || check_secure())
14482 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014483 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14484 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014485 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 varp = &argvars[2];
14487
14488 if (buf != NULL && varname != NULL && varp != NULL)
14489 {
14490 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492
14493 if (*varname == '&')
14494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014495 long numval;
14496 char_u *strval;
14497 int error = FALSE;
14498
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014500 numval = get_tv_number_chk(varp, &error);
14501 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014502 if (!error && strval != NULL)
14503 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 }
14505 else
14506 {
14507 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14508 if (bufvarname != NULL)
14509 {
14510 STRCPY(bufvarname, "b:");
14511 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014512 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 vim_free(bufvarname);
14514 }
14515 }
14516
14517 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520}
14521
14522/*
14523 * "setcmdpos()" function
14524 */
14525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014527 typval_T *argvars;
14528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 int pos = (int)get_tv_number(&argvars[0]) - 1;
14531
14532 if (pos >= 0)
14533 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534}
14535
14536/*
14537 * "setline()" function
14538 */
14539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014540f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014541 typval_T *argvars;
14542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543{
14544 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014545 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014546 list_T *l = NULL;
14547 listitem_T *li = NULL;
14548 long added = 0;
14549 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014551 lnum = get_tv_lnum(&argvars[0]);
14552 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014554 l = argvars[1].vval.v_list;
14555 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014557 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014559
14560 rettv->vval.v_number = 0; /* OK */
14561 for (;;)
14562 {
14563 if (l != NULL)
14564 {
14565 /* list argument, get next string */
14566 if (li == NULL)
14567 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014569 li = li->li_next;
14570 }
14571
14572 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014574 break;
14575 if (lnum <= curbuf->b_ml.ml_line_count)
14576 {
14577 /* existing line, replace it */
14578 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14579 {
14580 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014581 if (lnum == curwin->w_cursor.lnum)
14582 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014583 rettv->vval.v_number = 0; /* OK */
14584 }
14585 }
14586 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14587 {
14588 /* lnum is one past the last line, append the line */
14589 ++added;
14590 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14591 rettv->vval.v_number = 0; /* OK */
14592 }
14593
14594 if (l == NULL) /* only one string argument */
14595 break;
14596 ++lnum;
14597 }
14598
14599 if (added > 0)
14600 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601}
14602
14603/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014604 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014605 */
14606/*ARGSUSED*/
14607 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014608set_qf_ll_list(wp, list_arg, action_arg, rettv)
14609 win_T *wp;
14610 typval_T *list_arg;
14611 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014612 typval_T *rettv;
14613{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014614#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014615 char_u *act;
14616 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014617#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014618
Bram Moolenaar2641f772005-03-25 21:58:17 +000014619 rettv->vval.v_number = -1;
14620
14621#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014622 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014623 EMSG(_(e_listreq));
14624 else
14625 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014626 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014627
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014628 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014629 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014630 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014631 if (act == NULL)
14632 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014633 if (*act == 'a' || *act == 'r')
14634 action = *act;
14635 }
14636
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014637 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014638 rettv->vval.v_number = 0;
14639 }
14640#endif
14641}
14642
14643/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014644 * "setloclist()" function
14645 */
14646/*ARGSUSED*/
14647 static void
14648f_setloclist(argvars, rettv)
14649 typval_T *argvars;
14650 typval_T *rettv;
14651{
14652 win_T *win;
14653
14654 rettv->vval.v_number = -1;
14655
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014656 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014657 if (win != NULL)
14658 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14659}
14660
14661/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014662 * "setmatches()" function
14663 */
14664 static void
14665f_setmatches(argvars, rettv)
14666 typval_T *argvars;
14667 typval_T *rettv;
14668{
14669#ifdef FEAT_SEARCH_EXTRA
14670 list_T *l;
14671 listitem_T *li;
14672 dict_T *d;
14673
14674 rettv->vval.v_number = -1;
14675 if (argvars[0].v_type != VAR_LIST)
14676 {
14677 EMSG(_(e_listreq));
14678 return;
14679 }
14680 if ((l = argvars[0].vval.v_list) != NULL)
14681 {
14682
14683 /* To some extent make sure that we are dealing with a list from
14684 * "getmatches()". */
14685 li = l->lv_first;
14686 while (li != NULL)
14687 {
14688 if (li->li_tv.v_type != VAR_DICT
14689 || (d = li->li_tv.vval.v_dict) == NULL)
14690 {
14691 EMSG(_(e_invarg));
14692 return;
14693 }
14694 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14695 && dict_find(d, (char_u *)"pattern", -1) != NULL
14696 && dict_find(d, (char_u *)"priority", -1) != NULL
14697 && dict_find(d, (char_u *)"id", -1) != NULL))
14698 {
14699 EMSG(_(e_invarg));
14700 return;
14701 }
14702 li = li->li_next;
14703 }
14704
14705 clear_matches(curwin);
14706 li = l->lv_first;
14707 while (li != NULL)
14708 {
14709 d = li->li_tv.vval.v_dict;
14710 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14711 get_dict_string(d, (char_u *)"pattern", FALSE),
14712 (int)get_dict_number(d, (char_u *)"priority"),
14713 (int)get_dict_number(d, (char_u *)"id"));
14714 li = li->li_next;
14715 }
14716 rettv->vval.v_number = 0;
14717 }
14718#endif
14719}
14720
14721/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014722 * "setpos()" function
14723 */
14724/*ARGSUSED*/
14725 static void
14726f_setpos(argvars, rettv)
14727 typval_T *argvars;
14728 typval_T *rettv;
14729{
14730 pos_T pos;
14731 int fnum;
14732 char_u *name;
14733
14734 name = get_tv_string_chk(argvars);
14735 if (name != NULL)
14736 {
14737 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14738 {
14739 --pos.col;
14740 if (name[0] == '.') /* cursor */
14741 {
14742 if (fnum == curbuf->b_fnum)
14743 {
14744 curwin->w_cursor = pos;
14745 check_cursor();
14746 }
14747 else
14748 EMSG(_(e_invarg));
14749 }
14750 else if (name[0] == '\'') /* mark */
14751 (void)setmark_pos(name[1], &pos, fnum);
14752 else
14753 EMSG(_(e_invarg));
14754 }
14755 }
14756}
14757
14758/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014759 * "setqflist()" function
14760 */
14761/*ARGSUSED*/
14762 static void
14763f_setqflist(argvars, rettv)
14764 typval_T *argvars;
14765 typval_T *rettv;
14766{
14767 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14768}
14769
14770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 * "setreg()" function
14772 */
14773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014775 typval_T *argvars;
14776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777{
14778 int regname;
14779 char_u *strregname;
14780 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014781 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782 int append;
14783 char_u yank_type;
14784 long block_len;
14785
14786 block_len = -1;
14787 yank_type = MAUTO;
14788 append = FALSE;
14789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014790 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 if (strregname == NULL)
14794 return; /* type error; errmsg already given */
14795 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 if (regname == 0 || regname == '@')
14797 regname = '"';
14798 else if (regname == '=')
14799 return;
14800
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014801 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014803 stropt = get_tv_string_chk(&argvars[2]);
14804 if (stropt == NULL)
14805 return; /* type error */
14806 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014807 switch (*stropt)
14808 {
14809 case 'a': case 'A': /* append */
14810 append = TRUE;
14811 break;
14812 case 'v': case 'c': /* character-wise selection */
14813 yank_type = MCHAR;
14814 break;
14815 case 'V': case 'l': /* line-wise selection */
14816 yank_type = MLINE;
14817 break;
14818#ifdef FEAT_VISUAL
14819 case 'b': case Ctrl_V: /* block-wise selection */
14820 yank_type = MBLOCK;
14821 if (VIM_ISDIGIT(stropt[1]))
14822 {
14823 ++stropt;
14824 block_len = getdigits(&stropt) - 1;
14825 --stropt;
14826 }
14827 break;
14828#endif
14829 }
14830 }
14831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014832 strval = get_tv_string_chk(&argvars[1]);
14833 if (strval != NULL)
14834 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014836 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837}
14838
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014839/*
14840 * "settabwinvar()" function
14841 */
14842 static void
14843f_settabwinvar(argvars, rettv)
14844 typval_T *argvars;
14845 typval_T *rettv;
14846{
14847 setwinvar(argvars, rettv, 1);
14848}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849
14850/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014851 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014854f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014855 typval_T *argvars;
14856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014858 setwinvar(argvars, rettv, 0);
14859}
14860
14861/*
14862 * "setwinvar()" and "settabwinvar()" functions
14863 */
14864 static void
14865setwinvar(argvars, rettv, off)
14866 typval_T *argvars;
14867 typval_T *rettv;
14868 int off;
14869{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 win_T *win;
14871#ifdef FEAT_WINDOWS
14872 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014873 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874#endif
14875 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014876 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014878 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014880 rettv->vval.v_number = 0;
14881
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 if (check_restricted() || check_secure())
14883 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014884
14885#ifdef FEAT_WINDOWS
14886 if (off == 1)
14887 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14888 else
14889 tp = curtab;
14890#endif
14891 win = find_win_by_nr(&argvars[off], tp);
14892 varname = get_tv_string_chk(&argvars[off + 1]);
14893 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894
14895 if (win != NULL && varname != NULL && varp != NULL)
14896 {
14897#ifdef FEAT_WINDOWS
14898 /* set curwin to be our win, temporarily */
14899 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014900 save_curtab = curtab;
14901 goto_tabpage_tp(tp);
14902 if (!win_valid(win))
14903 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 curwin = win;
14905 curbuf = curwin->w_buffer;
14906#endif
14907
14908 if (*varname == '&')
14909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014910 long numval;
14911 char_u *strval;
14912 int error = FALSE;
14913
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014915 numval = get_tv_number_chk(varp, &error);
14916 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014917 if (!error && strval != NULL)
14918 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 }
14920 else
14921 {
14922 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14923 if (winvarname != NULL)
14924 {
14925 STRCPY(winvarname, "w:");
14926 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014927 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 vim_free(winvarname);
14929 }
14930 }
14931
14932#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014933 /* Restore current tabpage and window, if still valid (autocomands can
14934 * make them invalid). */
14935 if (valid_tabpage(save_curtab))
14936 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 if (win_valid(save_curwin))
14938 {
14939 curwin = save_curwin;
14940 curbuf = curwin->w_buffer;
14941 }
14942#endif
14943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944}
14945
14946/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014947 * "shellescape({string})" function
14948 */
14949 static void
14950f_shellescape(argvars, rettv)
14951 typval_T *argvars;
14952 typval_T *rettv;
14953{
14954 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14955 rettv->v_type = VAR_STRING;
14956}
14957
14958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 */
14961 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014963 typval_T *argvars;
14964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014966 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967
Bram Moolenaar0d660222005-01-07 21:51:51 +000014968 p = get_tv_string(&argvars[0]);
14969 rettv->vval.v_string = vim_strsave(p);
14970 simplify_filename(rettv->vval.v_string); /* simplify in place */
14971 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972}
14973
Bram Moolenaar0d660222005-01-07 21:51:51 +000014974static int
14975#ifdef __BORLANDC__
14976 _RTLENTRYF
14977#endif
14978 item_compare __ARGS((const void *s1, const void *s2));
14979static int
14980#ifdef __BORLANDC__
14981 _RTLENTRYF
14982#endif
14983 item_compare2 __ARGS((const void *s1, const void *s2));
14984
14985static int item_compare_ic;
14986static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014987static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988#define ITEM_COMPARE_FAIL 999
14989
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014991 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014993 static int
14994#ifdef __BORLANDC__
14995_RTLENTRYF
14996#endif
14997item_compare(s1, s2)
14998 const void *s1;
14999 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015001 char_u *p1, *p2;
15002 char_u *tofree1, *tofree2;
15003 int res;
15004 char_u numbuf1[NUMBUFLEN];
15005 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015007 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15008 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015009 if (p1 == NULL)
15010 p1 = (char_u *)"";
15011 if (p2 == NULL)
15012 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015013 if (item_compare_ic)
15014 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016 res = STRCMP(p1, p2);
15017 vim_free(tofree1);
15018 vim_free(tofree2);
15019 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020}
15021
15022 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015023#ifdef __BORLANDC__
15024_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015026item_compare2(s1, s2)
15027 const void *s1;
15028 const void *s2;
15029{
15030 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015031 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015032 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015033 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015035 /* shortcut after failure in previous call; compare all items equal */
15036 if (item_compare_func_err)
15037 return 0;
15038
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015039 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15040 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015041 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15042 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043
15044 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015045 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015046 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015047 clear_tv(&argv[0]);
15048 clear_tv(&argv[1]);
15049
15050 if (res == FAIL)
15051 res = ITEM_COMPARE_FAIL;
15052 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015053 /* return value has wrong type */
15054 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15055 if (item_compare_func_err)
15056 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057 clear_tv(&rettv);
15058 return res;
15059}
15060
15061/*
15062 * "sort({list})" function
15063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015066 typval_T *argvars;
15067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068{
Bram Moolenaar33570922005-01-25 22:26:29 +000015069 list_T *l;
15070 listitem_T *li;
15071 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072 long len;
15073 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075 rettv->vval.v_number = 0;
15076 if (argvars[0].v_type != VAR_LIST)
15077 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078 else
15079 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015081 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 return;
15083 rettv->vval.v_list = l;
15084 rettv->v_type = VAR_LIST;
15085 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087 len = list_len(l);
15088 if (len <= 1)
15089 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091 item_compare_ic = FALSE;
15092 item_compare_func = NULL;
15093 if (argvars[1].v_type != VAR_UNKNOWN)
15094 {
15095 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015096 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097 else
15098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015099 int error = FALSE;
15100
15101 i = get_tv_number_chk(&argvars[1], &error);
15102 if (error)
15103 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104 if (i == 1)
15105 item_compare_ic = TRUE;
15106 else
15107 item_compare_func = get_tv_string(&argvars[1]);
15108 }
15109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015112 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113 if (ptrs == NULL)
15114 return;
15115 i = 0;
15116 for (li = l->lv_first; li != NULL; li = li->li_next)
15117 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015119 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015120 /* test the compare function */
15121 if (item_compare_func != NULL
15122 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15123 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015124 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015126 {
15127 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015128 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129 item_compare_func == NULL ? item_compare : item_compare2);
15130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015131 if (!item_compare_func_err)
15132 {
15133 /* Clear the List and append the items in the sorted order. */
15134 l->lv_first = l->lv_last = NULL;
15135 l->lv_len = 0;
15136 for (i = 0; i < len; ++i)
15137 list_append(l, ptrs[i]);
15138 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139 }
15140
15141 vim_free(ptrs);
15142 }
15143}
15144
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015145/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015146 * "soundfold({word})" function
15147 */
15148 static void
15149f_soundfold(argvars, rettv)
15150 typval_T *argvars;
15151 typval_T *rettv;
15152{
15153 char_u *s;
15154
15155 rettv->v_type = VAR_STRING;
15156 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015157#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015158 rettv->vval.v_string = eval_soundfold(s);
15159#else
15160 rettv->vval.v_string = vim_strsave(s);
15161#endif
15162}
15163
15164/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015165 * "spellbadword()" function
15166 */
15167/* ARGSUSED */
15168 static void
15169f_spellbadword(argvars, rettv)
15170 typval_T *argvars;
15171 typval_T *rettv;
15172{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015173 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015174 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015175 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015176
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015177 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015178 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015179
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015180#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015181 if (argvars[0].v_type == VAR_UNKNOWN)
15182 {
15183 /* Find the start and length of the badly spelled word. */
15184 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15185 if (len != 0)
15186 word = ml_get_cursor();
15187 }
15188 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15189 {
15190 char_u *str = get_tv_string_chk(&argvars[0]);
15191 int capcol = -1;
15192
15193 if (str != NULL)
15194 {
15195 /* Check the argument for spelling. */
15196 while (*str != NUL)
15197 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015198 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015199 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015200 {
15201 word = str;
15202 break;
15203 }
15204 str += len;
15205 }
15206 }
15207 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015208#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015209
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015210 list_append_string(rettv->vval.v_list, word, len);
15211 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015212 attr == HLF_SPB ? "bad" :
15213 attr == HLF_SPR ? "rare" :
15214 attr == HLF_SPL ? "local" :
15215 attr == HLF_SPC ? "caps" :
15216 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015217}
15218
15219/*
15220 * "spellsuggest()" function
15221 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015222/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015223 static void
15224f_spellsuggest(argvars, rettv)
15225 typval_T *argvars;
15226 typval_T *rettv;
15227{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015228#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015229 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015230 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015231 int maxcount;
15232 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015233 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015234 listitem_T *li;
15235 int need_capital = FALSE;
15236#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015237
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015238 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015239 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015240
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015241#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015242 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15243 {
15244 str = get_tv_string(&argvars[0]);
15245 if (argvars[1].v_type != VAR_UNKNOWN)
15246 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015247 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015248 if (maxcount <= 0)
15249 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015250 if (argvars[2].v_type != VAR_UNKNOWN)
15251 {
15252 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15253 if (typeerr)
15254 return;
15255 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015256 }
15257 else
15258 maxcount = 25;
15259
Bram Moolenaar4770d092006-01-12 23:22:24 +000015260 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015261
15262 for (i = 0; i < ga.ga_len; ++i)
15263 {
15264 str = ((char_u **)ga.ga_data)[i];
15265
15266 li = listitem_alloc();
15267 if (li == NULL)
15268 vim_free(str);
15269 else
15270 {
15271 li->li_tv.v_type = VAR_STRING;
15272 li->li_tv.v_lock = 0;
15273 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015274 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015275 }
15276 }
15277 ga_clear(&ga);
15278 }
15279#endif
15280}
15281
Bram Moolenaar0d660222005-01-07 21:51:51 +000015282 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015283f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015284 typval_T *argvars;
15285 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286{
15287 char_u *str;
15288 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015289 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290 regmatch_T regmatch;
15291 char_u patbuf[NUMBUFLEN];
15292 char_u *save_cpo;
15293 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015294 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015295 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015296 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015297
15298 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15299 save_cpo = p_cpo;
15300 p_cpo = (char_u *)"";
15301
15302 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015303 if (argvars[1].v_type != VAR_UNKNOWN)
15304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015305 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15306 if (pat == NULL)
15307 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015308 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015310 }
15311 if (pat == NULL || *pat == NUL)
15312 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015313
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015314 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316 if (typeerr)
15317 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318
Bram Moolenaar0d660222005-01-07 21:51:51 +000015319 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15320 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015323 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015324 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015325 if (*str == NUL)
15326 match = FALSE; /* empty item at the end */
15327 else
15328 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329 if (match)
15330 end = regmatch.startp[0];
15331 else
15332 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015333 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15334 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015335 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015336 if (list_append_string(rettv->vval.v_list, str,
15337 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015338 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339 }
15340 if (!match)
15341 break;
15342 /* Advance to just after the match. */
15343 if (regmatch.endp[0] > str)
15344 col = 0;
15345 else
15346 {
15347 /* Don't get stuck at the same match. */
15348#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015349 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015350#else
15351 col = 1;
15352#endif
15353 }
15354 str = regmatch.endp[0];
15355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356
Bram Moolenaar0d660222005-01-07 21:51:51 +000015357 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359
Bram Moolenaar0d660222005-01-07 21:51:51 +000015360 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361}
15362
Bram Moolenaar2c932302006-03-18 21:42:09 +000015363/*
15364 * "str2nr()" function
15365 */
15366 static void
15367f_str2nr(argvars, rettv)
15368 typval_T *argvars;
15369 typval_T *rettv;
15370{
15371 int base = 10;
15372 char_u *p;
15373 long n;
15374
15375 if (argvars[1].v_type != VAR_UNKNOWN)
15376 {
15377 base = get_tv_number(&argvars[1]);
15378 if (base != 8 && base != 10 && base != 16)
15379 {
15380 EMSG(_(e_invarg));
15381 return;
15382 }
15383 }
15384
15385 p = skipwhite(get_tv_string(&argvars[0]));
15386 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15387 rettv->vval.v_number = n;
15388}
15389
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390#ifdef HAVE_STRFTIME
15391/*
15392 * "strftime({format}[, {time}])" function
15393 */
15394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015396 typval_T *argvars;
15397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398{
15399 char_u result_buf[256];
15400 struct tm *curtime;
15401 time_t seconds;
15402 char_u *p;
15403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015407 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 seconds = time(NULL);
15409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015410 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 curtime = localtime(&seconds);
15412 /* MSVC returns NULL for an invalid value of seconds. */
15413 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015414 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 else
15416 {
15417# ifdef FEAT_MBYTE
15418 vimconv_T conv;
15419 char_u *enc;
15420
15421 conv.vc_type = CONV_NONE;
15422 enc = enc_locale();
15423 convert_setup(&conv, p_enc, enc);
15424 if (conv.vc_type != CONV_NONE)
15425 p = string_convert(&conv, p, NULL);
15426# endif
15427 if (p != NULL)
15428 (void)strftime((char *)result_buf, sizeof(result_buf),
15429 (char *)p, curtime);
15430 else
15431 result_buf[0] = NUL;
15432
15433# ifdef FEAT_MBYTE
15434 if (conv.vc_type != CONV_NONE)
15435 vim_free(p);
15436 convert_setup(&conv, enc, p_enc);
15437 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 else
15440# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442
15443# ifdef FEAT_MBYTE
15444 /* Release conversion descriptors */
15445 convert_setup(&conv, NULL, NULL);
15446 vim_free(enc);
15447# endif
15448 }
15449}
15450#endif
15451
15452/*
15453 * "stridx()" function
15454 */
15455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015456f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015457 typval_T *argvars;
15458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459{
15460 char_u buf[NUMBUFLEN];
15461 char_u *needle;
15462 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015463 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015467 needle = get_tv_string_chk(&argvars[1]);
15468 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015469 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015470 if (needle == NULL || haystack == NULL)
15471 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472
Bram Moolenaar33570922005-01-25 22:26:29 +000015473 if (argvars[2].v_type != VAR_UNKNOWN)
15474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015475 int error = FALSE;
15476
15477 start_idx = get_tv_number_chk(&argvars[2], &error);
15478 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015479 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015480 if (start_idx >= 0)
15481 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015482 }
15483
15484 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15485 if (pos != NULL)
15486 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487}
15488
15489/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015490 * "string()" function
15491 */
15492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015494 typval_T *argvars;
15495 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015496{
15497 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015498 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015500 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015501 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015502 /* Make a copy if we have a value but it's not in allocate memory. */
15503 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015504 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505}
15506
15507/*
15508 * "strlen()" function
15509 */
15510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015512 typval_T *argvars;
15513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015515 rettv->vval.v_number = (varnumber_T)(STRLEN(
15516 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517}
15518
15519/*
15520 * "strpart()" function
15521 */
15522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015523f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015524 typval_T *argvars;
15525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526{
15527 char_u *p;
15528 int n;
15529 int len;
15530 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015531 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015533 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 slen = (int)STRLEN(p);
15535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015536 n = get_tv_number_chk(&argvars[1], &error);
15537 if (error)
15538 len = 0;
15539 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015540 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 else
15542 len = slen - n; /* default len: all bytes that are available. */
15543
15544 /*
15545 * Only return the overlap between the specified part and the actual
15546 * string.
15547 */
15548 if (n < 0)
15549 {
15550 len += n;
15551 n = 0;
15552 }
15553 else if (n > slen)
15554 n = slen;
15555 if (len < 0)
15556 len = 0;
15557 else if (n + len > slen)
15558 len = slen - n;
15559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->v_type = VAR_STRING;
15561 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562}
15563
15564/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015565 * "strridx()" function
15566 */
15567 static void
15568f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015569 typval_T *argvars;
15570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571{
15572 char_u buf[NUMBUFLEN];
15573 char_u *needle;
15574 char_u *haystack;
15575 char_u *rest;
15576 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015577 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015579 needle = get_tv_string_chk(&argvars[1]);
15580 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015581
15582 rettv->vval.v_number = -1;
15583 if (needle == NULL || haystack == NULL)
15584 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015585
15586 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015587 if (argvars[2].v_type != VAR_UNKNOWN)
15588 {
15589 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015590 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015591 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015592 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015593 }
15594 else
15595 end_idx = haystack_len;
15596
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015598 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015599 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015600 lastmatch = haystack + end_idx;
15601 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015603 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604 for (rest = haystack; *rest != '\0'; ++rest)
15605 {
15606 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015607 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015608 break;
15609 lastmatch = rest;
15610 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015611 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015612
15613 if (lastmatch == NULL)
15614 rettv->vval.v_number = -1;
15615 else
15616 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15617}
15618
15619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 * "strtrans()" function
15621 */
15622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015623f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015624 typval_T *argvars;
15625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015627 rettv->v_type = VAR_STRING;
15628 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629}
15630
15631/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015632 * "submatch()" function
15633 */
15634 static void
15635f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015636 typval_T *argvars;
15637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015638{
15639 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015640 rettv->vval.v_string =
15641 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015642}
15643
15644/*
15645 * "substitute()" function
15646 */
15647 static void
15648f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015649 typval_T *argvars;
15650 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015651{
15652 char_u patbuf[NUMBUFLEN];
15653 char_u subbuf[NUMBUFLEN];
15654 char_u flagsbuf[NUMBUFLEN];
15655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015656 char_u *str = get_tv_string_chk(&argvars[0]);
15657 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15658 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15659 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15660
Bram Moolenaar0d660222005-01-07 21:51:51 +000015661 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015662 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15663 rettv->vval.v_string = NULL;
15664 else
15665 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015666}
15667
15668/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015669 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 */
15671/*ARGSUSED*/
15672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015674 typval_T *argvars;
15675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676{
15677 int id = 0;
15678#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015679 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 long col;
15681 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015682 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015684 lnum = get_tv_lnum(argvars); /* -1 on type error */
15685 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15686 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015688 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015689 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015690 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691#endif
15692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015693 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694}
15695
15696/*
15697 * "synIDattr(id, what [, mode])" function
15698 */
15699/*ARGSUSED*/
15700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015701f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015702 typval_T *argvars;
15703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704{
15705 char_u *p = NULL;
15706#ifdef FEAT_SYN_HL
15707 int id;
15708 char_u *what;
15709 char_u *mode;
15710 char_u modebuf[NUMBUFLEN];
15711 int modec;
15712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015713 id = get_tv_number(&argvars[0]);
15714 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015715 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015717 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 modec = TOLOWER_ASC(mode[0]);
15719 if (modec != 't' && modec != 'c'
15720#ifdef FEAT_GUI
15721 && modec != 'g'
15722#endif
15723 )
15724 modec = 0; /* replace invalid with current */
15725 }
15726 else
15727 {
15728#ifdef FEAT_GUI
15729 if (gui.in_use)
15730 modec = 'g';
15731 else
15732#endif
15733 if (t_colors > 1)
15734 modec = 'c';
15735 else
15736 modec = 't';
15737 }
15738
15739
15740 switch (TOLOWER_ASC(what[0]))
15741 {
15742 case 'b':
15743 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15744 p = highlight_color(id, what, modec);
15745 else /* bold */
15746 p = highlight_has_attr(id, HL_BOLD, modec);
15747 break;
15748
15749 case 'f': /* fg[#] */
15750 p = highlight_color(id, what, modec);
15751 break;
15752
15753 case 'i':
15754 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15755 p = highlight_has_attr(id, HL_INVERSE, modec);
15756 else /* italic */
15757 p = highlight_has_attr(id, HL_ITALIC, modec);
15758 break;
15759
15760 case 'n': /* name */
15761 p = get_highlight_name(NULL, id - 1);
15762 break;
15763
15764 case 'r': /* reverse */
15765 p = highlight_has_attr(id, HL_INVERSE, modec);
15766 break;
15767
15768 case 's': /* standout */
15769 p = highlight_has_attr(id, HL_STANDOUT, modec);
15770 break;
15771
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015772 case 'u':
15773 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15774 /* underline */
15775 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15776 else
15777 /* undercurl */
15778 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 break;
15780 }
15781
15782 if (p != NULL)
15783 p = vim_strsave(p);
15784#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785 rettv->v_type = VAR_STRING;
15786 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787}
15788
15789/*
15790 * "synIDtrans(id)" function
15791 */
15792/*ARGSUSED*/
15793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015794f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015795 typval_T *argvars;
15796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797{
15798 int id;
15799
15800#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015801 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802
15803 if (id > 0)
15804 id = syn_get_final_id(id);
15805 else
15806#endif
15807 id = 0;
15808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015809 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810}
15811
15812/*
15813 * "system()" function
15814 */
15815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015816f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015817 typval_T *argvars;
15818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015820 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015822 char_u *infile = NULL;
15823 char_u buf[NUMBUFLEN];
15824 int err = FALSE;
15825 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015827 if (check_restricted() || check_secure())
15828 return;
15829
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015830 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015831 {
15832 /*
15833 * Write the string to a temp file, to be used for input of the shell
15834 * command.
15835 */
15836 if ((infile = vim_tempname('i')) == NULL)
15837 {
15838 EMSG(_(e_notmp));
15839 return;
15840 }
15841
15842 fd = mch_fopen((char *)infile, WRITEBIN);
15843 if (fd == NULL)
15844 {
15845 EMSG2(_(e_notopen), infile);
15846 goto done;
15847 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015848 p = get_tv_string_buf_chk(&argvars[1], buf);
15849 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015850 {
15851 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015852 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015853 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015854 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15855 err = TRUE;
15856 if (fclose(fd) != 0)
15857 err = TRUE;
15858 if (err)
15859 {
15860 EMSG(_("E677: Error writing temp file"));
15861 goto done;
15862 }
15863 }
15864
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015865 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15866 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015867
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868#ifdef USE_CR
15869 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015870 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 {
15872 char_u *s;
15873
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015874 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875 {
15876 if (*s == CAR)
15877 *s = NL;
15878 }
15879 }
15880#else
15881# ifdef USE_CRNL
15882 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015883 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 {
15885 char_u *s, *d;
15886
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015887 d = res;
15888 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 {
15890 if (s[0] == CAR && s[1] == NL)
15891 ++s;
15892 *d++ = *s;
15893 }
15894 *d = NUL;
15895 }
15896# endif
15897#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015898
15899done:
15900 if (infile != NULL)
15901 {
15902 mch_remove(infile);
15903 vim_free(infile);
15904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015905 rettv->v_type = VAR_STRING;
15906 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907}
15908
15909/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015910 * "tabpagebuflist()" function
15911 */
15912/* ARGSUSED */
15913 static void
15914f_tabpagebuflist(argvars, rettv)
15915 typval_T *argvars;
15916 typval_T *rettv;
15917{
15918#ifndef FEAT_WINDOWS
15919 rettv->vval.v_number = 0;
15920#else
15921 tabpage_T *tp;
15922 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015923
15924 if (argvars[0].v_type == VAR_UNKNOWN)
15925 wp = firstwin;
15926 else
15927 {
15928 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15929 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015930 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015931 }
15932 if (wp == NULL)
15933 rettv->vval.v_number = 0;
15934 else
15935 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015936 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015937 rettv->vval.v_number = 0;
15938 else
15939 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015940 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015941 if (list_append_number(rettv->vval.v_list,
15942 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015943 break;
15944 }
15945 }
15946#endif
15947}
15948
15949
15950/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015951 * "tabpagenr()" function
15952 */
15953/* ARGSUSED */
15954 static void
15955f_tabpagenr(argvars, rettv)
15956 typval_T *argvars;
15957 typval_T *rettv;
15958{
15959 int nr = 1;
15960#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015961 char_u *arg;
15962
15963 if (argvars[0].v_type != VAR_UNKNOWN)
15964 {
15965 arg = get_tv_string_chk(&argvars[0]);
15966 nr = 0;
15967 if (arg != NULL)
15968 {
15969 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015970 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015971 else
15972 EMSG2(_(e_invexpr2), arg);
15973 }
15974 }
15975 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015976 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015977#endif
15978 rettv->vval.v_number = nr;
15979}
15980
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015981
15982#ifdef FEAT_WINDOWS
15983static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15984
15985/*
15986 * Common code for tabpagewinnr() and winnr().
15987 */
15988 static int
15989get_winnr(tp, argvar)
15990 tabpage_T *tp;
15991 typval_T *argvar;
15992{
15993 win_T *twin;
15994 int nr = 1;
15995 win_T *wp;
15996 char_u *arg;
15997
15998 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15999 if (argvar->v_type != VAR_UNKNOWN)
16000 {
16001 arg = get_tv_string_chk(argvar);
16002 if (arg == NULL)
16003 nr = 0; /* type error; errmsg already given */
16004 else if (STRCMP(arg, "$") == 0)
16005 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16006 else if (STRCMP(arg, "#") == 0)
16007 {
16008 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16009 if (twin == NULL)
16010 nr = 0;
16011 }
16012 else
16013 {
16014 EMSG2(_(e_invexpr2), arg);
16015 nr = 0;
16016 }
16017 }
16018
16019 if (nr > 0)
16020 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16021 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016022 {
16023 if (wp == NULL)
16024 {
16025 /* didn't find it in this tabpage */
16026 nr = 0;
16027 break;
16028 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016029 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016030 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016031 return nr;
16032}
16033#endif
16034
16035/*
16036 * "tabpagewinnr()" function
16037 */
16038/* ARGSUSED */
16039 static void
16040f_tabpagewinnr(argvars, rettv)
16041 typval_T *argvars;
16042 typval_T *rettv;
16043{
16044 int nr = 1;
16045#ifdef FEAT_WINDOWS
16046 tabpage_T *tp;
16047
16048 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16049 if (tp == NULL)
16050 nr = 0;
16051 else
16052 nr = get_winnr(tp, &argvars[1]);
16053#endif
16054 rettv->vval.v_number = nr;
16055}
16056
16057
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016058/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016059 * "tagfiles()" function
16060 */
16061/*ARGSUSED*/
16062 static void
16063f_tagfiles(argvars, rettv)
16064 typval_T *argvars;
16065 typval_T *rettv;
16066{
16067 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016068 tagname_T tn;
16069 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016070
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016071 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016072 {
16073 rettv->vval.v_number = 0;
16074 return;
16075 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016076
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016077 for (first = TRUE; ; first = FALSE)
16078 if (get_tagfname(&tn, first, fname) == FAIL
16079 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016080 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016081 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016082}
16083
16084/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016085 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016086 */
16087 static void
16088f_taglist(argvars, rettv)
16089 typval_T *argvars;
16090 typval_T *rettv;
16091{
16092 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016093
16094 tag_pattern = get_tv_string(&argvars[0]);
16095
16096 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016097 if (*tag_pattern == NUL)
16098 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016099
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016100 if (rettv_list_alloc(rettv) == OK)
16101 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016102}
16103
16104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 * "tempname()" function
16106 */
16107/*ARGSUSED*/
16108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016109f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016110 typval_T *argvars;
16111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112{
16113 static int x = 'A';
16114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016115 rettv->v_type = VAR_STRING;
16116 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117
16118 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16119 * names. Skip 'I' and 'O', they are used for shell redirection. */
16120 do
16121 {
16122 if (x == 'Z')
16123 x = '0';
16124 else if (x == '9')
16125 x = 'A';
16126 else
16127 {
16128#ifdef EBCDIC
16129 if (x == 'I')
16130 x = 'J';
16131 else if (x == 'R')
16132 x = 'S';
16133 else
16134#endif
16135 ++x;
16136 }
16137 } while (x == 'I' || x == 'O');
16138}
16139
16140/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016141 * "test(list)" function: Just checking the walls...
16142 */
16143/*ARGSUSED*/
16144 static void
16145f_test(argvars, rettv)
16146 typval_T *argvars;
16147 typval_T *rettv;
16148{
16149 /* Used for unit testing. Change the code below to your liking. */
16150#if 0
16151 listitem_T *li;
16152 list_T *l;
16153 char_u *bad, *good;
16154
16155 if (argvars[0].v_type != VAR_LIST)
16156 return;
16157 l = argvars[0].vval.v_list;
16158 if (l == NULL)
16159 return;
16160 li = l->lv_first;
16161 if (li == NULL)
16162 return;
16163 bad = get_tv_string(&li->li_tv);
16164 li = li->li_next;
16165 if (li == NULL)
16166 return;
16167 good = get_tv_string(&li->li_tv);
16168 rettv->vval.v_number = test_edit_score(bad, good);
16169#endif
16170}
16171
16172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 * "tolower(string)" function
16174 */
16175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016176f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016177 typval_T *argvars;
16178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179{
16180 char_u *p;
16181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182 p = vim_strsave(get_tv_string(&argvars[0]));
16183 rettv->v_type = VAR_STRING;
16184 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185
16186 if (p != NULL)
16187 while (*p != NUL)
16188 {
16189#ifdef FEAT_MBYTE
16190 int l;
16191
16192 if (enc_utf8)
16193 {
16194 int c, lc;
16195
16196 c = utf_ptr2char(p);
16197 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016198 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 /* TODO: reallocate string when byte count changes. */
16200 if (utf_char2len(lc) == l)
16201 utf_char2bytes(lc, p);
16202 p += l;
16203 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016204 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 p += l; /* skip multi-byte character */
16206 else
16207#endif
16208 {
16209 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16210 ++p;
16211 }
16212 }
16213}
16214
16215/*
16216 * "toupper(string)" function
16217 */
16218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016219f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016220 typval_T *argvars;
16221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016223 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016224 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225}
16226
16227/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016228 * "tr(string, fromstr, tostr)" function
16229 */
16230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016231f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016232 typval_T *argvars;
16233 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016234{
16235 char_u *instr;
16236 char_u *fromstr;
16237 char_u *tostr;
16238 char_u *p;
16239#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016240 int inlen;
16241 int fromlen;
16242 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016243 int idx;
16244 char_u *cpstr;
16245 int cplen;
16246 int first = TRUE;
16247#endif
16248 char_u buf[NUMBUFLEN];
16249 char_u buf2[NUMBUFLEN];
16250 garray_T ga;
16251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016252 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016253 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16254 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016255
16256 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257 rettv->v_type = VAR_STRING;
16258 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 if (fromstr == NULL || tostr == NULL)
16260 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016261 ga_init2(&ga, (int)sizeof(char), 80);
16262
16263#ifdef FEAT_MBYTE
16264 if (!has_mbyte)
16265#endif
16266 /* not multi-byte: fromstr and tostr must be the same length */
16267 if (STRLEN(fromstr) != STRLEN(tostr))
16268 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016269#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016270error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016271#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016272 EMSG2(_(e_invarg2), fromstr);
16273 ga_clear(&ga);
16274 return;
16275 }
16276
16277 /* fromstr and tostr have to contain the same number of chars */
16278 while (*instr != NUL)
16279 {
16280#ifdef FEAT_MBYTE
16281 if (has_mbyte)
16282 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016283 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016284 cpstr = instr;
16285 cplen = inlen;
16286 idx = 0;
16287 for (p = fromstr; *p != NUL; p += fromlen)
16288 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016289 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016290 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16291 {
16292 for (p = tostr; *p != NUL; p += tolen)
16293 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016294 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016295 if (idx-- == 0)
16296 {
16297 cplen = tolen;
16298 cpstr = p;
16299 break;
16300 }
16301 }
16302 if (*p == NUL) /* tostr is shorter than fromstr */
16303 goto error;
16304 break;
16305 }
16306 ++idx;
16307 }
16308
16309 if (first && cpstr == instr)
16310 {
16311 /* Check that fromstr and tostr have the same number of
16312 * (multi-byte) characters. Done only once when a character
16313 * of instr doesn't appear in fromstr. */
16314 first = FALSE;
16315 for (p = tostr; *p != NUL; p += tolen)
16316 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016317 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016318 --idx;
16319 }
16320 if (idx != 0)
16321 goto error;
16322 }
16323
16324 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016325 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016326 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016327
16328 instr += inlen;
16329 }
16330 else
16331#endif
16332 {
16333 /* When not using multi-byte chars we can do it faster. */
16334 p = vim_strchr(fromstr, *instr);
16335 if (p != NULL)
16336 ga_append(&ga, tostr[p - fromstr]);
16337 else
16338 ga_append(&ga, *instr);
16339 ++instr;
16340 }
16341 }
16342
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016343 /* add a terminating NUL */
16344 ga_grow(&ga, 1);
16345 ga_append(&ga, NUL);
16346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016347 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016348}
16349
16350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351 * "type(expr)" function
16352 */
16353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016354f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016355 typval_T *argvars;
16356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016358 int n;
16359
16360 switch (argvars[0].v_type)
16361 {
16362 case VAR_NUMBER: n = 0; break;
16363 case VAR_STRING: n = 1; break;
16364 case VAR_FUNC: n = 2; break;
16365 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016366 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016367 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16368 }
16369 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370}
16371
16372/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016373 * "values(dict)" function
16374 */
16375 static void
16376f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016377 typval_T *argvars;
16378 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016379{
16380 dict_list(argvars, rettv, 1);
16381}
16382
16383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384 * "virtcol(string)" function
16385 */
16386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016387f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016388 typval_T *argvars;
16389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390{
16391 colnr_T vcol = 0;
16392 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016393 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016395 fp = var2fpos(&argvars[0], FALSE, &fnum);
16396 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16397 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 {
16399 getvvcol(curwin, fp, NULL, NULL, &vcol);
16400 ++vcol;
16401 }
16402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016403 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404}
16405
16406/*
16407 * "visualmode()" function
16408 */
16409/*ARGSUSED*/
16410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016411f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016412 typval_T *argvars;
16413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414{
16415#ifdef FEAT_VISUAL
16416 char_u str[2];
16417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016418 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 str[0] = curbuf->b_visual_mode_eval;
16420 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016421 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422
16423 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016424 if ((argvars[0].v_type == VAR_NUMBER
16425 && argvars[0].vval.v_number != 0)
16426 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016427 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 curbuf->b_visual_mode_eval = NUL;
16429#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016430 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431#endif
16432}
16433
16434/*
16435 * "winbufnr(nr)" function
16436 */
16437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016438f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016439 typval_T *argvars;
16440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441{
16442 win_T *wp;
16443
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016444 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016446 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016448 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449}
16450
16451/*
16452 * "wincol()" function
16453 */
16454/*ARGSUSED*/
16455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016456f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016457 typval_T *argvars;
16458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459{
16460 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016461 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462}
16463
16464/*
16465 * "winheight(nr)" function
16466 */
16467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016468f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016469 typval_T *argvars;
16470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471{
16472 win_T *wp;
16473
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016474 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016478 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479}
16480
16481/*
16482 * "winline()" function
16483 */
16484/*ARGSUSED*/
16485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016486f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016487 typval_T *argvars;
16488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489{
16490 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016491 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492}
16493
16494/*
16495 * "winnr()" function
16496 */
16497/* ARGSUSED */
16498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016499f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016500 typval_T *argvars;
16501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502{
16503 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016504
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016506 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016508 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509}
16510
16511/*
16512 * "winrestcmd()" function
16513 */
16514/* ARGSUSED */
16515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016516f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016517 typval_T *argvars;
16518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519{
16520#ifdef FEAT_WINDOWS
16521 win_T *wp;
16522 int winnr = 1;
16523 garray_T ga;
16524 char_u buf[50];
16525
16526 ga_init2(&ga, (int)sizeof(char), 70);
16527 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16528 {
16529 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16530 ga_concat(&ga, buf);
16531# ifdef FEAT_VERTSPLIT
16532 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16533 ga_concat(&ga, buf);
16534# endif
16535 ++winnr;
16536 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016537 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016539 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016543 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544}
16545
16546/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016547 * "winrestview()" function
16548 */
16549/* ARGSUSED */
16550 static void
16551f_winrestview(argvars, rettv)
16552 typval_T *argvars;
16553 typval_T *rettv;
16554{
16555 dict_T *dict;
16556
16557 if (argvars[0].v_type != VAR_DICT
16558 || (dict = argvars[0].vval.v_dict) == NULL)
16559 EMSG(_(e_invarg));
16560 else
16561 {
16562 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16563 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16564#ifdef FEAT_VIRTUALEDIT
16565 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16566#endif
16567 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016568 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016569
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016570 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016571#ifdef FEAT_DIFF
16572 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16573#endif
16574 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16575 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16576
16577 check_cursor();
16578 changed_cline_bef_curs();
16579 invalidate_botline();
16580 redraw_later(VALID);
16581
16582 if (curwin->w_topline == 0)
16583 curwin->w_topline = 1;
16584 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16585 curwin->w_topline = curbuf->b_ml.ml_line_count;
16586#ifdef FEAT_DIFF
16587 check_topfill(curwin, TRUE);
16588#endif
16589 }
16590}
16591
16592/*
16593 * "winsaveview()" function
16594 */
16595/* ARGSUSED */
16596 static void
16597f_winsaveview(argvars, rettv)
16598 typval_T *argvars;
16599 typval_T *rettv;
16600{
16601 dict_T *dict;
16602
16603 dict = dict_alloc();
16604 if (dict == NULL)
16605 return;
16606 rettv->v_type = VAR_DICT;
16607 rettv->vval.v_dict = dict;
16608 ++dict->dv_refcount;
16609
16610 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16611 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16612#ifdef FEAT_VIRTUALEDIT
16613 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16614#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016615 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016616 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16617
16618 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16619#ifdef FEAT_DIFF
16620 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16621#endif
16622 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16623 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16624}
16625
16626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 * "winwidth(nr)" function
16628 */
16629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016630f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016631 typval_T *argvars;
16632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633{
16634 win_T *wp;
16635
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016636 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 else
16640#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016641 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016643 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644#endif
16645}
16646
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016648 * "writefile()" function
16649 */
16650 static void
16651f_writefile(argvars, rettv)
16652 typval_T *argvars;
16653 typval_T *rettv;
16654{
16655 int binary = FALSE;
16656 char_u *fname;
16657 FILE *fd;
16658 listitem_T *li;
16659 char_u *s;
16660 int ret = 0;
16661 int c;
16662
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016663 if (check_restricted() || check_secure())
16664 return;
16665
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016666 if (argvars[0].v_type != VAR_LIST)
16667 {
16668 EMSG2(_(e_listarg), "writefile()");
16669 return;
16670 }
16671 if (argvars[0].vval.v_list == NULL)
16672 return;
16673
16674 if (argvars[2].v_type != VAR_UNKNOWN
16675 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16676 binary = TRUE;
16677
16678 /* Always open the file in binary mode, library functions have a mind of
16679 * their own about CR-LF conversion. */
16680 fname = get_tv_string(&argvars[1]);
16681 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16682 {
16683 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16684 ret = -1;
16685 }
16686 else
16687 {
16688 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16689 li = li->li_next)
16690 {
16691 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16692 {
16693 if (*s == '\n')
16694 c = putc(NUL, fd);
16695 else
16696 c = putc(*s, fd);
16697 if (c == EOF)
16698 {
16699 ret = -1;
16700 break;
16701 }
16702 }
16703 if (!binary || li->li_next != NULL)
16704 if (putc('\n', fd) == EOF)
16705 {
16706 ret = -1;
16707 break;
16708 }
16709 if (ret < 0)
16710 {
16711 EMSG(_(e_write));
16712 break;
16713 }
16714 }
16715 fclose(fd);
16716 }
16717
16718 rettv->vval.v_number = ret;
16719}
16720
16721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016723 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 */
16725 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016726var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016727 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016728 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016729 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016731 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016733 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734
Bram Moolenaara5525202006-03-02 22:52:09 +000016735 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016736 if (varp->v_type == VAR_LIST)
16737 {
16738 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016739 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016740 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016741 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016742
16743 l = varp->vval.v_list;
16744 if (l == NULL)
16745 return NULL;
16746
16747 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016748 pos.lnum = list_find_nr(l, 0L, &error);
16749 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016750 return NULL; /* invalid line number */
16751
16752 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016753 pos.col = list_find_nr(l, 1L, &error);
16754 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016755 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016756 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016757
16758 /* We accept "$" for the column number: last column. */
16759 li = list_find(l, 1L);
16760 if (li != NULL && li->li_tv.v_type == VAR_STRING
16761 && li->li_tv.vval.v_string != NULL
16762 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16763 pos.col = len + 1;
16764
Bram Moolenaara5525202006-03-02 22:52:09 +000016765 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016766 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016767 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016768 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016769
Bram Moolenaara5525202006-03-02 22:52:09 +000016770#ifdef FEAT_VIRTUALEDIT
16771 /* Get the virtual offset. Defaults to zero. */
16772 pos.coladd = list_find_nr(l, 2L, &error);
16773 if (error)
16774 pos.coladd = 0;
16775#endif
16776
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016777 return &pos;
16778 }
16779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016780 name = get_tv_string_chk(varp);
16781 if (name == NULL)
16782 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 if (name[0] == '.') /* cursor */
16784 return &curwin->w_cursor;
16785 if (name[0] == '\'') /* mark */
16786 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016787 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16789 return NULL;
16790 return pp;
16791 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016792
16793#ifdef FEAT_VIRTUALEDIT
16794 pos.coladd = 0;
16795#endif
16796
Bram Moolenaar477933c2007-07-17 14:32:23 +000016797 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016798 {
16799 pos.col = 0;
16800 if (name[1] == '0') /* "w0": first visible line */
16801 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016802 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016803 pos.lnum = curwin->w_topline;
16804 return &pos;
16805 }
16806 else if (name[1] == '$') /* "w$": last visible line */
16807 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016808 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016809 pos.lnum = curwin->w_botline - 1;
16810 return &pos;
16811 }
16812 }
16813 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016815 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 {
16817 pos.lnum = curbuf->b_ml.ml_line_count;
16818 pos.col = 0;
16819 }
16820 else
16821 {
16822 pos.lnum = curwin->w_cursor.lnum;
16823 pos.col = (colnr_T)STRLEN(ml_get_curline());
16824 }
16825 return &pos;
16826 }
16827 return NULL;
16828}
16829
16830/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016831 * Convert list in "arg" into a position and optional file number.
16832 * When "fnump" is NULL there is no file number, only 3 items.
16833 * Note that the column is passed on as-is, the caller may want to decrement
16834 * it to use 1 for the first column.
16835 * Return FAIL when conversion is not possible, doesn't check the position for
16836 * validity.
16837 */
16838 static int
16839list2fpos(arg, posp, fnump)
16840 typval_T *arg;
16841 pos_T *posp;
16842 int *fnump;
16843{
16844 list_T *l = arg->vval.v_list;
16845 long i = 0;
16846 long n;
16847
Bram Moolenaarbde35262006-07-23 20:12:24 +000016848 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16849 * when "fnump" isn't NULL and "coladd" is optional. */
16850 if (arg->v_type != VAR_LIST
16851 || l == NULL
16852 || l->lv_len < (fnump == NULL ? 2 : 3)
16853 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016854 return FAIL;
16855
16856 if (fnump != NULL)
16857 {
16858 n = list_find_nr(l, i++, NULL); /* fnum */
16859 if (n < 0)
16860 return FAIL;
16861 if (n == 0)
16862 n = curbuf->b_fnum; /* current buffer */
16863 *fnump = n;
16864 }
16865
16866 n = list_find_nr(l, i++, NULL); /* lnum */
16867 if (n < 0)
16868 return FAIL;
16869 posp->lnum = n;
16870
16871 n = list_find_nr(l, i++, NULL); /* col */
16872 if (n < 0)
16873 return FAIL;
16874 posp->col = n;
16875
16876#ifdef FEAT_VIRTUALEDIT
16877 n = list_find_nr(l, i, NULL);
16878 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016879 posp->coladd = 0;
16880 else
16881 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016882#endif
16883
16884 return OK;
16885}
16886
16887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 * Get the length of an environment variable name.
16889 * Advance "arg" to the first character after the name.
16890 * Return 0 for error.
16891 */
16892 static int
16893get_env_len(arg)
16894 char_u **arg;
16895{
16896 char_u *p;
16897 int len;
16898
16899 for (p = *arg; vim_isIDc(*p); ++p)
16900 ;
16901 if (p == *arg) /* no name found */
16902 return 0;
16903
16904 len = (int)(p - *arg);
16905 *arg = p;
16906 return len;
16907}
16908
16909/*
16910 * Get the length of the name of a function or internal variable.
16911 * "arg" is advanced to the first non-white character after the name.
16912 * Return 0 if something is wrong.
16913 */
16914 static int
16915get_id_len(arg)
16916 char_u **arg;
16917{
16918 char_u *p;
16919 int len;
16920
16921 /* Find the end of the name. */
16922 for (p = *arg; eval_isnamec(*p); ++p)
16923 ;
16924 if (p == *arg) /* no name found */
16925 return 0;
16926
16927 len = (int)(p - *arg);
16928 *arg = skipwhite(p);
16929
16930 return len;
16931}
16932
16933/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016934 * Get the length of the name of a variable or function.
16935 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016937 * Return -1 if curly braces expansion failed.
16938 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939 * If the name contains 'magic' {}'s, expand them and return the
16940 * expanded name in an allocated string via 'alias' - caller must free.
16941 */
16942 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016943get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 char_u **arg;
16945 char_u **alias;
16946 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016947 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948{
16949 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 char_u *p;
16951 char_u *expr_start;
16952 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953
16954 *alias = NULL; /* default to no alias */
16955
16956 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16957 && (*arg)[2] == (int)KE_SNR)
16958 {
16959 /* hard coded <SNR>, already translated */
16960 *arg += 3;
16961 return get_id_len(arg) + 3;
16962 }
16963 len = eval_fname_script(*arg);
16964 if (len > 0)
16965 {
16966 /* literal "<SID>", "s:" or "<SNR>" */
16967 *arg += len;
16968 }
16969
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016971 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016973 p = find_name_end(*arg, &expr_start, &expr_end,
16974 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975 if (expr_start != NULL)
16976 {
16977 char_u *temp_string;
16978
16979 if (!evaluate)
16980 {
16981 len += (int)(p - *arg);
16982 *arg = skipwhite(p);
16983 return len;
16984 }
16985
16986 /*
16987 * Include any <SID> etc in the expanded string:
16988 * Thus the -len here.
16989 */
16990 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16991 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016992 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 *alias = temp_string;
16994 *arg = skipwhite(p);
16995 return (int)STRLEN(temp_string);
16996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997
16998 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016999 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 EMSG2(_(e_invexpr2), *arg);
17001
17002 return len;
17003}
17004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017005/*
17006 * Find the end of a variable or function name, taking care of magic braces.
17007 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17008 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017009 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017010 * Return a pointer to just after the name. Equal to "arg" if there is no
17011 * valid name.
17012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017014find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 char_u *arg;
17016 char_u **expr_start;
17017 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017018 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017020 int mb_nest = 0;
17021 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 char_u *p;
17023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017024 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026 *expr_start = NULL;
17027 *expr_end = NULL;
17028 }
17029
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017030 /* Quick check for valid starting character. */
17031 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17032 return arg;
17033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 for (p = arg; *p != NUL
17035 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017036 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017037 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017038 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017039 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017040 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017041 if (*p == '\'')
17042 {
17043 /* skip over 'string' to avoid counting [ and ] inside it. */
17044 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17045 ;
17046 if (*p == NUL)
17047 break;
17048 }
17049 else if (*p == '"')
17050 {
17051 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17052 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17053 if (*p == '\\' && p[1] != NUL)
17054 ++p;
17055 if (*p == NUL)
17056 break;
17057 }
17058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017059 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017061 if (*p == '[')
17062 ++br_nest;
17063 else if (*p == ']')
17064 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017067 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017069 if (*p == '{')
17070 {
17071 mb_nest++;
17072 if (expr_start != NULL && *expr_start == NULL)
17073 *expr_start = p;
17074 }
17075 else if (*p == '}')
17076 {
17077 mb_nest--;
17078 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17079 *expr_end = p;
17080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082 }
17083
17084 return p;
17085}
17086
17087/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017088 * Expands out the 'magic' {}'s in a variable/function name.
17089 * Note that this can call itself recursively, to deal with
17090 * constructs like foo{bar}{baz}{bam}
17091 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17092 * "in_start" ^
17093 * "expr_start" ^
17094 * "expr_end" ^
17095 * "in_end" ^
17096 *
17097 * Returns a new allocated string, which the caller must free.
17098 * Returns NULL for failure.
17099 */
17100 static char_u *
17101make_expanded_name(in_start, expr_start, expr_end, in_end)
17102 char_u *in_start;
17103 char_u *expr_start;
17104 char_u *expr_end;
17105 char_u *in_end;
17106{
17107 char_u c1;
17108 char_u *retval = NULL;
17109 char_u *temp_result;
17110 char_u *nextcmd = NULL;
17111
17112 if (expr_end == NULL || in_end == NULL)
17113 return NULL;
17114 *expr_start = NUL;
17115 *expr_end = NUL;
17116 c1 = *in_end;
17117 *in_end = NUL;
17118
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017119 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017120 if (temp_result != NULL && nextcmd == NULL)
17121 {
17122 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17123 + (in_end - expr_end) + 1));
17124 if (retval != NULL)
17125 {
17126 STRCPY(retval, in_start);
17127 STRCAT(retval, temp_result);
17128 STRCAT(retval, expr_end + 1);
17129 }
17130 }
17131 vim_free(temp_result);
17132
17133 *in_end = c1; /* put char back for error messages */
17134 *expr_start = '{';
17135 *expr_end = '}';
17136
17137 if (retval != NULL)
17138 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017139 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017140 if (expr_start != NULL)
17141 {
17142 /* Further expansion! */
17143 temp_result = make_expanded_name(retval, expr_start,
17144 expr_end, temp_result);
17145 vim_free(retval);
17146 retval = temp_result;
17147 }
17148 }
17149
17150 return retval;
17151}
17152
17153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017155 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 */
17157 static int
17158eval_isnamec(c)
17159 int c;
17160{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017161 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17162}
17163
17164/*
17165 * Return TRUE if character "c" can be used as the first character in a
17166 * variable or function name (excluding '{' and '}').
17167 */
17168 static int
17169eval_isnamec1(c)
17170 int c;
17171{
17172 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173}
17174
17175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 * Set number v: variable to "val".
17177 */
17178 void
17179set_vim_var_nr(idx, val)
17180 int idx;
17181 long val;
17182{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017183 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184}
17185
17186/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017187 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 */
17189 long
17190get_vim_var_nr(idx)
17191 int idx;
17192{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017193 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194}
17195
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017196#if defined(FEAT_AUTOCMD) || defined(PROTO)
17197/*
17198 * Get string v: variable value. Uses a static buffer, can only be used once.
17199 */
17200 char_u *
17201get_vim_var_str(idx)
17202 int idx;
17203{
17204 return get_tv_string(&vimvars[idx].vv_tv);
17205}
17206#endif
17207
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208/*
17209 * Set v:count, v:count1 and v:prevcount.
17210 */
17211 void
17212set_vcount(count, count1)
17213 long count;
17214 long count1;
17215{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017216 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17217 vimvars[VV_COUNT].vv_nr = count;
17218 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219}
17220
17221/*
17222 * Set string v: variable to a copy of "val".
17223 */
17224 void
17225set_vim_var_string(idx, val, len)
17226 int idx;
17227 char_u *val;
17228 int len; /* length of "val" to use or -1 (whole string) */
17229{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017230 /* Need to do this (at least) once, since we can't initialize a union.
17231 * Will always be invoked when "v:progname" is set. */
17232 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17233
Bram Moolenaare9a41262005-01-15 22:18:47 +000017234 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017236 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017238 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017240 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241}
17242
17243/*
17244 * Set v:register if needed.
17245 */
17246 void
17247set_reg_var(c)
17248 int c;
17249{
17250 char_u regname;
17251
17252 if (c == 0 || c == ' ')
17253 regname = '"';
17254 else
17255 regname = c;
17256 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017257 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 set_vim_var_string(VV_REG, &regname, 1);
17259}
17260
17261/*
17262 * Get or set v:exception. If "oldval" == NULL, return the current value.
17263 * Otherwise, restore the value to "oldval" and return NULL.
17264 * Must always be called in pairs to save and restore v:exception! Does not
17265 * take care of memory allocations.
17266 */
17267 char_u *
17268v_exception(oldval)
17269 char_u *oldval;
17270{
17271 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017272 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273
Bram Moolenaare9a41262005-01-15 22:18:47 +000017274 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 return NULL;
17276}
17277
17278/*
17279 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17280 * Otherwise, restore the value to "oldval" and return NULL.
17281 * Must always be called in pairs to save and restore v:throwpoint! Does not
17282 * take care of memory allocations.
17283 */
17284 char_u *
17285v_throwpoint(oldval)
17286 char_u *oldval;
17287{
17288 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017289 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290
Bram Moolenaare9a41262005-01-15 22:18:47 +000017291 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 return NULL;
17293}
17294
17295#if defined(FEAT_AUTOCMD) || defined(PROTO)
17296/*
17297 * Set v:cmdarg.
17298 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17299 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17300 * Must always be called in pairs!
17301 */
17302 char_u *
17303set_cmdarg(eap, oldarg)
17304 exarg_T *eap;
17305 char_u *oldarg;
17306{
17307 char_u *oldval;
17308 char_u *newval;
17309 unsigned len;
17310
Bram Moolenaare9a41262005-01-15 22:18:47 +000017311 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017312 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017314 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017315 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017316 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 }
17318
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017319 if (eap->force_bin == FORCE_BIN)
17320 len = 6;
17321 else if (eap->force_bin == FORCE_NOBIN)
17322 len = 8;
17323 else
17324 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017325
17326 if (eap->read_edit)
17327 len += 7;
17328
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017329 if (eap->force_ff != 0)
17330 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17331# ifdef FEAT_MBYTE
17332 if (eap->force_enc != 0)
17333 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017334 if (eap->bad_char != 0)
17335 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017336# endif
17337
17338 newval = alloc(len + 1);
17339 if (newval == NULL)
17340 return NULL;
17341
17342 if (eap->force_bin == FORCE_BIN)
17343 sprintf((char *)newval, " ++bin");
17344 else if (eap->force_bin == FORCE_NOBIN)
17345 sprintf((char *)newval, " ++nobin");
17346 else
17347 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017348
17349 if (eap->read_edit)
17350 STRCAT(newval, " ++edit");
17351
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017352 if (eap->force_ff != 0)
17353 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17354 eap->cmd + eap->force_ff);
17355# ifdef FEAT_MBYTE
17356 if (eap->force_enc != 0)
17357 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17358 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017359 if (eap->bad_char != 0)
17360 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17361 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017362# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017363 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017364 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365}
17366#endif
17367
17368/*
17369 * Get the value of internal variable "name".
17370 * Return OK or FAIL.
17371 */
17372 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017373get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 char_u *name;
17375 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017376 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017377 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378{
17379 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017380 typval_T *tv = NULL;
17381 typval_T atv;
17382 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384
17385 /* truncate the name, so that we can use strcmp() */
17386 cc = name[len];
17387 name[len] = NUL;
17388
17389 /*
17390 * Check for "b:changedtick".
17391 */
17392 if (STRCMP(name, "b:changedtick") == 0)
17393 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017394 atv.v_type = VAR_NUMBER;
17395 atv.vval.v_number = curbuf->b_changedtick;
17396 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 }
17398
17399 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 * Check for user-defined variables.
17401 */
17402 else
17403 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017404 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017406 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 }
17408
Bram Moolenaare9a41262005-01-15 22:18:47 +000017409 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017411 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017412 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 ret = FAIL;
17414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017415 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017416 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417
17418 name[len] = cc;
17419
17420 return ret;
17421}
17422
17423/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017424 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17425 * Also handle function call with Funcref variable: func(expr)
17426 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17427 */
17428 static int
17429handle_subscript(arg, rettv, evaluate, verbose)
17430 char_u **arg;
17431 typval_T *rettv;
17432 int evaluate; /* do more than finding the end */
17433 int verbose; /* give error messages */
17434{
17435 int ret = OK;
17436 dict_T *selfdict = NULL;
17437 char_u *s;
17438 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017439 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017440
17441 while (ret == OK
17442 && (**arg == '['
17443 || (**arg == '.' && rettv->v_type == VAR_DICT)
17444 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17445 && !vim_iswhite(*(*arg - 1)))
17446 {
17447 if (**arg == '(')
17448 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017449 /* need to copy the funcref so that we can clear rettv */
17450 functv = *rettv;
17451 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017452
17453 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017454 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017455 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017456 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17457 &len, evaluate, selfdict);
17458
17459 /* Clear the funcref afterwards, so that deleting it while
17460 * evaluating the arguments is possible (see test55). */
17461 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017462
17463 /* Stop the expression evaluation when immediately aborting on
17464 * error, or when an interrupt occurred or an exception was thrown
17465 * but not caught. */
17466 if (aborting())
17467 {
17468 if (ret == OK)
17469 clear_tv(rettv);
17470 ret = FAIL;
17471 }
17472 dict_unref(selfdict);
17473 selfdict = NULL;
17474 }
17475 else /* **arg == '[' || **arg == '.' */
17476 {
17477 dict_unref(selfdict);
17478 if (rettv->v_type == VAR_DICT)
17479 {
17480 selfdict = rettv->vval.v_dict;
17481 if (selfdict != NULL)
17482 ++selfdict->dv_refcount;
17483 }
17484 else
17485 selfdict = NULL;
17486 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17487 {
17488 clear_tv(rettv);
17489 ret = FAIL;
17490 }
17491 }
17492 }
17493 dict_unref(selfdict);
17494 return ret;
17495}
17496
17497/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017498 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17499 * value).
17500 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017502alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017503{
Bram Moolenaar33570922005-01-25 22:26:29 +000017504 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017505}
17506
17507/*
17508 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 * The string "s" must have been allocated, it is consumed.
17510 * Return NULL for out of memory, the variable otherwise.
17511 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017513alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 char_u *s;
17515{
Bram Moolenaar33570922005-01-25 22:26:29 +000017516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017518 rettv = alloc_tv();
17519 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 rettv->v_type = VAR_STRING;
17522 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 }
17524 else
17525 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017526 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527}
17528
17529/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017530 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017532 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017533free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535{
17536 if (varp != NULL)
17537 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017538 switch (varp->v_type)
17539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017540 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017541 func_unref(varp->vval.v_string);
17542 /*FALLTHROUGH*/
17543 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017544 vim_free(varp->vval.v_string);
17545 break;
17546 case VAR_LIST:
17547 list_unref(varp->vval.v_list);
17548 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017549 case VAR_DICT:
17550 dict_unref(varp->vval.v_dict);
17551 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017552 case VAR_NUMBER:
17553 case VAR_UNKNOWN:
17554 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017555 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017556 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017557 break;
17558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 vim_free(varp);
17560 }
17561}
17562
17563/*
17564 * Free the memory for a variable value and set the value to NULL or 0.
17565 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017566 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017567clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017568 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569{
17570 if (varp != NULL)
17571 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017572 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017574 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017575 func_unref(varp->vval.v_string);
17576 /*FALLTHROUGH*/
17577 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017578 vim_free(varp->vval.v_string);
17579 varp->vval.v_string = NULL;
17580 break;
17581 case VAR_LIST:
17582 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017583 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017584 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017585 case VAR_DICT:
17586 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017587 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017588 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017589 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017590 varp->vval.v_number = 0;
17591 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017592 case VAR_UNKNOWN:
17593 break;
17594 default:
17595 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017597 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 }
17599}
17600
17601/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017602 * Set the value of a variable to NULL without freeing items.
17603 */
17604 static void
17605init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017606 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017607{
17608 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017609 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017610}
17611
17612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 * Get the number value of a variable.
17614 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017615 * For incompatible types, return 0.
17616 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17617 * caller of incompatible types: it sets *denote to TRUE if "denote"
17618 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 */
17620 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017621get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017622 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017624 int error = FALSE;
17625
17626 return get_tv_number_chk(varp, &error); /* return 0L on error */
17627}
17628
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017629 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017630get_tv_number_chk(varp, denote)
17631 typval_T *varp;
17632 int *denote;
17633{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017634 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017636 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017638 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017639 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017640 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017641 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017642 break;
17643 case VAR_STRING:
17644 if (varp->vval.v_string != NULL)
17645 vim_str2nr(varp->vval.v_string, NULL, NULL,
17646 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017647 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017648 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017649 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017650 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017651 case VAR_DICT:
17652 EMSG(_("E728: Using a Dictionary as a number"));
17653 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017654 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017655 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017656 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017658 if (denote == NULL) /* useful for values that must be unsigned */
17659 n = -1;
17660 else
17661 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017662 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663}
17664
17665/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017666 * Get the lnum from the first argument.
17667 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017668 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 */
17670 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017672 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673{
Bram Moolenaar33570922005-01-25 22:26:29 +000017674 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 linenr_T lnum;
17676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017677 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 if (lnum == 0) /* no valid number, try using line() */
17679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017680 rettv.v_type = VAR_NUMBER;
17681 f_line(argvars, &rettv);
17682 lnum = rettv.vval.v_number;
17683 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 }
17685 return lnum;
17686}
17687
17688/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017689 * Get the lnum from the first argument.
17690 * Also accepts "$", then "buf" is used.
17691 * Returns 0 on error.
17692 */
17693 static linenr_T
17694get_tv_lnum_buf(argvars, buf)
17695 typval_T *argvars;
17696 buf_T *buf;
17697{
17698 if (argvars[0].v_type == VAR_STRING
17699 && argvars[0].vval.v_string != NULL
17700 && argvars[0].vval.v_string[0] == '$'
17701 && buf != NULL)
17702 return buf->b_ml.ml_line_count;
17703 return get_tv_number_chk(&argvars[0], NULL);
17704}
17705
17706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 * Get the string value of a variable.
17708 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017709 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17710 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 * If the String variable has never been set, return an empty string.
17712 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017713 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17714 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 */
17716 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017717get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017718 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719{
17720 static char_u mybuf[NUMBUFLEN];
17721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017722 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723}
17724
17725 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017726get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017727 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017728 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017730 char_u *res = get_tv_string_buf_chk(varp, buf);
17731
17732 return res != NULL ? res : (char_u *)"";
17733}
17734
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017735 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017736get_tv_string_chk(varp)
17737 typval_T *varp;
17738{
17739 static char_u mybuf[NUMBUFLEN];
17740
17741 return get_tv_string_buf_chk(varp, mybuf);
17742}
17743
17744 static char_u *
17745get_tv_string_buf_chk(varp, buf)
17746 typval_T *varp;
17747 char_u *buf;
17748{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017749 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017751 case VAR_NUMBER:
17752 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17753 return buf;
17754 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017755 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017756 break;
17757 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017758 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017759 break;
17760 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017761 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017762 break;
17763 case VAR_STRING:
17764 if (varp->vval.v_string != NULL)
17765 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017766 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017767 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017769 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017771 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772}
17773
17774/*
17775 * Find variable "name" in the list of variables.
17776 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017777 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017778 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017779 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017781 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017782find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017784 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017787 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788
Bram Moolenaara7043832005-01-21 11:56:39 +000017789 ht = find_var_ht(name, &varname);
17790 if (htp != NULL)
17791 *htp = ht;
17792 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017794 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795}
17796
17797/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017798 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017799 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017801 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017802find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017803 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017804 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017805 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017806{
Bram Moolenaar33570922005-01-25 22:26:29 +000017807 hashitem_T *hi;
17808
17809 if (*varname == NUL)
17810 {
17811 /* Must be something like "s:", otherwise "ht" would be NULL. */
17812 switch (varname[-2])
17813 {
17814 case 's': return &SCRIPT_SV(current_SID).sv_var;
17815 case 'g': return &globvars_var;
17816 case 'v': return &vimvars_var;
17817 case 'b': return &curbuf->b_bufvar;
17818 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017819#ifdef FEAT_WINDOWS
17820 case 't': return &curtab->tp_winvar;
17821#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017822 case 'l': return current_funccal == NULL
17823 ? NULL : &current_funccal->l_vars_var;
17824 case 'a': return current_funccal == NULL
17825 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017826 }
17827 return NULL;
17828 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017829
17830 hi = hash_find(ht, varname);
17831 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017832 {
17833 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017834 * worked find the variable again. Don't auto-load a script if it was
17835 * loaded already, otherwise it would be loaded every time when
17836 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017837 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017838 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017839 hi = hash_find(ht, varname);
17840 if (HASHITEM_EMPTY(hi))
17841 return NULL;
17842 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017843 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017844}
17845
17846/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017847 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017848 * Set "varname" to the start of name without ':'.
17849 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017850 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017851find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 char_u *name;
17853 char_u **varname;
17854{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017855 hashitem_T *hi;
17856
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 if (name[1] != ':')
17858 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017859 /* The name must not start with a colon or #. */
17860 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 return NULL;
17862 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017863
17864 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017865 hi = hash_find(&compat_hashtab, name);
17866 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017867 return &compat_hashtab;
17868
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017870 return &globvarht; /* global variable */
17871 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 }
17873 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017874 if (*name == 'g') /* global variable */
17875 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017876 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17877 */
17878 if (vim_strchr(name + 2, ':') != NULL
17879 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017880 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017882 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017884 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017885#ifdef FEAT_WINDOWS
17886 if (*name == 't') /* tab page variable */
17887 return &curtab->tp_vars.dv_hashtab;
17888#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017889 if (*name == 'v') /* v: variable */
17890 return &vimvarht;
17891 if (*name == 'a' && current_funccal != NULL) /* function argument */
17892 return &current_funccal->l_avars.dv_hashtab;
17893 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17894 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 if (*name == 's' /* script variable */
17896 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17897 return &SCRIPT_VARS(current_SID);
17898 return NULL;
17899}
17900
17901/*
17902 * Get the string value of a (global/local) variable.
17903 * Returns NULL when it doesn't exist.
17904 */
17905 char_u *
17906get_var_value(name)
17907 char_u *name;
17908{
Bram Moolenaar33570922005-01-25 22:26:29 +000017909 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910
Bram Moolenaara7043832005-01-21 11:56:39 +000017911 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 if (v == NULL)
17913 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017914 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915}
17916
17917/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017918 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 * sourcing this script and when executing functions defined in the script.
17920 */
17921 void
17922new_script_vars(id)
17923 scid_T id;
17924{
Bram Moolenaara7043832005-01-21 11:56:39 +000017925 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017926 hashtab_T *ht;
17927 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017928
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17930 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017931 /* Re-allocating ga_data means that an ht_array pointing to
17932 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017933 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017934 for (i = 1; i <= ga_scripts.ga_len; ++i)
17935 {
17936 ht = &SCRIPT_VARS(i);
17937 if (ht->ht_mask == HT_INIT_SIZE - 1)
17938 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017939 sv = &SCRIPT_SV(i);
17940 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017941 }
17942
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 while (ga_scripts.ga_len < id)
17944 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017945 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17946 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 }
17949 }
17950}
17951
17952/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017953 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17954 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 */
17956 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017957init_var_dict(dict, dict_var)
17958 dict_T *dict;
17959 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960{
Bram Moolenaar33570922005-01-25 22:26:29 +000017961 hash_init(&dict->dv_hashtab);
17962 dict->dv_refcount = 99999;
17963 dict_var->di_tv.vval.v_dict = dict;
17964 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017965 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017966 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17967 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968}
17969
17970/*
17971 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017972 * Frees all allocated variables and the value they contain.
17973 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 */
17975 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017976vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017977 hashtab_T *ht;
17978{
17979 vars_clear_ext(ht, TRUE);
17980}
17981
17982/*
17983 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17984 */
17985 static void
17986vars_clear_ext(ht, free_val)
17987 hashtab_T *ht;
17988 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989{
Bram Moolenaara7043832005-01-21 11:56:39 +000017990 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017991 hashitem_T *hi;
17992 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017995 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017996 for (hi = ht->ht_array; todo > 0; ++hi)
17997 {
17998 if (!HASHITEM_EMPTY(hi))
17999 {
18000 --todo;
18001
Bram Moolenaar33570922005-01-25 22:26:29 +000018002 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018003 * ht_array might change then. hash_clear() takes care of it
18004 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018005 v = HI2DI(hi);
18006 if (free_val)
18007 clear_tv(&v->di_tv);
18008 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18009 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018010 }
18011 }
18012 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018013 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014}
18015
Bram Moolenaara7043832005-01-21 11:56:39 +000018016/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018017 * Delete a variable from hashtab "ht" at item "hi".
18018 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018021delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018022 hashtab_T *ht;
18023 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024{
Bram Moolenaar33570922005-01-25 22:26:29 +000018025 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018026
18027 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 clear_tv(&di->di_tv);
18029 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030}
18031
18032/*
18033 * List the value of one internal variable.
18034 */
18035 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018036list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018037 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018039 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018041 char_u *tofree;
18042 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018043 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018044
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018045 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018046 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018047 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018048 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049}
18050
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018052list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 char_u *prefix;
18054 char_u *name;
18055 int type;
18056 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018057 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058{
Bram Moolenaar31859182007-08-14 20:41:13 +000018059 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18060 msg_start();
18061 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 if (name != NULL) /* "a:" vars don't have a name stored */
18063 msg_puts(name);
18064 msg_putchar(' ');
18065 msg_advance(22);
18066 if (type == VAR_NUMBER)
18067 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018068 else if (type == VAR_FUNC)
18069 msg_putchar('*');
18070 else if (type == VAR_LIST)
18071 {
18072 msg_putchar('[');
18073 if (*string == '[')
18074 ++string;
18075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018076 else if (type == VAR_DICT)
18077 {
18078 msg_putchar('{');
18079 if (*string == '{')
18080 ++string;
18081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 else
18083 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018084
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018086
18087 if (type == VAR_FUNC)
18088 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018089 if (*first)
18090 {
18091 msg_clr_eos();
18092 *first = FALSE;
18093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094}
18095
18096/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018097 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 * If the variable already exists, the value is updated.
18099 * Otherwise the variable is created.
18100 */
18101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018102set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018104 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018105 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106{
Bram Moolenaar33570922005-01-25 22:26:29 +000018107 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018109 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018110 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018112 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018113 {
18114 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18115 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18116 ? name[2] : name[0]))
18117 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018118 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018119 return;
18120 }
18121 if (function_exists(name))
18122 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018123 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018124 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018125 return;
18126 }
18127 }
18128
Bram Moolenaara7043832005-01-21 11:56:39 +000018129 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018130 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018131 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018132 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018133 return;
18134 }
18135
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018136 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018137 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018139 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018140 if (var_check_ro(v->di_flags, name)
18141 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018142 return;
18143 if (v->di_tv.v_type != tv->v_type
18144 && !((v->di_tv.v_type == VAR_STRING
18145 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018146 && (tv->v_type == VAR_STRING
18147 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018148 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018149 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018150 return;
18151 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018152
18153 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018154 * Handle setting internal v: variables separately: we don't change
18155 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018156 */
18157 if (ht == &vimvarht)
18158 {
18159 if (v->di_tv.v_type == VAR_STRING)
18160 {
18161 vim_free(v->di_tv.vval.v_string);
18162 if (copy || tv->v_type != VAR_STRING)
18163 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18164 else
18165 {
18166 /* Take over the string to avoid an extra alloc/free. */
18167 v->di_tv.vval.v_string = tv->vval.v_string;
18168 tv->vval.v_string = NULL;
18169 }
18170 }
18171 else if (v->di_tv.v_type != VAR_NUMBER)
18172 EMSG2(_(e_intern2), "set_var()");
18173 else
18174 v->di_tv.vval.v_number = get_tv_number(tv);
18175 return;
18176 }
18177
18178 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 }
18180 else /* add a new variable */
18181 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018182 /* Can't add "v:" variable. */
18183 if (ht == &vimvarht)
18184 {
18185 EMSG2(_(e_illvar), name);
18186 return;
18187 }
18188
Bram Moolenaar92124a32005-06-17 22:03:40 +000018189 /* Make sure the variable name is valid. */
18190 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018191 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18192 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018193 {
18194 EMSG2(_(e_illvar), varname);
18195 return;
18196 }
18197
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018198 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18199 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018200 if (v == NULL)
18201 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018202 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018203 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018205 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 return;
18207 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018208 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018211 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018212 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018213 else
18214 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018215 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018216 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018217 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219}
18220
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018221/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018222 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018223 * Also give an error message.
18224 */
18225 static int
18226var_check_ro(flags, name)
18227 int flags;
18228 char_u *name;
18229{
18230 if (flags & DI_FLAGS_RO)
18231 {
18232 EMSG2(_(e_readonlyvar), name);
18233 return TRUE;
18234 }
18235 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18236 {
18237 EMSG2(_(e_readonlysbx), name);
18238 return TRUE;
18239 }
18240 return FALSE;
18241}
18242
18243/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018244 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18245 * Also give an error message.
18246 */
18247 static int
18248var_check_fixed(flags, name)
18249 int flags;
18250 char_u *name;
18251{
18252 if (flags & DI_FLAGS_FIX)
18253 {
18254 EMSG2(_("E795: Cannot delete variable %s"), name);
18255 return TRUE;
18256 }
18257 return FALSE;
18258}
18259
18260/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018261 * Return TRUE if typeval "tv" is set to be locked (immutable).
18262 * Also give an error message, using "name".
18263 */
18264 static int
18265tv_check_lock(lock, name)
18266 int lock;
18267 char_u *name;
18268{
18269 if (lock & VAR_LOCKED)
18270 {
18271 EMSG2(_("E741: Value is locked: %s"),
18272 name == NULL ? (char_u *)_("Unknown") : name);
18273 return TRUE;
18274 }
18275 if (lock & VAR_FIXED)
18276 {
18277 EMSG2(_("E742: Cannot change value of %s"),
18278 name == NULL ? (char_u *)_("Unknown") : name);
18279 return TRUE;
18280 }
18281 return FALSE;
18282}
18283
18284/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018285 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018286 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018287 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018290copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018291 typval_T *from;
18292 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018294 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018295 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018296 switch (from->v_type)
18297 {
18298 case VAR_NUMBER:
18299 to->vval.v_number = from->vval.v_number;
18300 break;
18301 case VAR_STRING:
18302 case VAR_FUNC:
18303 if (from->vval.v_string == NULL)
18304 to->vval.v_string = NULL;
18305 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018306 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018307 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018308 if (from->v_type == VAR_FUNC)
18309 func_ref(to->vval.v_string);
18310 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018311 break;
18312 case VAR_LIST:
18313 if (from->vval.v_list == NULL)
18314 to->vval.v_list = NULL;
18315 else
18316 {
18317 to->vval.v_list = from->vval.v_list;
18318 ++to->vval.v_list->lv_refcount;
18319 }
18320 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018321 case VAR_DICT:
18322 if (from->vval.v_dict == NULL)
18323 to->vval.v_dict = NULL;
18324 else
18325 {
18326 to->vval.v_dict = from->vval.v_dict;
18327 ++to->vval.v_dict->dv_refcount;
18328 }
18329 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018330 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018331 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018332 break;
18333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334}
18335
18336/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018337 * Make a copy of an item.
18338 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018339 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18340 * reference to an already copied list/dict can be used.
18341 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018342 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018343 static int
18344item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018345 typval_T *from;
18346 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018347 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018348 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018349{
18350 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018351 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018352
Bram Moolenaar33570922005-01-25 22:26:29 +000018353 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018354 {
18355 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018356 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018357 }
18358 ++recurse;
18359
18360 switch (from->v_type)
18361 {
18362 case VAR_NUMBER:
18363 case VAR_STRING:
18364 case VAR_FUNC:
18365 copy_tv(from, to);
18366 break;
18367 case VAR_LIST:
18368 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018369 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018370 if (from->vval.v_list == NULL)
18371 to->vval.v_list = NULL;
18372 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18373 {
18374 /* use the copy made earlier */
18375 to->vval.v_list = from->vval.v_list->lv_copylist;
18376 ++to->vval.v_list->lv_refcount;
18377 }
18378 else
18379 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18380 if (to->vval.v_list == NULL)
18381 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018382 break;
18383 case VAR_DICT:
18384 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018385 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018386 if (from->vval.v_dict == NULL)
18387 to->vval.v_dict = NULL;
18388 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18389 {
18390 /* use the copy made earlier */
18391 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18392 ++to->vval.v_dict->dv_refcount;
18393 }
18394 else
18395 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18396 if (to->vval.v_dict == NULL)
18397 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018398 break;
18399 default:
18400 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018401 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018402 }
18403 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018404 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018405}
18406
18407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 * ":echo expr1 ..." print each argument separated with a space, add a
18409 * newline at the end.
18410 * ":echon expr1 ..." print each argument plain.
18411 */
18412 void
18413ex_echo(eap)
18414 exarg_T *eap;
18415{
18416 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018417 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018418 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 char_u *p;
18420 int needclr = TRUE;
18421 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018422 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423
18424 if (eap->skip)
18425 ++emsg_skip;
18426 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18427 {
18428 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430 {
18431 /*
18432 * Report the invalid expression unless the expression evaluation
18433 * has been cancelled due to an aborting error, an interrupt, or an
18434 * exception.
18435 */
18436 if (!aborting())
18437 EMSG2(_(e_invexpr2), p);
18438 break;
18439 }
18440 if (!eap->skip)
18441 {
18442 if (atstart)
18443 {
18444 atstart = FALSE;
18445 /* Call msg_start() after eval1(), evaluating the expression
18446 * may cause a message to appear. */
18447 if (eap->cmdidx == CMD_echo)
18448 msg_start();
18449 }
18450 else if (eap->cmdidx == CMD_echo)
18451 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018452 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018453 if (p != NULL)
18454 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018456 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018458 if (*p != TAB && needclr)
18459 {
18460 /* remove any text still there from the command */
18461 msg_clr_eos();
18462 needclr = FALSE;
18463 }
18464 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 }
18466 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018467 {
18468#ifdef FEAT_MBYTE
18469 if (has_mbyte)
18470 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018471 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018472
18473 (void)msg_outtrans_len_attr(p, i, echo_attr);
18474 p += i - 1;
18475 }
18476 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018478 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018481 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 arg = skipwhite(arg);
18485 }
18486 eap->nextcmd = check_nextcmd(arg);
18487
18488 if (eap->skip)
18489 --emsg_skip;
18490 else
18491 {
18492 /* remove text that may still be there from the command */
18493 if (needclr)
18494 msg_clr_eos();
18495 if (eap->cmdidx == CMD_echo)
18496 msg_end();
18497 }
18498}
18499
18500/*
18501 * ":echohl {name}".
18502 */
18503 void
18504ex_echohl(eap)
18505 exarg_T *eap;
18506{
18507 int id;
18508
18509 id = syn_name2id(eap->arg);
18510 if (id == 0)
18511 echo_attr = 0;
18512 else
18513 echo_attr = syn_id2attr(id);
18514}
18515
18516/*
18517 * ":execute expr1 ..." execute the result of an expression.
18518 * ":echomsg expr1 ..." Print a message
18519 * ":echoerr expr1 ..." Print an error
18520 * Each gets spaces around each argument and a newline at the end for
18521 * echo commands
18522 */
18523 void
18524ex_execute(eap)
18525 exarg_T *eap;
18526{
18527 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018528 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 int ret = OK;
18530 char_u *p;
18531 garray_T ga;
18532 int len;
18533 int save_did_emsg;
18534
18535 ga_init2(&ga, 1, 80);
18536
18537 if (eap->skip)
18538 ++emsg_skip;
18539 while (*arg != NUL && *arg != '|' && *arg != '\n')
18540 {
18541 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018542 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 {
18544 /*
18545 * Report the invalid expression unless the expression evaluation
18546 * has been cancelled due to an aborting error, an interrupt, or an
18547 * exception.
18548 */
18549 if (!aborting())
18550 EMSG2(_(e_invexpr2), p);
18551 ret = FAIL;
18552 break;
18553 }
18554
18555 if (!eap->skip)
18556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018557 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 len = (int)STRLEN(p);
18559 if (ga_grow(&ga, len + 2) == FAIL)
18560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018561 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 ret = FAIL;
18563 break;
18564 }
18565 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 ga.ga_len += len;
18569 }
18570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 arg = skipwhite(arg);
18573 }
18574
18575 if (ret != FAIL && ga.ga_data != NULL)
18576 {
18577 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018580 out_flush();
18581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 else if (eap->cmdidx == CMD_echoerr)
18583 {
18584 /* We don't want to abort following commands, restore did_emsg. */
18585 save_did_emsg = did_emsg;
18586 EMSG((char_u *)ga.ga_data);
18587 if (!force_abort)
18588 did_emsg = save_did_emsg;
18589 }
18590 else if (eap->cmdidx == CMD_execute)
18591 do_cmdline((char_u *)ga.ga_data,
18592 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18593 }
18594
18595 ga_clear(&ga);
18596
18597 if (eap->skip)
18598 --emsg_skip;
18599
18600 eap->nextcmd = check_nextcmd(arg);
18601}
18602
18603/*
18604 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18605 * "arg" points to the "&" or '+' when called, to "option" when returning.
18606 * Returns NULL when no option name found. Otherwise pointer to the char
18607 * after the option name.
18608 */
18609 static char_u *
18610find_option_end(arg, opt_flags)
18611 char_u **arg;
18612 int *opt_flags;
18613{
18614 char_u *p = *arg;
18615
18616 ++p;
18617 if (*p == 'g' && p[1] == ':')
18618 {
18619 *opt_flags = OPT_GLOBAL;
18620 p += 2;
18621 }
18622 else if (*p == 'l' && p[1] == ':')
18623 {
18624 *opt_flags = OPT_LOCAL;
18625 p += 2;
18626 }
18627 else
18628 *opt_flags = 0;
18629
18630 if (!ASCII_ISALPHA(*p))
18631 return NULL;
18632 *arg = p;
18633
18634 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18635 p += 4; /* termcap option */
18636 else
18637 while (ASCII_ISALPHA(*p))
18638 ++p;
18639 return p;
18640}
18641
18642/*
18643 * ":function"
18644 */
18645 void
18646ex_function(eap)
18647 exarg_T *eap;
18648{
18649 char_u *theline;
18650 int j;
18651 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 char_u *name = NULL;
18654 char_u *p;
18655 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018656 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 garray_T newargs;
18658 garray_T newlines;
18659 int varargs = FALSE;
18660 int mustend = FALSE;
18661 int flags = 0;
18662 ufunc_T *fp;
18663 int indent;
18664 int nesting;
18665 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 dictitem_T *v;
18667 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018668 static int func_nr = 0; /* number for nameless function */
18669 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018670 hashtab_T *ht;
18671 int todo;
18672 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018673 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674
18675 /*
18676 * ":function" without argument: list functions.
18677 */
18678 if (ends_excmd(*eap->arg))
18679 {
18680 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018681 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018682 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018683 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018684 {
18685 if (!HASHITEM_EMPTY(hi))
18686 {
18687 --todo;
18688 fp = HI2UF(hi);
18689 if (!isdigit(*fp->uf_name))
18690 list_func_head(fp, FALSE);
18691 }
18692 }
18693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 eap->nextcmd = check_nextcmd(eap->arg);
18695 return;
18696 }
18697
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018698 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018699 * ":function /pat": list functions matching pattern.
18700 */
18701 if (*eap->arg == '/')
18702 {
18703 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18704 if (!eap->skip)
18705 {
18706 regmatch_T regmatch;
18707
18708 c = *p;
18709 *p = NUL;
18710 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18711 *p = c;
18712 if (regmatch.regprog != NULL)
18713 {
18714 regmatch.rm_ic = p_ic;
18715
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018716 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018717 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18718 {
18719 if (!HASHITEM_EMPTY(hi))
18720 {
18721 --todo;
18722 fp = HI2UF(hi);
18723 if (!isdigit(*fp->uf_name)
18724 && vim_regexec(&regmatch, fp->uf_name, 0))
18725 list_func_head(fp, FALSE);
18726 }
18727 }
18728 }
18729 }
18730 if (*p == '/')
18731 ++p;
18732 eap->nextcmd = check_nextcmd(p);
18733 return;
18734 }
18735
18736 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018737 * Get the function name. There are these situations:
18738 * func normal function name
18739 * "name" == func, "fudi.fd_dict" == NULL
18740 * dict.func new dictionary entry
18741 * "name" == NULL, "fudi.fd_dict" set,
18742 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18743 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018744 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018745 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18746 * dict.func existing dict entry that's not a Funcref
18747 * "name" == NULL, "fudi.fd_dict" set,
18748 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018751 name = trans_function_name(&p, eap->skip, 0, &fudi);
18752 paren = (vim_strchr(p, '(') != NULL);
18753 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 {
18755 /*
18756 * Return on an invalid expression in braces, unless the expression
18757 * evaluation has been cancelled due to an aborting error, an
18758 * interrupt, or an exception.
18759 */
18760 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018761 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018762 if (!eap->skip && fudi.fd_newkey != NULL)
18763 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018764 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 else
18768 eap->skip = TRUE;
18769 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018770
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 /* An error in a function call during evaluation of an expression in magic
18772 * braces should not cause the function not to be defined. */
18773 saved_did_emsg = did_emsg;
18774 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775
18776 /*
18777 * ":function func" with only function name: list function.
18778 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018779 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 {
18781 if (!ends_excmd(*skipwhite(p)))
18782 {
18783 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018784 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 }
18786 eap->nextcmd = check_nextcmd(p);
18787 if (eap->nextcmd != NULL)
18788 *p = NUL;
18789 if (!eap->skip && !got_int)
18790 {
18791 fp = find_func(name);
18792 if (fp != NULL)
18793 {
18794 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018795 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018797 if (FUNCLINE(fp, j) == NULL)
18798 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 msg_putchar('\n');
18800 msg_outnum((long)(j + 1));
18801 if (j < 9)
18802 msg_putchar(' ');
18803 if (j < 99)
18804 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018805 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 out_flush(); /* show a line at a time */
18807 ui_breakcheck();
18808 }
18809 if (!got_int)
18810 {
18811 msg_putchar('\n');
18812 msg_puts((char_u *)" endfunction");
18813 }
18814 }
18815 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018816 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018818 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 }
18820
18821 /*
18822 * ":function name(arg1, arg2)" Define function.
18823 */
18824 p = skipwhite(p);
18825 if (*p != '(')
18826 {
18827 if (!eap->skip)
18828 {
18829 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018830 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 }
18832 /* attempt to continue by skipping some text */
18833 if (vim_strchr(p, '(') != NULL)
18834 p = vim_strchr(p, '(');
18835 }
18836 p = skipwhite(p + 1);
18837
18838 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18839 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18840
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018841 if (!eap->skip)
18842 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018843 /* Check the name of the function. Unless it's a dictionary function
18844 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018845 if (name != NULL)
18846 arg = name;
18847 else
18848 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018849 if (arg != NULL && (fudi.fd_di == NULL
18850 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018851 {
18852 if (*arg == K_SPECIAL)
18853 j = 3;
18854 else
18855 j = 0;
18856 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18857 : eval_isnamec(arg[j])))
18858 ++j;
18859 if (arg[j] != NUL)
18860 emsg_funcname(_(e_invarg2), arg);
18861 }
18862 }
18863
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 /*
18865 * Isolate the arguments: "arg1, arg2, ...)"
18866 */
18867 while (*p != ')')
18868 {
18869 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18870 {
18871 varargs = TRUE;
18872 p += 3;
18873 mustend = TRUE;
18874 }
18875 else
18876 {
18877 arg = p;
18878 while (ASCII_ISALNUM(*p) || *p == '_')
18879 ++p;
18880 if (arg == p || isdigit(*arg)
18881 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18882 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18883 {
18884 if (!eap->skip)
18885 EMSG2(_("E125: Illegal argument: %s"), arg);
18886 break;
18887 }
18888 if (ga_grow(&newargs, 1) == FAIL)
18889 goto erret;
18890 c = *p;
18891 *p = NUL;
18892 arg = vim_strsave(arg);
18893 if (arg == NULL)
18894 goto erret;
18895 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18896 *p = c;
18897 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 if (*p == ',')
18899 ++p;
18900 else
18901 mustend = TRUE;
18902 }
18903 p = skipwhite(p);
18904 if (mustend && *p != ')')
18905 {
18906 if (!eap->skip)
18907 EMSG2(_(e_invarg2), eap->arg);
18908 break;
18909 }
18910 }
18911 ++p; /* skip the ')' */
18912
Bram Moolenaare9a41262005-01-15 22:18:47 +000018913 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 for (;;)
18915 {
18916 p = skipwhite(p);
18917 if (STRNCMP(p, "range", 5) == 0)
18918 {
18919 flags |= FC_RANGE;
18920 p += 5;
18921 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018922 else if (STRNCMP(p, "dict", 4) == 0)
18923 {
18924 flags |= FC_DICT;
18925 p += 4;
18926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 else if (STRNCMP(p, "abort", 5) == 0)
18928 {
18929 flags |= FC_ABORT;
18930 p += 5;
18931 }
18932 else
18933 break;
18934 }
18935
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018936 /* When there is a line break use what follows for the function body.
18937 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18938 if (*p == '\n')
18939 line_arg = p + 1;
18940 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 EMSG(_(e_trailing));
18942
18943 /*
18944 * Read the body of the function, until ":endfunction" is found.
18945 */
18946 if (KeyTyped)
18947 {
18948 /* Check if the function already exists, don't let the user type the
18949 * whole function before telling him it doesn't work! For a script we
18950 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018951 if (!eap->skip && !eap->forceit)
18952 {
18953 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18954 EMSG(_(e_funcdict));
18955 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018956 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018959 if (!eap->skip && did_emsg)
18960 goto erret;
18961
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 msg_putchar('\n'); /* don't overwrite the function name */
18963 cmdline_row = msg_row;
18964 }
18965
18966 indent = 2;
18967 nesting = 0;
18968 for (;;)
18969 {
18970 msg_scroll = TRUE;
18971 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018972 sourcing_lnum_off = sourcing_lnum;
18973
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018974 if (line_arg != NULL)
18975 {
18976 /* Use eap->arg, split up in parts by line breaks. */
18977 theline = line_arg;
18978 p = vim_strchr(theline, '\n');
18979 if (p == NULL)
18980 line_arg += STRLEN(line_arg);
18981 else
18982 {
18983 *p = NUL;
18984 line_arg = p + 1;
18985 }
18986 }
18987 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 theline = getcmdline(':', 0L, indent);
18989 else
18990 theline = eap->getline(':', eap->cookie, indent);
18991 if (KeyTyped)
18992 lines_left = Rows - 1;
18993 if (theline == NULL)
18994 {
18995 EMSG(_("E126: Missing :endfunction"));
18996 goto erret;
18997 }
18998
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018999 /* Detect line continuation: sourcing_lnum increased more than one. */
19000 if (sourcing_lnum > sourcing_lnum_off + 1)
19001 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19002 else
19003 sourcing_lnum_off = 0;
19004
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 if (skip_until != NULL)
19006 {
19007 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19008 * don't check for ":endfunc". */
19009 if (STRCMP(theline, skip_until) == 0)
19010 {
19011 vim_free(skip_until);
19012 skip_until = NULL;
19013 }
19014 }
19015 else
19016 {
19017 /* skip ':' and blanks*/
19018 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19019 ;
19020
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019021 /* Check for "endfunction". */
19022 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019024 if (line_arg == NULL)
19025 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 break;
19027 }
19028
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019029 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030 * at "end". */
19031 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19032 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019033 else if (STRNCMP(p, "if", 2) == 0
19034 || STRNCMP(p, "wh", 2) == 0
19035 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 || STRNCMP(p, "try", 3) == 0)
19037 indent += 2;
19038
19039 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019040 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019042 if (*p == '!')
19043 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 p += eval_fname_script(p);
19045 if (ASCII_ISALPHA(*p))
19046 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019047 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 if (*skipwhite(p) == '(')
19049 {
19050 ++nesting;
19051 indent += 2;
19052 }
19053 }
19054 }
19055
19056 /* Check for ":append" or ":insert". */
19057 p = skip_range(p, NULL);
19058 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19059 || (p[0] == 'i'
19060 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19061 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19062 skip_until = vim_strsave((char_u *)".");
19063
19064 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19065 arg = skipwhite(skiptowhite(p));
19066 if (arg[0] == '<' && arg[1] =='<'
19067 && ((p[0] == 'p' && p[1] == 'y'
19068 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19069 || (p[0] == 'p' && p[1] == 'e'
19070 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19071 || (p[0] == 't' && p[1] == 'c'
19072 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19073 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19074 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019075 || (p[0] == 'm' && p[1] == 'z'
19076 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 ))
19078 {
19079 /* ":python <<" continues until a dot, like ":append" */
19080 p = skipwhite(arg + 2);
19081 if (*p == NUL)
19082 skip_until = vim_strsave((char_u *)".");
19083 else
19084 skip_until = vim_strsave(p);
19085 }
19086 }
19087
19088 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019089 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019090 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019091 if (line_arg == NULL)
19092 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019094 }
19095
19096 /* Copy the line to newly allocated memory. get_one_sourceline()
19097 * allocates 250 bytes per line, this saves 80% on average. The cost
19098 * is an extra alloc/free. */
19099 p = vim_strsave(theline);
19100 if (p != NULL)
19101 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019102 if (line_arg == NULL)
19103 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019104 theline = p;
19105 }
19106
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019107 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19108
19109 /* Add NULL lines for continuation lines, so that the line count is
19110 * equal to the index in the growarray. */
19111 while (sourcing_lnum_off-- > 0)
19112 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019113
19114 /* Check for end of eap->arg. */
19115 if (line_arg != NULL && *line_arg == NUL)
19116 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 }
19118
19119 /* Don't define the function when skipping commands or when an error was
19120 * detected. */
19121 if (eap->skip || did_emsg)
19122 goto erret;
19123
19124 /*
19125 * If there are no errors, add the function
19126 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019127 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019128 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019129 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019130 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019131 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019132 emsg_funcname("E707: Function name conflicts with variable: %s",
19133 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019134 goto erret;
19135 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019136
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019137 fp = find_func(name);
19138 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019140 if (!eap->forceit)
19141 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019142 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019143 goto erret;
19144 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019145 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019146 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019147 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019148 name);
19149 goto erret;
19150 }
19151 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019152 ga_clear_strings(&(fp->uf_args));
19153 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019154 vim_free(name);
19155 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 }
19158 else
19159 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019160 char numbuf[20];
19161
19162 fp = NULL;
19163 if (fudi.fd_newkey == NULL && !eap->forceit)
19164 {
19165 EMSG(_(e_funcdict));
19166 goto erret;
19167 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019168 if (fudi.fd_di == NULL)
19169 {
19170 /* Can't add a function to a locked dictionary */
19171 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19172 goto erret;
19173 }
19174 /* Can't change an existing function if it is locked */
19175 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19176 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019177
19178 /* Give the function a sequential number. Can only be used with a
19179 * Funcref! */
19180 vim_free(name);
19181 sprintf(numbuf, "%d", ++func_nr);
19182 name = vim_strsave((char_u *)numbuf);
19183 if (name == NULL)
19184 goto erret;
19185 }
19186
19187 if (fp == NULL)
19188 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019189 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019190 {
19191 int slen, plen;
19192 char_u *scriptname;
19193
19194 /* Check that the autoload name matches the script name. */
19195 j = FAIL;
19196 if (sourcing_name != NULL)
19197 {
19198 scriptname = autoload_name(name);
19199 if (scriptname != NULL)
19200 {
19201 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019202 plen = (int)STRLEN(p);
19203 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019204 if (slen > plen && fnamecmp(p,
19205 sourcing_name + slen - plen) == 0)
19206 j = OK;
19207 vim_free(scriptname);
19208 }
19209 }
19210 if (j == FAIL)
19211 {
19212 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19213 goto erret;
19214 }
19215 }
19216
19217 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 if (fp == NULL)
19219 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019220
19221 if (fudi.fd_dict != NULL)
19222 {
19223 if (fudi.fd_di == NULL)
19224 {
19225 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019226 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019227 if (fudi.fd_di == NULL)
19228 {
19229 vim_free(fp);
19230 goto erret;
19231 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019232 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19233 {
19234 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019235 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019236 goto erret;
19237 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019238 }
19239 else
19240 /* overwrite existing dict entry */
19241 clear_tv(&fudi.fd_di->di_tv);
19242 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019243 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019244 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019245 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019246
19247 /* behave like "dict" was used */
19248 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019249 }
19250
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019252 STRCPY(fp->uf_name, name);
19253 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019255 fp->uf_args = newargs;
19256 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019257#ifdef FEAT_PROFILE
19258 fp->uf_tml_count = NULL;
19259 fp->uf_tml_total = NULL;
19260 fp->uf_tml_self = NULL;
19261 fp->uf_profiling = FALSE;
19262 if (prof_def_func())
19263 func_do_profile(fp);
19264#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019265 fp->uf_varargs = varargs;
19266 fp->uf_flags = flags;
19267 fp->uf_calls = 0;
19268 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019269 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270
19271erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 ga_clear_strings(&newargs);
19273 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019274ret_free:
19275 vim_free(skip_until);
19276 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279}
19280
19281/*
19282 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019283 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019285 * flags:
19286 * TFN_INT: internal function name OK
19287 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 * Advances "pp" to just after the function name (if no error).
19289 */
19290 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019291trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 char_u **pp;
19293 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019294 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019295 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019297 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 char_u *start;
19299 char_u *end;
19300 int lead;
19301 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019303 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019304
19305 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019306 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019308
19309 /* Check for hard coded <SNR>: already translated function ID (from a user
19310 * command). */
19311 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19312 && (*pp)[2] == (int)KE_SNR)
19313 {
19314 *pp += 3;
19315 len = get_id_len(pp) + 3;
19316 return vim_strnsave(start, len);
19317 }
19318
19319 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19320 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019322 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019324
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019325 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19326 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019327 if (end == start)
19328 {
19329 if (!skip)
19330 EMSG(_("E129: Function name required"));
19331 goto theend;
19332 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019333 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019334 {
19335 /*
19336 * Report an invalid expression in braces, unless the expression
19337 * evaluation has been cancelled due to an aborting error, an
19338 * interrupt, or an exception.
19339 */
19340 if (!aborting())
19341 {
19342 if (end != NULL)
19343 EMSG2(_(e_invarg2), start);
19344 }
19345 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019346 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019347 goto theend;
19348 }
19349
19350 if (lv.ll_tv != NULL)
19351 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019352 if (fdp != NULL)
19353 {
19354 fdp->fd_dict = lv.ll_dict;
19355 fdp->fd_newkey = lv.ll_newkey;
19356 lv.ll_newkey = NULL;
19357 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019358 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019359 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19360 {
19361 name = vim_strsave(lv.ll_tv->vval.v_string);
19362 *pp = end;
19363 }
19364 else
19365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019366 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19367 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019368 EMSG(_(e_funcref));
19369 else
19370 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019371 name = NULL;
19372 }
19373 goto theend;
19374 }
19375
19376 if (lv.ll_name == NULL)
19377 {
19378 /* Error found, but continue after the function name. */
19379 *pp = end;
19380 goto theend;
19381 }
19382
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019383 /* Check if the name is a Funcref. If so, use the value. */
19384 if (lv.ll_exp_name != NULL)
19385 {
19386 len = (int)STRLEN(lv.ll_exp_name);
19387 name = deref_func_name(lv.ll_exp_name, &len);
19388 if (name == lv.ll_exp_name)
19389 name = NULL;
19390 }
19391 else
19392 {
19393 len = (int)(end - *pp);
19394 name = deref_func_name(*pp, &len);
19395 if (name == *pp)
19396 name = NULL;
19397 }
19398 if (name != NULL)
19399 {
19400 name = vim_strsave(name);
19401 *pp = end;
19402 goto theend;
19403 }
19404
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019405 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019406 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019407 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019408 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19409 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19410 {
19411 /* When there was "s:" already or the name expanded to get a
19412 * leading "s:" then remove it. */
19413 lv.ll_name += 2;
19414 len -= 2;
19415 lead = 2;
19416 }
19417 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019418 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019419 {
19420 if (lead == 2) /* skip over "s:" */
19421 lv.ll_name += 2;
19422 len = (int)(end - lv.ll_name);
19423 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019424
19425 /*
19426 * Copy the function name to allocated memory.
19427 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19428 * Accept <SNR>123_name() outside a script.
19429 */
19430 if (skip)
19431 lead = 0; /* do nothing */
19432 else if (lead > 0)
19433 {
19434 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019435 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19436 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019437 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019438 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019439 if (current_SID <= 0)
19440 {
19441 EMSG(_(e_usingsid));
19442 goto theend;
19443 }
19444 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19445 lead += (int)STRLEN(sid_buf);
19446 }
19447 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019448 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019449 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019450 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019451 goto theend;
19452 }
19453 name = alloc((unsigned)(len + lead + 1));
19454 if (name != NULL)
19455 {
19456 if (lead > 0)
19457 {
19458 name[0] = K_SPECIAL;
19459 name[1] = KS_EXTRA;
19460 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019461 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019462 STRCPY(name + 3, sid_buf);
19463 }
19464 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19465 name[len + lead] = NUL;
19466 }
19467 *pp = end;
19468
19469theend:
19470 clear_lval(&lv);
19471 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472}
19473
19474/*
19475 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19476 * Return 2 if "p" starts with "s:".
19477 * Return 0 otherwise.
19478 */
19479 static int
19480eval_fname_script(p)
19481 char_u *p;
19482{
19483 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19484 || STRNICMP(p + 1, "SNR>", 4) == 0))
19485 return 5;
19486 if (p[0] == 's' && p[1] == ':')
19487 return 2;
19488 return 0;
19489}
19490
19491/*
19492 * Return TRUE if "p" starts with "<SID>" or "s:".
19493 * Only works if eval_fname_script() returned non-zero for "p"!
19494 */
19495 static int
19496eval_fname_sid(p)
19497 char_u *p;
19498{
19499 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19500}
19501
19502/*
19503 * List the head of the function: "name(arg1, arg2)".
19504 */
19505 static void
19506list_func_head(fp, indent)
19507 ufunc_T *fp;
19508 int indent;
19509{
19510 int j;
19511
19512 msg_start();
19513 if (indent)
19514 MSG_PUTS(" ");
19515 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019516 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 {
19518 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019519 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 }
19521 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019522 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019524 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 {
19526 if (j)
19527 MSG_PUTS(", ");
19528 msg_puts(FUNCARG(fp, j));
19529 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019530 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 {
19532 if (j)
19533 MSG_PUTS(", ");
19534 MSG_PUTS("...");
19535 }
19536 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019537 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019538 if (p_verbose > 0)
19539 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540}
19541
19542/*
19543 * Find a function by name, return pointer to it in ufuncs.
19544 * Return NULL for unknown function.
19545 */
19546 static ufunc_T *
19547find_func(name)
19548 char_u *name;
19549{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019550 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019552 hi = hash_find(&func_hashtab, name);
19553 if (!HASHITEM_EMPTY(hi))
19554 return HI2UF(hi);
19555 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556}
19557
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019558#if defined(EXITFREE) || defined(PROTO)
19559 void
19560free_all_functions()
19561{
19562 hashitem_T *hi;
19563
19564 /* Need to start all over every time, because func_free() may change the
19565 * hash table. */
19566 while (func_hashtab.ht_used > 0)
19567 for (hi = func_hashtab.ht_array; ; ++hi)
19568 if (!HASHITEM_EMPTY(hi))
19569 {
19570 func_free(HI2UF(hi));
19571 break;
19572 }
19573}
19574#endif
19575
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019576/*
19577 * Return TRUE if a function "name" exists.
19578 */
19579 static int
19580function_exists(name)
19581 char_u *name;
19582{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019583 char_u *nm = name;
19584 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019585 int n = FALSE;
19586
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019587 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019588 nm = skipwhite(nm);
19589
19590 /* Only accept "funcname", "funcname ", "funcname (..." and
19591 * "funcname(...", not "funcname!...". */
19592 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019593 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019594 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019595 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019596 else
19597 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019598 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019599 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019600 return n;
19601}
19602
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019603/*
19604 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019605 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019606 */
19607 static int
19608builtin_function(name)
19609 char_u *name;
19610{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019611 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19612 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019613}
19614
Bram Moolenaar05159a02005-02-26 23:04:13 +000019615#if defined(FEAT_PROFILE) || defined(PROTO)
19616/*
19617 * Start profiling function "fp".
19618 */
19619 static void
19620func_do_profile(fp)
19621 ufunc_T *fp;
19622{
19623 fp->uf_tm_count = 0;
19624 profile_zero(&fp->uf_tm_self);
19625 profile_zero(&fp->uf_tm_total);
19626 if (fp->uf_tml_count == NULL)
19627 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19628 (sizeof(int) * fp->uf_lines.ga_len));
19629 if (fp->uf_tml_total == NULL)
19630 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19631 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19632 if (fp->uf_tml_self == NULL)
19633 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19634 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19635 fp->uf_tml_idx = -1;
19636 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19637 || fp->uf_tml_self == NULL)
19638 return; /* out of memory */
19639
19640 fp->uf_profiling = TRUE;
19641}
19642
19643/*
19644 * Dump the profiling results for all functions in file "fd".
19645 */
19646 void
19647func_dump_profile(fd)
19648 FILE *fd;
19649{
19650 hashitem_T *hi;
19651 int todo;
19652 ufunc_T *fp;
19653 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019654 ufunc_T **sorttab;
19655 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019656
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019657 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019658 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19659
Bram Moolenaar05159a02005-02-26 23:04:13 +000019660 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19661 {
19662 if (!HASHITEM_EMPTY(hi))
19663 {
19664 --todo;
19665 fp = HI2UF(hi);
19666 if (fp->uf_profiling)
19667 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019668 if (sorttab != NULL)
19669 sorttab[st_len++] = fp;
19670
Bram Moolenaar05159a02005-02-26 23:04:13 +000019671 if (fp->uf_name[0] == K_SPECIAL)
19672 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19673 else
19674 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19675 if (fp->uf_tm_count == 1)
19676 fprintf(fd, "Called 1 time\n");
19677 else
19678 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19679 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19680 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19681 fprintf(fd, "\n");
19682 fprintf(fd, "count total (s) self (s)\n");
19683
19684 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19685 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019686 if (FUNCLINE(fp, i) == NULL)
19687 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019688 prof_func_line(fd, fp->uf_tml_count[i],
19689 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019690 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19691 }
19692 fprintf(fd, "\n");
19693 }
19694 }
19695 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019696
19697 if (sorttab != NULL && st_len > 0)
19698 {
19699 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19700 prof_total_cmp);
19701 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19702 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19703 prof_self_cmp);
19704 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19705 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019706}
Bram Moolenaar73830342005-02-28 22:48:19 +000019707
19708 static void
19709prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19710 FILE *fd;
19711 ufunc_T **sorttab;
19712 int st_len;
19713 char *title;
19714 int prefer_self; /* when equal print only self time */
19715{
19716 int i;
19717 ufunc_T *fp;
19718
19719 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19720 fprintf(fd, "count total (s) self (s) function\n");
19721 for (i = 0; i < 20 && i < st_len; ++i)
19722 {
19723 fp = sorttab[i];
19724 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19725 prefer_self);
19726 if (fp->uf_name[0] == K_SPECIAL)
19727 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19728 else
19729 fprintf(fd, " %s()\n", fp->uf_name);
19730 }
19731 fprintf(fd, "\n");
19732}
19733
19734/*
19735 * Print the count and times for one function or function line.
19736 */
19737 static void
19738prof_func_line(fd, count, total, self, prefer_self)
19739 FILE *fd;
19740 int count;
19741 proftime_T *total;
19742 proftime_T *self;
19743 int prefer_self; /* when equal print only self time */
19744{
19745 if (count > 0)
19746 {
19747 fprintf(fd, "%5d ", count);
19748 if (prefer_self && profile_equal(total, self))
19749 fprintf(fd, " ");
19750 else
19751 fprintf(fd, "%s ", profile_msg(total));
19752 if (!prefer_self && profile_equal(total, self))
19753 fprintf(fd, " ");
19754 else
19755 fprintf(fd, "%s ", profile_msg(self));
19756 }
19757 else
19758 fprintf(fd, " ");
19759}
19760
19761/*
19762 * Compare function for total time sorting.
19763 */
19764 static int
19765#ifdef __BORLANDC__
19766_RTLENTRYF
19767#endif
19768prof_total_cmp(s1, s2)
19769 const void *s1;
19770 const void *s2;
19771{
19772 ufunc_T *p1, *p2;
19773
19774 p1 = *(ufunc_T **)s1;
19775 p2 = *(ufunc_T **)s2;
19776 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19777}
19778
19779/*
19780 * Compare function for self time sorting.
19781 */
19782 static int
19783#ifdef __BORLANDC__
19784_RTLENTRYF
19785#endif
19786prof_self_cmp(s1, s2)
19787 const void *s1;
19788 const void *s2;
19789{
19790 ufunc_T *p1, *p2;
19791
19792 p1 = *(ufunc_T **)s1;
19793 p2 = *(ufunc_T **)s2;
19794 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19795}
19796
Bram Moolenaar05159a02005-02-26 23:04:13 +000019797#endif
19798
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019799/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019800 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019801 * Return TRUE if a package was loaded.
19802 */
19803 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019804script_autoload(name, reload)
19805 char_u *name;
19806 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019807{
19808 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019809 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019810 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019811 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019812
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019813 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019814 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019815 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019816 return FALSE;
19817
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019818 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019819
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019820 /* Find the name in the list of previously loaded package names. Skip
19821 * "autoload/", it's always the same. */
19822 for (i = 0; i < ga_loaded.ga_len; ++i)
19823 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19824 break;
19825 if (!reload && i < ga_loaded.ga_len)
19826 ret = FALSE; /* was loaded already */
19827 else
19828 {
19829 /* Remember the name if it wasn't loaded already. */
19830 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19831 {
19832 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19833 tofree = NULL;
19834 }
19835
19836 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019837 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019838 ret = TRUE;
19839 }
19840
19841 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019842 return ret;
19843}
19844
19845/*
19846 * Return the autoload script name for a function or variable name.
19847 * Returns NULL when out of memory.
19848 */
19849 static char_u *
19850autoload_name(name)
19851 char_u *name;
19852{
19853 char_u *p;
19854 char_u *scriptname;
19855
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019856 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019857 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19858 if (scriptname == NULL)
19859 return FALSE;
19860 STRCPY(scriptname, "autoload/");
19861 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019862 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019863 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019864 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019865 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019866 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019867}
19868
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19870
19871/*
19872 * Function given to ExpandGeneric() to obtain the list of user defined
19873 * function names.
19874 */
19875 char_u *
19876get_user_func_name(xp, idx)
19877 expand_T *xp;
19878 int idx;
19879{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019880 static long_u done;
19881 static hashitem_T *hi;
19882 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883
19884 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019886 done = 0;
19887 hi = func_hashtab.ht_array;
19888 }
19889 if (done < func_hashtab.ht_used)
19890 {
19891 if (done++ > 0)
19892 ++hi;
19893 while (HASHITEM_EMPTY(hi))
19894 ++hi;
19895 fp = HI2UF(hi);
19896
19897 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19898 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899
19900 cat_func_name(IObuff, fp);
19901 if (xp->xp_context != EXPAND_USER_FUNC)
19902 {
19903 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019904 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 STRCAT(IObuff, ")");
19906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 return IObuff;
19908 }
19909 return NULL;
19910}
19911
19912#endif /* FEAT_CMDL_COMPL */
19913
19914/*
19915 * Copy the function name of "fp" to buffer "buf".
19916 * "buf" must be able to hold the function name plus three bytes.
19917 * Takes care of script-local function names.
19918 */
19919 static void
19920cat_func_name(buf, fp)
19921 char_u *buf;
19922 ufunc_T *fp;
19923{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019924 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 {
19926 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019927 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 }
19929 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019930 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931}
19932
19933/*
19934 * ":delfunction {name}"
19935 */
19936 void
19937ex_delfunction(eap)
19938 exarg_T *eap;
19939{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019940 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 char_u *p;
19942 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019943 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944
19945 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019946 name = trans_function_name(&p, eap->skip, 0, &fudi);
19947 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019949 {
19950 if (fudi.fd_dict != NULL && !eap->skip)
19951 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 if (!ends_excmd(*skipwhite(p)))
19955 {
19956 vim_free(name);
19957 EMSG(_(e_trailing));
19958 return;
19959 }
19960 eap->nextcmd = check_nextcmd(p);
19961 if (eap->nextcmd != NULL)
19962 *p = NUL;
19963
19964 if (!eap->skip)
19965 fp = find_func(name);
19966 vim_free(name);
19967
19968 if (!eap->skip)
19969 {
19970 if (fp == NULL)
19971 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019972 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 return;
19974 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019975 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 {
19977 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19978 return;
19979 }
19980
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019981 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019983 /* Delete the dict item that refers to the function, it will
19984 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019985 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019987 else
19988 func_free(fp);
19989 }
19990}
19991
19992/*
19993 * Free a function and remove it from the list of functions.
19994 */
19995 static void
19996func_free(fp)
19997 ufunc_T *fp;
19998{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019999 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020000
20001 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020002 ga_clear_strings(&(fp->uf_args));
20003 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020004#ifdef FEAT_PROFILE
20005 vim_free(fp->uf_tml_count);
20006 vim_free(fp->uf_tml_total);
20007 vim_free(fp->uf_tml_self);
20008#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020009
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020010 /* remove the function from the function hashtable */
20011 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20012 if (HASHITEM_EMPTY(hi))
20013 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020014 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020015 hash_remove(&func_hashtab, hi);
20016
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020017 vim_free(fp);
20018}
20019
20020/*
20021 * Unreference a Function: decrement the reference count and free it when it
20022 * becomes zero. Only for numbered functions.
20023 */
20024 static void
20025func_unref(name)
20026 char_u *name;
20027{
20028 ufunc_T *fp;
20029
20030 if (name != NULL && isdigit(*name))
20031 {
20032 fp = find_func(name);
20033 if (fp == NULL)
20034 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020035 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020036 {
20037 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020038 * when "uf_calls" becomes zero. */
20039 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020040 func_free(fp);
20041 }
20042 }
20043}
20044
20045/*
20046 * Count a reference to a Function.
20047 */
20048 static void
20049func_ref(name)
20050 char_u *name;
20051{
20052 ufunc_T *fp;
20053
20054 if (name != NULL && isdigit(*name))
20055 {
20056 fp = find_func(name);
20057 if (fp == NULL)
20058 EMSG2(_(e_intern2), "func_ref()");
20059 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020060 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 }
20062}
20063
20064/*
20065 * Call a user function.
20066 */
20067 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020068call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 ufunc_T *fp; /* pointer to function */
20070 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020071 typval_T *argvars; /* arguments */
20072 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 linenr_T firstline; /* first line of range */
20074 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020075 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076{
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 char_u *save_sourcing_name;
20078 linenr_T save_sourcing_lnum;
20079 scid_T save_current_SID;
20080 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020081 int save_did_emsg;
20082 static int depth = 0;
20083 dictitem_T *v;
20084 int fixvar_idx = 0; /* index in fixvar[] */
20085 int i;
20086 int ai;
20087 char_u numbuf[NUMBUFLEN];
20088 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020089#ifdef FEAT_PROFILE
20090 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020091 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093
20094 /* If depth of calling is getting too high, don't execute the function */
20095 if (depth >= p_mfd)
20096 {
20097 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 rettv->v_type = VAR_NUMBER;
20099 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 return;
20101 }
20102 ++depth;
20103
20104 line_breakcheck(); /* check for CTRL-C hit */
20105
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020106 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020109 fc.rettv = rettv;
20110 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 fc.linenr = 0;
20112 fc.returned = FALSE;
20113 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020115 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 fc.dbg_tick = debug_tick;
20117
Bram Moolenaar33570922005-01-25 22:26:29 +000020118 /*
20119 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20120 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20121 * each argument variable and saves a lot of time.
20122 */
20123 /*
20124 * Init l: variables.
20125 */
20126 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020127 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020128 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020129 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20130 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020131 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020132 name = v->di_key;
20133 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20135 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20136 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020137 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020138 v->di_tv.vval.v_dict = selfdict;
20139 ++selfdict->dv_refcount;
20140 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020141
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 /*
20143 * Init a: variables.
20144 * Set a:0 to "argcount".
20145 * Set a:000 to a list with room for the "..." arguments.
20146 */
20147 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20148 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020149 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020150 v = &fc.fixvar[fixvar_idx++].var;
20151 STRCPY(v->di_key, "000");
20152 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20153 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20154 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020155 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 v->di_tv.vval.v_list = &fc.l_varlist;
20157 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20158 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020159 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020160
20161 /*
20162 * Set a:firstline to "firstline" and a:lastline to "lastline".
20163 * Set a:name to named arguments.
20164 * Set a:N to the "..." arguments.
20165 */
20166 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20167 (varnumber_T)firstline);
20168 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20169 (varnumber_T)lastline);
20170 for (i = 0; i < argcount; ++i)
20171 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020172 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 if (ai < 0)
20174 /* named argument a:name */
20175 name = FUNCARG(fp, i);
20176 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020177 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020178 /* "..." argument a:1, a:2, etc. */
20179 sprintf((char *)numbuf, "%d", ai + 1);
20180 name = numbuf;
20181 }
20182 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20183 {
20184 v = &fc.fixvar[fixvar_idx++].var;
20185 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20186 }
20187 else
20188 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020189 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20190 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020191 if (v == NULL)
20192 break;
20193 v->di_flags = DI_FLAGS_RO;
20194 }
20195 STRCPY(v->di_key, name);
20196 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20197
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020198 /* Note: the values are copied directly to avoid alloc/free.
20199 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020200 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020201 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020202
20203 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20204 {
20205 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20206 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020207 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020208 }
20209 }
20210
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 /* Don't redraw while executing the function. */
20212 ++RedrawingDisabled;
20213 save_sourcing_name = sourcing_name;
20214 save_sourcing_lnum = sourcing_lnum;
20215 sourcing_lnum = 1;
20216 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020217 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 if (sourcing_name != NULL)
20219 {
20220 if (save_sourcing_name != NULL
20221 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20222 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20223 else
20224 STRCPY(sourcing_name, "function ");
20225 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20226
20227 if (p_verbose >= 12)
20228 {
20229 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020230 verbose_enter_scroll();
20231
Bram Moolenaar555b2802005-05-19 21:08:39 +000020232 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 if (p_verbose >= 14)
20234 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020236 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020237 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020238 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239
20240 msg_puts((char_u *)"(");
20241 for (i = 0; i < argcount; ++i)
20242 {
20243 if (i > 0)
20244 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020245 if (argvars[i].v_type == VAR_NUMBER)
20246 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 else
20248 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020249 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20250 if (s != NULL)
20251 {
20252 trunc_string(s, buf, MSG_BUF_CLEN);
20253 msg_puts(buf);
20254 vim_free(tofree);
20255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 }
20257 }
20258 msg_puts((char_u *)")");
20259 }
20260 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020261
20262 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 --no_wait_return;
20264 }
20265 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020266#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020267 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020268 {
20269 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20270 func_do_profile(fp);
20271 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020272 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020273 {
20274 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020275 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020276 profile_zero(&fp->uf_tm_children);
20277 }
20278 script_prof_save(&wait_start);
20279 }
20280#endif
20281
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020283 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 save_did_emsg = did_emsg;
20285 did_emsg = FALSE;
20286
20287 /* call do_cmdline() to execute the lines */
20288 do_cmdline(NULL, get_func_line, (void *)&fc,
20289 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20290
20291 --RedrawingDisabled;
20292
20293 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020294 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020296 clear_tv(rettv);
20297 rettv->v_type = VAR_NUMBER;
20298 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 }
20300
Bram Moolenaar05159a02005-02-26 23:04:13 +000020301#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020302 if (do_profiling == PROF_YES && (fp->uf_profiling
20303 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020304 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020305 profile_end(&call_start);
20306 profile_sub_wait(&wait_start, &call_start);
20307 profile_add(&fp->uf_tm_total, &call_start);
20308 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020309 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020310 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020311 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20312 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020313 }
20314 }
20315#endif
20316
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 /* when being verbose, mention the return value */
20318 if (p_verbose >= 12)
20319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020321 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020324 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020325 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020326 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20327 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020328 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020330 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020331 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020332 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020333 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020334
Bram Moolenaar555b2802005-05-19 21:08:39 +000020335 /* The value may be very long. Skip the middle part, so that we
20336 * have some idea how it starts and ends. smsg() would always
20337 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020338 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20339 if (s != NULL)
20340 {
20341 trunc_string(s, buf, MSG_BUF_CLEN);
20342 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20343 vim_free(tofree);
20344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 }
20346 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020347
20348 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 --no_wait_return;
20350 }
20351
20352 vim_free(sourcing_name);
20353 sourcing_name = save_sourcing_name;
20354 sourcing_lnum = save_sourcing_lnum;
20355 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020356#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020357 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020358 script_prof_restore(&wait_start);
20359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360
20361 if (p_verbose >= 12 && sourcing_name != NULL)
20362 {
20363 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020364 verbose_enter_scroll();
20365
Bram Moolenaar555b2802005-05-19 21:08:39 +000020366 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020368
20369 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 --no_wait_return;
20371 }
20372
20373 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020374 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375
Bram Moolenaar33570922005-01-25 22:26:29 +000020376 /* The a: variables typevals were not alloced, only free the allocated
20377 * variables. */
20378 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20379
20380 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 --depth;
20382}
20383
20384/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020385 * Add a number variable "name" to dict "dp" with value "nr".
20386 */
20387 static void
20388add_nr_var(dp, v, name, nr)
20389 dict_T *dp;
20390 dictitem_T *v;
20391 char *name;
20392 varnumber_T nr;
20393{
20394 STRCPY(v->di_key, name);
20395 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20396 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20397 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020398 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020399 v->di_tv.vval.v_number = nr;
20400}
20401
20402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 * ":return [expr]"
20404 */
20405 void
20406ex_return(eap)
20407 exarg_T *eap;
20408{
20409 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020410 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 int returning = FALSE;
20412
20413 if (current_funccal == NULL)
20414 {
20415 EMSG(_("E133: :return not inside a function"));
20416 return;
20417 }
20418
20419 if (eap->skip)
20420 ++emsg_skip;
20421
20422 eap->nextcmd = NULL;
20423 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 {
20426 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020427 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020429 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 }
20431 /* It's safer to return also on error. */
20432 else if (!eap->skip)
20433 {
20434 /*
20435 * Return unless the expression evaluation has been cancelled due to an
20436 * aborting error, an interrupt, or an exception.
20437 */
20438 if (!aborting())
20439 returning = do_return(eap, FALSE, TRUE, NULL);
20440 }
20441
20442 /* When skipping or the return gets pending, advance to the next command
20443 * in this line (!returning). Otherwise, ignore the rest of the line.
20444 * Following lines will be ignored by get_func_line(). */
20445 if (returning)
20446 eap->nextcmd = NULL;
20447 else if (eap->nextcmd == NULL) /* no argument */
20448 eap->nextcmd = check_nextcmd(arg);
20449
20450 if (eap->skip)
20451 --emsg_skip;
20452}
20453
20454/*
20455 * Return from a function. Possibly makes the return pending. Also called
20456 * for a pending return at the ":endtry" or after returning from an extra
20457 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020458 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020459 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 * FALSE when the return gets pending.
20461 */
20462 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020463do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 exarg_T *eap;
20465 int reanimate;
20466 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020467 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468{
20469 int idx;
20470 struct condstack *cstack = eap->cstack;
20471
20472 if (reanimate)
20473 /* Undo the return. */
20474 current_funccal->returned = FALSE;
20475
20476 /*
20477 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20478 * not in its finally clause (which then is to be executed next) is found.
20479 * In this case, make the ":return" pending for execution at the ":endtry".
20480 * Otherwise, return normally.
20481 */
20482 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20483 if (idx >= 0)
20484 {
20485 cstack->cs_pending[idx] = CSTP_RETURN;
20486
20487 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020488 /* A pending return again gets pending. "rettv" points to an
20489 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020491 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 else
20493 {
20494 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020497 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020499 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 {
20501 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020502 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020503 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504 else
20505 EMSG(_(e_outofmem));
20506 }
20507 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020508 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509
20510 if (reanimate)
20511 {
20512 /* The pending return value could be overwritten by a ":return"
20513 * without argument in a finally clause; reset the default
20514 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020515 current_funccal->rettv->v_type = VAR_NUMBER;
20516 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 }
20518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 }
20521 else
20522 {
20523 current_funccal->returned = TRUE;
20524
20525 /* If the return is carried out now, store the return value. For
20526 * a return immediately after reanimation, the value is already
20527 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020528 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020530 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020531 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020533 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 }
20535 }
20536
20537 return idx < 0;
20538}
20539
20540/*
20541 * Free the variable with a pending return value.
20542 */
20543 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020544discard_pending_return(rettv)
20545 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546{
Bram Moolenaar33570922005-01-25 22:26:29 +000020547 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548}
20549
20550/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 * is an allocated string. Used by report_pending() for verbose messages.
20553 */
20554 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020555get_return_cmd(rettv)
20556 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020558 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020559 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020560 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020562 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020563 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020564 if (s == NULL)
20565 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020566
20567 STRCPY(IObuff, ":return ");
20568 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20569 if (STRLEN(s) + 8 >= IOSIZE)
20570 STRCPY(IObuff + IOSIZE - 4, "...");
20571 vim_free(tofree);
20572 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573}
20574
20575/*
20576 * Get next function line.
20577 * Called by do_cmdline() to get the next line.
20578 * Returns allocated string, or NULL for end of function.
20579 */
20580/* ARGSUSED */
20581 char_u *
20582get_func_line(c, cookie, indent)
20583 int c; /* not used */
20584 void *cookie;
20585 int indent; /* not used */
20586{
Bram Moolenaar33570922005-01-25 22:26:29 +000020587 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020588 ufunc_T *fp = fcp->func;
20589 char_u *retval;
20590 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591
20592 /* If breakpoints have been added/deleted need to check for it. */
20593 if (fcp->dbg_tick != debug_tick)
20594 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020595 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 sourcing_lnum);
20597 fcp->dbg_tick = debug_tick;
20598 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020599#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020600 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020601 func_line_end(cookie);
20602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603
Bram Moolenaar05159a02005-02-26 23:04:13 +000020604 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020605 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20606 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 retval = NULL;
20608 else
20609 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020610 /* Skip NULL lines (continuation lines). */
20611 while (fcp->linenr < gap->ga_len
20612 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20613 ++fcp->linenr;
20614 if (fcp->linenr >= gap->ga_len)
20615 retval = NULL;
20616 else
20617 {
20618 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20619 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020620#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020621 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020622 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020623#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 }
20626
20627 /* Did we encounter a breakpoint? */
20628 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20629 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020630 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020632 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 sourcing_lnum);
20634 fcp->dbg_tick = debug_tick;
20635 }
20636
20637 return retval;
20638}
20639
Bram Moolenaar05159a02005-02-26 23:04:13 +000020640#if defined(FEAT_PROFILE) || defined(PROTO)
20641/*
20642 * Called when starting to read a function line.
20643 * "sourcing_lnum" must be correct!
20644 * When skipping lines it may not actually be executed, but we won't find out
20645 * until later and we need to store the time now.
20646 */
20647 void
20648func_line_start(cookie)
20649 void *cookie;
20650{
20651 funccall_T *fcp = (funccall_T *)cookie;
20652 ufunc_T *fp = fcp->func;
20653
20654 if (fp->uf_profiling && sourcing_lnum >= 1
20655 && sourcing_lnum <= fp->uf_lines.ga_len)
20656 {
20657 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020658 /* Skip continuation lines. */
20659 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20660 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020661 fp->uf_tml_execed = FALSE;
20662 profile_start(&fp->uf_tml_start);
20663 profile_zero(&fp->uf_tml_children);
20664 profile_get_wait(&fp->uf_tml_wait);
20665 }
20666}
20667
20668/*
20669 * Called when actually executing a function line.
20670 */
20671 void
20672func_line_exec(cookie)
20673 void *cookie;
20674{
20675 funccall_T *fcp = (funccall_T *)cookie;
20676 ufunc_T *fp = fcp->func;
20677
20678 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20679 fp->uf_tml_execed = TRUE;
20680}
20681
20682/*
20683 * Called when done with a function line.
20684 */
20685 void
20686func_line_end(cookie)
20687 void *cookie;
20688{
20689 funccall_T *fcp = (funccall_T *)cookie;
20690 ufunc_T *fp = fcp->func;
20691
20692 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20693 {
20694 if (fp->uf_tml_execed)
20695 {
20696 ++fp->uf_tml_count[fp->uf_tml_idx];
20697 profile_end(&fp->uf_tml_start);
20698 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020699 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020700 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20701 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020702 }
20703 fp->uf_tml_idx = -1;
20704 }
20705}
20706#endif
20707
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708/*
20709 * Return TRUE if the currently active function should be ended, because a
20710 * return was encountered or an error occured. Used inside a ":while".
20711 */
20712 int
20713func_has_ended(cookie)
20714 void *cookie;
20715{
Bram Moolenaar33570922005-01-25 22:26:29 +000020716 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717
20718 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20719 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020720 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 || fcp->returned);
20722}
20723
20724/*
20725 * return TRUE if cookie indicates a function which "abort"s on errors.
20726 */
20727 int
20728func_has_abort(cookie)
20729 void *cookie;
20730{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020731 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732}
20733
20734#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20735typedef enum
20736{
20737 VAR_FLAVOUR_DEFAULT,
20738 VAR_FLAVOUR_SESSION,
20739 VAR_FLAVOUR_VIMINFO
20740} var_flavour_T;
20741
20742static var_flavour_T var_flavour __ARGS((char_u *varname));
20743
20744 static var_flavour_T
20745var_flavour(varname)
20746 char_u *varname;
20747{
20748 char_u *p = varname;
20749
20750 if (ASCII_ISUPPER(*p))
20751 {
20752 while (*(++p))
20753 if (ASCII_ISLOWER(*p))
20754 return VAR_FLAVOUR_SESSION;
20755 return VAR_FLAVOUR_VIMINFO;
20756 }
20757 else
20758 return VAR_FLAVOUR_DEFAULT;
20759}
20760#endif
20761
20762#if defined(FEAT_VIMINFO) || defined(PROTO)
20763/*
20764 * Restore global vars that start with a capital from the viminfo file
20765 */
20766 int
20767read_viminfo_varlist(virp, writing)
20768 vir_T *virp;
20769 int writing;
20770{
20771 char_u *tab;
20772 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020773 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774
20775 if (!writing && (find_viminfo_parameter('!') != NULL))
20776 {
20777 tab = vim_strchr(virp->vir_line + 1, '\t');
20778 if (tab != NULL)
20779 {
20780 *tab++ = '\0'; /* isolate the variable name */
20781 if (*tab == 'S') /* string var */
20782 is_string = TRUE;
20783
20784 tab = vim_strchr(tab, '\t');
20785 if (tab != NULL)
20786 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 if (is_string)
20788 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020789 tv.v_type = VAR_STRING;
20790 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 }
20793 else
20794 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020795 tv.v_type = VAR_NUMBER;
20796 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020798 set_var(virp->vir_line + 1, &tv, FALSE);
20799 if (is_string)
20800 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 }
20802 }
20803 }
20804
20805 return viminfo_readline(virp);
20806}
20807
20808/*
20809 * Write global vars that start with a capital to the viminfo file
20810 */
20811 void
20812write_viminfo_varlist(fp)
20813 FILE *fp;
20814{
Bram Moolenaar33570922005-01-25 22:26:29 +000020815 hashitem_T *hi;
20816 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020817 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020818 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020819 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020820 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020821 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822
20823 if (find_viminfo_parameter('!') == NULL)
20824 return;
20825
20826 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020827
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020828 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020829 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020831 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020833 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020834 this_var = HI2DI(hi);
20835 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020836 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020837 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020838 {
20839 case VAR_STRING: s = "STR"; break;
20840 case VAR_NUMBER: s = "NUM"; break;
20841 default: continue;
20842 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020843 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020844 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020845 if (p != NULL)
20846 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020847 vim_free(tofree);
20848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 }
20850 }
20851}
20852#endif
20853
20854#if defined(FEAT_SESSION) || defined(PROTO)
20855 int
20856store_session_globals(fd)
20857 FILE *fd;
20858{
Bram Moolenaar33570922005-01-25 22:26:29 +000020859 hashitem_T *hi;
20860 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020861 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 char_u *p, *t;
20863
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020864 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020865 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020867 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020869 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020870 this_var = HI2DI(hi);
20871 if ((this_var->di_tv.v_type == VAR_NUMBER
20872 || this_var->di_tv.v_type == VAR_STRING)
20873 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020874 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020875 /* Escape special characters with a backslash. Turn a LF and
20876 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020877 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020878 (char_u *)"\\\"\n\r");
20879 if (p == NULL) /* out of memory */
20880 break;
20881 for (t = p; *t != NUL; ++t)
20882 if (*t == '\n')
20883 *t = 'n';
20884 else if (*t == '\r')
20885 *t = 'r';
20886 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020887 this_var->di_key,
20888 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20889 : ' ',
20890 p,
20891 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20892 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020893 || put_eol(fd) == FAIL)
20894 {
20895 vim_free(p);
20896 return FAIL;
20897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 vim_free(p);
20899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 }
20901 }
20902 return OK;
20903}
20904#endif
20905
Bram Moolenaar661b1822005-07-28 22:36:45 +000020906/*
20907 * Display script name where an item was last set.
20908 * Should only be invoked when 'verbose' is non-zero.
20909 */
20910 void
20911last_set_msg(scriptID)
20912 scid_T scriptID;
20913{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020914 char_u *p;
20915
Bram Moolenaar661b1822005-07-28 22:36:45 +000020916 if (scriptID != 0)
20917 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020918 p = home_replace_save(NULL, get_scriptname(scriptID));
20919 if (p != NULL)
20920 {
20921 verbose_enter();
20922 MSG_PUTS(_("\n\tLast set from "));
20923 MSG_PUTS(p);
20924 vim_free(p);
20925 verbose_leave();
20926 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020927 }
20928}
20929
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930#endif /* FEAT_EVAL */
20931
20932#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20933
20934
20935#ifdef WIN3264
20936/*
20937 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20938 */
20939static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20940static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20941static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20942
20943/*
20944 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020945 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 */
20947 static int
20948get_short_pathname(fnamep, bufp, fnamelen)
20949 char_u **fnamep;
20950 char_u **bufp;
20951 int *fnamelen;
20952{
20953 int l,len;
20954 char_u *newbuf;
20955
20956 len = *fnamelen;
20957
20958 l = GetShortPathName(*fnamep, *fnamep, len);
20959 if (l > len - 1)
20960 {
20961 /* If that doesn't work (not enough space), then save the string
20962 * and try again with a new buffer big enough
20963 */
20964 newbuf = vim_strnsave(*fnamep, l);
20965 if (newbuf == NULL)
20966 return 0;
20967
20968 vim_free(*bufp);
20969 *fnamep = *bufp = newbuf;
20970
20971 l = GetShortPathName(*fnamep,*fnamep,l+1);
20972
20973 /* Really should always succeed, as the buffer is big enough */
20974 }
20975
20976 *fnamelen = l;
20977 return 1;
20978}
20979
20980/*
20981 * Create a short path name. Returns the length of the buffer it needs.
20982 * Doesn't copy over the end of the buffer passed in.
20983 */
20984 static int
20985shortpath_for_invalid_fname(fname, bufp, fnamelen)
20986 char_u **fname;
20987 char_u **bufp;
20988 int *fnamelen;
20989{
20990 char_u *s, *p, *pbuf2, *pbuf3;
20991 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020992 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993
20994 /* Make a copy */
20995 len2 = *fnamelen;
20996 pbuf2 = vim_strnsave(*fname, len2);
20997 pbuf3 = NULL;
20998
20999 s = pbuf2 + len2 - 1; /* Find the end */
21000 slen = 1;
21001 plen = len2;
21002
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021003 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 {
21005 --s;
21006 ++slen;
21007 --plen;
21008 }
21009
21010 do
21011 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021012 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021013 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 {
21015 --s;
21016 ++slen;
21017 --plen;
21018 }
21019 if (s <= pbuf2)
21020 break;
21021
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021022 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023 ch = *s;
21024 *s = 0; /* get_short_pathname requires a null-terminated string */
21025
21026 /* Try it in situ */
21027 p = pbuf2;
21028 if (!get_short_pathname(&p, &pbuf3, &plen))
21029 {
21030 vim_free(pbuf2);
21031 return -1;
21032 }
21033 *s = ch; /* Preserve the string */
21034 } while (plen == 0);
21035
21036 if (plen > 0)
21037 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021038 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 *fnamelen = len = plen + slen;
21040 vim_free(*bufp);
21041 if (len > len2)
21042 {
21043 /* If there's not enough space in the currently allocated string,
21044 * then copy it to a buffer big enough.
21045 */
21046 *fname= *bufp = vim_strnsave(p, len);
21047 if (*fname == NULL)
21048 return -1;
21049 }
21050 else
21051 {
21052 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21053 *fname = *bufp = pbuf2;
21054 if (p != pbuf2)
21055 strncpy(*fname, p, plen);
21056 pbuf2 = NULL;
21057 }
21058 /* Concat the next bit */
21059 strncpy(*fname + plen, s, slen);
21060 (*fname)[len] = '\0';
21061 }
21062 vim_free(pbuf3);
21063 vim_free(pbuf2);
21064 return 0;
21065}
21066
21067/*
21068 * Get a pathname for a partial path.
21069 */
21070 static int
21071shortpath_for_partial(fnamep, bufp, fnamelen)
21072 char_u **fnamep;
21073 char_u **bufp;
21074 int *fnamelen;
21075{
21076 int sepcount, len, tflen;
21077 char_u *p;
21078 char_u *pbuf, *tfname;
21079 int hasTilde;
21080
21081 /* Count up the path seperators from the RHS.. so we know which part
21082 * of the path to return.
21083 */
21084 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021085 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 if (vim_ispathsep(*p))
21087 ++sepcount;
21088
21089 /* Need full path first (use expand_env() to remove a "~/") */
21090 hasTilde = (**fnamep == '~');
21091 if (hasTilde)
21092 pbuf = tfname = expand_env_save(*fnamep);
21093 else
21094 pbuf = tfname = FullName_save(*fnamep, FALSE);
21095
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021096 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097
21098 if (!get_short_pathname(&tfname, &pbuf, &len))
21099 return -1;
21100
21101 if (len == 0)
21102 {
21103 /* Don't have a valid filename, so shorten the rest of the
21104 * path if we can. This CAN give us invalid 8.3 filenames, but
21105 * there's not a lot of point in guessing what it might be.
21106 */
21107 len = tflen;
21108 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21109 return -1;
21110 }
21111
21112 /* Count the paths backward to find the beginning of the desired string. */
21113 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021114 {
21115#ifdef FEAT_MBYTE
21116 if (has_mbyte)
21117 p -= mb_head_off(tfname, p);
21118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 if (vim_ispathsep(*p))
21120 {
21121 if (sepcount == 0 || (hasTilde && sepcount == 1))
21122 break;
21123 else
21124 sepcount --;
21125 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 if (hasTilde)
21128 {
21129 --p;
21130 if (p >= tfname)
21131 *p = '~';
21132 else
21133 return -1;
21134 }
21135 else
21136 ++p;
21137
21138 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21139 vim_free(*bufp);
21140 *fnamelen = (int)STRLEN(p);
21141 *bufp = pbuf;
21142 *fnamep = p;
21143
21144 return 0;
21145}
21146#endif /* WIN3264 */
21147
21148/*
21149 * Adjust a filename, according to a string of modifiers.
21150 * *fnamep must be NUL terminated when called. When returning, the length is
21151 * determined by *fnamelen.
21152 * Returns valid flags.
21153 * When there is an error, *fnamep is set to NULL.
21154 */
21155 int
21156modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21157 char_u *src; /* string with modifiers */
21158 int *usedlen; /* characters after src that are used */
21159 char_u **fnamep; /* file name so far */
21160 char_u **bufp; /* buffer for allocated file name or NULL */
21161 int *fnamelen; /* length of fnamep */
21162{
21163 int valid = 0;
21164 char_u *tail;
21165 char_u *s, *p, *pbuf;
21166 char_u dirname[MAXPATHL];
21167 int c;
21168 int has_fullname = 0;
21169#ifdef WIN3264
21170 int has_shortname = 0;
21171#endif
21172
21173repeat:
21174 /* ":p" - full path/file_name */
21175 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21176 {
21177 has_fullname = 1;
21178
21179 valid |= VALID_PATH;
21180 *usedlen += 2;
21181
21182 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21183 if ((*fnamep)[0] == '~'
21184#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21185 && ((*fnamep)[1] == '/'
21186# ifdef BACKSLASH_IN_FILENAME
21187 || (*fnamep)[1] == '\\'
21188# endif
21189 || (*fnamep)[1] == NUL)
21190
21191#endif
21192 )
21193 {
21194 *fnamep = expand_env_save(*fnamep);
21195 vim_free(*bufp); /* free any allocated file name */
21196 *bufp = *fnamep;
21197 if (*fnamep == NULL)
21198 return -1;
21199 }
21200
21201 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021202 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 {
21204 if (vim_ispathsep(*p)
21205 && p[1] == '.'
21206 && (p[2] == NUL
21207 || vim_ispathsep(p[2])
21208 || (p[2] == '.'
21209 && (p[3] == NUL || vim_ispathsep(p[3])))))
21210 break;
21211 }
21212
21213 /* FullName_save() is slow, don't use it when not needed. */
21214 if (*p != NUL || !vim_isAbsName(*fnamep))
21215 {
21216 *fnamep = FullName_save(*fnamep, *p != NUL);
21217 vim_free(*bufp); /* free any allocated file name */
21218 *bufp = *fnamep;
21219 if (*fnamep == NULL)
21220 return -1;
21221 }
21222
21223 /* Append a path separator to a directory. */
21224 if (mch_isdir(*fnamep))
21225 {
21226 /* Make room for one or two extra characters. */
21227 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21228 vim_free(*bufp); /* free any allocated file name */
21229 *bufp = *fnamep;
21230 if (*fnamep == NULL)
21231 return -1;
21232 add_pathsep(*fnamep);
21233 }
21234 }
21235
21236 /* ":." - path relative to the current directory */
21237 /* ":~" - path relative to the home directory */
21238 /* ":8" - shortname path - postponed till after */
21239 while (src[*usedlen] == ':'
21240 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21241 {
21242 *usedlen += 2;
21243 if (c == '8')
21244 {
21245#ifdef WIN3264
21246 has_shortname = 1; /* Postpone this. */
21247#endif
21248 continue;
21249 }
21250 pbuf = NULL;
21251 /* Need full path first (use expand_env() to remove a "~/") */
21252 if (!has_fullname)
21253 {
21254 if (c == '.' && **fnamep == '~')
21255 p = pbuf = expand_env_save(*fnamep);
21256 else
21257 p = pbuf = FullName_save(*fnamep, FALSE);
21258 }
21259 else
21260 p = *fnamep;
21261
21262 has_fullname = 0;
21263
21264 if (p != NULL)
21265 {
21266 if (c == '.')
21267 {
21268 mch_dirname(dirname, MAXPATHL);
21269 s = shorten_fname(p, dirname);
21270 if (s != NULL)
21271 {
21272 *fnamep = s;
21273 if (pbuf != NULL)
21274 {
21275 vim_free(*bufp); /* free any allocated file name */
21276 *bufp = pbuf;
21277 pbuf = NULL;
21278 }
21279 }
21280 }
21281 else
21282 {
21283 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21284 /* Only replace it when it starts with '~' */
21285 if (*dirname == '~')
21286 {
21287 s = vim_strsave(dirname);
21288 if (s != NULL)
21289 {
21290 *fnamep = s;
21291 vim_free(*bufp);
21292 *bufp = s;
21293 }
21294 }
21295 }
21296 vim_free(pbuf);
21297 }
21298 }
21299
21300 tail = gettail(*fnamep);
21301 *fnamelen = (int)STRLEN(*fnamep);
21302
21303 /* ":h" - head, remove "/file_name", can be repeated */
21304 /* Don't remove the first "/" or "c:\" */
21305 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21306 {
21307 valid |= VALID_HEAD;
21308 *usedlen += 2;
21309 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021310 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021311 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 *fnamelen = (int)(tail - *fnamep);
21313#ifdef VMS
21314 if (*fnamelen > 0)
21315 *fnamelen += 1; /* the path separator is part of the path */
21316#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021317 if (*fnamelen == 0)
21318 {
21319 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21320 p = vim_strsave((char_u *)".");
21321 if (p == NULL)
21322 return -1;
21323 vim_free(*bufp);
21324 *bufp = *fnamep = tail = p;
21325 *fnamelen = 1;
21326 }
21327 else
21328 {
21329 while (tail > s && !after_pathsep(s, tail))
21330 mb_ptr_back(*fnamep, tail);
21331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 }
21333
21334 /* ":8" - shortname */
21335 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21336 {
21337 *usedlen += 2;
21338#ifdef WIN3264
21339 has_shortname = 1;
21340#endif
21341 }
21342
21343#ifdef WIN3264
21344 /* Check shortname after we have done 'heads' and before we do 'tails'
21345 */
21346 if (has_shortname)
21347 {
21348 pbuf = NULL;
21349 /* Copy the string if it is shortened by :h */
21350 if (*fnamelen < (int)STRLEN(*fnamep))
21351 {
21352 p = vim_strnsave(*fnamep, *fnamelen);
21353 if (p == 0)
21354 return -1;
21355 vim_free(*bufp);
21356 *bufp = *fnamep = p;
21357 }
21358
21359 /* Split into two implementations - makes it easier. First is where
21360 * there isn't a full name already, second is where there is.
21361 */
21362 if (!has_fullname && !vim_isAbsName(*fnamep))
21363 {
21364 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21365 return -1;
21366 }
21367 else
21368 {
21369 int l;
21370
21371 /* Simple case, already have the full-name
21372 * Nearly always shorter, so try first time. */
21373 l = *fnamelen;
21374 if (!get_short_pathname(fnamep, bufp, &l))
21375 return -1;
21376
21377 if (l == 0)
21378 {
21379 /* Couldn't find the filename.. search the paths.
21380 */
21381 l = *fnamelen;
21382 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21383 return -1;
21384 }
21385 *fnamelen = l;
21386 }
21387 }
21388#endif /* WIN3264 */
21389
21390 /* ":t" - tail, just the basename */
21391 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21392 {
21393 *usedlen += 2;
21394 *fnamelen -= (int)(tail - *fnamep);
21395 *fnamep = tail;
21396 }
21397
21398 /* ":e" - extension, can be repeated */
21399 /* ":r" - root, without extension, can be repeated */
21400 while (src[*usedlen] == ':'
21401 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21402 {
21403 /* find a '.' in the tail:
21404 * - for second :e: before the current fname
21405 * - otherwise: The last '.'
21406 */
21407 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21408 s = *fnamep - 2;
21409 else
21410 s = *fnamep + *fnamelen - 1;
21411 for ( ; s > tail; --s)
21412 if (s[0] == '.')
21413 break;
21414 if (src[*usedlen + 1] == 'e') /* :e */
21415 {
21416 if (s > tail)
21417 {
21418 *fnamelen += (int)(*fnamep - (s + 1));
21419 *fnamep = s + 1;
21420#ifdef VMS
21421 /* cut version from the extension */
21422 s = *fnamep + *fnamelen - 1;
21423 for ( ; s > *fnamep; --s)
21424 if (s[0] == ';')
21425 break;
21426 if (s > *fnamep)
21427 *fnamelen = s - *fnamep;
21428#endif
21429 }
21430 else if (*fnamep <= tail)
21431 *fnamelen = 0;
21432 }
21433 else /* :r */
21434 {
21435 if (s > tail) /* remove one extension */
21436 *fnamelen = (int)(s - *fnamep);
21437 }
21438 *usedlen += 2;
21439 }
21440
21441 /* ":s?pat?foo?" - substitute */
21442 /* ":gs?pat?foo?" - global substitute */
21443 if (src[*usedlen] == ':'
21444 && (src[*usedlen + 1] == 's'
21445 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21446 {
21447 char_u *str;
21448 char_u *pat;
21449 char_u *sub;
21450 int sep;
21451 char_u *flags;
21452 int didit = FALSE;
21453
21454 flags = (char_u *)"";
21455 s = src + *usedlen + 2;
21456 if (src[*usedlen + 1] == 'g')
21457 {
21458 flags = (char_u *)"g";
21459 ++s;
21460 }
21461
21462 sep = *s++;
21463 if (sep)
21464 {
21465 /* find end of pattern */
21466 p = vim_strchr(s, sep);
21467 if (p != NULL)
21468 {
21469 pat = vim_strnsave(s, (int)(p - s));
21470 if (pat != NULL)
21471 {
21472 s = p + 1;
21473 /* find end of substitution */
21474 p = vim_strchr(s, sep);
21475 if (p != NULL)
21476 {
21477 sub = vim_strnsave(s, (int)(p - s));
21478 str = vim_strnsave(*fnamep, *fnamelen);
21479 if (sub != NULL && str != NULL)
21480 {
21481 *usedlen = (int)(p + 1 - src);
21482 s = do_string_sub(str, pat, sub, flags);
21483 if (s != NULL)
21484 {
21485 *fnamep = s;
21486 *fnamelen = (int)STRLEN(s);
21487 vim_free(*bufp);
21488 *bufp = s;
21489 didit = TRUE;
21490 }
21491 }
21492 vim_free(sub);
21493 vim_free(str);
21494 }
21495 vim_free(pat);
21496 }
21497 }
21498 /* after using ":s", repeat all the modifiers */
21499 if (didit)
21500 goto repeat;
21501 }
21502 }
21503
21504 return valid;
21505}
21506
21507/*
21508 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21509 * "flags" can be "g" to do a global substitute.
21510 * Returns an allocated string, NULL for error.
21511 */
21512 char_u *
21513do_string_sub(str, pat, sub, flags)
21514 char_u *str;
21515 char_u *pat;
21516 char_u *sub;
21517 char_u *flags;
21518{
21519 int sublen;
21520 regmatch_T regmatch;
21521 int i;
21522 int do_all;
21523 char_u *tail;
21524 garray_T ga;
21525 char_u *ret;
21526 char_u *save_cpo;
21527
21528 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21529 save_cpo = p_cpo;
21530 p_cpo = (char_u *)"";
21531
21532 ga_init2(&ga, 1, 200);
21533
21534 do_all = (flags[0] == 'g');
21535
21536 regmatch.rm_ic = p_ic;
21537 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21538 if (regmatch.regprog != NULL)
21539 {
21540 tail = str;
21541 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21542 {
21543 /*
21544 * Get some space for a temporary buffer to do the substitution
21545 * into. It will contain:
21546 * - The text up to where the match is.
21547 * - The substituted text.
21548 * - The text after the match.
21549 */
21550 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21551 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21552 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21553 {
21554 ga_clear(&ga);
21555 break;
21556 }
21557
21558 /* copy the text up to where the match is */
21559 i = (int)(regmatch.startp[0] - tail);
21560 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21561 /* add the substituted text */
21562 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21563 + ga.ga_len + i, TRUE, TRUE, FALSE);
21564 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 /* avoid getting stuck on a match with an empty string */
21566 if (tail == regmatch.endp[0])
21567 {
21568 if (*tail == NUL)
21569 break;
21570 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21571 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 }
21573 else
21574 {
21575 tail = regmatch.endp[0];
21576 if (*tail == NUL)
21577 break;
21578 }
21579 if (!do_all)
21580 break;
21581 }
21582
21583 if (ga.ga_data != NULL)
21584 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21585
21586 vim_free(regmatch.regprog);
21587 }
21588
21589 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21590 ga_clear(&ga);
21591 p_cpo = save_cpo;
21592
21593 return ret;
21594}
21595
21596#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */