blob: 1ad9cec1ef87435da80d263c76b5445b1bb11a3a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169 proftime_T uf_tm_children; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count; /* nr of times line was executed */
172 proftime_T *uf_tml_total; /* time spend in a line + children */
173 proftime_T *uf_tml_self; /* time spend in a line itself */
174 proftime_T uf_tml_start; /* start time for current line */
175 proftime_T uf_tml_children; /* time spent in children for this line */
176 proftime_T uf_tml_wait; /* start wait time for current line */
177 int uf_tml_idx; /* index of line being timed; -1 if none */
178 int uf_tml_execed; /* line being timed was executed */
179#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000180 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000182 int uf_refcount; /* for numbered function: reference count */
183 char_u uf_name[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
185 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186};
187
188/* function flags */
189#define FC_ABORT 1 /* abort function on error */
190#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000191#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000194 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000196static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000198/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000199static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000344 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000345 {VV_NAME("mouse_win", VAR_NUMBER), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000348 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000349};
350
351/* shorthand */
352#define vv_type vv_di.di_tv.v_type
353#define vv_nr vv_di.di_tv.vval.v_number
354#define vv_str vv_di.di_tv.vval.v_string
355#define vv_tv vv_di.di_tv
356
357/*
358 * The v: variables are stored in dictionary "vimvardict".
359 * "vimvars_var" is the variable that is used for the "l:" scope.
360 */
361static dict_T vimvardict;
362static dictitem_T vimvars_var;
363#define vimvarht vimvardict.dv_hashtab
364
Bram Moolenaara40058a2005-07-11 22:42:07 +0000365static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
366static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
367#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
368static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
369#endif
370static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
371static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
372static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000373static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
374static void list_glob_vars __ARGS((int *first));
375static void list_buf_vars __ARGS((int *first));
376static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000377#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000378static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000379#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000380static void list_vim_vars __ARGS((int *first));
381static void list_script_vars __ARGS((int *first));
382static void list_func_vars __ARGS((int *first));
383static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000384static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
385static int check_changedtick __ARGS((char_u *arg));
386static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
387static void clear_lval __ARGS((lval_T *lp));
388static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
389static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
390static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
391static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
392static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
393static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
394static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
395static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
396static void item_lock __ARGS((typval_T *tv, int deep, int lock));
397static int tv_islocked __ARGS((typval_T *tv));
398
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
400static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000408static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000413static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static listitem_T *listitem_alloc __ARGS((void));
415static void listitem_free __ARGS((listitem_T *item));
416static void listitem_remove __ARGS((list_T *l, listitem_T *item));
417static long list_len __ARGS((list_T *l));
418static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
419static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
420static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void list_append __ARGS((list_T *l, listitem_T *item));
425static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000426static int list_append_string __ARGS((list_T *l, char_u *str, int len));
427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000433static char_u *list2string __ARGS((typval_T *tv, int copyID));
434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000435static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
436static void set_ref_in_list __ARGS((list_T *l, int copyID));
437static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000439static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static dictitem_T *dictitem_alloc __ARGS((char_u *key));
441static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
442static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
443static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000444static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int dict_add __ARGS((dict_T *d, dictitem_T *item));
446static long dict_len __ARGS((dict_T *d));
447static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
453static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
454static int find_internal_func __ARGS((char_u *name));
455static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
456static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
457static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000458static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000476static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000479static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000482static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000483static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
491static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000504static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000510static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000519static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000521static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000527static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000535static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000536static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000537static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000538static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000541static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000549static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000563static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000569static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000585static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000586static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000587static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000589static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000593#ifdef vim_mkdir
594static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
595#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000599static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000601static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000602static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000604static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000605static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000618static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000620static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000627static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000629static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000630static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000632static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000634static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000637static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000638static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000641static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642#ifdef HAVE_STRFTIME
643static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
645static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000656static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000658static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000659static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000660static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000661static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000662static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000664static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000678static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000681static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000684static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static int get_env_len __ARGS((char_u **arg));
686static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000687static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000688static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
689#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
690#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
691 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000692static 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 +0000693static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000694static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000695static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
696static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static typval_T *alloc_tv __ARGS((void));
698static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void init_tv __ARGS((typval_T *varp));
700static long get_tv_number __ARGS((typval_T *varp));
701static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000702static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static char_u *get_tv_string __ARGS((typval_T *varp));
704static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000705static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000707static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
709static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
710static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000711static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
712static 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 +0000713static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
714static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000715static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000716static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000718static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
720static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
721static int eval_fname_script __ARGS((char_u *p));
722static int eval_fname_sid __ARGS((char_u *p));
723static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static ufunc_T *find_func __ARGS((char_u *name));
725static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000726static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000727#ifdef FEAT_PROFILE
728static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000729static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
730static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
731static int
732# ifdef __BORLANDC__
733 _RTLENTRYF
734# endif
735 prof_total_cmp __ARGS((const void *s1, const void *s2));
736static int
737# ifdef __BORLANDC__
738 _RTLENTRYF
739# endif
740 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000741#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000742static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000743static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000744static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void func_free __ARGS((ufunc_T *fp));
746static void func_unref __ARGS((char_u *name));
747static void func_ref __ARGS((char_u *name));
748static 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));
749static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000750static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
751static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000752static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000753static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000754static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000756/* Character used as separated in autoload function/variable names. */
757#define AUTOLOAD_CHAR '#'
758
Bram Moolenaar33570922005-01-25 22:26:29 +0000759/*
760 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000761 */
762 void
763eval_init()
764{
Bram Moolenaar33570922005-01-25 22:26:29 +0000765 int i;
766 struct vimvar *p;
767
768 init_var_dict(&globvardict, &globvars_var);
769 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000770 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000771 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000772
773 for (i = 0; i < VV_LEN; ++i)
774 {
775 p = &vimvars[i];
776 STRCPY(p->vv_di.di_key, p->vv_name);
777 if (p->vv_flags & VV_RO)
778 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
779 else if (p->vv_flags & VV_RO_SBX)
780 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
781 else
782 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000783
784 /* add to v: scope dict, unless the value is not always available */
785 if (p->vv_type != VAR_UNKNOWN)
786 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000787 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000788 /* add to compat scope dict */
789 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000790 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000791}
792
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000793#if defined(EXITFREE) || defined(PROTO)
794 void
795eval_clear()
796{
797 int i;
798 struct vimvar *p;
799
800 for (i = 0; i < VV_LEN; ++i)
801 {
802 p = &vimvars[i];
803 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000804 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000805 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000806 p->vv_di.di_tv.vval.v_string = NULL;
807 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000808 }
809 hash_clear(&vimvarht);
810 hash_clear(&compat_hashtab);
811
812 /* script-local variables */
813 for (i = 1; i <= ga_scripts.ga_len; ++i)
814 vars_clear(&SCRIPT_VARS(i));
815 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000816 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000817
818 /* global variables */
819 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000820
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000821 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000822 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000823 hash_clear(&func_hashtab);
824
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000825 /* autoloaded script names */
826 ga_clear_strings(&ga_loaded);
827
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000828 /* unreferenced lists and dicts */
829 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000830}
831#endif
832
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * Return the name of the executed function.
835 */
836 char_u *
837func_name(cookie)
838 void *cookie;
839{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000840 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841}
842
843/*
844 * Return the address holding the next breakpoint line for a funccall cookie.
845 */
846 linenr_T *
847func_breakpoint(cookie)
848 void *cookie;
849{
Bram Moolenaar33570922005-01-25 22:26:29 +0000850 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851}
852
853/*
854 * Return the address holding the debug tick for a funccall cookie.
855 */
856 int *
857func_dbg_tick(cookie)
858 void *cookie;
859{
Bram Moolenaar33570922005-01-25 22:26:29 +0000860 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862
863/*
864 * Return the nesting level for a funccall cookie.
865 */
866 int
867func_level(cookie)
868 void *cookie;
869{
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871}
872
873/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000874funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
876/*
877 * Return TRUE when a function was ended by a ":return" command.
878 */
879 int
880current_func_returned()
881{
882 return current_funccal->returned;
883}
884
885
886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 * Set an internal variable to a string value. Creates the variable if it does
888 * not already exist.
889 */
890 void
891set_internal_string_var(name, value)
892 char_u *name;
893 char_u *value;
894{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000895 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000896 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
898 val = vim_strsave(value);
899 if (val != NULL)
900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000901 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000902 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000904 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000905 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907 }
908}
909
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000911static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000912static char_u *redir_endp = NULL;
913static char_u *redir_varname = NULL;
914
915/*
916 * Start recording command output to a variable
917 * Returns OK if successfully completed the setup. FAIL otherwise.
918 */
919 int
920var_redir_start(name, append)
921 char_u *name;
922 int append; /* append to an existing variable */
923{
924 int save_emsg;
925 int err;
926 typval_T tv;
927
928 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000929 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000930 {
931 EMSG(_(e_invarg));
932 return FAIL;
933 }
934
935 redir_varname = vim_strsave(name);
936 if (redir_varname == NULL)
937 return FAIL;
938
939 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
940 if (redir_lval == NULL)
941 {
942 var_redir_stop();
943 return FAIL;
944 }
945
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000946 /* The output is stored in growarray "redir_ga" until redirection ends. */
947 ga_init2(&redir_ga, (int)sizeof(char), 500);
948
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000950 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
951 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000952 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
953 {
954 if (redir_endp != NULL && *redir_endp != NUL)
955 /* Trailing characters are present after the variable name */
956 EMSG(_(e_trailing));
957 else
958 EMSG(_(e_invarg));
959 var_redir_stop();
960 return FAIL;
961 }
962
963 /* check if we can write to the variable: set it to or append an empty
964 * string */
965 save_emsg = did_emsg;
966 did_emsg = FALSE;
967 tv.v_type = VAR_STRING;
968 tv.vval.v_string = (char_u *)"";
969 if (append)
970 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
971 else
972 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
973 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000974 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000975 if (err)
976 {
977 var_redir_stop();
978 return FAIL;
979 }
980 if (redir_lval->ll_newkey != NULL)
981 {
982 /* Dictionary item was created, don't do it again. */
983 vim_free(redir_lval->ll_newkey);
984 redir_lval->ll_newkey = NULL;
985 }
986
987 return OK;
988}
989
990/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000991 * Append "value[value_len]" to the variable set by var_redir_start().
992 * The actual appending is postponed until redirection ends, because the value
993 * appended may in fact be the string we write to, changing it may cause freed
994 * memory to be used:
995 * :redir => foo
996 * :let foo
997 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000998 */
999 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001000var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001002 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001003{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001004 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001005
1006 if (redir_lval == NULL)
1007 return;
1008
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001009 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001010 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001012 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001013
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001014 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001015 {
1016 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001017 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001018 }
1019 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021}
1022
1023/*
1024 * Stop redirecting command output to a variable.
1025 */
1026 void
1027var_redir_stop()
1028{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001029 typval_T tv;
1030
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031 if (redir_lval != NULL)
1032 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001033 /* Append the trailing NUL. */
1034 ga_append(&redir_ga, NUL);
1035
1036 /* Assign the text to the variable. */
1037 tv.v_type = VAR_STRING;
1038 tv.vval.v_string = redir_ga.ga_data;
1039 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1040 vim_free(tv.vval.v_string);
1041
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 clear_lval(redir_lval);
1043 vim_free(redir_lval);
1044 redir_lval = NULL;
1045 }
1046 vim_free(redir_varname);
1047 redir_varname = NULL;
1048}
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050# if defined(FEAT_MBYTE) || defined(PROTO)
1051 int
1052eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1053 char_u *enc_from;
1054 char_u *enc_to;
1055 char_u *fname_from;
1056 char_u *fname_to;
1057{
1058 int err = FALSE;
1059
1060 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1061 set_vim_var_string(VV_CC_TO, enc_to, -1);
1062 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1063 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1064 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1065 err = TRUE;
1066 set_vim_var_string(VV_CC_FROM, NULL, -1);
1067 set_vim_var_string(VV_CC_TO, NULL, -1);
1068 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1069 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1070
1071 if (err)
1072 return FAIL;
1073 return OK;
1074}
1075# endif
1076
1077# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1078 int
1079eval_printexpr(fname, args)
1080 char_u *fname;
1081 char_u *args;
1082{
1083 int err = FALSE;
1084
1085 set_vim_var_string(VV_FNAME_IN, fname, -1);
1086 set_vim_var_string(VV_CMDARG, args, -1);
1087 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1088 err = TRUE;
1089 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1090 set_vim_var_string(VV_CMDARG, NULL, -1);
1091
1092 if (err)
1093 {
1094 mch_remove(fname);
1095 return FAIL;
1096 }
1097 return OK;
1098}
1099# endif
1100
1101# if defined(FEAT_DIFF) || defined(PROTO)
1102 void
1103eval_diff(origfile, newfile, outfile)
1104 char_u *origfile;
1105 char_u *newfile;
1106 char_u *outfile;
1107{
1108 int err = FALSE;
1109
1110 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1111 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1112 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1113 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1114 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1115 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1116 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1117}
1118
1119 void
1120eval_patch(origfile, difffile, outfile)
1121 char_u *origfile;
1122 char_u *difffile;
1123 char_u *outfile;
1124{
1125 int err;
1126
1127 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1128 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1129 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1130 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1131 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1132 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1133 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1134}
1135# endif
1136
1137/*
1138 * Top level evaluation function, returning a boolean.
1139 * Sets "error" to TRUE if there was an error.
1140 * Return TRUE or FALSE.
1141 */
1142 int
1143eval_to_bool(arg, error, nextcmd, skip)
1144 char_u *arg;
1145 int *error;
1146 char_u **nextcmd;
1147 int skip; /* only parse, don't execute */
1148{
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 int retval = FALSE;
1151
1152 if (skip)
1153 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 else
1157 {
1158 *error = FALSE;
1159 if (!skip)
1160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001161 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001162 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
1164 }
1165 if (skip)
1166 --emsg_skip;
1167
1168 return retval;
1169}
1170
1171/*
1172 * Top level evaluation function, returning a string. If "skip" is TRUE,
1173 * only parsing to "nextcmd" is done, without reporting errors. Return
1174 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1175 */
1176 char_u *
1177eval_to_string_skip(arg, nextcmd, skip)
1178 char_u *arg;
1179 char_u **nextcmd;
1180 int skip; /* only parse, don't execute */
1181{
Bram Moolenaar33570922005-01-25 22:26:29 +00001182 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 char_u *retval;
1184
1185 if (skip)
1186 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 retval = NULL;
1189 else
1190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001191 retval = vim_strsave(get_tv_string(&tv));
1192 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 }
1194 if (skip)
1195 --emsg_skip;
1196
1197 return retval;
1198}
1199
1200/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001201 * Skip over an expression at "*pp".
1202 * Return FAIL for an error, OK otherwise.
1203 */
1204 int
1205skip_expr(pp)
1206 char_u **pp;
1207{
Bram Moolenaar33570922005-01-25 22:26:29 +00001208 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001209
1210 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001212}
1213
1214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 * Top level evaluation function, returning a string.
1216 * Return pointer to allocated memory, or NULL for failure.
1217 */
1218 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001219eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 char_u *arg;
1221 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001222 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223{
Bram Moolenaar33570922005-01-25 22:26:29 +00001224 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001226 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001228 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 retval = NULL;
1230 else
1231 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001232 if (dolist && tv.v_type == VAR_LIST)
1233 {
1234 ga_init2(&ga, (int)sizeof(char), 80);
1235 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1236 ga_append(&ga, NUL);
1237 retval = (char_u *)ga.ga_data;
1238 }
1239 else
1240 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001241 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243
1244 return retval;
1245}
1246
1247/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001248 * Call eval_to_string() without using current local variables and using
1249 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
1251 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001252eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 char_u *arg;
1254 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001255 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256{
1257 char_u *retval;
1258 void *save_funccalp;
1259
1260 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001261 if (use_sandbox)
1262 ++sandbox;
1263 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001264 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001265 if (use_sandbox)
1266 --sandbox;
1267 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 restore_funccal(save_funccalp);
1269 return retval;
1270}
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272/*
1273 * Top level evaluation function, returning a number.
1274 * Evaluates "expr" silently.
1275 * Returns -1 for an error.
1276 */
1277 int
1278eval_to_number(expr)
1279 char_u *expr;
1280{
Bram Moolenaar33570922005-01-25 22:26:29 +00001281 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001283 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
1285 ++emsg_off;
1286
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001287 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 retval = -1;
1289 else
1290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001291 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001292 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294 --emsg_off;
1295
1296 return retval;
1297}
1298
Bram Moolenaara40058a2005-07-11 22:42:07 +00001299/*
1300 * Prepare v: variable "idx" to be used.
1301 * Save the current typeval in "save_tv".
1302 * When not used yet add the variable to the v: hashtable.
1303 */
1304 static void
1305prepare_vimvar(idx, save_tv)
1306 int idx;
1307 typval_T *save_tv;
1308{
1309 *save_tv = vimvars[idx].vv_tv;
1310 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1311 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1312}
1313
1314/*
1315 * Restore v: variable "idx" to typeval "save_tv".
1316 * When no longer defined, remove the variable from the v: hashtable.
1317 */
1318 static void
1319restore_vimvar(idx, save_tv)
1320 int idx;
1321 typval_T *save_tv;
1322{
1323 hashitem_T *hi;
1324
Bram Moolenaara40058a2005-07-11 22:42:07 +00001325 vimvars[idx].vv_tv = *save_tv;
1326 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1327 {
1328 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1329 if (HASHITEM_EMPTY(hi))
1330 EMSG2(_(e_intern2), "restore_vimvar()");
1331 else
1332 hash_remove(&vimvarht, hi);
1333 }
1334}
1335
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001336#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001337/*
1338 * Evaluate an expression to a list with suggestions.
1339 * For the "expr:" part of 'spellsuggest'.
1340 */
1341 list_T *
1342eval_spell_expr(badword, expr)
1343 char_u *badword;
1344 char_u *expr;
1345{
1346 typval_T save_val;
1347 typval_T rettv;
1348 list_T *list = NULL;
1349 char_u *p = skipwhite(expr);
1350
1351 /* Set "v:val" to the bad word. */
1352 prepare_vimvar(VV_VAL, &save_val);
1353 vimvars[VV_VAL].vv_type = VAR_STRING;
1354 vimvars[VV_VAL].vv_str = badword;
1355 if (p_verbose == 0)
1356 ++emsg_off;
1357
1358 if (eval1(&p, &rettv, TRUE) == OK)
1359 {
1360 if (rettv.v_type != VAR_LIST)
1361 clear_tv(&rettv);
1362 else
1363 list = rettv.vval.v_list;
1364 }
1365
1366 if (p_verbose == 0)
1367 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001368 restore_vimvar(VV_VAL, &save_val);
1369
1370 return list;
1371}
1372
1373/*
1374 * "list" is supposed to contain two items: a word and a number. Return the
1375 * word in "pp" and the number as the return value.
1376 * Return -1 if anything isn't right.
1377 * Used to get the good word and score from the eval_spell_expr() result.
1378 */
1379 int
1380get_spellword(list, pp)
1381 list_T *list;
1382 char_u **pp;
1383{
1384 listitem_T *li;
1385
1386 li = list->lv_first;
1387 if (li == NULL)
1388 return -1;
1389 *pp = get_tv_string(&li->li_tv);
1390
1391 li = li->li_next;
1392 if (li == NULL)
1393 return -1;
1394 return get_tv_number(&li->li_tv);
1395}
1396#endif
1397
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001398/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001399 * Top level evaluation function.
1400 * Returns an allocated typval_T with the result.
1401 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001402 */
1403 typval_T *
1404eval_expr(arg, nextcmd)
1405 char_u *arg;
1406 char_u **nextcmd;
1407{
1408 typval_T *tv;
1409
1410 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001411 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001412 {
1413 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001414 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001415 }
1416
1417 return tv;
1418}
1419
1420
Bram Moolenaar4f688582007-07-24 12:34:30 +00001421#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1422 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001428 static int
1429call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 char_u *func;
1431 int argc;
1432 char_u **argv;
1433 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
Bram Moolenaar33570922005-01-25 22:26:29 +00001436 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 long n;
1438 int len;
1439 int i;
1440 int doesrange;
1441 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001444 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
1448 for (i = 0; i < argc; i++)
1449 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001450 /* Pass a NULL or empty argument as an empty string */
1451 if (argv[i] == NULL || *argv[i] == NUL)
1452 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001453 argvars[i].v_type = VAR_STRING;
1454 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001455 continue;
1456 }
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 /* Recognize a number argument, the others must be strings. */
1459 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1460 if (len != 0 && len == (int)STRLEN(argv[i]))
1461 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001462 argvars[i].v_type = VAR_NUMBER;
1463 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465 else
1466 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001467 argvars[i].v_type = VAR_STRING;
1468 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470 }
1471
1472 if (safe)
1473 {
1474 save_funccalp = save_funccal();
1475 ++sandbox;
1476 }
1477
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1479 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001481 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (safe)
1483 {
1484 --sandbox;
1485 restore_funccal(save_funccalp);
1486 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001487 vim_free(argvars);
1488
1489 if (ret == FAIL)
1490 clear_tv(rettv);
1491
1492 return ret;
1493}
1494
Bram Moolenaar4f688582007-07-24 12:34:30 +00001495# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001496/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001497 * Call vimL function "func" and return the result as a string.
1498 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001499 * Uses argv[argc] for the function arguments.
1500 */
1501 void *
1502call_func_retstr(func, argc, argv, safe)
1503 char_u *func;
1504 int argc;
1505 char_u **argv;
1506 int safe; /* use the sandbox */
1507{
1508 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001509 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001510
1511 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1512 return NULL;
1513
1514 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001515 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 return retval;
1517}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001518# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001519
Bram Moolenaar4f688582007-07-24 12:34:30 +00001520# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001521/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001522 * Call vimL function "func" and return the result as a number.
1523 * Returns -1 when calling the function fails.
1524 * Uses argv[argc] for the function arguments.
1525 */
1526 long
1527call_func_retnr(func, argc, argv, safe)
1528 char_u *func;
1529 int argc;
1530 char_u **argv;
1531 int safe; /* use the sandbox */
1532{
1533 typval_T rettv;
1534 long retval;
1535
1536 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1537 return -1;
1538
1539 retval = get_tv_number_chk(&rettv, NULL);
1540 clear_tv(&rettv);
1541 return retval;
1542}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001543# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001544
1545/*
1546 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001547 * Uses argv[argc] for the function arguments.
1548 */
1549 void *
1550call_func_retlist(func, argc, argv, safe)
1551 char_u *func;
1552 int argc;
1553 char_u **argv;
1554 int safe; /* use the sandbox */
1555{
1556 typval_T rettv;
1557
1558 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1559 return NULL;
1560
1561 if (rettv.v_type != VAR_LIST)
1562 {
1563 clear_tv(&rettv);
1564 return NULL;
1565 }
1566
1567 return rettv.vval.v_list;
1568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#endif
1570
Bram Moolenaar4f688582007-07-24 12:34:30 +00001571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572/*
1573 * Save the current function call pointer, and set it to NULL.
1574 * Used when executing autocommands and for ":source".
1575 */
1576 void *
1577save_funccal()
1578{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001579 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 current_funccal = NULL;
1582 return (void *)fc;
1583}
1584
1585 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001586restore_funccal(vfc)
1587 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001589 funccall_T *fc = (funccall_T *)vfc;
1590
1591 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592}
1593
Bram Moolenaar05159a02005-02-26 23:04:13 +00001594#if defined(FEAT_PROFILE) || defined(PROTO)
1595/*
1596 * Prepare profiling for entering a child or something else that is not
1597 * counted for the script/function itself.
1598 * Should always be called in pair with prof_child_exit().
1599 */
1600 void
1601prof_child_enter(tm)
1602 proftime_T *tm; /* place to store waittime */
1603{
1604 funccall_T *fc = current_funccal;
1605
1606 if (fc != NULL && fc->func->uf_profiling)
1607 profile_start(&fc->prof_child);
1608 script_prof_save(tm);
1609}
1610
1611/*
1612 * Take care of time spent in a child.
1613 * Should always be called after prof_child_enter().
1614 */
1615 void
1616prof_child_exit(tm)
1617 proftime_T *tm; /* where waittime was stored */
1618{
1619 funccall_T *fc = current_funccal;
1620
1621 if (fc != NULL && fc->func->uf_profiling)
1622 {
1623 profile_end(&fc->prof_child);
1624 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1625 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1626 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1627 }
1628 script_prof_restore(tm);
1629}
1630#endif
1631
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#ifdef FEAT_FOLDING
1634/*
1635 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1636 * it in "*cp". Doesn't give error messages.
1637 */
1638 int
1639eval_foldexpr(arg, cp)
1640 char_u *arg;
1641 int *cp;
1642{
Bram Moolenaar33570922005-01-25 22:26:29 +00001643 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 int retval;
1645 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001646 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1647 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001650 if (use_sandbox)
1651 ++sandbox;
1652 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 retval = 0;
1656 else
1657 {
1658 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659 if (tv.v_type == VAR_NUMBER)
1660 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001661 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 retval = 0;
1663 else
1664 {
1665 /* If the result is a string, check if there is a non-digit before
1666 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001667 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (!VIM_ISDIGIT(*s) && *s != '-')
1669 *cp = *s++;
1670 retval = atol((char *)s);
1671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 }
1674 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001675 if (use_sandbox)
1676 --sandbox;
1677 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 return retval;
1680}
1681#endif
1682
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 * ":let" list all variable values
1685 * ":let var1 var2" list variable values
1686 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001687 * ":let var += expr" assignment command.
1688 * ":let var -= expr" assignment command.
1689 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001690 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 */
1692 void
1693ex_let(eap)
1694 exarg_T *eap;
1695{
1696 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001698 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 int var_count = 0;
1701 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001703 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001704 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaardb552d602006-03-23 22:59:57 +00001706 argend = skip_var_list(arg, &var_count, &semicolon);
1707 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001709 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1710 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001711 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001714 /*
1715 * ":let" without "=": list variables
1716 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 if (*arg == '[')
1718 EMSG(_(e_invarg));
1719 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001721 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001724 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001725 list_glob_vars(&first);
1726 list_buf_vars(&first);
1727 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001728#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001729 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001730#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001731 list_script_vars(&first);
1732 list_func_vars(&first);
1733 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 eap->nextcmd = check_nextcmd(arg);
1736 }
1737 else
1738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 op[0] = '=';
1740 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001741 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001742 {
1743 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1744 op[0] = expr[-1]; /* +=, -= or .= */
1745 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (eap->skip)
1749 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 if (eap->skip)
1752 {
1753 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001754 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 --emsg_skip;
1756 }
1757 else if (i != FAIL)
1758 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001761 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
1763 }
1764}
1765
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766/*
1767 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1768 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 * When "nextchars" is not NULL it points to a string with characters that
1770 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1771 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 * Returns OK or FAIL;
1773 */
1774 static int
1775ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1776 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 int copy; /* copy values from "tv", don't move */
1779 int semicolon; /* from skip_var_list() */
1780 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001781 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782{
1783 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 listitem_T *item;
1787 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788
1789 if (*arg != '[')
1790 {
1791 /*
1792 * ":let var = expr" or ":for var in list"
1793 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001794 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 return FAIL;
1796 return OK;
1797 }
1798
1799 /*
1800 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1801 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001802 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 {
1804 EMSG(_(e_listreq));
1805 return FAIL;
1806 }
1807
1808 i = list_len(l);
1809 if (semicolon == 0 && var_count < i)
1810 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001811 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 return FAIL;
1813 }
1814 if (var_count - semicolon > i)
1815 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001816 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 return FAIL;
1818 }
1819
1820 item = l->lv_first;
1821 while (*arg != ']')
1822 {
1823 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001824 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 item = item->li_next;
1826 if (arg == NULL)
1827 return FAIL;
1828
1829 arg = skipwhite(arg);
1830 if (*arg == ';')
1831 {
1832 /* Put the rest of the list (may be empty) in the var after ';'.
1833 * Create a new list for this. */
1834 l = list_alloc();
1835 if (l == NULL)
1836 return FAIL;
1837 while (item != NULL)
1838 {
1839 list_append_tv(l, &item->li_tv);
1840 item = item->li_next;
1841 }
1842
1843 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001844 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 ltv.vval.v_list = l;
1846 l->lv_refcount = 1;
1847
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001848 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1849 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 clear_tv(&ltv);
1851 if (arg == NULL)
1852 return FAIL;
1853 break;
1854 }
1855 else if (*arg != ',' && *arg != ']')
1856 {
1857 EMSG2(_(e_intern2), "ex_let_vars()");
1858 return FAIL;
1859 }
1860 }
1861
1862 return OK;
1863}
1864
1865/*
1866 * Skip over assignable variable "var" or list of variables "[var, var]".
1867 * Used for ":let varvar = expr" and ":for varvar in expr".
1868 * For "[var, var]" increment "*var_count" for each variable.
1869 * for "[var, var; var]" set "semicolon".
1870 * Return NULL for an error.
1871 */
1872 static char_u *
1873skip_var_list(arg, var_count, semicolon)
1874 char_u *arg;
1875 int *var_count;
1876 int *semicolon;
1877{
1878 char_u *p, *s;
1879
1880 if (*arg == '[')
1881 {
1882 /* "[var, var]": find the matching ']'. */
1883 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001884 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 {
1886 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1887 s = skip_var_one(p);
1888 if (s == p)
1889 {
1890 EMSG2(_(e_invarg2), p);
1891 return NULL;
1892 }
1893 ++*var_count;
1894
1895 p = skipwhite(s);
1896 if (*p == ']')
1897 break;
1898 else if (*p == ';')
1899 {
1900 if (*semicolon == 1)
1901 {
1902 EMSG(_("Double ; in list of variables"));
1903 return NULL;
1904 }
1905 *semicolon = 1;
1906 }
1907 else if (*p != ',')
1908 {
1909 EMSG2(_(e_invarg2), p);
1910 return NULL;
1911 }
1912 }
1913 return p + 1;
1914 }
1915 else
1916 return skip_var_one(arg);
1917}
1918
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001919/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001920 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001921 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001922 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 static char_u *
1924skip_var_one(arg)
1925 char_u *arg;
1926{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001927 if (*arg == '@' && arg[1] != NUL)
1928 return arg + 2;
1929 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1930 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931}
1932
Bram Moolenaara7043832005-01-21 11:56:39 +00001933/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001934 * List variables for hashtab "ht" with prefix "prefix".
1935 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001938list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001940 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001941 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001942 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001943{
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 hashitem_T *hi;
1945 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001946 int todo;
1947
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001948 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001949 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1950 {
1951 if (!HASHITEM_EMPTY(hi))
1952 {
1953 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001954 di = HI2DI(hi);
1955 if (empty || di->di_tv.v_type != VAR_STRING
1956 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001957 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001958 }
1959 }
1960}
1961
1962/*
1963 * List global variables.
1964 */
1965 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001966list_glob_vars(first)
1967 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001968{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001969 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001970}
1971
1972/*
1973 * List buffer variables.
1974 */
1975 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001976list_buf_vars(first)
1977 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001978{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001979 char_u numbuf[NUMBUFLEN];
1980
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001981 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1982 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001983
1984 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001985 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1986 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001987}
1988
1989/*
1990 * List window variables.
1991 */
1992 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001993list_win_vars(first)
1994 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001995{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001996 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1997 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001998}
1999
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002000#ifdef FEAT_WINDOWS
2001/*
2002 * List tab page variables.
2003 */
2004 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002005list_tab_vars(first)
2006 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002007{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002008 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2009 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002010}
2011#endif
2012
Bram Moolenaara7043832005-01-21 11:56:39 +00002013/*
2014 * List Vim variables.
2015 */
2016 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002017list_vim_vars(first)
2018 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002020 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021}
2022
2023/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002024 * List script-local variables, if there is a script.
2025 */
2026 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002027list_script_vars(first)
2028 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002029{
2030 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002031 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2032 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002033}
2034
2035/*
2036 * List function variables, if there is a function.
2037 */
2038 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002039list_func_vars(first)
2040 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002041{
2042 if (current_funccal != NULL)
2043 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002044 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002045}
2046
2047/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 * List variables in "arg".
2049 */
2050 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002051list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 exarg_T *eap;
2053 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002054 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002055{
2056 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059 char_u *name_start;
2060 char_u *arg_subsc;
2061 char_u *tofree;
2062 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063
2064 while (!ends_excmd(*arg) && !got_int)
2065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002067 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002068 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002069 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2070 {
2071 emsg_severe = TRUE;
2072 EMSG(_(e_trailing));
2073 break;
2074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002078 /* get_name_len() takes care of expanding curly braces */
2079 name_start = name = arg;
2080 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2081 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002082 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 /* This is mainly to keep test 49 working: when expanding
2084 * curly braces fails overrule the exception error message. */
2085 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002087 emsg_severe = TRUE;
2088 EMSG2(_(e_invarg2), arg);
2089 break;
2090 }
2091 error = TRUE;
2092 }
2093 else
2094 {
2095 if (tofree != NULL)
2096 name = tofree;
2097 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 else
2100 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101 /* handle d.key, l[idx], f(expr) */
2102 arg_subsc = arg;
2103 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002108 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002110 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111 case 'g': list_glob_vars(first); break;
2112 case 'b': list_buf_vars(first); break;
2113 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002114#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002116#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117 case 'v': list_vim_vars(first); break;
2118 case 's': list_script_vars(first); break;
2119 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120 default:
2121 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002123 }
2124 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002125 {
2126 char_u numbuf[NUMBUFLEN];
2127 char_u *tf;
2128 int c;
2129 char_u *s;
2130
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002131 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 c = *arg;
2133 *arg = NUL;
2134 list_one_var_a((char_u *)"",
2135 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 tv.v_type,
2137 s == NULL ? (char_u *)"" : s,
2138 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002139 *arg = c;
2140 vim_free(tf);
2141 }
2142 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002144 }
2145 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002149
2150 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002151 }
2152
2153 return arg;
2154}
2155
2156/*
2157 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2158 * Returns a pointer to the char just after the var name.
2159 * Returns NULL if there is an error.
2160 */
2161 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002164 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002167 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168{
2169 int c1;
2170 char_u *name;
2171 char_u *p;
2172 char_u *arg_end = NULL;
2173 int len;
2174 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176
2177 /*
2178 * ":let $VAR = expr": Set environment variable.
2179 */
2180 if (*arg == '$')
2181 {
2182 /* Find the end of the name. */
2183 ++arg;
2184 name = arg;
2185 len = get_env_len(&arg);
2186 if (len == 0)
2187 EMSG2(_(e_invarg2), name - 1);
2188 else
2189 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002190 if (op != NULL && (*op == '+' || *op == '-'))
2191 EMSG2(_(e_letwrong), op);
2192 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002193 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 EMSG(_(e_letunexp));
2195 else
2196 {
2197 c1 = name[len];
2198 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002199 p = get_tv_string_chk(tv);
2200 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002201 {
2202 int mustfree = FALSE;
2203 char_u *s = vim_getenv(name, &mustfree);
2204
2205 if (s != NULL)
2206 {
2207 p = tofree = concat_str(s, p);
2208 if (mustfree)
2209 vim_free(s);
2210 }
2211 }
2212 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002213 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002214 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002215 if (STRICMP(name, "HOME") == 0)
2216 init_homedir();
2217 else if (didset_vim && STRICMP(name, "VIM") == 0)
2218 didset_vim = FALSE;
2219 else if (didset_vimruntime
2220 && STRICMP(name, "VIMRUNTIME") == 0)
2221 didset_vimruntime = FALSE;
2222 arg_end = arg;
2223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002225 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 }
2227 }
2228 }
2229
2230 /*
2231 * ":let &option = expr": Set option value.
2232 * ":let &l:option = expr": Set local option value.
2233 * ":let &g:option = expr": Set global option value.
2234 */
2235 else if (*arg == '&')
2236 {
2237 /* Find the end of the name. */
2238 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002239 if (p == NULL || (endchars != NULL
2240 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 EMSG(_(e_letunexp));
2242 else
2243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 long n;
2245 int opt_type;
2246 long numval;
2247 char_u *stringval = NULL;
2248 char_u *s;
2249
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 c1 = *p;
2251 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002252
2253 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002254 s = get_tv_string_chk(tv); /* != NULL if number or string */
2255 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002256 {
2257 opt_type = get_option_value(arg, &numval,
2258 &stringval, opt_flags);
2259 if ((opt_type == 1 && *op == '.')
2260 || (opt_type == 0 && *op != '.'))
2261 EMSG2(_(e_letwrong), op);
2262 else
2263 {
2264 if (opt_type == 1) /* number */
2265 {
2266 if (*op == '+')
2267 n = numval + n;
2268 else
2269 n = numval - n;
2270 }
2271 else if (opt_type == 0 && stringval != NULL) /* string */
2272 {
2273 s = concat_str(stringval, s);
2274 vim_free(stringval);
2275 stringval = s;
2276 }
2277 }
2278 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 if (s != NULL)
2280 {
2281 set_option_value(arg, n, s, opt_flags);
2282 arg_end = p;
2283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002285 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 }
2287 }
2288
2289 /*
2290 * ":let @r = expr": Set register contents.
2291 */
2292 else if (*arg == '@')
2293 {
2294 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295 if (op != NULL && (*op == '+' || *op == '-'))
2296 EMSG2(_(e_letwrong), op);
2297 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002298 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 EMSG(_(e_letunexp));
2300 else
2301 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002302 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 char_u *s;
2304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002305 p = get_tv_string_chk(tv);
2306 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002308 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 if (s != NULL)
2310 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002311 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 vim_free(s);
2313 }
2314 }
2315 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002317 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002318 arg_end = arg + 1;
2319 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002320 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
2322 }
2323
2324 /*
2325 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002328 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002330 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002332 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002335 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2336 EMSG(_(e_letunexp));
2337 else
2338 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 arg_end = p;
2341 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
2345
2346 else
2347 EMSG2(_(e_invarg2), arg);
2348
2349 return arg_end;
2350}
2351
2352/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2354 */
2355 static int
2356check_changedtick(arg)
2357 char_u *arg;
2358{
2359 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2360 {
2361 EMSG2(_(e_readonlyvar), arg);
2362 return TRUE;
2363 }
2364 return FALSE;
2365}
2366
2367/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368 * Get an lval: variable, Dict item or List item that can be assigned a value
2369 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2370 * "name.key", "name.key[expr]" etc.
2371 * Indexing only works if "name" is an existing List or Dictionary.
2372 * "name" points to the start of the name.
2373 * If "rettv" is not NULL it points to the value to be assigned.
2374 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2375 * wrong; must end in space or cmd separator.
2376 *
2377 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002378 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 */
2381 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002382get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002384 typval_T *rettv;
2385 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 int unlet;
2387 int skip;
2388 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002389 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 char_u *expr_start, *expr_end;
2393 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002394 dictitem_T *v;
2395 typval_T var1;
2396 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002398 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002401 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002404 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405
2406 if (skip)
2407 {
2408 /* When skipping just find the end of the name. */
2409 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002410 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 }
2412
2413 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002414 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 if (expr_start != NULL)
2416 {
2417 /* Don't expand the name when we already know there is an error. */
2418 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2419 && *p != '[' && *p != '.')
2420 {
2421 EMSG(_(e_trailing));
2422 return NULL;
2423 }
2424
2425 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2426 if (lp->ll_exp_name == NULL)
2427 {
2428 /* Report an invalid expression in braces, unless the
2429 * expression evaluation has been cancelled due to an
2430 * aborting error, an interrupt, or an exception. */
2431 if (!aborting() && !quiet)
2432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002433 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 EMSG2(_(e_invarg2), name);
2435 return NULL;
2436 }
2437 }
2438 lp->ll_name = lp->ll_exp_name;
2439 }
2440 else
2441 lp->ll_name = name;
2442
2443 /* Without [idx] or .key we are done. */
2444 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2445 return p;
2446
2447 cc = *p;
2448 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002449 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 if (v == NULL && !quiet)
2451 EMSG2(_(e_undefvar), lp->ll_name);
2452 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 if (v == NULL)
2454 return NULL;
2455
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 /*
2457 * Loop until no more [idx] or .key is following.
2458 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002459 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2463 && !(lp->ll_tv->v_type == VAR_DICT
2464 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (!quiet)
2467 EMSG(_("E689: Can only index a List or Dictionary"));
2468 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 if (!quiet)
2473 EMSG(_("E708: [:] must come last"));
2474 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002476
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 len = -1;
2478 if (*p == '.')
2479 {
2480 key = p + 1;
2481 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2482 ;
2483 if (len == 0)
2484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (!quiet)
2486 EMSG(_(e_emptykey));
2487 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 }
2489 p = key + len;
2490 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002491 else
2492 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002494 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 if (*p == ':')
2496 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002497 else
2498 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 empty1 = FALSE;
2500 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 if (get_tv_string_chk(&var1) == NULL)
2503 {
2504 /* not a number or string */
2505 clear_tv(&var1);
2506 return NULL;
2507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 }
2509
2510 /* Optionally get the second index [ :expr]. */
2511 if (*p == ':')
2512 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002516 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002517 if (!empty1)
2518 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002520 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (rettv != NULL && (rettv->v_type != VAR_LIST
2522 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (!quiet)
2525 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 if (!empty1)
2527 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 }
2530 p = skipwhite(p + 1);
2531 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 else
2534 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2537 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 if (!empty1)
2539 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002542 if (get_tv_string_chk(&var2) == NULL)
2543 {
2544 /* not a number or string */
2545 if (!empty1)
2546 clear_tv(&var1);
2547 clear_tv(&var2);
2548 return NULL;
2549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002552 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002555
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (!quiet)
2559 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 if (!empty1)
2561 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 }
2566
2567 /* Skip to past ']'. */
2568 ++p;
2569 }
2570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 {
2573 if (len == -1)
2574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002576 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 if (*key == NUL)
2578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 if (!quiet)
2580 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 }
2584 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002585 lp->ll_list = NULL;
2586 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002587 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002590 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002594 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 if (len == -1)
2596 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 }
2599 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 if (len == -1)
2604 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 p = NULL;
2607 break;
2608 }
2609 if (len == -1)
2610 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 }
2613 else
2614 {
2615 /*
2616 * Get the number and item for the only or first index of the List.
2617 */
2618 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 else
2621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002622 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 clear_tv(&var1);
2624 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002625 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 lp->ll_list = lp->ll_tv->vval.v_list;
2627 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2628 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002630 if (lp->ll_n1 < 0)
2631 {
2632 lp->ll_n1 = 0;
2633 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2634 }
2635 }
2636 if (lp->ll_li == NULL)
2637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642
2643 /*
2644 * May need to find the item or absolute index for the second
2645 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 * When no index given: "lp->ll_empty2" is TRUE.
2647 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002651 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 }
2660
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2662 if (lp->ll_n1 < 0)
2663 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2664 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 }
2667
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 }
2671
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return p;
2673}
2674
2675/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002676 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 */
2678 static void
2679clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002680 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681{
2682 vim_free(lp->ll_exp_name);
2683 vim_free(lp->ll_newkey);
2684}
2685
2686/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002687 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 */
2691 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002692set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002693 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002695 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002697 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698{
2699 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002700 listitem_T *ri;
2701 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702
2703 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 cc = *endp;
2708 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002709 if (op != NULL && *op != '=')
2710 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002711 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002712
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002713 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002714 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002715 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 {
2717 if (tv_op(&tv, rettv, op) == OK)
2718 set_var(lp->ll_name, &tv, FALSE);
2719 clear_tv(&tv);
2720 }
2721 }
2722 else
2723 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002725 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002727 else if (tv_check_lock(lp->ll_newkey == NULL
2728 ? lp->ll_tv->v_lock
2729 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2730 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 else if (lp->ll_range)
2732 {
2733 /*
2734 * Assign the List values to the list items.
2735 */
2736 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 if (op != NULL && *op != '=')
2739 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2740 else
2741 {
2742 clear_tv(&lp->ll_li->li_tv);
2743 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2744 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 ri = ri->li_next;
2746 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2747 break;
2748 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002751 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 ri = NULL;
2754 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002755 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002756 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 lp->ll_li = lp->ll_li->li_next;
2758 ++lp->ll_n1;
2759 }
2760 if (ri != NULL)
2761 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002762 else if (lp->ll_empty2
2763 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 : lp->ll_n1 != lp->ll_n2)
2765 EMSG(_("E711: List value has not enough items"));
2766 }
2767 else
2768 {
2769 /*
2770 * Assign to a List or Dictionary item.
2771 */
2772 if (lp->ll_newkey != NULL)
2773 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002774 if (op != NULL && *op != '=')
2775 {
2776 EMSG2(_(e_letwrong), op);
2777 return;
2778 }
2779
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (di == NULL)
2783 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002784 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2785 {
2786 vim_free(di);
2787 return;
2788 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002790 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002791 else if (op != NULL && *op != '=')
2792 {
2793 tv_op(lp->ll_tv, rettv, op);
2794 return;
2795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002796 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 /*
2800 * Assign the value to the variable or list item.
2801 */
2802 if (copy)
2803 copy_tv(rettv, lp->ll_tv);
2804 else
2805 {
2806 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002807 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002809 }
2810 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002811}
2812
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002813/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2815 * Returns OK or FAIL.
2816 */
2817 static int
2818tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 typval_T *tv1;
2820 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 char_u *op;
2822{
2823 long n;
2824 char_u numbuf[NUMBUFLEN];
2825 char_u *s;
2826
2827 /* Can't do anything with a Funcref or a Dict on the right. */
2828 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2829 {
2830 switch (tv1->v_type)
2831 {
2832 case VAR_DICT:
2833 case VAR_FUNC:
2834 break;
2835
2836 case VAR_LIST:
2837 if (*op != '+' || tv2->v_type != VAR_LIST)
2838 break;
2839 /* List += List */
2840 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2841 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2842 return OK;
2843
2844 case VAR_NUMBER:
2845 case VAR_STRING:
2846 if (tv2->v_type == VAR_LIST)
2847 break;
2848 if (*op == '+' || *op == '-')
2849 {
2850 /* nr += nr or nr -= nr*/
2851 n = get_tv_number(tv1);
2852 if (*op == '+')
2853 n += get_tv_number(tv2);
2854 else
2855 n -= get_tv_number(tv2);
2856 clear_tv(tv1);
2857 tv1->v_type = VAR_NUMBER;
2858 tv1->vval.v_number = n;
2859 }
2860 else
2861 {
2862 /* str .= str */
2863 s = get_tv_string(tv1);
2864 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2865 clear_tv(tv1);
2866 tv1->v_type = VAR_STRING;
2867 tv1->vval.v_string = s;
2868 }
2869 return OK;
2870 }
2871 }
2872
2873 EMSG2(_(e_letwrong), op);
2874 return FAIL;
2875}
2876
2877/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002878 * Add a watcher to a list.
2879 */
2880 static void
2881list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 list_T *l;
2883 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884{
2885 lw->lw_next = l->lv_watch;
2886 l->lv_watch = lw;
2887}
2888
2889/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002890 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002891 * No warning when it isn't found...
2892 */
2893 static void
2894list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 list_T *l;
2896 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002897{
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002899
2900 lwp = &l->lv_watch;
2901 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2902 {
2903 if (lw == lwrem)
2904 {
2905 *lwp = lw->lw_next;
2906 break;
2907 }
2908 lwp = &lw->lw_next;
2909 }
2910}
2911
2912/*
2913 * Just before removing an item from a list: advance watchers to the next
2914 * item.
2915 */
2916 static void
2917list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 list_T *l;
2919 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002920{
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002922
2923 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2924 if (lw->lw_item == item)
2925 lw->lw_item = item->li_next;
2926}
2927
2928/*
2929 * Evaluate the expression used in a ":for var in expr" command.
2930 * "arg" points to "var".
2931 * Set "*errp" to TRUE for an error, FALSE otherwise;
2932 * Return a pointer that holds the info. Null when there is an error.
2933 */
2934 void *
2935eval_for_line(arg, errp, nextcmdp, skip)
2936 char_u *arg;
2937 int *errp;
2938 char_u **nextcmdp;
2939 int skip;
2940{
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 typval_T tv;
2944 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002945
2946 *errp = TRUE; /* default: there is an error */
2947
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002949 if (fi == NULL)
2950 return NULL;
2951
2952 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2953 if (expr == NULL)
2954 return fi;
2955
2956 expr = skipwhite(expr);
2957 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2958 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002959 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002960 return fi;
2961 }
2962
2963 if (skip)
2964 ++emsg_skip;
2965 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2966 {
2967 *errp = FALSE;
2968 if (!skip)
2969 {
2970 l = tv.vval.v_list;
2971 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002972 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002973 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002974 clear_tv(&tv);
2975 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976 else
2977 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002978 /* No need to increment the refcount, it's already set for the
2979 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002980 fi->fi_list = l;
2981 list_add_watch(l, &fi->fi_lw);
2982 fi->fi_lw.lw_item = l->lv_first;
2983 }
2984 }
2985 }
2986 if (skip)
2987 --emsg_skip;
2988
2989 return fi;
2990}
2991
2992/*
2993 * Use the first item in a ":for" list. Advance to the next.
2994 * Assign the values to the variable (list). "arg" points to the first one.
2995 * Return TRUE when a valid item was found, FALSE when at end of list or
2996 * something wrong.
2997 */
2998 int
2999next_for_item(fi_void, arg)
3000 void *fi_void;
3001 char_u *arg;
3002{
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003005 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006
3007 item = fi->fi_lw.lw_item;
3008 if (item == NULL)
3009 result = FALSE;
3010 else
3011 {
3012 fi->fi_lw.lw_item = item->li_next;
3013 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3014 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3015 }
3016 return result;
3017}
3018
3019/*
3020 * Free the structure used to store info used by ":for".
3021 */
3022 void
3023free_for_info(fi_void)
3024 void *fi_void;
3025{
Bram Moolenaar33570922005-01-25 22:26:29 +00003026 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003027
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003028 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003029 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003030 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003031 list_unref(fi->fi_list);
3032 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003033 vim_free(fi);
3034}
3035
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3037
3038 void
3039set_context_for_expression(xp, arg, cmdidx)
3040 expand_T *xp;
3041 char_u *arg;
3042 cmdidx_T cmdidx;
3043{
3044 int got_eq = FALSE;
3045 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003046 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048 if (cmdidx == CMD_let)
3049 {
3050 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003051 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003052 {
3053 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003054 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003055 {
3056 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058 if (vim_iswhite(*p))
3059 break;
3060 }
3061 return;
3062 }
3063 }
3064 else
3065 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3066 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 while ((xp->xp_pattern = vim_strpbrk(arg,
3068 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3069 {
3070 c = *xp->xp_pattern;
3071 if (c == '&')
3072 {
3073 c = xp->xp_pattern[1];
3074 if (c == '&')
3075 {
3076 ++xp->xp_pattern;
3077 xp->xp_context = cmdidx != CMD_let || got_eq
3078 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3079 }
3080 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003083 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3084 xp->xp_pattern += 2;
3085
3086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
3088 else if (c == '$')
3089 {
3090 /* environment variable */
3091 xp->xp_context = EXPAND_ENV_VARS;
3092 }
3093 else if (c == '=')
3094 {
3095 got_eq = TRUE;
3096 xp->xp_context = EXPAND_EXPRESSION;
3097 }
3098 else if (c == '<'
3099 && xp->xp_context == EXPAND_FUNCTIONS
3100 && vim_strchr(xp->xp_pattern, '(') == NULL)
3101 {
3102 /* Function name can start with "<SNR>" */
3103 break;
3104 }
3105 else if (cmdidx != CMD_let || got_eq)
3106 {
3107 if (c == '"') /* string */
3108 {
3109 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3110 if (c == '\\' && xp->xp_pattern[1] != NUL)
3111 ++xp->xp_pattern;
3112 xp->xp_context = EXPAND_NOTHING;
3113 }
3114 else if (c == '\'') /* literal string */
3115 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003116 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3118 /* skip */ ;
3119 xp->xp_context = EXPAND_NOTHING;
3120 }
3121 else if (c == '|')
3122 {
3123 if (xp->xp_pattern[1] == '|')
3124 {
3125 ++xp->xp_pattern;
3126 xp->xp_context = EXPAND_EXPRESSION;
3127 }
3128 else
3129 xp->xp_context = EXPAND_COMMANDS;
3130 }
3131 else
3132 xp->xp_context = EXPAND_EXPRESSION;
3133 }
3134 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135 /* Doesn't look like something valid, expand as an expression
3136 * anyway. */
3137 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 arg = xp->xp_pattern;
3139 if (*arg != NUL)
3140 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3141 /* skip */ ;
3142 }
3143 xp->xp_pattern = arg;
3144}
3145
3146#endif /* FEAT_CMDL_COMPL */
3147
3148/*
3149 * ":1,25call func(arg1, arg2)" function call.
3150 */
3151 void
3152ex_call(eap)
3153 exarg_T *eap;
3154{
3155 char_u *arg = eap->arg;
3156 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003158 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 linenr_T lnum;
3162 int doesrange;
3163 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003166 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003167 if (fudi.fd_newkey != NULL)
3168 {
3169 /* Still need to give an error message for missing key. */
3170 EMSG2(_(e_dictkey), fudi.fd_newkey);
3171 vim_free(fudi.fd_newkey);
3172 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003173 if (tofree == NULL)
3174 return;
3175
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003176 /* Increase refcount on dictionary, it could get deleted when evaluating
3177 * the arguments. */
3178 if (fudi.fd_dict != NULL)
3179 ++fudi.fd_dict->dv_refcount;
3180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003182 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003183 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar532c7802005-01-27 14:44:31 +00003185 /* Skip white space to allow ":call func ()". Not good, but required for
3186 * backward compatibility. */
3187 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003188 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 if (*startarg != '(')
3191 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003192 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 goto end;
3194 }
3195
3196 /*
3197 * When skipping, evaluate the function once, to find the end of the
3198 * arguments.
3199 * When the function takes a range, this is discovered after the first
3200 * call, and the loop is broken.
3201 */
3202 if (eap->skip)
3203 {
3204 ++emsg_skip;
3205 lnum = eap->line2; /* do it once, also with an invalid range */
3206 }
3207 else
3208 lnum = eap->line1;
3209 for ( ; lnum <= eap->line2; ++lnum)
3210 {
3211 if (!eap->skip && eap->addr_count > 0)
3212 {
3213 curwin->w_cursor.lnum = lnum;
3214 curwin->w_cursor.col = 0;
3215 }
3216 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003217 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003218 eap->line1, eap->line2, &doesrange,
3219 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
3221 failed = TRUE;
3222 break;
3223 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003224
3225 /* Handle a function returning a Funcref, Dictionary or List. */
3226 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3227 {
3228 failed = TRUE;
3229 break;
3230 }
3231
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003232 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (doesrange || eap->skip)
3234 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003235
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003237 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003238 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003239 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 if (aborting())
3241 break;
3242 }
3243 if (eap->skip)
3244 --emsg_skip;
3245
3246 if (!failed)
3247 {
3248 /* Check for trailing illegal characters and a following command. */
3249 if (!ends_excmd(*arg))
3250 {
3251 emsg_severe = TRUE;
3252 EMSG(_(e_trailing));
3253 }
3254 else
3255 eap->nextcmd = check_nextcmd(arg);
3256 }
3257
3258end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003259 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003260 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261}
3262
3263/*
3264 * ":unlet[!] var1 ... " command.
3265 */
3266 void
3267ex_unlet(eap)
3268 exarg_T *eap;
3269{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003270 ex_unletlock(eap, eap->arg, 0);
3271}
3272
3273/*
3274 * ":lockvar" and ":unlockvar" commands
3275 */
3276 void
3277ex_lockvar(eap)
3278 exarg_T *eap;
3279{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003281 int deep = 2;
3282
3283 if (eap->forceit)
3284 deep = -1;
3285 else if (vim_isdigit(*arg))
3286 {
3287 deep = getdigits(&arg);
3288 arg = skipwhite(arg);
3289 }
3290
3291 ex_unletlock(eap, arg, deep);
3292}
3293
3294/*
3295 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3296 */
3297 static void
3298ex_unletlock(eap, argstart, deep)
3299 exarg_T *eap;
3300 char_u *argstart;
3301 int deep;
3302{
3303 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003306 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
3308 do
3309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003310 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003311 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3312 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003313 if (lv.ll_name == NULL)
3314 error = TRUE; /* error but continue parsing */
3315 if (name_end == NULL || (!vim_iswhite(*name_end)
3316 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003318 if (name_end != NULL)
3319 {
3320 emsg_severe = TRUE;
3321 EMSG(_(e_trailing));
3322 }
3323 if (!(eap->skip || error))
3324 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 break;
3326 }
3327
3328 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003329 {
3330 if (eap->cmdidx == CMD_unlet)
3331 {
3332 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3333 error = TRUE;
3334 }
3335 else
3336 {
3337 if (do_lock_var(&lv, name_end, deep,
3338 eap->cmdidx == CMD_lockvar) == FAIL)
3339 error = TRUE;
3340 }
3341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003343 if (!eap->skip)
3344 clear_lval(&lv);
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 arg = skipwhite(name_end);
3347 } while (!ends_excmd(*arg));
3348
3349 eap->nextcmd = check_nextcmd(arg);
3350}
3351
Bram Moolenaar8c711452005-01-14 21:53:12 +00003352 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003353do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003354 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003355 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003356 int forceit;
3357{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003358 int ret = OK;
3359 int cc;
3360
3361 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003362 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003363 cc = *name_end;
3364 *name_end = NUL;
3365
3366 /* Normal name or expanded name. */
3367 if (check_changedtick(lp->ll_name))
3368 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003369 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003370 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003371 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003372 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003373 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3374 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003375 else if (lp->ll_range)
3376 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003377 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003378
3379 /* Delete a range of List items. */
3380 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3381 {
3382 li = lp->ll_li->li_next;
3383 listitem_remove(lp->ll_list, lp->ll_li);
3384 lp->ll_li = li;
3385 ++lp->ll_n1;
3386 }
3387 }
3388 else
3389 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003390 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003391 /* unlet a List item. */
3392 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003393 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003394 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003395 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003396 }
3397
3398 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003399}
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401/*
3402 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003403 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 */
3405 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003406do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003408 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 hashtab_T *ht;
3411 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003412 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003413 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaar33570922005-01-25 22:26:29 +00003415 ht = find_var_ht(name, &varname);
3416 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003418 hi = hash_find(ht, varname);
3419 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003420 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003421 di = HI2DI(hi);
3422 if (var_check_fixed(di->di_flags, name)
3423 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003424 return FAIL;
3425 delete_var(ht, hi);
3426 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003429 if (forceit)
3430 return OK;
3431 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 return FAIL;
3433}
3434
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003435/*
3436 * Lock or unlock variable indicated by "lp".
3437 * "deep" is the levels to go (-1 for unlimited);
3438 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3439 */
3440 static int
3441do_lock_var(lp, name_end, deep, lock)
3442 lval_T *lp;
3443 char_u *name_end;
3444 int deep;
3445 int lock;
3446{
3447 int ret = OK;
3448 int cc;
3449 dictitem_T *di;
3450
3451 if (deep == 0) /* nothing to do */
3452 return OK;
3453
3454 if (lp->ll_tv == NULL)
3455 {
3456 cc = *name_end;
3457 *name_end = NUL;
3458
3459 /* Normal name or expanded name. */
3460 if (check_changedtick(lp->ll_name))
3461 ret = FAIL;
3462 else
3463 {
3464 di = find_var(lp->ll_name, NULL);
3465 if (di == NULL)
3466 ret = FAIL;
3467 else
3468 {
3469 if (lock)
3470 di->di_flags |= DI_FLAGS_LOCK;
3471 else
3472 di->di_flags &= ~DI_FLAGS_LOCK;
3473 item_lock(&di->di_tv, deep, lock);
3474 }
3475 }
3476 *name_end = cc;
3477 }
3478 else if (lp->ll_range)
3479 {
3480 listitem_T *li = lp->ll_li;
3481
3482 /* (un)lock a range of List items. */
3483 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3484 {
3485 item_lock(&li->li_tv, deep, lock);
3486 li = li->li_next;
3487 ++lp->ll_n1;
3488 }
3489 }
3490 else if (lp->ll_list != NULL)
3491 /* (un)lock a List item. */
3492 item_lock(&lp->ll_li->li_tv, deep, lock);
3493 else
3494 /* un(lock) a Dictionary item. */
3495 item_lock(&lp->ll_di->di_tv, deep, lock);
3496
3497 return ret;
3498}
3499
3500/*
3501 * Lock or unlock an item. "deep" is nr of levels to go.
3502 */
3503 static void
3504item_lock(tv, deep, lock)
3505 typval_T *tv;
3506 int deep;
3507 int lock;
3508{
3509 static int recurse = 0;
3510 list_T *l;
3511 listitem_T *li;
3512 dict_T *d;
3513 hashitem_T *hi;
3514 int todo;
3515
3516 if (recurse >= DICT_MAXNEST)
3517 {
3518 EMSG(_("E743: variable nested too deep for (un)lock"));
3519 return;
3520 }
3521 if (deep == 0)
3522 return;
3523 ++recurse;
3524
3525 /* lock/unlock the item itself */
3526 if (lock)
3527 tv->v_lock |= VAR_LOCKED;
3528 else
3529 tv->v_lock &= ~VAR_LOCKED;
3530
3531 switch (tv->v_type)
3532 {
3533 case VAR_LIST:
3534 if ((l = tv->vval.v_list) != NULL)
3535 {
3536 if (lock)
3537 l->lv_lock |= VAR_LOCKED;
3538 else
3539 l->lv_lock &= ~VAR_LOCKED;
3540 if (deep < 0 || deep > 1)
3541 /* recursive: lock/unlock the items the List contains */
3542 for (li = l->lv_first; li != NULL; li = li->li_next)
3543 item_lock(&li->li_tv, deep - 1, lock);
3544 }
3545 break;
3546 case VAR_DICT:
3547 if ((d = tv->vval.v_dict) != NULL)
3548 {
3549 if (lock)
3550 d->dv_lock |= VAR_LOCKED;
3551 else
3552 d->dv_lock &= ~VAR_LOCKED;
3553 if (deep < 0 || deep > 1)
3554 {
3555 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003556 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003557 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3558 {
3559 if (!HASHITEM_EMPTY(hi))
3560 {
3561 --todo;
3562 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3563 }
3564 }
3565 }
3566 }
3567 }
3568 --recurse;
3569}
3570
Bram Moolenaara40058a2005-07-11 22:42:07 +00003571/*
3572 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3573 * it refers to a List or Dictionary that is locked.
3574 */
3575 static int
3576tv_islocked(tv)
3577 typval_T *tv;
3578{
3579 return (tv->v_lock & VAR_LOCKED)
3580 || (tv->v_type == VAR_LIST
3581 && tv->vval.v_list != NULL
3582 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3583 || (tv->v_type == VAR_DICT
3584 && tv->vval.v_dict != NULL
3585 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3586}
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3589/*
3590 * Delete all "menutrans_" variables.
3591 */
3592 void
3593del_menutrans_vars()
3594{
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003596 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003599 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003601 {
3602 if (!HASHITEM_EMPTY(hi))
3603 {
3604 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003605 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3606 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003607 }
3608 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003609 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610}
3611#endif
3612
3613#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3614
3615/*
3616 * Local string buffer for the next two functions to store a variable name
3617 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3618 * get_user_var_name().
3619 */
3620
3621static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3622
3623static char_u *varnamebuf = NULL;
3624static int varnamebuflen = 0;
3625
3626/*
3627 * Function to concatenate a prefix and a variable name.
3628 */
3629 static char_u *
3630cat_prefix_varname(prefix, name)
3631 int prefix;
3632 char_u *name;
3633{
3634 int len;
3635
3636 len = (int)STRLEN(name) + 3;
3637 if (len > varnamebuflen)
3638 {
3639 vim_free(varnamebuf);
3640 len += 10; /* some additional space */
3641 varnamebuf = alloc(len);
3642 if (varnamebuf == NULL)
3643 {
3644 varnamebuflen = 0;
3645 return NULL;
3646 }
3647 varnamebuflen = len;
3648 }
3649 *varnamebuf = prefix;
3650 varnamebuf[1] = ':';
3651 STRCPY(varnamebuf + 2, name);
3652 return varnamebuf;
3653}
3654
3655/*
3656 * Function given to ExpandGeneric() to obtain the list of user defined
3657 * (global/buffer/window/built-in) variable names.
3658 */
3659/*ARGSUSED*/
3660 char_u *
3661get_user_var_name(xp, idx)
3662 expand_T *xp;
3663 int idx;
3664{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003665 static long_u gdone;
3666 static long_u bdone;
3667 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003668#ifdef FEAT_WINDOWS
3669 static long_u tdone;
3670#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003671 static int vidx;
3672 static hashitem_T *hi;
3673 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
3675 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003676 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003677 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003678#ifdef FEAT_WINDOWS
3679 tdone = 0;
3680#endif
3681 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003682
3683 /* Global variables */
3684 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003686 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003687 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003688 else
3689 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003690 while (HASHITEM_EMPTY(hi))
3691 ++hi;
3692 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3693 return cat_prefix_varname('g', hi->hi_key);
3694 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003696
3697 /* b: variables */
3698 ht = &curbuf->b_vars.dv_hashtab;
3699 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003701 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003702 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003703 else
3704 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003705 while (HASHITEM_EMPTY(hi))
3706 ++hi;
3707 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003709 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003711 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 return (char_u *)"b:changedtick";
3713 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003714
3715 /* w: variables */
3716 ht = &curwin->w_vars.dv_hashtab;
3717 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003719 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003720 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003721 else
3722 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003723 while (HASHITEM_EMPTY(hi))
3724 ++hi;
3725 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003727
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003728#ifdef FEAT_WINDOWS
3729 /* t: variables */
3730 ht = &curtab->tp_vars.dv_hashtab;
3731 if (tdone < ht->ht_used)
3732 {
3733 if (tdone++ == 0)
3734 hi = ht->ht_array;
3735 else
3736 ++hi;
3737 while (HASHITEM_EMPTY(hi))
3738 ++hi;
3739 return cat_prefix_varname('t', hi->hi_key);
3740 }
3741#endif
3742
Bram Moolenaar33570922005-01-25 22:26:29 +00003743 /* v: variables */
3744 if (vidx < VV_LEN)
3745 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
3747 vim_free(varnamebuf);
3748 varnamebuf = NULL;
3749 varnamebuflen = 0;
3750 return NULL;
3751}
3752
3753#endif /* FEAT_CMDL_COMPL */
3754
3755/*
3756 * types for expressions.
3757 */
3758typedef enum
3759{
3760 TYPE_UNKNOWN = 0
3761 , TYPE_EQUAL /* == */
3762 , TYPE_NEQUAL /* != */
3763 , TYPE_GREATER /* > */
3764 , TYPE_GEQUAL /* >= */
3765 , TYPE_SMALLER /* < */
3766 , TYPE_SEQUAL /* <= */
3767 , TYPE_MATCH /* =~ */
3768 , TYPE_NOMATCH /* !~ */
3769} exptype_T;
3770
3771/*
3772 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003773 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3775 */
3776
3777/*
3778 * Handle zero level expression.
3779 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003780 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003781 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 * Return OK or FAIL.
3783 */
3784 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003785eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 char_u **nextcmd;
3789 int evaluate;
3790{
3791 int ret;
3792 char_u *p;
3793
3794 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003795 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (ret == FAIL || !ends_excmd(*p))
3797 {
3798 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 /*
3801 * Report the invalid expression unless the expression evaluation has
3802 * been cancelled due to an aborting error, an interrupt, or an
3803 * exception.
3804 */
3805 if (!aborting())
3806 EMSG2(_(e_invexpr2), arg);
3807 ret = FAIL;
3808 }
3809 if (nextcmd != NULL)
3810 *nextcmd = check_nextcmd(p);
3811
3812 return ret;
3813}
3814
3815/*
3816 * Handle top level expression:
3817 * expr1 ? expr0 : expr0
3818 *
3819 * "arg" must point to the first non-white of the expression.
3820 * "arg" is advanced to the next non-white after the recognized expression.
3821 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003822 * Note: "rettv.v_lock" is not set.
3823 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 * Return OK or FAIL.
3825 */
3826 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003827eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 int evaluate;
3831{
3832 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003833 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 /*
3836 * Get the first variable.
3837 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003838 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return FAIL;
3840
3841 if ((*arg)[0] == '?')
3842 {
3843 result = FALSE;
3844 if (evaluate)
3845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003846 int error = FALSE;
3847
3848 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003850 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003851 if (error)
3852 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
3854
3855 /*
3856 * Get the second variable.
3857 */
3858 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003859 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 return FAIL;
3861
3862 /*
3863 * Check for the ":".
3864 */
3865 if ((*arg)[0] != ':')
3866 {
3867 EMSG(_("E109: Missing ':' after '?'"));
3868 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003869 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 return FAIL;
3871 }
3872
3873 /*
3874 * Get the third variable.
3875 */
3876 *arg = skipwhite(*arg + 1);
3877 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3878 {
3879 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003880 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 return FAIL;
3882 }
3883 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003884 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886
3887 return OK;
3888}
3889
3890/*
3891 * Handle first level expression:
3892 * expr2 || expr2 || expr2 logical OR
3893 *
3894 * "arg" must point to the first non-white of the expression.
3895 * "arg" is advanced to the next non-white after the recognized expression.
3896 *
3897 * Return OK or FAIL.
3898 */
3899 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 int evaluate;
3904{
Bram Moolenaar33570922005-01-25 22:26:29 +00003905 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 long result;
3907 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909
3910 /*
3911 * Get the first variable.
3912 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003913 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 return FAIL;
3915
3916 /*
3917 * Repeat until there is no following "||".
3918 */
3919 first = TRUE;
3920 result = FALSE;
3921 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3922 {
3923 if (evaluate && first)
3924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003927 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003928 if (error)
3929 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 first = FALSE;
3931 }
3932
3933 /*
3934 * Get the second variable.
3935 */
3936 *arg = skipwhite(*arg + 2);
3937 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3938 return FAIL;
3939
3940 /*
3941 * Compute the result.
3942 */
3943 if (evaluate && !result)
3944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003945 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003948 if (error)
3949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 if (evaluate)
3952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 rettv->v_type = VAR_NUMBER;
3954 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956 }
3957
3958 return OK;
3959}
3960
3961/*
3962 * Handle second level expression:
3963 * expr3 && expr3 && expr3 logical AND
3964 *
3965 * "arg" must point to the first non-white of the expression.
3966 * "arg" is advanced to the next non-white after the recognized expression.
3967 *
3968 * Return OK or FAIL.
3969 */
3970 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003971eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 int evaluate;
3975{
Bram Moolenaar33570922005-01-25 22:26:29 +00003976 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 long result;
3978 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003979 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981 /*
3982 * Get the first variable.
3983 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003984 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return FAIL;
3986
3987 /*
3988 * Repeat until there is no following "&&".
3989 */
3990 first = TRUE;
3991 result = TRUE;
3992 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3993 {
3994 if (evaluate && first)
3995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003999 if (error)
4000 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 first = FALSE;
4002 }
4003
4004 /*
4005 * Get the second variable.
4006 */
4007 *arg = skipwhite(*arg + 2);
4008 if (eval4(arg, &var2, evaluate && result) == FAIL)
4009 return FAIL;
4010
4011 /*
4012 * Compute the result.
4013 */
4014 if (evaluate && result)
4015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004016 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004019 if (error)
4020 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022 if (evaluate)
4023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004024 rettv->v_type = VAR_NUMBER;
4025 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027 }
4028
4029 return OK;
4030}
4031
4032/*
4033 * Handle third level expression:
4034 * var1 == var2
4035 * var1 =~ var2
4036 * var1 != var2
4037 * var1 !~ var2
4038 * var1 > var2
4039 * var1 >= var2
4040 * var1 < var2
4041 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004042 * var1 is var2
4043 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 *
4045 * "arg" must point to the first non-white of the expression.
4046 * "arg" is advanced to the next non-white after the recognized expression.
4047 *
4048 * Return OK or FAIL.
4049 */
4050 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 int evaluate;
4055{
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 char_u *p;
4058 int i;
4059 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004060 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 int len = 2;
4062 long n1, n2;
4063 char_u *s1, *s2;
4064 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4065 regmatch_T regmatch;
4066 int ic;
4067 char_u *save_cpo;
4068
4069 /*
4070 * Get the first variable.
4071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return FAIL;
4074
4075 p = *arg;
4076 switch (p[0])
4077 {
4078 case '=': if (p[1] == '=')
4079 type = TYPE_EQUAL;
4080 else if (p[1] == '~')
4081 type = TYPE_MATCH;
4082 break;
4083 case '!': if (p[1] == '=')
4084 type = TYPE_NEQUAL;
4085 else if (p[1] == '~')
4086 type = TYPE_NOMATCH;
4087 break;
4088 case '>': if (p[1] != '=')
4089 {
4090 type = TYPE_GREATER;
4091 len = 1;
4092 }
4093 else
4094 type = TYPE_GEQUAL;
4095 break;
4096 case '<': if (p[1] != '=')
4097 {
4098 type = TYPE_SMALLER;
4099 len = 1;
4100 }
4101 else
4102 type = TYPE_SEQUAL;
4103 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004104 case 'i': if (p[1] == 's')
4105 {
4106 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4107 len = 5;
4108 if (!vim_isIDc(p[len]))
4109 {
4110 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4111 type_is = TRUE;
4112 }
4113 }
4114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116
4117 /*
4118 * If there is a comparitive operator, use it.
4119 */
4120 if (type != TYPE_UNKNOWN)
4121 {
4122 /* extra question mark appended: ignore case */
4123 if (p[len] == '?')
4124 {
4125 ic = TRUE;
4126 ++len;
4127 }
4128 /* extra '#' appended: match case */
4129 else if (p[len] == '#')
4130 {
4131 ic = FALSE;
4132 ++len;
4133 }
4134 /* nothing appened: use 'ignorecase' */
4135 else
4136 ic = p_ic;
4137
4138 /*
4139 * Get the second variable.
4140 */
4141 *arg = skipwhite(p + len);
4142 if (eval5(arg, &var2, evaluate) == FAIL)
4143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 return FAIL;
4146 }
4147
4148 if (evaluate)
4149 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004150 if (type_is && rettv->v_type != var2.v_type)
4151 {
4152 /* For "is" a different type always means FALSE, for "notis"
4153 * it means TRUE. */
4154 n1 = (type == TYPE_NEQUAL);
4155 }
4156 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4157 {
4158 if (type_is)
4159 {
4160 n1 = (rettv->v_type == var2.v_type
4161 && rettv->vval.v_list == var2.vval.v_list);
4162 if (type == TYPE_NEQUAL)
4163 n1 = !n1;
4164 }
4165 else if (rettv->v_type != var2.v_type
4166 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4167 {
4168 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004169 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004170 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004171 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004172 clear_tv(rettv);
4173 clear_tv(&var2);
4174 return FAIL;
4175 }
4176 else
4177 {
4178 /* Compare two Lists for being equal or unequal. */
4179 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4180 if (type == TYPE_NEQUAL)
4181 n1 = !n1;
4182 }
4183 }
4184
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004185 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4186 {
4187 if (type_is)
4188 {
4189 n1 = (rettv->v_type == var2.v_type
4190 && rettv->vval.v_dict == var2.vval.v_dict);
4191 if (type == TYPE_NEQUAL)
4192 n1 = !n1;
4193 }
4194 else if (rettv->v_type != var2.v_type
4195 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4196 {
4197 if (rettv->v_type != var2.v_type)
4198 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4199 else
4200 EMSG(_("E736: Invalid operation for Dictionary"));
4201 clear_tv(rettv);
4202 clear_tv(&var2);
4203 return FAIL;
4204 }
4205 else
4206 {
4207 /* Compare two Dictionaries for being equal or unequal. */
4208 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4209 if (type == TYPE_NEQUAL)
4210 n1 = !n1;
4211 }
4212 }
4213
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004214 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4215 {
4216 if (rettv->v_type != var2.v_type
4217 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4218 {
4219 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004220 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004221 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004222 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004223 clear_tv(rettv);
4224 clear_tv(&var2);
4225 return FAIL;
4226 }
4227 else
4228 {
4229 /* Compare two Funcrefs for being equal or unequal. */
4230 if (rettv->vval.v_string == NULL
4231 || var2.vval.v_string == NULL)
4232 n1 = FALSE;
4233 else
4234 n1 = STRCMP(rettv->vval.v_string,
4235 var2.vval.v_string) == 0;
4236 if (type == TYPE_NEQUAL)
4237 n1 = !n1;
4238 }
4239 }
4240
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 /*
4242 * If one of the two variables is a number, compare as a number.
4243 * When using "=~" or "!~", always compare as string.
4244 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004245 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 n1 = get_tv_number(rettv);
4249 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 switch (type)
4251 {
4252 case TYPE_EQUAL: n1 = (n1 == n2); break;
4253 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4254 case TYPE_GREATER: n1 = (n1 > n2); break;
4255 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4256 case TYPE_SMALLER: n1 = (n1 < n2); break;
4257 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4258 case TYPE_UNKNOWN:
4259 case TYPE_MATCH:
4260 case TYPE_NOMATCH: break; /* avoid gcc warning */
4261 }
4262 }
4263 else
4264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 s1 = get_tv_string_buf(rettv, buf1);
4266 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4268 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4269 else
4270 i = 0;
4271 n1 = FALSE;
4272 switch (type)
4273 {
4274 case TYPE_EQUAL: n1 = (i == 0); break;
4275 case TYPE_NEQUAL: n1 = (i != 0); break;
4276 case TYPE_GREATER: n1 = (i > 0); break;
4277 case TYPE_GEQUAL: n1 = (i >= 0); break;
4278 case TYPE_SMALLER: n1 = (i < 0); break;
4279 case TYPE_SEQUAL: n1 = (i <= 0); break;
4280
4281 case TYPE_MATCH:
4282 case TYPE_NOMATCH:
4283 /* avoid 'l' flag in 'cpoptions' */
4284 save_cpo = p_cpo;
4285 p_cpo = (char_u *)"";
4286 regmatch.regprog = vim_regcomp(s2,
4287 RE_MAGIC + RE_STRING);
4288 regmatch.rm_ic = ic;
4289 if (regmatch.regprog != NULL)
4290 {
4291 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4292 vim_free(regmatch.regprog);
4293 if (type == TYPE_NOMATCH)
4294 n1 = !n1;
4295 }
4296 p_cpo = save_cpo;
4297 break;
4298
4299 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4300 }
4301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 clear_tv(rettv);
4303 clear_tv(&var2);
4304 rettv->v_type = VAR_NUMBER;
4305 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307 }
4308
4309 return OK;
4310}
4311
4312/*
4313 * Handle fourth level expression:
4314 * + number addition
4315 * - number subtraction
4316 * . string concatenation
4317 *
4318 * "arg" must point to the first non-white of the expression.
4319 * "arg" is advanced to the next non-white after the recognized expression.
4320 *
4321 * Return OK or FAIL.
4322 */
4323 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 int evaluate;
4328{
Bram Moolenaar33570922005-01-25 22:26:29 +00004329 typval_T var2;
4330 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 int op;
4332 long n1, n2;
4333 char_u *s1, *s2;
4334 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4335 char_u *p;
4336
4337 /*
4338 * Get the first variable.
4339 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 return FAIL;
4342
4343 /*
4344 * Repeat computing, until no '+', '-' or '.' is following.
4345 */
4346 for (;;)
4347 {
4348 op = **arg;
4349 if (op != '+' && op != '-' && op != '.')
4350 break;
4351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004352 if (op != '+' || rettv->v_type != VAR_LIST)
4353 {
4354 /* For "list + ...", an illegal use of the first operand as
4355 * a number cannot be determined before evaluating the 2nd
4356 * operand: if this is also a list, all is ok.
4357 * For "something . ...", "something - ..." or "non-list + ...",
4358 * we know that the first operand needs to be a string or number
4359 * without evaluating the 2nd operand. So check before to avoid
4360 * side effects after an error. */
4361 if (evaluate && get_tv_string_chk(rettv) == NULL)
4362 {
4363 clear_tv(rettv);
4364 return FAIL;
4365 }
4366 }
4367
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 /*
4369 * Get the second variable.
4370 */
4371 *arg = skipwhite(*arg + 1);
4372 if (eval6(arg, &var2, evaluate) == FAIL)
4373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 return FAIL;
4376 }
4377
4378 if (evaluate)
4379 {
4380 /*
4381 * Compute the result.
4382 */
4383 if (op == '.')
4384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004385 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4386 s2 = get_tv_string_buf_chk(&var2, buf2);
4387 if (s2 == NULL) /* type error ? */
4388 {
4389 clear_tv(rettv);
4390 clear_tv(&var2);
4391 return FAIL;
4392 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004393 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 clear_tv(rettv);
4395 rettv->v_type = VAR_STRING;
4396 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004398 else if (op == '+' && rettv->v_type == VAR_LIST
4399 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004400 {
4401 /* concatenate Lists */
4402 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4403 &var3) == FAIL)
4404 {
4405 clear_tv(rettv);
4406 clear_tv(&var2);
4407 return FAIL;
4408 }
4409 clear_tv(rettv);
4410 *rettv = var3;
4411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 else
4413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004414 int error = FALSE;
4415
4416 n1 = get_tv_number_chk(rettv, &error);
4417 if (error)
4418 {
4419 /* This can only happen for "list + non-list".
4420 * For "non-list + ..." or "something - ...", we returned
4421 * before evaluating the 2nd operand. */
4422 clear_tv(rettv);
4423 return FAIL;
4424 }
4425 n2 = get_tv_number_chk(&var2, &error);
4426 if (error)
4427 {
4428 clear_tv(rettv);
4429 clear_tv(&var2);
4430 return FAIL;
4431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 if (op == '+')
4434 n1 = n1 + n2;
4435 else
4436 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 rettv->v_type = VAR_NUMBER;
4438 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442 }
4443 return OK;
4444}
4445
4446/*
4447 * Handle fifth level expression:
4448 * * number multiplication
4449 * / number division
4450 * % number modulo
4451 *
4452 * "arg" must point to the first non-white of the expression.
4453 * "arg" is advanced to the next non-white after the recognized expression.
4454 *
4455 * Return OK or FAIL.
4456 */
4457 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 int evaluate;
4462{
Bram Moolenaar33570922005-01-25 22:26:29 +00004463 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 int op;
4465 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004466 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
4468 /*
4469 * Get the first variable.
4470 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 return FAIL;
4473
4474 /*
4475 * Repeat computing, until no '*', '/' or '%' is following.
4476 */
4477 for (;;)
4478 {
4479 op = **arg;
4480 if (op != '*' && op != '/' && op != '%')
4481 break;
4482
4483 if (evaluate)
4484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004485 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004486 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004487 if (error)
4488 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 else
4491 n1 = 0;
4492
4493 /*
4494 * Get the second variable.
4495 */
4496 *arg = skipwhite(*arg + 1);
4497 if (eval7(arg, &var2, evaluate) == FAIL)
4498 return FAIL;
4499
4500 if (evaluate)
4501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004503 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004504 if (error)
4505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 /*
4508 * Compute the result.
4509 */
4510 if (op == '*')
4511 n1 = n1 * n2;
4512 else if (op == '/')
4513 {
4514 if (n2 == 0) /* give an error message? */
4515 n1 = 0x7fffffffL;
4516 else
4517 n1 = n1 / n2;
4518 }
4519 else
4520 {
4521 if (n2 == 0) /* give an error message? */
4522 n1 = 0;
4523 else
4524 n1 = n1 % n2;
4525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526 rettv->v_type = VAR_NUMBER;
4527 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529 }
4530
4531 return OK;
4532}
4533
4534/*
4535 * Handle sixth level expression:
4536 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004537 * "string" string constant
4538 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 * &option-name option value
4540 * @r register contents
4541 * identifier variable value
4542 * function() function call
4543 * $VAR environment variable
4544 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004545 * [expr, expr] List
4546 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 *
4548 * Also handle:
4549 * ! in front logical NOT
4550 * - in front unary minus
4551 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552 * trailing [] subscript in String or List
4553 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 *
4555 * "arg" must point to the first non-white of the expression.
4556 * "arg" is advanced to the next non-white after the recognized expression.
4557 *
4558 * Return OK or FAIL.
4559 */
4560 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 int evaluate;
4565{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 long n;
4567 int len;
4568 char_u *s;
4569 int val;
4570 char_u *start_leader, *end_leader;
4571 int ret = OK;
4572 char_u *alias;
4573
4574 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
4580 /*
4581 * Skip '!' and '-' characters. They are handled later.
4582 */
4583 start_leader = *arg;
4584 while (**arg == '!' || **arg == '-' || **arg == '+')
4585 *arg = skipwhite(*arg + 1);
4586 end_leader = *arg;
4587
4588 switch (**arg)
4589 {
4590 /*
4591 * Number constant.
4592 */
4593 case '0':
4594 case '1':
4595 case '2':
4596 case '3':
4597 case '4':
4598 case '5':
4599 case '6':
4600 case '7':
4601 case '8':
4602 case '9':
4603 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4604 *arg += len;
4605 if (evaluate)
4606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607 rettv->v_type = VAR_NUMBER;
4608 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610 break;
4611
4612 /*
4613 * String constant: "string".
4614 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 break;
4617
4618 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004621 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 break;
4623
4624 /*
4625 * List: [expr, expr]
4626 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 break;
4629
4630 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631 * Dictionary: {key: val, key: val}
4632 */
4633 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4634 break;
4635
4636 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004637 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004639 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 break;
4641
4642 /*
4643 * Environment variable: $VAR.
4644 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004645 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 break;
4647
4648 /*
4649 * Register contents: @r.
4650 */
4651 case '@': ++*arg;
4652 if (evaluate)
4653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004655 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
4657 if (**arg != NUL)
4658 ++*arg;
4659 break;
4660
4661 /*
4662 * nested expression: (expression).
4663 */
4664 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (**arg == ')')
4667 ++*arg;
4668 else if (ret == OK)
4669 {
4670 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 ret = FAIL;
4673 }
4674 break;
4675
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 break;
4678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679
4680 if (ret == NOTDONE)
4681 {
4682 /*
4683 * Must be a variable or function name.
4684 * Can also be a curly-braces kind of name: {expr}.
4685 */
4686 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004687 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 if (alias != NULL)
4689 s = alias;
4690
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004691 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004692 ret = FAIL;
4693 else
4694 {
4695 if (**arg == '(') /* recursive! */
4696 {
4697 /* If "s" is the name of a variable of type VAR_FUNC
4698 * use its contents. */
4699 s = deref_func_name(s, &len);
4700
4701 /* Invoke the function. */
4702 ret = get_func_tv(s, len, rettv, arg,
4703 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004704 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004705 /* Stop the expression evaluation when immediately
4706 * aborting on error, or when an interrupt occurred or
4707 * an exception was thrown but not caught. */
4708 if (aborting())
4709 {
4710 if (ret == OK)
4711 clear_tv(rettv);
4712 ret = FAIL;
4713 }
4714 }
4715 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004716 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004717 else
4718 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004719 }
4720
4721 if (alias != NULL)
4722 vim_free(alias);
4723 }
4724
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 *arg = skipwhite(*arg);
4726
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004727 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4728 * expr(expr). */
4729 if (ret == OK)
4730 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
4732 /*
4733 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4734 */
4735 if (ret == OK && evaluate && end_leader > start_leader)
4736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737 int error = FALSE;
4738
4739 val = get_tv_number_chk(rettv, &error);
4740 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004742 clear_tv(rettv);
4743 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004745 else
4746 {
4747 while (end_leader > start_leader)
4748 {
4749 --end_leader;
4750 if (*end_leader == '!')
4751 val = !val;
4752 else if (*end_leader == '-')
4753 val = -val;
4754 }
4755 clear_tv(rettv);
4756 rettv->v_type = VAR_NUMBER;
4757 rettv->vval.v_number = val;
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760
4761 return ret;
4762}
4763
4764/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004765 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4766 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4768 */
4769 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004770eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004772 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004774 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775{
4776 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004777 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 long len = -1;
4780 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004782 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004784 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004786 if (verbose)
4787 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788 return FAIL;
4789 }
4790
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 /*
4794 * dict.name
4795 */
4796 key = *arg + 1;
4797 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4798 ;
4799 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802 }
4803 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 /*
4806 * something[idx]
4807 *
4808 * Get the (first) variable from inside the [].
4809 */
4810 *arg = skipwhite(*arg + 1);
4811 if (**arg == ':')
4812 empty1 = TRUE;
4813 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4814 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004815 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4816 {
4817 /* not a number or string */
4818 clear_tv(&var1);
4819 return FAIL;
4820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004821
4822 /*
4823 * Get the second variable from inside the [:].
4824 */
4825 if (**arg == ':')
4826 {
4827 range = TRUE;
4828 *arg = skipwhite(*arg + 1);
4829 if (**arg == ']')
4830 empty2 = TRUE;
4831 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004833 if (!empty1)
4834 clear_tv(&var1);
4835 return FAIL;
4836 }
4837 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4838 {
4839 /* not a number or string */
4840 if (!empty1)
4841 clear_tv(&var1);
4842 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004843 return FAIL;
4844 }
4845 }
4846
4847 /* Check for the ']'. */
4848 if (**arg != ']')
4849 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004850 if (verbose)
4851 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004852 clear_tv(&var1);
4853 if (range)
4854 clear_tv(&var2);
4855 return FAIL;
4856 }
4857 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004858 }
4859
4860 if (evaluate)
4861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004862 n1 = 0;
4863 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004865 n1 = get_tv_number(&var1);
4866 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004867 }
4868 if (range)
4869 {
4870 if (empty2)
4871 n2 = -1;
4872 else
4873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 n2 = get_tv_number(&var2);
4875 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004876 }
4877 }
4878
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004880 {
4881 case VAR_NUMBER:
4882 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004884 len = (long)STRLEN(s);
4885 if (range)
4886 {
4887 /* The resulting variable is a substring. If the indexes
4888 * are out of range the result is empty. */
4889 if (n1 < 0)
4890 {
4891 n1 = len + n1;
4892 if (n1 < 0)
4893 n1 = 0;
4894 }
4895 if (n2 < 0)
4896 n2 = len + n2;
4897 else if (n2 >= len)
4898 n2 = len;
4899 if (n1 >= len || n2 < 0 || n1 > n2)
4900 s = NULL;
4901 else
4902 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4903 }
4904 else
4905 {
4906 /* The resulting variable is a string of a single
4907 * character. If the index is too big or negative the
4908 * result is empty. */
4909 if (n1 >= len || n1 < 0)
4910 s = NULL;
4911 else
4912 s = vim_strnsave(s + n1, 1);
4913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
4915 rettv->v_type = VAR_STRING;
4916 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 break;
4918
4919 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004921 if (n1 < 0)
4922 n1 = len + n1;
4923 if (!empty1 && (n1 < 0 || n1 >= len))
4924 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004925 /* For a range we allow invalid values and return an empty
4926 * list. A list index out of range is an error. */
4927 if (!range)
4928 {
4929 if (verbose)
4930 EMSGN(_(e_listidx), n1);
4931 return FAIL;
4932 }
4933 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934 }
4935 if (range)
4936 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004937 list_T *l;
4938 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004939
4940 if (n2 < 0)
4941 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004942 else if (n2 >= len)
4943 n2 = len - 1;
4944 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004945 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004946 l = list_alloc();
4947 if (l == NULL)
4948 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004949 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004950 n1 <= n2; ++n1)
4951 {
4952 if (list_append_tv(l, &item->li_tv) == FAIL)
4953 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004954 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004955 return FAIL;
4956 }
4957 item = item->li_next;
4958 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004959 clear_tv(rettv);
4960 rettv->v_type = VAR_LIST;
4961 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004962 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004963 }
4964 else
4965 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004966 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004967 clear_tv(rettv);
4968 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004969 }
4970 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971
4972 case VAR_DICT:
4973 if (range)
4974 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004975 if (verbose)
4976 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 if (len == -1)
4978 clear_tv(&var1);
4979 return FAIL;
4980 }
4981 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004982 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983
4984 if (len == -1)
4985 {
4986 key = get_tv_string(&var1);
4987 if (*key == NUL)
4988 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004989 if (verbose)
4990 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 clear_tv(&var1);
4992 return FAIL;
4993 }
4994 }
4995
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004996 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004998 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004999 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 if (len == -1)
5001 clear_tv(&var1);
5002 if (item == NULL)
5003 return FAIL;
5004
5005 copy_tv(&item->di_tv, &var1);
5006 clear_tv(rettv);
5007 *rettv = var1;
5008 }
5009 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005010 }
5011 }
5012
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005013 return OK;
5014}
5015
5016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 * Get an option value.
5018 * "arg" points to the '&' or '+' before the option name.
5019 * "arg" is advanced to character after the option name.
5020 * Return OK or FAIL.
5021 */
5022 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005025 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 int evaluate;
5027{
5028 char_u *option_end;
5029 long numval;
5030 char_u *stringval;
5031 int opt_type;
5032 int c;
5033 int working = (**arg == '+'); /* has("+option") */
5034 int ret = OK;
5035 int opt_flags;
5036
5037 /*
5038 * Isolate the option name and find its value.
5039 */
5040 option_end = find_option_end(arg, &opt_flags);
5041 if (option_end == NULL)
5042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005043 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 EMSG2(_("E112: Option name missing: %s"), *arg);
5045 return FAIL;
5046 }
5047
5048 if (!evaluate)
5049 {
5050 *arg = option_end;
5051 return OK;
5052 }
5053
5054 c = *option_end;
5055 *option_end = NUL;
5056 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 if (opt_type == -3) /* invalid name */
5060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 EMSG2(_("E113: Unknown option: %s"), *arg);
5063 ret = FAIL;
5064 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 if (opt_type == -2) /* hidden string option */
5068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 rettv->v_type = VAR_STRING;
5070 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 else if (opt_type == -1) /* hidden number option */
5073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 rettv->v_type = VAR_NUMBER;
5075 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 else if (opt_type == 1) /* number option */
5078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 rettv->v_type = VAR_NUMBER;
5080 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 else /* string option */
5083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 rettv->v_type = VAR_STRING;
5085 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087 }
5088 else if (working && (opt_type == -2 || opt_type == -1))
5089 ret = FAIL;
5090
5091 *option_end = c; /* put back for error messages */
5092 *arg = option_end;
5093
5094 return ret;
5095}
5096
5097/*
5098 * Allocate a variable for a string constant.
5099 * Return OK or FAIL.
5100 */
5101 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 int evaluate;
5106{
5107 char_u *p;
5108 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 int extra = 0;
5110
5111 /*
5112 * Find the end of the string, skipping backslashed characters.
5113 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
5116 if (*p == '\\' && p[1] != NUL)
5117 {
5118 ++p;
5119 /* A "\<x>" form occupies at least 4 characters, and produces up
5120 * to 6 characters: reserve space for 2 extra */
5121 if (*p == '<')
5122 extra += 2;
5123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125
5126 if (*p != '"')
5127 {
5128 EMSG2(_("E114: Missing quote: %s"), *arg);
5129 return FAIL;
5130 }
5131
5132 /* If only parsing, set *arg and return here */
5133 if (!evaluate)
5134 {
5135 *arg = p + 1;
5136 return OK;
5137 }
5138
5139 /*
5140 * Copy the string into allocated memory, handling backslashed
5141 * characters.
5142 */
5143 name = alloc((unsigned)(p - *arg + extra));
5144 if (name == NULL)
5145 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 rettv->v_type = VAR_STRING;
5147 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 if (*p == '\\')
5152 {
5153 switch (*++p)
5154 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 case 'b': *name++ = BS; ++p; break;
5156 case 'e': *name++ = ESC; ++p; break;
5157 case 'f': *name++ = FF; ++p; break;
5158 case 'n': *name++ = NL; ++p; break;
5159 case 'r': *name++ = CAR; ++p; break;
5160 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
5162 case 'X': /* hex: "\x1", "\x12" */
5163 case 'x':
5164 case 'u': /* Unicode: "\u0023" */
5165 case 'U':
5166 if (vim_isxdigit(p[1]))
5167 {
5168 int n, nr;
5169 int c = toupper(*p);
5170
5171 if (c == 'X')
5172 n = 2;
5173 else
5174 n = 4;
5175 nr = 0;
5176 while (--n >= 0 && vim_isxdigit(p[1]))
5177 {
5178 ++p;
5179 nr = (nr << 4) + hex2nr(*p);
5180 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182#ifdef FEAT_MBYTE
5183 /* For "\u" store the number according to
5184 * 'encoding'. */
5185 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 else
5188#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 break;
5192
5193 /* octal: "\1", "\12", "\123" */
5194 case '0':
5195 case '1':
5196 case '2':
5197 case '3':
5198 case '4':
5199 case '5':
5200 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 case '7': *name = *p++ - '0';
5202 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 *name = (*name << 3) + *p++ - '0';
5205 if (*p >= '0' && *p <= '7')
5206 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 break;
5210
5211 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (extra != 0)
5214 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 break;
5217 }
5218 /* FALLTHROUGH */
5219
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 break;
5222 }
5223 }
5224 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 *arg = p + 1;
5230
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 return OK;
5232}
5233
5234/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 * Return OK or FAIL.
5237 */
5238 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005239get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 int evaluate;
5243{
5244 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005245 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005246 int reduce = 0;
5247
5248 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005250 */
5251 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5252 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005254 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005256 break;
5257 ++reduce;
5258 ++p;
5259 }
5260 }
5261
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005263 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005265 return FAIL;
5266 }
5267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005269 if (!evaluate)
5270 {
5271 *arg = p + 1;
5272 return OK;
5273 }
5274
5275 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005277 */
5278 str = alloc((unsigned)((p - *arg) - reduce));
5279 if (str == NULL)
5280 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 rettv->v_type = VAR_STRING;
5282 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005283
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005285 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005287 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005289 break;
5290 ++p;
5291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005293 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005295 *arg = p + 1;
5296
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005297 return OK;
5298}
5299
5300/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 * Allocate a variable for a List and fill it from "*arg".
5302 * Return OK or FAIL.
5303 */
5304 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005307 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 int evaluate;
5309{
Bram Moolenaar33570922005-01-25 22:26:29 +00005310 list_T *l = NULL;
5311 typval_T tv;
5312 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313
5314 if (evaluate)
5315 {
5316 l = list_alloc();
5317 if (l == NULL)
5318 return FAIL;
5319 }
5320
5321 *arg = skipwhite(*arg + 1);
5322 while (**arg != ']' && **arg != NUL)
5323 {
5324 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5325 goto failret;
5326 if (evaluate)
5327 {
5328 item = listitem_alloc();
5329 if (item != NULL)
5330 {
5331 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005332 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 list_append(l, item);
5334 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005335 else
5336 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 }
5338
5339 if (**arg == ']')
5340 break;
5341 if (**arg != ',')
5342 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 goto failret;
5345 }
5346 *arg = skipwhite(*arg + 1);
5347 }
5348
5349 if (**arg != ']')
5350 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352failret:
5353 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005354 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 return FAIL;
5356 }
5357
5358 *arg = skipwhite(*arg + 1);
5359 if (evaluate)
5360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005361 rettv->v_type = VAR_LIST;
5362 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 ++l->lv_refcount;
5364 }
5365
5366 return OK;
5367}
5368
5369/*
5370 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005371 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005373 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374list_alloc()
5375{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005376 list_T *l;
5377
5378 l = (list_T *)alloc_clear(sizeof(list_T));
5379 if (l != NULL)
5380 {
5381 /* Prepend the list to the list of lists for garbage collection. */
5382 if (first_list != NULL)
5383 first_list->lv_used_prev = l;
5384 l->lv_used_prev = NULL;
5385 l->lv_used_next = first_list;
5386 first_list = l;
5387 }
5388 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389}
5390
5391/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005392 * Allocate an empty list for a return value.
5393 * Returns OK or FAIL.
5394 */
5395 static int
5396rettv_list_alloc(rettv)
5397 typval_T *rettv;
5398{
5399 list_T *l = list_alloc();
5400
5401 if (l == NULL)
5402 return FAIL;
5403
5404 rettv->vval.v_list = l;
5405 rettv->v_type = VAR_LIST;
5406 ++l->lv_refcount;
5407 return OK;
5408}
5409
5410/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 * Unreference a list: decrement the reference count and free it when it
5412 * becomes zero.
5413 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005414 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005416 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005418 if (l != NULL && --l->lv_refcount <= 0)
5419 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420}
5421
5422/*
5423 * Free a list, including all items it points to.
5424 * Ignores the reference count.
5425 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005426 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005427list_free(l, recurse)
5428 list_T *l;
5429 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430{
Bram Moolenaar33570922005-01-25 22:26:29 +00005431 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005433 /* Remove the list from the list of lists for garbage collection. */
5434 if (l->lv_used_prev == NULL)
5435 first_list = l->lv_used_next;
5436 else
5437 l->lv_used_prev->lv_used_next = l->lv_used_next;
5438 if (l->lv_used_next != NULL)
5439 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5440
Bram Moolenaard9fba312005-06-26 22:34:35 +00005441 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005443 /* Remove the item before deleting it. */
5444 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005445 if (recurse || (item->li_tv.v_type != VAR_LIST
5446 && item->li_tv.v_type != VAR_DICT))
5447 clear_tv(&item->li_tv);
5448 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 }
5450 vim_free(l);
5451}
5452
5453/*
5454 * Allocate a list item.
5455 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005456 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457listitem_alloc()
5458{
Bram Moolenaar33570922005-01-25 22:26:29 +00005459 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460}
5461
5462/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005463 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 */
5465 static void
5466listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005467 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 vim_free(item);
5471}
5472
5473/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005474 * Remove a list item from a List and free it. Also clears the value.
5475 */
5476 static void
5477listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005478 list_T *l;
5479 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005480{
5481 list_remove(l, item, item);
5482 listitem_free(item);
5483}
5484
5485/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 * Get the number of items in a list.
5487 */
5488 static long
5489list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005490 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 if (l == NULL)
5493 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005494 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495}
5496
5497/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005498 * Return TRUE when two lists have exactly the same values.
5499 */
5500 static int
5501list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 list_T *l1;
5503 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005504 int ic; /* ignore case for strings */
5505{
Bram Moolenaar33570922005-01-25 22:26:29 +00005506 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005507
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005508 if (l1 == l2)
5509 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005510 if (list_len(l1) != list_len(l2))
5511 return FALSE;
5512
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005513 for (item1 = l1->lv_first, item2 = l2->lv_first;
5514 item1 != NULL && item2 != NULL;
5515 item1 = item1->li_next, item2 = item2->li_next)
5516 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5517 return FALSE;
5518 return item1 == NULL && item2 == NULL;
5519}
5520
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005521#if defined(FEAT_PYTHON) || defined(PROTO)
5522/*
5523 * Return the dictitem that an entry in a hashtable points to.
5524 */
5525 dictitem_T *
5526dict_lookup(hi)
5527 hashitem_T *hi;
5528{
5529 return HI2DI(hi);
5530}
5531#endif
5532
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005533/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005534 * Return TRUE when two dictionaries have exactly the same key/values.
5535 */
5536 static int
5537dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005538 dict_T *d1;
5539 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005540 int ic; /* ignore case for strings */
5541{
Bram Moolenaar33570922005-01-25 22:26:29 +00005542 hashitem_T *hi;
5543 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005544 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005545
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005546 if (d1 == d2)
5547 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005548 if (dict_len(d1) != dict_len(d2))
5549 return FALSE;
5550
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005551 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005554 if (!HASHITEM_EMPTY(hi))
5555 {
5556 item2 = dict_find(d2, hi->hi_key, -1);
5557 if (item2 == NULL)
5558 return FALSE;
5559 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5560 return FALSE;
5561 --todo;
5562 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005563 }
5564 return TRUE;
5565}
5566
5567/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005568 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005569 * Compares the items just like "==" would compare them, but strings and
5570 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005571 */
5572 static int
5573tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005574 typval_T *tv1;
5575 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005576 int ic; /* ignore case */
5577{
5578 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005579 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005580 static int recursive = 0; /* cach recursive loops */
5581 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005582
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005583 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005584 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005585 /* Catch lists and dicts that have an endless loop by limiting
5586 * recursiveness to 1000. We guess they are equal then. */
5587 if (recursive >= 1000)
5588 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005589
5590 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005591 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005592 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005593 ++recursive;
5594 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5595 --recursive;
5596 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005597
5598 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005599 ++recursive;
5600 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5601 --recursive;
5602 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005603
5604 case VAR_FUNC:
5605 return (tv1->vval.v_string != NULL
5606 && tv2->vval.v_string != NULL
5607 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5608
5609 case VAR_NUMBER:
5610 return tv1->vval.v_number == tv2->vval.v_number;
5611
5612 case VAR_STRING:
5613 s1 = get_tv_string_buf(tv1, buf1);
5614 s2 = get_tv_string_buf(tv2, buf2);
5615 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005616 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005617
5618 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005619 return TRUE;
5620}
5621
5622/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 * Locate item with index "n" in list "l" and return it.
5624 * A negative index is counted from the end; -1 is the last item.
5625 * Returns NULL when "n" is out of range.
5626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 long n;
5631{
Bram Moolenaar33570922005-01-25 22:26:29 +00005632 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 long idx;
5634
5635 if (l == NULL)
5636 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637
5638 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005639 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005640 n = l->lv_len + n;
5641
5642 /* Check for index out of range. */
5643 if (n < 0 || n >= l->lv_len)
5644 return NULL;
5645
5646 /* When there is a cached index may start search from there. */
5647 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005649 if (n < l->lv_idx / 2)
5650 {
5651 /* closest to the start of the list */
5652 item = l->lv_first;
5653 idx = 0;
5654 }
5655 else if (n > (l->lv_idx + l->lv_len) / 2)
5656 {
5657 /* closest to the end of the list */
5658 item = l->lv_last;
5659 idx = l->lv_len - 1;
5660 }
5661 else
5662 {
5663 /* closest to the cached index */
5664 item = l->lv_idx_item;
5665 idx = l->lv_idx;
5666 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667 }
5668 else
5669 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005670 if (n < l->lv_len / 2)
5671 {
5672 /* closest to the start of the list */
5673 item = l->lv_first;
5674 idx = 0;
5675 }
5676 else
5677 {
5678 /* closest to the end of the list */
5679 item = l->lv_last;
5680 idx = l->lv_len - 1;
5681 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005682 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005683
5684 while (n > idx)
5685 {
5686 /* search forward */
5687 item = item->li_next;
5688 ++idx;
5689 }
5690 while (n < idx)
5691 {
5692 /* search backward */
5693 item = item->li_prev;
5694 --idx;
5695 }
5696
5697 /* cache the used index */
5698 l->lv_idx = idx;
5699 l->lv_idx_item = item;
5700
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701 return item;
5702}
5703
5704/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005705 * Get list item "l[idx]" as a number.
5706 */
5707 static long
5708list_find_nr(l, idx, errorp)
5709 list_T *l;
5710 long idx;
5711 int *errorp; /* set to TRUE when something wrong */
5712{
5713 listitem_T *li;
5714
5715 li = list_find(l, idx);
5716 if (li == NULL)
5717 {
5718 if (errorp != NULL)
5719 *errorp = TRUE;
5720 return -1L;
5721 }
5722 return get_tv_number_chk(&li->li_tv, errorp);
5723}
5724
5725/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005726 * Locate "item" list "l" and return its index.
5727 * Returns -1 when "item" is not in the list.
5728 */
5729 static long
5730list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 list_T *l;
5732 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005733{
5734 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005735 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005736
5737 if (l == NULL)
5738 return -1;
5739 idx = 0;
5740 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5741 ++idx;
5742 if (li == NULL)
5743 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005744 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005745}
5746
5747/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748 * Append item "item" to the end of list "l".
5749 */
5750 static void
5751list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005752 list_T *l;
5753 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754{
5755 if (l->lv_last == NULL)
5756 {
5757 /* empty list */
5758 l->lv_first = item;
5759 l->lv_last = item;
5760 item->li_prev = NULL;
5761 }
5762 else
5763 {
5764 l->lv_last->li_next = item;
5765 item->li_prev = l->lv_last;
5766 l->lv_last = item;
5767 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005768 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 item->li_next = NULL;
5770}
5771
5772/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005773 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005774 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775 */
5776 static int
5777list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005778 list_T *l;
5779 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005781 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782
Bram Moolenaar05159a02005-02-26 23:04:13 +00005783 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005785 copy_tv(tv, &li->li_tv);
5786 list_append(l, li);
5787 return OK;
5788}
5789
5790/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005791 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005792 * Return FAIL when out of memory.
5793 */
5794 int
5795list_append_dict(list, dict)
5796 list_T *list;
5797 dict_T *dict;
5798{
5799 listitem_T *li = listitem_alloc();
5800
5801 if (li == NULL)
5802 return FAIL;
5803 li->li_tv.v_type = VAR_DICT;
5804 li->li_tv.v_lock = 0;
5805 li->li_tv.vval.v_dict = dict;
5806 list_append(list, li);
5807 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808 return OK;
5809}
5810
5811/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005812 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005813 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005814 * Returns FAIL when out of memory.
5815 */
5816 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005817list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005818 list_T *l;
5819 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005820 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005821{
5822 listitem_T *li = listitem_alloc();
5823
5824 if (li == NULL)
5825 return FAIL;
5826 list_append(l, li);
5827 li->li_tv.v_type = VAR_STRING;
5828 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005829 if (str == NULL)
5830 li->li_tv.vval.v_string = NULL;
5831 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005832 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005833 return FAIL;
5834 return OK;
5835}
5836
5837/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005838 * Append "n" to list "l".
5839 * Returns FAIL when out of memory.
5840 */
5841 static int
5842list_append_number(l, n)
5843 list_T *l;
5844 varnumber_T n;
5845{
5846 listitem_T *li;
5847
5848 li = listitem_alloc();
5849 if (li == NULL)
5850 return FAIL;
5851 li->li_tv.v_type = VAR_NUMBER;
5852 li->li_tv.v_lock = 0;
5853 li->li_tv.vval.v_number = n;
5854 list_append(l, li);
5855 return OK;
5856}
5857
5858/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005860 * If "item" is NULL append at the end.
5861 * Return FAIL when out of memory.
5862 */
5863 static int
5864list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005865 list_T *l;
5866 typval_T *tv;
5867 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005868{
Bram Moolenaar33570922005-01-25 22:26:29 +00005869 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005870
5871 if (ni == NULL)
5872 return FAIL;
5873 copy_tv(tv, &ni->li_tv);
5874 if (item == NULL)
5875 /* Append new item at end of list. */
5876 list_append(l, ni);
5877 else
5878 {
5879 /* Insert new item before existing item. */
5880 ni->li_prev = item->li_prev;
5881 ni->li_next = item;
5882 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005883 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005884 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005885 ++l->lv_idx;
5886 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005888 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005890 l->lv_idx_item = NULL;
5891 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005892 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005893 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005894 }
5895 return OK;
5896}
5897
5898/*
5899 * Extend "l1" with "l2".
5900 * If "bef" is NULL append at the end, otherwise insert before this item.
5901 * Returns FAIL when out of memory.
5902 */
5903 static int
5904list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *l1;
5906 list_T *l2;
5907 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005908{
Bram Moolenaar33570922005-01-25 22:26:29 +00005909 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005910
5911 for (item = l2->lv_first; item != NULL; item = item->li_next)
5912 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5913 return FAIL;
5914 return OK;
5915}
5916
5917/*
5918 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5919 * Return FAIL when out of memory.
5920 */
5921 static int
5922list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005923 list_T *l1;
5924 list_T *l2;
5925 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926{
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928
5929 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005930 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931 if (l == NULL)
5932 return FAIL;
5933 tv->v_type = VAR_LIST;
5934 tv->vval.v_list = l;
5935
5936 /* append all items from the second list */
5937 return list_extend(l, l2, NULL);
5938}
5939
5940/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005941 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005943 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 * Returns NULL when out of memory.
5945 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005946 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005947list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005950 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951{
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 list_T *copy;
5953 listitem_T *item;
5954 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955
5956 if (orig == NULL)
5957 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958
5959 copy = list_alloc();
5960 if (copy != NULL)
5961 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005962 if (copyID != 0)
5963 {
5964 /* Do this before adding the items, because one of the items may
5965 * refer back to this list. */
5966 orig->lv_copyID = copyID;
5967 orig->lv_copylist = copy;
5968 }
5969 for (item = orig->lv_first; item != NULL && !got_int;
5970 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 {
5972 ni = listitem_alloc();
5973 if (ni == NULL)
5974 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005975 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005976 {
5977 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5978 {
5979 vim_free(ni);
5980 break;
5981 }
5982 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005984 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 list_append(copy, ni);
5986 }
5987 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005988 if (item != NULL)
5989 {
5990 list_unref(copy);
5991 copy = NULL;
5992 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993 }
5994
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995 return copy;
5996}
5997
5998/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006003list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 list_T *l;
6005 listitem_T *item;
6006 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007{
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006010 /* notify watchers */
6011 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006013 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 list_fix_watch(l, ip);
6015 if (ip == item2)
6016 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018
6019 if (item2->li_next == NULL)
6020 l->lv_last = item->li_prev;
6021 else
6022 item2->li_next->li_prev = item->li_prev;
6023 if (item->li_prev == NULL)
6024 l->lv_first = item2->li_next;
6025 else
6026 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006027 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028}
6029
6030/*
6031 * Return an allocated string with the string representation of a list.
6032 * May return NULL.
6033 */
6034 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006035list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006037 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038{
6039 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040
6041 if (tv->vval.v_list == NULL)
6042 return NULL;
6043 ga_init2(&ga, (int)sizeof(char), 80);
6044 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006045 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006046 {
6047 vim_free(ga.ga_data);
6048 return NULL;
6049 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 ga_append(&ga, ']');
6051 ga_append(&ga, NUL);
6052 return (char_u *)ga.ga_data;
6053}
6054
6055/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006056 * Join list "l" into a string in "*gap", using separator "sep".
6057 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006058 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006059 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006060 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006061list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006062 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064 char_u *sep;
6065 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006066 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006067{
6068 int first = TRUE;
6069 char_u *tofree;
6070 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006071 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006072 char_u *s;
6073
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006074 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006075 {
6076 if (first)
6077 first = FALSE;
6078 else
6079 ga_concat(gap, sep);
6080
6081 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006082 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006083 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006084 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006085 if (s != NULL)
6086 ga_concat(gap, s);
6087 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006088 if (s == NULL)
6089 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006090 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006091 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006092}
6093
6094/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006095 * Garbage collection for lists and dictionaries.
6096 *
6097 * We use reference counts to be able to free most items right away when they
6098 * are no longer used. But for composite items it's possible that it becomes
6099 * unused while the reference count is > 0: When there is a recursive
6100 * reference. Example:
6101 * :let l = [1, 2, 3]
6102 * :let d = {9: l}
6103 * :let l[1] = d
6104 *
6105 * Since this is quite unusual we handle this with garbage collection: every
6106 * once in a while find out which lists and dicts are not referenced from any
6107 * variable.
6108 *
6109 * Here is a good reference text about garbage collection (refers to Python
6110 * but it applies to all reference-counting mechanisms):
6111 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006112 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006113
6114/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115 * Do garbage collection for lists and dicts.
6116 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006117 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006118 int
6119garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006120{
6121 dict_T *dd;
6122 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006123 int copyID = ++current_copyID;
6124 buf_T *buf;
6125 win_T *wp;
6126 int i;
6127 funccall_T *fc;
6128 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006129#ifdef FEAT_WINDOWS
6130 tabpage_T *tp;
6131#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006132
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006133 /* Only do this once. */
6134 want_garbage_collect = FALSE;
6135 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006136 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006137
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006138 /*
6139 * 1. Go through all accessible variables and mark all lists and dicts
6140 * with copyID.
6141 */
6142 /* script-local variables */
6143 for (i = 1; i <= ga_scripts.ga_len; ++i)
6144 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6145
6146 /* buffer-local variables */
6147 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6148 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6149
6150 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006151 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006152 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6153
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006154#ifdef FEAT_WINDOWS
6155 /* tabpage-local variables */
6156 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6157 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6158#endif
6159
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006160 /* global variables */
6161 set_ref_in_ht(&globvarht, copyID);
6162
6163 /* function-local variables */
6164 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6165 {
6166 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6167 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6168 }
6169
6170 /*
6171 * 2. Go through the list of dicts and free items without the copyID.
6172 */
6173 for (dd = first_dict; dd != NULL; )
6174 if (dd->dv_copyID != copyID)
6175 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006176 /* Free the Dictionary and ordinary items it contains, but don't
6177 * recurse into Lists and Dictionaries, they will be in the list
6178 * of dicts or list of lists. */
6179 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006180 did_free = TRUE;
6181
6182 /* restart, next dict may also have been freed */
6183 dd = first_dict;
6184 }
6185 else
6186 dd = dd->dv_used_next;
6187
6188 /*
6189 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006190 * But don't free a list that has a watcher (used in a for loop), these
6191 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006192 */
6193 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006194 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006195 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006196 /* Free the List and ordinary items it contains, but don't recurse
6197 * into Lists and Dictionaries, they will be in the list of dicts
6198 * or list of lists. */
6199 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006200 did_free = TRUE;
6201
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006202 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006203 ll = first_list;
6204 }
6205 else
6206 ll = ll->lv_used_next;
6207
6208 return did_free;
6209}
6210
6211/*
6212 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6213 */
6214 static void
6215set_ref_in_ht(ht, copyID)
6216 hashtab_T *ht;
6217 int copyID;
6218{
6219 int todo;
6220 hashitem_T *hi;
6221
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006222 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006223 for (hi = ht->ht_array; todo > 0; ++hi)
6224 if (!HASHITEM_EMPTY(hi))
6225 {
6226 --todo;
6227 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6228 }
6229}
6230
6231/*
6232 * Mark all lists and dicts referenced through list "l" with "copyID".
6233 */
6234 static void
6235set_ref_in_list(l, copyID)
6236 list_T *l;
6237 int copyID;
6238{
6239 listitem_T *li;
6240
6241 for (li = l->lv_first; li != NULL; li = li->li_next)
6242 set_ref_in_item(&li->li_tv, copyID);
6243}
6244
6245/*
6246 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6247 */
6248 static void
6249set_ref_in_item(tv, copyID)
6250 typval_T *tv;
6251 int copyID;
6252{
6253 dict_T *dd;
6254 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006255
6256 switch (tv->v_type)
6257 {
6258 case VAR_DICT:
6259 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006260 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006261 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006262 /* Didn't see this dict yet. */
6263 dd->dv_copyID = copyID;
6264 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006265 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006266 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006267
6268 case VAR_LIST:
6269 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006270 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006271 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006272 /* Didn't see this list yet. */
6273 ll->lv_copyID = copyID;
6274 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006275 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006276 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006277 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006278 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006279}
6280
6281/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006282 * Allocate an empty header for a dictionary.
6283 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006284 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006285dict_alloc()
6286{
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006290 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006291 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006292 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006293 if (first_dict != NULL)
6294 first_dict->dv_used_prev = d;
6295 d->dv_used_next = first_dict;
6296 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006297 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006298
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006300 d->dv_lock = 0;
6301 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006302 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006303 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006304 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006305}
6306
6307/*
6308 * Unreference a Dictionary: decrement the reference count and free it when it
6309 * becomes zero.
6310 */
6311 static void
6312dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006315 if (d != NULL && --d->dv_refcount <= 0)
6316 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006317}
6318
6319/*
6320 * Free a Dictionary, including all items it contains.
6321 * Ignores the reference count.
6322 */
6323 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006324dict_free(d, recurse)
6325 dict_T *d;
6326 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006327{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006330 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006331
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006332 /* Remove the dict from the list of dicts for garbage collection. */
6333 if (d->dv_used_prev == NULL)
6334 first_dict = d->dv_used_next;
6335 else
6336 d->dv_used_prev->dv_used_next = d->dv_used_next;
6337 if (d->dv_used_next != NULL)
6338 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6339
6340 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006341 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006342 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345 if (!HASHITEM_EMPTY(hi))
6346 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006347 /* Remove the item before deleting it, just in case there is
6348 * something recursive causing trouble. */
6349 di = HI2DI(hi);
6350 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006351 if (recurse || (di->di_tv.v_type != VAR_LIST
6352 && di->di_tv.v_type != VAR_DICT))
6353 clear_tv(&di->di_tv);
6354 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006355 --todo;
6356 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006357 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006359 vim_free(d);
6360}
6361
6362/*
6363 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006364 * The "key" is copied to the new item.
6365 * Note that the value of the item "di_tv" still needs to be initialized!
6366 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006367 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369dictitem_alloc(key)
6370 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371{
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006373
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006374 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006377 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006378 di->di_flags = 0;
6379 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006380 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381}
6382
6383/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006384 * Make a copy of a Dictionary item.
6385 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006389{
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006391
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006392 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6393 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006394 if (di != NULL)
6395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006396 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006397 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006398 copy_tv(&org->di_tv, &di->di_tv);
6399 }
6400 return di;
6401}
6402
6403/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404 * Remove item "item" from Dictionary "dict" and free it.
6405 */
6406 static void
6407dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 dict_T *dict;
6409 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410{
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006414 if (HASHITEM_EMPTY(hi))
6415 EMSG2(_(e_intern2), "dictitem_remove()");
6416 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006418 dictitem_free(item);
6419}
6420
6421/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006422 * Free a dict item. Also clears the value.
6423 */
6424 static void
6425dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428 clear_tv(&item->di_tv);
6429 vim_free(item);
6430}
6431
6432/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006433 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6434 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006435 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006436 * Returns NULL when out of memory.
6437 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006439dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006441 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006442 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006443{
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 dict_T *copy;
6445 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006446 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006448
6449 if (orig == NULL)
6450 return NULL;
6451
6452 copy = dict_alloc();
6453 if (copy != NULL)
6454 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006455 if (copyID != 0)
6456 {
6457 orig->dv_copyID = copyID;
6458 orig->dv_copydict = copy;
6459 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006460 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006461 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006463 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006464 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006465 --todo;
6466
6467 di = dictitem_alloc(hi->hi_key);
6468 if (di == NULL)
6469 break;
6470 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006471 {
6472 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6473 copyID) == FAIL)
6474 {
6475 vim_free(di);
6476 break;
6477 }
6478 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006479 else
6480 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6481 if (dict_add(copy, di) == FAIL)
6482 {
6483 dictitem_free(di);
6484 break;
6485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006487 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006488
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006490 if (todo > 0)
6491 {
6492 dict_unref(copy);
6493 copy = NULL;
6494 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006495 }
6496
6497 return copy;
6498}
6499
6500/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006502 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006503 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006504 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 dict_T *d;
6507 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508{
Bram Moolenaar33570922005-01-25 22:26:29 +00006509 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510}
6511
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006513 * Add a number or string entry to dictionary "d".
6514 * When "str" is NULL use number "nr", otherwise use "str".
6515 * Returns FAIL when out of memory and when key already exists.
6516 */
6517 int
6518dict_add_nr_str(d, key, nr, str)
6519 dict_T *d;
6520 char *key;
6521 long nr;
6522 char_u *str;
6523{
6524 dictitem_T *item;
6525
6526 item = dictitem_alloc((char_u *)key);
6527 if (item == NULL)
6528 return FAIL;
6529 item->di_tv.v_lock = 0;
6530 if (str == NULL)
6531 {
6532 item->di_tv.v_type = VAR_NUMBER;
6533 item->di_tv.vval.v_number = nr;
6534 }
6535 else
6536 {
6537 item->di_tv.v_type = VAR_STRING;
6538 item->di_tv.vval.v_string = vim_strsave(str);
6539 }
6540 if (dict_add(d, item) == FAIL)
6541 {
6542 dictitem_free(item);
6543 return FAIL;
6544 }
6545 return OK;
6546}
6547
6548/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 * Get the number of items in a Dictionary.
6550 */
6551 static long
6552dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 if (d == NULL)
6556 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006557 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006558}
6559
6560/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006561 * Find item "key[len]" in Dictionary "d".
6562 * If "len" is negative use strlen(key).
6563 * Returns NULL when not found.
6564 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006565 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006566dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006568 char_u *key;
6569 int len;
6570{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006571#define AKEYLEN 200
6572 char_u buf[AKEYLEN];
6573 char_u *akey;
6574 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006576
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006577 if (len < 0)
6578 akey = key;
6579 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006581 tofree = akey = vim_strnsave(key, len);
6582 if (akey == NULL)
6583 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006584 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006585 else
6586 {
6587 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006588 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006589 akey = buf;
6590 }
6591
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006593 vim_free(tofree);
6594 if (HASHITEM_EMPTY(hi))
6595 return NULL;
6596 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597}
6598
6599/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006600 * Get a string item from a dictionary.
6601 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006602 * Returns NULL if the entry doesn't exist or out of memory.
6603 */
6604 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006605get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006606 dict_T *d;
6607 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006608 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006609{
6610 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006611 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006612
6613 di = dict_find(d, key, -1);
6614 if (di == NULL)
6615 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006616 s = get_tv_string(&di->di_tv);
6617 if (save && s != NULL)
6618 s = vim_strsave(s);
6619 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006620}
6621
6622/*
6623 * Get a number item from a dictionary.
6624 * Returns 0 if the entry doesn't exist or out of memory.
6625 */
6626 long
6627get_dict_number(d, key)
6628 dict_T *d;
6629 char_u *key;
6630{
6631 dictitem_T *di;
6632
6633 di = dict_find(d, key, -1);
6634 if (di == NULL)
6635 return 0;
6636 return get_tv_number(&di->di_tv);
6637}
6638
6639/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006640 * Return an allocated string with the string representation of a Dictionary.
6641 * May return NULL.
6642 */
6643 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006644dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006646 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647{
6648 garray_T ga;
6649 int first = TRUE;
6650 char_u *tofree;
6651 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006652 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006653 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006654 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006655 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006657 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006658 return NULL;
6659 ga_init2(&ga, (int)sizeof(char), 80);
6660 ga_append(&ga, '{');
6661
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006662 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006663 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006665 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006667 --todo;
6668
6669 if (first)
6670 first = FALSE;
6671 else
6672 ga_concat(&ga, (char_u *)", ");
6673
6674 tofree = string_quote(hi->hi_key, FALSE);
6675 if (tofree != NULL)
6676 {
6677 ga_concat(&ga, tofree);
6678 vim_free(tofree);
6679 }
6680 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006681 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006682 if (s != NULL)
6683 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006685 if (s == NULL)
6686 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006688 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006689 if (todo > 0)
6690 {
6691 vim_free(ga.ga_data);
6692 return NULL;
6693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006694
6695 ga_append(&ga, '}');
6696 ga_append(&ga, NUL);
6697 return (char_u *)ga.ga_data;
6698}
6699
6700/*
6701 * Allocate a variable for a Dictionary and fill it from "*arg".
6702 * Return OK or FAIL. Returns NOTDONE for {expr}.
6703 */
6704 static int
6705get_dict_tv(arg, rettv, evaluate)
6706 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006707 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006708 int evaluate;
6709{
Bram Moolenaar33570922005-01-25 22:26:29 +00006710 dict_T *d = NULL;
6711 typval_T tvkey;
6712 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00006713 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006714 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006715 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006716 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006717
6718 /*
6719 * First check if it's not a curly-braces thing: {expr}.
6720 * Must do this without evaluating, otherwise a function may be called
6721 * twice. Unfortunately this means we need to call eval1() twice for the
6722 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006723 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006724 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006725 if (*start != '}')
6726 {
6727 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6728 return FAIL;
6729 if (*start == '}')
6730 return NOTDONE;
6731 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006732
6733 if (evaluate)
6734 {
6735 d = dict_alloc();
6736 if (d == NULL)
6737 return FAIL;
6738 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006739 tvkey.v_type = VAR_UNKNOWN;
6740 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006741
6742 *arg = skipwhite(*arg + 1);
6743 while (**arg != '}' && **arg != NUL)
6744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006745 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006746 goto failret;
6747 if (**arg != ':')
6748 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006749 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006750 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751 goto failret;
6752 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006753 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006754 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006755 key = get_tv_string_buf_chk(&tvkey, buf);
6756 if (key == NULL || *key == NUL)
6757 {
6758 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6759 if (key != NULL)
6760 EMSG(_(e_emptykey));
6761 clear_tv(&tvkey);
6762 goto failret;
6763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006764 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765
6766 *arg = skipwhite(*arg + 1);
6767 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6768 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006769 if (evaluate)
6770 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006771 goto failret;
6772 }
6773 if (evaluate)
6774 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006775 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006776 if (item != NULL)
6777 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006778 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006779 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006780 clear_tv(&tv);
6781 goto failret;
6782 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006783 item = dictitem_alloc(key);
6784 clear_tv(&tvkey);
6785 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006786 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006787 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006788 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006789 if (dict_add(d, item) == FAIL)
6790 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791 }
6792 }
6793
6794 if (**arg == '}')
6795 break;
6796 if (**arg != ',')
6797 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006798 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006799 goto failret;
6800 }
6801 *arg = skipwhite(*arg + 1);
6802 }
6803
6804 if (**arg != '}')
6805 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006806 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006807failret:
6808 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006809 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006810 return FAIL;
6811 }
6812
6813 *arg = skipwhite(*arg + 1);
6814 if (evaluate)
6815 {
6816 rettv->v_type = VAR_DICT;
6817 rettv->vval.v_dict = d;
6818 ++d->dv_refcount;
6819 }
6820
6821 return OK;
6822}
6823
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 * Return a string with the string representation of a variable.
6826 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006827 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006828 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006829 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006830 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006831 */
6832 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006833echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006834 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006835 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006836 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006837 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006838{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006839 static int recurse = 0;
6840 char_u *r = NULL;
6841
Bram Moolenaar33570922005-01-25 22:26:29 +00006842 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006843 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006844 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006845 *tofree = NULL;
6846 return NULL;
6847 }
6848 ++recurse;
6849
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006850 switch (tv->v_type)
6851 {
6852 case VAR_FUNC:
6853 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006854 r = tv->vval.v_string;
6855 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006856
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006858 if (tv->vval.v_list == NULL)
6859 {
6860 *tofree = NULL;
6861 r = NULL;
6862 }
6863 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6864 {
6865 *tofree = NULL;
6866 r = (char_u *)"[...]";
6867 }
6868 else
6869 {
6870 tv->vval.v_list->lv_copyID = copyID;
6871 *tofree = list2string(tv, copyID);
6872 r = *tofree;
6873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006874 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006875
Bram Moolenaar8c711452005-01-14 21:53:12 +00006876 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006877 if (tv->vval.v_dict == NULL)
6878 {
6879 *tofree = NULL;
6880 r = NULL;
6881 }
6882 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6883 {
6884 *tofree = NULL;
6885 r = (char_u *)"{...}";
6886 }
6887 else
6888 {
6889 tv->vval.v_dict->dv_copyID = copyID;
6890 *tofree = dict2string(tv, copyID);
6891 r = *tofree;
6892 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006893 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006894
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006895 case VAR_STRING:
6896 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897 *tofree = NULL;
6898 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006899 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006900
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006901 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006902 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006903 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006905
6906 --recurse;
6907 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006908}
6909
6910/*
6911 * Return a string with the string representation of a variable.
6912 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6913 * "numbuf" is used for a number.
6914 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006915 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006916 */
6917 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006918tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006920 char_u **tofree;
6921 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006922 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006923{
6924 switch (tv->v_type)
6925 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006926 case VAR_FUNC:
6927 *tofree = string_quote(tv->vval.v_string, TRUE);
6928 return *tofree;
6929 case VAR_STRING:
6930 *tofree = string_quote(tv->vval.v_string, FALSE);
6931 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006932 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006933 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006934 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006935 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006936 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006937 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006938 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006939 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006940}
6941
6942/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 * Return string "str" in ' quotes, doubling ' characters.
6944 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006945 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006946 */
6947 static char_u *
6948string_quote(str, function)
6949 char_u *str;
6950 int function;
6951{
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006953 char_u *p, *r, *s;
6954
Bram Moolenaar33570922005-01-25 22:26:29 +00006955 len = (function ? 13 : 3);
6956 if (str != NULL)
6957 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006958 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006959 for (p = str; *p != NUL; mb_ptr_adv(p))
6960 if (*p == '\'')
6961 ++len;
6962 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006963 s = r = alloc(len);
6964 if (r != NULL)
6965 {
6966 if (function)
6967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006969 r += 10;
6970 }
6971 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 if (str != NULL)
6974 for (p = str; *p != NUL; )
6975 {
6976 if (*p == '\'')
6977 *r++ = '\'';
6978 MB_COPY_CHAR(p, r);
6979 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006981 if (function)
6982 *r++ = ')';
6983 *r++ = NUL;
6984 }
6985 return s;
6986}
6987
6988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 * Get the value of an environment variable.
6990 * "arg" is pointing to the '$'. It is advanced to after the name.
6991 * If the environment variable was not set, silently assume it is empty.
6992 * Always return OK.
6993 */
6994 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006995get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 int evaluate;
6999{
7000 char_u *string = NULL;
7001 int len;
7002 int cc;
7003 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007004 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
7006 ++*arg;
7007 name = *arg;
7008 len = get_env_len(arg);
7009 if (evaluate)
7010 {
7011 if (len != 0)
7012 {
7013 cc = name[len];
7014 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007015 /* first try vim_getenv(), fast for normal environment vars */
7016 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007018 {
7019 if (!mustfree)
7020 string = vim_strsave(string);
7021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 else
7023 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007024 if (mustfree)
7025 vim_free(string);
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 /* next try expanding things like $VIM and ${HOME} */
7028 string = expand_env_save(name - 1);
7029 if (string != NULL && *string == '$')
7030 {
7031 vim_free(string);
7032 string = NULL;
7033 }
7034 }
7035 name[len] = cc;
7036 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007037 rettv->v_type = VAR_STRING;
7038 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 }
7040
7041 return OK;
7042}
7043
7044/*
7045 * Array with names and number of arguments of all internal functions
7046 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7047 */
7048static struct fst
7049{
7050 char *f_name; /* function name */
7051 char f_min_argc; /* minimal number of arguments */
7052 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007053 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007054 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055} functions[] =
7056{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007057 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"append", 2, 2, f_append},
7059 {"argc", 0, 0, f_argc},
7060 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007061 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007063 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {"bufexists", 1, 1, f_bufexists},
7065 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7066 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7067 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7068 {"buflisted", 1, 1, f_buflisted},
7069 {"bufloaded", 1, 1, f_bufloaded},
7070 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007071 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {"bufwinnr", 1, 1, f_bufwinnr},
7073 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007074 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007075 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007076 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {"char2nr", 1, 1, f_char2nr},
7078 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007079 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007081#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007082 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007083 {"complete_add", 1, 1, f_complete_add},
7084 {"complete_check", 0, 0, f_complete_check},
7085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007087 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007088 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007090 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007091 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {"delete", 1, 1, f_delete},
7093 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007094 {"diff_filler", 1, 1, f_diff_filler},
7095 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007096 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007098 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {"eventhandler", 0, 0, f_eventhandler},
7100 {"executable", 1, 1, f_executable},
7101 {"exists", 1, 1, f_exists},
7102 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007103 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007104 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7106 {"filereadable", 1, 1, f_filereadable},
7107 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007108 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007109 {"finddir", 1, 3, f_finddir},
7110 {"findfile", 1, 3, f_findfile},
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007111 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {"fnamemodify", 2, 2, f_fnamemodify},
7113 {"foldclosed", 1, 1, f_foldclosed},
7114 {"foldclosedend", 1, 1, f_foldclosedend},
7115 {"foldlevel", 1, 1, f_foldlevel},
7116 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007117 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007119 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007120 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007121 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007122 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 {"getbufvar", 2, 2, f_getbufvar},
7124 {"getchar", 0, 1, f_getchar},
7125 {"getcharmod", 0, 0, f_getcharmod},
7126 {"getcmdline", 0, 0, f_getcmdline},
7127 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007128 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007130 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007131 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 {"getfsize", 1, 1, f_getfsize},
7133 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007134 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007135 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007136 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007137 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007138 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007139 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007140 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007141 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007143 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {"getwinposx", 0, 0, f_getwinposx},
7145 {"getwinposy", 0, 0, f_getwinposy},
7146 {"getwinvar", 2, 2, f_getwinvar},
7147 {"glob", 1, 1, f_glob},
7148 {"globpath", 2, 2, f_globpath},
7149 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007151 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007152 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7154 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7155 {"histadd", 2, 2, f_histadd},
7156 {"histdel", 1, 2, f_histdel},
7157 {"histget", 1, 2, f_histget},
7158 {"histnr", 1, 1, f_histnr},
7159 {"hlID", 1, 1, f_hlID},
7160 {"hlexists", 1, 1, f_hlexists},
7161 {"hostname", 0, 0, f_hostname},
7162 {"iconv", 3, 3, f_iconv},
7163 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007164 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007165 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007167 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {"inputrestore", 0, 0, f_inputrestore},
7169 {"inputsave", 0, 0, f_inputsave},
7170 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007171 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007173 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007175 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007178 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {"libcall", 3, 3, f_libcall},
7180 {"libcallnr", 3, 3, f_libcallnr},
7181 {"line", 1, 1, f_line},
7182 {"line2byte", 1, 1, f_line2byte},
7183 {"lispindent", 1, 1, f_lispindent},
7184 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007185 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007186 {"maparg", 1, 3, f_maparg},
7187 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007188 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007189 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007190 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007191 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007192 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007193 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007194 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007195 {"max", 1, 1, f_max},
7196 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007197#ifdef vim_mkdir
7198 {"mkdir", 1, 3, f_mkdir},
7199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {"mode", 0, 0, f_mode},
7201 {"nextnonblank", 1, 1, f_nextnonblank},
7202 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007203 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007205 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007206 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007208 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007209 {"reltime", 0, 2, f_reltime},
7210 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {"remote_expr", 2, 3, f_remote_expr},
7212 {"remote_foreground", 1, 1, f_remote_foreground},
7213 {"remote_peek", 1, 2, f_remote_peek},
7214 {"remote_read", 1, 1, f_remote_read},
7215 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007216 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007218 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007220 {"reverse", 1, 1, f_reverse},
Bram Moolenaar76929292008-01-06 19:07:36 +00007221 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007222 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007223 {"searchpair", 3, 7, f_searchpair},
7224 {"searchpairpos", 3, 7, f_searchpairpos},
7225 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {"server2client", 2, 2, f_server2client},
7227 {"serverlist", 0, 0, f_serverlist},
7228 {"setbufvar", 3, 3, f_setbufvar},
7229 {"setcmdpos", 1, 1, f_setcmdpos},
7230 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007231 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007232 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007233 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007234 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007236 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007238 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007240 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007241 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007242 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007243 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007244 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007245 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246#ifdef HAVE_STRFTIME
7247 {"strftime", 1, 2, f_strftime},
7248#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007250 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {"strlen", 1, 1, f_strlen},
7252 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007253 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {"strtrans", 1, 1, f_strtrans},
7255 {"submatch", 1, 1, f_submatch},
7256 {"substitute", 4, 4, f_substitute},
7257 {"synID", 3, 3, f_synID},
7258 {"synIDattr", 2, 3, f_synIDattr},
7259 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007260 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007261 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007262 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007263 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007264 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007265 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007266 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007268 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {"tolower", 1, 1, f_tolower},
7270 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007271 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {"virtcol", 1, 1, f_virtcol},
7275 {"visualmode", 0, 1, f_visualmode},
7276 {"winbufnr", 1, 1, f_winbufnr},
7277 {"wincol", 0, 0, f_wincol},
7278 {"winheight", 1, 1, f_winheight},
7279 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007280 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007282 {"winrestview", 1, 1, f_winrestview},
7283 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007285 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286};
7287
7288#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7289
7290/*
7291 * Function given to ExpandGeneric() to obtain the list of internal
7292 * or user defined function names.
7293 */
7294 char_u *
7295get_function_name(xp, idx)
7296 expand_T *xp;
7297 int idx;
7298{
7299 static int intidx = -1;
7300 char_u *name;
7301
7302 if (idx == 0)
7303 intidx = -1;
7304 if (intidx < 0)
7305 {
7306 name = get_user_func_name(xp, idx);
7307 if (name != NULL)
7308 return name;
7309 }
7310 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7311 {
7312 STRCPY(IObuff, functions[intidx].f_name);
7313 STRCAT(IObuff, "(");
7314 if (functions[intidx].f_max_argc == 0)
7315 STRCAT(IObuff, ")");
7316 return IObuff;
7317 }
7318
7319 return NULL;
7320}
7321
7322/*
7323 * Function given to ExpandGeneric() to obtain the list of internal or
7324 * user defined variable or function names.
7325 */
7326/*ARGSUSED*/
7327 char_u *
7328get_expr_name(xp, idx)
7329 expand_T *xp;
7330 int idx;
7331{
7332 static int intidx = -1;
7333 char_u *name;
7334
7335 if (idx == 0)
7336 intidx = -1;
7337 if (intidx < 0)
7338 {
7339 name = get_function_name(xp, idx);
7340 if (name != NULL)
7341 return name;
7342 }
7343 return get_user_var_name(xp, ++intidx);
7344}
7345
7346#endif /* FEAT_CMDL_COMPL */
7347
7348/*
7349 * Find internal function in table above.
7350 * Return index, or -1 if not found
7351 */
7352 static int
7353find_internal_func(name)
7354 char_u *name; /* name of the function */
7355{
7356 int first = 0;
7357 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7358 int cmp;
7359 int x;
7360
7361 /*
7362 * Find the function name in the table. Binary search.
7363 */
7364 while (first <= last)
7365 {
7366 x = first + ((unsigned)(last - first) >> 1);
7367 cmp = STRCMP(name, functions[x].f_name);
7368 if (cmp < 0)
7369 last = x - 1;
7370 else if (cmp > 0)
7371 first = x + 1;
7372 else
7373 return x;
7374 }
7375 return -1;
7376}
7377
7378/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007379 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7380 * name it contains, otherwise return "name".
7381 */
7382 static char_u *
7383deref_func_name(name, lenp)
7384 char_u *name;
7385 int *lenp;
7386{
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388 int cc;
7389
7390 cc = name[*lenp];
7391 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007392 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007393 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007395 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007396 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007397 {
7398 *lenp = 0;
7399 return (char_u *)""; /* just in case */
7400 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007401 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007403 }
7404
7405 return name;
7406}
7407
7408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 * Allocate a variable for the result of a function.
7410 * Return OK or FAIL.
7411 */
7412 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7414 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 char_u *name; /* name of the function */
7416 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 char_u **arg; /* argument, pointing to the '(' */
7419 linenr_T firstline; /* first line of range */
7420 linenr_T lastline; /* last line of range */
7421 int *doesrange; /* return: function handled range */
7422 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
7425 char_u *argp;
7426 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007427 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 int argcount = 0; /* number of arguments found */
7429
7430 /*
7431 * Get the arguments.
7432 */
7433 argp = *arg;
7434 while (argcount < MAX_FUNC_ARGS)
7435 {
7436 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7437 if (*argp == ')' || *argp == ',' || *argp == NUL)
7438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7440 {
7441 ret = FAIL;
7442 break;
7443 }
7444 ++argcount;
7445 if (*argp != ',')
7446 break;
7447 }
7448 if (*argp == ')')
7449 ++argp;
7450 else
7451 ret = FAIL;
7452
7453 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007454 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 {
7458 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007461 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
7464 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007465 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466
7467 *arg = skipwhite(argp);
7468 return ret;
7469}
7470
7471
7472/*
7473 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007474 * Return OK when the function can't be called, FAIL otherwise.
7475 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 */
7477 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007478call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007479 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 char_u *name; /* name of the function */
7481 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007484 typval_T *argvars; /* vars for arguments, must have "argcount"
7485 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 linenr_T firstline; /* first line of range */
7487 linenr_T lastline; /* last line of range */
7488 int *doesrange; /* return: function handled range */
7489 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493#define ERROR_UNKNOWN 0
7494#define ERROR_TOOMANY 1
7495#define ERROR_TOOFEW 2
7496#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497#define ERROR_DICT 4
7498#define ERROR_NONE 5
7499#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 int error = ERROR_NONE;
7501 int i;
7502 int llen;
7503 ufunc_T *fp;
7504 int cc;
7505#define FLEN_FIXED 40
7506 char_u fname_buf[FLEN_FIXED + 1];
7507 char_u *fname;
7508
7509 /*
7510 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7511 * Change <SNR>123_name() to K_SNR 123_name().
7512 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7513 */
7514 cc = name[len];
7515 name[len] = NUL;
7516 llen = eval_fname_script(name);
7517 if (llen > 0)
7518 {
7519 fname_buf[0] = K_SPECIAL;
7520 fname_buf[1] = KS_EXTRA;
7521 fname_buf[2] = (int)KE_SNR;
7522 i = 3;
7523 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7524 {
7525 if (current_SID <= 0)
7526 error = ERROR_SCRIPT;
7527 else
7528 {
7529 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7530 i = (int)STRLEN(fname_buf);
7531 }
7532 }
7533 if (i + STRLEN(name + llen) < FLEN_FIXED)
7534 {
7535 STRCPY(fname_buf + i, name + llen);
7536 fname = fname_buf;
7537 }
7538 else
7539 {
7540 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7541 if (fname == NULL)
7542 error = ERROR_OTHER;
7543 else
7544 {
7545 mch_memmove(fname, fname_buf, (size_t)i);
7546 STRCPY(fname + i, name + llen);
7547 }
7548 }
7549 }
7550 else
7551 fname = name;
7552
7553 *doesrange = FALSE;
7554
7555
7556 /* execute the function if no errors detected and executing */
7557 if (evaluate && error == ERROR_NONE)
7558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007559 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 error = ERROR_UNKNOWN;
7561
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007562 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {
7564 /*
7565 * User defined function.
7566 */
7567 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007568
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007570 /* Trigger FuncUndefined event, may load the function. */
7571 if (fp == NULL
7572 && apply_autocmds(EVENT_FUNCUNDEFINED,
7573 fname, fname, TRUE, NULL)
7574 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007576 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 fp = find_func(fname);
7578 }
7579#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007580 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007581 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007582 {
7583 /* loaded a package, search for the function again */
7584 fp = find_func(fname);
7585 }
7586
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (fp != NULL)
7588 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007589 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007591 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007593 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007595 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007596 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 else
7598 {
7599 /*
7600 * Call the user function.
7601 * Save and restore search patterns, script variables and
7602 * redo buffer.
7603 */
7604 save_search_patterns();
7605 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007606 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007607 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007608 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007609 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7610 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7611 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007612 /* Function was unreferenced while being used, free it
7613 * now. */
7614 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 restoreRedobuff();
7616 restore_search_patterns();
7617 error = ERROR_NONE;
7618 }
7619 }
7620 }
7621 else
7622 {
7623 /*
7624 * Find the function name in the table, call its implementation.
7625 */
7626 i = find_internal_func(fname);
7627 if (i >= 0)
7628 {
7629 if (argcount < functions[i].f_min_argc)
7630 error = ERROR_TOOFEW;
7631 else if (argcount > functions[i].f_max_argc)
7632 error = ERROR_TOOMANY;
7633 else
7634 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007636 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 error = ERROR_NONE;
7638 }
7639 }
7640 }
7641 /*
7642 * The function call (or "FuncUndefined" autocommand sequence) might
7643 * have been aborted by an error, an interrupt, or an explicitly thrown
7644 * exception that has not been caught so far. This situation can be
7645 * tested for by calling aborting(). For an error in an internal
7646 * function or for the "E132" error in call_user_func(), however, the
7647 * throw point at which the "force_abort" flag (temporarily reset by
7648 * emsg()) is normally updated has not been reached yet. We need to
7649 * update that flag first to make aborting() reliable.
7650 */
7651 update_force_abort();
7652 }
7653 if (error == ERROR_NONE)
7654 ret = OK;
7655
7656 /*
7657 * Report an error unless the argument evaluation or function call has been
7658 * cancelled due to an aborting error, an interrupt, or an exception.
7659 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 if (!aborting())
7661 {
7662 switch (error)
7663 {
7664 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007665 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 break;
7667 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007668 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 break;
7670 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007671 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 name);
7673 break;
7674 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007675 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 name);
7677 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007678 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007679 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007680 name);
7681 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 }
7683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684
7685 name[len] = cc;
7686 if (fname != name && fname != fname_buf)
7687 vim_free(fname);
7688
7689 return ret;
7690}
7691
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007692/*
7693 * Give an error message with a function name. Handle <SNR> things.
7694 */
7695 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007696emsg_funcname(ermsg, name)
7697 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007698 char_u *name;
7699{
7700 char_u *p;
7701
7702 if (*name == K_SPECIAL)
7703 p = concat_str((char_u *)"<SNR>", name + 3);
7704 else
7705 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007706 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007707 if (p != name)
7708 vim_free(p);
7709}
7710
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711/*********************************************
7712 * Implementation of the built-in functions
7713 */
7714
7715/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007716 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 */
7718 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007719f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007720 typval_T *argvars;
7721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007725 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007728 if ((l = argvars[0].vval.v_list) != NULL
7729 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7730 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007732 }
7733 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007734 EMSG(_(e_listreq));
7735}
7736
7737/*
7738 * "append(lnum, string/list)" function
7739 */
7740 static void
7741f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 typval_T *argvars;
7743 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007744{
7745 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007746 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007747 list_T *l = NULL;
7748 listitem_T *li = NULL;
7749 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007750 long added = 0;
7751
Bram Moolenaar0d660222005-01-07 21:51:51 +00007752 lnum = get_tv_lnum(argvars);
7753 if (lnum >= 0
7754 && lnum <= curbuf->b_ml.ml_line_count
7755 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007757 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007758 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007759 l = argvars[1].vval.v_list;
7760 if (l == NULL)
7761 return;
7762 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007763 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007764 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007765 for (;;)
7766 {
7767 if (l == NULL)
7768 tv = &argvars[1]; /* append a string */
7769 else if (li == NULL)
7770 break; /* end of list */
7771 else
7772 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007773 line = get_tv_string_chk(tv);
7774 if (line == NULL) /* type error */
7775 {
7776 rettv->vval.v_number = 1; /* Failed */
7777 break;
7778 }
7779 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007780 ++added;
7781 if (l == NULL)
7782 break;
7783 li = li->li_next;
7784 }
7785
7786 appended_lines_mark(lnum, added);
7787 if (curwin->w_cursor.lnum > lnum)
7788 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 else
7791 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792}
7793
7794/*
7795 * "argc()" function
7796 */
7797/* ARGSUSED */
7798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007799f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007800 typval_T *argvars;
7801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007803 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804}
7805
7806/*
7807 * "argidx()" function
7808 */
7809/* ARGSUSED */
7810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 typval_T *argvars;
7813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816}
7817
7818/*
7819 * "argv(nr)" function
7820 */
7821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007823 typval_T *argvars;
7824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 int idx;
7827
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007828 if (argvars[0].v_type != VAR_UNKNOWN)
7829 {
7830 idx = get_tv_number_chk(&argvars[0], NULL);
7831 if (idx >= 0 && idx < ARGCOUNT)
7832 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7833 else
7834 rettv->vval.v_string = NULL;
7835 rettv->v_type = VAR_STRING;
7836 }
7837 else if (rettv_list_alloc(rettv) == OK)
7838 for (idx = 0; idx < ARGCOUNT; ++idx)
7839 list_append_string(rettv->vval.v_list,
7840 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841}
7842
7843/*
7844 * "browse(save, title, initdir, default)" function
7845 */
7846/* ARGSUSED */
7847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007849 typval_T *argvars;
7850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
7852#ifdef FEAT_BROWSE
7853 int save;
7854 char_u *title;
7855 char_u *initdir;
7856 char_u *defname;
7857 char_u buf[NUMBUFLEN];
7858 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007859 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007861 save = get_tv_number_chk(&argvars[0], &error);
7862 title = get_tv_string_chk(&argvars[1]);
7863 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7864 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007866 if (error || title == NULL || initdir == NULL || defname == NULL)
7867 rettv->vval.v_string = NULL;
7868 else
7869 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007870 do_browse(save ? BROWSE_SAVE : 0,
7871 title, defname, NULL, initdir, NULL, curbuf);
7872#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007874#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007876}
7877
7878/*
7879 * "browsedir(title, initdir)" function
7880 */
7881/* ARGSUSED */
7882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 typval_T *argvars;
7885 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007886{
7887#ifdef FEAT_BROWSE
7888 char_u *title;
7889 char_u *initdir;
7890 char_u buf[NUMBUFLEN];
7891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007892 title = get_tv_string_chk(&argvars[0]);
7893 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007895 if (title == NULL || initdir == NULL)
7896 rettv->vval.v_string = NULL;
7897 else
7898 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007899 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007901 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904}
7905
Bram Moolenaar33570922005-01-25 22:26:29 +00007906static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007907
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908/*
7909 * Find a buffer by number or exact name.
7910 */
7911 static buf_T *
7912find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
7915 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 if (avar->v_type == VAR_NUMBER)
7918 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007919 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007921 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007922 if (buf == NULL)
7923 {
7924 /* No full path name match, try a match with a URL or a "nofile"
7925 * buffer, these don't use the full path. */
7926 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7927 if (buf->b_fname != NULL
7928 && (path_with_url(buf->b_fname)
7929#ifdef FEAT_QUICKFIX
7930 || bt_nofile(buf)
7931#endif
7932 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007934 break;
7935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 }
7937 return buf;
7938}
7939
7940/*
7941 * "bufexists(expr)" function
7942 */
7943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 typval_T *argvars;
7946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949}
7950
7951/*
7952 * "buflisted(expr)" function
7953 */
7954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 typval_T *argvars;
7957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
7959 buf_T *buf;
7960
7961 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963}
7964
7965/*
7966 * "bufloaded(expr)" function
7967 */
7968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007969f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 typval_T *argvars;
7971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
7973 buf_T *buf;
7974
7975 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977}
7978
Bram Moolenaar33570922005-01-25 22:26:29 +00007979static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007980
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981/*
7982 * Get buffer by number or pattern.
7983 */
7984 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 int save_magic;
7990 char_u *save_cpo;
7991 buf_T *buf;
7992
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 if (tv->v_type == VAR_NUMBER)
7994 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007995 if (tv->v_type != VAR_STRING)
7996 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 if (name == NULL || *name == NUL)
7998 return curbuf;
7999 if (name[0] == '$' && name[1] == NUL)
8000 return lastbuf;
8001
8002 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8003 save_magic = p_magic;
8004 p_magic = TRUE;
8005 save_cpo = p_cpo;
8006 p_cpo = (char_u *)"";
8007
8008 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8009 TRUE, FALSE));
8010
8011 p_magic = save_magic;
8012 p_cpo = save_cpo;
8013
8014 /* If not found, try expanding the name, like done for bufexists(). */
8015 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018 return buf;
8019}
8020
8021/*
8022 * "bufname(expr)" function
8023 */
8024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008025f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008026 typval_T *argvars;
8027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028{
8029 buf_T *buf;
8030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008031 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008033 buf = get_buf_tv(&argvars[0]);
8034 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 --emsg_off;
8040}
8041
8042/*
8043 * "bufnr(expr)" function
8044 */
8045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008046f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008047 typval_T *argvars;
8048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049{
8050 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008051 int error = FALSE;
8052 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008054 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008057 --emsg_off;
8058
8059 /* If the buffer isn't found and the second argument is not zero create a
8060 * new buffer. */
8061 if (buf == NULL
8062 && argvars[1].v_type != VAR_UNKNOWN
8063 && get_tv_number_chk(&argvars[1], &error) != 0
8064 && !error
8065 && (name = get_tv_string_chk(&argvars[0])) != NULL
8066 && !error)
8067 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8068
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073}
8074
8075/*
8076 * "bufwinnr(nr)" function
8077 */
8078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008079f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 typval_T *argvars;
8081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082{
8083#ifdef FEAT_WINDOWS
8084 win_T *wp;
8085 int winnr = 0;
8086#endif
8087 buf_T *buf;
8088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008091 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092#ifdef FEAT_WINDOWS
8093 for (wp = firstwin; wp; wp = wp->w_next)
8094 {
8095 ++winnr;
8096 if (wp->w_buffer == buf)
8097 break;
8098 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008101 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102#endif
8103 --emsg_off;
8104}
8105
8106/*
8107 * "byte2line(byte)" function
8108 */
8109/*ARGSUSED*/
8110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 typval_T *argvars;
8113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114{
8115#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117#else
8118 long boff = 0;
8119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008120 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008122 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 (linenr_T)0, &boff);
8126#endif
8127}
8128
8129/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008130 * "byteidx()" function
8131 */
8132/*ARGSUSED*/
8133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008134f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 typval_T *argvars;
8136 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008137{
8138#ifdef FEAT_MBYTE
8139 char_u *t;
8140#endif
8141 char_u *str;
8142 long idx;
8143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008144 str = get_tv_string_chk(&argvars[0]);
8145 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008146 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008147 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008148 return;
8149
8150#ifdef FEAT_MBYTE
8151 t = str;
8152 for ( ; idx > 0; idx--)
8153 {
8154 if (*t == NUL) /* EOL reached */
8155 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008156 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008157 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008158 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008159#else
8160 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008161 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008162#endif
8163}
8164
8165/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008166 * "call(func, arglist)" function
8167 */
8168 static void
8169f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008170 typval_T *argvars;
8171 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008172{
8173 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008174 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008175 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008177 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008178 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008179
8180 rettv->vval.v_number = 0;
8181 if (argvars[1].v_type != VAR_LIST)
8182 {
8183 EMSG(_(e_listreq));
8184 return;
8185 }
8186 if (argvars[1].vval.v_list == NULL)
8187 return;
8188
8189 if (argvars[0].v_type == VAR_FUNC)
8190 func = argvars[0].vval.v_string;
8191 else
8192 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008193 if (*func == NUL)
8194 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008195
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 if (argvars[2].v_type != VAR_UNKNOWN)
8197 {
8198 if (argvars[2].v_type != VAR_DICT)
8199 {
8200 EMSG(_(e_dictreq));
8201 return;
8202 }
8203 selfdict = argvars[2].vval.v_dict;
8204 }
8205
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008206 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8207 item = item->li_next)
8208 {
8209 if (argc == MAX_FUNC_ARGS)
8210 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008211 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008212 break;
8213 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008214 /* Make a copy of each argument. This is needed to be able to set
8215 * v_lock to VAR_FIXED in the copy without changing the original list.
8216 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008217 copy_tv(&item->li_tv, &argv[argc++]);
8218 }
8219
8220 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008221 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008222 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8223 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008224
8225 /* Free the arguments. */
8226 while (argc > 0)
8227 clear_tv(&argv[--argc]);
8228}
8229
8230/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008231 * "changenr()" function
8232 */
8233/*ARGSUSED*/
8234 static void
8235f_changenr(argvars, rettv)
8236 typval_T *argvars;
8237 typval_T *rettv;
8238{
8239 rettv->vval.v_number = curbuf->b_u_seq_cur;
8240}
8241
8242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 * "char2nr(string)" function
8244 */
8245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008247 typval_T *argvars;
8248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249{
8250#ifdef FEAT_MBYTE
8251 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008252 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 else
8254#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256}
8257
8258/*
8259 * "cindent(lnum)" function
8260 */
8261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008262f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 typval_T *argvars;
8264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266#ifdef FEAT_CINDENT
8267 pos_T pos;
8268 linenr_T lnum;
8269
8270 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8273 {
8274 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008275 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 curwin->w_cursor = pos;
8277 }
8278 else
8279#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008280 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281}
8282
8283/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008284 * "clearmatches()" function
8285 */
8286/*ARGSUSED*/
8287 static void
8288f_clearmatches(argvars, rettv)
8289 typval_T *argvars;
8290 typval_T *rettv;
8291{
8292#ifdef FEAT_SEARCH_EXTRA
8293 clear_matches(curwin);
8294#endif
8295}
8296
8297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 * "col(string)" function
8299 */
8300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008301f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008302 typval_T *argvars;
8303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304{
8305 colnr_T col = 0;
8306 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008307 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008309 fp = var2fpos(&argvars[0], FALSE, &fnum);
8310 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {
8312 if (fp->col == MAXCOL)
8313 {
8314 /* '> can be MAXCOL, get the length of the line then */
8315 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008316 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 else
8318 col = MAXCOL;
8319 }
8320 else
8321 {
8322 col = fp->col + 1;
8323#ifdef FEAT_VIRTUALEDIT
8324 /* col(".") when the cursor is on the NUL at the end of the line
8325 * because of "coladd" can be seen as an extra column. */
8326 if (virtual_active() && fp == &curwin->w_cursor)
8327 {
8328 char_u *p = ml_get_cursor();
8329
8330 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8331 curwin->w_virtcol - curwin->w_cursor.coladd))
8332 {
8333# ifdef FEAT_MBYTE
8334 int l;
8335
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008336 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 col += l;
8338# else
8339 if (*p != NUL && p[1] == NUL)
8340 ++col;
8341# endif
8342 }
8343 }
8344#endif
8345 }
8346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008347 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348}
8349
Bram Moolenaar572cb562005-08-05 21:35:02 +00008350#if defined(FEAT_INS_EXPAND)
8351/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008352 * "complete()" function
8353 */
8354/*ARGSUSED*/
8355 static void
8356f_complete(argvars, rettv)
8357 typval_T *argvars;
8358 typval_T *rettv;
8359{
8360 int startcol;
8361
8362 if ((State & INSERT) == 0)
8363 {
8364 EMSG(_("E785: complete() can only be used in Insert mode"));
8365 return;
8366 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008367
8368 /* Check for undo allowed here, because if something was already inserted
8369 * the line was already saved for undo and this check isn't done. */
8370 if (!undo_allowed())
8371 return;
8372
Bram Moolenaarade00832006-03-10 21:46:58 +00008373 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8374 {
8375 EMSG(_(e_invarg));
8376 return;
8377 }
8378
8379 startcol = get_tv_number_chk(&argvars[0], NULL);
8380 if (startcol <= 0)
8381 return;
8382
8383 set_completion(startcol - 1, argvars[1].vval.v_list);
8384}
8385
8386/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008387 * "complete_add()" function
8388 */
8389/*ARGSUSED*/
8390 static void
8391f_complete_add(argvars, rettv)
8392 typval_T *argvars;
8393 typval_T *rettv;
8394{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008395 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008396}
8397
8398/*
8399 * "complete_check()" function
8400 */
8401/*ARGSUSED*/
8402 static void
8403f_complete_check(argvars, rettv)
8404 typval_T *argvars;
8405 typval_T *rettv;
8406{
8407 int saved = RedrawingDisabled;
8408
8409 RedrawingDisabled = 0;
8410 ins_compl_check_keys(0);
8411 rettv->vval.v_number = compl_interrupted;
8412 RedrawingDisabled = saved;
8413}
8414#endif
8415
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416/*
8417 * "confirm(message, buttons[, default [, type]])" function
8418 */
8419/*ARGSUSED*/
8420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008421f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008422 typval_T *argvars;
8423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
8425#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8426 char_u *message;
8427 char_u *buttons = NULL;
8428 char_u buf[NUMBUFLEN];
8429 char_u buf2[NUMBUFLEN];
8430 int def = 1;
8431 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008432 char_u *typestr;
8433 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008435 message = get_tv_string_chk(&argvars[0]);
8436 if (message == NULL)
8437 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008438 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008440 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8441 if (buttons == NULL)
8442 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008443 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008445 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008446 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008448 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8449 if (typestr == NULL)
8450 error = TRUE;
8451 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008453 switch (TOUPPER_ASC(*typestr))
8454 {
8455 case 'E': type = VIM_ERROR; break;
8456 case 'Q': type = VIM_QUESTION; break;
8457 case 'I': type = VIM_INFO; break;
8458 case 'W': type = VIM_WARNING; break;
8459 case 'G': type = VIM_GENERIC; break;
8460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 }
8462 }
8463 }
8464 }
8465
8466 if (buttons == NULL || *buttons == NUL)
8467 buttons = (char_u *)_("&Ok");
8468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008469 if (error)
8470 rettv->vval.v_number = 0;
8471 else
8472 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 def, NULL);
8474#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008475 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476#endif
8477}
8478
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008479/*
8480 * "copy()" function
8481 */
8482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008484 typval_T *argvars;
8485 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008487 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008488}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
8490/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008491 * "count()" function
8492 */
8493 static void
8494f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 typval_T *argvars;
8496 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008497{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008498 long n = 0;
8499 int ic = FALSE;
8500
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008502 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 listitem_T *li;
8504 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008505 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008506
Bram Moolenaare9a41262005-01-15 22:18:47 +00008507 if ((l = argvars[0].vval.v_list) != NULL)
8508 {
8509 li = l->lv_first;
8510 if (argvars[2].v_type != VAR_UNKNOWN)
8511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008512 int error = FALSE;
8513
8514 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008515 if (argvars[3].v_type != VAR_UNKNOWN)
8516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008517 idx = get_tv_number_chk(&argvars[3], &error);
8518 if (!error)
8519 {
8520 li = list_find(l, idx);
8521 if (li == NULL)
8522 EMSGN(_(e_listidx), idx);
8523 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008524 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008525 if (error)
8526 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008527 }
8528
8529 for ( ; li != NULL; li = li->li_next)
8530 if (tv_equal(&li->li_tv, &argvars[1], ic))
8531 ++n;
8532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008533 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008534 else if (argvars[0].v_type == VAR_DICT)
8535 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 int todo;
8537 dict_T *d;
8538 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008540 if ((d = argvars[0].vval.v_dict) != NULL)
8541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008542 int error = FALSE;
8543
Bram Moolenaare9a41262005-01-15 22:18:47 +00008544 if (argvars[2].v_type != VAR_UNKNOWN)
8545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008546 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008547 if (argvars[3].v_type != VAR_UNKNOWN)
8548 EMSG(_(e_invarg));
8549 }
8550
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008551 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008552 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008553 {
8554 if (!HASHITEM_EMPTY(hi))
8555 {
8556 --todo;
8557 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8558 ++n;
8559 }
8560 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561 }
8562 }
8563 else
8564 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008565 rettv->vval.v_number = n;
8566}
8567
8568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8570 *
8571 * Checks the existence of a cscope connection.
8572 */
8573/*ARGSUSED*/
8574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 typval_T *argvars;
8577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579#ifdef FEAT_CSCOPE
8580 int num = 0;
8581 char_u *dbpath = NULL;
8582 char_u *prepend = NULL;
8583 char_u buf[NUMBUFLEN];
8584
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008585 if (argvars[0].v_type != VAR_UNKNOWN
8586 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 num = (int)get_tv_number(&argvars[0]);
8589 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008590 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 }
8593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#endif
8598}
8599
8600/*
8601 * "cursor(lnum, col)" function
8602 *
8603 * Moves the cursor to the specified line and column
8604 */
8605/*ARGSUSED*/
8606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008608 typval_T *argvars;
8609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
8611 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008612#ifdef FEAT_VIRTUALEDIT
8613 long coladd = 0;
8614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
Bram Moolenaara5525202006-03-02 22:52:09 +00008616 if (argvars[1].v_type == VAR_UNKNOWN)
8617 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008618 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008619
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008620 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008621 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008622 line = pos.lnum;
8623 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008624#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008625 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008626#endif
8627 }
8628 else
8629 {
8630 line = get_tv_lnum(argvars);
8631 col = get_tv_number_chk(&argvars[1], NULL);
8632#ifdef FEAT_VIRTUALEDIT
8633 if (argvars[2].v_type != VAR_UNKNOWN)
8634 coladd = get_tv_number_chk(&argvars[2], NULL);
8635#endif
8636 }
8637 if (line < 0 || col < 0
8638#ifdef FEAT_VIRTUALEDIT
8639 || coladd < 0
8640#endif
8641 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008642 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (line > 0)
8644 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 if (col > 0)
8646 curwin->w_cursor.col = col - 1;
8647#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008648 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649#endif
8650
8651 /* Make sure the cursor is in a valid position. */
8652 check_cursor();
8653#ifdef FEAT_MBYTE
8654 /* Correct cursor for multi-byte character. */
8655 if (has_mbyte)
8656 mb_adjust_cursor();
8657#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008658
8659 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660}
8661
8662/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008663 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 */
8665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008666f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008667 typval_T *argvars;
8668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008670 int noref = 0;
8671
8672 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008673 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008674 if (noref < 0 || noref > 1)
8675 EMSG(_(e_invarg));
8676 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008677 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678}
8679
8680/*
8681 * "delete()" function
8682 */
8683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 typval_T *argvars;
8686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008691 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692}
8693
8694/*
8695 * "did_filetype()" function
8696 */
8697/*ARGSUSED*/
8698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008700 typval_T *argvars;
8701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707#endif
8708}
8709
8710/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008711 * "diff_filler()" function
8712 */
8713/*ARGSUSED*/
8714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008715f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008716 typval_T *argvars;
8717 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008718{
8719#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008721#endif
8722}
8723
8724/*
8725 * "diff_hlID()" function
8726 */
8727/*ARGSUSED*/
8728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 typval_T *argvars;
8731 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008732{
8733#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008735 static linenr_T prev_lnum = 0;
8736 static int changedtick = 0;
8737 static int fnum = 0;
8738 static int change_start = 0;
8739 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00008740 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008741 int filler_lines;
8742 int col;
8743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008744 if (lnum < 0) /* ignore type error in {lnum} arg */
8745 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008746 if (lnum != prev_lnum
8747 || changedtick != curbuf->b_changedtick
8748 || fnum != curbuf->b_fnum)
8749 {
8750 /* New line, buffer, change: need to get the values. */
8751 filler_lines = diff_check(curwin, lnum);
8752 if (filler_lines < 0)
8753 {
8754 if (filler_lines == -1)
8755 {
8756 change_start = MAXCOL;
8757 change_end = -1;
8758 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8759 hlID = HLF_ADD; /* added line */
8760 else
8761 hlID = HLF_CHD; /* changed line */
8762 }
8763 else
8764 hlID = HLF_ADD; /* added line */
8765 }
8766 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008767 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008768 prev_lnum = lnum;
8769 changedtick = curbuf->b_changedtick;
8770 fnum = curbuf->b_fnum;
8771 }
8772
8773 if (hlID == HLF_CHD || hlID == HLF_TXD)
8774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008776 if (col >= change_start && col <= change_end)
8777 hlID = HLF_TXD; /* changed text */
8778 else
8779 hlID = HLF_CHD; /* changed line */
8780 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008781 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008782#endif
8783}
8784
8785/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008786 * "empty({expr})" function
8787 */
8788 static void
8789f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008790 typval_T *argvars;
8791 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008792{
8793 int n;
8794
8795 switch (argvars[0].v_type)
8796 {
8797 case VAR_STRING:
8798 case VAR_FUNC:
8799 n = argvars[0].vval.v_string == NULL
8800 || *argvars[0].vval.v_string == NUL;
8801 break;
8802 case VAR_NUMBER:
8803 n = argvars[0].vval.v_number == 0;
8804 break;
8805 case VAR_LIST:
8806 n = argvars[0].vval.v_list == NULL
8807 || argvars[0].vval.v_list->lv_first == NULL;
8808 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008809 case VAR_DICT:
8810 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008812 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008813 default:
8814 EMSG2(_(e_intern2), "f_empty()");
8815 n = 0;
8816 }
8817
8818 rettv->vval.v_number = n;
8819}
8820
8821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 * "escape({string}, {chars})" function
8823 */
8824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008826 typval_T *argvars;
8827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829 char_u buf[NUMBUFLEN];
8830
Bram Moolenaar758711c2005-02-02 23:11:38 +00008831 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8832 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834}
8835
8836/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008837 * "eval()" function
8838 */
8839/*ARGSUSED*/
8840 static void
8841f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 typval_T *argvars;
8843 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008844{
8845 char_u *s;
8846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008847 s = get_tv_string_chk(&argvars[0]);
8848 if (s != NULL)
8849 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008851 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8852 {
8853 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008854 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008855 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008856 else if (*s != NUL)
8857 EMSG(_(e_trailing));
8858}
8859
8860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 * "eventhandler()" function
8862 */
8863/*ARGSUSED*/
8864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870}
8871
8872/*
8873 * "executable()" function
8874 */
8875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008877 typval_T *argvars;
8878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881}
8882
8883/*
8884 * "exists()" function
8885 */
8886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008887f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008888 typval_T *argvars;
8889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 char_u *p;
8892 char_u *name;
8893 int n = FALSE;
8894 int len = 0;
8895
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008896 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 if (*p == '$') /* environment variable */
8898 {
8899 /* first try "normal" environment variables (fast) */
8900 if (mch_getenv(p + 1) != NULL)
8901 n = TRUE;
8902 else
8903 {
8904 /* try expanding things like $VIM and ${HOME} */
8905 p = expand_env_save(p);
8906 if (p != NULL && *p != '$')
8907 n = TRUE;
8908 vim_free(p);
8909 }
8910 }
8911 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008914 if (*skipwhite(p) != NUL)
8915 n = FALSE; /* trailing garbage */
8916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 else if (*p == '*') /* internal or user defined function */
8918 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008919 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 }
8921 else if (*p == ':')
8922 {
8923 n = cmd_exists(p + 1);
8924 }
8925 else if (*p == '#')
8926 {
8927#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008928 if (p[1] == '#')
8929 n = autocmd_supported(p + 2);
8930 else
8931 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932#endif
8933 }
8934 else /* internal variable */
8935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008936 char_u *tofree;
8937 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008939 /* get_name_len() takes care of expanding curly braces */
8940 name = p;
8941 len = get_name_len(&p, &tofree, TRUE, FALSE);
8942 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008944 if (tofree != NULL)
8945 name = tofree;
8946 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8947 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008949 /* handle d.key, l[idx], f(expr) */
8950 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8951 if (n)
8952 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 }
8954 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008955 if (*p != NUL)
8956 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008958 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 }
8960
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962}
8963
8964/*
8965 * "expand()" function
8966 */
8967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008969 typval_T *argvars;
8970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972 char_u *s;
8973 int len;
8974 char_u *errormsg;
8975 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8976 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008977 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->v_type = VAR_STRING;
8980 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 if (*s == '%' || *s == '#' || *s == '<')
8982 {
8983 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008984 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 --emsg_off;
8986 }
8987 else
8988 {
8989 /* When the optional second argument is non-zero, don't remove matches
8990 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008991 if (argvars[1].v_type != VAR_UNKNOWN
8992 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008994 if (!error)
8995 {
8996 ExpandInit(&xpc);
8997 xpc.xp_context = EXPAND_FILES;
8998 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008999 }
9000 else
9001 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 }
9003}
9004
9005/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009006 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009007 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009008 */
9009 static void
9010f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 typval_T *argvars;
9012 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009013{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009016 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 list_T *l1, *l2;
9018 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009021
Bram Moolenaare9a41262005-01-15 22:18:47 +00009022 l1 = argvars[0].vval.v_list;
9023 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009024 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9025 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009026 {
9027 if (argvars[2].v_type != VAR_UNKNOWN)
9028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009029 before = get_tv_number_chk(&argvars[2], &error);
9030 if (error)
9031 return; /* type error; errmsg already given */
9032
Bram Moolenaar758711c2005-02-02 23:11:38 +00009033 if (before == l1->lv_len)
9034 item = NULL;
9035 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009036 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009037 item = list_find(l1, before);
9038 if (item == NULL)
9039 {
9040 EMSGN(_(e_listidx), before);
9041 return;
9042 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009043 }
9044 }
9045 else
9046 item = NULL;
9047 list_extend(l1, l2, item);
9048
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049 copy_tv(&argvars[0], rettv);
9050 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009051 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009052 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9053 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 dict_T *d1, *d2;
9055 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056 char_u *action;
9057 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009058 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009059 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009060
9061 d1 = argvars[0].vval.v_dict;
9062 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009063 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9064 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009065 {
9066 /* Check the third argument. */
9067 if (argvars[2].v_type != VAR_UNKNOWN)
9068 {
9069 static char *(av[]) = {"keep", "force", "error"};
9070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009071 action = get_tv_string_chk(&argvars[2]);
9072 if (action == NULL)
9073 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009074 for (i = 0; i < 3; ++i)
9075 if (STRCMP(action, av[i]) == 0)
9076 break;
9077 if (i == 3)
9078 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009079 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009080 return;
9081 }
9082 }
9083 else
9084 action = (char_u *)"force";
9085
9086 /* Go over all entries in the second dict and add them to the
9087 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009088 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009091 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009092 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009093 --todo;
9094 di1 = dict_find(d1, hi2->hi_key, -1);
9095 if (di1 == NULL)
9096 {
9097 di1 = dictitem_copy(HI2DI(hi2));
9098 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9099 dictitem_free(di1);
9100 }
9101 else if (*action == 'e')
9102 {
9103 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9104 break;
9105 }
9106 else if (*action == 'f')
9107 {
9108 clear_tv(&di1->di_tv);
9109 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9110 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009111 }
9112 }
9113
Bram Moolenaare9a41262005-01-15 22:18:47 +00009114 copy_tv(&argvars[0], rettv);
9115 }
9116 }
9117 else
9118 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009119}
9120
9121/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009122 * "feedkeys()" function
9123 */
9124/*ARGSUSED*/
9125 static void
9126f_feedkeys(argvars, rettv)
9127 typval_T *argvars;
9128 typval_T *rettv;
9129{
9130 int remap = TRUE;
9131 char_u *keys, *flags;
9132 char_u nbuf[NUMBUFLEN];
9133 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009134 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009135
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009136 /* This is not allowed in the sandbox. If the commands would still be
9137 * executed in the sandbox it would be OK, but it probably happens later,
9138 * when "sandbox" is no longer set. */
9139 if (check_secure())
9140 return;
9141
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009142 rettv->vval.v_number = 0;
9143 keys = get_tv_string(&argvars[0]);
9144 if (*keys != NUL)
9145 {
9146 if (argvars[1].v_type != VAR_UNKNOWN)
9147 {
9148 flags = get_tv_string_buf(&argvars[1], nbuf);
9149 for ( ; *flags != NUL; ++flags)
9150 {
9151 switch (*flags)
9152 {
9153 case 'n': remap = FALSE; break;
9154 case 'm': remap = TRUE; break;
9155 case 't': typed = TRUE; break;
9156 }
9157 }
9158 }
9159
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009160 /* Need to escape K_SPECIAL and CSI before putting the string in the
9161 * typeahead buffer. */
9162 keys_esc = vim_strsave_escape_csi(keys);
9163 if (keys_esc != NULL)
9164 {
9165 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009166 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009167 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009168 if (vgetc_busy)
9169 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009170 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009171 }
9172}
9173
9174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 * "filereadable()" function
9176 */
9177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009179 typval_T *argvars;
9180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182 FILE *fd;
9183 char_u *p;
9184 int n;
9185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9188 {
9189 n = TRUE;
9190 fclose(fd);
9191 }
9192 else
9193 n = FALSE;
9194
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196}
9197
9198/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009199 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 * rights to write into.
9201 */
9202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009204 typval_T *argvars;
9205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009207 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208}
9209
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009210static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009211
9212 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009213findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009214 typval_T *argvars;
9215 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009216 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009217{
9218#ifdef FEAT_SEARCHPATH
9219 char_u *fname;
9220 char_u *fresult = NULL;
9221 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9222 char_u *p;
9223 char_u pathbuf[NUMBUFLEN];
9224 int count = 1;
9225 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009226 int error = FALSE;
9227#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009228
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009229 rettv->vval.v_string = NULL;
9230 rettv->v_type = VAR_STRING;
9231
9232#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009233 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009234
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009235 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9238 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009239 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 else
9241 {
9242 if (*p != NUL)
9243 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009246 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009247 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009248 }
9249
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009250 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9251 error = TRUE;
9252
9253 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 do
9256 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009257 if (rettv->v_type == VAR_STRING)
9258 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009259 fresult = find_file_in_path_option(first ? fname : NULL,
9260 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009261 0, first, path,
9262 find_what,
9263 curbuf->b_ffname,
9264 find_what == FINDFILE_DIR
9265 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009266 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009267
9268 if (fresult != NULL && rettv->v_type == VAR_LIST)
9269 list_append_string(rettv->vval.v_list, fresult, -1);
9270
9271 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009272 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009273
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009274 if (rettv->v_type == VAR_STRING)
9275 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009276#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009277}
9278
Bram Moolenaar33570922005-01-25 22:26:29 +00009279static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9280static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009281
9282/*
9283 * Implementation of map() and filter().
9284 */
9285 static void
9286filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009287 typval_T *argvars;
9288 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009289 int map;
9290{
9291 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009292 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009293 listitem_T *li, *nli;
9294 list_T *l = NULL;
9295 dictitem_T *di;
9296 hashtab_T *ht;
9297 hashitem_T *hi;
9298 dict_T *d = NULL;
9299 typval_T save_val;
9300 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009301 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009302 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009303 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009304 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009305
9306 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009308 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009309 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009310 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009311 return;
9312 }
9313 else if (argvars[0].v_type == VAR_DICT)
9314 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009315 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009316 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009317 return;
9318 }
9319 else
9320 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009321 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009322 return;
9323 }
9324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 expr = get_tv_string_buf_chk(&argvars[1], buf);
9326 /* On type errors, the preceding call has already displayed an error
9327 * message. Avoid a misleading error message for an empty string that
9328 * was not passed as argument. */
9329 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 prepare_vimvar(VV_VAL, &save_val);
9332 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009333
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009334 /* We reset "did_emsg" to be able to detect whether an error
9335 * occurred during evaluation of the expression. */
9336 save_did_emsg = did_emsg;
9337 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009339 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 prepare_vimvar(VV_KEY, &save_key);
9342 vimvars[VV_KEY].vv_type = VAR_STRING;
9343
9344 ht = &d->dv_hashtab;
9345 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009346 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009347 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009349 if (!HASHITEM_EMPTY(hi))
9350 {
9351 --todo;
9352 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009353 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 break;
9355 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009356 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009357 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 break;
9359 if (!map && rem)
9360 dictitem_remove(d, di);
9361 clear_tv(&vimvars[VV_KEY].vv_tv);
9362 }
9363 }
9364 hash_unlock(ht);
9365
9366 restore_vimvar(VV_KEY, &save_key);
9367 }
9368 else
9369 {
9370 for (li = l->lv_first; li != NULL; li = nli)
9371 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009372 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009373 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009374 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009375 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009376 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009377 break;
9378 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009380 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009381 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009383 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009384
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009385 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387
9388 copy_tv(&argvars[0], rettv);
9389}
9390
9391 static int
9392filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009393 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009394 char_u *expr;
9395 int map;
9396 int *remp;
9397{
Bram Moolenaar33570922005-01-25 22:26:29 +00009398 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009399 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009400 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009401
Bram Moolenaar33570922005-01-25 22:26:29 +00009402 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009403 s = expr;
9404 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009405 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009406 if (*s != NUL) /* check for trailing chars after expr */
9407 {
9408 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009409 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009410 }
9411 if (map)
9412 {
9413 /* map(): replace the list item value */
9414 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009415 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009416 *tv = rettv;
9417 }
9418 else
9419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009420 int error = FALSE;
9421
Bram Moolenaare9a41262005-01-15 22:18:47 +00009422 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009424 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009425 /* On type error, nothing has been removed; return FAIL to stop the
9426 * loop. The error message was given by get_tv_number_chk(). */
9427 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009428 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009429 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009430 retval = OK;
9431theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009432 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009433 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009434}
9435
9436/*
9437 * "filter()" function
9438 */
9439 static void
9440f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009441 typval_T *argvars;
9442 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009443{
9444 filter_map(argvars, rettv, FALSE);
9445}
9446
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009447/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009448 * "finddir({fname}[, {path}[, {count}]])" function
9449 */
9450 static void
9451f_finddir(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{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009455 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009456}
9457
9458/*
9459 * "findfile({fname}[, {path}[, {count}]])" function
9460 */
9461 static void
9462f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009463 typval_T *argvars;
9464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009466 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009467}
9468
9469/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00009470 * "fnameescape({string})" function
9471 */
9472 static void
9473f_fnameescape(argvars, rettv)
9474 typval_T *argvars;
9475 typval_T *rettv;
9476{
9477 rettv->vval.v_string = vim_strsave_fnameescape(
9478 get_tv_string(&argvars[0]), FALSE);
9479 rettv->v_type = VAR_STRING;
9480}
9481
9482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 * "fnamemodify({fname}, {mods})" function
9484 */
9485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009487 typval_T *argvars;
9488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
9490 char_u *fname;
9491 char_u *mods;
9492 int usedlen = 0;
9493 int len;
9494 char_u *fbuf = NULL;
9495 char_u buf[NUMBUFLEN];
9496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009497 fname = get_tv_string_chk(&argvars[0]);
9498 mods = get_tv_string_buf_chk(&argvars[1], buf);
9499 if (fname == NULL || mods == NULL)
9500 fname = NULL;
9501 else
9502 {
9503 len = (int)STRLEN(fname);
9504 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 vim_free(fbuf);
9513}
9514
Bram Moolenaar33570922005-01-25 22:26:29 +00009515static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516
9517/*
9518 * "foldclosed()" function
9519 */
9520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009522 typval_T *argvars;
9523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 int end;
9525{
9526#ifdef FEAT_FOLDING
9527 linenr_T lnum;
9528 linenr_T first, last;
9529
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9532 {
9533 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9534 {
9535 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 return;
9540 }
9541 }
9542#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544}
9545
9546/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009547 * "foldclosed()" function
9548 */
9549 static void
9550f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009551 typval_T *argvars;
9552 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009553{
9554 foldclosed_both(argvars, rettv, FALSE);
9555}
9556
9557/*
9558 * "foldclosedend()" function
9559 */
9560 static void
9561f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009562 typval_T *argvars;
9563 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009564{
9565 foldclosed_both(argvars, rettv, TRUE);
9566}
9567
9568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 * "foldlevel()" function
9570 */
9571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009573 typval_T *argvars;
9574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575{
9576#ifdef FEAT_FOLDING
9577 linenr_T lnum;
9578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 else
9583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585}
9586
9587/*
9588 * "foldtext()" function
9589 */
9590/*ARGSUSED*/
9591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009593 typval_T *argvars;
9594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596#ifdef FEAT_FOLDING
9597 linenr_T lnum;
9598 char_u *s;
9599 char_u *r;
9600 int len;
9601 char *txt;
9602#endif
9603
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->v_type = VAR_STRING;
9605 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9608 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9609 <= curbuf->b_ml.ml_line_count
9610 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 {
9612 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9614 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 {
9616 if (!linewhite(lnum))
9617 break;
9618 ++lnum;
9619 }
9620
9621 /* Find interesting text in this line. */
9622 s = skipwhite(ml_get(lnum));
9623 /* skip C comment-start */
9624 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009627 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009628 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009629 {
9630 s = skipwhite(ml_get(lnum + 1));
9631 if (*s == '*')
9632 s = skipwhite(s + 1);
9633 }
9634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 txt = _("+-%s%3ld lines: ");
9636 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009637 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 + 20 /* for %3ld */
9639 + STRLEN(s))); /* concatenated */
9640 if (r != NULL)
9641 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9643 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9644 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 len = (int)STRLEN(r);
9646 STRCAT(r, s);
9647 /* remove 'foldmarker' and 'commentstring' */
9648 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 }
9651 }
9652#endif
9653}
9654
9655/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009656 * "foldtextresult(lnum)" function
9657 */
9658/*ARGSUSED*/
9659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 typval_T *argvars;
9662 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009663{
9664#ifdef FEAT_FOLDING
9665 linenr_T lnum;
9666 char_u *text;
9667 char_u buf[51];
9668 foldinfo_T foldinfo;
9669 int fold_count;
9670#endif
9671
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 rettv->v_type = VAR_STRING;
9673 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009674#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009676 /* treat illegal types and illegal string values for {lnum} the same */
9677 if (lnum < 0)
9678 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009679 fold_count = foldedCount(curwin, lnum, &foldinfo);
9680 if (fold_count > 0)
9681 {
9682 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9683 &foldinfo, buf);
9684 if (text == buf)
9685 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009687 }
9688#endif
9689}
9690
9691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 * "foreground()" function
9693 */
9694/*ARGSUSED*/
9695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009697 typval_T *argvars;
9698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701#ifdef FEAT_GUI
9702 if (gui.in_use)
9703 gui_mch_set_foreground();
9704#else
9705# ifdef WIN32
9706 win32_set_foreground();
9707# endif
9708#endif
9709}
9710
9711/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009712 * "function()" function
9713 */
9714/*ARGSUSED*/
9715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009717 typval_T *argvars;
9718 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009719{
9720 char_u *s;
9721
Bram Moolenaara7043832005-01-21 11:56:39 +00009722 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009723 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009724 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009725 EMSG2(_(e_invarg2), s);
9726 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009727 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009728 else
9729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_string = vim_strsave(s);
9731 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009732 }
9733}
9734
9735/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009736 * "garbagecollect()" function
9737 */
9738/*ARGSUSED*/
9739 static void
9740f_garbagecollect(argvars, rettv)
9741 typval_T *argvars;
9742 typval_T *rettv;
9743{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009744 /* This is postponed until we are back at the toplevel, because we may be
9745 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9746 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00009747
9748 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
9749 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009750}
9751
9752/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009753 * "get()" function
9754 */
9755 static void
9756f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009757 typval_T *argvars;
9758 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009759{
Bram Moolenaar33570922005-01-25 22:26:29 +00009760 listitem_T *li;
9761 list_T *l;
9762 dictitem_T *di;
9763 dict_T *d;
9764 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009765
Bram Moolenaare9a41262005-01-15 22:18:47 +00009766 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009767 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009768 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009770 int error = FALSE;
9771
9772 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9773 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009774 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009775 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009777 else if (argvars[0].v_type == VAR_DICT)
9778 {
9779 if ((d = argvars[0].vval.v_dict) != NULL)
9780 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009781 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009782 if (di != NULL)
9783 tv = &di->di_tv;
9784 }
9785 }
9786 else
9787 EMSG2(_(e_listdictarg), "get()");
9788
9789 if (tv == NULL)
9790 {
9791 if (argvars[2].v_type == VAR_UNKNOWN)
9792 rettv->vval.v_number = 0;
9793 else
9794 copy_tv(&argvars[2], rettv);
9795 }
9796 else
9797 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009798}
9799
Bram Moolenaar342337a2005-07-21 21:11:17 +00009800static 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 +00009801
9802/*
9803 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009804 * Return a range (from start to end) of lines in rettv from the specified
9805 * buffer.
9806 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009807 */
9808 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009809get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009810 buf_T *buf;
9811 linenr_T start;
9812 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009813 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009814 typval_T *rettv;
9815{
9816 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009817
Bram Moolenaar342337a2005-07-21 21:11:17 +00009818 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009819 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009820 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009821 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009822 }
9823 else
9824 rettv->vval.v_number = 0;
9825
9826 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9827 return;
9828
9829 if (!retlist)
9830 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009831 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9832 p = ml_get_buf(buf, start, FALSE);
9833 else
9834 p = (char_u *)"";
9835
9836 rettv->v_type = VAR_STRING;
9837 rettv->vval.v_string = vim_strsave(p);
9838 }
9839 else
9840 {
9841 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009842 return;
9843
9844 if (start < 1)
9845 start = 1;
9846 if (end > buf->b_ml.ml_line_count)
9847 end = buf->b_ml.ml_line_count;
9848 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009849 if (list_append_string(rettv->vval.v_list,
9850 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009851 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009852 }
9853}
9854
9855/*
9856 * "getbufline()" function
9857 */
9858 static void
9859f_getbufline(argvars, rettv)
9860 typval_T *argvars;
9861 typval_T *rettv;
9862{
9863 linenr_T lnum;
9864 linenr_T end;
9865 buf_T *buf;
9866
9867 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9868 ++emsg_off;
9869 buf = get_buf_tv(&argvars[0]);
9870 --emsg_off;
9871
Bram Moolenaar661b1822005-07-28 22:36:45 +00009872 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009873 if (argvars[2].v_type == VAR_UNKNOWN)
9874 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009875 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009876 end = get_tv_lnum_buf(&argvars[2], buf);
9877
Bram Moolenaar342337a2005-07-21 21:11:17 +00009878 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009879}
9880
Bram Moolenaar0d660222005-01-07 21:51:51 +00009881/*
9882 * "getbufvar()" function
9883 */
9884 static void
9885f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009886 typval_T *argvars;
9887 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009888{
9889 buf_T *buf;
9890 buf_T *save_curbuf;
9891 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9895 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009896 ++emsg_off;
9897 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009898
9899 rettv->v_type = VAR_STRING;
9900 rettv->vval.v_string = NULL;
9901
9902 if (buf != NULL && varname != NULL)
9903 {
9904 if (*varname == '&') /* buffer-local-option */
9905 {
9906 /* set curbuf to be our buf, temporarily */
9907 save_curbuf = curbuf;
9908 curbuf = buf;
9909
9910 get_option_tv(&varname, rettv, TRUE);
9911
9912 /* restore previous notion of curbuf */
9913 curbuf = save_curbuf;
9914 }
9915 else
9916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009917 if (*varname == NUL)
9918 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9919 * scope prefix before the NUL byte is required by
9920 * find_var_in_ht(). */
9921 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009922 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009923 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009924 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009925 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009926 }
9927 }
9928
9929 --emsg_off;
9930}
9931
9932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 * "getchar()" function
9934 */
9935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009937 typval_T *argvars;
9938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939{
9940 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009941 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009943 /* Position the cursor. Needed after a message that ends in a space. */
9944 windgoto(msg_row, msg_col);
9945
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 ++no_mapping;
9947 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009948 for (;;)
9949 {
9950 if (argvars[0].v_type == VAR_UNKNOWN)
9951 /* getchar(): blocking wait. */
9952 n = safe_vgetc();
9953 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9954 /* getchar(1): only check if char avail */
9955 n = vpeekc();
9956 else if (error || vpeekc() == NUL)
9957 /* illegal argument or getchar(0) and no char avail: return zero */
9958 n = 0;
9959 else
9960 /* getchar(0) and char avail: return char */
9961 n = safe_vgetc();
9962 if (n == K_IGNORE)
9963 continue;
9964 break;
9965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 --no_mapping;
9967 --allow_keys;
9968
Bram Moolenaar219b8702006-11-01 14:32:36 +00009969 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9970 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9971 vimvars[VV_MOUSE_COL].vv_nr = 0;
9972
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 if (IS_SPECIAL(n) || mod_mask != 0)
9975 {
9976 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9977 int i = 0;
9978
9979 /* Turn a special key into three bytes, plus modifier. */
9980 if (mod_mask != 0)
9981 {
9982 temp[i++] = K_SPECIAL;
9983 temp[i++] = KS_MODIFIER;
9984 temp[i++] = mod_mask;
9985 }
9986 if (IS_SPECIAL(n))
9987 {
9988 temp[i++] = K_SPECIAL;
9989 temp[i++] = K_SECOND(n);
9990 temp[i++] = K_THIRD(n);
9991 }
9992#ifdef FEAT_MBYTE
9993 else if (has_mbyte)
9994 i += (*mb_char2bytes)(n, temp + i);
9995#endif
9996 else
9997 temp[i++] = n;
9998 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009999 rettv->v_type = VAR_STRING;
10000 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010001
10002#ifdef FEAT_MOUSE
10003 if (n == K_LEFTMOUSE
10004 || n == K_LEFTMOUSE_NM
10005 || n == K_LEFTDRAG
10006 || n == K_LEFTRELEASE
10007 || n == K_LEFTRELEASE_NM
10008 || n == K_MIDDLEMOUSE
10009 || n == K_MIDDLEDRAG
10010 || n == K_MIDDLERELEASE
10011 || n == K_RIGHTMOUSE
10012 || n == K_RIGHTDRAG
10013 || n == K_RIGHTRELEASE
10014 || n == K_X1MOUSE
10015 || n == K_X1DRAG
10016 || n == K_X1RELEASE
10017 || n == K_X2MOUSE
10018 || n == K_X2DRAG
10019 || n == K_X2RELEASE
10020 || n == K_MOUSEDOWN
10021 || n == K_MOUSEUP)
10022 {
10023 int row = mouse_row;
10024 int col = mouse_col;
10025 win_T *win;
10026 linenr_T lnum;
10027# ifdef FEAT_WINDOWS
10028 win_T *wp;
10029# endif
10030 int n = 1;
10031
10032 if (row >= 0 && col >= 0)
10033 {
10034 /* Find the window at the mouse coordinates and compute the
10035 * text position. */
10036 win = mouse_find_win(&row, &col);
10037 (void)mouse_comp_pos(win, &row, &col, &lnum);
10038# ifdef FEAT_WINDOWS
10039 for (wp = firstwin; wp != win; wp = wp->w_next)
10040 ++n;
10041# endif
10042 vimvars[VV_MOUSE_WIN].vv_nr = n;
10043 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10044 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10045 }
10046 }
10047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 }
10049}
10050
10051/*
10052 * "getcharmod()" function
10053 */
10054/*ARGSUSED*/
10055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010057 typval_T *argvars;
10058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061}
10062
10063/*
10064 * "getcmdline()" function
10065 */
10066/*ARGSUSED*/
10067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010069 typval_T *argvars;
10070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->v_type = VAR_STRING;
10073 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074}
10075
10076/*
10077 * "getcmdpos()" function
10078 */
10079/*ARGSUSED*/
10080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010082 typval_T *argvars;
10083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010085 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086}
10087
10088/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010089 * "getcmdtype()" function
10090 */
10091/*ARGSUSED*/
10092 static void
10093f_getcmdtype(argvars, rettv)
10094 typval_T *argvars;
10095 typval_T *rettv;
10096{
10097 rettv->v_type = VAR_STRING;
10098 rettv->vval.v_string = alloc(2);
10099 if (rettv->vval.v_string != NULL)
10100 {
10101 rettv->vval.v_string[0] = get_cmdline_type();
10102 rettv->vval.v_string[1] = NUL;
10103 }
10104}
10105
10106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 * "getcwd()" function
10108 */
10109/*ARGSUSED*/
10110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010112 typval_T *argvars;
10113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
10115 char_u cwd[MAXPATHL];
10116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 else
10121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010124 if (rettv->vval.v_string != NULL)
10125 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126#endif
10127 }
10128}
10129
10130/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010131 * "getfontname()" function
10132 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010133/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010136 typval_T *argvars;
10137 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010138{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 rettv->v_type = VAR_STRING;
10140 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010141#ifdef FEAT_GUI
10142 if (gui.in_use)
10143 {
10144 GuiFont font;
10145 char_u *name = NULL;
10146
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010147 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010148 {
10149 /* Get the "Normal" font. Either the name saved by
10150 * hl_set_font_name() or from the font ID. */
10151 font = gui.norm_font;
10152 name = hl_get_font_name();
10153 }
10154 else
10155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010157 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10158 return;
10159 font = gui_mch_get_font(name, FALSE);
10160 if (font == NOFONT)
10161 return; /* Invalid font name, return empty string. */
10162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010163 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010164 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010165 gui_mch_free_font(font);
10166 }
10167#endif
10168}
10169
10170/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010171 * "getfperm({fname})" function
10172 */
10173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010174f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010175 typval_T *argvars;
10176 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010177{
10178 char_u *fname;
10179 struct stat st;
10180 char_u *perm = NULL;
10181 char_u flags[] = "rwx";
10182 int i;
10183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010187 if (mch_stat((char *)fname, &st) >= 0)
10188 {
10189 perm = vim_strsave((char_u *)"---------");
10190 if (perm != NULL)
10191 {
10192 for (i = 0; i < 9; i++)
10193 {
10194 if (st.st_mode & (1 << (8 - i)))
10195 perm[i] = flags[i % 3];
10196 }
10197 }
10198 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010199 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010200}
10201
10202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 * "getfsize({fname})" function
10204 */
10205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010206f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010207 typval_T *argvars;
10208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209{
10210 char_u *fname;
10211 struct stat st;
10212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216
10217 if (mch_stat((char *)fname, &st) >= 0)
10218 {
10219 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010224
10225 /* non-perfect check for overflow */
10226 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10227 rettv->vval.v_number = -2;
10228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 }
10230 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232}
10233
10234/*
10235 * "getftime({fname})" function
10236 */
10237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010239 typval_T *argvars;
10240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241{
10242 char_u *fname;
10243 struct stat st;
10244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010245 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246
10247 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010250 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251}
10252
10253/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010254 * "getftype({fname})" function
10255 */
10256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010258 typval_T *argvars;
10259 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010260{
10261 char_u *fname;
10262 struct stat st;
10263 char_u *type = NULL;
10264 char *t;
10265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010266 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010268 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010269 if (mch_lstat((char *)fname, &st) >= 0)
10270 {
10271#ifdef S_ISREG
10272 if (S_ISREG(st.st_mode))
10273 t = "file";
10274 else if (S_ISDIR(st.st_mode))
10275 t = "dir";
10276# ifdef S_ISLNK
10277 else if (S_ISLNK(st.st_mode))
10278 t = "link";
10279# endif
10280# ifdef S_ISBLK
10281 else if (S_ISBLK(st.st_mode))
10282 t = "bdev";
10283# endif
10284# ifdef S_ISCHR
10285 else if (S_ISCHR(st.st_mode))
10286 t = "cdev";
10287# endif
10288# ifdef S_ISFIFO
10289 else if (S_ISFIFO(st.st_mode))
10290 t = "fifo";
10291# endif
10292# ifdef S_ISSOCK
10293 else if (S_ISSOCK(st.st_mode))
10294 t = "fifo";
10295# endif
10296 else
10297 t = "other";
10298#else
10299# ifdef S_IFMT
10300 switch (st.st_mode & S_IFMT)
10301 {
10302 case S_IFREG: t = "file"; break;
10303 case S_IFDIR: t = "dir"; break;
10304# ifdef S_IFLNK
10305 case S_IFLNK: t = "link"; break;
10306# endif
10307# ifdef S_IFBLK
10308 case S_IFBLK: t = "bdev"; break;
10309# endif
10310# ifdef S_IFCHR
10311 case S_IFCHR: t = "cdev"; break;
10312# endif
10313# ifdef S_IFIFO
10314 case S_IFIFO: t = "fifo"; break;
10315# endif
10316# ifdef S_IFSOCK
10317 case S_IFSOCK: t = "socket"; break;
10318# endif
10319 default: t = "other";
10320 }
10321# else
10322 if (mch_isdir(fname))
10323 t = "dir";
10324 else
10325 t = "file";
10326# endif
10327#endif
10328 type = vim_strsave((char_u *)t);
10329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010330 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010331}
10332
10333/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010334 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010335 */
10336 static void
10337f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010338 typval_T *argvars;
10339 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010340{
10341 linenr_T lnum;
10342 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010343 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010344
10345 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010346 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010347 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010348 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010349 retlist = FALSE;
10350 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010351 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010352 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010353 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010354 retlist = TRUE;
10355 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010356
Bram Moolenaar342337a2005-07-21 21:11:17 +000010357 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010358}
10359
10360/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010361 * "getmatches()" function
10362 */
10363/*ARGSUSED*/
10364 static void
10365f_getmatches(argvars, rettv)
10366 typval_T *argvars;
10367 typval_T *rettv;
10368{
10369#ifdef FEAT_SEARCH_EXTRA
10370 dict_T *dict;
10371 matchitem_T *cur = curwin->w_match_head;
10372
10373 rettv->vval.v_number = 0;
10374
10375 if (rettv_list_alloc(rettv) == OK)
10376 {
10377 while (cur != NULL)
10378 {
10379 dict = dict_alloc();
10380 if (dict == NULL)
10381 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010382 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10383 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10384 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10385 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10386 list_append_dict(rettv->vval.v_list, dict);
10387 cur = cur->next;
10388 }
10389 }
10390#endif
10391}
10392
10393/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000010394 * "getpid()" function
10395 */
10396/*ARGSUSED*/
10397 static void
10398f_getpid(argvars, rettv)
10399 typval_T *argvars;
10400 typval_T *rettv;
10401{
10402 rettv->vval.v_number = mch_get_pid();
10403}
10404
10405/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010406 * "getpos(string)" function
10407 */
10408 static void
10409f_getpos(argvars, rettv)
10410 typval_T *argvars;
10411 typval_T *rettv;
10412{
10413 pos_T *fp;
10414 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010415 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010416
10417 if (rettv_list_alloc(rettv) == OK)
10418 {
10419 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010420 fp = var2fpos(&argvars[0], TRUE, &fnum);
10421 if (fnum != -1)
10422 list_append_number(l, (varnumber_T)fnum);
10423 else
10424 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010425 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10426 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010427 list_append_number(l, (fp != NULL)
10428 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010429 : (varnumber_T)0);
10430 list_append_number(l,
10431#ifdef FEAT_VIRTUALEDIT
10432 (fp != NULL) ? (varnumber_T)fp->coladd :
10433#endif
10434 (varnumber_T)0);
10435 }
10436 else
10437 rettv->vval.v_number = FALSE;
10438}
10439
10440/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010441 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010442 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010443/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010444 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010445f_getqflist(argvars, rettv)
10446 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010447 typval_T *rettv;
10448{
10449#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010450 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010451#endif
10452
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010453 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010454#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010455 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010456 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010457 wp = NULL;
10458 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10459 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010460 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010461 if (wp == NULL)
10462 return;
10463 }
10464
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010465 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010466 }
10467#endif
10468}
10469
10470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 * "getreg()" function
10472 */
10473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010474f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010475 typval_T *argvars;
10476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477{
10478 char_u *strregname;
10479 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010480 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010483 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010485 strregname = get_tv_string_chk(&argvars[0]);
10486 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010487 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010491 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 regname = (strregname == NULL ? '"' : *strregname);
10493 if (regname == 0)
10494 regname = '"';
10495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010496 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010497 rettv->vval.v_string = error ? NULL :
10498 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499}
10500
10501/*
10502 * "getregtype()" function
10503 */
10504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010506 typval_T *argvars;
10507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 char_u *strregname;
10510 int regname;
10511 char_u buf[NUMBUFLEN + 2];
10512 long reglen = 0;
10513
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010514 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010515 {
10516 strregname = get_tv_string_chk(&argvars[0]);
10517 if (strregname == NULL) /* type error; errmsg already given */
10518 {
10519 rettv->v_type = VAR_STRING;
10520 rettv->vval.v_string = NULL;
10521 return;
10522 }
10523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 else
10525 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010526 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527
10528 regname = (strregname == NULL ? '"' : *strregname);
10529 if (regname == 0)
10530 regname = '"';
10531
10532 buf[0] = NUL;
10533 buf[1] = NUL;
10534 switch (get_reg_type(regname, &reglen))
10535 {
10536 case MLINE: buf[0] = 'V'; break;
10537 case MCHAR: buf[0] = 'v'; break;
10538#ifdef FEAT_VISUAL
10539 case MBLOCK:
10540 buf[0] = Ctrl_V;
10541 sprintf((char *)buf + 1, "%ld", reglen + 1);
10542 break;
10543#endif
10544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 rettv->v_type = VAR_STRING;
10546 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547}
10548
10549/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010550 * "gettabwinvar()" function
10551 */
10552 static void
10553f_gettabwinvar(argvars, rettv)
10554 typval_T *argvars;
10555 typval_T *rettv;
10556{
10557 getwinvar(argvars, rettv, 1);
10558}
10559
10560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 * "getwinposx()" function
10562 */
10563/*ARGSUSED*/
10564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570#ifdef FEAT_GUI
10571 if (gui.in_use)
10572 {
10573 int x, y;
10574
10575 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 }
10578#endif
10579}
10580
10581/*
10582 * "getwinposy()" function
10583 */
10584/*ARGSUSED*/
10585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010586f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010587 typval_T *argvars;
10588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591#ifdef FEAT_GUI
10592 if (gui.in_use)
10593 {
10594 int x, y;
10595
10596 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010597 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 }
10599#endif
10600}
10601
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010602/*
10603 * Find window specifed by "vp" in tabpage "tp".
10604 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010605 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010606find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010607 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010608 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010609{
10610#ifdef FEAT_WINDOWS
10611 win_T *wp;
10612#endif
10613 int nr;
10614
10615 nr = get_tv_number_chk(vp, NULL);
10616
10617#ifdef FEAT_WINDOWS
10618 if (nr < 0)
10619 return NULL;
10620 if (nr == 0)
10621 return curwin;
10622
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010623 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10624 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010625 if (--nr <= 0)
10626 break;
10627 return wp;
10628#else
10629 if (nr == 0 || nr == 1)
10630 return curwin;
10631 return NULL;
10632#endif
10633}
10634
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635/*
10636 * "getwinvar()" function
10637 */
10638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010640 typval_T *argvars;
10641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010643 getwinvar(argvars, rettv, 0);
10644}
10645
10646/*
10647 * getwinvar() and gettabwinvar()
10648 */
10649 static void
10650getwinvar(argvars, rettv, off)
10651 typval_T *argvars;
10652 typval_T *rettv;
10653 int off; /* 1 for gettabwinvar() */
10654{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 win_T *win, *oldcurwin;
10656 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010657 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010658 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010660#ifdef FEAT_WINDOWS
10661 if (off == 1)
10662 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10663 else
10664 tp = curtab;
10665#endif
10666 win = find_win_by_nr(&argvars[off], tp);
10667 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010668 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670 rettv->v_type = VAR_STRING;
10671 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672
10673 if (win != NULL && varname != NULL)
10674 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010675 /* Set curwin to be our win, temporarily. Also set curbuf, so
10676 * that we can get buffer-local options. */
10677 oldcurwin = curwin;
10678 curwin = win;
10679 curbuf = win->w_buffer;
10680
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010682 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 else
10684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010685 if (*varname == NUL)
10686 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10687 * scope prefix before the NUL byte is required by
10688 * find_var_in_ht(). */
10689 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010691 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010693 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010695
10696 /* restore previous notion of curwin */
10697 curwin = oldcurwin;
10698 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 }
10700
10701 --emsg_off;
10702}
10703
10704/*
10705 * "glob()" function
10706 */
10707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010709 typval_T *argvars;
10710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
10712 expand_T xpc;
10713
10714 ExpandInit(&xpc);
10715 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010716 rettv->v_type = VAR_STRING;
10717 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719}
10720
10721/*
10722 * "globpath()" function
10723 */
10724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010726 typval_T *argvars;
10727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728{
10729 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010730 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010733 if (file == NULL)
10734 rettv->vval.v_string = NULL;
10735 else
10736 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737}
10738
10739/*
10740 * "has()" function
10741 */
10742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010743f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010744 typval_T *argvars;
10745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746{
10747 int i;
10748 char_u *name;
10749 int n = FALSE;
10750 static char *(has_list[]) =
10751 {
10752#ifdef AMIGA
10753 "amiga",
10754# ifdef FEAT_ARP
10755 "arp",
10756# endif
10757#endif
10758#ifdef __BEOS__
10759 "beos",
10760#endif
10761#ifdef MSDOS
10762# ifdef DJGPP
10763 "dos32",
10764# else
10765 "dos16",
10766# endif
10767#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010768#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 "mac",
10770#endif
10771#if defined(MACOS_X_UNIX)
10772 "macunix",
10773#endif
10774#ifdef OS2
10775 "os2",
10776#endif
10777#ifdef __QNX__
10778 "qnx",
10779#endif
10780#ifdef RISCOS
10781 "riscos",
10782#endif
10783#ifdef UNIX
10784 "unix",
10785#endif
10786#ifdef VMS
10787 "vms",
10788#endif
10789#ifdef WIN16
10790 "win16",
10791#endif
10792#ifdef WIN32
10793 "win32",
10794#endif
10795#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10796 "win32unix",
10797#endif
10798#ifdef WIN64
10799 "win64",
10800#endif
10801#ifdef EBCDIC
10802 "ebcdic",
10803#endif
10804#ifndef CASE_INSENSITIVE_FILENAME
10805 "fname_case",
10806#endif
10807#ifdef FEAT_ARABIC
10808 "arabic",
10809#endif
10810#ifdef FEAT_AUTOCMD
10811 "autocmd",
10812#endif
10813#ifdef FEAT_BEVAL
10814 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010815# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10816 "balloon_multiline",
10817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818#endif
10819#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10820 "builtin_terms",
10821# ifdef ALL_BUILTIN_TCAPS
10822 "all_builtin_terms",
10823# endif
10824#endif
10825#ifdef FEAT_BYTEOFF
10826 "byte_offset",
10827#endif
10828#ifdef FEAT_CINDENT
10829 "cindent",
10830#endif
10831#ifdef FEAT_CLIENTSERVER
10832 "clientserver",
10833#endif
10834#ifdef FEAT_CLIPBOARD
10835 "clipboard",
10836#endif
10837#ifdef FEAT_CMDL_COMPL
10838 "cmdline_compl",
10839#endif
10840#ifdef FEAT_CMDHIST
10841 "cmdline_hist",
10842#endif
10843#ifdef FEAT_COMMENTS
10844 "comments",
10845#endif
10846#ifdef FEAT_CRYPT
10847 "cryptv",
10848#endif
10849#ifdef FEAT_CSCOPE
10850 "cscope",
10851#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010852#ifdef CURSOR_SHAPE
10853 "cursorshape",
10854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855#ifdef DEBUG
10856 "debug",
10857#endif
10858#ifdef FEAT_CON_DIALOG
10859 "dialog_con",
10860#endif
10861#ifdef FEAT_GUI_DIALOG
10862 "dialog_gui",
10863#endif
10864#ifdef FEAT_DIFF
10865 "diff",
10866#endif
10867#ifdef FEAT_DIGRAPHS
10868 "digraphs",
10869#endif
10870#ifdef FEAT_DND
10871 "dnd",
10872#endif
10873#ifdef FEAT_EMACS_TAGS
10874 "emacs_tags",
10875#endif
10876 "eval", /* always present, of course! */
10877#ifdef FEAT_EX_EXTRA
10878 "ex_extra",
10879#endif
10880#ifdef FEAT_SEARCH_EXTRA
10881 "extra_search",
10882#endif
10883#ifdef FEAT_FKMAP
10884 "farsi",
10885#endif
10886#ifdef FEAT_SEARCHPATH
10887 "file_in_path",
10888#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010889#if defined(UNIX) && !defined(USE_SYSTEM)
10890 "filterpipe",
10891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892#ifdef FEAT_FIND_ID
10893 "find_in_path",
10894#endif
10895#ifdef FEAT_FOLDING
10896 "folding",
10897#endif
10898#ifdef FEAT_FOOTER
10899 "footer",
10900#endif
10901#if !defined(USE_SYSTEM) && defined(UNIX)
10902 "fork",
10903#endif
10904#ifdef FEAT_GETTEXT
10905 "gettext",
10906#endif
10907#ifdef FEAT_GUI
10908 "gui",
10909#endif
10910#ifdef FEAT_GUI_ATHENA
10911# ifdef FEAT_GUI_NEXTAW
10912 "gui_neXtaw",
10913# else
10914 "gui_athena",
10915# endif
10916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917#ifdef FEAT_GUI_GTK
10918 "gui_gtk",
10919# ifdef HAVE_GTK2
10920 "gui_gtk2",
10921# endif
10922#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000010923#ifdef FEAT_GUI_GNOME
10924 "gui_gnome",
10925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926#ifdef FEAT_GUI_MAC
10927 "gui_mac",
10928#endif
10929#ifdef FEAT_GUI_MOTIF
10930 "gui_motif",
10931#endif
10932#ifdef FEAT_GUI_PHOTON
10933 "gui_photon",
10934#endif
10935#ifdef FEAT_GUI_W16
10936 "gui_win16",
10937#endif
10938#ifdef FEAT_GUI_W32
10939 "gui_win32",
10940#endif
10941#ifdef FEAT_HANGULIN
10942 "hangul_input",
10943#endif
10944#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10945 "iconv",
10946#endif
10947#ifdef FEAT_INS_EXPAND
10948 "insert_expand",
10949#endif
10950#ifdef FEAT_JUMPLIST
10951 "jumplist",
10952#endif
10953#ifdef FEAT_KEYMAP
10954 "keymap",
10955#endif
10956#ifdef FEAT_LANGMAP
10957 "langmap",
10958#endif
10959#ifdef FEAT_LIBCALL
10960 "libcall",
10961#endif
10962#ifdef FEAT_LINEBREAK
10963 "linebreak",
10964#endif
10965#ifdef FEAT_LISP
10966 "lispindent",
10967#endif
10968#ifdef FEAT_LISTCMDS
10969 "listcmds",
10970#endif
10971#ifdef FEAT_LOCALMAP
10972 "localmap",
10973#endif
10974#ifdef FEAT_MENU
10975 "menu",
10976#endif
10977#ifdef FEAT_SESSION
10978 "mksession",
10979#endif
10980#ifdef FEAT_MODIFY_FNAME
10981 "modify_fname",
10982#endif
10983#ifdef FEAT_MOUSE
10984 "mouse",
10985#endif
10986#ifdef FEAT_MOUSESHAPE
10987 "mouseshape",
10988#endif
10989#if defined(UNIX) || defined(VMS)
10990# ifdef FEAT_MOUSE_DEC
10991 "mouse_dec",
10992# endif
10993# ifdef FEAT_MOUSE_GPM
10994 "mouse_gpm",
10995# endif
10996# ifdef FEAT_MOUSE_JSB
10997 "mouse_jsbterm",
10998# endif
10999# ifdef FEAT_MOUSE_NET
11000 "mouse_netterm",
11001# endif
11002# ifdef FEAT_MOUSE_PTERM
11003 "mouse_pterm",
11004# endif
11005# ifdef FEAT_MOUSE_XTERM
11006 "mouse_xterm",
11007# endif
11008#endif
11009#ifdef FEAT_MBYTE
11010 "multi_byte",
11011#endif
11012#ifdef FEAT_MBYTE_IME
11013 "multi_byte_ime",
11014#endif
11015#ifdef FEAT_MULTI_LANG
11016 "multi_lang",
11017#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011018#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011019#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011020 "mzscheme",
11021#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023#ifdef FEAT_OLE
11024 "ole",
11025#endif
11026#ifdef FEAT_OSFILETYPE
11027 "osfiletype",
11028#endif
11029#ifdef FEAT_PATH_EXTRA
11030 "path_extra",
11031#endif
11032#ifdef FEAT_PERL
11033#ifndef DYNAMIC_PERL
11034 "perl",
11035#endif
11036#endif
11037#ifdef FEAT_PYTHON
11038#ifndef DYNAMIC_PYTHON
11039 "python",
11040#endif
11041#endif
11042#ifdef FEAT_POSTSCRIPT
11043 "postscript",
11044#endif
11045#ifdef FEAT_PRINTER
11046 "printer",
11047#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011048#ifdef FEAT_PROFILE
11049 "profile",
11050#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011051#ifdef FEAT_RELTIME
11052 "reltime",
11053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054#ifdef FEAT_QUICKFIX
11055 "quickfix",
11056#endif
11057#ifdef FEAT_RIGHTLEFT
11058 "rightleft",
11059#endif
11060#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11061 "ruby",
11062#endif
11063#ifdef FEAT_SCROLLBIND
11064 "scrollbind",
11065#endif
11066#ifdef FEAT_CMDL_INFO
11067 "showcmd",
11068 "cmdline_info",
11069#endif
11070#ifdef FEAT_SIGNS
11071 "signs",
11072#endif
11073#ifdef FEAT_SMARTINDENT
11074 "smartindent",
11075#endif
11076#ifdef FEAT_SNIFF
11077 "sniff",
11078#endif
11079#ifdef FEAT_STL_OPT
11080 "statusline",
11081#endif
11082#ifdef FEAT_SUN_WORKSHOP
11083 "sun_workshop",
11084#endif
11085#ifdef FEAT_NETBEANS_INTG
11086 "netbeans_intg",
11087#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011088#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011089 "spell",
11090#endif
11091#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 "syntax",
11093#endif
11094#if defined(USE_SYSTEM) || !defined(UNIX)
11095 "system",
11096#endif
11097#ifdef FEAT_TAG_BINS
11098 "tag_binary",
11099#endif
11100#ifdef FEAT_TAG_OLDSTATIC
11101 "tag_old_static",
11102#endif
11103#ifdef FEAT_TAG_ANYWHITE
11104 "tag_any_white",
11105#endif
11106#ifdef FEAT_TCL
11107# ifndef DYNAMIC_TCL
11108 "tcl",
11109# endif
11110#endif
11111#ifdef TERMINFO
11112 "terminfo",
11113#endif
11114#ifdef FEAT_TERMRESPONSE
11115 "termresponse",
11116#endif
11117#ifdef FEAT_TEXTOBJ
11118 "textobjects",
11119#endif
11120#ifdef HAVE_TGETENT
11121 "tgetent",
11122#endif
11123#ifdef FEAT_TITLE
11124 "title",
11125#endif
11126#ifdef FEAT_TOOLBAR
11127 "toolbar",
11128#endif
11129#ifdef FEAT_USR_CMDS
11130 "user-commands", /* was accidentally included in 5.4 */
11131 "user_commands",
11132#endif
11133#ifdef FEAT_VIMINFO
11134 "viminfo",
11135#endif
11136#ifdef FEAT_VERTSPLIT
11137 "vertsplit",
11138#endif
11139#ifdef FEAT_VIRTUALEDIT
11140 "virtualedit",
11141#endif
11142#ifdef FEAT_VISUAL
11143 "visual",
11144#endif
11145#ifdef FEAT_VISUALEXTRA
11146 "visualextra",
11147#endif
11148#ifdef FEAT_VREPLACE
11149 "vreplace",
11150#endif
11151#ifdef FEAT_WILDIGN
11152 "wildignore",
11153#endif
11154#ifdef FEAT_WILDMENU
11155 "wildmenu",
11156#endif
11157#ifdef FEAT_WINDOWS
11158 "windows",
11159#endif
11160#ifdef FEAT_WAK
11161 "winaltkeys",
11162#endif
11163#ifdef FEAT_WRITEBACKUP
11164 "writebackup",
11165#endif
11166#ifdef FEAT_XIM
11167 "xim",
11168#endif
11169#ifdef FEAT_XFONTSET
11170 "xfontset",
11171#endif
11172#ifdef USE_XSMP
11173 "xsmp",
11174#endif
11175#ifdef USE_XSMP_INTERACT
11176 "xsmp_interact",
11177#endif
11178#ifdef FEAT_XCLIPBOARD
11179 "xterm_clipboard",
11180#endif
11181#ifdef FEAT_XTERM_SAVE
11182 "xterm_save",
11183#endif
11184#if defined(UNIX) && defined(FEAT_X11)
11185 "X11",
11186#endif
11187 NULL
11188 };
11189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 for (i = 0; has_list[i] != NULL; ++i)
11192 if (STRICMP(name, has_list[i]) == 0)
11193 {
11194 n = TRUE;
11195 break;
11196 }
11197
11198 if (n == FALSE)
11199 {
11200 if (STRNICMP(name, "patch", 5) == 0)
11201 n = has_patch(atoi((char *)name + 5));
11202 else if (STRICMP(name, "vim_starting") == 0)
11203 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011204#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11205 else if (STRICMP(name, "balloon_multiline") == 0)
11206 n = multiline_balloon_available();
11207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208#ifdef DYNAMIC_TCL
11209 else if (STRICMP(name, "tcl") == 0)
11210 n = tcl_enabled(FALSE);
11211#endif
11212#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11213 else if (STRICMP(name, "iconv") == 0)
11214 n = iconv_enabled(FALSE);
11215#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011216#ifdef DYNAMIC_MZSCHEME
11217 else if (STRICMP(name, "mzscheme") == 0)
11218 n = mzscheme_enabled(FALSE);
11219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220#ifdef DYNAMIC_RUBY
11221 else if (STRICMP(name, "ruby") == 0)
11222 n = ruby_enabled(FALSE);
11223#endif
11224#ifdef DYNAMIC_PYTHON
11225 else if (STRICMP(name, "python") == 0)
11226 n = python_enabled(FALSE);
11227#endif
11228#ifdef DYNAMIC_PERL
11229 else if (STRICMP(name, "perl") == 0)
11230 n = perl_enabled(FALSE);
11231#endif
11232#ifdef FEAT_GUI
11233 else if (STRICMP(name, "gui_running") == 0)
11234 n = (gui.in_use || gui.starting);
11235# ifdef FEAT_GUI_W32
11236 else if (STRICMP(name, "gui_win32s") == 0)
11237 n = gui_is_win32s();
11238# endif
11239# ifdef FEAT_BROWSE
11240 else if (STRICMP(name, "browse") == 0)
11241 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11242# endif
11243#endif
11244#ifdef FEAT_SYN_HL
11245 else if (STRICMP(name, "syntax_items") == 0)
11246 n = syntax_present(curbuf);
11247#endif
11248#if defined(WIN3264)
11249 else if (STRICMP(name, "win95") == 0)
11250 n = mch_windows95();
11251#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011252#ifdef FEAT_NETBEANS_INTG
11253 else if (STRICMP(name, "netbeans_enabled") == 0)
11254 n = usingNetbeans;
11255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 }
11257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259}
11260
11261/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011262 * "has_key()" function
11263 */
11264 static void
11265f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011266 typval_T *argvars;
11267 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011268{
11269 rettv->vval.v_number = 0;
11270 if (argvars[0].v_type != VAR_DICT)
11271 {
11272 EMSG(_(e_dictreq));
11273 return;
11274 }
11275 if (argvars[0].vval.v_dict == NULL)
11276 return;
11277
11278 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011279 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011280}
11281
11282/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011283 * "haslocaldir()" function
11284 */
11285/*ARGSUSED*/
11286 static void
11287f_haslocaldir(argvars, rettv)
11288 typval_T *argvars;
11289 typval_T *rettv;
11290{
11291 rettv->vval.v_number = (curwin->w_localdir != NULL);
11292}
11293
11294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 * "hasmapto()" function
11296 */
11297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011299 typval_T *argvars;
11300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301{
11302 char_u *name;
11303 char_u *mode;
11304 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011305 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011308 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 mode = (char_u *)"nvo";
11310 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011313 if (argvars[2].v_type != VAR_UNKNOWN)
11314 abbr = get_tv_number(&argvars[2]);
11315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316
Bram Moolenaar2c932302006-03-18 21:42:09 +000011317 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321}
11322
11323/*
11324 * "histadd()" function
11325 */
11326/*ARGSUSED*/
11327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 typval_T *argvars;
11330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331{
11332#ifdef FEAT_CMDHIST
11333 int histype;
11334 char_u *str;
11335 char_u buf[NUMBUFLEN];
11336#endif
11337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 if (check_restricted() || check_secure())
11340 return;
11341#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11343 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 if (histype >= 0)
11345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 if (*str != NUL)
11348 {
11349 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 return;
11352 }
11353 }
11354#endif
11355}
11356
11357/*
11358 * "histdel()" function
11359 */
11360/*ARGSUSED*/
11361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *argvars;
11364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365{
11366#ifdef FEAT_CMDHIST
11367 int n;
11368 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11372 if (str == NULL)
11373 n = 0;
11374 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011376 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011377 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 else
11382 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 get_tv_string_buf(&argvars[1], buf));
11385 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388#endif
11389}
11390
11391/*
11392 * "histget()" function
11393 */
11394/*ARGSUSED*/
11395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *argvars;
11398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
11400#ifdef FEAT_CMDHIST
11401 int type;
11402 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11406 if (str == NULL)
11407 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011409 {
11410 type = get_histtype(str);
11411 if (argvars[1].v_type == VAR_UNKNOWN)
11412 idx = get_history_idx(type);
11413 else
11414 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11415 /* -1 on type error */
11416 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422}
11423
11424/*
11425 * "histnr()" function
11426 */
11427/*ARGSUSED*/
11428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011430 typval_T *argvars;
11431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432{
11433 int i;
11434
11435#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 char_u *history = get_tv_string_chk(&argvars[0]);
11437
11438 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 if (i >= HIST_CMD && i < HIST_COUNT)
11440 i = get_history_idx(i);
11441 else
11442#endif
11443 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445}
11446
11447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 * "highlightID(name)" function
11449 */
11450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011452 typval_T *argvars;
11453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456}
11457
11458/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011459 * "highlight_exists()" function
11460 */
11461 static void
11462f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011463 typval_T *argvars;
11464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011465{
11466 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11467}
11468
11469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 * "hostname()" function
11471 */
11472/*ARGSUSED*/
11473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011475 typval_T *argvars;
11476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477{
11478 char_u hostname[256];
11479
11480 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 rettv->v_type = VAR_STRING;
11482 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483}
11484
11485/*
11486 * iconv() function
11487 */
11488/*ARGSUSED*/
11489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490f_iconv(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#ifdef FEAT_MBYTE
11495 char_u buf1[NUMBUFLEN];
11496 char_u buf2[NUMBUFLEN];
11497 char_u *from, *to, *str;
11498 vimconv_T vimconv;
11499#endif
11500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->v_type = VAR_STRING;
11502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503
11504#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011505 str = get_tv_string(&argvars[0]);
11506 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11507 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 vimconv.vc_type = CONV_NONE;
11509 convert_setup(&vimconv, from, to);
11510
11511 /* If the encodings are equal, no conversion needed. */
11512 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516
11517 convert_setup(&vimconv, NULL, NULL);
11518 vim_free(from);
11519 vim_free(to);
11520#endif
11521}
11522
11523/*
11524 * "indent()" function
11525 */
11526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 typval_T *argvars;
11529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531 linenr_T lnum;
11532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538}
11539
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011540/*
11541 * "index()" function
11542 */
11543 static void
11544f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011545 typval_T *argvars;
11546 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011547{
Bram Moolenaar33570922005-01-25 22:26:29 +000011548 list_T *l;
11549 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011550 long idx = 0;
11551 int ic = FALSE;
11552
11553 rettv->vval.v_number = -1;
11554 if (argvars[0].v_type != VAR_LIST)
11555 {
11556 EMSG(_(e_listreq));
11557 return;
11558 }
11559 l = argvars[0].vval.v_list;
11560 if (l != NULL)
11561 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011562 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011563 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011564 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011565 int error = FALSE;
11566
Bram Moolenaar758711c2005-02-02 23:11:38 +000011567 /* Start at specified item. Use the cached index that list_find()
11568 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011569 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011570 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011571 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011572 ic = get_tv_number_chk(&argvars[3], &error);
11573 if (error)
11574 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011575 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011576
Bram Moolenaar758711c2005-02-02 23:11:38 +000011577 for ( ; item != NULL; item = item->li_next, ++idx)
11578 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011579 {
11580 rettv->vval.v_number = idx;
11581 break;
11582 }
11583 }
11584}
11585
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586static int inputsecret_flag = 0;
11587
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011588static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11589
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011591 * This function is used by f_input() and f_inputdialog() functions. The third
11592 * argument to f_input() specifies the type of completion to use at the
11593 * prompt. The third argument to f_inputdialog() specifies the value to return
11594 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 */
11596 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011597get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011598 typval_T *argvars;
11599 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011600 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011602 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 char_u *p = NULL;
11604 int c;
11605 char_u buf[NUMBUFLEN];
11606 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011607 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011608 int xp_type = EXPAND_NOTHING;
11609 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011612 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613
11614#ifdef NO_CONSOLE_INPUT
11615 /* While starting up, there is no place to enter text. */
11616 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618#endif
11619
11620 cmd_silent = FALSE; /* Want to see the prompt. */
11621 if (prompt != NULL)
11622 {
11623 /* Only the part of the message after the last NL is considered as
11624 * prompt for the command line */
11625 p = vim_strrchr(prompt, '\n');
11626 if (p == NULL)
11627 p = prompt;
11628 else
11629 {
11630 ++p;
11631 c = *p;
11632 *p = NUL;
11633 msg_start();
11634 msg_clr_eos();
11635 msg_puts_attr(prompt, echo_attr);
11636 msg_didout = FALSE;
11637 msg_starthere();
11638 *p = c;
11639 }
11640 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011642 if (argvars[1].v_type != VAR_UNKNOWN)
11643 {
11644 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11645 if (defstr != NULL)
11646 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011648 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011649 {
11650 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011651 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011652 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011653
Bram Moolenaar4463f292005-09-25 22:20:24 +000011654 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011655
Bram Moolenaar4463f292005-09-25 22:20:24 +000011656 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11657 if (xp_name == NULL)
11658 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011660 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011661
Bram Moolenaar4463f292005-09-25 22:20:24 +000011662 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11663 &xp_arg) == FAIL)
11664 return;
11665 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011666 }
11667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011668 if (defstr != NULL)
11669 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011670 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11671 xp_type, xp_arg);
11672
11673 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 /* since the user typed this, no need to wait for return */
11676 need_wait_return = FALSE;
11677 msg_didout = FALSE;
11678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 cmd_silent = cmd_silent_save;
11680}
11681
11682/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011683 * "input()" function
11684 * Also handles inputsecret() when inputsecret is set.
11685 */
11686 static void
11687f_input(argvars, rettv)
11688 typval_T *argvars;
11689 typval_T *rettv;
11690{
11691 get_user_input(argvars, rettv, FALSE);
11692}
11693
11694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695 * "inputdialog()" function
11696 */
11697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011698f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011699 typval_T *argvars;
11700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701{
11702#if defined(FEAT_GUI_TEXTDIALOG)
11703 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11704 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11705 {
11706 char_u *message;
11707 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011708 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 message = get_tv_string_chk(&argvars[0]);
11711 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011712 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011713 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 else
11715 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 if (message != NULL && defstr != NULL
11717 && do_dialog(VIM_QUESTION, NULL, message,
11718 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011719 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 else
11721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011722 if (message != NULL && defstr != NULL
11723 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011724 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725 rettv->vval.v_string = vim_strsave(
11726 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011728 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 }
11732 else
11733#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011734 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735}
11736
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011737/*
11738 * "inputlist()" function
11739 */
11740 static void
11741f_inputlist(argvars, rettv)
11742 typval_T *argvars;
11743 typval_T *rettv;
11744{
11745 listitem_T *li;
11746 int selected;
11747 int mouse_used;
11748
11749 rettv->vval.v_number = 0;
11750#ifdef NO_CONSOLE_INPUT
11751 /* While starting up, there is no place to enter text. */
11752 if (no_console_input())
11753 return;
11754#endif
11755 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11756 {
11757 EMSG2(_(e_listarg), "inputlist()");
11758 return;
11759 }
11760
11761 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011762 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011763 lines_left = Rows; /* avoid more prompt */
11764 msg_scroll = TRUE;
11765 msg_clr_eos();
11766
11767 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11768 {
11769 msg_puts(get_tv_string(&li->li_tv));
11770 msg_putchar('\n');
11771 }
11772
11773 /* Ask for choice. */
11774 selected = prompt_for_number(&mouse_used);
11775 if (mouse_used)
11776 selected -= lines_left;
11777
11778 rettv->vval.v_number = selected;
11779}
11780
11781
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11783
11784/*
11785 * "inputrestore()" function
11786 */
11787/*ARGSUSED*/
11788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011789f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011790 typval_T *argvars;
11791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792{
11793 if (ga_userinput.ga_len > 0)
11794 {
11795 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11797 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011798 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 }
11800 else if (p_verbose > 1)
11801 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011802 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 }
11805}
11806
11807/*
11808 * "inputsave()" function
11809 */
11810/*ARGSUSED*/
11811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011813 typval_T *argvars;
11814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815{
11816 /* Add an entry to the stack of typehead storage. */
11817 if (ga_grow(&ga_userinput, 1) == OK)
11818 {
11819 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11820 + ga_userinput.ga_len);
11821 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 }
11824 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826}
11827
11828/*
11829 * "inputsecret()" function
11830 */
11831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011833 typval_T *argvars;
11834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835{
11836 ++cmdline_star;
11837 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 --cmdline_star;
11840 --inputsecret_flag;
11841}
11842
11843/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011844 * "insert()" function
11845 */
11846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011848 typval_T *argvars;
11849 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011850{
11851 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011852 listitem_T *item;
11853 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011854 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011855
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011856 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011857 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011858 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011859 else if ((l = argvars[0].vval.v_list) != NULL
11860 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011861 {
11862 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011863 before = get_tv_number_chk(&argvars[2], &error);
11864 if (error)
11865 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011866
Bram Moolenaar758711c2005-02-02 23:11:38 +000011867 if (before == l->lv_len)
11868 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011869 else
11870 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011871 item = list_find(l, before);
11872 if (item == NULL)
11873 {
11874 EMSGN(_(e_listidx), before);
11875 l = NULL;
11876 }
11877 }
11878 if (l != NULL)
11879 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011880 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011881 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011882 }
11883 }
11884}
11885
11886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 * "isdirectory()" function
11888 */
11889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011891 typval_T *argvars;
11892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011894 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895}
11896
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011897/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011898 * "islocked()" function
11899 */
11900 static void
11901f_islocked(argvars, rettv)
11902 typval_T *argvars;
11903 typval_T *rettv;
11904{
11905 lval_T lv;
11906 char_u *end;
11907 dictitem_T *di;
11908
11909 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011910 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11911 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011912 if (end != NULL && lv.ll_name != NULL)
11913 {
11914 if (*end != NUL)
11915 EMSG(_(e_trailing));
11916 else
11917 {
11918 if (lv.ll_tv == NULL)
11919 {
11920 if (check_changedtick(lv.ll_name))
11921 rettv->vval.v_number = 1; /* always locked */
11922 else
11923 {
11924 di = find_var(lv.ll_name, NULL);
11925 if (di != NULL)
11926 {
11927 /* Consider a variable locked when:
11928 * 1. the variable itself is locked
11929 * 2. the value of the variable is locked.
11930 * 3. the List or Dict value is locked.
11931 */
11932 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11933 || tv_islocked(&di->di_tv));
11934 }
11935 }
11936 }
11937 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011938 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011939 else if (lv.ll_newkey != NULL)
11940 EMSG2(_(e_dictkey), lv.ll_newkey);
11941 else if (lv.ll_list != NULL)
11942 /* List item. */
11943 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11944 else
11945 /* Dictionary item. */
11946 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11947 }
11948 }
11949
11950 clear_lval(&lv);
11951}
11952
Bram Moolenaar33570922005-01-25 22:26:29 +000011953static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011954
11955/*
11956 * Turn a dict into a list:
11957 * "what" == 0: list of keys
11958 * "what" == 1: list of values
11959 * "what" == 2: list of items
11960 */
11961 static void
11962dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011963 typval_T *argvars;
11964 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011965 int what;
11966{
Bram Moolenaar33570922005-01-25 22:26:29 +000011967 list_T *l2;
11968 dictitem_T *di;
11969 hashitem_T *hi;
11970 listitem_T *li;
11971 listitem_T *li2;
11972 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011973 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011974
11975 rettv->vval.v_number = 0;
11976 if (argvars[0].v_type != VAR_DICT)
11977 {
11978 EMSG(_(e_dictreq));
11979 return;
11980 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011981 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011982 return;
11983
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011984 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011985 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011986
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011987 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011988 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011989 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011990 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011991 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011992 --todo;
11993 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011994
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011995 li = listitem_alloc();
11996 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011997 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011998 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011999
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012000 if (what == 0)
12001 {
12002 /* keys() */
12003 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012004 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012005 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12006 }
12007 else if (what == 1)
12008 {
12009 /* values() */
12010 copy_tv(&di->di_tv, &li->li_tv);
12011 }
12012 else
12013 {
12014 /* items() */
12015 l2 = list_alloc();
12016 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012017 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012018 li->li_tv.vval.v_list = l2;
12019 if (l2 == NULL)
12020 break;
12021 ++l2->lv_refcount;
12022
12023 li2 = listitem_alloc();
12024 if (li2 == NULL)
12025 break;
12026 list_append(l2, li2);
12027 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012028 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012029 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12030
12031 li2 = listitem_alloc();
12032 if (li2 == NULL)
12033 break;
12034 list_append(l2, li2);
12035 copy_tv(&di->di_tv, &li2->li_tv);
12036 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012037 }
12038 }
12039}
12040
12041/*
12042 * "items(dict)" function
12043 */
12044 static void
12045f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012046 typval_T *argvars;
12047 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012048{
12049 dict_list(argvars, rettv, 2);
12050}
12051
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012053 * "join()" function
12054 */
12055 static void
12056f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012057 typval_T *argvars;
12058 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012059{
12060 garray_T ga;
12061 char_u *sep;
12062
12063 rettv->vval.v_number = 0;
12064 if (argvars[0].v_type != VAR_LIST)
12065 {
12066 EMSG(_(e_listreq));
12067 return;
12068 }
12069 if (argvars[0].vval.v_list == NULL)
12070 return;
12071 if (argvars[1].v_type == VAR_UNKNOWN)
12072 sep = (char_u *)" ";
12073 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012074 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012075
12076 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077
12078 if (sep != NULL)
12079 {
12080 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012081 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012082 ga_append(&ga, NUL);
12083 rettv->vval.v_string = (char_u *)ga.ga_data;
12084 }
12085 else
12086 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012087}
12088
12089/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012090 * "keys()" function
12091 */
12092 static void
12093f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012096{
12097 dict_list(argvars, rettv, 0);
12098}
12099
12100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 * "last_buffer_nr()" function.
12102 */
12103/*ARGSUSED*/
12104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 typval_T *argvars;
12107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108{
12109 int n = 0;
12110 buf_T *buf;
12111
12112 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12113 if (n < buf->b_fnum)
12114 n = buf->b_fnum;
12115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012117}
12118
12119/*
12120 * "len()" function
12121 */
12122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012124 typval_T *argvars;
12125 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012126{
12127 switch (argvars[0].v_type)
12128 {
12129 case VAR_STRING:
12130 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131 rettv->vval.v_number = (varnumber_T)STRLEN(
12132 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012133 break;
12134 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012135 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012136 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012137 case VAR_DICT:
12138 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12139 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012140 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012141 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012142 break;
12143 }
12144}
12145
Bram Moolenaar33570922005-01-25 22:26:29 +000012146static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012147
12148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012150 typval_T *argvars;
12151 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012152 int type;
12153{
12154#ifdef FEAT_LIBCALL
12155 char_u *string_in;
12156 char_u **string_result;
12157 int nr_result;
12158#endif
12159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012161 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012162 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012163 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012165
12166 if (check_restricted() || check_secure())
12167 return;
12168
12169#ifdef FEAT_LIBCALL
12170 /* The first two args must be strings, otherwise its meaningless */
12171 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12172 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012173 string_in = NULL;
12174 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012175 string_in = argvars[2].vval.v_string;
12176 if (type == VAR_NUMBER)
12177 string_result = NULL;
12178 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012180 if (mch_libcall(argvars[0].vval.v_string,
12181 argvars[1].vval.v_string,
12182 string_in,
12183 argvars[2].vval.v_number,
12184 string_result,
12185 &nr_result) == OK
12186 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012187 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012188 }
12189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190}
12191
12192/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012193 * "libcall()" function
12194 */
12195 static void
12196f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012197 typval_T *argvars;
12198 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012199{
12200 libcall_common(argvars, rettv, VAR_STRING);
12201}
12202
12203/*
12204 * "libcallnr()" function
12205 */
12206 static void
12207f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012208 typval_T *argvars;
12209 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012210{
12211 libcall_common(argvars, rettv, VAR_NUMBER);
12212}
12213
12214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 * "line(string)" function
12216 */
12217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012218f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012219 typval_T *argvars;
12220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221{
12222 linenr_T lnum = 0;
12223 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012224 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012226 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 if (fp != NULL)
12228 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230}
12231
12232/*
12233 * "line2byte(lnum)" function
12234 */
12235/*ARGSUSED*/
12236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012238 typval_T *argvars;
12239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240{
12241#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012242 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243#else
12244 linenr_T lnum;
12245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012246 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012248 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12251 if (rettv->vval.v_number >= 0)
12252 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253#endif
12254}
12255
12256/*
12257 * "lispindent(lnum)" function
12258 */
12259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012261 typval_T *argvars;
12262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263{
12264#ifdef FEAT_LISP
12265 pos_T pos;
12266 linenr_T lnum;
12267
12268 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012269 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12271 {
12272 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 curwin->w_cursor = pos;
12275 }
12276 else
12277#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279}
12280
12281/*
12282 * "localtime()" function
12283 */
12284/*ARGSUSED*/
12285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012287 typval_T *argvars;
12288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291}
12292
Bram Moolenaar33570922005-01-25 22:26:29 +000012293static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294
12295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012297 typval_T *argvars;
12298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 int exact;
12300{
12301 char_u *keys;
12302 char_u *which;
12303 char_u buf[NUMBUFLEN];
12304 char_u *keys_buf = NULL;
12305 char_u *rhs;
12306 int mode;
12307 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012308 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309
12310 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311 rettv->v_type = VAR_STRING;
12312 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012314 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 if (*keys == NUL)
12316 return;
12317
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012318 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012320 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012321 if (argvars[2].v_type != VAR_UNKNOWN)
12322 abbr = get_tv_number(&argvars[2]);
12323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 else
12325 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012326 if (which == NULL)
12327 return;
12328
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 mode = get_map_mode(&which, 0);
12330
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012331 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012332 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 vim_free(keys_buf);
12334 if (rhs != NULL)
12335 {
12336 ga_init(&ga);
12337 ga.ga_itemsize = 1;
12338 ga.ga_growsize = 40;
12339
12340 while (*rhs != NUL)
12341 ga_concat(&ga, str2special(&rhs, FALSE));
12342
12343 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 }
12346}
12347
12348/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012349 * "map()" function
12350 */
12351 static void
12352f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012353 typval_T *argvars;
12354 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012355{
12356 filter_map(argvars, rettv, TRUE);
12357}
12358
12359/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012360 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 */
12362 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012363f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012364 typval_T *argvars;
12365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012367 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368}
12369
12370/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 */
12373 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012374f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012375 typval_T *argvars;
12376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012378 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379}
12380
Bram Moolenaar33570922005-01-25 22:26:29 +000012381static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382
12383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012385 typval_T *argvars;
12386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 int type;
12388{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012389 char_u *str = NULL;
12390 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391 char_u *pat;
12392 regmatch_T regmatch;
12393 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012394 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 char_u *save_cpo;
12396 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012397 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012398 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012399 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012400 list_T *l = NULL;
12401 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012402 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012403 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404
12405 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12406 save_cpo = p_cpo;
12407 p_cpo = (char_u *)"";
12408
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012409 rettv->vval.v_number = -1;
12410 if (type == 3)
12411 {
12412 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012413 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012414 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012415 }
12416 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012418 rettv->v_type = VAR_STRING;
12419 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012422 if (argvars[0].v_type == VAR_LIST)
12423 {
12424 if ((l = argvars[0].vval.v_list) == NULL)
12425 goto theend;
12426 li = l->lv_first;
12427 }
12428 else
12429 expr = str = get_tv_string(&argvars[0]);
12430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012431 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12432 if (pat == NULL)
12433 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012435 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012437 int error = FALSE;
12438
12439 start = get_tv_number_chk(&argvars[2], &error);
12440 if (error)
12441 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012442 if (l != NULL)
12443 {
12444 li = list_find(l, start);
12445 if (li == NULL)
12446 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012447 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012448 }
12449 else
12450 {
12451 if (start < 0)
12452 start = 0;
12453 if (start > (long)STRLEN(str))
12454 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012455 /* When "count" argument is there ignore matches before "start",
12456 * otherwise skip part of the string. Differs when pattern is "^"
12457 * or "\<". */
12458 if (argvars[3].v_type != VAR_UNKNOWN)
12459 startcol = start;
12460 else
12461 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012462 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012463
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012464 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012465 nth = get_tv_number_chk(&argvars[3], &error);
12466 if (error)
12467 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468 }
12469
12470 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12471 if (regmatch.regprog != NULL)
12472 {
12473 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012474
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012475 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012476 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012477 if (l != NULL)
12478 {
12479 if (li == NULL)
12480 {
12481 match = FALSE;
12482 break;
12483 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012484 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012485 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012486 if (str == NULL)
12487 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012488 }
12489
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012490 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012491
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012492 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012493 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012494 if (l == NULL && !match)
12495 break;
12496
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012497 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012498 if (l != NULL)
12499 {
12500 li = li->li_next;
12501 ++idx;
12502 }
12503 else
12504 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012505#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012506 startcol = (colnr_T)(regmatch.startp[0]
12507 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012508#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012509 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012510#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012511 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012512 }
12513
12514 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012516 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012517 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012518 int i;
12519
12520 /* return list with matched string and submatches */
12521 for (i = 0; i < NSUBEXP; ++i)
12522 {
12523 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012524 {
12525 if (list_append_string(rettv->vval.v_list,
12526 (char_u *)"", 0) == FAIL)
12527 break;
12528 }
12529 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012530 regmatch.startp[i],
12531 (int)(regmatch.endp[i] - regmatch.startp[i]))
12532 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012533 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012534 }
12535 }
12536 else if (type == 2)
12537 {
12538 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012539 if (l != NULL)
12540 copy_tv(&li->li_tv, rettv);
12541 else
12542 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012544 }
12545 else if (l != NULL)
12546 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547 else
12548 {
12549 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012550 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 (varnumber_T)(regmatch.startp[0] - str);
12552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012555 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 }
12557 }
12558 vim_free(regmatch.regprog);
12559 }
12560
12561theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012562 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563 p_cpo = save_cpo;
12564}
12565
12566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012567 * "match()" function
12568 */
12569 static void
12570f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012571 typval_T *argvars;
12572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012573{
12574 find_some_match(argvars, rettv, 1);
12575}
12576
12577/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012578 * "matchadd()" function
12579 */
12580 static void
12581f_matchadd(argvars, rettv)
12582 typval_T *argvars;
12583 typval_T *rettv;
12584{
12585#ifdef FEAT_SEARCH_EXTRA
12586 char_u buf[NUMBUFLEN];
12587 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12588 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12589 int prio = 10; /* default priority */
12590 int id = -1;
12591 int error = FALSE;
12592
12593 rettv->vval.v_number = -1;
12594
12595 if (grp == NULL || pat == NULL)
12596 return;
12597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012598 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012599 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012600 if (argvars[3].v_type != VAR_UNKNOWN)
12601 id = get_tv_number_chk(&argvars[3], &error);
12602 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012603 if (error == TRUE)
12604 return;
12605 if (id >= 1 && id <= 3)
12606 {
12607 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12608 return;
12609 }
12610
12611 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12612#endif
12613}
12614
12615/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012616 * "matcharg()" function
12617 */
12618 static void
12619f_matcharg(argvars, rettv)
12620 typval_T *argvars;
12621 typval_T *rettv;
12622{
12623 if (rettv_list_alloc(rettv) == OK)
12624 {
12625#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012626 int id = get_tv_number(&argvars[0]);
12627 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012628
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012629 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012630 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012631 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12632 {
12633 list_append_string(rettv->vval.v_list,
12634 syn_id2name(m->hlg_id), -1);
12635 list_append_string(rettv->vval.v_list, m->pattern, -1);
12636 }
12637 else
12638 {
12639 list_append_string(rettv->vval.v_list, NUL, -1);
12640 list_append_string(rettv->vval.v_list, NUL, -1);
12641 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012642 }
12643#endif
12644 }
12645}
12646
12647/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012648 * "matchdelete()" function
12649 */
12650 static void
12651f_matchdelete(argvars, rettv)
12652 typval_T *argvars;
12653 typval_T *rettv;
12654{
12655#ifdef FEAT_SEARCH_EXTRA
12656 rettv->vval.v_number = match_delete(curwin,
12657 (int)get_tv_number(&argvars[0]), TRUE);
12658#endif
12659}
12660
12661/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012662 * "matchend()" function
12663 */
12664 static void
12665f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012666 typval_T *argvars;
12667 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012668{
12669 find_some_match(argvars, rettv, 0);
12670}
12671
12672/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012673 * "matchlist()" function
12674 */
12675 static void
12676f_matchlist(argvars, rettv)
12677 typval_T *argvars;
12678 typval_T *rettv;
12679{
12680 find_some_match(argvars, rettv, 3);
12681}
12682
12683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012684 * "matchstr()" function
12685 */
12686 static void
12687f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 typval_T *argvars;
12689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012690{
12691 find_some_match(argvars, rettv, 2);
12692}
12693
Bram Moolenaar33570922005-01-25 22:26:29 +000012694static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012695
12696 static void
12697max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012698 typval_T *argvars;
12699 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012700 int domax;
12701{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012702 long n = 0;
12703 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012704 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012705
12706 if (argvars[0].v_type == VAR_LIST)
12707 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012708 list_T *l;
12709 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012710
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012711 l = argvars[0].vval.v_list;
12712 if (l != NULL)
12713 {
12714 li = l->lv_first;
12715 if (li != NULL)
12716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012718 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012719 {
12720 li = li->li_next;
12721 if (li == NULL)
12722 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012724 if (domax ? i > n : i < n)
12725 n = i;
12726 }
12727 }
12728 }
12729 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012730 else if (argvars[0].v_type == VAR_DICT)
12731 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012732 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012733 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012734 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012735 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012736
12737 d = argvars[0].vval.v_dict;
12738 if (d != NULL)
12739 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012740 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012741 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012743 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012745 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012747 if (first)
12748 {
12749 n = i;
12750 first = FALSE;
12751 }
12752 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012753 n = i;
12754 }
12755 }
12756 }
12757 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012758 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012759 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012761}
12762
12763/*
12764 * "max()" function
12765 */
12766 static void
12767f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012768 typval_T *argvars;
12769 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012770{
12771 max_min(argvars, rettv, TRUE);
12772}
12773
12774/*
12775 * "min()" function
12776 */
12777 static void
12778f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012779 typval_T *argvars;
12780 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012781{
12782 max_min(argvars, rettv, FALSE);
12783}
12784
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012785static int mkdir_recurse __ARGS((char_u *dir, int prot));
12786
12787/*
12788 * Create the directory in which "dir" is located, and higher levels when
12789 * needed.
12790 */
12791 static int
12792mkdir_recurse(dir, prot)
12793 char_u *dir;
12794 int prot;
12795{
12796 char_u *p;
12797 char_u *updir;
12798 int r = FAIL;
12799
12800 /* Get end of directory name in "dir".
12801 * We're done when it's "/" or "c:/". */
12802 p = gettail_sep(dir);
12803 if (p <= get_past_head(dir))
12804 return OK;
12805
12806 /* If the directory exists we're done. Otherwise: create it.*/
12807 updir = vim_strnsave(dir, (int)(p - dir));
12808 if (updir == NULL)
12809 return FAIL;
12810 if (mch_isdir(updir))
12811 r = OK;
12812 else if (mkdir_recurse(updir, prot) == OK)
12813 r = vim_mkdir_emsg(updir, prot);
12814 vim_free(updir);
12815 return r;
12816}
12817
12818#ifdef vim_mkdir
12819/*
12820 * "mkdir()" function
12821 */
12822 static void
12823f_mkdir(argvars, rettv)
12824 typval_T *argvars;
12825 typval_T *rettv;
12826{
12827 char_u *dir;
12828 char_u buf[NUMBUFLEN];
12829 int prot = 0755;
12830
12831 rettv->vval.v_number = FAIL;
12832 if (check_restricted() || check_secure())
12833 return;
12834
12835 dir = get_tv_string_buf(&argvars[0], buf);
12836 if (argvars[1].v_type != VAR_UNKNOWN)
12837 {
12838 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012839 prot = get_tv_number_chk(&argvars[2], NULL);
12840 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012841 mkdir_recurse(dir, prot);
12842 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012843 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012844}
12845#endif
12846
Bram Moolenaar0d660222005-01-07 21:51:51 +000012847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 * "mode()" function
12849 */
12850/*ARGSUSED*/
12851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012853 typval_T *argvars;
12854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855{
12856 char_u buf[2];
12857
12858#ifdef FEAT_VISUAL
12859 if (VIsual_active)
12860 {
12861 if (VIsual_select)
12862 buf[0] = VIsual_mode + 's' - 'v';
12863 else
12864 buf[0] = VIsual_mode;
12865 }
12866 else
12867#endif
12868 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12869 buf[0] = 'r';
12870 else if (State & INSERT)
12871 {
12872 if (State & REPLACE_FLAG)
12873 buf[0] = 'R';
12874 else
12875 buf[0] = 'i';
12876 }
12877 else if (State & CMDLINE)
12878 buf[0] = 'c';
12879 else
12880 buf[0] = 'n';
12881
12882 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_string = vim_strsave(buf);
12884 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885}
12886
12887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012888 * "nextnonblank()" function
12889 */
12890 static void
12891f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012892 typval_T *argvars;
12893 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012894{
12895 linenr_T lnum;
12896
12897 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012899 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012900 {
12901 lnum = 0;
12902 break;
12903 }
12904 if (*skipwhite(ml_get(lnum)) != NUL)
12905 break;
12906 }
12907 rettv->vval.v_number = lnum;
12908}
12909
12910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 * "nr2char()" function
12912 */
12913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012915 typval_T *argvars;
12916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917{
12918 char_u buf[NUMBUFLEN];
12919
12920#ifdef FEAT_MBYTE
12921 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 else
12924#endif
12925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012926 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 buf[1] = NUL;
12928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->v_type = VAR_STRING;
12930 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012931}
12932
12933/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012934 * "pathshorten()" function
12935 */
12936 static void
12937f_pathshorten(argvars, rettv)
12938 typval_T *argvars;
12939 typval_T *rettv;
12940{
12941 char_u *p;
12942
12943 rettv->v_type = VAR_STRING;
12944 p = get_tv_string_chk(&argvars[0]);
12945 if (p == NULL)
12946 rettv->vval.v_string = NULL;
12947 else
12948 {
12949 p = vim_strsave(p);
12950 rettv->vval.v_string = p;
12951 if (p != NULL)
12952 shorten_dir(p);
12953 }
12954}
12955
12956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012957 * "prevnonblank()" function
12958 */
12959 static void
12960f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012961 typval_T *argvars;
12962 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012963{
12964 linenr_T lnum;
12965
12966 lnum = get_tv_lnum(argvars);
12967 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12968 lnum = 0;
12969 else
12970 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12971 --lnum;
12972 rettv->vval.v_number = lnum;
12973}
12974
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012975#ifdef HAVE_STDARG_H
12976/* This dummy va_list is here because:
12977 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12978 * - locally in the function results in a "used before set" warning
12979 * - using va_start() to initialize it gives "function with fixed args" error */
12980static va_list ap;
12981#endif
12982
Bram Moolenaar8c711452005-01-14 21:53:12 +000012983/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012984 * "printf()" function
12985 */
12986 static void
12987f_printf(argvars, rettv)
12988 typval_T *argvars;
12989 typval_T *rettv;
12990{
12991 rettv->v_type = VAR_STRING;
12992 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012993#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012994 {
12995 char_u buf[NUMBUFLEN];
12996 int len;
12997 char_u *s;
12998 int saved_did_emsg = did_emsg;
12999 char *fmt;
13000
13001 /* Get the required length, allocate the buffer and do it for real. */
13002 did_emsg = FALSE;
13003 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013004 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013005 if (!did_emsg)
13006 {
13007 s = alloc(len + 1);
13008 if (s != NULL)
13009 {
13010 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013011 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013012 }
13013 }
13014 did_emsg |= saved_did_emsg;
13015 }
13016#endif
13017}
13018
13019/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013020 * "pumvisible()" function
13021 */
13022/*ARGSUSED*/
13023 static void
13024f_pumvisible(argvars, rettv)
13025 typval_T *argvars;
13026 typval_T *rettv;
13027{
13028 rettv->vval.v_number = 0;
13029#ifdef FEAT_INS_EXPAND
13030 if (pum_visible())
13031 rettv->vval.v_number = 1;
13032#endif
13033}
13034
13035/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013036 * "range()" function
13037 */
13038 static void
13039f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013040 typval_T *argvars;
13041 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013042{
13043 long start;
13044 long end;
13045 long stride = 1;
13046 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013047 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013049 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013050 if (argvars[1].v_type == VAR_UNKNOWN)
13051 {
13052 end = start - 1;
13053 start = 0;
13054 }
13055 else
13056 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013057 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013058 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013059 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013060 }
13061
13062 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013063 if (error)
13064 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013065 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013066 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013067 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013068 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013069 else
13070 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013071 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013072 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013073 if (list_append_number(rettv->vval.v_list,
13074 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013075 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013076 }
13077}
13078
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013079/*
13080 * "readfile()" function
13081 */
13082 static void
13083f_readfile(argvars, rettv)
13084 typval_T *argvars;
13085 typval_T *rettv;
13086{
13087 int binary = FALSE;
13088 char_u *fname;
13089 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013090 listitem_T *li;
13091#define FREAD_SIZE 200 /* optimized for text lines */
13092 char_u buf[FREAD_SIZE];
13093 int readlen; /* size of last fread() */
13094 int buflen; /* nr of valid chars in buf[] */
13095 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13096 int tolist; /* first byte in buf[] still to be put in list */
13097 int chop; /* how many CR to chop off */
13098 char_u *prev = NULL; /* previously read bytes, if any */
13099 int prevlen = 0; /* length of "prev" if not NULL */
13100 char_u *s;
13101 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013102 long maxline = MAXLNUM;
13103 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013104
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013105 if (argvars[1].v_type != VAR_UNKNOWN)
13106 {
13107 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13108 binary = TRUE;
13109 if (argvars[2].v_type != VAR_UNKNOWN)
13110 maxline = get_tv_number(&argvars[2]);
13111 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013112
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013113 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013114 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013115
13116 /* Always open the file in binary mode, library functions have a mind of
13117 * their own about CR-LF conversion. */
13118 fname = get_tv_string(&argvars[0]);
13119 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13120 {
13121 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13122 return;
13123 }
13124
13125 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013126 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013127 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013128 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013129 buflen = filtd + readlen;
13130 tolist = 0;
13131 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13132 {
13133 if (buf[filtd] == '\n' || readlen <= 0)
13134 {
13135 /* Only when in binary mode add an empty list item when the
13136 * last line ends in a '\n'. */
13137 if (!binary && readlen == 0 && filtd == 0)
13138 break;
13139
13140 /* Found end-of-line or end-of-file: add a text line to the
13141 * list. */
13142 chop = 0;
13143 if (!binary)
13144 while (filtd - chop - 1 >= tolist
13145 && buf[filtd - chop - 1] == '\r')
13146 ++chop;
13147 len = filtd - tolist - chop;
13148 if (prev == NULL)
13149 s = vim_strnsave(buf + tolist, len);
13150 else
13151 {
13152 s = alloc((unsigned)(prevlen + len + 1));
13153 if (s != NULL)
13154 {
13155 mch_memmove(s, prev, prevlen);
13156 vim_free(prev);
13157 prev = NULL;
13158 mch_memmove(s + prevlen, buf + tolist, len);
13159 s[prevlen + len] = NUL;
13160 }
13161 }
13162 tolist = filtd + 1;
13163
13164 li = listitem_alloc();
13165 if (li == NULL)
13166 {
13167 vim_free(s);
13168 break;
13169 }
13170 li->li_tv.v_type = VAR_STRING;
13171 li->li_tv.v_lock = 0;
13172 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013173 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013174
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013175 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013176 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013177 if (readlen <= 0)
13178 break;
13179 }
13180 else if (buf[filtd] == NUL)
13181 buf[filtd] = '\n';
13182 }
13183 if (readlen <= 0)
13184 break;
13185
13186 if (tolist == 0)
13187 {
13188 /* "buf" is full, need to move text to an allocated buffer */
13189 if (prev == NULL)
13190 {
13191 prev = vim_strnsave(buf, buflen);
13192 prevlen = buflen;
13193 }
13194 else
13195 {
13196 s = alloc((unsigned)(prevlen + buflen));
13197 if (s != NULL)
13198 {
13199 mch_memmove(s, prev, prevlen);
13200 mch_memmove(s + prevlen, buf, buflen);
13201 vim_free(prev);
13202 prev = s;
13203 prevlen += buflen;
13204 }
13205 }
13206 filtd = 0;
13207 }
13208 else
13209 {
13210 mch_memmove(buf, buf + tolist, buflen - tolist);
13211 filtd -= tolist;
13212 }
13213 }
13214
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013215 /*
13216 * For a negative line count use only the lines at the end of the file,
13217 * free the rest.
13218 */
13219 if (maxline < 0)
13220 while (cnt > -maxline)
13221 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013222 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013223 --cnt;
13224 }
13225
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013226 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013227 fclose(fd);
13228}
13229
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013230#if defined(FEAT_RELTIME)
13231static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13232
13233/*
13234 * Convert a List to proftime_T.
13235 * Return FAIL when there is something wrong.
13236 */
13237 static int
13238list2proftime(arg, tm)
13239 typval_T *arg;
13240 proftime_T *tm;
13241{
13242 long n1, n2;
13243 int error = FALSE;
13244
13245 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13246 || arg->vval.v_list->lv_len != 2)
13247 return FAIL;
13248 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13249 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13250# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013251 tm->HighPart = n1;
13252 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013253# else
13254 tm->tv_sec = n1;
13255 tm->tv_usec = n2;
13256# endif
13257 return error ? FAIL : OK;
13258}
13259#endif /* FEAT_RELTIME */
13260
13261/*
13262 * "reltime()" function
13263 */
13264 static void
13265f_reltime(argvars, rettv)
13266 typval_T *argvars;
13267 typval_T *rettv;
13268{
13269#ifdef FEAT_RELTIME
13270 proftime_T res;
13271 proftime_T start;
13272
13273 if (argvars[0].v_type == VAR_UNKNOWN)
13274 {
13275 /* No arguments: get current time. */
13276 profile_start(&res);
13277 }
13278 else if (argvars[1].v_type == VAR_UNKNOWN)
13279 {
13280 if (list2proftime(&argvars[0], &res) == FAIL)
13281 return;
13282 profile_end(&res);
13283 }
13284 else
13285 {
13286 /* Two arguments: compute the difference. */
13287 if (list2proftime(&argvars[0], &start) == FAIL
13288 || list2proftime(&argvars[1], &res) == FAIL)
13289 return;
13290 profile_sub(&res, &start);
13291 }
13292
13293 if (rettv_list_alloc(rettv) == OK)
13294 {
13295 long n1, n2;
13296
13297# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013298 n1 = res.HighPart;
13299 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013300# else
13301 n1 = res.tv_sec;
13302 n2 = res.tv_usec;
13303# endif
13304 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13305 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13306 }
13307#endif
13308}
13309
13310/*
13311 * "reltimestr()" function
13312 */
13313 static void
13314f_reltimestr(argvars, rettv)
13315 typval_T *argvars;
13316 typval_T *rettv;
13317{
13318#ifdef FEAT_RELTIME
13319 proftime_T tm;
13320#endif
13321
13322 rettv->v_type = VAR_STRING;
13323 rettv->vval.v_string = NULL;
13324#ifdef FEAT_RELTIME
13325 if (list2proftime(&argvars[0], &tm) == OK)
13326 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13327#endif
13328}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013329
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13331static void make_connection __ARGS((void));
13332static int check_connection __ARGS((void));
13333
13334 static void
13335make_connection()
13336{
13337 if (X_DISPLAY == NULL
13338# ifdef FEAT_GUI
13339 && !gui.in_use
13340# endif
13341 )
13342 {
13343 x_force_connect = TRUE;
13344 setup_term_clip();
13345 x_force_connect = FALSE;
13346 }
13347}
13348
13349 static int
13350check_connection()
13351{
13352 make_connection();
13353 if (X_DISPLAY == NULL)
13354 {
13355 EMSG(_("E240: No connection to Vim server"));
13356 return FAIL;
13357 }
13358 return OK;
13359}
13360#endif
13361
13362#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013363static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013364
13365 static void
13366remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 typval_T *argvars;
13368 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013369 int expr;
13370{
13371 char_u *server_name;
13372 char_u *keys;
13373 char_u *r = NULL;
13374 char_u buf[NUMBUFLEN];
13375# ifdef WIN32
13376 HWND w;
13377# else
13378 Window w;
13379# endif
13380
13381 if (check_restricted() || check_secure())
13382 return;
13383
13384# ifdef FEAT_X11
13385 if (check_connection() == FAIL)
13386 return;
13387# endif
13388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 server_name = get_tv_string_chk(&argvars[0]);
13390 if (server_name == NULL)
13391 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013392 keys = get_tv_string_buf(&argvars[1], buf);
13393# ifdef WIN32
13394 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13395# else
13396 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13397 < 0)
13398# endif
13399 {
13400 if (r != NULL)
13401 EMSG(r); /* sending worked but evaluation failed */
13402 else
13403 EMSG2(_("E241: Unable to send to %s"), server_name);
13404 return;
13405 }
13406
13407 rettv->vval.v_string = r;
13408
13409 if (argvars[2].v_type != VAR_UNKNOWN)
13410 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013411 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013412 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013413 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013414
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013415 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013416 v.di_tv.v_type = VAR_STRING;
13417 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013418 idvar = get_tv_string_chk(&argvars[2]);
13419 if (idvar != NULL)
13420 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013421 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013422 }
13423}
13424#endif
13425
13426/*
13427 * "remote_expr()" function
13428 */
13429/*ARGSUSED*/
13430 static void
13431f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013432 typval_T *argvars;
13433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013434{
13435 rettv->v_type = VAR_STRING;
13436 rettv->vval.v_string = NULL;
13437#ifdef FEAT_CLIENTSERVER
13438 remote_common(argvars, rettv, TRUE);
13439#endif
13440}
13441
13442/*
13443 * "remote_foreground()" function
13444 */
13445/*ARGSUSED*/
13446 static void
13447f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013448 typval_T *argvars;
13449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013450{
13451 rettv->vval.v_number = 0;
13452#ifdef FEAT_CLIENTSERVER
13453# ifdef WIN32
13454 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 {
13456 char_u *server_name = get_tv_string_chk(&argvars[0]);
13457
13458 if (server_name != NULL)
13459 serverForeground(server_name);
13460 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013461# else
13462 /* Send a foreground() expression to the server. */
13463 argvars[1].v_type = VAR_STRING;
13464 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13465 argvars[2].v_type = VAR_UNKNOWN;
13466 remote_common(argvars, rettv, TRUE);
13467 vim_free(argvars[1].vval.v_string);
13468# endif
13469#endif
13470}
13471
13472/*ARGSUSED*/
13473 static void
13474f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 typval_T *argvars;
13476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013477{
13478#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013479 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013480 char_u *s = NULL;
13481# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013482 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013483# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013484 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013485
13486 if (check_restricted() || check_secure())
13487 {
13488 rettv->vval.v_number = -1;
13489 return;
13490 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 serverid = get_tv_string_chk(&argvars[0]);
13492 if (serverid == NULL)
13493 {
13494 rettv->vval.v_number = -1;
13495 return; /* type error; errmsg already given */
13496 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013497# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013498 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013499 if (n == 0)
13500 rettv->vval.v_number = -1;
13501 else
13502 {
13503 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13504 rettv->vval.v_number = (s != NULL);
13505 }
13506# else
13507 rettv->vval.v_number = 0;
13508 if (check_connection() == FAIL)
13509 return;
13510
13511 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013512 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013513# endif
13514
13515 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013517 char_u *retvar;
13518
Bram Moolenaar33570922005-01-25 22:26:29 +000013519 v.di_tv.v_type = VAR_STRING;
13520 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521 retvar = get_tv_string_chk(&argvars[1]);
13522 if (retvar != NULL)
13523 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013524 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013525 }
13526#else
13527 rettv->vval.v_number = -1;
13528#endif
13529}
13530
13531/*ARGSUSED*/
13532 static void
13533f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 typval_T *argvars;
13535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536{
13537 char_u *r = NULL;
13538
13539#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540 char_u *serverid = get_tv_string_chk(&argvars[0]);
13541
13542 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013543 {
13544# ifdef WIN32
13545 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013546 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013547
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013548 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549 if (n != 0)
13550 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13551 if (r == NULL)
13552# else
13553 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013554 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013555# endif
13556 EMSG(_("E277: Unable to read a server reply"));
13557 }
13558#endif
13559 rettv->v_type = VAR_STRING;
13560 rettv->vval.v_string = r;
13561}
13562
13563/*
13564 * "remote_send()" function
13565 */
13566/*ARGSUSED*/
13567 static void
13568f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013571{
13572 rettv->v_type = VAR_STRING;
13573 rettv->vval.v_string = NULL;
13574#ifdef FEAT_CLIENTSERVER
13575 remote_common(argvars, rettv, FALSE);
13576#endif
13577}
13578
13579/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013580 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013581 */
13582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013584 typval_T *argvars;
13585 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013586{
Bram Moolenaar33570922005-01-25 22:26:29 +000013587 list_T *l;
13588 listitem_T *item, *item2;
13589 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013590 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013591 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013592 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 dict_T *d;
13594 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013595
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013596 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013597 if (argvars[0].v_type == VAR_DICT)
13598 {
13599 if (argvars[2].v_type != VAR_UNKNOWN)
13600 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013601 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013602 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013604 key = get_tv_string_chk(&argvars[1]);
13605 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013607 di = dict_find(d, key, -1);
13608 if (di == NULL)
13609 EMSG2(_(e_dictkey), key);
13610 else
13611 {
13612 *rettv = di->di_tv;
13613 init_tv(&di->di_tv);
13614 dictitem_remove(d, di);
13615 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013616 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013617 }
13618 }
13619 else if (argvars[0].v_type != VAR_LIST)
13620 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013621 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013622 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 int error = FALSE;
13625
13626 idx = get_tv_number_chk(&argvars[1], &error);
13627 if (error)
13628 ; /* type error: do nothing, errmsg already given */
13629 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013630 EMSGN(_(e_listidx), idx);
13631 else
13632 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013633 if (argvars[2].v_type == VAR_UNKNOWN)
13634 {
13635 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013636 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013637 *rettv = item->li_tv;
13638 vim_free(item);
13639 }
13640 else
13641 {
13642 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013643 end = get_tv_number_chk(&argvars[2], &error);
13644 if (error)
13645 ; /* type error: do nothing */
13646 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013647 EMSGN(_(e_listidx), end);
13648 else
13649 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013650 int cnt = 0;
13651
13652 for (li = item; li != NULL; li = li->li_next)
13653 {
13654 ++cnt;
13655 if (li == item2)
13656 break;
13657 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013658 if (li == NULL) /* didn't find "item2" after "item" */
13659 EMSG(_(e_invrange));
13660 else
13661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013662 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013663 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013664 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013665 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013666 l->lv_first = item;
13667 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013668 item->li_prev = NULL;
13669 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013670 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013671 }
13672 }
13673 }
13674 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013675 }
13676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677}
13678
13679/*
13680 * "rename({from}, {to})" function
13681 */
13682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013683f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013684 typval_T *argvars;
13685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686{
13687 char_u buf[NUMBUFLEN];
13688
13689 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13693 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694}
13695
13696/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013697 * "repeat()" function
13698 */
13699/*ARGSUSED*/
13700 static void
13701f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013702 typval_T *argvars;
13703 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013704{
13705 char_u *p;
13706 int n;
13707 int slen;
13708 int len;
13709 char_u *r;
13710 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013711
13712 n = get_tv_number(&argvars[1]);
13713 if (argvars[0].v_type == VAR_LIST)
13714 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013715 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013716 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013717 if (list_extend(rettv->vval.v_list,
13718 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013719 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013720 }
13721 else
13722 {
13723 p = get_tv_string(&argvars[0]);
13724 rettv->v_type = VAR_STRING;
13725 rettv->vval.v_string = NULL;
13726
13727 slen = (int)STRLEN(p);
13728 len = slen * n;
13729 if (len <= 0)
13730 return;
13731
13732 r = alloc(len + 1);
13733 if (r != NULL)
13734 {
13735 for (i = 0; i < n; i++)
13736 mch_memmove(r + i * slen, p, (size_t)slen);
13737 r[len] = NUL;
13738 }
13739
13740 rettv->vval.v_string = r;
13741 }
13742}
13743
13744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 * "resolve()" function
13746 */
13747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013749 typval_T *argvars;
13750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751{
13752 char_u *p;
13753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013754 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#ifdef FEAT_SHORTCUT
13756 {
13757 char_u *v = NULL;
13758
13759 v = mch_resolve_shortcut(p);
13760 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013763 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 }
13765#else
13766# ifdef HAVE_READLINK
13767 {
13768 char_u buf[MAXPATHL + 1];
13769 char_u *cpy;
13770 int len;
13771 char_u *remain = NULL;
13772 char_u *q;
13773 int is_relative_to_current = FALSE;
13774 int has_trailing_pathsep = FALSE;
13775 int limit = 100;
13776
13777 p = vim_strsave(p);
13778
13779 if (p[0] == '.' && (vim_ispathsep(p[1])
13780 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13781 is_relative_to_current = TRUE;
13782
13783 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013784 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 has_trailing_pathsep = TRUE;
13786
13787 q = getnextcomp(p);
13788 if (*q != NUL)
13789 {
13790 /* Separate the first path component in "p", and keep the
13791 * remainder (beginning with the path separator). */
13792 remain = vim_strsave(q - 1);
13793 q[-1] = NUL;
13794 }
13795
13796 for (;;)
13797 {
13798 for (;;)
13799 {
13800 len = readlink((char *)p, (char *)buf, MAXPATHL);
13801 if (len <= 0)
13802 break;
13803 buf[len] = NUL;
13804
13805 if (limit-- == 0)
13806 {
13807 vim_free(p);
13808 vim_free(remain);
13809 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013810 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 goto fail;
13812 }
13813
13814 /* Ensure that the result will have a trailing path separator
13815 * if the argument has one. */
13816 if (remain == NULL && has_trailing_pathsep)
13817 add_pathsep(buf);
13818
13819 /* Separate the first path component in the link value and
13820 * concatenate the remainders. */
13821 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13822 if (*q != NUL)
13823 {
13824 if (remain == NULL)
13825 remain = vim_strsave(q - 1);
13826 else
13827 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013828 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 if (cpy != NULL)
13830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 vim_free(remain);
13832 remain = cpy;
13833 }
13834 }
13835 q[-1] = NUL;
13836 }
13837
13838 q = gettail(p);
13839 if (q > p && *q == NUL)
13840 {
13841 /* Ignore trailing path separator. */
13842 q[-1] = NUL;
13843 q = gettail(p);
13844 }
13845 if (q > p && !mch_isFullName(buf))
13846 {
13847 /* symlink is relative to directory of argument */
13848 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13849 if (cpy != NULL)
13850 {
13851 STRCPY(cpy, p);
13852 STRCPY(gettail(cpy), buf);
13853 vim_free(p);
13854 p = cpy;
13855 }
13856 }
13857 else
13858 {
13859 vim_free(p);
13860 p = vim_strsave(buf);
13861 }
13862 }
13863
13864 if (remain == NULL)
13865 break;
13866
13867 /* Append the first path component of "remain" to "p". */
13868 q = getnextcomp(remain + 1);
13869 len = q - remain - (*q != NUL);
13870 cpy = vim_strnsave(p, STRLEN(p) + len);
13871 if (cpy != NULL)
13872 {
13873 STRNCAT(cpy, remain, len);
13874 vim_free(p);
13875 p = cpy;
13876 }
13877 /* Shorten "remain". */
13878 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013879 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 else
13881 {
13882 vim_free(remain);
13883 remain = NULL;
13884 }
13885 }
13886
13887 /* If the result is a relative path name, make it explicitly relative to
13888 * the current directory if and only if the argument had this form. */
13889 if (!vim_ispathsep(*p))
13890 {
13891 if (is_relative_to_current
13892 && *p != NUL
13893 && !(p[0] == '.'
13894 && (p[1] == NUL
13895 || vim_ispathsep(p[1])
13896 || (p[1] == '.'
13897 && (p[2] == NUL
13898 || vim_ispathsep(p[2]))))))
13899 {
13900 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013901 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 if (cpy != NULL)
13903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 vim_free(p);
13905 p = cpy;
13906 }
13907 }
13908 else if (!is_relative_to_current)
13909 {
13910 /* Strip leading "./". */
13911 q = p;
13912 while (q[0] == '.' && vim_ispathsep(q[1]))
13913 q += 2;
13914 if (q > p)
13915 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13916 }
13917 }
13918
13919 /* Ensure that the result will have no trailing path separator
13920 * if the argument had none. But keep "/" or "//". */
13921 if (!has_trailing_pathsep)
13922 {
13923 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013924 if (after_pathsep(p, q))
13925 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926 }
13927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013928 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 }
13930# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013931 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932# endif
13933#endif
13934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013935 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936
13937#ifdef HAVE_READLINK
13938fail:
13939#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941}
13942
13943/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013944 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945 */
13946 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013948 typval_T *argvars;
13949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950{
Bram Moolenaar33570922005-01-25 22:26:29 +000013951 list_T *l;
13952 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953
Bram Moolenaar0d660222005-01-07 21:51:51 +000013954 rettv->vval.v_number = 0;
13955 if (argvars[0].v_type != VAR_LIST)
13956 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013957 else if ((l = argvars[0].vval.v_list) != NULL
13958 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013959 {
13960 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013961 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013962 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013963 while (li != NULL)
13964 {
13965 ni = li->li_prev;
13966 list_append(l, li);
13967 li = ni;
13968 }
13969 rettv->vval.v_list = l;
13970 rettv->v_type = VAR_LIST;
13971 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000013972 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974}
13975
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013976#define SP_NOMOVE 0x01 /* don't move cursor */
13977#define SP_REPEAT 0x02 /* repeat to find outer pair */
13978#define SP_RETCOUNT 0x04 /* return matchcount */
13979#define SP_SETPCMARK 0x08 /* set previous context mark */
13980#define SP_START 0x10 /* accept match at start position */
13981#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13982#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013983
Bram Moolenaar33570922005-01-25 22:26:29 +000013984static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013985
13986/*
13987 * Get flags for a search function.
13988 * Possibly sets "p_ws".
13989 * Returns BACKWARD, FORWARD or zero (for an error).
13990 */
13991 static int
13992get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013993 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013994 int *flagsp;
13995{
13996 int dir = FORWARD;
13997 char_u *flags;
13998 char_u nbuf[NUMBUFLEN];
13999 int mask;
14000
14001 if (varp->v_type != VAR_UNKNOWN)
14002 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003 flags = get_tv_string_buf_chk(varp, nbuf);
14004 if (flags == NULL)
14005 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014006 while (*flags != NUL)
14007 {
14008 switch (*flags)
14009 {
14010 case 'b': dir = BACKWARD; break;
14011 case 'w': p_ws = TRUE; break;
14012 case 'W': p_ws = FALSE; break;
14013 default: mask = 0;
14014 if (flagsp != NULL)
14015 switch (*flags)
14016 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014017 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014018 case 'e': mask = SP_END; break;
14019 case 'm': mask = SP_RETCOUNT; break;
14020 case 'n': mask = SP_NOMOVE; break;
14021 case 'p': mask = SP_SUBPAT; break;
14022 case 'r': mask = SP_REPEAT; break;
14023 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014024 }
14025 if (mask == 0)
14026 {
14027 EMSG2(_(e_invarg2), flags);
14028 dir = 0;
14029 }
14030 else
14031 *flagsp |= mask;
14032 }
14033 if (dir == 0)
14034 break;
14035 ++flags;
14036 }
14037 }
14038 return dir;
14039}
14040
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014042 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014044 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014045search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014046 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014047 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014048 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014050 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 char_u *pat;
14052 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014053 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 int save_p_ws = p_ws;
14055 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014056 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014057 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014058 proftime_T tm;
14059#ifdef FEAT_RELTIME
14060 long time_limit = 0;
14061#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014062 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014063 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014066 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014067 if (dir == 0)
14068 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014069 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014070 if (flags & SP_START)
14071 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014072 if (flags & SP_END)
14073 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014074
Bram Moolenaar76929292008-01-06 19:07:36 +000014075 /* Optional arguments: line number to stop searching and timeout. */
14076 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014077 {
14078 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14079 if (lnum_stop < 0)
14080 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014081#ifdef FEAT_RELTIME
14082 if (argvars[3].v_type != VAR_UNKNOWN)
14083 {
14084 time_limit = get_tv_number_chk(&argvars[3], NULL);
14085 if (time_limit < 0)
14086 goto theend;
14087 }
14088#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014089 }
14090
Bram Moolenaar76929292008-01-06 19:07:36 +000014091#ifdef FEAT_RELTIME
14092 /* Set the time limit, if there is one. */
14093 profile_setlimit(time_limit, &tm);
14094#endif
14095
Bram Moolenaar231334e2005-07-25 20:46:57 +000014096 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014097 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014098 * Check to make sure only those flags are set.
14099 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14100 * flags cannot be set. Check for that condition also.
14101 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014102 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014103 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014105 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014106 goto theend;
14107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014109 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014110 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014111 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014112 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014114 if (flags & SP_SUBPAT)
14115 retval = subpatnum;
14116 else
14117 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014118 if (flags & SP_SETPCMARK)
14119 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014121 if (match_pos != NULL)
14122 {
14123 /* Store the match cursor position */
14124 match_pos->lnum = pos.lnum;
14125 match_pos->col = pos.col + 1;
14126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 /* "/$" will put the cursor after the end of the line, may need to
14128 * correct that here */
14129 check_cursor();
14130 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014131
14132 /* If 'n' flag is used: restore cursor position. */
14133 if (flags & SP_NOMOVE)
14134 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014135 else
14136 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014137theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014139
14140 return retval;
14141}
14142
14143/*
14144 * "search()" function
14145 */
14146 static void
14147f_search(argvars, rettv)
14148 typval_T *argvars;
14149 typval_T *rettv;
14150{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014151 int flags = 0;
14152
14153 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154}
14155
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014157 * "searchdecl()" function
14158 */
14159 static void
14160f_searchdecl(argvars, rettv)
14161 typval_T *argvars;
14162 typval_T *rettv;
14163{
14164 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014165 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014166 int error = FALSE;
14167 char_u *name;
14168
14169 rettv->vval.v_number = 1; /* default: FAIL */
14170
14171 name = get_tv_string_chk(&argvars[0]);
14172 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014173 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014174 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014175 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14176 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14177 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014178 if (!error && name != NULL)
14179 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014180 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014181}
14182
14183/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014184 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014186 static int
14187searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014188 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014189 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190{
14191 char_u *spat, *mpat, *epat;
14192 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 int dir;
14195 int flags = 0;
14196 char_u nbuf1[NUMBUFLEN];
14197 char_u nbuf2[NUMBUFLEN];
14198 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014199 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014200 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014201 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 spat = get_tv_string_chk(&argvars[0]);
14205 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14206 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14207 if (spat == NULL || mpat == NULL || epat == NULL)
14208 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210 /* Handle the optional fourth argument: flags */
14211 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014212 if (dir == 0)
14213 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014214
14215 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014216 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14217 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014218 if ((flags & (SP_END | SP_SUBPAT)) != 0
14219 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014220 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014221 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014222 goto theend;
14223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014225 /* Using 'r' implies 'W', otherwise it doesn't work. */
14226 if (flags & SP_REPEAT)
14227 p_ws = FALSE;
14228
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014229 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014230 if (argvars[3].v_type == VAR_UNKNOWN
14231 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 skip = (char_u *)"";
14233 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014236 if (argvars[5].v_type != VAR_UNKNOWN)
14237 {
14238 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14239 if (lnum_stop < 0)
14240 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014241#ifdef FEAT_RELTIME
14242 if (argvars[6].v_type != VAR_UNKNOWN)
14243 {
14244 time_limit = get_tv_number_chk(&argvars[6], NULL);
14245 if (time_limit < 0)
14246 goto theend;
14247 }
14248#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014249 }
14250 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 if (skip == NULL)
14252 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014254 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014255 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014256
14257theend:
14258 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014259
14260 return retval;
14261}
14262
14263/*
14264 * "searchpair()" function
14265 */
14266 static void
14267f_searchpair(argvars, rettv)
14268 typval_T *argvars;
14269 typval_T *rettv;
14270{
14271 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14272}
14273
14274/*
14275 * "searchpairpos()" function
14276 */
14277 static void
14278f_searchpairpos(argvars, rettv)
14279 typval_T *argvars;
14280 typval_T *rettv;
14281{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014282 pos_T match_pos;
14283 int lnum = 0;
14284 int col = 0;
14285
14286 rettv->vval.v_number = 0;
14287
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014288 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014289 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014290
14291 if (searchpair_cmn(argvars, &match_pos) > 0)
14292 {
14293 lnum = match_pos.lnum;
14294 col = match_pos.col;
14295 }
14296
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014297 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14298 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014299}
14300
14301/*
14302 * Search for a start/middle/end thing.
14303 * Used by searchpair(), see its documentation for the details.
14304 * Returns 0 or -1 for no match,
14305 */
14306 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014307do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14308 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014309 char_u *spat; /* start pattern */
14310 char_u *mpat; /* middle pattern */
14311 char_u *epat; /* end pattern */
14312 int dir; /* BACKWARD or FORWARD */
14313 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014314 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014315 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014316 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014317 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014318{
14319 char_u *save_cpo;
14320 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14321 long retval = 0;
14322 pos_T pos;
14323 pos_T firstpos;
14324 pos_T foundpos;
14325 pos_T save_cursor;
14326 pos_T save_pos;
14327 int n;
14328 int r;
14329 int nest = 1;
14330 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014331 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014332 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014333
14334 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14335 save_cpo = p_cpo;
14336 p_cpo = (char_u *)"";
14337
Bram Moolenaar76929292008-01-06 19:07:36 +000014338#ifdef FEAT_RELTIME
14339 /* Set the time limit, if there is one. */
14340 profile_setlimit(time_limit, &tm);
14341#endif
14342
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014343 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14344 * start/middle/end (pat3, for the top pair). */
14345 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14346 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14347 if (pat2 == NULL || pat3 == NULL)
14348 goto theend;
14349 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14350 if (*mpat == NUL)
14351 STRCPY(pat3, pat2);
14352 else
14353 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14354 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014355 if (flags & SP_START)
14356 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014357
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 save_cursor = curwin->w_cursor;
14359 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014360 clearpos(&firstpos);
14361 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 pat = pat3;
14363 for (;;)
14364 {
14365 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014366 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14368 /* didn't find it or found the first match again: FAIL */
14369 break;
14370
14371 if (firstpos.lnum == 0)
14372 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014373 if (equalpos(pos, foundpos))
14374 {
14375 /* Found the same position again. Can happen with a pattern that
14376 * has "\zs" at the end and searching backwards. Advance one
14377 * character and try again. */
14378 if (dir == BACKWARD)
14379 decl(&pos);
14380 else
14381 incl(&pos);
14382 }
14383 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014385 /* clear the start flag to avoid getting stuck here */
14386 options &= ~SEARCH_START;
14387
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 /* If the skip pattern matches, ignore this match. */
14389 if (*skip != NUL)
14390 {
14391 save_pos = curwin->w_cursor;
14392 curwin->w_cursor = pos;
14393 r = eval_to_bool(skip, &err, NULL, FALSE);
14394 curwin->w_cursor = save_pos;
14395 if (err)
14396 {
14397 /* Evaluating {skip} caused an error, break here. */
14398 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014399 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 break;
14401 }
14402 if (r)
14403 continue;
14404 }
14405
14406 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14407 {
14408 /* Found end when searching backwards or start when searching
14409 * forward: nested pair. */
14410 ++nest;
14411 pat = pat2; /* nested, don't search for middle */
14412 }
14413 else
14414 {
14415 /* Found end when searching forward or start when searching
14416 * backward: end of (nested) pair; or found middle in outer pair. */
14417 if (--nest == 1)
14418 pat = pat3; /* outer level, search for middle */
14419 }
14420
14421 if (nest == 0)
14422 {
14423 /* Found the match: return matchcount or line number. */
14424 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014425 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014427 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014428 if (flags & SP_SETPCMARK)
14429 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 curwin->w_cursor = pos;
14431 if (!(flags & SP_REPEAT))
14432 break;
14433 nest = 1; /* search for next unmatched */
14434 }
14435 }
14436
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014437 if (match_pos != NULL)
14438 {
14439 /* Store the match cursor position */
14440 match_pos->lnum = curwin->w_cursor.lnum;
14441 match_pos->col = curwin->w_cursor.col + 1;
14442 }
14443
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014445 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 curwin->w_cursor = save_cursor;
14447
14448theend:
14449 vim_free(pat2);
14450 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014452
14453 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454}
14455
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014456/*
14457 * "searchpos()" function
14458 */
14459 static void
14460f_searchpos(argvars, rettv)
14461 typval_T *argvars;
14462 typval_T *rettv;
14463{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014464 pos_T match_pos;
14465 int lnum = 0;
14466 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014467 int n;
14468 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014469
14470 rettv->vval.v_number = 0;
14471
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014472 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014473 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014474
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014475 n = search_cmn(argvars, &match_pos, &flags);
14476 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014477 {
14478 lnum = match_pos.lnum;
14479 col = match_pos.col;
14480 }
14481
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014482 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14483 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014484 if (flags & SP_SUBPAT)
14485 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014486}
14487
14488
Bram Moolenaar0d660222005-01-07 21:51:51 +000014489/*ARGSUSED*/
14490 static void
14491f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014492 typval_T *argvars;
14493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495#ifdef FEAT_CLIENTSERVER
14496 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 char_u *server = get_tv_string_chk(&argvars[0]);
14498 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499
Bram Moolenaar0d660222005-01-07 21:51:51 +000014500 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014501 if (server == NULL || reply == NULL)
14502 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014503 if (check_restricted() || check_secure())
14504 return;
14505# ifdef FEAT_X11
14506 if (check_connection() == FAIL)
14507 return;
14508# endif
14509
14510 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014512 EMSG(_("E258: Unable to send to client"));
14513 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014515 rettv->vval.v_number = 0;
14516#else
14517 rettv->vval.v_number = -1;
14518#endif
14519}
14520
14521/*ARGSUSED*/
14522 static void
14523f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 typval_T *argvars;
14525 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014526{
14527 char_u *r = NULL;
14528
14529#ifdef FEAT_CLIENTSERVER
14530# ifdef WIN32
14531 r = serverGetVimNames();
14532# else
14533 make_connection();
14534 if (X_DISPLAY != NULL)
14535 r = serverGetVimNames(X_DISPLAY);
14536# endif
14537#endif
14538 rettv->v_type = VAR_STRING;
14539 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540}
14541
14542/*
14543 * "setbufvar()" function
14544 */
14545/*ARGSUSED*/
14546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014548 typval_T *argvars;
14549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550{
14551 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014554 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 char_u nbuf[NUMBUFLEN];
14556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557 rettv->vval.v_number = 0;
14558
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 if (check_restricted() || check_secure())
14560 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014561 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14562 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 varp = &argvars[2];
14565
14566 if (buf != NULL && varname != NULL && varp != NULL)
14567 {
14568 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570
14571 if (*varname == '&')
14572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 long numval;
14574 char_u *strval;
14575 int error = FALSE;
14576
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 numval = get_tv_number_chk(varp, &error);
14579 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 if (!error && strval != NULL)
14581 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 }
14583 else
14584 {
14585 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14586 if (bufvarname != NULL)
14587 {
14588 STRCPY(bufvarname, "b:");
14589 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014590 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 vim_free(bufvarname);
14592 }
14593 }
14594
14595 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598}
14599
14600/*
14601 * "setcmdpos()" function
14602 */
14603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014605 typval_T *argvars;
14606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014608 int pos = (int)get_tv_number(&argvars[0]) - 1;
14609
14610 if (pos >= 0)
14611 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612}
14613
14614/*
14615 * "setline()" function
14616 */
14617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014618f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014619 typval_T *argvars;
14620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621{
14622 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014623 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014624 list_T *l = NULL;
14625 listitem_T *li = NULL;
14626 long added = 0;
14627 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014629 lnum = get_tv_lnum(&argvars[0]);
14630 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014632 l = argvars[1].vval.v_list;
14633 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014635 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014637
14638 rettv->vval.v_number = 0; /* OK */
14639 for (;;)
14640 {
14641 if (l != NULL)
14642 {
14643 /* list argument, get next string */
14644 if (li == NULL)
14645 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014646 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014647 li = li->li_next;
14648 }
14649
14650 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014651 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014652 break;
14653 if (lnum <= curbuf->b_ml.ml_line_count)
14654 {
14655 /* existing line, replace it */
14656 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14657 {
14658 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014659 if (lnum == curwin->w_cursor.lnum)
14660 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014661 rettv->vval.v_number = 0; /* OK */
14662 }
14663 }
14664 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14665 {
14666 /* lnum is one past the last line, append the line */
14667 ++added;
14668 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14669 rettv->vval.v_number = 0; /* OK */
14670 }
14671
14672 if (l == NULL) /* only one string argument */
14673 break;
14674 ++lnum;
14675 }
14676
14677 if (added > 0)
14678 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679}
14680
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000014681static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
14682
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014684 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014685 */
14686/*ARGSUSED*/
14687 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014688set_qf_ll_list(wp, list_arg, action_arg, rettv)
14689 win_T *wp;
14690 typval_T *list_arg;
14691 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014692 typval_T *rettv;
14693{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014694#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014695 char_u *act;
14696 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014697#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014698
Bram Moolenaar2641f772005-03-25 21:58:17 +000014699 rettv->vval.v_number = -1;
14700
14701#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014702 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014703 EMSG(_(e_listreq));
14704 else
14705 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014706 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014707
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014708 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014709 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014710 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014711 if (act == NULL)
14712 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014713 if (*act == 'a' || *act == 'r')
14714 action = *act;
14715 }
14716
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014717 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014718 rettv->vval.v_number = 0;
14719 }
14720#endif
14721}
14722
14723/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014724 * "setloclist()" function
14725 */
14726/*ARGSUSED*/
14727 static void
14728f_setloclist(argvars, rettv)
14729 typval_T *argvars;
14730 typval_T *rettv;
14731{
14732 win_T *win;
14733
14734 rettv->vval.v_number = -1;
14735
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014736 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014737 if (win != NULL)
14738 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14739}
14740
14741/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014742 * "setmatches()" function
14743 */
14744 static void
14745f_setmatches(argvars, rettv)
14746 typval_T *argvars;
14747 typval_T *rettv;
14748{
14749#ifdef FEAT_SEARCH_EXTRA
14750 list_T *l;
14751 listitem_T *li;
14752 dict_T *d;
14753
14754 rettv->vval.v_number = -1;
14755 if (argvars[0].v_type != VAR_LIST)
14756 {
14757 EMSG(_(e_listreq));
14758 return;
14759 }
14760 if ((l = argvars[0].vval.v_list) != NULL)
14761 {
14762
14763 /* To some extent make sure that we are dealing with a list from
14764 * "getmatches()". */
14765 li = l->lv_first;
14766 while (li != NULL)
14767 {
14768 if (li->li_tv.v_type != VAR_DICT
14769 || (d = li->li_tv.vval.v_dict) == NULL)
14770 {
14771 EMSG(_(e_invarg));
14772 return;
14773 }
14774 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14775 && dict_find(d, (char_u *)"pattern", -1) != NULL
14776 && dict_find(d, (char_u *)"priority", -1) != NULL
14777 && dict_find(d, (char_u *)"id", -1) != NULL))
14778 {
14779 EMSG(_(e_invarg));
14780 return;
14781 }
14782 li = li->li_next;
14783 }
14784
14785 clear_matches(curwin);
14786 li = l->lv_first;
14787 while (li != NULL)
14788 {
14789 d = li->li_tv.vval.v_dict;
14790 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14791 get_dict_string(d, (char_u *)"pattern", FALSE),
14792 (int)get_dict_number(d, (char_u *)"priority"),
14793 (int)get_dict_number(d, (char_u *)"id"));
14794 li = li->li_next;
14795 }
14796 rettv->vval.v_number = 0;
14797 }
14798#endif
14799}
14800
14801/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014802 * "setpos()" function
14803 */
14804/*ARGSUSED*/
14805 static void
14806f_setpos(argvars, rettv)
14807 typval_T *argvars;
14808 typval_T *rettv;
14809{
14810 pos_T pos;
14811 int fnum;
14812 char_u *name;
14813
Bram Moolenaar08250432008-02-13 11:42:46 +000014814 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014815 name = get_tv_string_chk(argvars);
14816 if (name != NULL)
14817 {
14818 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14819 {
14820 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000014821 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014822 {
Bram Moolenaar08250432008-02-13 11:42:46 +000014823 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014824 if (fnum == curbuf->b_fnum)
14825 {
14826 curwin->w_cursor = pos;
14827 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000014828 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014829 }
14830 else
14831 EMSG(_(e_invarg));
14832 }
Bram Moolenaar08250432008-02-13 11:42:46 +000014833 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
14834 {
14835 /* set mark */
14836 if (setmark_pos(name[1], &pos, fnum) == OK)
14837 rettv->vval.v_number = 0;
14838 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014839 else
14840 EMSG(_(e_invarg));
14841 }
14842 }
14843}
14844
14845/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014846 * "setqflist()" function
14847 */
14848/*ARGSUSED*/
14849 static void
14850f_setqflist(argvars, rettv)
14851 typval_T *argvars;
14852 typval_T *rettv;
14853{
14854 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14855}
14856
14857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 * "setreg()" function
14859 */
14860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014861f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014862 typval_T *argvars;
14863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864{
14865 int regname;
14866 char_u *strregname;
14867 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014868 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 int append;
14870 char_u yank_type;
14871 long block_len;
14872
14873 block_len = -1;
14874 yank_type = MAUTO;
14875 append = FALSE;
14876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014877 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014878 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014880 if (strregname == NULL)
14881 return; /* type error; errmsg already given */
14882 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 if (regname == 0 || regname == '@')
14884 regname = '"';
14885 else if (regname == '=')
14886 return;
14887
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014888 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014890 stropt = get_tv_string_chk(&argvars[2]);
14891 if (stropt == NULL)
14892 return; /* type error */
14893 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894 switch (*stropt)
14895 {
14896 case 'a': case 'A': /* append */
14897 append = TRUE;
14898 break;
14899 case 'v': case 'c': /* character-wise selection */
14900 yank_type = MCHAR;
14901 break;
14902 case 'V': case 'l': /* line-wise selection */
14903 yank_type = MLINE;
14904 break;
14905#ifdef FEAT_VISUAL
14906 case 'b': case Ctrl_V: /* block-wise selection */
14907 yank_type = MBLOCK;
14908 if (VIM_ISDIGIT(stropt[1]))
14909 {
14910 ++stropt;
14911 block_len = getdigits(&stropt) - 1;
14912 --stropt;
14913 }
14914 break;
14915#endif
14916 }
14917 }
14918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014919 strval = get_tv_string_chk(&argvars[1]);
14920 if (strval != NULL)
14921 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924}
14925
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014926/*
14927 * "settabwinvar()" function
14928 */
14929 static void
14930f_settabwinvar(argvars, rettv)
14931 typval_T *argvars;
14932 typval_T *rettv;
14933{
14934 setwinvar(argvars, rettv, 1);
14935}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936
14937/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014938 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014941f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014942 typval_T *argvars;
14943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014945 setwinvar(argvars, rettv, 0);
14946}
14947
14948/*
14949 * "setwinvar()" and "settabwinvar()" functions
14950 */
14951 static void
14952setwinvar(argvars, rettv, off)
14953 typval_T *argvars;
14954 typval_T *rettv;
14955 int off;
14956{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957 win_T *win;
14958#ifdef FEAT_WINDOWS
14959 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014960 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961#endif
14962 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014963 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014965 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014967 rettv->vval.v_number = 0;
14968
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 if (check_restricted() || check_secure())
14970 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014971
14972#ifdef FEAT_WINDOWS
14973 if (off == 1)
14974 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14975 else
14976 tp = curtab;
14977#endif
14978 win = find_win_by_nr(&argvars[off], tp);
14979 varname = get_tv_string_chk(&argvars[off + 1]);
14980 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981
14982 if (win != NULL && varname != NULL && varp != NULL)
14983 {
14984#ifdef FEAT_WINDOWS
14985 /* set curwin to be our win, temporarily */
14986 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014987 save_curtab = curtab;
14988 goto_tabpage_tp(tp);
14989 if (!win_valid(win))
14990 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991 curwin = win;
14992 curbuf = curwin->w_buffer;
14993#endif
14994
14995 if (*varname == '&')
14996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014997 long numval;
14998 char_u *strval;
14999 int error = FALSE;
15000
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015002 numval = get_tv_number_chk(varp, &error);
15003 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015004 if (!error && strval != NULL)
15005 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 }
15007 else
15008 {
15009 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15010 if (winvarname != NULL)
15011 {
15012 STRCPY(winvarname, "w:");
15013 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015014 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 vim_free(winvarname);
15016 }
15017 }
15018
15019#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015020 /* Restore current tabpage and window, if still valid (autocomands can
15021 * make them invalid). */
15022 if (valid_tabpage(save_curtab))
15023 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 if (win_valid(save_curwin))
15025 {
15026 curwin = save_curwin;
15027 curbuf = curwin->w_buffer;
15028 }
15029#endif
15030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031}
15032
15033/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015034 * "shellescape({string})" function
15035 */
15036 static void
15037f_shellescape(argvars, rettv)
15038 typval_T *argvars;
15039 typval_T *rettv;
15040{
15041 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
15042 rettv->v_type = VAR_STRING;
15043}
15044
15045/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 */
15048 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015049f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015050 typval_T *argvars;
15051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015053 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 p = get_tv_string(&argvars[0]);
15056 rettv->vval.v_string = vim_strsave(p);
15057 simplify_filename(rettv->vval.v_string); /* simplify in place */
15058 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059}
15060
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061static int
15062#ifdef __BORLANDC__
15063 _RTLENTRYF
15064#endif
15065 item_compare __ARGS((const void *s1, const void *s2));
15066static int
15067#ifdef __BORLANDC__
15068 _RTLENTRYF
15069#endif
15070 item_compare2 __ARGS((const void *s1, const void *s2));
15071
15072static int item_compare_ic;
15073static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015074static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075#define ITEM_COMPARE_FAIL 999
15076
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080 static int
15081#ifdef __BORLANDC__
15082_RTLENTRYF
15083#endif
15084item_compare(s1, s2)
15085 const void *s1;
15086 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088 char_u *p1, *p2;
15089 char_u *tofree1, *tofree2;
15090 int res;
15091 char_u numbuf1[NUMBUFLEN];
15092 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015094 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15095 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015096 if (p1 == NULL)
15097 p1 = (char_u *)"";
15098 if (p2 == NULL)
15099 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015100 if (item_compare_ic)
15101 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015102 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015103 res = STRCMP(p1, p2);
15104 vim_free(tofree1);
15105 vim_free(tofree2);
15106 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107}
15108
15109 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015110#ifdef __BORLANDC__
15111_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113item_compare2(s1, s2)
15114 const void *s1;
15115 const void *s2;
15116{
15117 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015118 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015119 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015120 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015122 /* shortcut after failure in previous call; compare all items equal */
15123 if (item_compare_func_err)
15124 return 0;
15125
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015126 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15127 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015128 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15129 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015130
15131 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015132 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015133 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134 clear_tv(&argv[0]);
15135 clear_tv(&argv[1]);
15136
15137 if (res == FAIL)
15138 res = ITEM_COMPARE_FAIL;
15139 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015140 /* return value has wrong type */
15141 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15142 if (item_compare_func_err)
15143 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144 clear_tv(&rettv);
15145 return res;
15146}
15147
15148/*
15149 * "sort({list})" function
15150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015152f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015153 typval_T *argvars;
15154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155{
Bram Moolenaar33570922005-01-25 22:26:29 +000015156 list_T *l;
15157 listitem_T *li;
15158 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 long len;
15160 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162 rettv->vval.v_number = 0;
15163 if (argvars[0].v_type != VAR_LIST)
15164 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165 else
15166 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015168 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169 return;
15170 rettv->vval.v_list = l;
15171 rettv->v_type = VAR_LIST;
15172 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174 len = list_len(l);
15175 if (len <= 1)
15176 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178 item_compare_ic = FALSE;
15179 item_compare_func = NULL;
15180 if (argvars[1].v_type != VAR_UNKNOWN)
15181 {
15182 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015183 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015184 else
15185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015186 int error = FALSE;
15187
15188 i = get_tv_number_chk(&argvars[1], &error);
15189 if (error)
15190 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191 if (i == 1)
15192 item_compare_ic = TRUE;
15193 else
15194 item_compare_func = get_tv_string(&argvars[1]);
15195 }
15196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015199 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 if (ptrs == NULL)
15201 return;
15202 i = 0;
15203 for (li = l->lv_first; li != NULL; li = li->li_next)
15204 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015206 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015207 /* test the compare function */
15208 if (item_compare_func != NULL
15209 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15210 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015211 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015213 {
15214 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015215 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216 item_compare_func == NULL ? item_compare : item_compare2);
15217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015218 if (!item_compare_func_err)
15219 {
15220 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015221 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015222 l->lv_len = 0;
15223 for (i = 0; i < len; ++i)
15224 list_append(l, ptrs[i]);
15225 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015226 }
15227
15228 vim_free(ptrs);
15229 }
15230}
15231
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015232/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015233 * "soundfold({word})" function
15234 */
15235 static void
15236f_soundfold(argvars, rettv)
15237 typval_T *argvars;
15238 typval_T *rettv;
15239{
15240 char_u *s;
15241
15242 rettv->v_type = VAR_STRING;
15243 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015244#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015245 rettv->vval.v_string = eval_soundfold(s);
15246#else
15247 rettv->vval.v_string = vim_strsave(s);
15248#endif
15249}
15250
15251/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015252 * "spellbadword()" function
15253 */
15254/* ARGSUSED */
15255 static void
15256f_spellbadword(argvars, rettv)
15257 typval_T *argvars;
15258 typval_T *rettv;
15259{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015260 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015261 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015262 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015263
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015264 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015265 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015266
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015267#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015268 if (argvars[0].v_type == VAR_UNKNOWN)
15269 {
15270 /* Find the start and length of the badly spelled word. */
15271 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15272 if (len != 0)
15273 word = ml_get_cursor();
15274 }
15275 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15276 {
15277 char_u *str = get_tv_string_chk(&argvars[0]);
15278 int capcol = -1;
15279
15280 if (str != NULL)
15281 {
15282 /* Check the argument for spelling. */
15283 while (*str != NUL)
15284 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015285 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015286 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015287 {
15288 word = str;
15289 break;
15290 }
15291 str += len;
15292 }
15293 }
15294 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015295#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015296
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015297 list_append_string(rettv->vval.v_list, word, len);
15298 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015299 attr == HLF_SPB ? "bad" :
15300 attr == HLF_SPR ? "rare" :
15301 attr == HLF_SPL ? "local" :
15302 attr == HLF_SPC ? "caps" :
15303 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015304}
15305
15306/*
15307 * "spellsuggest()" function
15308 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015309/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015310 static void
15311f_spellsuggest(argvars, rettv)
15312 typval_T *argvars;
15313 typval_T *rettv;
15314{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015315#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015316 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015317 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015318 int maxcount;
15319 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015320 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015321 listitem_T *li;
15322 int need_capital = FALSE;
15323#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015324
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015325 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015326 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015327
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015328#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015329 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15330 {
15331 str = get_tv_string(&argvars[0]);
15332 if (argvars[1].v_type != VAR_UNKNOWN)
15333 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015334 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015335 if (maxcount <= 0)
15336 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015337 if (argvars[2].v_type != VAR_UNKNOWN)
15338 {
15339 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15340 if (typeerr)
15341 return;
15342 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015343 }
15344 else
15345 maxcount = 25;
15346
Bram Moolenaar4770d092006-01-12 23:22:24 +000015347 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015348
15349 for (i = 0; i < ga.ga_len; ++i)
15350 {
15351 str = ((char_u **)ga.ga_data)[i];
15352
15353 li = listitem_alloc();
15354 if (li == NULL)
15355 vim_free(str);
15356 else
15357 {
15358 li->li_tv.v_type = VAR_STRING;
15359 li->li_tv.v_lock = 0;
15360 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015361 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015362 }
15363 }
15364 ga_clear(&ga);
15365 }
15366#endif
15367}
15368
Bram Moolenaar0d660222005-01-07 21:51:51 +000015369 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015370f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015371 typval_T *argvars;
15372 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373{
15374 char_u *str;
15375 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015376 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015377 regmatch_T regmatch;
15378 char_u patbuf[NUMBUFLEN];
15379 char_u *save_cpo;
15380 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015381 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015382 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015383 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015384
15385 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15386 save_cpo = p_cpo;
15387 p_cpo = (char_u *)"";
15388
15389 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015390 if (argvars[1].v_type != VAR_UNKNOWN)
15391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015392 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15393 if (pat == NULL)
15394 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015395 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015396 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015397 }
15398 if (pat == NULL || *pat == NUL)
15399 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015400
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015401 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015403 if (typeerr)
15404 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405
Bram Moolenaar0d660222005-01-07 21:51:51 +000015406 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15407 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015409 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015410 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015411 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015412 if (*str == NUL)
15413 match = FALSE; /* empty item at the end */
15414 else
15415 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015416 if (match)
15417 end = regmatch.startp[0];
15418 else
15419 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015420 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15421 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015422 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015423 if (list_append_string(rettv->vval.v_list, str,
15424 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015425 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015426 }
15427 if (!match)
15428 break;
15429 /* Advance to just after the match. */
15430 if (regmatch.endp[0] > str)
15431 col = 0;
15432 else
15433 {
15434 /* Don't get stuck at the same match. */
15435#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015436 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015437#else
15438 col = 1;
15439#endif
15440 }
15441 str = regmatch.endp[0];
15442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443
Bram Moolenaar0d660222005-01-07 21:51:51 +000015444 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446
Bram Moolenaar0d660222005-01-07 21:51:51 +000015447 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448}
15449
Bram Moolenaar2c932302006-03-18 21:42:09 +000015450/*
15451 * "str2nr()" function
15452 */
15453 static void
15454f_str2nr(argvars, rettv)
15455 typval_T *argvars;
15456 typval_T *rettv;
15457{
15458 int base = 10;
15459 char_u *p;
15460 long n;
15461
15462 if (argvars[1].v_type != VAR_UNKNOWN)
15463 {
15464 base = get_tv_number(&argvars[1]);
15465 if (base != 8 && base != 10 && base != 16)
15466 {
15467 EMSG(_(e_invarg));
15468 return;
15469 }
15470 }
15471
15472 p = skipwhite(get_tv_string(&argvars[0]));
15473 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15474 rettv->vval.v_number = n;
15475}
15476
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477#ifdef HAVE_STRFTIME
15478/*
15479 * "strftime({format}[, {time}])" function
15480 */
15481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015482f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015483 typval_T *argvars;
15484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485{
15486 char_u result_buf[256];
15487 struct tm *curtime;
15488 time_t seconds;
15489 char_u *p;
15490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015494 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 seconds = time(NULL);
15496 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015497 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 curtime = localtime(&seconds);
15499 /* MSVC returns NULL for an invalid value of seconds. */
15500 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 else
15503 {
15504# ifdef FEAT_MBYTE
15505 vimconv_T conv;
15506 char_u *enc;
15507
15508 conv.vc_type = CONV_NONE;
15509 enc = enc_locale();
15510 convert_setup(&conv, p_enc, enc);
15511 if (conv.vc_type != CONV_NONE)
15512 p = string_convert(&conv, p, NULL);
15513# endif
15514 if (p != NULL)
15515 (void)strftime((char *)result_buf, sizeof(result_buf),
15516 (char *)p, curtime);
15517 else
15518 result_buf[0] = NUL;
15519
15520# ifdef FEAT_MBYTE
15521 if (conv.vc_type != CONV_NONE)
15522 vim_free(p);
15523 convert_setup(&conv, enc, p_enc);
15524 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015525 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 else
15527# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529
15530# ifdef FEAT_MBYTE
15531 /* Release conversion descriptors */
15532 convert_setup(&conv, NULL, NULL);
15533 vim_free(enc);
15534# endif
15535 }
15536}
15537#endif
15538
15539/*
15540 * "stridx()" function
15541 */
15542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015543f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015544 typval_T *argvars;
15545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546{
15547 char_u buf[NUMBUFLEN];
15548 char_u *needle;
15549 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015550 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015552 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015554 needle = get_tv_string_chk(&argvars[1]);
15555 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015556 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015557 if (needle == NULL || haystack == NULL)
15558 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559
Bram Moolenaar33570922005-01-25 22:26:29 +000015560 if (argvars[2].v_type != VAR_UNKNOWN)
15561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015562 int error = FALSE;
15563
15564 start_idx = get_tv_number_chk(&argvars[2], &error);
15565 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015567 if (start_idx >= 0)
15568 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015569 }
15570
15571 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15572 if (pos != NULL)
15573 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574}
15575
15576/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015577 * "string()" function
15578 */
15579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015580f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015581 typval_T *argvars;
15582 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015583{
15584 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015585 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015587 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015588 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015589 /* Make a copy if we have a value but it's not in allocate memory. */
15590 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592}
15593
15594/*
15595 * "strlen()" function
15596 */
15597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015598f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015599 typval_T *argvars;
15600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015602 rettv->vval.v_number = (varnumber_T)(STRLEN(
15603 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604}
15605
15606/*
15607 * "strpart()" function
15608 */
15609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015610f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015611 typval_T *argvars;
15612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613{
15614 char_u *p;
15615 int n;
15616 int len;
15617 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015618 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015620 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 slen = (int)STRLEN(p);
15622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015623 n = get_tv_number_chk(&argvars[1], &error);
15624 if (error)
15625 len = 0;
15626 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015627 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 else
15629 len = slen - n; /* default len: all bytes that are available. */
15630
15631 /*
15632 * Only return the overlap between the specified part and the actual
15633 * string.
15634 */
15635 if (n < 0)
15636 {
15637 len += n;
15638 n = 0;
15639 }
15640 else if (n > slen)
15641 n = slen;
15642 if (len < 0)
15643 len = 0;
15644 else if (n + len > slen)
15645 len = slen - n;
15646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015647 rettv->v_type = VAR_STRING;
15648 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649}
15650
15651/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015652 * "strridx()" function
15653 */
15654 static void
15655f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015656 typval_T *argvars;
15657 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015658{
15659 char_u buf[NUMBUFLEN];
15660 char_u *needle;
15661 char_u *haystack;
15662 char_u *rest;
15663 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015664 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015666 needle = get_tv_string_chk(&argvars[1]);
15667 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015668
15669 rettv->vval.v_number = -1;
15670 if (needle == NULL || haystack == NULL)
15671 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015672
15673 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015674 if (argvars[2].v_type != VAR_UNKNOWN)
15675 {
15676 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015677 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015678 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015679 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015680 }
15681 else
15682 end_idx = haystack_len;
15683
Bram Moolenaar0d660222005-01-07 21:51:51 +000015684 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015685 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015686 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015687 lastmatch = haystack + end_idx;
15688 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015689 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015690 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015691 for (rest = haystack; *rest != '\0'; ++rest)
15692 {
15693 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015694 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015695 break;
15696 lastmatch = rest;
15697 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015698 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699
15700 if (lastmatch == NULL)
15701 rettv->vval.v_number = -1;
15702 else
15703 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15704}
15705
15706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 * "strtrans()" function
15708 */
15709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015710f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015711 typval_T *argvars;
15712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015714 rettv->v_type = VAR_STRING;
15715 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716}
15717
15718/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015719 * "submatch()" function
15720 */
15721 static void
15722f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 typval_T *argvars;
15724 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725{
15726 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015727 rettv->vval.v_string =
15728 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729}
15730
15731/*
15732 * "substitute()" function
15733 */
15734 static void
15735f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015736 typval_T *argvars;
15737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738{
15739 char_u patbuf[NUMBUFLEN];
15740 char_u subbuf[NUMBUFLEN];
15741 char_u flagsbuf[NUMBUFLEN];
15742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015743 char_u *str = get_tv_string_chk(&argvars[0]);
15744 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15745 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15746 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15747
Bram Moolenaar0d660222005-01-07 21:51:51 +000015748 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015749 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15750 rettv->vval.v_string = NULL;
15751 else
15752 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753}
15754
15755/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015756 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 */
15758/*ARGSUSED*/
15759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015760f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015761 typval_T *argvars;
15762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763{
15764 int id = 0;
15765#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015766 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 long col;
15768 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015769 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015771 lnum = get_tv_lnum(argvars); /* -1 on type error */
15772 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15773 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015775 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015776 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015777 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778#endif
15779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015780 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781}
15782
15783/*
15784 * "synIDattr(id, what [, mode])" function
15785 */
15786/*ARGSUSED*/
15787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015789 typval_T *argvars;
15790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791{
15792 char_u *p = NULL;
15793#ifdef FEAT_SYN_HL
15794 int id;
15795 char_u *what;
15796 char_u *mode;
15797 char_u modebuf[NUMBUFLEN];
15798 int modec;
15799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015800 id = get_tv_number(&argvars[0]);
15801 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015802 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015804 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 modec = TOLOWER_ASC(mode[0]);
15806 if (modec != 't' && modec != 'c'
15807#ifdef FEAT_GUI
15808 && modec != 'g'
15809#endif
15810 )
15811 modec = 0; /* replace invalid with current */
15812 }
15813 else
15814 {
15815#ifdef FEAT_GUI
15816 if (gui.in_use)
15817 modec = 'g';
15818 else
15819#endif
15820 if (t_colors > 1)
15821 modec = 'c';
15822 else
15823 modec = 't';
15824 }
15825
15826
15827 switch (TOLOWER_ASC(what[0]))
15828 {
15829 case 'b':
15830 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15831 p = highlight_color(id, what, modec);
15832 else /* bold */
15833 p = highlight_has_attr(id, HL_BOLD, modec);
15834 break;
15835
15836 case 'f': /* fg[#] */
15837 p = highlight_color(id, what, modec);
15838 break;
15839
15840 case 'i':
15841 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15842 p = highlight_has_attr(id, HL_INVERSE, modec);
15843 else /* italic */
15844 p = highlight_has_attr(id, HL_ITALIC, modec);
15845 break;
15846
15847 case 'n': /* name */
15848 p = get_highlight_name(NULL, id - 1);
15849 break;
15850
15851 case 'r': /* reverse */
15852 p = highlight_has_attr(id, HL_INVERSE, modec);
15853 break;
15854
15855 case 's': /* standout */
15856 p = highlight_has_attr(id, HL_STANDOUT, modec);
15857 break;
15858
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015859 case 'u':
15860 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15861 /* underline */
15862 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15863 else
15864 /* undercurl */
15865 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 break;
15867 }
15868
15869 if (p != NULL)
15870 p = vim_strsave(p);
15871#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015872 rettv->v_type = VAR_STRING;
15873 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874}
15875
15876/*
15877 * "synIDtrans(id)" function
15878 */
15879/*ARGSUSED*/
15880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015881f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015882 typval_T *argvars;
15883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884{
15885 int id;
15886
15887#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015888 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889
15890 if (id > 0)
15891 id = syn_get_final_id(id);
15892 else
15893#endif
15894 id = 0;
15895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897}
15898
15899/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015900 * "synstack(lnum, col)" function
15901 */
15902/*ARGSUSED*/
15903 static void
15904f_synstack(argvars, rettv)
15905 typval_T *argvars;
15906 typval_T *rettv;
15907{
15908#ifdef FEAT_SYN_HL
15909 long lnum;
15910 long col;
15911 int i;
15912 int id;
15913#endif
15914
15915 rettv->v_type = VAR_LIST;
15916 rettv->vval.v_list = NULL;
15917
15918#ifdef FEAT_SYN_HL
15919 lnum = get_tv_lnum(argvars); /* -1 on type error */
15920 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15921
15922 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
15923 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
15924 && rettv_list_alloc(rettv) != FAIL)
15925 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015926 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015927 for (i = 0; ; ++i)
15928 {
15929 id = syn_get_stack_item(i);
15930 if (id < 0)
15931 break;
15932 if (list_append_number(rettv->vval.v_list, id) == FAIL)
15933 break;
15934 }
15935 }
15936#endif
15937}
15938
15939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940 * "system()" function
15941 */
15942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015943f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015944 typval_T *argvars;
15945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015947 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015949 char_u *infile = NULL;
15950 char_u buf[NUMBUFLEN];
15951 int err = FALSE;
15952 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015954 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015955 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015957 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015958 {
15959 /*
15960 * Write the string to a temp file, to be used for input of the shell
15961 * command.
15962 */
15963 if ((infile = vim_tempname('i')) == NULL)
15964 {
15965 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015966 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015967 }
15968
15969 fd = mch_fopen((char *)infile, WRITEBIN);
15970 if (fd == NULL)
15971 {
15972 EMSG2(_(e_notopen), infile);
15973 goto done;
15974 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015975 p = get_tv_string_buf_chk(&argvars[1], buf);
15976 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015977 {
15978 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015979 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015980 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015981 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15982 err = TRUE;
15983 if (fclose(fd) != 0)
15984 err = TRUE;
15985 if (err)
15986 {
15987 EMSG(_("E677: Error writing temp file"));
15988 goto done;
15989 }
15990 }
15991
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015992 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15993 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015994
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995#ifdef USE_CR
15996 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015997 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 {
15999 char_u *s;
16000
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016001 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002 {
16003 if (*s == CAR)
16004 *s = NL;
16005 }
16006 }
16007#else
16008# ifdef USE_CRNL
16009 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016010 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 {
16012 char_u *s, *d;
16013
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016014 d = res;
16015 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016 {
16017 if (s[0] == CAR && s[1] == NL)
16018 ++s;
16019 *d++ = *s;
16020 }
16021 *d = NUL;
16022 }
16023# endif
16024#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016025
16026done:
16027 if (infile != NULL)
16028 {
16029 mch_remove(infile);
16030 vim_free(infile);
16031 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016032 rettv->v_type = VAR_STRING;
16033 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034}
16035
16036/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016037 * "tabpagebuflist()" function
16038 */
16039/* ARGSUSED */
16040 static void
16041f_tabpagebuflist(argvars, rettv)
16042 typval_T *argvars;
16043 typval_T *rettv;
16044{
16045#ifndef FEAT_WINDOWS
16046 rettv->vval.v_number = 0;
16047#else
16048 tabpage_T *tp;
16049 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016050
16051 if (argvars[0].v_type == VAR_UNKNOWN)
16052 wp = firstwin;
16053 else
16054 {
16055 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16056 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016057 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016058 }
16059 if (wp == NULL)
16060 rettv->vval.v_number = 0;
16061 else
16062 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016063 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016064 rettv->vval.v_number = 0;
16065 else
16066 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016067 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016068 if (list_append_number(rettv->vval.v_list,
16069 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016070 break;
16071 }
16072 }
16073#endif
16074}
16075
16076
16077/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016078 * "tabpagenr()" function
16079 */
16080/* ARGSUSED */
16081 static void
16082f_tabpagenr(argvars, rettv)
16083 typval_T *argvars;
16084 typval_T *rettv;
16085{
16086 int nr = 1;
16087#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016088 char_u *arg;
16089
16090 if (argvars[0].v_type != VAR_UNKNOWN)
16091 {
16092 arg = get_tv_string_chk(&argvars[0]);
16093 nr = 0;
16094 if (arg != NULL)
16095 {
16096 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016097 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016098 else
16099 EMSG2(_(e_invexpr2), arg);
16100 }
16101 }
16102 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016103 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016104#endif
16105 rettv->vval.v_number = nr;
16106}
16107
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016108
16109#ifdef FEAT_WINDOWS
16110static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16111
16112/*
16113 * Common code for tabpagewinnr() and winnr().
16114 */
16115 static int
16116get_winnr(tp, argvar)
16117 tabpage_T *tp;
16118 typval_T *argvar;
16119{
16120 win_T *twin;
16121 int nr = 1;
16122 win_T *wp;
16123 char_u *arg;
16124
16125 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16126 if (argvar->v_type != VAR_UNKNOWN)
16127 {
16128 arg = get_tv_string_chk(argvar);
16129 if (arg == NULL)
16130 nr = 0; /* type error; errmsg already given */
16131 else if (STRCMP(arg, "$") == 0)
16132 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16133 else if (STRCMP(arg, "#") == 0)
16134 {
16135 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16136 if (twin == NULL)
16137 nr = 0;
16138 }
16139 else
16140 {
16141 EMSG2(_(e_invexpr2), arg);
16142 nr = 0;
16143 }
16144 }
16145
16146 if (nr > 0)
16147 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16148 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016149 {
16150 if (wp == NULL)
16151 {
16152 /* didn't find it in this tabpage */
16153 nr = 0;
16154 break;
16155 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016156 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016157 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016158 return nr;
16159}
16160#endif
16161
16162/*
16163 * "tabpagewinnr()" function
16164 */
16165/* ARGSUSED */
16166 static void
16167f_tabpagewinnr(argvars, rettv)
16168 typval_T *argvars;
16169 typval_T *rettv;
16170{
16171 int nr = 1;
16172#ifdef FEAT_WINDOWS
16173 tabpage_T *tp;
16174
16175 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16176 if (tp == NULL)
16177 nr = 0;
16178 else
16179 nr = get_winnr(tp, &argvars[1]);
16180#endif
16181 rettv->vval.v_number = nr;
16182}
16183
16184
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016185/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016186 * "tagfiles()" function
16187 */
16188/*ARGSUSED*/
16189 static void
16190f_tagfiles(argvars, rettv)
16191 typval_T *argvars;
16192 typval_T *rettv;
16193{
16194 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016195 tagname_T tn;
16196 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016197
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016198 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016199 {
16200 rettv->vval.v_number = 0;
16201 return;
16202 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016203
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016204 for (first = TRUE; ; first = FALSE)
16205 if (get_tagfname(&tn, first, fname) == FAIL
16206 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016207 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016208 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016209}
16210
16211/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016212 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016213 */
16214 static void
16215f_taglist(argvars, rettv)
16216 typval_T *argvars;
16217 typval_T *rettv;
16218{
16219 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016220
16221 tag_pattern = get_tv_string(&argvars[0]);
16222
16223 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016224 if (*tag_pattern == NUL)
16225 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016226
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016227 if (rettv_list_alloc(rettv) == OK)
16228 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016229}
16230
16231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 * "tempname()" function
16233 */
16234/*ARGSUSED*/
16235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016236f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016237 typval_T *argvars;
16238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239{
16240 static int x = 'A';
16241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016242 rettv->v_type = VAR_STRING;
16243 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244
16245 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16246 * names. Skip 'I' and 'O', they are used for shell redirection. */
16247 do
16248 {
16249 if (x == 'Z')
16250 x = '0';
16251 else if (x == '9')
16252 x = 'A';
16253 else
16254 {
16255#ifdef EBCDIC
16256 if (x == 'I')
16257 x = 'J';
16258 else if (x == 'R')
16259 x = 'S';
16260 else
16261#endif
16262 ++x;
16263 }
16264 } while (x == 'I' || x == 'O');
16265}
16266
16267/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016268 * "test(list)" function: Just checking the walls...
16269 */
16270/*ARGSUSED*/
16271 static void
16272f_test(argvars, rettv)
16273 typval_T *argvars;
16274 typval_T *rettv;
16275{
16276 /* Used for unit testing. Change the code below to your liking. */
16277#if 0
16278 listitem_T *li;
16279 list_T *l;
16280 char_u *bad, *good;
16281
16282 if (argvars[0].v_type != VAR_LIST)
16283 return;
16284 l = argvars[0].vval.v_list;
16285 if (l == NULL)
16286 return;
16287 li = l->lv_first;
16288 if (li == NULL)
16289 return;
16290 bad = get_tv_string(&li->li_tv);
16291 li = li->li_next;
16292 if (li == NULL)
16293 return;
16294 good = get_tv_string(&li->li_tv);
16295 rettv->vval.v_number = test_edit_score(bad, good);
16296#endif
16297}
16298
16299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300 * "tolower(string)" function
16301 */
16302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016303f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 typval_T *argvars;
16305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306{
16307 char_u *p;
16308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016309 p = vim_strsave(get_tv_string(&argvars[0]));
16310 rettv->v_type = VAR_STRING;
16311 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312
16313 if (p != NULL)
16314 while (*p != NUL)
16315 {
16316#ifdef FEAT_MBYTE
16317 int l;
16318
16319 if (enc_utf8)
16320 {
16321 int c, lc;
16322
16323 c = utf_ptr2char(p);
16324 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016325 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326 /* TODO: reallocate string when byte count changes. */
16327 if (utf_char2len(lc) == l)
16328 utf_char2bytes(lc, p);
16329 p += l;
16330 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016331 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332 p += l; /* skip multi-byte character */
16333 else
16334#endif
16335 {
16336 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16337 ++p;
16338 }
16339 }
16340}
16341
16342/*
16343 * "toupper(string)" function
16344 */
16345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016346f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016347 typval_T *argvars;
16348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016351 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352}
16353
16354/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016355 * "tr(string, fromstr, tostr)" function
16356 */
16357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016358f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016359 typval_T *argvars;
16360 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016361{
16362 char_u *instr;
16363 char_u *fromstr;
16364 char_u *tostr;
16365 char_u *p;
16366#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016367 int inlen;
16368 int fromlen;
16369 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016370 int idx;
16371 char_u *cpstr;
16372 int cplen;
16373 int first = TRUE;
16374#endif
16375 char_u buf[NUMBUFLEN];
16376 char_u buf2[NUMBUFLEN];
16377 garray_T ga;
16378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016379 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016380 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16381 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016382
16383 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016384 rettv->v_type = VAR_STRING;
16385 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016386 if (fromstr == NULL || tostr == NULL)
16387 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016388 ga_init2(&ga, (int)sizeof(char), 80);
16389
16390#ifdef FEAT_MBYTE
16391 if (!has_mbyte)
16392#endif
16393 /* not multi-byte: fromstr and tostr must be the same length */
16394 if (STRLEN(fromstr) != STRLEN(tostr))
16395 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016396#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016397error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016398#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016399 EMSG2(_(e_invarg2), fromstr);
16400 ga_clear(&ga);
16401 return;
16402 }
16403
16404 /* fromstr and tostr have to contain the same number of chars */
16405 while (*instr != NUL)
16406 {
16407#ifdef FEAT_MBYTE
16408 if (has_mbyte)
16409 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016410 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016411 cpstr = instr;
16412 cplen = inlen;
16413 idx = 0;
16414 for (p = fromstr; *p != NUL; p += fromlen)
16415 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016416 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016417 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16418 {
16419 for (p = tostr; *p != NUL; p += tolen)
16420 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016421 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016422 if (idx-- == 0)
16423 {
16424 cplen = tolen;
16425 cpstr = p;
16426 break;
16427 }
16428 }
16429 if (*p == NUL) /* tostr is shorter than fromstr */
16430 goto error;
16431 break;
16432 }
16433 ++idx;
16434 }
16435
16436 if (first && cpstr == instr)
16437 {
16438 /* Check that fromstr and tostr have the same number of
16439 * (multi-byte) characters. Done only once when a character
16440 * of instr doesn't appear in fromstr. */
16441 first = FALSE;
16442 for (p = tostr; *p != NUL; p += tolen)
16443 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016444 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016445 --idx;
16446 }
16447 if (idx != 0)
16448 goto error;
16449 }
16450
16451 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016452 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016453 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016454
16455 instr += inlen;
16456 }
16457 else
16458#endif
16459 {
16460 /* When not using multi-byte chars we can do it faster. */
16461 p = vim_strchr(fromstr, *instr);
16462 if (p != NULL)
16463 ga_append(&ga, tostr[p - fromstr]);
16464 else
16465 ga_append(&ga, *instr);
16466 ++instr;
16467 }
16468 }
16469
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016470 /* add a terminating NUL */
16471 ga_grow(&ga, 1);
16472 ga_append(&ga, NUL);
16473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016474 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016475}
16476
16477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 * "type(expr)" function
16479 */
16480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016481f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016482 typval_T *argvars;
16483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016485 int n;
16486
16487 switch (argvars[0].v_type)
16488 {
16489 case VAR_NUMBER: n = 0; break;
16490 case VAR_STRING: n = 1; break;
16491 case VAR_FUNC: n = 2; break;
16492 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016493 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016494 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16495 }
16496 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497}
16498
16499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016500 * "values(dict)" function
16501 */
16502 static void
16503f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016504 typval_T *argvars;
16505 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016506{
16507 dict_list(argvars, rettv, 1);
16508}
16509
16510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 * "virtcol(string)" function
16512 */
16513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016514f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016515 typval_T *argvars;
16516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517{
16518 colnr_T vcol = 0;
16519 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016520 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016522 fp = var2fpos(&argvars[0], FALSE, &fnum);
16523 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16524 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 {
16526 getvvcol(curwin, fp, NULL, NULL, &vcol);
16527 ++vcol;
16528 }
16529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016530 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531}
16532
16533/*
16534 * "visualmode()" function
16535 */
16536/*ARGSUSED*/
16537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016538f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016539 typval_T *argvars;
16540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541{
16542#ifdef FEAT_VISUAL
16543 char_u str[2];
16544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016545 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 str[0] = curbuf->b_visual_mode_eval;
16547 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016548 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549
16550 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016551 if ((argvars[0].v_type == VAR_NUMBER
16552 && argvars[0].vval.v_number != 0)
16553 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016554 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 curbuf->b_visual_mode_eval = NUL;
16556#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016557 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558#endif
16559}
16560
16561/*
16562 * "winbufnr(nr)" function
16563 */
16564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016565f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016566 typval_T *argvars;
16567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568{
16569 win_T *wp;
16570
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016571 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016575 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576}
16577
16578/*
16579 * "wincol()" function
16580 */
16581/*ARGSUSED*/
16582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016583f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016584 typval_T *argvars;
16585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586{
16587 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016588 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589}
16590
16591/*
16592 * "winheight(nr)" function
16593 */
16594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016595f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 typval_T *argvars;
16597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598{
16599 win_T *wp;
16600
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016601 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016605 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606}
16607
16608/*
16609 * "winline()" function
16610 */
16611/*ARGSUSED*/
16612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016614 typval_T *argvars;
16615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616{
16617 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016618 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619}
16620
16621/*
16622 * "winnr()" function
16623 */
16624/* ARGSUSED */
16625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016627 typval_T *argvars;
16628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629{
16630 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016631
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016633 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016635 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636}
16637
16638/*
16639 * "winrestcmd()" function
16640 */
16641/* ARGSUSED */
16642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016643f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016644 typval_T *argvars;
16645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646{
16647#ifdef FEAT_WINDOWS
16648 win_T *wp;
16649 int winnr = 1;
16650 garray_T ga;
16651 char_u buf[50];
16652
16653 ga_init2(&ga, (int)sizeof(char), 70);
16654 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16655 {
16656 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16657 ga_concat(&ga, buf);
16658# ifdef FEAT_VERTSPLIT
16659 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16660 ga_concat(&ga, buf);
16661# endif
16662 ++winnr;
16663 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016664 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016666 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016668 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016670 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671}
16672
16673/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016674 * "winrestview()" function
16675 */
16676/* ARGSUSED */
16677 static void
16678f_winrestview(argvars, rettv)
16679 typval_T *argvars;
16680 typval_T *rettv;
16681{
16682 dict_T *dict;
16683
16684 if (argvars[0].v_type != VAR_DICT
16685 || (dict = argvars[0].vval.v_dict) == NULL)
16686 EMSG(_(e_invarg));
16687 else
16688 {
16689 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16690 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16691#ifdef FEAT_VIRTUALEDIT
16692 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16693#endif
16694 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016695 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016696
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016697 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016698#ifdef FEAT_DIFF
16699 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16700#endif
16701 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16702 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16703
16704 check_cursor();
16705 changed_cline_bef_curs();
16706 invalidate_botline();
16707 redraw_later(VALID);
16708
16709 if (curwin->w_topline == 0)
16710 curwin->w_topline = 1;
16711 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16712 curwin->w_topline = curbuf->b_ml.ml_line_count;
16713#ifdef FEAT_DIFF
16714 check_topfill(curwin, TRUE);
16715#endif
16716 }
16717}
16718
16719/*
16720 * "winsaveview()" function
16721 */
16722/* ARGSUSED */
16723 static void
16724f_winsaveview(argvars, rettv)
16725 typval_T *argvars;
16726 typval_T *rettv;
16727{
16728 dict_T *dict;
16729
16730 dict = dict_alloc();
16731 if (dict == NULL)
16732 return;
16733 rettv->v_type = VAR_DICT;
16734 rettv->vval.v_dict = dict;
16735 ++dict->dv_refcount;
16736
16737 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16738 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16739#ifdef FEAT_VIRTUALEDIT
16740 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16741#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016742 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016743 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16744
16745 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16746#ifdef FEAT_DIFF
16747 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16748#endif
16749 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16750 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16751}
16752
16753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754 * "winwidth(nr)" function
16755 */
16756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016757f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016758 typval_T *argvars;
16759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760{
16761 win_T *wp;
16762
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016763 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016765 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 else
16767#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016768 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771#endif
16772}
16773
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016775 * "writefile()" function
16776 */
16777 static void
16778f_writefile(argvars, rettv)
16779 typval_T *argvars;
16780 typval_T *rettv;
16781{
16782 int binary = FALSE;
16783 char_u *fname;
16784 FILE *fd;
16785 listitem_T *li;
16786 char_u *s;
16787 int ret = 0;
16788 int c;
16789
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016790 if (check_restricted() || check_secure())
16791 return;
16792
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016793 if (argvars[0].v_type != VAR_LIST)
16794 {
16795 EMSG2(_(e_listarg), "writefile()");
16796 return;
16797 }
16798 if (argvars[0].vval.v_list == NULL)
16799 return;
16800
16801 if (argvars[2].v_type != VAR_UNKNOWN
16802 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16803 binary = TRUE;
16804
16805 /* Always open the file in binary mode, library functions have a mind of
16806 * their own about CR-LF conversion. */
16807 fname = get_tv_string(&argvars[1]);
16808 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16809 {
16810 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16811 ret = -1;
16812 }
16813 else
16814 {
16815 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16816 li = li->li_next)
16817 {
16818 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16819 {
16820 if (*s == '\n')
16821 c = putc(NUL, fd);
16822 else
16823 c = putc(*s, fd);
16824 if (c == EOF)
16825 {
16826 ret = -1;
16827 break;
16828 }
16829 }
16830 if (!binary || li->li_next != NULL)
16831 if (putc('\n', fd) == EOF)
16832 {
16833 ret = -1;
16834 break;
16835 }
16836 if (ret < 0)
16837 {
16838 EMSG(_(e_write));
16839 break;
16840 }
16841 }
16842 fclose(fd);
16843 }
16844
16845 rettv->vval.v_number = ret;
16846}
16847
16848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016850 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 */
16852 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016853var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016854 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016855 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016856 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016858 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016860 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861
Bram Moolenaara5525202006-03-02 22:52:09 +000016862 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016863 if (varp->v_type == VAR_LIST)
16864 {
16865 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016866 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016867 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016868 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016869
16870 l = varp->vval.v_list;
16871 if (l == NULL)
16872 return NULL;
16873
16874 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016875 pos.lnum = list_find_nr(l, 0L, &error);
16876 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016877 return NULL; /* invalid line number */
16878
16879 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016880 pos.col = list_find_nr(l, 1L, &error);
16881 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016882 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016883 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016884
16885 /* We accept "$" for the column number: last column. */
16886 li = list_find(l, 1L);
16887 if (li != NULL && li->li_tv.v_type == VAR_STRING
16888 && li->li_tv.vval.v_string != NULL
16889 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16890 pos.col = len + 1;
16891
Bram Moolenaara5525202006-03-02 22:52:09 +000016892 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016893 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016894 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016895 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016896
Bram Moolenaara5525202006-03-02 22:52:09 +000016897#ifdef FEAT_VIRTUALEDIT
16898 /* Get the virtual offset. Defaults to zero. */
16899 pos.coladd = list_find_nr(l, 2L, &error);
16900 if (error)
16901 pos.coladd = 0;
16902#endif
16903
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016904 return &pos;
16905 }
16906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016907 name = get_tv_string_chk(varp);
16908 if (name == NULL)
16909 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910 if (name[0] == '.') /* cursor */
16911 return &curwin->w_cursor;
16912 if (name[0] == '\'') /* mark */
16913 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016914 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16916 return NULL;
16917 return pp;
16918 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016919
16920#ifdef FEAT_VIRTUALEDIT
16921 pos.coladd = 0;
16922#endif
16923
Bram Moolenaar477933c2007-07-17 14:32:23 +000016924 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016925 {
16926 pos.col = 0;
16927 if (name[1] == '0') /* "w0": first visible line */
16928 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016929 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016930 pos.lnum = curwin->w_topline;
16931 return &pos;
16932 }
16933 else if (name[1] == '$') /* "w$": last visible line */
16934 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016935 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016936 pos.lnum = curwin->w_botline - 1;
16937 return &pos;
16938 }
16939 }
16940 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016942 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943 {
16944 pos.lnum = curbuf->b_ml.ml_line_count;
16945 pos.col = 0;
16946 }
16947 else
16948 {
16949 pos.lnum = curwin->w_cursor.lnum;
16950 pos.col = (colnr_T)STRLEN(ml_get_curline());
16951 }
16952 return &pos;
16953 }
16954 return NULL;
16955}
16956
16957/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016958 * Convert list in "arg" into a position and optional file number.
16959 * When "fnump" is NULL there is no file number, only 3 items.
16960 * Note that the column is passed on as-is, the caller may want to decrement
16961 * it to use 1 for the first column.
16962 * Return FAIL when conversion is not possible, doesn't check the position for
16963 * validity.
16964 */
16965 static int
16966list2fpos(arg, posp, fnump)
16967 typval_T *arg;
16968 pos_T *posp;
16969 int *fnump;
16970{
16971 list_T *l = arg->vval.v_list;
16972 long i = 0;
16973 long n;
16974
Bram Moolenaarbde35262006-07-23 20:12:24 +000016975 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16976 * when "fnump" isn't NULL and "coladd" is optional. */
16977 if (arg->v_type != VAR_LIST
16978 || l == NULL
16979 || l->lv_len < (fnump == NULL ? 2 : 3)
16980 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016981 return FAIL;
16982
16983 if (fnump != NULL)
16984 {
16985 n = list_find_nr(l, i++, NULL); /* fnum */
16986 if (n < 0)
16987 return FAIL;
16988 if (n == 0)
16989 n = curbuf->b_fnum; /* current buffer */
16990 *fnump = n;
16991 }
16992
16993 n = list_find_nr(l, i++, NULL); /* lnum */
16994 if (n < 0)
16995 return FAIL;
16996 posp->lnum = n;
16997
16998 n = list_find_nr(l, i++, NULL); /* col */
16999 if (n < 0)
17000 return FAIL;
17001 posp->col = n;
17002
17003#ifdef FEAT_VIRTUALEDIT
17004 n = list_find_nr(l, i, NULL);
17005 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017006 posp->coladd = 0;
17007 else
17008 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017009#endif
17010
17011 return OK;
17012}
17013
17014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 * Get the length of an environment variable name.
17016 * Advance "arg" to the first character after the name.
17017 * Return 0 for error.
17018 */
17019 static int
17020get_env_len(arg)
17021 char_u **arg;
17022{
17023 char_u *p;
17024 int len;
17025
17026 for (p = *arg; vim_isIDc(*p); ++p)
17027 ;
17028 if (p == *arg) /* no name found */
17029 return 0;
17030
17031 len = (int)(p - *arg);
17032 *arg = p;
17033 return len;
17034}
17035
17036/*
17037 * Get the length of the name of a function or internal variable.
17038 * "arg" is advanced to the first non-white character after the name.
17039 * Return 0 if something is wrong.
17040 */
17041 static int
17042get_id_len(arg)
17043 char_u **arg;
17044{
17045 char_u *p;
17046 int len;
17047
17048 /* Find the end of the name. */
17049 for (p = *arg; eval_isnamec(*p); ++p)
17050 ;
17051 if (p == *arg) /* no name found */
17052 return 0;
17053
17054 len = (int)(p - *arg);
17055 *arg = skipwhite(p);
17056
17057 return len;
17058}
17059
17060/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017061 * Get the length of the name of a variable or function.
17062 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017064 * Return -1 if curly braces expansion failed.
17065 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 * If the name contains 'magic' {}'s, expand them and return the
17067 * expanded name in an allocated string via 'alias' - caller must free.
17068 */
17069 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017070get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071 char_u **arg;
17072 char_u **alias;
17073 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017074 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075{
17076 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 char_u *p;
17078 char_u *expr_start;
17079 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080
17081 *alias = NULL; /* default to no alias */
17082
17083 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17084 && (*arg)[2] == (int)KE_SNR)
17085 {
17086 /* hard coded <SNR>, already translated */
17087 *arg += 3;
17088 return get_id_len(arg) + 3;
17089 }
17090 len = eval_fname_script(*arg);
17091 if (len > 0)
17092 {
17093 /* literal "<SID>", "s:" or "<SNR>" */
17094 *arg += len;
17095 }
17096
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017098 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017100 p = find_name_end(*arg, &expr_start, &expr_end,
17101 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 if (expr_start != NULL)
17103 {
17104 char_u *temp_string;
17105
17106 if (!evaluate)
17107 {
17108 len += (int)(p - *arg);
17109 *arg = skipwhite(p);
17110 return len;
17111 }
17112
17113 /*
17114 * Include any <SID> etc in the expanded string:
17115 * Thus the -len here.
17116 */
17117 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17118 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017119 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 *alias = temp_string;
17121 *arg = skipwhite(p);
17122 return (int)STRLEN(temp_string);
17123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124
17125 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017126 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 EMSG2(_(e_invexpr2), *arg);
17128
17129 return len;
17130}
17131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017132/*
17133 * Find the end of a variable or function name, taking care of magic braces.
17134 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17135 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017136 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017137 * Return a pointer to just after the name. Equal to "arg" if there is no
17138 * valid name.
17139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017141find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 char_u *arg;
17143 char_u **expr_start;
17144 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017145 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017147 int mb_nest = 0;
17148 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 char_u *p;
17150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017151 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017153 *expr_start = NULL;
17154 *expr_end = NULL;
17155 }
17156
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017157 /* Quick check for valid starting character. */
17158 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17159 return arg;
17160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017161 for (p = arg; *p != NUL
17162 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017163 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017164 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017166 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017167 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017168 if (*p == '\'')
17169 {
17170 /* skip over 'string' to avoid counting [ and ] inside it. */
17171 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17172 ;
17173 if (*p == NUL)
17174 break;
17175 }
17176 else if (*p == '"')
17177 {
17178 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17179 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17180 if (*p == '\\' && p[1] != NUL)
17181 ++p;
17182 if (*p == NUL)
17183 break;
17184 }
17185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017186 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188 if (*p == '[')
17189 ++br_nest;
17190 else if (*p == ']')
17191 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017194 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017196 if (*p == '{')
17197 {
17198 mb_nest++;
17199 if (expr_start != NULL && *expr_start == NULL)
17200 *expr_start = p;
17201 }
17202 else if (*p == '}')
17203 {
17204 mb_nest--;
17205 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17206 *expr_end = p;
17207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 }
17210
17211 return p;
17212}
17213
17214/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017215 * Expands out the 'magic' {}'s in a variable/function name.
17216 * Note that this can call itself recursively, to deal with
17217 * constructs like foo{bar}{baz}{bam}
17218 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17219 * "in_start" ^
17220 * "expr_start" ^
17221 * "expr_end" ^
17222 * "in_end" ^
17223 *
17224 * Returns a new allocated string, which the caller must free.
17225 * Returns NULL for failure.
17226 */
17227 static char_u *
17228make_expanded_name(in_start, expr_start, expr_end, in_end)
17229 char_u *in_start;
17230 char_u *expr_start;
17231 char_u *expr_end;
17232 char_u *in_end;
17233{
17234 char_u c1;
17235 char_u *retval = NULL;
17236 char_u *temp_result;
17237 char_u *nextcmd = NULL;
17238
17239 if (expr_end == NULL || in_end == NULL)
17240 return NULL;
17241 *expr_start = NUL;
17242 *expr_end = NUL;
17243 c1 = *in_end;
17244 *in_end = NUL;
17245
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017246 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017247 if (temp_result != NULL && nextcmd == NULL)
17248 {
17249 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17250 + (in_end - expr_end) + 1));
17251 if (retval != NULL)
17252 {
17253 STRCPY(retval, in_start);
17254 STRCAT(retval, temp_result);
17255 STRCAT(retval, expr_end + 1);
17256 }
17257 }
17258 vim_free(temp_result);
17259
17260 *in_end = c1; /* put char back for error messages */
17261 *expr_start = '{';
17262 *expr_end = '}';
17263
17264 if (retval != NULL)
17265 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017266 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017267 if (expr_start != NULL)
17268 {
17269 /* Further expansion! */
17270 temp_result = make_expanded_name(retval, expr_start,
17271 expr_end, temp_result);
17272 vim_free(retval);
17273 retval = temp_result;
17274 }
17275 }
17276
17277 return retval;
17278}
17279
17280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017282 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 */
17284 static int
17285eval_isnamec(c)
17286 int c;
17287{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017288 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17289}
17290
17291/*
17292 * Return TRUE if character "c" can be used as the first character in a
17293 * variable or function name (excluding '{' and '}').
17294 */
17295 static int
17296eval_isnamec1(c)
17297 int c;
17298{
17299 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300}
17301
17302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 * Set number v: variable to "val".
17304 */
17305 void
17306set_vim_var_nr(idx, val)
17307 int idx;
17308 long val;
17309{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017310 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311}
17312
17313/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017314 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 */
17316 long
17317get_vim_var_nr(idx)
17318 int idx;
17319{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017320 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321}
17322
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017323#if defined(FEAT_AUTOCMD) || defined(PROTO)
17324/*
17325 * Get string v: variable value. Uses a static buffer, can only be used once.
17326 */
17327 char_u *
17328get_vim_var_str(idx)
17329 int idx;
17330{
17331 return get_tv_string(&vimvars[idx].vv_tv);
17332}
17333#endif
17334
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335/*
17336 * Set v:count, v:count1 and v:prevcount.
17337 */
17338 void
17339set_vcount(count, count1)
17340 long count;
17341 long count1;
17342{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017343 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17344 vimvars[VV_COUNT].vv_nr = count;
17345 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346}
17347
17348/*
17349 * Set string v: variable to a copy of "val".
17350 */
17351 void
17352set_vim_var_string(idx, val, len)
17353 int idx;
17354 char_u *val;
17355 int len; /* length of "val" to use or -1 (whole string) */
17356{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017357 /* Need to do this (at least) once, since we can't initialize a union.
17358 * Will always be invoked when "v:progname" is set. */
17359 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17360
Bram Moolenaare9a41262005-01-15 22:18:47 +000017361 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017363 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017365 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017367 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368}
17369
17370/*
17371 * Set v:register if needed.
17372 */
17373 void
17374set_reg_var(c)
17375 int c;
17376{
17377 char_u regname;
17378
17379 if (c == 0 || c == ' ')
17380 regname = '"';
17381 else
17382 regname = c;
17383 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017384 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 set_vim_var_string(VV_REG, &regname, 1);
17386}
17387
17388/*
17389 * Get or set v:exception. If "oldval" == NULL, return the current value.
17390 * Otherwise, restore the value to "oldval" and return NULL.
17391 * Must always be called in pairs to save and restore v:exception! Does not
17392 * take care of memory allocations.
17393 */
17394 char_u *
17395v_exception(oldval)
17396 char_u *oldval;
17397{
17398 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017399 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400
Bram Moolenaare9a41262005-01-15 22:18:47 +000017401 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 return NULL;
17403}
17404
17405/*
17406 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17407 * Otherwise, restore the value to "oldval" and return NULL.
17408 * Must always be called in pairs to save and restore v:throwpoint! Does not
17409 * take care of memory allocations.
17410 */
17411 char_u *
17412v_throwpoint(oldval)
17413 char_u *oldval;
17414{
17415 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017416 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417
Bram Moolenaare9a41262005-01-15 22:18:47 +000017418 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 return NULL;
17420}
17421
17422#if defined(FEAT_AUTOCMD) || defined(PROTO)
17423/*
17424 * Set v:cmdarg.
17425 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17426 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17427 * Must always be called in pairs!
17428 */
17429 char_u *
17430set_cmdarg(eap, oldarg)
17431 exarg_T *eap;
17432 char_u *oldarg;
17433{
17434 char_u *oldval;
17435 char_u *newval;
17436 unsigned len;
17437
Bram Moolenaare9a41262005-01-15 22:18:47 +000017438 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017439 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017441 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017442 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017443 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 }
17445
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017446 if (eap->force_bin == FORCE_BIN)
17447 len = 6;
17448 else if (eap->force_bin == FORCE_NOBIN)
17449 len = 8;
17450 else
17451 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017452
17453 if (eap->read_edit)
17454 len += 7;
17455
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017456 if (eap->force_ff != 0)
17457 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17458# ifdef FEAT_MBYTE
17459 if (eap->force_enc != 0)
17460 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017461 if (eap->bad_char != 0)
17462 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017463# endif
17464
17465 newval = alloc(len + 1);
17466 if (newval == NULL)
17467 return NULL;
17468
17469 if (eap->force_bin == FORCE_BIN)
17470 sprintf((char *)newval, " ++bin");
17471 else if (eap->force_bin == FORCE_NOBIN)
17472 sprintf((char *)newval, " ++nobin");
17473 else
17474 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017475
17476 if (eap->read_edit)
17477 STRCAT(newval, " ++edit");
17478
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017479 if (eap->force_ff != 0)
17480 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17481 eap->cmd + eap->force_ff);
17482# ifdef FEAT_MBYTE
17483 if (eap->force_enc != 0)
17484 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17485 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017486 if (eap->bad_char != 0)
17487 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17488 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017489# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017490 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017491 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492}
17493#endif
17494
17495/*
17496 * Get the value of internal variable "name".
17497 * Return OK or FAIL.
17498 */
17499 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017500get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 char_u *name;
17502 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017503 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017504 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505{
17506 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017507 typval_T *tv = NULL;
17508 typval_T atv;
17509 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511
17512 /* truncate the name, so that we can use strcmp() */
17513 cc = name[len];
17514 name[len] = NUL;
17515
17516 /*
17517 * Check for "b:changedtick".
17518 */
17519 if (STRCMP(name, "b:changedtick") == 0)
17520 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017521 atv.v_type = VAR_NUMBER;
17522 atv.vval.v_number = curbuf->b_changedtick;
17523 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 }
17525
17526 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 * Check for user-defined variables.
17528 */
17529 else
17530 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017531 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017533 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 }
17535
Bram Moolenaare9a41262005-01-15 22:18:47 +000017536 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017538 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017539 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 ret = FAIL;
17541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017542 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017543 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544
17545 name[len] = cc;
17546
17547 return ret;
17548}
17549
17550/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017551 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17552 * Also handle function call with Funcref variable: func(expr)
17553 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17554 */
17555 static int
17556handle_subscript(arg, rettv, evaluate, verbose)
17557 char_u **arg;
17558 typval_T *rettv;
17559 int evaluate; /* do more than finding the end */
17560 int verbose; /* give error messages */
17561{
17562 int ret = OK;
17563 dict_T *selfdict = NULL;
17564 char_u *s;
17565 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017566 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017567
17568 while (ret == OK
17569 && (**arg == '['
17570 || (**arg == '.' && rettv->v_type == VAR_DICT)
17571 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17572 && !vim_iswhite(*(*arg - 1)))
17573 {
17574 if (**arg == '(')
17575 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017576 /* need to copy the funcref so that we can clear rettv */
17577 functv = *rettv;
17578 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017579
17580 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017581 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017582 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017583 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17584 &len, evaluate, selfdict);
17585
17586 /* Clear the funcref afterwards, so that deleting it while
17587 * evaluating the arguments is possible (see test55). */
17588 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017589
17590 /* Stop the expression evaluation when immediately aborting on
17591 * error, or when an interrupt occurred or an exception was thrown
17592 * but not caught. */
17593 if (aborting())
17594 {
17595 if (ret == OK)
17596 clear_tv(rettv);
17597 ret = FAIL;
17598 }
17599 dict_unref(selfdict);
17600 selfdict = NULL;
17601 }
17602 else /* **arg == '[' || **arg == '.' */
17603 {
17604 dict_unref(selfdict);
17605 if (rettv->v_type == VAR_DICT)
17606 {
17607 selfdict = rettv->vval.v_dict;
17608 if (selfdict != NULL)
17609 ++selfdict->dv_refcount;
17610 }
17611 else
17612 selfdict = NULL;
17613 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17614 {
17615 clear_tv(rettv);
17616 ret = FAIL;
17617 }
17618 }
17619 }
17620 dict_unref(selfdict);
17621 return ret;
17622}
17623
17624/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017625 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17626 * value).
17627 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017628 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017629alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017630{
Bram Moolenaar33570922005-01-25 22:26:29 +000017631 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017632}
17633
17634/*
17635 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 * The string "s" must have been allocated, it is consumed.
17637 * Return NULL for out of memory, the variable otherwise.
17638 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017639 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017640alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 char_u *s;
17642{
Bram Moolenaar33570922005-01-25 22:26:29 +000017643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017645 rettv = alloc_tv();
17646 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017648 rettv->v_type = VAR_STRING;
17649 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 }
17651 else
17652 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017653 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654}
17655
17656/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017657 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017659 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017660free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017661 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662{
17663 if (varp != NULL)
17664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017665 switch (varp->v_type)
17666 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017667 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017668 func_unref(varp->vval.v_string);
17669 /*FALLTHROUGH*/
17670 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017671 vim_free(varp->vval.v_string);
17672 break;
17673 case VAR_LIST:
17674 list_unref(varp->vval.v_list);
17675 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017676 case VAR_DICT:
17677 dict_unref(varp->vval.v_dict);
17678 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017679 case VAR_NUMBER:
17680 case VAR_UNKNOWN:
17681 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017682 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017683 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017684 break;
17685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 vim_free(varp);
17687 }
17688}
17689
17690/*
17691 * Free the memory for a variable value and set the value to NULL or 0.
17692 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017693 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017694clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017695 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696{
17697 if (varp != NULL)
17698 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017699 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017701 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017702 func_unref(varp->vval.v_string);
17703 /*FALLTHROUGH*/
17704 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017705 vim_free(varp->vval.v_string);
17706 varp->vval.v_string = NULL;
17707 break;
17708 case VAR_LIST:
17709 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017710 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017711 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017712 case VAR_DICT:
17713 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017714 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017715 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017716 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017717 varp->vval.v_number = 0;
17718 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719 case VAR_UNKNOWN:
17720 break;
17721 default:
17722 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017724 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 }
17726}
17727
17728/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017729 * Set the value of a variable to NULL without freeing items.
17730 */
17731 static void
17732init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017733 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017734{
17735 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017736 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017737}
17738
17739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 * Get the number value of a variable.
17741 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017742 * For incompatible types, return 0.
17743 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17744 * caller of incompatible types: it sets *denote to TRUE if "denote"
17745 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 */
17747 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017748get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017749 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017751 int error = FALSE;
17752
17753 return get_tv_number_chk(varp, &error); /* return 0L on error */
17754}
17755
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017756 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017757get_tv_number_chk(varp, denote)
17758 typval_T *varp;
17759 int *denote;
17760{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017761 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017763 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017765 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017766 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017767 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017768 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017769 break;
17770 case VAR_STRING:
17771 if (varp->vval.v_string != NULL)
17772 vim_str2nr(varp->vval.v_string, NULL, NULL,
17773 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017774 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017775 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017776 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017777 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017778 case VAR_DICT:
17779 EMSG(_("E728: Using a Dictionary as a number"));
17780 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017781 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017782 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017783 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017785 if (denote == NULL) /* useful for values that must be unsigned */
17786 n = -1;
17787 else
17788 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017789 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790}
17791
17792/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017793 * Get the lnum from the first argument.
17794 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017795 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 */
17797 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017798get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017799 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800{
Bram Moolenaar33570922005-01-25 22:26:29 +000017801 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 linenr_T lnum;
17803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017804 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 if (lnum == 0) /* no valid number, try using line() */
17806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017807 rettv.v_type = VAR_NUMBER;
17808 f_line(argvars, &rettv);
17809 lnum = rettv.vval.v_number;
17810 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 }
17812 return lnum;
17813}
17814
17815/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017816 * Get the lnum from the first argument.
17817 * Also accepts "$", then "buf" is used.
17818 * Returns 0 on error.
17819 */
17820 static linenr_T
17821get_tv_lnum_buf(argvars, buf)
17822 typval_T *argvars;
17823 buf_T *buf;
17824{
17825 if (argvars[0].v_type == VAR_STRING
17826 && argvars[0].vval.v_string != NULL
17827 && argvars[0].vval.v_string[0] == '$'
17828 && buf != NULL)
17829 return buf->b_ml.ml_line_count;
17830 return get_tv_number_chk(&argvars[0], NULL);
17831}
17832
17833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 * Get the string value of a variable.
17835 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017836 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17837 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 * If the String variable has never been set, return an empty string.
17839 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017840 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17841 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 */
17843 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017844get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017845 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846{
17847 static char_u mybuf[NUMBUFLEN];
17848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017849 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850}
17851
17852 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017853get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017854 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017855 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017857 char_u *res = get_tv_string_buf_chk(varp, buf);
17858
17859 return res != NULL ? res : (char_u *)"";
17860}
17861
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017862 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017863get_tv_string_chk(varp)
17864 typval_T *varp;
17865{
17866 static char_u mybuf[NUMBUFLEN];
17867
17868 return get_tv_string_buf_chk(varp, mybuf);
17869}
17870
17871 static char_u *
17872get_tv_string_buf_chk(varp, buf)
17873 typval_T *varp;
17874 char_u *buf;
17875{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017876 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017878 case VAR_NUMBER:
17879 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17880 return buf;
17881 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017882 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017883 break;
17884 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017885 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017886 break;
17887 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017888 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017889 break;
17890 case VAR_STRING:
17891 if (varp->vval.v_string != NULL)
17892 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017893 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017894 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017895 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017896 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017898 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899}
17900
17901/*
17902 * Find variable "name" in the list of variables.
17903 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017904 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017905 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017906 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017908 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017909find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017911 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017914 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915
Bram Moolenaara7043832005-01-21 11:56:39 +000017916 ht = find_var_ht(name, &varname);
17917 if (htp != NULL)
17918 *htp = ht;
17919 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017921 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922}
17923
17924/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017925 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017926 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017928 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017929find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017930 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017931 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017932 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017933{
Bram Moolenaar33570922005-01-25 22:26:29 +000017934 hashitem_T *hi;
17935
17936 if (*varname == NUL)
17937 {
17938 /* Must be something like "s:", otherwise "ht" would be NULL. */
17939 switch (varname[-2])
17940 {
17941 case 's': return &SCRIPT_SV(current_SID).sv_var;
17942 case 'g': return &globvars_var;
17943 case 'v': return &vimvars_var;
17944 case 'b': return &curbuf->b_bufvar;
17945 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017946#ifdef FEAT_WINDOWS
17947 case 't': return &curtab->tp_winvar;
17948#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017949 case 'l': return current_funccal == NULL
17950 ? NULL : &current_funccal->l_vars_var;
17951 case 'a': return current_funccal == NULL
17952 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017953 }
17954 return NULL;
17955 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017956
17957 hi = hash_find(ht, varname);
17958 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017959 {
17960 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017961 * worked find the variable again. Don't auto-load a script if it was
17962 * loaded already, otherwise it would be loaded every time when
17963 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017964 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017965 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017966 hi = hash_find(ht, varname);
17967 if (HASHITEM_EMPTY(hi))
17968 return NULL;
17969 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017970 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017971}
17972
17973/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017974 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017975 * Set "varname" to the start of name without ':'.
17976 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017977 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017978find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 char_u *name;
17980 char_u **varname;
17981{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017982 hashitem_T *hi;
17983
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984 if (name[1] != ':')
17985 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017986 /* The name must not start with a colon or #. */
17987 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988 return NULL;
17989 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017990
17991 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017992 hi = hash_find(&compat_hashtab, name);
17993 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017994 return &compat_hashtab;
17995
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017997 return &globvarht; /* global variable */
17998 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999 }
18000 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018001 if (*name == 'g') /* global variable */
18002 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018003 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18004 */
18005 if (vim_strchr(name + 2, ':') != NULL
18006 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018007 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018009 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018011 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018012#ifdef FEAT_WINDOWS
18013 if (*name == 't') /* tab page variable */
18014 return &curtab->tp_vars.dv_hashtab;
18015#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018016 if (*name == 'v') /* v: variable */
18017 return &vimvarht;
18018 if (*name == 'a' && current_funccal != NULL) /* function argument */
18019 return &current_funccal->l_avars.dv_hashtab;
18020 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18021 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 if (*name == 's' /* script variable */
18023 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18024 return &SCRIPT_VARS(current_SID);
18025 return NULL;
18026}
18027
18028/*
18029 * Get the string value of a (global/local) variable.
18030 * Returns NULL when it doesn't exist.
18031 */
18032 char_u *
18033get_var_value(name)
18034 char_u *name;
18035{
Bram Moolenaar33570922005-01-25 22:26:29 +000018036 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037
Bram Moolenaara7043832005-01-21 11:56:39 +000018038 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039 if (v == NULL)
18040 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018041 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042}
18043
18044/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 * sourcing this script and when executing functions defined in the script.
18047 */
18048 void
18049new_script_vars(id)
18050 scid_T id;
18051{
Bram Moolenaara7043832005-01-21 11:56:39 +000018052 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018053 hashtab_T *ht;
18054 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018055
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18057 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018058 /* Re-allocating ga_data means that an ht_array pointing to
18059 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018060 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018061 for (i = 1; i <= ga_scripts.ga_len; ++i)
18062 {
18063 ht = &SCRIPT_VARS(i);
18064 if (ht->ht_mask == HT_INIT_SIZE - 1)
18065 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018066 sv = &SCRIPT_SV(i);
18067 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018068 }
18069
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 while (ga_scripts.ga_len < id)
18071 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018072 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18073 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 }
18076 }
18077}
18078
18079/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018080 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18081 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 */
18083 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018084init_var_dict(dict, dict_var)
18085 dict_T *dict;
18086 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087{
Bram Moolenaar33570922005-01-25 22:26:29 +000018088 hash_init(&dict->dv_hashtab);
18089 dict->dv_refcount = 99999;
18090 dict_var->di_tv.vval.v_dict = dict;
18091 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018092 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018093 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18094 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095}
18096
18097/*
18098 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018099 * Frees all allocated variables and the value they contain.
18100 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 */
18102 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018103vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018104 hashtab_T *ht;
18105{
18106 vars_clear_ext(ht, TRUE);
18107}
18108
18109/*
18110 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18111 */
18112 static void
18113vars_clear_ext(ht, free_val)
18114 hashtab_T *ht;
18115 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116{
Bram Moolenaara7043832005-01-21 11:56:39 +000018117 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018118 hashitem_T *hi;
18119 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120
Bram Moolenaar33570922005-01-25 22:26:29 +000018121 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018122 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018123 for (hi = ht->ht_array; todo > 0; ++hi)
18124 {
18125 if (!HASHITEM_EMPTY(hi))
18126 {
18127 --todo;
18128
Bram Moolenaar33570922005-01-25 22:26:29 +000018129 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018130 * ht_array might change then. hash_clear() takes care of it
18131 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018132 v = HI2DI(hi);
18133 if (free_val)
18134 clear_tv(&v->di_tv);
18135 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18136 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018137 }
18138 }
18139 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018140 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141}
18142
Bram Moolenaara7043832005-01-21 11:56:39 +000018143/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018144 * Delete a variable from hashtab "ht" at item "hi".
18145 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018148delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018149 hashtab_T *ht;
18150 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151{
Bram Moolenaar33570922005-01-25 22:26:29 +000018152 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018153
18154 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018155 clear_tv(&di->di_tv);
18156 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157}
18158
18159/*
18160 * List the value of one internal variable.
18161 */
18162 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018163list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018164 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018166 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018168 char_u *tofree;
18169 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018170 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018171
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018172 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018173 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018174 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018175 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176}
18177
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018179list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 char_u *prefix;
18181 char_u *name;
18182 int type;
18183 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018184 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185{
Bram Moolenaar31859182007-08-14 20:41:13 +000018186 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18187 msg_start();
18188 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 if (name != NULL) /* "a:" vars don't have a name stored */
18190 msg_puts(name);
18191 msg_putchar(' ');
18192 msg_advance(22);
18193 if (type == VAR_NUMBER)
18194 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018195 else if (type == VAR_FUNC)
18196 msg_putchar('*');
18197 else if (type == VAR_LIST)
18198 {
18199 msg_putchar('[');
18200 if (*string == '[')
18201 ++string;
18202 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018203 else if (type == VAR_DICT)
18204 {
18205 msg_putchar('{');
18206 if (*string == '{')
18207 ++string;
18208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 else
18210 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018211
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018213
18214 if (type == VAR_FUNC)
18215 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018216 if (*first)
18217 {
18218 msg_clr_eos();
18219 *first = FALSE;
18220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221}
18222
18223/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018224 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 * If the variable already exists, the value is updated.
18226 * Otherwise the variable is created.
18227 */
18228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018229set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018231 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018232 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233{
Bram Moolenaar33570922005-01-25 22:26:29 +000018234 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018236 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018237 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018239 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018240 {
18241 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18242 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18243 ? name[2] : name[0]))
18244 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018245 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018246 return;
18247 }
18248 if (function_exists(name))
18249 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018250 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018251 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018252 return;
18253 }
18254 }
18255
Bram Moolenaara7043832005-01-21 11:56:39 +000018256 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018257 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018258 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018259 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018260 return;
18261 }
18262
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018263 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018264 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018266 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018267 if (var_check_ro(v->di_flags, name)
18268 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018269 return;
18270 if (v->di_tv.v_type != tv->v_type
18271 && !((v->di_tv.v_type == VAR_STRING
18272 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018273 && (tv->v_type == VAR_STRING
18274 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018275 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018276 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018277 return;
18278 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018279
18280 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018281 * Handle setting internal v: variables separately: we don't change
18282 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018283 */
18284 if (ht == &vimvarht)
18285 {
18286 if (v->di_tv.v_type == VAR_STRING)
18287 {
18288 vim_free(v->di_tv.vval.v_string);
18289 if (copy || tv->v_type != VAR_STRING)
18290 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18291 else
18292 {
18293 /* Take over the string to avoid an extra alloc/free. */
18294 v->di_tv.vval.v_string = tv->vval.v_string;
18295 tv->vval.v_string = NULL;
18296 }
18297 }
18298 else if (v->di_tv.v_type != VAR_NUMBER)
18299 EMSG2(_(e_intern2), "set_var()");
18300 else
18301 v->di_tv.vval.v_number = get_tv_number(tv);
18302 return;
18303 }
18304
18305 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 }
18307 else /* add a new variable */
18308 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018309 /* Can't add "v:" variable. */
18310 if (ht == &vimvarht)
18311 {
18312 EMSG2(_(e_illvar), name);
18313 return;
18314 }
18315
Bram Moolenaar92124a32005-06-17 22:03:40 +000018316 /* Make sure the variable name is valid. */
18317 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018318 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18319 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018320 {
18321 EMSG2(_(e_illvar), varname);
18322 return;
18323 }
18324
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018325 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18326 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018327 if (v == NULL)
18328 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018329 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018330 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018332 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 return;
18334 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018335 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018338 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018339 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018340 else
18341 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018342 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018343 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018344 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346}
18347
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018348/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018349 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018350 * Also give an error message.
18351 */
18352 static int
18353var_check_ro(flags, name)
18354 int flags;
18355 char_u *name;
18356{
18357 if (flags & DI_FLAGS_RO)
18358 {
18359 EMSG2(_(e_readonlyvar), name);
18360 return TRUE;
18361 }
18362 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18363 {
18364 EMSG2(_(e_readonlysbx), name);
18365 return TRUE;
18366 }
18367 return FALSE;
18368}
18369
18370/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018371 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18372 * Also give an error message.
18373 */
18374 static int
18375var_check_fixed(flags, name)
18376 int flags;
18377 char_u *name;
18378{
18379 if (flags & DI_FLAGS_FIX)
18380 {
18381 EMSG2(_("E795: Cannot delete variable %s"), name);
18382 return TRUE;
18383 }
18384 return FALSE;
18385}
18386
18387/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018388 * Return TRUE if typeval "tv" is set to be locked (immutable).
18389 * Also give an error message, using "name".
18390 */
18391 static int
18392tv_check_lock(lock, name)
18393 int lock;
18394 char_u *name;
18395{
18396 if (lock & VAR_LOCKED)
18397 {
18398 EMSG2(_("E741: Value is locked: %s"),
18399 name == NULL ? (char_u *)_("Unknown") : name);
18400 return TRUE;
18401 }
18402 if (lock & VAR_FIXED)
18403 {
18404 EMSG2(_("E742: Cannot change value of %s"),
18405 name == NULL ? (char_u *)_("Unknown") : name);
18406 return TRUE;
18407 }
18408 return FALSE;
18409}
18410
18411/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018412 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018413 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018414 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018417copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018418 typval_T *from;
18419 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018420{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018421 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018422 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018423 switch (from->v_type)
18424 {
18425 case VAR_NUMBER:
18426 to->vval.v_number = from->vval.v_number;
18427 break;
18428 case VAR_STRING:
18429 case VAR_FUNC:
18430 if (from->vval.v_string == NULL)
18431 to->vval.v_string = NULL;
18432 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018434 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018435 if (from->v_type == VAR_FUNC)
18436 func_ref(to->vval.v_string);
18437 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018438 break;
18439 case VAR_LIST:
18440 if (from->vval.v_list == NULL)
18441 to->vval.v_list = NULL;
18442 else
18443 {
18444 to->vval.v_list = from->vval.v_list;
18445 ++to->vval.v_list->lv_refcount;
18446 }
18447 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018448 case VAR_DICT:
18449 if (from->vval.v_dict == NULL)
18450 to->vval.v_dict = NULL;
18451 else
18452 {
18453 to->vval.v_dict = from->vval.v_dict;
18454 ++to->vval.v_dict->dv_refcount;
18455 }
18456 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018457 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018458 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018459 break;
18460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461}
18462
18463/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018464 * Make a copy of an item.
18465 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018466 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18467 * reference to an already copied list/dict can be used.
18468 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018469 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018470 static int
18471item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018472 typval_T *from;
18473 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018474 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018475 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018476{
18477 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018478 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018479
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018481 {
18482 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018483 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018484 }
18485 ++recurse;
18486
18487 switch (from->v_type)
18488 {
18489 case VAR_NUMBER:
18490 case VAR_STRING:
18491 case VAR_FUNC:
18492 copy_tv(from, to);
18493 break;
18494 case VAR_LIST:
18495 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018496 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018497 if (from->vval.v_list == NULL)
18498 to->vval.v_list = NULL;
18499 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18500 {
18501 /* use the copy made earlier */
18502 to->vval.v_list = from->vval.v_list->lv_copylist;
18503 ++to->vval.v_list->lv_refcount;
18504 }
18505 else
18506 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18507 if (to->vval.v_list == NULL)
18508 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018509 break;
18510 case VAR_DICT:
18511 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018512 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018513 if (from->vval.v_dict == NULL)
18514 to->vval.v_dict = NULL;
18515 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18516 {
18517 /* use the copy made earlier */
18518 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18519 ++to->vval.v_dict->dv_refcount;
18520 }
18521 else
18522 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18523 if (to->vval.v_dict == NULL)
18524 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018525 break;
18526 default:
18527 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018528 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018529 }
18530 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018531 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018532}
18533
18534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 * ":echo expr1 ..." print each argument separated with a space, add a
18536 * newline at the end.
18537 * ":echon expr1 ..." print each argument plain.
18538 */
18539 void
18540ex_echo(eap)
18541 exarg_T *eap;
18542{
18543 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018544 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018545 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 char_u *p;
18547 int needclr = TRUE;
18548 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018549 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550
18551 if (eap->skip)
18552 ++emsg_skip;
18553 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18554 {
18555 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018556 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 {
18558 /*
18559 * Report the invalid expression unless the expression evaluation
18560 * has been cancelled due to an aborting error, an interrupt, or an
18561 * exception.
18562 */
18563 if (!aborting())
18564 EMSG2(_(e_invexpr2), p);
18565 break;
18566 }
18567 if (!eap->skip)
18568 {
18569 if (atstart)
18570 {
18571 atstart = FALSE;
18572 /* Call msg_start() after eval1(), evaluating the expression
18573 * may cause a message to appear. */
18574 if (eap->cmdidx == CMD_echo)
18575 msg_start();
18576 }
18577 else if (eap->cmdidx == CMD_echo)
18578 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018579 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018580 if (p != NULL)
18581 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018583 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018585 if (*p != TAB && needclr)
18586 {
18587 /* remove any text still there from the command */
18588 msg_clr_eos();
18589 needclr = FALSE;
18590 }
18591 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 }
18593 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018594 {
18595#ifdef FEAT_MBYTE
18596 if (has_mbyte)
18597 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018598 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018599
18600 (void)msg_outtrans_len_attr(p, i, echo_attr);
18601 p += i - 1;
18602 }
18603 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018605 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018608 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 arg = skipwhite(arg);
18612 }
18613 eap->nextcmd = check_nextcmd(arg);
18614
18615 if (eap->skip)
18616 --emsg_skip;
18617 else
18618 {
18619 /* remove text that may still be there from the command */
18620 if (needclr)
18621 msg_clr_eos();
18622 if (eap->cmdidx == CMD_echo)
18623 msg_end();
18624 }
18625}
18626
18627/*
18628 * ":echohl {name}".
18629 */
18630 void
18631ex_echohl(eap)
18632 exarg_T *eap;
18633{
18634 int id;
18635
18636 id = syn_name2id(eap->arg);
18637 if (id == 0)
18638 echo_attr = 0;
18639 else
18640 echo_attr = syn_id2attr(id);
18641}
18642
18643/*
18644 * ":execute expr1 ..." execute the result of an expression.
18645 * ":echomsg expr1 ..." Print a message
18646 * ":echoerr expr1 ..." Print an error
18647 * Each gets spaces around each argument and a newline at the end for
18648 * echo commands
18649 */
18650 void
18651ex_execute(eap)
18652 exarg_T *eap;
18653{
18654 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018655 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 int ret = OK;
18657 char_u *p;
18658 garray_T ga;
18659 int len;
18660 int save_did_emsg;
18661
18662 ga_init2(&ga, 1, 80);
18663
18664 if (eap->skip)
18665 ++emsg_skip;
18666 while (*arg != NUL && *arg != '|' && *arg != '\n')
18667 {
18668 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 {
18671 /*
18672 * Report the invalid expression unless the expression evaluation
18673 * has been cancelled due to an aborting error, an interrupt, or an
18674 * exception.
18675 */
18676 if (!aborting())
18677 EMSG2(_(e_invexpr2), p);
18678 ret = FAIL;
18679 break;
18680 }
18681
18682 if (!eap->skip)
18683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 len = (int)STRLEN(p);
18686 if (ga_grow(&ga, len + 2) == FAIL)
18687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689 ret = FAIL;
18690 break;
18691 }
18692 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 ga.ga_len += len;
18696 }
18697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 arg = skipwhite(arg);
18700 }
18701
18702 if (ret != FAIL && ga.ga_data != NULL)
18703 {
18704 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018707 out_flush();
18708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 else if (eap->cmdidx == CMD_echoerr)
18710 {
18711 /* We don't want to abort following commands, restore did_emsg. */
18712 save_did_emsg = did_emsg;
18713 EMSG((char_u *)ga.ga_data);
18714 if (!force_abort)
18715 did_emsg = save_did_emsg;
18716 }
18717 else if (eap->cmdidx == CMD_execute)
18718 do_cmdline((char_u *)ga.ga_data,
18719 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18720 }
18721
18722 ga_clear(&ga);
18723
18724 if (eap->skip)
18725 --emsg_skip;
18726
18727 eap->nextcmd = check_nextcmd(arg);
18728}
18729
18730/*
18731 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18732 * "arg" points to the "&" or '+' when called, to "option" when returning.
18733 * Returns NULL when no option name found. Otherwise pointer to the char
18734 * after the option name.
18735 */
18736 static char_u *
18737find_option_end(arg, opt_flags)
18738 char_u **arg;
18739 int *opt_flags;
18740{
18741 char_u *p = *arg;
18742
18743 ++p;
18744 if (*p == 'g' && p[1] == ':')
18745 {
18746 *opt_flags = OPT_GLOBAL;
18747 p += 2;
18748 }
18749 else if (*p == 'l' && p[1] == ':')
18750 {
18751 *opt_flags = OPT_LOCAL;
18752 p += 2;
18753 }
18754 else
18755 *opt_flags = 0;
18756
18757 if (!ASCII_ISALPHA(*p))
18758 return NULL;
18759 *arg = p;
18760
18761 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18762 p += 4; /* termcap option */
18763 else
18764 while (ASCII_ISALPHA(*p))
18765 ++p;
18766 return p;
18767}
18768
18769/*
18770 * ":function"
18771 */
18772 void
18773ex_function(eap)
18774 exarg_T *eap;
18775{
18776 char_u *theline;
18777 int j;
18778 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 char_u *name = NULL;
18781 char_u *p;
18782 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018783 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 garray_T newargs;
18785 garray_T newlines;
18786 int varargs = FALSE;
18787 int mustend = FALSE;
18788 int flags = 0;
18789 ufunc_T *fp;
18790 int indent;
18791 int nesting;
18792 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018793 dictitem_T *v;
18794 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018795 static int func_nr = 0; /* number for nameless function */
18796 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018797 hashtab_T *ht;
18798 int todo;
18799 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018800 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801
18802 /*
18803 * ":function" without argument: list functions.
18804 */
18805 if (ends_excmd(*eap->arg))
18806 {
18807 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018808 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018809 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018810 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018811 {
18812 if (!HASHITEM_EMPTY(hi))
18813 {
18814 --todo;
18815 fp = HI2UF(hi);
18816 if (!isdigit(*fp->uf_name))
18817 list_func_head(fp, FALSE);
18818 }
18819 }
18820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 eap->nextcmd = check_nextcmd(eap->arg);
18822 return;
18823 }
18824
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018825 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018826 * ":function /pat": list functions matching pattern.
18827 */
18828 if (*eap->arg == '/')
18829 {
18830 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18831 if (!eap->skip)
18832 {
18833 regmatch_T regmatch;
18834
18835 c = *p;
18836 *p = NUL;
18837 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18838 *p = c;
18839 if (regmatch.regprog != NULL)
18840 {
18841 regmatch.rm_ic = p_ic;
18842
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018843 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018844 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18845 {
18846 if (!HASHITEM_EMPTY(hi))
18847 {
18848 --todo;
18849 fp = HI2UF(hi);
18850 if (!isdigit(*fp->uf_name)
18851 && vim_regexec(&regmatch, fp->uf_name, 0))
18852 list_func_head(fp, FALSE);
18853 }
18854 }
18855 }
18856 }
18857 if (*p == '/')
18858 ++p;
18859 eap->nextcmd = check_nextcmd(p);
18860 return;
18861 }
18862
18863 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018864 * Get the function name. There are these situations:
18865 * func normal function name
18866 * "name" == func, "fudi.fd_dict" == NULL
18867 * dict.func new dictionary entry
18868 * "name" == NULL, "fudi.fd_dict" set,
18869 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18870 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018871 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018872 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18873 * dict.func existing dict entry that's not a Funcref
18874 * "name" == NULL, "fudi.fd_dict" set,
18875 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018878 name = trans_function_name(&p, eap->skip, 0, &fudi);
18879 paren = (vim_strchr(p, '(') != NULL);
18880 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 {
18882 /*
18883 * Return on an invalid expression in braces, unless the expression
18884 * evaluation has been cancelled due to an aborting error, an
18885 * interrupt, or an exception.
18886 */
18887 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018888 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018889 if (!eap->skip && fudi.fd_newkey != NULL)
18890 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018891 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894 else
18895 eap->skip = TRUE;
18896 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018897
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 /* An error in a function call during evaluation of an expression in magic
18899 * braces should not cause the function not to be defined. */
18900 saved_did_emsg = did_emsg;
18901 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902
18903 /*
18904 * ":function func" with only function name: list function.
18905 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018906 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 {
18908 if (!ends_excmd(*skipwhite(p)))
18909 {
18910 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018911 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 }
18913 eap->nextcmd = check_nextcmd(p);
18914 if (eap->nextcmd != NULL)
18915 *p = NUL;
18916 if (!eap->skip && !got_int)
18917 {
18918 fp = find_func(name);
18919 if (fp != NULL)
18920 {
18921 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018922 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018924 if (FUNCLINE(fp, j) == NULL)
18925 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 msg_putchar('\n');
18927 msg_outnum((long)(j + 1));
18928 if (j < 9)
18929 msg_putchar(' ');
18930 if (j < 99)
18931 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018932 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 out_flush(); /* show a line at a time */
18934 ui_breakcheck();
18935 }
18936 if (!got_int)
18937 {
18938 msg_putchar('\n');
18939 msg_puts((char_u *)" endfunction");
18940 }
18941 }
18942 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018943 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018945 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 }
18947
18948 /*
18949 * ":function name(arg1, arg2)" Define function.
18950 */
18951 p = skipwhite(p);
18952 if (*p != '(')
18953 {
18954 if (!eap->skip)
18955 {
18956 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018957 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 }
18959 /* attempt to continue by skipping some text */
18960 if (vim_strchr(p, '(') != NULL)
18961 p = vim_strchr(p, '(');
18962 }
18963 p = skipwhite(p + 1);
18964
18965 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18966 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18967
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018968 if (!eap->skip)
18969 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018970 /* Check the name of the function. Unless it's a dictionary function
18971 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018972 if (name != NULL)
18973 arg = name;
18974 else
18975 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018976 if (arg != NULL && (fudi.fd_di == NULL
18977 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018978 {
18979 if (*arg == K_SPECIAL)
18980 j = 3;
18981 else
18982 j = 0;
18983 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18984 : eval_isnamec(arg[j])))
18985 ++j;
18986 if (arg[j] != NUL)
18987 emsg_funcname(_(e_invarg2), arg);
18988 }
18989 }
18990
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 /*
18992 * Isolate the arguments: "arg1, arg2, ...)"
18993 */
18994 while (*p != ')')
18995 {
18996 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18997 {
18998 varargs = TRUE;
18999 p += 3;
19000 mustend = TRUE;
19001 }
19002 else
19003 {
19004 arg = p;
19005 while (ASCII_ISALNUM(*p) || *p == '_')
19006 ++p;
19007 if (arg == p || isdigit(*arg)
19008 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19009 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19010 {
19011 if (!eap->skip)
19012 EMSG2(_("E125: Illegal argument: %s"), arg);
19013 break;
19014 }
19015 if (ga_grow(&newargs, 1) == FAIL)
19016 goto erret;
19017 c = *p;
19018 *p = NUL;
19019 arg = vim_strsave(arg);
19020 if (arg == NULL)
19021 goto erret;
19022 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19023 *p = c;
19024 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025 if (*p == ',')
19026 ++p;
19027 else
19028 mustend = TRUE;
19029 }
19030 p = skipwhite(p);
19031 if (mustend && *p != ')')
19032 {
19033 if (!eap->skip)
19034 EMSG2(_(e_invarg2), eap->arg);
19035 break;
19036 }
19037 }
19038 ++p; /* skip the ')' */
19039
Bram Moolenaare9a41262005-01-15 22:18:47 +000019040 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 for (;;)
19042 {
19043 p = skipwhite(p);
19044 if (STRNCMP(p, "range", 5) == 0)
19045 {
19046 flags |= FC_RANGE;
19047 p += 5;
19048 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019049 else if (STRNCMP(p, "dict", 4) == 0)
19050 {
19051 flags |= FC_DICT;
19052 p += 4;
19053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 else if (STRNCMP(p, "abort", 5) == 0)
19055 {
19056 flags |= FC_ABORT;
19057 p += 5;
19058 }
19059 else
19060 break;
19061 }
19062
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019063 /* When there is a line break use what follows for the function body.
19064 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19065 if (*p == '\n')
19066 line_arg = p + 1;
19067 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 EMSG(_(e_trailing));
19069
19070 /*
19071 * Read the body of the function, until ":endfunction" is found.
19072 */
19073 if (KeyTyped)
19074 {
19075 /* Check if the function already exists, don't let the user type the
19076 * whole function before telling him it doesn't work! For a script we
19077 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019078 if (!eap->skip && !eap->forceit)
19079 {
19080 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19081 EMSG(_(e_funcdict));
19082 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019083 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019086 if (!eap->skip && did_emsg)
19087 goto erret;
19088
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 msg_putchar('\n'); /* don't overwrite the function name */
19090 cmdline_row = msg_row;
19091 }
19092
19093 indent = 2;
19094 nesting = 0;
19095 for (;;)
19096 {
19097 msg_scroll = TRUE;
19098 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019099 sourcing_lnum_off = sourcing_lnum;
19100
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019101 if (line_arg != NULL)
19102 {
19103 /* Use eap->arg, split up in parts by line breaks. */
19104 theline = line_arg;
19105 p = vim_strchr(theline, '\n');
19106 if (p == NULL)
19107 line_arg += STRLEN(line_arg);
19108 else
19109 {
19110 *p = NUL;
19111 line_arg = p + 1;
19112 }
19113 }
19114 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 theline = getcmdline(':', 0L, indent);
19116 else
19117 theline = eap->getline(':', eap->cookie, indent);
19118 if (KeyTyped)
19119 lines_left = Rows - 1;
19120 if (theline == NULL)
19121 {
19122 EMSG(_("E126: Missing :endfunction"));
19123 goto erret;
19124 }
19125
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019126 /* Detect line continuation: sourcing_lnum increased more than one. */
19127 if (sourcing_lnum > sourcing_lnum_off + 1)
19128 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19129 else
19130 sourcing_lnum_off = 0;
19131
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 if (skip_until != NULL)
19133 {
19134 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19135 * don't check for ":endfunc". */
19136 if (STRCMP(theline, skip_until) == 0)
19137 {
19138 vim_free(skip_until);
19139 skip_until = NULL;
19140 }
19141 }
19142 else
19143 {
19144 /* skip ':' and blanks*/
19145 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19146 ;
19147
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019148 /* Check for "endfunction". */
19149 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019151 if (line_arg == NULL)
19152 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 break;
19154 }
19155
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019156 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 * at "end". */
19158 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19159 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019160 else if (STRNCMP(p, "if", 2) == 0
19161 || STRNCMP(p, "wh", 2) == 0
19162 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 || STRNCMP(p, "try", 3) == 0)
19164 indent += 2;
19165
19166 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019167 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019169 if (*p == '!')
19170 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171 p += eval_fname_script(p);
19172 if (ASCII_ISALPHA(*p))
19173 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019174 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 if (*skipwhite(p) == '(')
19176 {
19177 ++nesting;
19178 indent += 2;
19179 }
19180 }
19181 }
19182
19183 /* Check for ":append" or ":insert". */
19184 p = skip_range(p, NULL);
19185 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19186 || (p[0] == 'i'
19187 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19188 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19189 skip_until = vim_strsave((char_u *)".");
19190
19191 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19192 arg = skipwhite(skiptowhite(p));
19193 if (arg[0] == '<' && arg[1] =='<'
19194 && ((p[0] == 'p' && p[1] == 'y'
19195 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19196 || (p[0] == 'p' && p[1] == 'e'
19197 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19198 || (p[0] == 't' && p[1] == 'c'
19199 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19200 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19201 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019202 || (p[0] == 'm' && p[1] == 'z'
19203 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 ))
19205 {
19206 /* ":python <<" continues until a dot, like ":append" */
19207 p = skipwhite(arg + 2);
19208 if (*p == NUL)
19209 skip_until = vim_strsave((char_u *)".");
19210 else
19211 skip_until = vim_strsave(p);
19212 }
19213 }
19214
19215 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019216 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019217 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019218 if (line_arg == NULL)
19219 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019221 }
19222
19223 /* Copy the line to newly allocated memory. get_one_sourceline()
19224 * allocates 250 bytes per line, this saves 80% on average. The cost
19225 * is an extra alloc/free. */
19226 p = vim_strsave(theline);
19227 if (p != NULL)
19228 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019229 if (line_arg == NULL)
19230 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019231 theline = p;
19232 }
19233
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019234 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19235
19236 /* Add NULL lines for continuation lines, so that the line count is
19237 * equal to the index in the growarray. */
19238 while (sourcing_lnum_off-- > 0)
19239 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019240
19241 /* Check for end of eap->arg. */
19242 if (line_arg != NULL && *line_arg == NUL)
19243 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 }
19245
19246 /* Don't define the function when skipping commands or when an error was
19247 * detected. */
19248 if (eap->skip || did_emsg)
19249 goto erret;
19250
19251 /*
19252 * If there are no errors, add the function
19253 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019254 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019255 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019256 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019257 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019258 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019259 emsg_funcname("E707: Function name conflicts with variable: %s",
19260 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019261 goto erret;
19262 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019263
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019264 fp = find_func(name);
19265 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019267 if (!eap->forceit)
19268 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019269 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019270 goto erret;
19271 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019272 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019273 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019274 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019275 name);
19276 goto erret;
19277 }
19278 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019279 ga_clear_strings(&(fp->uf_args));
19280 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019281 vim_free(name);
19282 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284 }
19285 else
19286 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019287 char numbuf[20];
19288
19289 fp = NULL;
19290 if (fudi.fd_newkey == NULL && !eap->forceit)
19291 {
19292 EMSG(_(e_funcdict));
19293 goto erret;
19294 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019295 if (fudi.fd_di == NULL)
19296 {
19297 /* Can't add a function to a locked dictionary */
19298 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19299 goto erret;
19300 }
19301 /* Can't change an existing function if it is locked */
19302 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19303 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019304
19305 /* Give the function a sequential number. Can only be used with a
19306 * Funcref! */
19307 vim_free(name);
19308 sprintf(numbuf, "%d", ++func_nr);
19309 name = vim_strsave((char_u *)numbuf);
19310 if (name == NULL)
19311 goto erret;
19312 }
19313
19314 if (fp == NULL)
19315 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019316 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019317 {
19318 int slen, plen;
19319 char_u *scriptname;
19320
19321 /* Check that the autoload name matches the script name. */
19322 j = FAIL;
19323 if (sourcing_name != NULL)
19324 {
19325 scriptname = autoload_name(name);
19326 if (scriptname != NULL)
19327 {
19328 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019329 plen = (int)STRLEN(p);
19330 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019331 if (slen > plen && fnamecmp(p,
19332 sourcing_name + slen - plen) == 0)
19333 j = OK;
19334 vim_free(scriptname);
19335 }
19336 }
19337 if (j == FAIL)
19338 {
19339 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19340 goto erret;
19341 }
19342 }
19343
19344 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 if (fp == NULL)
19346 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019347
19348 if (fudi.fd_dict != NULL)
19349 {
19350 if (fudi.fd_di == NULL)
19351 {
19352 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019353 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019354 if (fudi.fd_di == NULL)
19355 {
19356 vim_free(fp);
19357 goto erret;
19358 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019359 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19360 {
19361 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019362 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019363 goto erret;
19364 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019365 }
19366 else
19367 /* overwrite existing dict entry */
19368 clear_tv(&fudi.fd_di->di_tv);
19369 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019370 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019371 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019372 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019373
19374 /* behave like "dict" was used */
19375 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019376 }
19377
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019379 STRCPY(fp->uf_name, name);
19380 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019382 fp->uf_args = newargs;
19383 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019384#ifdef FEAT_PROFILE
19385 fp->uf_tml_count = NULL;
19386 fp->uf_tml_total = NULL;
19387 fp->uf_tml_self = NULL;
19388 fp->uf_profiling = FALSE;
19389 if (prof_def_func())
19390 func_do_profile(fp);
19391#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019392 fp->uf_varargs = varargs;
19393 fp->uf_flags = flags;
19394 fp->uf_calls = 0;
19395 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019396 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397
19398erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 ga_clear_strings(&newargs);
19400 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019401ret_free:
19402 vim_free(skip_until);
19403 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406}
19407
19408/*
19409 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019410 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019412 * flags:
19413 * TFN_INT: internal function name OK
19414 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 * Advances "pp" to just after the function name (if no error).
19416 */
19417 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019418trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 char_u **pp;
19420 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019421 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019422 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019424 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 char_u *start;
19426 char_u *end;
19427 int lead;
19428 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019430 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019431
19432 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019433 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019435
19436 /* Check for hard coded <SNR>: already translated function ID (from a user
19437 * command). */
19438 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19439 && (*pp)[2] == (int)KE_SNR)
19440 {
19441 *pp += 3;
19442 len = get_id_len(pp) + 3;
19443 return vim_strnsave(start, len);
19444 }
19445
19446 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19447 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019449 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019451
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019452 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19453 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019454 if (end == start)
19455 {
19456 if (!skip)
19457 EMSG(_("E129: Function name required"));
19458 goto theend;
19459 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019460 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019461 {
19462 /*
19463 * Report an invalid expression in braces, unless the expression
19464 * evaluation has been cancelled due to an aborting error, an
19465 * interrupt, or an exception.
19466 */
19467 if (!aborting())
19468 {
19469 if (end != NULL)
19470 EMSG2(_(e_invarg2), start);
19471 }
19472 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019473 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019474 goto theend;
19475 }
19476
19477 if (lv.ll_tv != NULL)
19478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019479 if (fdp != NULL)
19480 {
19481 fdp->fd_dict = lv.ll_dict;
19482 fdp->fd_newkey = lv.ll_newkey;
19483 lv.ll_newkey = NULL;
19484 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019485 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019486 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19487 {
19488 name = vim_strsave(lv.ll_tv->vval.v_string);
19489 *pp = end;
19490 }
19491 else
19492 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019493 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19494 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019495 EMSG(_(e_funcref));
19496 else
19497 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019498 name = NULL;
19499 }
19500 goto theend;
19501 }
19502
19503 if (lv.ll_name == NULL)
19504 {
19505 /* Error found, but continue after the function name. */
19506 *pp = end;
19507 goto theend;
19508 }
19509
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019510 /* Check if the name is a Funcref. If so, use the value. */
19511 if (lv.ll_exp_name != NULL)
19512 {
19513 len = (int)STRLEN(lv.ll_exp_name);
19514 name = deref_func_name(lv.ll_exp_name, &len);
19515 if (name == lv.ll_exp_name)
19516 name = NULL;
19517 }
19518 else
19519 {
19520 len = (int)(end - *pp);
19521 name = deref_func_name(*pp, &len);
19522 if (name == *pp)
19523 name = NULL;
19524 }
19525 if (name != NULL)
19526 {
19527 name = vim_strsave(name);
19528 *pp = end;
19529 goto theend;
19530 }
19531
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019532 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019533 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019534 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019535 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19536 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19537 {
19538 /* When there was "s:" already or the name expanded to get a
19539 * leading "s:" then remove it. */
19540 lv.ll_name += 2;
19541 len -= 2;
19542 lead = 2;
19543 }
19544 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019545 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019546 {
19547 if (lead == 2) /* skip over "s:" */
19548 lv.ll_name += 2;
19549 len = (int)(end - lv.ll_name);
19550 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019551
19552 /*
19553 * Copy the function name to allocated memory.
19554 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19555 * Accept <SNR>123_name() outside a script.
19556 */
19557 if (skip)
19558 lead = 0; /* do nothing */
19559 else if (lead > 0)
19560 {
19561 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019562 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19563 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019564 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019565 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019566 if (current_SID <= 0)
19567 {
19568 EMSG(_(e_usingsid));
19569 goto theend;
19570 }
19571 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19572 lead += (int)STRLEN(sid_buf);
19573 }
19574 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019575 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019576 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019577 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019578 goto theend;
19579 }
19580 name = alloc((unsigned)(len + lead + 1));
19581 if (name != NULL)
19582 {
19583 if (lead > 0)
19584 {
19585 name[0] = K_SPECIAL;
19586 name[1] = KS_EXTRA;
19587 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019588 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019589 STRCPY(name + 3, sid_buf);
19590 }
19591 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19592 name[len + lead] = NUL;
19593 }
19594 *pp = end;
19595
19596theend:
19597 clear_lval(&lv);
19598 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599}
19600
19601/*
19602 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19603 * Return 2 if "p" starts with "s:".
19604 * Return 0 otherwise.
19605 */
19606 static int
19607eval_fname_script(p)
19608 char_u *p;
19609{
19610 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19611 || STRNICMP(p + 1, "SNR>", 4) == 0))
19612 return 5;
19613 if (p[0] == 's' && p[1] == ':')
19614 return 2;
19615 return 0;
19616}
19617
19618/*
19619 * Return TRUE if "p" starts with "<SID>" or "s:".
19620 * Only works if eval_fname_script() returned non-zero for "p"!
19621 */
19622 static int
19623eval_fname_sid(p)
19624 char_u *p;
19625{
19626 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19627}
19628
19629/*
19630 * List the head of the function: "name(arg1, arg2)".
19631 */
19632 static void
19633list_func_head(fp, indent)
19634 ufunc_T *fp;
19635 int indent;
19636{
19637 int j;
19638
19639 msg_start();
19640 if (indent)
19641 MSG_PUTS(" ");
19642 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019643 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 {
19645 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019646 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 }
19648 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019649 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019651 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 {
19653 if (j)
19654 MSG_PUTS(", ");
19655 msg_puts(FUNCARG(fp, j));
19656 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019657 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 {
19659 if (j)
19660 MSG_PUTS(", ");
19661 MSG_PUTS("...");
19662 }
19663 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019664 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019665 if (p_verbose > 0)
19666 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667}
19668
19669/*
19670 * Find a function by name, return pointer to it in ufuncs.
19671 * Return NULL for unknown function.
19672 */
19673 static ufunc_T *
19674find_func(name)
19675 char_u *name;
19676{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019677 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019679 hi = hash_find(&func_hashtab, name);
19680 if (!HASHITEM_EMPTY(hi))
19681 return HI2UF(hi);
19682 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683}
19684
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019685#if defined(EXITFREE) || defined(PROTO)
19686 void
19687free_all_functions()
19688{
19689 hashitem_T *hi;
19690
19691 /* Need to start all over every time, because func_free() may change the
19692 * hash table. */
19693 while (func_hashtab.ht_used > 0)
19694 for (hi = func_hashtab.ht_array; ; ++hi)
19695 if (!HASHITEM_EMPTY(hi))
19696 {
19697 func_free(HI2UF(hi));
19698 break;
19699 }
19700}
19701#endif
19702
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019703/*
19704 * Return TRUE if a function "name" exists.
19705 */
19706 static int
19707function_exists(name)
19708 char_u *name;
19709{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019710 char_u *nm = name;
19711 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019712 int n = FALSE;
19713
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019714 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019715 nm = skipwhite(nm);
19716
19717 /* Only accept "funcname", "funcname ", "funcname (..." and
19718 * "funcname(...", not "funcname!...". */
19719 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019720 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019721 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019722 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019723 else
19724 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019725 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019726 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019727 return n;
19728}
19729
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019730/*
19731 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019732 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019733 */
19734 static int
19735builtin_function(name)
19736 char_u *name;
19737{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019738 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19739 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019740}
19741
Bram Moolenaar05159a02005-02-26 23:04:13 +000019742#if defined(FEAT_PROFILE) || defined(PROTO)
19743/*
19744 * Start profiling function "fp".
19745 */
19746 static void
19747func_do_profile(fp)
19748 ufunc_T *fp;
19749{
19750 fp->uf_tm_count = 0;
19751 profile_zero(&fp->uf_tm_self);
19752 profile_zero(&fp->uf_tm_total);
19753 if (fp->uf_tml_count == NULL)
19754 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19755 (sizeof(int) * fp->uf_lines.ga_len));
19756 if (fp->uf_tml_total == NULL)
19757 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19758 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19759 if (fp->uf_tml_self == NULL)
19760 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19761 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19762 fp->uf_tml_idx = -1;
19763 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19764 || fp->uf_tml_self == NULL)
19765 return; /* out of memory */
19766
19767 fp->uf_profiling = TRUE;
19768}
19769
19770/*
19771 * Dump the profiling results for all functions in file "fd".
19772 */
19773 void
19774func_dump_profile(fd)
19775 FILE *fd;
19776{
19777 hashitem_T *hi;
19778 int todo;
19779 ufunc_T *fp;
19780 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019781 ufunc_T **sorttab;
19782 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019783
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019784 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019785 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19786
Bram Moolenaar05159a02005-02-26 23:04:13 +000019787 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19788 {
19789 if (!HASHITEM_EMPTY(hi))
19790 {
19791 --todo;
19792 fp = HI2UF(hi);
19793 if (fp->uf_profiling)
19794 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019795 if (sorttab != NULL)
19796 sorttab[st_len++] = fp;
19797
Bram Moolenaar05159a02005-02-26 23:04:13 +000019798 if (fp->uf_name[0] == K_SPECIAL)
19799 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19800 else
19801 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19802 if (fp->uf_tm_count == 1)
19803 fprintf(fd, "Called 1 time\n");
19804 else
19805 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19806 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19807 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19808 fprintf(fd, "\n");
19809 fprintf(fd, "count total (s) self (s)\n");
19810
19811 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19812 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019813 if (FUNCLINE(fp, i) == NULL)
19814 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019815 prof_func_line(fd, fp->uf_tml_count[i],
19816 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019817 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19818 }
19819 fprintf(fd, "\n");
19820 }
19821 }
19822 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019823
19824 if (sorttab != NULL && st_len > 0)
19825 {
19826 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19827 prof_total_cmp);
19828 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19829 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19830 prof_self_cmp);
19831 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19832 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019833}
Bram Moolenaar73830342005-02-28 22:48:19 +000019834
19835 static void
19836prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19837 FILE *fd;
19838 ufunc_T **sorttab;
19839 int st_len;
19840 char *title;
19841 int prefer_self; /* when equal print only self time */
19842{
19843 int i;
19844 ufunc_T *fp;
19845
19846 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19847 fprintf(fd, "count total (s) self (s) function\n");
19848 for (i = 0; i < 20 && i < st_len; ++i)
19849 {
19850 fp = sorttab[i];
19851 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19852 prefer_self);
19853 if (fp->uf_name[0] == K_SPECIAL)
19854 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19855 else
19856 fprintf(fd, " %s()\n", fp->uf_name);
19857 }
19858 fprintf(fd, "\n");
19859}
19860
19861/*
19862 * Print the count and times for one function or function line.
19863 */
19864 static void
19865prof_func_line(fd, count, total, self, prefer_self)
19866 FILE *fd;
19867 int count;
19868 proftime_T *total;
19869 proftime_T *self;
19870 int prefer_self; /* when equal print only self time */
19871{
19872 if (count > 0)
19873 {
19874 fprintf(fd, "%5d ", count);
19875 if (prefer_self && profile_equal(total, self))
19876 fprintf(fd, " ");
19877 else
19878 fprintf(fd, "%s ", profile_msg(total));
19879 if (!prefer_self && profile_equal(total, self))
19880 fprintf(fd, " ");
19881 else
19882 fprintf(fd, "%s ", profile_msg(self));
19883 }
19884 else
19885 fprintf(fd, " ");
19886}
19887
19888/*
19889 * Compare function for total time sorting.
19890 */
19891 static int
19892#ifdef __BORLANDC__
19893_RTLENTRYF
19894#endif
19895prof_total_cmp(s1, s2)
19896 const void *s1;
19897 const void *s2;
19898{
19899 ufunc_T *p1, *p2;
19900
19901 p1 = *(ufunc_T **)s1;
19902 p2 = *(ufunc_T **)s2;
19903 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19904}
19905
19906/*
19907 * Compare function for self time sorting.
19908 */
19909 static int
19910#ifdef __BORLANDC__
19911_RTLENTRYF
19912#endif
19913prof_self_cmp(s1, s2)
19914 const void *s1;
19915 const void *s2;
19916{
19917 ufunc_T *p1, *p2;
19918
19919 p1 = *(ufunc_T **)s1;
19920 p2 = *(ufunc_T **)s2;
19921 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19922}
19923
Bram Moolenaar05159a02005-02-26 23:04:13 +000019924#endif
19925
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019926/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019927 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019928 * Return TRUE if a package was loaded.
19929 */
19930 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019931script_autoload(name, reload)
19932 char_u *name;
19933 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019934{
19935 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019936 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019937 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019938 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019939
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019940 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019941 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019942 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019943 return FALSE;
19944
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019945 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019946
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019947 /* Find the name in the list of previously loaded package names. Skip
19948 * "autoload/", it's always the same. */
19949 for (i = 0; i < ga_loaded.ga_len; ++i)
19950 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19951 break;
19952 if (!reload && i < ga_loaded.ga_len)
19953 ret = FALSE; /* was loaded already */
19954 else
19955 {
19956 /* Remember the name if it wasn't loaded already. */
19957 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19958 {
19959 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19960 tofree = NULL;
19961 }
19962
19963 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019964 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019965 ret = TRUE;
19966 }
19967
19968 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019969 return ret;
19970}
19971
19972/*
19973 * Return the autoload script name for a function or variable name.
19974 * Returns NULL when out of memory.
19975 */
19976 static char_u *
19977autoload_name(name)
19978 char_u *name;
19979{
19980 char_u *p;
19981 char_u *scriptname;
19982
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019983 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019984 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19985 if (scriptname == NULL)
19986 return FALSE;
19987 STRCPY(scriptname, "autoload/");
19988 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019989 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019990 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019991 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019992 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019993 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019994}
19995
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19997
19998/*
19999 * Function given to ExpandGeneric() to obtain the list of user defined
20000 * function names.
20001 */
20002 char_u *
20003get_user_func_name(xp, idx)
20004 expand_T *xp;
20005 int idx;
20006{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020007 static long_u done;
20008 static hashitem_T *hi;
20009 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010
20011 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020013 done = 0;
20014 hi = func_hashtab.ht_array;
20015 }
20016 if (done < func_hashtab.ht_used)
20017 {
20018 if (done++ > 0)
20019 ++hi;
20020 while (HASHITEM_EMPTY(hi))
20021 ++hi;
20022 fp = HI2UF(hi);
20023
20024 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20025 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026
20027 cat_func_name(IObuff, fp);
20028 if (xp->xp_context != EXPAND_USER_FUNC)
20029 {
20030 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020031 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 STRCAT(IObuff, ")");
20033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 return IObuff;
20035 }
20036 return NULL;
20037}
20038
20039#endif /* FEAT_CMDL_COMPL */
20040
20041/*
20042 * Copy the function name of "fp" to buffer "buf".
20043 * "buf" must be able to hold the function name plus three bytes.
20044 * Takes care of script-local function names.
20045 */
20046 static void
20047cat_func_name(buf, fp)
20048 char_u *buf;
20049 ufunc_T *fp;
20050{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020051 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 {
20053 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020054 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 }
20056 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020057 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058}
20059
20060/*
20061 * ":delfunction {name}"
20062 */
20063 void
20064ex_delfunction(eap)
20065 exarg_T *eap;
20066{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020067 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 char_u *p;
20069 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020070 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071
20072 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020073 name = trans_function_name(&p, eap->skip, 0, &fudi);
20074 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020076 {
20077 if (fudi.fd_dict != NULL && !eap->skip)
20078 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 if (!ends_excmd(*skipwhite(p)))
20082 {
20083 vim_free(name);
20084 EMSG(_(e_trailing));
20085 return;
20086 }
20087 eap->nextcmd = check_nextcmd(p);
20088 if (eap->nextcmd != NULL)
20089 *p = NUL;
20090
20091 if (!eap->skip)
20092 fp = find_func(name);
20093 vim_free(name);
20094
20095 if (!eap->skip)
20096 {
20097 if (fp == NULL)
20098 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020099 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 return;
20101 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020102 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 {
20104 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20105 return;
20106 }
20107
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020108 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020110 /* Delete the dict item that refers to the function, it will
20111 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020112 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020114 else
20115 func_free(fp);
20116 }
20117}
20118
20119/*
20120 * Free a function and remove it from the list of functions.
20121 */
20122 static void
20123func_free(fp)
20124 ufunc_T *fp;
20125{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020126 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020127
20128 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020129 ga_clear_strings(&(fp->uf_args));
20130 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020131#ifdef FEAT_PROFILE
20132 vim_free(fp->uf_tml_count);
20133 vim_free(fp->uf_tml_total);
20134 vim_free(fp->uf_tml_self);
20135#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020137 /* remove the function from the function hashtable */
20138 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20139 if (HASHITEM_EMPTY(hi))
20140 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020141 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020142 hash_remove(&func_hashtab, hi);
20143
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020144 vim_free(fp);
20145}
20146
20147/*
20148 * Unreference a Function: decrement the reference count and free it when it
20149 * becomes zero. Only for numbered functions.
20150 */
20151 static void
20152func_unref(name)
20153 char_u *name;
20154{
20155 ufunc_T *fp;
20156
20157 if (name != NULL && isdigit(*name))
20158 {
20159 fp = find_func(name);
20160 if (fp == NULL)
20161 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020162 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020163 {
20164 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020165 * when "uf_calls" becomes zero. */
20166 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020167 func_free(fp);
20168 }
20169 }
20170}
20171
20172/*
20173 * Count a reference to a Function.
20174 */
20175 static void
20176func_ref(name)
20177 char_u *name;
20178{
20179 ufunc_T *fp;
20180
20181 if (name != NULL && isdigit(*name))
20182 {
20183 fp = find_func(name);
20184 if (fp == NULL)
20185 EMSG2(_(e_intern2), "func_ref()");
20186 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020187 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 }
20189}
20190
20191/*
20192 * Call a user function.
20193 */
20194 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020195call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 ufunc_T *fp; /* pointer to function */
20197 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020198 typval_T *argvars; /* arguments */
20199 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 linenr_T firstline; /* first line of range */
20201 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020202 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203{
Bram Moolenaar33570922005-01-25 22:26:29 +000020204 char_u *save_sourcing_name;
20205 linenr_T save_sourcing_lnum;
20206 scid_T save_current_SID;
20207 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020208 int save_did_emsg;
20209 static int depth = 0;
20210 dictitem_T *v;
20211 int fixvar_idx = 0; /* index in fixvar[] */
20212 int i;
20213 int ai;
20214 char_u numbuf[NUMBUFLEN];
20215 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020216#ifdef FEAT_PROFILE
20217 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020218 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220
20221 /* If depth of calling is getting too high, don't execute the function */
20222 if (depth >= p_mfd)
20223 {
20224 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020225 rettv->v_type = VAR_NUMBER;
20226 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 return;
20228 }
20229 ++depth;
20230
20231 line_breakcheck(); /* check for CTRL-C hit */
20232
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020233 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020234 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020236 fc.rettv = rettv;
20237 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 fc.linenr = 0;
20239 fc.returned = FALSE;
20240 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020242 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 fc.dbg_tick = debug_tick;
20244
Bram Moolenaar33570922005-01-25 22:26:29 +000020245 /*
20246 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20247 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20248 * each argument variable and saves a lot of time.
20249 */
20250 /*
20251 * Init l: variables.
20252 */
20253 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020254 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020255 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020256 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20257 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020258 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020259 name = v->di_key;
20260 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020261 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20262 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20263 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020264 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 v->di_tv.vval.v_dict = selfdict;
20266 ++selfdict->dv_refcount;
20267 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020268
Bram Moolenaar33570922005-01-25 22:26:29 +000020269 /*
20270 * Init a: variables.
20271 * Set a:0 to "argcount".
20272 * Set a:000 to a list with room for the "..." arguments.
20273 */
20274 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20275 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020276 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020277 v = &fc.fixvar[fixvar_idx++].var;
20278 STRCPY(v->di_key, "000");
20279 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20280 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20281 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020282 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020283 v->di_tv.vval.v_list = &fc.l_varlist;
20284 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20285 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020286 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020287
20288 /*
20289 * Set a:firstline to "firstline" and a:lastline to "lastline".
20290 * Set a:name to named arguments.
20291 * Set a:N to the "..." arguments.
20292 */
20293 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20294 (varnumber_T)firstline);
20295 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20296 (varnumber_T)lastline);
20297 for (i = 0; i < argcount; ++i)
20298 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020299 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 if (ai < 0)
20301 /* named argument a:name */
20302 name = FUNCARG(fp, i);
20303 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020304 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020305 /* "..." argument a:1, a:2, etc. */
20306 sprintf((char *)numbuf, "%d", ai + 1);
20307 name = numbuf;
20308 }
20309 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20310 {
20311 v = &fc.fixvar[fixvar_idx++].var;
20312 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20313 }
20314 else
20315 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020316 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20317 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020318 if (v == NULL)
20319 break;
20320 v->di_flags = DI_FLAGS_RO;
20321 }
20322 STRCPY(v->di_key, name);
20323 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20324
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020325 /* Note: the values are copied directly to avoid alloc/free.
20326 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020327 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020328 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020329
20330 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20331 {
20332 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20333 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020334 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020335 }
20336 }
20337
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 /* Don't redraw while executing the function. */
20339 ++RedrawingDisabled;
20340 save_sourcing_name = sourcing_name;
20341 save_sourcing_lnum = sourcing_lnum;
20342 sourcing_lnum = 1;
20343 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020344 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 if (sourcing_name != NULL)
20346 {
20347 if (save_sourcing_name != NULL
20348 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20349 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20350 else
20351 STRCPY(sourcing_name, "function ");
20352 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20353
20354 if (p_verbose >= 12)
20355 {
20356 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020357 verbose_enter_scroll();
20358
Bram Moolenaar555b2802005-05-19 21:08:39 +000020359 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 if (p_verbose >= 14)
20361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020363 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020364 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020365 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366
20367 msg_puts((char_u *)"(");
20368 for (i = 0; i < argcount; ++i)
20369 {
20370 if (i > 0)
20371 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020372 if (argvars[i].v_type == VAR_NUMBER)
20373 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 else
20375 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020376 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20377 if (s != NULL)
20378 {
20379 trunc_string(s, buf, MSG_BUF_CLEN);
20380 msg_puts(buf);
20381 vim_free(tofree);
20382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 }
20384 }
20385 msg_puts((char_u *)")");
20386 }
20387 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020388
20389 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 --no_wait_return;
20391 }
20392 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020393#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020394 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020395 {
20396 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20397 func_do_profile(fp);
20398 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020399 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020400 {
20401 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020402 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020403 profile_zero(&fp->uf_tm_children);
20404 }
20405 script_prof_save(&wait_start);
20406 }
20407#endif
20408
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020410 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 save_did_emsg = did_emsg;
20412 did_emsg = FALSE;
20413
20414 /* call do_cmdline() to execute the lines */
20415 do_cmdline(NULL, get_func_line, (void *)&fc,
20416 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20417
20418 --RedrawingDisabled;
20419
20420 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020421 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020423 clear_tv(rettv);
20424 rettv->v_type = VAR_NUMBER;
20425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 }
20427
Bram Moolenaar05159a02005-02-26 23:04:13 +000020428#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020429 if (do_profiling == PROF_YES && (fp->uf_profiling
20430 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020431 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020432 profile_end(&call_start);
20433 profile_sub_wait(&wait_start, &call_start);
20434 profile_add(&fp->uf_tm_total, &call_start);
20435 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020436 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020437 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020438 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20439 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020440 }
20441 }
20442#endif
20443
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 /* when being verbose, mention the return value */
20445 if (p_verbose >= 12)
20446 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020448 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020451 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020452 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020453 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20454 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020457 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020458 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020459 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020460 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020461
Bram Moolenaar555b2802005-05-19 21:08:39 +000020462 /* The value may be very long. Skip the middle part, so that we
20463 * have some idea how it starts and ends. smsg() would always
20464 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020465 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20466 if (s != NULL)
20467 {
20468 trunc_string(s, buf, MSG_BUF_CLEN);
20469 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20470 vim_free(tofree);
20471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 }
20473 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020474
20475 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 --no_wait_return;
20477 }
20478
20479 vim_free(sourcing_name);
20480 sourcing_name = save_sourcing_name;
20481 sourcing_lnum = save_sourcing_lnum;
20482 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020483#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020484 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020485 script_prof_restore(&wait_start);
20486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487
20488 if (p_verbose >= 12 && sourcing_name != NULL)
20489 {
20490 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020491 verbose_enter_scroll();
20492
Bram Moolenaar555b2802005-05-19 21:08:39 +000020493 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020495
20496 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 --no_wait_return;
20498 }
20499
20500 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020501 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502
Bram Moolenaar33570922005-01-25 22:26:29 +000020503 /* The a: variables typevals were not alloced, only free the allocated
20504 * variables. */
20505 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20506
20507 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 --depth;
20509}
20510
20511/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020512 * Add a number variable "name" to dict "dp" with value "nr".
20513 */
20514 static void
20515add_nr_var(dp, v, name, nr)
20516 dict_T *dp;
20517 dictitem_T *v;
20518 char *name;
20519 varnumber_T nr;
20520{
20521 STRCPY(v->di_key, name);
20522 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20523 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20524 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020525 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020526 v->di_tv.vval.v_number = nr;
20527}
20528
20529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 * ":return [expr]"
20531 */
20532 void
20533ex_return(eap)
20534 exarg_T *eap;
20535{
20536 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020537 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 int returning = FALSE;
20539
20540 if (current_funccal == NULL)
20541 {
20542 EMSG(_("E133: :return not inside a function"));
20543 return;
20544 }
20545
20546 if (eap->skip)
20547 ++emsg_skip;
20548
20549 eap->nextcmd = NULL;
20550 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 {
20553 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020554 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020556 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 }
20558 /* It's safer to return also on error. */
20559 else if (!eap->skip)
20560 {
20561 /*
20562 * Return unless the expression evaluation has been cancelled due to an
20563 * aborting error, an interrupt, or an exception.
20564 */
20565 if (!aborting())
20566 returning = do_return(eap, FALSE, TRUE, NULL);
20567 }
20568
20569 /* When skipping or the return gets pending, advance to the next command
20570 * in this line (!returning). Otherwise, ignore the rest of the line.
20571 * Following lines will be ignored by get_func_line(). */
20572 if (returning)
20573 eap->nextcmd = NULL;
20574 else if (eap->nextcmd == NULL) /* no argument */
20575 eap->nextcmd = check_nextcmd(arg);
20576
20577 if (eap->skip)
20578 --emsg_skip;
20579}
20580
20581/*
20582 * Return from a function. Possibly makes the return pending. Also called
20583 * for a pending return at the ":endtry" or after returning from an extra
20584 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020585 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020586 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 * FALSE when the return gets pending.
20588 */
20589 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020590do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 exarg_T *eap;
20592 int reanimate;
20593 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020594 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595{
20596 int idx;
20597 struct condstack *cstack = eap->cstack;
20598
20599 if (reanimate)
20600 /* Undo the return. */
20601 current_funccal->returned = FALSE;
20602
20603 /*
20604 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20605 * not in its finally clause (which then is to be executed next) is found.
20606 * In this case, make the ":return" pending for execution at the ":endtry".
20607 * Otherwise, return normally.
20608 */
20609 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20610 if (idx >= 0)
20611 {
20612 cstack->cs_pending[idx] = CSTP_RETURN;
20613
20614 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020615 /* A pending return again gets pending. "rettv" points to an
20616 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020618 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 else
20620 {
20621 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020622 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020624 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020626 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 {
20628 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020629 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020630 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 else
20632 EMSG(_(e_outofmem));
20633 }
20634 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020635 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636
20637 if (reanimate)
20638 {
20639 /* The pending return value could be overwritten by a ":return"
20640 * without argument in a finally clause; reset the default
20641 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020642 current_funccal->rettv->v_type = VAR_NUMBER;
20643 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 }
20645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020646 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 }
20648 else
20649 {
20650 current_funccal->returned = TRUE;
20651
20652 /* If the return is carried out now, store the return value. For
20653 * a return immediately after reanimation, the value is already
20654 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020655 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020657 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020658 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020660 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 }
20662 }
20663
20664 return idx < 0;
20665}
20666
20667/*
20668 * Free the variable with a pending return value.
20669 */
20670 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020671discard_pending_return(rettv)
20672 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673{
Bram Moolenaar33570922005-01-25 22:26:29 +000020674 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675}
20676
20677/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020678 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 * is an allocated string. Used by report_pending() for verbose messages.
20680 */
20681 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020682get_return_cmd(rettv)
20683 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020685 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020686 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020687 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020689 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020690 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020691 if (s == NULL)
20692 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020693
20694 STRCPY(IObuff, ":return ");
20695 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20696 if (STRLEN(s) + 8 >= IOSIZE)
20697 STRCPY(IObuff + IOSIZE - 4, "...");
20698 vim_free(tofree);
20699 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700}
20701
20702/*
20703 * Get next function line.
20704 * Called by do_cmdline() to get the next line.
20705 * Returns allocated string, or NULL for end of function.
20706 */
20707/* ARGSUSED */
20708 char_u *
20709get_func_line(c, cookie, indent)
20710 int c; /* not used */
20711 void *cookie;
20712 int indent; /* not used */
20713{
Bram Moolenaar33570922005-01-25 22:26:29 +000020714 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020715 ufunc_T *fp = fcp->func;
20716 char_u *retval;
20717 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718
20719 /* If breakpoints have been added/deleted need to check for it. */
20720 if (fcp->dbg_tick != debug_tick)
20721 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020722 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 sourcing_lnum);
20724 fcp->dbg_tick = debug_tick;
20725 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020726#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020727 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020728 func_line_end(cookie);
20729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730
Bram Moolenaar05159a02005-02-26 23:04:13 +000020731 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020732 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20733 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 retval = NULL;
20735 else
20736 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020737 /* Skip NULL lines (continuation lines). */
20738 while (fcp->linenr < gap->ga_len
20739 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20740 ++fcp->linenr;
20741 if (fcp->linenr >= gap->ga_len)
20742 retval = NULL;
20743 else
20744 {
20745 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20746 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020747#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020748 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020749 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020750#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 }
20753
20754 /* Did we encounter a breakpoint? */
20755 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20756 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020757 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020759 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 sourcing_lnum);
20761 fcp->dbg_tick = debug_tick;
20762 }
20763
20764 return retval;
20765}
20766
Bram Moolenaar05159a02005-02-26 23:04:13 +000020767#if defined(FEAT_PROFILE) || defined(PROTO)
20768/*
20769 * Called when starting to read a function line.
20770 * "sourcing_lnum" must be correct!
20771 * When skipping lines it may not actually be executed, but we won't find out
20772 * until later and we need to store the time now.
20773 */
20774 void
20775func_line_start(cookie)
20776 void *cookie;
20777{
20778 funccall_T *fcp = (funccall_T *)cookie;
20779 ufunc_T *fp = fcp->func;
20780
20781 if (fp->uf_profiling && sourcing_lnum >= 1
20782 && sourcing_lnum <= fp->uf_lines.ga_len)
20783 {
20784 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020785 /* Skip continuation lines. */
20786 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20787 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020788 fp->uf_tml_execed = FALSE;
20789 profile_start(&fp->uf_tml_start);
20790 profile_zero(&fp->uf_tml_children);
20791 profile_get_wait(&fp->uf_tml_wait);
20792 }
20793}
20794
20795/*
20796 * Called when actually executing a function line.
20797 */
20798 void
20799func_line_exec(cookie)
20800 void *cookie;
20801{
20802 funccall_T *fcp = (funccall_T *)cookie;
20803 ufunc_T *fp = fcp->func;
20804
20805 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20806 fp->uf_tml_execed = TRUE;
20807}
20808
20809/*
20810 * Called when done with a function line.
20811 */
20812 void
20813func_line_end(cookie)
20814 void *cookie;
20815{
20816 funccall_T *fcp = (funccall_T *)cookie;
20817 ufunc_T *fp = fcp->func;
20818
20819 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20820 {
20821 if (fp->uf_tml_execed)
20822 {
20823 ++fp->uf_tml_count[fp->uf_tml_idx];
20824 profile_end(&fp->uf_tml_start);
20825 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020826 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020827 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20828 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020829 }
20830 fp->uf_tml_idx = -1;
20831 }
20832}
20833#endif
20834
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835/*
20836 * Return TRUE if the currently active function should be ended, because a
20837 * return was encountered or an error occured. Used inside a ":while".
20838 */
20839 int
20840func_has_ended(cookie)
20841 void *cookie;
20842{
Bram Moolenaar33570922005-01-25 22:26:29 +000020843 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844
20845 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20846 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020847 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 || fcp->returned);
20849}
20850
20851/*
20852 * return TRUE if cookie indicates a function which "abort"s on errors.
20853 */
20854 int
20855func_has_abort(cookie)
20856 void *cookie;
20857{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020858 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859}
20860
20861#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20862typedef enum
20863{
20864 VAR_FLAVOUR_DEFAULT,
20865 VAR_FLAVOUR_SESSION,
20866 VAR_FLAVOUR_VIMINFO
20867} var_flavour_T;
20868
20869static var_flavour_T var_flavour __ARGS((char_u *varname));
20870
20871 static var_flavour_T
20872var_flavour(varname)
20873 char_u *varname;
20874{
20875 char_u *p = varname;
20876
20877 if (ASCII_ISUPPER(*p))
20878 {
20879 while (*(++p))
20880 if (ASCII_ISLOWER(*p))
20881 return VAR_FLAVOUR_SESSION;
20882 return VAR_FLAVOUR_VIMINFO;
20883 }
20884 else
20885 return VAR_FLAVOUR_DEFAULT;
20886}
20887#endif
20888
20889#if defined(FEAT_VIMINFO) || defined(PROTO)
20890/*
20891 * Restore global vars that start with a capital from the viminfo file
20892 */
20893 int
20894read_viminfo_varlist(virp, writing)
20895 vir_T *virp;
20896 int writing;
20897{
20898 char_u *tab;
20899 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020900 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901
20902 if (!writing && (find_viminfo_parameter('!') != NULL))
20903 {
20904 tab = vim_strchr(virp->vir_line + 1, '\t');
20905 if (tab != NULL)
20906 {
20907 *tab++ = '\0'; /* isolate the variable name */
20908 if (*tab == 'S') /* string var */
20909 is_string = TRUE;
20910
20911 tab = vim_strchr(tab, '\t');
20912 if (tab != NULL)
20913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 if (is_string)
20915 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020916 tv.v_type = VAR_STRING;
20917 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 }
20920 else
20921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020922 tv.v_type = VAR_NUMBER;
20923 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020925 set_var(virp->vir_line + 1, &tv, FALSE);
20926 if (is_string)
20927 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 }
20929 }
20930 }
20931
20932 return viminfo_readline(virp);
20933}
20934
20935/*
20936 * Write global vars that start with a capital to the viminfo file
20937 */
20938 void
20939write_viminfo_varlist(fp)
20940 FILE *fp;
20941{
Bram Moolenaar33570922005-01-25 22:26:29 +000020942 hashitem_T *hi;
20943 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020944 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020945 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020946 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020947 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020948 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949
20950 if (find_viminfo_parameter('!') == NULL)
20951 return;
20952
20953 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020954
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020955 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020956 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020958 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020960 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020961 this_var = HI2DI(hi);
20962 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020963 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020964 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020965 {
20966 case VAR_STRING: s = "STR"; break;
20967 case VAR_NUMBER: s = "NUM"; break;
20968 default: continue;
20969 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020970 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020971 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020972 if (p != NULL)
20973 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020974 vim_free(tofree);
20975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 }
20977 }
20978}
20979#endif
20980
20981#if defined(FEAT_SESSION) || defined(PROTO)
20982 int
20983store_session_globals(fd)
20984 FILE *fd;
20985{
Bram Moolenaar33570922005-01-25 22:26:29 +000020986 hashitem_T *hi;
20987 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020988 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 char_u *p, *t;
20990
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020991 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020992 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020994 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020996 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020997 this_var = HI2DI(hi);
20998 if ((this_var->di_tv.v_type == VAR_NUMBER
20999 || this_var->di_tv.v_type == VAR_STRING)
21000 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021001 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021002 /* Escape special characters with a backslash. Turn a LF and
21003 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021004 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021005 (char_u *)"\\\"\n\r");
21006 if (p == NULL) /* out of memory */
21007 break;
21008 for (t = p; *t != NUL; ++t)
21009 if (*t == '\n')
21010 *t = 'n';
21011 else if (*t == '\r')
21012 *t = 'r';
21013 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021014 this_var->di_key,
21015 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21016 : ' ',
21017 p,
21018 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21019 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021020 || put_eol(fd) == FAIL)
21021 {
21022 vim_free(p);
21023 return FAIL;
21024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 vim_free(p);
21026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 }
21028 }
21029 return OK;
21030}
21031#endif
21032
Bram Moolenaar661b1822005-07-28 22:36:45 +000021033/*
21034 * Display script name where an item was last set.
21035 * Should only be invoked when 'verbose' is non-zero.
21036 */
21037 void
21038last_set_msg(scriptID)
21039 scid_T scriptID;
21040{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021041 char_u *p;
21042
Bram Moolenaar661b1822005-07-28 22:36:45 +000021043 if (scriptID != 0)
21044 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021045 p = home_replace_save(NULL, get_scriptname(scriptID));
21046 if (p != NULL)
21047 {
21048 verbose_enter();
21049 MSG_PUTS(_("\n\tLast set from "));
21050 MSG_PUTS(p);
21051 vim_free(p);
21052 verbose_leave();
21053 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021054 }
21055}
21056
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057#endif /* FEAT_EVAL */
21058
21059#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21060
21061
21062#ifdef WIN3264
21063/*
21064 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21065 */
21066static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21067static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21068static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21069
21070/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021071 * Get the short path (8.3) for the filename in "fnamep".
21072 * Only works for a valid file name.
21073 * When the path gets longer "fnamep" is changed and the allocated buffer
21074 * is put in "bufp".
21075 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
21076 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 */
21078 static int
21079get_short_pathname(fnamep, bufp, fnamelen)
21080 char_u **fnamep;
21081 char_u **bufp;
21082 int *fnamelen;
21083{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021084 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 char_u *newbuf;
21086
21087 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 l = GetShortPathName(*fnamep, *fnamep, len);
21089 if (l > len - 1)
21090 {
21091 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021092 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 newbuf = vim_strnsave(*fnamep, l);
21094 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021095 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096
21097 vim_free(*bufp);
21098 *fnamep = *bufp = newbuf;
21099
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021100 /* Really should always succeed, as the buffer is big enough. */
21101 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 }
21103
21104 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021105 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106}
21107
21108/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021109 * Get the short path (8.3) for the filename in "fname". The converted
21110 * path is returned in "bufp".
21111 *
21112 * Some of the directories specified in "fname" may not exist. This function
21113 * will shorten the existing directories at the beginning of the path and then
21114 * append the remaining non-existing path.
21115 *
21116 * fname - Pointer to the filename to shorten. On return, contains the
21117 * pointer to the shortened pathname
21118 * bufp - Pointer to an allocated buffer for the filename.
21119 * fnamelen - Length of the filename pointed to by fname
21120 *
21121 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 */
21123 static int
21124shortpath_for_invalid_fname(fname, bufp, fnamelen)
21125 char_u **fname;
21126 char_u **bufp;
21127 int *fnamelen;
21128{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021129 char_u *short_fname, *save_fname, *pbuf_unused;
21130 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021132 int old_len, len;
21133 int new_len, sfx_len;
21134 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135
21136 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021137 old_len = *fnamelen;
21138 save_fname = vim_strnsave(*fname, old_len);
21139 pbuf_unused = NULL;
21140 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021142 endp = save_fname + old_len - 1; /* Find the end of the copy */
21143 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021145 /*
21146 * Try shortening the supplied path till it succeeds by removing one
21147 * directory at a time from the tail of the path.
21148 */
21149 len = 0;
21150 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021152 /* go back one path-separator */
21153 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
21154 --endp;
21155 if (endp <= save_fname)
21156 break; /* processed the complete path */
21157
21158 /*
21159 * Replace the path separator with a NUL and try to shorten the
21160 * resulting path.
21161 */
21162 ch = *endp;
21163 *endp = 0;
21164 short_fname = save_fname;
21165 len = STRLEN(short_fname) + 1;
21166 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
21167 {
21168 retval = FAIL;
21169 goto theend;
21170 }
21171 *endp = ch; /* preserve the string */
21172
21173 if (len > 0)
21174 break; /* successfully shortened the path */
21175
21176 /* failed to shorten the path. Skip the path separator */
21177 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 }
21179
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021180 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021182 /*
21183 * Succeeded in shortening the path. Now concatenate the shortened
21184 * path with the remaining path at the tail.
21185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021187 /* Compute the length of the new path. */
21188 sfx_len = (int)(save_endp - endp) + 1;
21189 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021191 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021193 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021195 /* There is not enough space in the currently allocated string,
21196 * copy it to a buffer big enough. */
21197 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021199 {
21200 retval = FAIL;
21201 goto theend;
21202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 }
21204 else
21205 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021206 /* Transfer short_fname to the main buffer (it's big enough),
21207 * unless get_short_pathname() did its work in-place. */
21208 *fname = *bufp = save_fname;
21209 if (short_fname != save_fname)
21210 vim_strncpy(save_fname, short_fname, len);
21211 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021213
21214 /* concat the not-shortened part of the path */
21215 vim_strncpy(*fname + len, endp, sfx_len);
21216 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021218
21219theend:
21220 vim_free(pbuf_unused);
21221 vim_free(save_fname);
21222
21223 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224}
21225
21226/*
21227 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021228 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 */
21230 static int
21231shortpath_for_partial(fnamep, bufp, fnamelen)
21232 char_u **fnamep;
21233 char_u **bufp;
21234 int *fnamelen;
21235{
21236 int sepcount, len, tflen;
21237 char_u *p;
21238 char_u *pbuf, *tfname;
21239 int hasTilde;
21240
21241 /* Count up the path seperators from the RHS.. so we know which part
21242 * of the path to return.
21243 */
21244 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021245 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246 if (vim_ispathsep(*p))
21247 ++sepcount;
21248
21249 /* Need full path first (use expand_env() to remove a "~/") */
21250 hasTilde = (**fnamep == '~');
21251 if (hasTilde)
21252 pbuf = tfname = expand_env_save(*fnamep);
21253 else
21254 pbuf = tfname = FullName_save(*fnamep, FALSE);
21255
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021256 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021258 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
21259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260
21261 if (len == 0)
21262 {
21263 /* Don't have a valid filename, so shorten the rest of the
21264 * path if we can. This CAN give us invalid 8.3 filenames, but
21265 * there's not a lot of point in guessing what it might be.
21266 */
21267 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021268 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
21269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 }
21271
21272 /* Count the paths backward to find the beginning of the desired string. */
21273 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021274 {
21275#ifdef FEAT_MBYTE
21276 if (has_mbyte)
21277 p -= mb_head_off(tfname, p);
21278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 if (vim_ispathsep(*p))
21280 {
21281 if (sepcount == 0 || (hasTilde && sepcount == 1))
21282 break;
21283 else
21284 sepcount --;
21285 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 if (hasTilde)
21288 {
21289 --p;
21290 if (p >= tfname)
21291 *p = '~';
21292 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294 }
21295 else
21296 ++p;
21297
21298 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21299 vim_free(*bufp);
21300 *fnamelen = (int)STRLEN(p);
21301 *bufp = pbuf;
21302 *fnamep = p;
21303
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021304 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305}
21306#endif /* WIN3264 */
21307
21308/*
21309 * Adjust a filename, according to a string of modifiers.
21310 * *fnamep must be NUL terminated when called. When returning, the length is
21311 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021312 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 * When there is an error, *fnamep is set to NULL.
21314 */
21315 int
21316modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21317 char_u *src; /* string with modifiers */
21318 int *usedlen; /* characters after src that are used */
21319 char_u **fnamep; /* file name so far */
21320 char_u **bufp; /* buffer for allocated file name or NULL */
21321 int *fnamelen; /* length of fnamep */
21322{
21323 int valid = 0;
21324 char_u *tail;
21325 char_u *s, *p, *pbuf;
21326 char_u dirname[MAXPATHL];
21327 int c;
21328 int has_fullname = 0;
21329#ifdef WIN3264
21330 int has_shortname = 0;
21331#endif
21332
21333repeat:
21334 /* ":p" - full path/file_name */
21335 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21336 {
21337 has_fullname = 1;
21338
21339 valid |= VALID_PATH;
21340 *usedlen += 2;
21341
21342 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21343 if ((*fnamep)[0] == '~'
21344#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21345 && ((*fnamep)[1] == '/'
21346# ifdef BACKSLASH_IN_FILENAME
21347 || (*fnamep)[1] == '\\'
21348# endif
21349 || (*fnamep)[1] == NUL)
21350
21351#endif
21352 )
21353 {
21354 *fnamep = expand_env_save(*fnamep);
21355 vim_free(*bufp); /* free any allocated file name */
21356 *bufp = *fnamep;
21357 if (*fnamep == NULL)
21358 return -1;
21359 }
21360
21361 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021362 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 {
21364 if (vim_ispathsep(*p)
21365 && p[1] == '.'
21366 && (p[2] == NUL
21367 || vim_ispathsep(p[2])
21368 || (p[2] == '.'
21369 && (p[3] == NUL || vim_ispathsep(p[3])))))
21370 break;
21371 }
21372
21373 /* FullName_save() is slow, don't use it when not needed. */
21374 if (*p != NUL || !vim_isAbsName(*fnamep))
21375 {
21376 *fnamep = FullName_save(*fnamep, *p != NUL);
21377 vim_free(*bufp); /* free any allocated file name */
21378 *bufp = *fnamep;
21379 if (*fnamep == NULL)
21380 return -1;
21381 }
21382
21383 /* Append a path separator to a directory. */
21384 if (mch_isdir(*fnamep))
21385 {
21386 /* Make room for one or two extra characters. */
21387 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21388 vim_free(*bufp); /* free any allocated file name */
21389 *bufp = *fnamep;
21390 if (*fnamep == NULL)
21391 return -1;
21392 add_pathsep(*fnamep);
21393 }
21394 }
21395
21396 /* ":." - path relative to the current directory */
21397 /* ":~" - path relative to the home directory */
21398 /* ":8" - shortname path - postponed till after */
21399 while (src[*usedlen] == ':'
21400 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21401 {
21402 *usedlen += 2;
21403 if (c == '8')
21404 {
21405#ifdef WIN3264
21406 has_shortname = 1; /* Postpone this. */
21407#endif
21408 continue;
21409 }
21410 pbuf = NULL;
21411 /* Need full path first (use expand_env() to remove a "~/") */
21412 if (!has_fullname)
21413 {
21414 if (c == '.' && **fnamep == '~')
21415 p = pbuf = expand_env_save(*fnamep);
21416 else
21417 p = pbuf = FullName_save(*fnamep, FALSE);
21418 }
21419 else
21420 p = *fnamep;
21421
21422 has_fullname = 0;
21423
21424 if (p != NULL)
21425 {
21426 if (c == '.')
21427 {
21428 mch_dirname(dirname, MAXPATHL);
21429 s = shorten_fname(p, dirname);
21430 if (s != NULL)
21431 {
21432 *fnamep = s;
21433 if (pbuf != NULL)
21434 {
21435 vim_free(*bufp); /* free any allocated file name */
21436 *bufp = pbuf;
21437 pbuf = NULL;
21438 }
21439 }
21440 }
21441 else
21442 {
21443 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21444 /* Only replace it when it starts with '~' */
21445 if (*dirname == '~')
21446 {
21447 s = vim_strsave(dirname);
21448 if (s != NULL)
21449 {
21450 *fnamep = s;
21451 vim_free(*bufp);
21452 *bufp = s;
21453 }
21454 }
21455 }
21456 vim_free(pbuf);
21457 }
21458 }
21459
21460 tail = gettail(*fnamep);
21461 *fnamelen = (int)STRLEN(*fnamep);
21462
21463 /* ":h" - head, remove "/file_name", can be repeated */
21464 /* Don't remove the first "/" or "c:\" */
21465 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21466 {
21467 valid |= VALID_HEAD;
21468 *usedlen += 2;
21469 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021470 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021471 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 *fnamelen = (int)(tail - *fnamep);
21473#ifdef VMS
21474 if (*fnamelen > 0)
21475 *fnamelen += 1; /* the path separator is part of the path */
21476#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021477 if (*fnamelen == 0)
21478 {
21479 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21480 p = vim_strsave((char_u *)".");
21481 if (p == NULL)
21482 return -1;
21483 vim_free(*bufp);
21484 *bufp = *fnamep = tail = p;
21485 *fnamelen = 1;
21486 }
21487 else
21488 {
21489 while (tail > s && !after_pathsep(s, tail))
21490 mb_ptr_back(*fnamep, tail);
21491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 }
21493
21494 /* ":8" - shortname */
21495 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21496 {
21497 *usedlen += 2;
21498#ifdef WIN3264
21499 has_shortname = 1;
21500#endif
21501 }
21502
21503#ifdef WIN3264
21504 /* Check shortname after we have done 'heads' and before we do 'tails'
21505 */
21506 if (has_shortname)
21507 {
21508 pbuf = NULL;
21509 /* Copy the string if it is shortened by :h */
21510 if (*fnamelen < (int)STRLEN(*fnamep))
21511 {
21512 p = vim_strnsave(*fnamep, *fnamelen);
21513 if (p == 0)
21514 return -1;
21515 vim_free(*bufp);
21516 *bufp = *fnamep = p;
21517 }
21518
21519 /* Split into two implementations - makes it easier. First is where
21520 * there isn't a full name already, second is where there is.
21521 */
21522 if (!has_fullname && !vim_isAbsName(*fnamep))
21523 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021524 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 return -1;
21526 }
21527 else
21528 {
21529 int l;
21530
21531 /* Simple case, already have the full-name
21532 * Nearly always shorter, so try first time. */
21533 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021534 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 return -1;
21536
21537 if (l == 0)
21538 {
21539 /* Couldn't find the filename.. search the paths.
21540 */
21541 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021542 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 return -1;
21544 }
21545 *fnamelen = l;
21546 }
21547 }
21548#endif /* WIN3264 */
21549
21550 /* ":t" - tail, just the basename */
21551 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21552 {
21553 *usedlen += 2;
21554 *fnamelen -= (int)(tail - *fnamep);
21555 *fnamep = tail;
21556 }
21557
21558 /* ":e" - extension, can be repeated */
21559 /* ":r" - root, without extension, can be repeated */
21560 while (src[*usedlen] == ':'
21561 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21562 {
21563 /* find a '.' in the tail:
21564 * - for second :e: before the current fname
21565 * - otherwise: The last '.'
21566 */
21567 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21568 s = *fnamep - 2;
21569 else
21570 s = *fnamep + *fnamelen - 1;
21571 for ( ; s > tail; --s)
21572 if (s[0] == '.')
21573 break;
21574 if (src[*usedlen + 1] == 'e') /* :e */
21575 {
21576 if (s > tail)
21577 {
21578 *fnamelen += (int)(*fnamep - (s + 1));
21579 *fnamep = s + 1;
21580#ifdef VMS
21581 /* cut version from the extension */
21582 s = *fnamep + *fnamelen - 1;
21583 for ( ; s > *fnamep; --s)
21584 if (s[0] == ';')
21585 break;
21586 if (s > *fnamep)
21587 *fnamelen = s - *fnamep;
21588#endif
21589 }
21590 else if (*fnamep <= tail)
21591 *fnamelen = 0;
21592 }
21593 else /* :r */
21594 {
21595 if (s > tail) /* remove one extension */
21596 *fnamelen = (int)(s - *fnamep);
21597 }
21598 *usedlen += 2;
21599 }
21600
21601 /* ":s?pat?foo?" - substitute */
21602 /* ":gs?pat?foo?" - global substitute */
21603 if (src[*usedlen] == ':'
21604 && (src[*usedlen + 1] == 's'
21605 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21606 {
21607 char_u *str;
21608 char_u *pat;
21609 char_u *sub;
21610 int sep;
21611 char_u *flags;
21612 int didit = FALSE;
21613
21614 flags = (char_u *)"";
21615 s = src + *usedlen + 2;
21616 if (src[*usedlen + 1] == 'g')
21617 {
21618 flags = (char_u *)"g";
21619 ++s;
21620 }
21621
21622 sep = *s++;
21623 if (sep)
21624 {
21625 /* find end of pattern */
21626 p = vim_strchr(s, sep);
21627 if (p != NULL)
21628 {
21629 pat = vim_strnsave(s, (int)(p - s));
21630 if (pat != NULL)
21631 {
21632 s = p + 1;
21633 /* find end of substitution */
21634 p = vim_strchr(s, sep);
21635 if (p != NULL)
21636 {
21637 sub = vim_strnsave(s, (int)(p - s));
21638 str = vim_strnsave(*fnamep, *fnamelen);
21639 if (sub != NULL && str != NULL)
21640 {
21641 *usedlen = (int)(p + 1 - src);
21642 s = do_string_sub(str, pat, sub, flags);
21643 if (s != NULL)
21644 {
21645 *fnamep = s;
21646 *fnamelen = (int)STRLEN(s);
21647 vim_free(*bufp);
21648 *bufp = s;
21649 didit = TRUE;
21650 }
21651 }
21652 vim_free(sub);
21653 vim_free(str);
21654 }
21655 vim_free(pat);
21656 }
21657 }
21658 /* after using ":s", repeat all the modifiers */
21659 if (didit)
21660 goto repeat;
21661 }
21662 }
21663
21664 return valid;
21665}
21666
21667/*
21668 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21669 * "flags" can be "g" to do a global substitute.
21670 * Returns an allocated string, NULL for error.
21671 */
21672 char_u *
21673do_string_sub(str, pat, sub, flags)
21674 char_u *str;
21675 char_u *pat;
21676 char_u *sub;
21677 char_u *flags;
21678{
21679 int sublen;
21680 regmatch_T regmatch;
21681 int i;
21682 int do_all;
21683 char_u *tail;
21684 garray_T ga;
21685 char_u *ret;
21686 char_u *save_cpo;
21687
21688 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21689 save_cpo = p_cpo;
21690 p_cpo = (char_u *)"";
21691
21692 ga_init2(&ga, 1, 200);
21693
21694 do_all = (flags[0] == 'g');
21695
21696 regmatch.rm_ic = p_ic;
21697 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21698 if (regmatch.regprog != NULL)
21699 {
21700 tail = str;
21701 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21702 {
21703 /*
21704 * Get some space for a temporary buffer to do the substitution
21705 * into. It will contain:
21706 * - The text up to where the match is.
21707 * - The substituted text.
21708 * - The text after the match.
21709 */
21710 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21711 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21712 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21713 {
21714 ga_clear(&ga);
21715 break;
21716 }
21717
21718 /* copy the text up to where the match is */
21719 i = (int)(regmatch.startp[0] - tail);
21720 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21721 /* add the substituted text */
21722 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21723 + ga.ga_len + i, TRUE, TRUE, FALSE);
21724 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725 /* avoid getting stuck on a match with an empty string */
21726 if (tail == regmatch.endp[0])
21727 {
21728 if (*tail == NUL)
21729 break;
21730 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21731 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 }
21733 else
21734 {
21735 tail = regmatch.endp[0];
21736 if (*tail == NUL)
21737 break;
21738 }
21739 if (!do_all)
21740 break;
21741 }
21742
21743 if (ga.ga_data != NULL)
21744 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21745
21746 vim_free(regmatch.regprog);
21747 }
21748
21749 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21750 ga_clear(&ga);
21751 p_cpo = save_cpo;
21752
21753 return ret;
21754}
21755
21756#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */