blob: e008df24aacd897c0651be16b5a1db649e8739cd [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169 proftime_T uf_tm_children; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count; /* nr of times line was executed */
172 proftime_T *uf_tml_total; /* time spend in a line + children */
173 proftime_T *uf_tml_self; /* time spend in a line itself */
174 proftime_T uf_tml_start; /* start time for current line */
175 proftime_T uf_tml_children; /* time spent in children for this line */
176 proftime_T uf_tml_wait; /* start wait time for current line */
177 int uf_tml_idx; /* index of line being timed; -1 if none */
178 int uf_tml_execed; /* line being timed was executed */
179#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000180 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000182 int uf_refcount; /* for numbered function: reference count */
183 char_u uf_name[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
185 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186};
187
188/* function flags */
189#define FC_ABORT 1 /* abort function on error */
190#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000191#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000194 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000196static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000198/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000199static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000344 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000345 {VV_NAME("mouse_win", VAR_NUMBER), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000348 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000349};
350
351/* shorthand */
352#define vv_type vv_di.di_tv.v_type
353#define vv_nr vv_di.di_tv.vval.v_number
354#define vv_str vv_di.di_tv.vval.v_string
355#define vv_tv vv_di.di_tv
356
357/*
358 * The v: variables are stored in dictionary "vimvardict".
359 * "vimvars_var" is the variable that is used for the "l:" scope.
360 */
361static dict_T vimvardict;
362static dictitem_T vimvars_var;
363#define vimvarht vimvardict.dv_hashtab
364
Bram Moolenaara40058a2005-07-11 22:42:07 +0000365static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
366static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
367#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
368static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
369#endif
370static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
371static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
372static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000373static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
374static void list_glob_vars __ARGS((int *first));
375static void list_buf_vars __ARGS((int *first));
376static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000377#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000378static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000379#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000380static void list_vim_vars __ARGS((int *first));
381static void list_script_vars __ARGS((int *first));
382static void list_func_vars __ARGS((int *first));
383static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000384static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
385static int check_changedtick __ARGS((char_u *arg));
386static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
387static void clear_lval __ARGS((lval_T *lp));
388static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
389static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
390static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
391static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
392static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
393static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
394static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
395static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
396static void item_lock __ARGS((typval_T *tv, int deep, int lock));
397static int tv_islocked __ARGS((typval_T *tv));
398
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
400static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000408static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000413static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static listitem_T *listitem_alloc __ARGS((void));
415static void listitem_free __ARGS((listitem_T *item));
416static void listitem_remove __ARGS((list_T *l, listitem_T *item));
417static long list_len __ARGS((list_T *l));
418static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
419static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
420static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void list_append __ARGS((list_T *l, listitem_T *item));
425static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000426static int list_append_string __ARGS((list_T *l, char_u *str, int len));
427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000433static char_u *list2string __ARGS((typval_T *tv, int copyID));
434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000435static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
436static void set_ref_in_list __ARGS((list_T *l, int copyID));
437static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000439static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static dictitem_T *dictitem_alloc __ARGS((char_u *key));
441static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
442static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
443static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000444static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int dict_add __ARGS((dict_T *d, dictitem_T *item));
446static long dict_len __ARGS((dict_T *d));
447static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
453static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
454static int find_internal_func __ARGS((char_u *name));
455static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
456static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
457static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000458static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000476static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000479static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000482static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000483static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
491static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000504static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000518static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000520static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000526static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000527static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000534static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000535static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000536static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000537static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000540static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000541static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000548static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000562static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000568static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000584static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000585static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000586static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000588static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000592#ifdef vim_mkdir
593static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
594#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000598static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000600static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000601static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000603static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000604static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000617static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000619static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000626static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000627static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000628static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000629static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000631static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000633static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000636static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000637static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000640static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641#ifdef HAVE_STRFTIME
642static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
643#endif
644static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000655static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000657static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000658static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000659static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000660static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000661static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000663static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000677static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000680static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000682static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000683static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static int get_env_len __ARGS((char_u **arg));
685static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000686static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000687static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
688#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
689#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
690 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000691static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000693static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000694static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
695static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static typval_T *alloc_tv __ARGS((void));
697static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void init_tv __ARGS((typval_T *varp));
699static long get_tv_number __ARGS((typval_T *varp));
700static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000701static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static char_u *get_tv_string __ARGS((typval_T *varp));
703static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000704static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000706static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
708static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
709static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000710static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
711static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
713static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000714static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000715static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000717static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
719static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
720static int eval_fname_script __ARGS((char_u *p));
721static int eval_fname_sid __ARGS((char_u *p));
722static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static ufunc_T *find_func __ARGS((char_u *name));
724static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000725static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000726#ifdef FEAT_PROFILE
727static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000728static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
729static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
730static int
731# ifdef __BORLANDC__
732 _RTLENTRYF
733# endif
734 prof_total_cmp __ARGS((const void *s1, const void *s2));
735static int
736# ifdef __BORLANDC__
737 _RTLENTRYF
738# endif
739 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000741static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000742static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000743static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void func_free __ARGS((ufunc_T *fp));
745static void func_unref __ARGS((char_u *name));
746static void func_ref __ARGS((char_u *name));
747static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
748static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000749static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
750static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000751static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000752static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000753static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000755/* Character used as separated in autoload function/variable names. */
756#define AUTOLOAD_CHAR '#'
757
Bram Moolenaar33570922005-01-25 22:26:29 +0000758/*
759 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000760 */
761 void
762eval_init()
763{
Bram Moolenaar33570922005-01-25 22:26:29 +0000764 int i;
765 struct vimvar *p;
766
767 init_var_dict(&globvardict, &globvars_var);
768 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000769 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000770 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000771
772 for (i = 0; i < VV_LEN; ++i)
773 {
774 p = &vimvars[i];
775 STRCPY(p->vv_di.di_key, p->vv_name);
776 if (p->vv_flags & VV_RO)
777 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
778 else if (p->vv_flags & VV_RO_SBX)
779 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
780 else
781 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000782
783 /* add to v: scope dict, unless the value is not always available */
784 if (p->vv_type != VAR_UNKNOWN)
785 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000787 /* add to compat scope dict */
788 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000789 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000790}
791
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000792#if defined(EXITFREE) || defined(PROTO)
793 void
794eval_clear()
795{
796 int i;
797 struct vimvar *p;
798
799 for (i = 0; i < VV_LEN; ++i)
800 {
801 p = &vimvars[i];
802 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000803 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000804 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000805 p->vv_di.di_tv.vval.v_string = NULL;
806 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000807 }
808 hash_clear(&vimvarht);
809 hash_clear(&compat_hashtab);
810
811 /* script-local variables */
812 for (i = 1; i <= ga_scripts.ga_len; ++i)
813 vars_clear(&SCRIPT_VARS(i));
814 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000815 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000816
817 /* global variables */
818 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000820 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000821 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000822 hash_clear(&func_hashtab);
823
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000824 /* autoloaded script names */
825 ga_clear_strings(&ga_loaded);
826
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000827 /* unreferenced lists and dicts */
828 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000829}
830#endif
831
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * Return the name of the executed function.
834 */
835 char_u *
836func_name(cookie)
837 void *cookie;
838{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000839 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841
842/*
843 * Return the address holding the next breakpoint line for a funccall cookie.
844 */
845 linenr_T *
846func_breakpoint(cookie)
847 void *cookie;
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
852/*
853 * Return the address holding the debug tick for a funccall cookie.
854 */
855 int *
856func_dbg_tick(cookie)
857 void *cookie;
858{
Bram Moolenaar33570922005-01-25 22:26:29 +0000859 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860}
861
862/*
863 * Return the nesting level for a funccall cookie.
864 */
865 int
866func_level(cookie)
867 void *cookie;
868{
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870}
871
872/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000873funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875/*
876 * Return TRUE when a function was ended by a ":return" command.
877 */
878 int
879current_func_returned()
880{
881 return current_funccal->returned;
882}
883
884
885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 * Set an internal variable to a string value. Creates the variable if it does
887 * not already exist.
888 */
889 void
890set_internal_string_var(name, value)
891 char_u *name;
892 char_u *value;
893{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000894 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 val = vim_strsave(value);
898 if (val != NULL)
899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000900 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000901 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000903 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000904 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
906 }
907}
908
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000909static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000910static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000911static char_u *redir_endp = NULL;
912static char_u *redir_varname = NULL;
913
914/*
915 * Start recording command output to a variable
916 * Returns OK if successfully completed the setup. FAIL otherwise.
917 */
918 int
919var_redir_start(name, append)
920 char_u *name;
921 int append; /* append to an existing variable */
922{
923 int save_emsg;
924 int err;
925 typval_T tv;
926
927 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000928 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000929 {
930 EMSG(_(e_invarg));
931 return FAIL;
932 }
933
934 redir_varname = vim_strsave(name);
935 if (redir_varname == NULL)
936 return FAIL;
937
938 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
939 if (redir_lval == NULL)
940 {
941 var_redir_stop();
942 return FAIL;
943 }
944
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000945 /* The output is stored in growarray "redir_ga" until redirection ends. */
946 ga_init2(&redir_ga, (int)sizeof(char), 500);
947
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000948 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000949 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
950 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000951 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
952 {
953 if (redir_endp != NULL && *redir_endp != NUL)
954 /* Trailing characters are present after the variable name */
955 EMSG(_(e_trailing));
956 else
957 EMSG(_(e_invarg));
958 var_redir_stop();
959 return FAIL;
960 }
961
962 /* check if we can write to the variable: set it to or append an empty
963 * string */
964 save_emsg = did_emsg;
965 did_emsg = FALSE;
966 tv.v_type = VAR_STRING;
967 tv.vval.v_string = (char_u *)"";
968 if (append)
969 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
970 else
971 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
972 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000973 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000974 if (err)
975 {
976 var_redir_stop();
977 return FAIL;
978 }
979 if (redir_lval->ll_newkey != NULL)
980 {
981 /* Dictionary item was created, don't do it again. */
982 vim_free(redir_lval->ll_newkey);
983 redir_lval->ll_newkey = NULL;
984 }
985
986 return OK;
987}
988
989/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000990 * Append "value[value_len]" to the variable set by var_redir_start().
991 * The actual appending is postponed until redirection ends, because the value
992 * appended may in fact be the string we write to, changing it may cause freed
993 * memory to be used:
994 * :redir => foo
995 * :let foo
996 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000997 */
998 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000999var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001000 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001001 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001002{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001003 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001004
1005 if (redir_lval == NULL)
1006 return;
1007
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001008 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001009 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001011 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001013 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001014 {
1015 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001016 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017 }
1018 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020}
1021
1022/*
1023 * Stop redirecting command output to a variable.
1024 */
1025 void
1026var_redir_stop()
1027{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001028 typval_T tv;
1029
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001030 if (redir_lval != NULL)
1031 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001032 /* Append the trailing NUL. */
1033 ga_append(&redir_ga, NUL);
1034
1035 /* Assign the text to the variable. */
1036 tv.v_type = VAR_STRING;
1037 tv.vval.v_string = redir_ga.ga_data;
1038 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1039 vim_free(tv.vval.v_string);
1040
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 clear_lval(redir_lval);
1042 vim_free(redir_lval);
1043 redir_lval = NULL;
1044 }
1045 vim_free(redir_varname);
1046 redir_varname = NULL;
1047}
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049# if defined(FEAT_MBYTE) || defined(PROTO)
1050 int
1051eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1052 char_u *enc_from;
1053 char_u *enc_to;
1054 char_u *fname_from;
1055 char_u *fname_to;
1056{
1057 int err = FALSE;
1058
1059 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1060 set_vim_var_string(VV_CC_TO, enc_to, -1);
1061 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1062 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1063 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1064 err = TRUE;
1065 set_vim_var_string(VV_CC_FROM, NULL, -1);
1066 set_vim_var_string(VV_CC_TO, NULL, -1);
1067 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1068 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1069
1070 if (err)
1071 return FAIL;
1072 return OK;
1073}
1074# endif
1075
1076# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1077 int
1078eval_printexpr(fname, args)
1079 char_u *fname;
1080 char_u *args;
1081{
1082 int err = FALSE;
1083
1084 set_vim_var_string(VV_FNAME_IN, fname, -1);
1085 set_vim_var_string(VV_CMDARG, args, -1);
1086 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1087 err = TRUE;
1088 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1089 set_vim_var_string(VV_CMDARG, NULL, -1);
1090
1091 if (err)
1092 {
1093 mch_remove(fname);
1094 return FAIL;
1095 }
1096 return OK;
1097}
1098# endif
1099
1100# if defined(FEAT_DIFF) || defined(PROTO)
1101 void
1102eval_diff(origfile, newfile, outfile)
1103 char_u *origfile;
1104 char_u *newfile;
1105 char_u *outfile;
1106{
1107 int err = FALSE;
1108
1109 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1110 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1111 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1112 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1113 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1114 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1115 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1116}
1117
1118 void
1119eval_patch(origfile, difffile, outfile)
1120 char_u *origfile;
1121 char_u *difffile;
1122 char_u *outfile;
1123{
1124 int err;
1125
1126 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1127 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1128 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1129 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1130 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1131 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1132 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1133}
1134# endif
1135
1136/*
1137 * Top level evaluation function, returning a boolean.
1138 * Sets "error" to TRUE if there was an error.
1139 * Return TRUE or FALSE.
1140 */
1141 int
1142eval_to_bool(arg, error, nextcmd, skip)
1143 char_u *arg;
1144 int *error;
1145 char_u **nextcmd;
1146 int skip; /* only parse, don't execute */
1147{
Bram Moolenaar33570922005-01-25 22:26:29 +00001148 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 int retval = FALSE;
1150
1151 if (skip)
1152 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 else
1156 {
1157 *error = FALSE;
1158 if (!skip)
1159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001160 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001161 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163 }
1164 if (skip)
1165 --emsg_skip;
1166
1167 return retval;
1168}
1169
1170/*
1171 * Top level evaluation function, returning a string. If "skip" is TRUE,
1172 * only parsing to "nextcmd" is done, without reporting errors. Return
1173 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1174 */
1175 char_u *
1176eval_to_string_skip(arg, nextcmd, skip)
1177 char_u *arg;
1178 char_u **nextcmd;
1179 int skip; /* only parse, don't execute */
1180{
Bram Moolenaar33570922005-01-25 22:26:29 +00001181 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 char_u *retval;
1183
1184 if (skip)
1185 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001186 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 retval = NULL;
1188 else
1189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 retval = vim_strsave(get_tv_string(&tv));
1191 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193 if (skip)
1194 --emsg_skip;
1195
1196 return retval;
1197}
1198
1199/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001200 * Skip over an expression at "*pp".
1201 * Return FAIL for an error, OK otherwise.
1202 */
1203 int
1204skip_expr(pp)
1205 char_u **pp;
1206{
Bram Moolenaar33570922005-01-25 22:26:29 +00001207 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001208
1209 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001211}
1212
1213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 * Top level evaluation function, returning a string.
1215 * Return pointer to allocated memory, or NULL for failure.
1216 */
1217 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001218eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 char_u *arg;
1220 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001221 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222{
Bram Moolenaar33570922005-01-25 22:26:29 +00001223 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001225 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001227 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 retval = NULL;
1229 else
1230 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001231 if (dolist && tv.v_type == VAR_LIST)
1232 {
1233 ga_init2(&ga, (int)sizeof(char), 80);
1234 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1235 ga_append(&ga, NUL);
1236 retval = (char_u *)ga.ga_data;
1237 }
1238 else
1239 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001240 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242
1243 return retval;
1244}
1245
1246/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001247 * Call eval_to_string() without using current local variables and using
1248 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 */
1250 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001251eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 char_u *arg;
1253 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001254 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255{
1256 char_u *retval;
1257 void *save_funccalp;
1258
1259 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001260 if (use_sandbox)
1261 ++sandbox;
1262 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001263 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001264 if (use_sandbox)
1265 --sandbox;
1266 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 restore_funccal(save_funccalp);
1268 return retval;
1269}
1270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271/*
1272 * Top level evaluation function, returning a number.
1273 * Evaluates "expr" silently.
1274 * Returns -1 for an error.
1275 */
1276 int
1277eval_to_number(expr)
1278 char_u *expr;
1279{
Bram Moolenaar33570922005-01-25 22:26:29 +00001280 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001282 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
1284 ++emsg_off;
1285
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001286 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 retval = -1;
1288 else
1289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001290 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001291 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293 --emsg_off;
1294
1295 return retval;
1296}
1297
Bram Moolenaara40058a2005-07-11 22:42:07 +00001298/*
1299 * Prepare v: variable "idx" to be used.
1300 * Save the current typeval in "save_tv".
1301 * When not used yet add the variable to the v: hashtable.
1302 */
1303 static void
1304prepare_vimvar(idx, save_tv)
1305 int idx;
1306 typval_T *save_tv;
1307{
1308 *save_tv = vimvars[idx].vv_tv;
1309 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1310 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1311}
1312
1313/*
1314 * Restore v: variable "idx" to typeval "save_tv".
1315 * When no longer defined, remove the variable from the v: hashtable.
1316 */
1317 static void
1318restore_vimvar(idx, save_tv)
1319 int idx;
1320 typval_T *save_tv;
1321{
1322 hashitem_T *hi;
1323
Bram Moolenaara40058a2005-07-11 22:42:07 +00001324 vimvars[idx].vv_tv = *save_tv;
1325 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1326 {
1327 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1328 if (HASHITEM_EMPTY(hi))
1329 EMSG2(_(e_intern2), "restore_vimvar()");
1330 else
1331 hash_remove(&vimvarht, hi);
1332 }
1333}
1334
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001335#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001336/*
1337 * Evaluate an expression to a list with suggestions.
1338 * For the "expr:" part of 'spellsuggest'.
1339 */
1340 list_T *
1341eval_spell_expr(badword, expr)
1342 char_u *badword;
1343 char_u *expr;
1344{
1345 typval_T save_val;
1346 typval_T rettv;
1347 list_T *list = NULL;
1348 char_u *p = skipwhite(expr);
1349
1350 /* Set "v:val" to the bad word. */
1351 prepare_vimvar(VV_VAL, &save_val);
1352 vimvars[VV_VAL].vv_type = VAR_STRING;
1353 vimvars[VV_VAL].vv_str = badword;
1354 if (p_verbose == 0)
1355 ++emsg_off;
1356
1357 if (eval1(&p, &rettv, TRUE) == OK)
1358 {
1359 if (rettv.v_type != VAR_LIST)
1360 clear_tv(&rettv);
1361 else
1362 list = rettv.vval.v_list;
1363 }
1364
1365 if (p_verbose == 0)
1366 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001367 restore_vimvar(VV_VAL, &save_val);
1368
1369 return list;
1370}
1371
1372/*
1373 * "list" is supposed to contain two items: a word and a number. Return the
1374 * word in "pp" and the number as the return value.
1375 * Return -1 if anything isn't right.
1376 * Used to get the good word and score from the eval_spell_expr() result.
1377 */
1378 int
1379get_spellword(list, pp)
1380 list_T *list;
1381 char_u **pp;
1382{
1383 listitem_T *li;
1384
1385 li = list->lv_first;
1386 if (li == NULL)
1387 return -1;
1388 *pp = get_tv_string(&li->li_tv);
1389
1390 li = li->li_next;
1391 if (li == NULL)
1392 return -1;
1393 return get_tv_number(&li->li_tv);
1394}
1395#endif
1396
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001397/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001398 * Top level evaluation function.
1399 * Returns an allocated typval_T with the result.
1400 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001401 */
1402 typval_T *
1403eval_expr(arg, nextcmd)
1404 char_u *arg;
1405 char_u **nextcmd;
1406{
1407 typval_T *tv;
1408
1409 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001410 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001411 {
1412 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001413 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001414 }
1415
1416 return tv;
1417}
1418
1419
Bram Moolenaar4f688582007-07-24 12:34:30 +00001420#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1421 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001423 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001427 static int
1428call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 char_u *func;
1430 int argc;
1431 char_u **argv;
1432 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
Bram Moolenaar33570922005-01-25 22:26:29 +00001435 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 long n;
1437 int len;
1438 int i;
1439 int doesrange;
1440 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001443 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
1447 for (i = 0; i < argc; i++)
1448 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001449 /* Pass a NULL or empty argument as an empty string */
1450 if (argv[i] == NULL || *argv[i] == NUL)
1451 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001452 argvars[i].v_type = VAR_STRING;
1453 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001454 continue;
1455 }
1456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 /* Recognize a number argument, the others must be strings. */
1458 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1459 if (len != 0 && len == (int)STRLEN(argv[i]))
1460 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001461 argvars[i].v_type = VAR_NUMBER;
1462 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
1464 else
1465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001466 argvars[i].v_type = VAR_STRING;
1467 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
1469 }
1470
1471 if (safe)
1472 {
1473 save_funccalp = save_funccal();
1474 ++sandbox;
1475 }
1476
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1478 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (safe)
1482 {
1483 --sandbox;
1484 restore_funccal(save_funccalp);
1485 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001486 vim_free(argvars);
1487
1488 if (ret == FAIL)
1489 clear_tv(rettv);
1490
1491 return ret;
1492}
1493
Bram Moolenaar4f688582007-07-24 12:34:30 +00001494# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001495/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001496 * Call vimL function "func" and return the result as a string.
1497 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001498 * Uses argv[argc] for the function arguments.
1499 */
1500 void *
1501call_func_retstr(func, argc, argv, safe)
1502 char_u *func;
1503 int argc;
1504 char_u **argv;
1505 int safe; /* use the sandbox */
1506{
1507 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001508 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001509
1510 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1511 return NULL;
1512
1513 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001514 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 return retval;
1516}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001517# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001518
Bram Moolenaar4f688582007-07-24 12:34:30 +00001519# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001520/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001521 * Call vimL function "func" and return the result as a number.
1522 * Returns -1 when calling the function fails.
1523 * Uses argv[argc] for the function arguments.
1524 */
1525 long
1526call_func_retnr(func, argc, argv, safe)
1527 char_u *func;
1528 int argc;
1529 char_u **argv;
1530 int safe; /* use the sandbox */
1531{
1532 typval_T rettv;
1533 long retval;
1534
1535 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1536 return -1;
1537
1538 retval = get_tv_number_chk(&rettv, NULL);
1539 clear_tv(&rettv);
1540 return retval;
1541}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001542# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001543
1544/*
1545 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001546 * Uses argv[argc] for the function arguments.
1547 */
1548 void *
1549call_func_retlist(func, argc, argv, safe)
1550 char_u *func;
1551 int argc;
1552 char_u **argv;
1553 int safe; /* use the sandbox */
1554{
1555 typval_T rettv;
1556
1557 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1558 return NULL;
1559
1560 if (rettv.v_type != VAR_LIST)
1561 {
1562 clear_tv(&rettv);
1563 return NULL;
1564 }
1565
1566 return rettv.vval.v_list;
1567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568#endif
1569
Bram Moolenaar4f688582007-07-24 12:34:30 +00001570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571/*
1572 * Save the current function call pointer, and set it to NULL.
1573 * Used when executing autocommands and for ":source".
1574 */
1575 void *
1576save_funccal()
1577{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001578 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 current_funccal = NULL;
1581 return (void *)fc;
1582}
1583
1584 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001585restore_funccal(vfc)
1586 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001588 funccall_T *fc = (funccall_T *)vfc;
1589
1590 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591}
1592
Bram Moolenaar05159a02005-02-26 23:04:13 +00001593#if defined(FEAT_PROFILE) || defined(PROTO)
1594/*
1595 * Prepare profiling for entering a child or something else that is not
1596 * counted for the script/function itself.
1597 * Should always be called in pair with prof_child_exit().
1598 */
1599 void
1600prof_child_enter(tm)
1601 proftime_T *tm; /* place to store waittime */
1602{
1603 funccall_T *fc = current_funccal;
1604
1605 if (fc != NULL && fc->func->uf_profiling)
1606 profile_start(&fc->prof_child);
1607 script_prof_save(tm);
1608}
1609
1610/*
1611 * Take care of time spent in a child.
1612 * Should always be called after prof_child_enter().
1613 */
1614 void
1615prof_child_exit(tm)
1616 proftime_T *tm; /* where waittime was stored */
1617{
1618 funccall_T *fc = current_funccal;
1619
1620 if (fc != NULL && fc->func->uf_profiling)
1621 {
1622 profile_end(&fc->prof_child);
1623 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1624 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1625 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1626 }
1627 script_prof_restore(tm);
1628}
1629#endif
1630
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#ifdef FEAT_FOLDING
1633/*
1634 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1635 * it in "*cp". Doesn't give error messages.
1636 */
1637 int
1638eval_foldexpr(arg, cp)
1639 char_u *arg;
1640 int *cp;
1641{
Bram Moolenaar33570922005-01-25 22:26:29 +00001642 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 int retval;
1644 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001645 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1646 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001649 if (use_sandbox)
1650 ++sandbox;
1651 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 retval = 0;
1655 else
1656 {
1657 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 if (tv.v_type == VAR_NUMBER)
1659 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001660 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 retval = 0;
1662 else
1663 {
1664 /* If the result is a string, check if there is a non-digit before
1665 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001666 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if (!VIM_ISDIGIT(*s) && *s != '-')
1668 *cp = *s++;
1669 retval = atol((char *)s);
1670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 }
1673 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001674 if (use_sandbox)
1675 --sandbox;
1676 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 return retval;
1679}
1680#endif
1681
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683 * ":let" list all variable values
1684 * ":let var1 var2" list variable values
1685 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001686 * ":let var += expr" assignment command.
1687 * ":let var -= expr" assignment command.
1688 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
1691 void
1692ex_let(eap)
1693 exarg_T *eap;
1694{
1695 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001697 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699 int var_count = 0;
1700 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001701 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001702 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001703 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaardb552d602006-03-23 22:59:57 +00001705 argend = skip_var_list(arg, &var_count, &semicolon);
1706 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001708 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1709 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001710 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001713 /*
1714 * ":let" without "=": list variables
1715 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 if (*arg == '[')
1717 EMSG(_(e_invarg));
1718 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001719 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001720 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001723 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001724 list_glob_vars(&first);
1725 list_buf_vars(&first);
1726 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001727#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001728 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001729#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001730 list_script_vars(&first);
1731 list_func_vars(&first);
1732 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 eap->nextcmd = check_nextcmd(arg);
1735 }
1736 else
1737 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 op[0] = '=';
1739 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001740 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741 {
1742 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1743 op[0] = expr[-1]; /* +=, -= or .= */
1744 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (eap->skip)
1748 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 if (eap->skip)
1751 {
1752 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001753 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 --emsg_skip;
1755 }
1756 else if (i != FAIL)
1757 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762 }
1763}
1764
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765/*
1766 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1767 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001768 * When "nextchars" is not NULL it points to a string with characters that
1769 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1770 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 * Returns OK or FAIL;
1772 */
1773 static int
1774ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1775 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001776 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 int copy; /* copy values from "tv", don't move */
1778 int semicolon; /* from skip_var_list() */
1779 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001780 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781{
1782 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001785 listitem_T *item;
1786 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787
1788 if (*arg != '[')
1789 {
1790 /*
1791 * ":let var = expr" or ":for var in list"
1792 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 return FAIL;
1795 return OK;
1796 }
1797
1798 /*
1799 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1800 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001801 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 {
1803 EMSG(_(e_listreq));
1804 return FAIL;
1805 }
1806
1807 i = list_len(l);
1808 if (semicolon == 0 && var_count < i)
1809 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001810 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 return FAIL;
1812 }
1813 if (var_count - semicolon > i)
1814 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001815 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 return FAIL;
1817 }
1818
1819 item = l->lv_first;
1820 while (*arg != ']')
1821 {
1822 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001823 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 item = item->li_next;
1825 if (arg == NULL)
1826 return FAIL;
1827
1828 arg = skipwhite(arg);
1829 if (*arg == ';')
1830 {
1831 /* Put the rest of the list (may be empty) in the var after ';'.
1832 * Create a new list for this. */
1833 l = list_alloc();
1834 if (l == NULL)
1835 return FAIL;
1836 while (item != NULL)
1837 {
1838 list_append_tv(l, &item->li_tv);
1839 item = item->li_next;
1840 }
1841
1842 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001843 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 ltv.vval.v_list = l;
1845 l->lv_refcount = 1;
1846
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1848 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 clear_tv(&ltv);
1850 if (arg == NULL)
1851 return FAIL;
1852 break;
1853 }
1854 else if (*arg != ',' && *arg != ']')
1855 {
1856 EMSG2(_(e_intern2), "ex_let_vars()");
1857 return FAIL;
1858 }
1859 }
1860
1861 return OK;
1862}
1863
1864/*
1865 * Skip over assignable variable "var" or list of variables "[var, var]".
1866 * Used for ":let varvar = expr" and ":for varvar in expr".
1867 * For "[var, var]" increment "*var_count" for each variable.
1868 * for "[var, var; var]" set "semicolon".
1869 * Return NULL for an error.
1870 */
1871 static char_u *
1872skip_var_list(arg, var_count, semicolon)
1873 char_u *arg;
1874 int *var_count;
1875 int *semicolon;
1876{
1877 char_u *p, *s;
1878
1879 if (*arg == '[')
1880 {
1881 /* "[var, var]": find the matching ']'. */
1882 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001883 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 {
1885 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1886 s = skip_var_one(p);
1887 if (s == p)
1888 {
1889 EMSG2(_(e_invarg2), p);
1890 return NULL;
1891 }
1892 ++*var_count;
1893
1894 p = skipwhite(s);
1895 if (*p == ']')
1896 break;
1897 else if (*p == ';')
1898 {
1899 if (*semicolon == 1)
1900 {
1901 EMSG(_("Double ; in list of variables"));
1902 return NULL;
1903 }
1904 *semicolon = 1;
1905 }
1906 else if (*p != ',')
1907 {
1908 EMSG2(_(e_invarg2), p);
1909 return NULL;
1910 }
1911 }
1912 return p + 1;
1913 }
1914 else
1915 return skip_var_one(arg);
1916}
1917
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001918/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001919 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001920 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 static char_u *
1923skip_var_one(arg)
1924 char_u *arg;
1925{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001926 if (*arg == '@' && arg[1] != NUL)
1927 return arg + 2;
1928 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1929 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930}
1931
Bram Moolenaara7043832005-01-21 11:56:39 +00001932/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 * List variables for hashtab "ht" with prefix "prefix".
1934 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001935 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001937list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001939 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001941 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001942{
Bram Moolenaar33570922005-01-25 22:26:29 +00001943 hashitem_T *hi;
1944 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001945 int todo;
1946
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001947 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001948 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1949 {
1950 if (!HASHITEM_EMPTY(hi))
1951 {
1952 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001953 di = HI2DI(hi);
1954 if (empty || di->di_tv.v_type != VAR_STRING
1955 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001956 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 }
1958 }
1959}
1960
1961/*
1962 * List global variables.
1963 */
1964 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965list_glob_vars(first)
1966 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001967{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001968 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001969}
1970
1971/*
1972 * List buffer variables.
1973 */
1974 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001975list_buf_vars(first)
1976 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001977{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001978 char_u numbuf[NUMBUFLEN];
1979
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001980 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1981 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982
1983 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001984 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1985 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001986}
1987
1988/*
1989 * List window variables.
1990 */
1991 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001992list_win_vars(first)
1993 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001994{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001995 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1996 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001997}
1998
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001999#ifdef FEAT_WINDOWS
2000/*
2001 * List tab page variables.
2002 */
2003 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002004list_tab_vars(first)
2005 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002006{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002007 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2008 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002009}
2010#endif
2011
Bram Moolenaara7043832005-01-21 11:56:39 +00002012/*
2013 * List Vim variables.
2014 */
2015 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002016list_vim_vars(first)
2017 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002019 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020}
2021
2022/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002023 * List script-local variables, if there is a script.
2024 */
2025 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002026list_script_vars(first)
2027 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002028{
2029 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002030 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2031 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002032}
2033
2034/*
2035 * List function variables, if there is a function.
2036 */
2037 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002038list_func_vars(first)
2039 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002040{
2041 if (current_funccal != NULL)
2042 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002043 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002044}
2045
2046/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 * List variables in "arg".
2048 */
2049 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002050list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 exarg_T *eap;
2052 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002053 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054{
2055 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058 char_u *name_start;
2059 char_u *arg_subsc;
2060 char_u *tofree;
2061 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062
2063 while (!ends_excmd(*arg) && !got_int)
2064 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002067 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002068 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2069 {
2070 emsg_severe = TRUE;
2071 EMSG(_(e_trailing));
2072 break;
2073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002077 /* get_name_len() takes care of expanding curly braces */
2078 name_start = name = arg;
2079 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2080 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082 /* This is mainly to keep test 49 working: when expanding
2083 * curly braces fails overrule the exception error message. */
2084 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002085 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002086 emsg_severe = TRUE;
2087 EMSG2(_(e_invarg2), arg);
2088 break;
2089 }
2090 error = TRUE;
2091 }
2092 else
2093 {
2094 if (tofree != NULL)
2095 name = tofree;
2096 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 else
2099 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 /* handle d.key, l[idx], f(expr) */
2101 arg_subsc = arg;
2102 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002108 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 case 'g': list_glob_vars(first); break;
2111 case 'b': list_buf_vars(first); break;
2112 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002113#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002115#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 case 'v': list_vim_vars(first); break;
2117 case 's': list_script_vars(first); break;
2118 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 default:
2120 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 }
2123 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124 {
2125 char_u numbuf[NUMBUFLEN];
2126 char_u *tf;
2127 int c;
2128 char_u *s;
2129
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002130 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002131 c = *arg;
2132 *arg = NUL;
2133 list_one_var_a((char_u *)"",
2134 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 tv.v_type,
2136 s == NULL ? (char_u *)"" : s,
2137 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002138 *arg = c;
2139 vim_free(tf);
2140 }
2141 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 }
2144 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145
2146 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002148
2149 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 }
2151
2152 return arg;
2153}
2154
2155/*
2156 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2157 * Returns a pointer to the char just after the var name.
2158 * Returns NULL if there is an error.
2159 */
2160 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002163 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002165 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167{
2168 int c1;
2169 char_u *name;
2170 char_u *p;
2171 char_u *arg_end = NULL;
2172 int len;
2173 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175
2176 /*
2177 * ":let $VAR = expr": Set environment variable.
2178 */
2179 if (*arg == '$')
2180 {
2181 /* Find the end of the name. */
2182 ++arg;
2183 name = arg;
2184 len = get_env_len(&arg);
2185 if (len == 0)
2186 EMSG2(_(e_invarg2), name - 1);
2187 else
2188 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189 if (op != NULL && (*op == '+' || *op == '-'))
2190 EMSG2(_(e_letwrong), op);
2191 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002192 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 EMSG(_(e_letunexp));
2194 else
2195 {
2196 c1 = name[len];
2197 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002198 p = get_tv_string_chk(tv);
2199 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002200 {
2201 int mustfree = FALSE;
2202 char_u *s = vim_getenv(name, &mustfree);
2203
2204 if (s != NULL)
2205 {
2206 p = tofree = concat_str(s, p);
2207 if (mustfree)
2208 vim_free(s);
2209 }
2210 }
2211 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002212 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002213 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002214 if (STRICMP(name, "HOME") == 0)
2215 init_homedir();
2216 else if (didset_vim && STRICMP(name, "VIM") == 0)
2217 didset_vim = FALSE;
2218 else if (didset_vimruntime
2219 && STRICMP(name, "VIMRUNTIME") == 0)
2220 didset_vimruntime = FALSE;
2221 arg_end = arg;
2222 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002224 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 }
2226 }
2227 }
2228
2229 /*
2230 * ":let &option = expr": Set option value.
2231 * ":let &l:option = expr": Set local option value.
2232 * ":let &g:option = expr": Set global option value.
2233 */
2234 else if (*arg == '&')
2235 {
2236 /* Find the end of the name. */
2237 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002238 if (p == NULL || (endchars != NULL
2239 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 EMSG(_(e_letunexp));
2241 else
2242 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002243 long n;
2244 int opt_type;
2245 long numval;
2246 char_u *stringval = NULL;
2247 char_u *s;
2248
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 c1 = *p;
2250 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002251
2252 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002253 s = get_tv_string_chk(tv); /* != NULL if number or string */
2254 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002255 {
2256 opt_type = get_option_value(arg, &numval,
2257 &stringval, opt_flags);
2258 if ((opt_type == 1 && *op == '.')
2259 || (opt_type == 0 && *op != '.'))
2260 EMSG2(_(e_letwrong), op);
2261 else
2262 {
2263 if (opt_type == 1) /* number */
2264 {
2265 if (*op == '+')
2266 n = numval + n;
2267 else
2268 n = numval - n;
2269 }
2270 else if (opt_type == 0 && stringval != NULL) /* string */
2271 {
2272 s = concat_str(stringval, s);
2273 vim_free(stringval);
2274 stringval = s;
2275 }
2276 }
2277 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 if (s != NULL)
2279 {
2280 set_option_value(arg, n, s, opt_flags);
2281 arg_end = p;
2282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002284 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286 }
2287
2288 /*
2289 * ":let @r = expr": Set register contents.
2290 */
2291 else if (*arg == '@')
2292 {
2293 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002294 if (op != NULL && (*op == '+' || *op == '-'))
2295 EMSG2(_(e_letwrong), op);
2296 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002297 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 EMSG(_(e_letunexp));
2299 else
2300 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002301 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 char_u *s;
2303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002304 p = get_tv_string_chk(tv);
2305 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002307 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 if (s != NULL)
2309 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002310 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002311 vim_free(s);
2312 }
2313 }
2314 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002315 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002317 arg_end = arg + 1;
2318 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002319 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
2321 }
2322
2323 /*
2324 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002327 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002329 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002331 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2335 EMSG(_(e_letunexp));
2336 else
2337 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 arg_end = p;
2340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 }
2344
2345 else
2346 EMSG2(_(e_invarg2), arg);
2347
2348 return arg_end;
2349}
2350
2351/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2353 */
2354 static int
2355check_changedtick(arg)
2356 char_u *arg;
2357{
2358 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2359 {
2360 EMSG2(_(e_readonlyvar), arg);
2361 return TRUE;
2362 }
2363 return FALSE;
2364}
2365
2366/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 * Get an lval: variable, Dict item or List item that can be assigned a value
2368 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2369 * "name.key", "name.key[expr]" etc.
2370 * Indexing only works if "name" is an existing List or Dictionary.
2371 * "name" points to the start of the name.
2372 * If "rettv" is not NULL it points to the value to be assigned.
2373 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2374 * wrong; must end in space or cmd separator.
2375 *
2376 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002377 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 */
2380 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002381get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002383 typval_T *rettv;
2384 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 int unlet;
2386 int skip;
2387 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002388 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 char_u *expr_start, *expr_end;
2392 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002393 dictitem_T *v;
2394 typval_T var1;
2395 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002397 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002400 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002403 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404
2405 if (skip)
2406 {
2407 /* When skipping just find the end of the name. */
2408 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002409 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 }
2411
2412 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002413 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 if (expr_start != NULL)
2415 {
2416 /* Don't expand the name when we already know there is an error. */
2417 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2418 && *p != '[' && *p != '.')
2419 {
2420 EMSG(_(e_trailing));
2421 return NULL;
2422 }
2423
2424 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2425 if (lp->ll_exp_name == NULL)
2426 {
2427 /* Report an invalid expression in braces, unless the
2428 * expression evaluation has been cancelled due to an
2429 * aborting error, an interrupt, or an exception. */
2430 if (!aborting() && !quiet)
2431 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002432 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 EMSG2(_(e_invarg2), name);
2434 return NULL;
2435 }
2436 }
2437 lp->ll_name = lp->ll_exp_name;
2438 }
2439 else
2440 lp->ll_name = name;
2441
2442 /* Without [idx] or .key we are done. */
2443 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2444 return p;
2445
2446 cc = *p;
2447 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002448 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 if (v == NULL && !quiet)
2450 EMSG2(_(e_undefvar), lp->ll_name);
2451 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 if (v == NULL)
2453 return NULL;
2454
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 /*
2456 * Loop until no more [idx] or .key is following.
2457 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002458 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2462 && !(lp->ll_tv->v_type == VAR_DICT
2463 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 if (!quiet)
2466 EMSG(_("E689: Can only index a List or Dictionary"));
2467 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (!quiet)
2472 EMSG(_("E708: [:] must come last"));
2473 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002475
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 len = -1;
2477 if (*p == '.')
2478 {
2479 key = p + 1;
2480 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2481 ;
2482 if (len == 0)
2483 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (!quiet)
2485 EMSG(_(e_emptykey));
2486 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 }
2488 p = key + len;
2489 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002490 else
2491 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002493 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 if (*p == ':')
2495 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002496 else
2497 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 empty1 = FALSE;
2499 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 if (get_tv_string_chk(&var1) == NULL)
2502 {
2503 /* not a number or string */
2504 clear_tv(&var1);
2505 return NULL;
2506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 }
2508
2509 /* Optionally get the second index [ :expr]. */
2510 if (*p == ':')
2511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516 if (!empty1)
2517 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (rettv != NULL && (rettv->v_type != VAR_LIST
2521 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (!quiet)
2524 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 if (!empty1)
2526 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 }
2529 p = skipwhite(p + 1);
2530 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 else
2533 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2536 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 if (!empty1)
2538 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002541 if (get_tv_string_chk(&var2) == NULL)
2542 {
2543 /* not a number or string */
2544 if (!empty1)
2545 clear_tv(&var1);
2546 clear_tv(&var2);
2547 return NULL;
2548 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002551 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002554
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 if (!quiet)
2558 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 if (!empty1)
2560 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 }
2565
2566 /* Skip to past ']'. */
2567 ++p;
2568 }
2569
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 {
2572 if (len == -1)
2573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002575 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 if (*key == NUL)
2577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 if (!quiet)
2579 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 }
2583 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002584 lp->ll_list = NULL;
2585 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002589 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002593 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 if (len == -1)
2595 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 }
2598 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 if (len == -1)
2603 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 p = NULL;
2606 break;
2607 }
2608 if (len == -1)
2609 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 }
2612 else
2613 {
2614 /*
2615 * Get the number and item for the only or first index of the List.
2616 */
2617 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 else
2620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002621 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 clear_tv(&var1);
2623 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002624 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 lp->ll_list = lp->ll_tv->vval.v_list;
2626 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2627 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002629 if (lp->ll_n1 < 0)
2630 {
2631 lp->ll_n1 = 0;
2632 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2633 }
2634 }
2635 if (lp->ll_li == NULL)
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641
2642 /*
2643 * May need to find the item or absolute index for the second
2644 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 * When no index given: "lp->ll_empty2" is TRUE.
2646 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002650 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 }
2659
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2661 if (lp->ll_n1 < 0)
2662 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2663 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 }
2666
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
2670
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return p;
2672}
2673
2674/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002675 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 */
2677 static void
2678clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002679 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680{
2681 vim_free(lp->ll_exp_name);
2682 vim_free(lp->ll_newkey);
2683}
2684
2685/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002686 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 */
2690 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002691set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002692 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002694 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002696 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697{
2698 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002699 listitem_T *ri;
2700 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701
2702 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 cc = *endp;
2707 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708 if (op != NULL && *op != '=')
2709 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002710 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002711
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002712 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002713 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002714 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 {
2716 if (tv_op(&tv, rettv, op) == OK)
2717 set_var(lp->ll_name, &tv, FALSE);
2718 clear_tv(&tv);
2719 }
2720 }
2721 else
2722 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002724 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002726 else if (tv_check_lock(lp->ll_newkey == NULL
2727 ? lp->ll_tv->v_lock
2728 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2729 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 else if (lp->ll_range)
2731 {
2732 /*
2733 * Assign the List values to the list items.
2734 */
2735 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 if (op != NULL && *op != '=')
2738 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2739 else
2740 {
2741 clear_tv(&lp->ll_li->li_tv);
2742 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2743 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 ri = ri->li_next;
2745 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2746 break;
2747 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002750 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 ri = NULL;
2753 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002755 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 lp->ll_li = lp->ll_li->li_next;
2757 ++lp->ll_n1;
2758 }
2759 if (ri != NULL)
2760 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002761 else if (lp->ll_empty2
2762 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 : lp->ll_n1 != lp->ll_n2)
2764 EMSG(_("E711: List value has not enough items"));
2765 }
2766 else
2767 {
2768 /*
2769 * Assign to a List or Dictionary item.
2770 */
2771 if (lp->ll_newkey != NULL)
2772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002773 if (op != NULL && *op != '=')
2774 {
2775 EMSG2(_(e_letwrong), op);
2776 return;
2777 }
2778
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002780 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (di == NULL)
2782 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002783 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2784 {
2785 vim_free(di);
2786 return;
2787 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002789 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002790 else if (op != NULL && *op != '=')
2791 {
2792 tv_op(lp->ll_tv, rettv, op);
2793 return;
2794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 /*
2799 * Assign the value to the variable or list item.
2800 */
2801 if (copy)
2802 copy_tv(rettv, lp->ll_tv);
2803 else
2804 {
2805 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002806 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002808 }
2809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002810}
2811
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002812/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2814 * Returns OK or FAIL.
2815 */
2816 static int
2817tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002818 typval_T *tv1;
2819 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 char_u *op;
2821{
2822 long n;
2823 char_u numbuf[NUMBUFLEN];
2824 char_u *s;
2825
2826 /* Can't do anything with a Funcref or a Dict on the right. */
2827 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2828 {
2829 switch (tv1->v_type)
2830 {
2831 case VAR_DICT:
2832 case VAR_FUNC:
2833 break;
2834
2835 case VAR_LIST:
2836 if (*op != '+' || tv2->v_type != VAR_LIST)
2837 break;
2838 /* List += List */
2839 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2840 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2841 return OK;
2842
2843 case VAR_NUMBER:
2844 case VAR_STRING:
2845 if (tv2->v_type == VAR_LIST)
2846 break;
2847 if (*op == '+' || *op == '-')
2848 {
2849 /* nr += nr or nr -= nr*/
2850 n = get_tv_number(tv1);
2851 if (*op == '+')
2852 n += get_tv_number(tv2);
2853 else
2854 n -= get_tv_number(tv2);
2855 clear_tv(tv1);
2856 tv1->v_type = VAR_NUMBER;
2857 tv1->vval.v_number = n;
2858 }
2859 else
2860 {
2861 /* str .= str */
2862 s = get_tv_string(tv1);
2863 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2864 clear_tv(tv1);
2865 tv1->v_type = VAR_STRING;
2866 tv1->vval.v_string = s;
2867 }
2868 return OK;
2869 }
2870 }
2871
2872 EMSG2(_(e_letwrong), op);
2873 return FAIL;
2874}
2875
2876/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002877 * Add a watcher to a list.
2878 */
2879 static void
2880list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 list_T *l;
2882 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883{
2884 lw->lw_next = l->lv_watch;
2885 l->lv_watch = lw;
2886}
2887
2888/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002889 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002890 * No warning when it isn't found...
2891 */
2892 static void
2893list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 list_T *l;
2895 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002896{
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002898
2899 lwp = &l->lv_watch;
2900 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2901 {
2902 if (lw == lwrem)
2903 {
2904 *lwp = lw->lw_next;
2905 break;
2906 }
2907 lwp = &lw->lw_next;
2908 }
2909}
2910
2911/*
2912 * Just before removing an item from a list: advance watchers to the next
2913 * item.
2914 */
2915 static void
2916list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 list_T *l;
2918 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919{
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002921
2922 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2923 if (lw->lw_item == item)
2924 lw->lw_item = item->li_next;
2925}
2926
2927/*
2928 * Evaluate the expression used in a ":for var in expr" command.
2929 * "arg" points to "var".
2930 * Set "*errp" to TRUE for an error, FALSE otherwise;
2931 * Return a pointer that holds the info. Null when there is an error.
2932 */
2933 void *
2934eval_for_line(arg, errp, nextcmdp, skip)
2935 char_u *arg;
2936 int *errp;
2937 char_u **nextcmdp;
2938 int skip;
2939{
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002942 typval_T tv;
2943 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002944
2945 *errp = TRUE; /* default: there is an error */
2946
Bram Moolenaar33570922005-01-25 22:26:29 +00002947 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002948 if (fi == NULL)
2949 return NULL;
2950
2951 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2952 if (expr == NULL)
2953 return fi;
2954
2955 expr = skipwhite(expr);
2956 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2957 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002958 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 return fi;
2960 }
2961
2962 if (skip)
2963 ++emsg_skip;
2964 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2965 {
2966 *errp = FALSE;
2967 if (!skip)
2968 {
2969 l = tv.vval.v_list;
2970 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002971 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002973 clear_tv(&tv);
2974 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 else
2976 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002977 /* No need to increment the refcount, it's already set for the
2978 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 fi->fi_list = l;
2980 list_add_watch(l, &fi->fi_lw);
2981 fi->fi_lw.lw_item = l->lv_first;
2982 }
2983 }
2984 }
2985 if (skip)
2986 --emsg_skip;
2987
2988 return fi;
2989}
2990
2991/*
2992 * Use the first item in a ":for" list. Advance to the next.
2993 * Assign the values to the variable (list). "arg" points to the first one.
2994 * Return TRUE when a valid item was found, FALSE when at end of list or
2995 * something wrong.
2996 */
2997 int
2998next_for_item(fi_void, arg)
2999 void *fi_void;
3000 char_u *arg;
3001{
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003004 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005
3006 item = fi->fi_lw.lw_item;
3007 if (item == NULL)
3008 result = FALSE;
3009 else
3010 {
3011 fi->fi_lw.lw_item = item->li_next;
3012 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3013 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3014 }
3015 return result;
3016}
3017
3018/*
3019 * Free the structure used to store info used by ":for".
3020 */
3021 void
3022free_for_info(fi_void)
3023 void *fi_void;
3024{
Bram Moolenaar33570922005-01-25 22:26:29 +00003025 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003026
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003027 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003028 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003029 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003030 list_unref(fi->fi_list);
3031 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003032 vim_free(fi);
3033}
3034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3036
3037 void
3038set_context_for_expression(xp, arg, cmdidx)
3039 expand_T *xp;
3040 char_u *arg;
3041 cmdidx_T cmdidx;
3042{
3043 int got_eq = FALSE;
3044 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003047 if (cmdidx == CMD_let)
3048 {
3049 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003050 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003051 {
3052 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003053 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003054 {
3055 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003056 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003057 if (vim_iswhite(*p))
3058 break;
3059 }
3060 return;
3061 }
3062 }
3063 else
3064 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3065 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 while ((xp->xp_pattern = vim_strpbrk(arg,
3067 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3068 {
3069 c = *xp->xp_pattern;
3070 if (c == '&')
3071 {
3072 c = xp->xp_pattern[1];
3073 if (c == '&')
3074 {
3075 ++xp->xp_pattern;
3076 xp->xp_context = cmdidx != CMD_let || got_eq
3077 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3078 }
3079 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003082 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3083 xp->xp_pattern += 2;
3084
3085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
3087 else if (c == '$')
3088 {
3089 /* environment variable */
3090 xp->xp_context = EXPAND_ENV_VARS;
3091 }
3092 else if (c == '=')
3093 {
3094 got_eq = TRUE;
3095 xp->xp_context = EXPAND_EXPRESSION;
3096 }
3097 else if (c == '<'
3098 && xp->xp_context == EXPAND_FUNCTIONS
3099 && vim_strchr(xp->xp_pattern, '(') == NULL)
3100 {
3101 /* Function name can start with "<SNR>" */
3102 break;
3103 }
3104 else if (cmdidx != CMD_let || got_eq)
3105 {
3106 if (c == '"') /* string */
3107 {
3108 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3109 if (c == '\\' && xp->xp_pattern[1] != NUL)
3110 ++xp->xp_pattern;
3111 xp->xp_context = EXPAND_NOTHING;
3112 }
3113 else if (c == '\'') /* literal string */
3114 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003115 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3117 /* skip */ ;
3118 xp->xp_context = EXPAND_NOTHING;
3119 }
3120 else if (c == '|')
3121 {
3122 if (xp->xp_pattern[1] == '|')
3123 {
3124 ++xp->xp_pattern;
3125 xp->xp_context = EXPAND_EXPRESSION;
3126 }
3127 else
3128 xp->xp_context = EXPAND_COMMANDS;
3129 }
3130 else
3131 xp->xp_context = EXPAND_EXPRESSION;
3132 }
3133 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 /* Doesn't look like something valid, expand as an expression
3135 * anyway. */
3136 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 arg = xp->xp_pattern;
3138 if (*arg != NUL)
3139 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3140 /* skip */ ;
3141 }
3142 xp->xp_pattern = arg;
3143}
3144
3145#endif /* FEAT_CMDL_COMPL */
3146
3147/*
3148 * ":1,25call func(arg1, arg2)" function call.
3149 */
3150 void
3151ex_call(eap)
3152 exarg_T *eap;
3153{
3154 char_u *arg = eap->arg;
3155 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003157 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003159 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 linenr_T lnum;
3161 int doesrange;
3162 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003165 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003166 if (fudi.fd_newkey != NULL)
3167 {
3168 /* Still need to give an error message for missing key. */
3169 EMSG2(_(e_dictkey), fudi.fd_newkey);
3170 vim_free(fudi.fd_newkey);
3171 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003172 if (tofree == NULL)
3173 return;
3174
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003175 /* Increase refcount on dictionary, it could get deleted when evaluating
3176 * the arguments. */
3177 if (fudi.fd_dict != NULL)
3178 ++fudi.fd_dict->dv_refcount;
3179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003181 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003182 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaar532c7802005-01-27 14:44:31 +00003184 /* Skip white space to allow ":call func ()". Not good, but required for
3185 * backward compatibility. */
3186 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003187 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 if (*startarg != '(')
3190 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003191 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 goto end;
3193 }
3194
3195 /*
3196 * When skipping, evaluate the function once, to find the end of the
3197 * arguments.
3198 * When the function takes a range, this is discovered after the first
3199 * call, and the loop is broken.
3200 */
3201 if (eap->skip)
3202 {
3203 ++emsg_skip;
3204 lnum = eap->line2; /* do it once, also with an invalid range */
3205 }
3206 else
3207 lnum = eap->line1;
3208 for ( ; lnum <= eap->line2; ++lnum)
3209 {
3210 if (!eap->skip && eap->addr_count > 0)
3211 {
3212 curwin->w_cursor.lnum = lnum;
3213 curwin->w_cursor.col = 0;
3214 }
3215 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003216 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003217 eap->line1, eap->line2, &doesrange,
3218 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
3220 failed = TRUE;
3221 break;
3222 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003223
3224 /* Handle a function returning a Funcref, Dictionary or List. */
3225 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3226 {
3227 failed = TRUE;
3228 break;
3229 }
3230
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003231 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (doesrange || eap->skip)
3233 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003236 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003237 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003238 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (aborting())
3240 break;
3241 }
3242 if (eap->skip)
3243 --emsg_skip;
3244
3245 if (!failed)
3246 {
3247 /* Check for trailing illegal characters and a following command. */
3248 if (!ends_excmd(*arg))
3249 {
3250 emsg_severe = TRUE;
3251 EMSG(_(e_trailing));
3252 }
3253 else
3254 eap->nextcmd = check_nextcmd(arg);
3255 }
3256
3257end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003258 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003259 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260}
3261
3262/*
3263 * ":unlet[!] var1 ... " command.
3264 */
3265 void
3266ex_unlet(eap)
3267 exarg_T *eap;
3268{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003269 ex_unletlock(eap, eap->arg, 0);
3270}
3271
3272/*
3273 * ":lockvar" and ":unlockvar" commands
3274 */
3275 void
3276ex_lockvar(eap)
3277 exarg_T *eap;
3278{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003280 int deep = 2;
3281
3282 if (eap->forceit)
3283 deep = -1;
3284 else if (vim_isdigit(*arg))
3285 {
3286 deep = getdigits(&arg);
3287 arg = skipwhite(arg);
3288 }
3289
3290 ex_unletlock(eap, arg, deep);
3291}
3292
3293/*
3294 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3295 */
3296 static void
3297ex_unletlock(eap, argstart, deep)
3298 exarg_T *eap;
3299 char_u *argstart;
3300 int deep;
3301{
3302 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003305 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 do
3308 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003309 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003310 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3311 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003312 if (lv.ll_name == NULL)
3313 error = TRUE; /* error but continue parsing */
3314 if (name_end == NULL || (!vim_iswhite(*name_end)
3315 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003317 if (name_end != NULL)
3318 {
3319 emsg_severe = TRUE;
3320 EMSG(_(e_trailing));
3321 }
3322 if (!(eap->skip || error))
3323 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 break;
3325 }
3326
3327 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003328 {
3329 if (eap->cmdidx == CMD_unlet)
3330 {
3331 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3332 error = TRUE;
3333 }
3334 else
3335 {
3336 if (do_lock_var(&lv, name_end, deep,
3337 eap->cmdidx == CMD_lockvar) == FAIL)
3338 error = TRUE;
3339 }
3340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003342 if (!eap->skip)
3343 clear_lval(&lv);
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 arg = skipwhite(name_end);
3346 } while (!ends_excmd(*arg));
3347
3348 eap->nextcmd = check_nextcmd(arg);
3349}
3350
Bram Moolenaar8c711452005-01-14 21:53:12 +00003351 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003352do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003353 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003354 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 int forceit;
3356{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003357 int ret = OK;
3358 int cc;
3359
3360 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003361 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003362 cc = *name_end;
3363 *name_end = NUL;
3364
3365 /* Normal name or expanded name. */
3366 if (check_changedtick(lp->ll_name))
3367 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003368 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003369 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003370 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003371 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003372 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3373 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003374 else if (lp->ll_range)
3375 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003376 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003377
3378 /* Delete a range of List items. */
3379 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3380 {
3381 li = lp->ll_li->li_next;
3382 listitem_remove(lp->ll_list, lp->ll_li);
3383 lp->ll_li = li;
3384 ++lp->ll_n1;
3385 }
3386 }
3387 else
3388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003389 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003390 /* unlet a List item. */
3391 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003392 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003393 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003394 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003395 }
3396
3397 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003398}
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
3401 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003402 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 */
3404 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003405do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003407 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
Bram Moolenaar33570922005-01-25 22:26:29 +00003409 hashtab_T *ht;
3410 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003411 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003412 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaar33570922005-01-25 22:26:29 +00003414 ht = find_var_ht(name, &varname);
3415 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003417 hi = hash_find(ht, varname);
3418 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003419 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003420 di = HI2DI(hi);
3421 if (var_check_fixed(di->di_flags, name)
3422 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003423 return FAIL;
3424 delete_var(ht, hi);
3425 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003428 if (forceit)
3429 return OK;
3430 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 return FAIL;
3432}
3433
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003434/*
3435 * Lock or unlock variable indicated by "lp".
3436 * "deep" is the levels to go (-1 for unlimited);
3437 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3438 */
3439 static int
3440do_lock_var(lp, name_end, deep, lock)
3441 lval_T *lp;
3442 char_u *name_end;
3443 int deep;
3444 int lock;
3445{
3446 int ret = OK;
3447 int cc;
3448 dictitem_T *di;
3449
3450 if (deep == 0) /* nothing to do */
3451 return OK;
3452
3453 if (lp->ll_tv == NULL)
3454 {
3455 cc = *name_end;
3456 *name_end = NUL;
3457
3458 /* Normal name or expanded name. */
3459 if (check_changedtick(lp->ll_name))
3460 ret = FAIL;
3461 else
3462 {
3463 di = find_var(lp->ll_name, NULL);
3464 if (di == NULL)
3465 ret = FAIL;
3466 else
3467 {
3468 if (lock)
3469 di->di_flags |= DI_FLAGS_LOCK;
3470 else
3471 di->di_flags &= ~DI_FLAGS_LOCK;
3472 item_lock(&di->di_tv, deep, lock);
3473 }
3474 }
3475 *name_end = cc;
3476 }
3477 else if (lp->ll_range)
3478 {
3479 listitem_T *li = lp->ll_li;
3480
3481 /* (un)lock a range of List items. */
3482 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3483 {
3484 item_lock(&li->li_tv, deep, lock);
3485 li = li->li_next;
3486 ++lp->ll_n1;
3487 }
3488 }
3489 else if (lp->ll_list != NULL)
3490 /* (un)lock a List item. */
3491 item_lock(&lp->ll_li->li_tv, deep, lock);
3492 else
3493 /* un(lock) a Dictionary item. */
3494 item_lock(&lp->ll_di->di_tv, deep, lock);
3495
3496 return ret;
3497}
3498
3499/*
3500 * Lock or unlock an item. "deep" is nr of levels to go.
3501 */
3502 static void
3503item_lock(tv, deep, lock)
3504 typval_T *tv;
3505 int deep;
3506 int lock;
3507{
3508 static int recurse = 0;
3509 list_T *l;
3510 listitem_T *li;
3511 dict_T *d;
3512 hashitem_T *hi;
3513 int todo;
3514
3515 if (recurse >= DICT_MAXNEST)
3516 {
3517 EMSG(_("E743: variable nested too deep for (un)lock"));
3518 return;
3519 }
3520 if (deep == 0)
3521 return;
3522 ++recurse;
3523
3524 /* lock/unlock the item itself */
3525 if (lock)
3526 tv->v_lock |= VAR_LOCKED;
3527 else
3528 tv->v_lock &= ~VAR_LOCKED;
3529
3530 switch (tv->v_type)
3531 {
3532 case VAR_LIST:
3533 if ((l = tv->vval.v_list) != NULL)
3534 {
3535 if (lock)
3536 l->lv_lock |= VAR_LOCKED;
3537 else
3538 l->lv_lock &= ~VAR_LOCKED;
3539 if (deep < 0 || deep > 1)
3540 /* recursive: lock/unlock the items the List contains */
3541 for (li = l->lv_first; li != NULL; li = li->li_next)
3542 item_lock(&li->li_tv, deep - 1, lock);
3543 }
3544 break;
3545 case VAR_DICT:
3546 if ((d = tv->vval.v_dict) != NULL)
3547 {
3548 if (lock)
3549 d->dv_lock |= VAR_LOCKED;
3550 else
3551 d->dv_lock &= ~VAR_LOCKED;
3552 if (deep < 0 || deep > 1)
3553 {
3554 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003555 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003556 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3557 {
3558 if (!HASHITEM_EMPTY(hi))
3559 {
3560 --todo;
3561 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3562 }
3563 }
3564 }
3565 }
3566 }
3567 --recurse;
3568}
3569
Bram Moolenaara40058a2005-07-11 22:42:07 +00003570/*
3571 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3572 * it refers to a List or Dictionary that is locked.
3573 */
3574 static int
3575tv_islocked(tv)
3576 typval_T *tv;
3577{
3578 return (tv->v_lock & VAR_LOCKED)
3579 || (tv->v_type == VAR_LIST
3580 && tv->vval.v_list != NULL
3581 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3582 || (tv->v_type == VAR_DICT
3583 && tv->vval.v_dict != NULL
3584 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3585}
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3588/*
3589 * Delete all "menutrans_" variables.
3590 */
3591 void
3592del_menutrans_vars()
3593{
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003595 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003598 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003599 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003600 {
3601 if (!HASHITEM_EMPTY(hi))
3602 {
3603 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3605 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003606 }
3607 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609}
3610#endif
3611
3612#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3613
3614/*
3615 * Local string buffer for the next two functions to store a variable name
3616 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3617 * get_user_var_name().
3618 */
3619
3620static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3621
3622static char_u *varnamebuf = NULL;
3623static int varnamebuflen = 0;
3624
3625/*
3626 * Function to concatenate a prefix and a variable name.
3627 */
3628 static char_u *
3629cat_prefix_varname(prefix, name)
3630 int prefix;
3631 char_u *name;
3632{
3633 int len;
3634
3635 len = (int)STRLEN(name) + 3;
3636 if (len > varnamebuflen)
3637 {
3638 vim_free(varnamebuf);
3639 len += 10; /* some additional space */
3640 varnamebuf = alloc(len);
3641 if (varnamebuf == NULL)
3642 {
3643 varnamebuflen = 0;
3644 return NULL;
3645 }
3646 varnamebuflen = len;
3647 }
3648 *varnamebuf = prefix;
3649 varnamebuf[1] = ':';
3650 STRCPY(varnamebuf + 2, name);
3651 return varnamebuf;
3652}
3653
3654/*
3655 * Function given to ExpandGeneric() to obtain the list of user defined
3656 * (global/buffer/window/built-in) variable names.
3657 */
3658/*ARGSUSED*/
3659 char_u *
3660get_user_var_name(xp, idx)
3661 expand_T *xp;
3662 int idx;
3663{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003664 static long_u gdone;
3665 static long_u bdone;
3666 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003667#ifdef FEAT_WINDOWS
3668 static long_u tdone;
3669#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003670 static int vidx;
3671 static hashitem_T *hi;
3672 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
3674 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003675 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003676 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003677#ifdef FEAT_WINDOWS
3678 tdone = 0;
3679#endif
3680 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003681
3682 /* Global variables */
3683 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003685 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003687 else
3688 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003689 while (HASHITEM_EMPTY(hi))
3690 ++hi;
3691 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3692 return cat_prefix_varname('g', hi->hi_key);
3693 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003695
3696 /* b: variables */
3697 ht = &curbuf->b_vars.dv_hashtab;
3698 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003700 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003701 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003702 else
3703 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003704 while (HASHITEM_EMPTY(hi))
3705 ++hi;
3706 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003708 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003710 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 return (char_u *)"b:changedtick";
3712 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003713
3714 /* w: variables */
3715 ht = &curwin->w_vars.dv_hashtab;
3716 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003718 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003719 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003720 else
3721 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003722 while (HASHITEM_EMPTY(hi))
3723 ++hi;
3724 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003726
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003727#ifdef FEAT_WINDOWS
3728 /* t: variables */
3729 ht = &curtab->tp_vars.dv_hashtab;
3730 if (tdone < ht->ht_used)
3731 {
3732 if (tdone++ == 0)
3733 hi = ht->ht_array;
3734 else
3735 ++hi;
3736 while (HASHITEM_EMPTY(hi))
3737 ++hi;
3738 return cat_prefix_varname('t', hi->hi_key);
3739 }
3740#endif
3741
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 /* v: variables */
3743 if (vidx < VV_LEN)
3744 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
3746 vim_free(varnamebuf);
3747 varnamebuf = NULL;
3748 varnamebuflen = 0;
3749 return NULL;
3750}
3751
3752#endif /* FEAT_CMDL_COMPL */
3753
3754/*
3755 * types for expressions.
3756 */
3757typedef enum
3758{
3759 TYPE_UNKNOWN = 0
3760 , TYPE_EQUAL /* == */
3761 , TYPE_NEQUAL /* != */
3762 , TYPE_GREATER /* > */
3763 , TYPE_GEQUAL /* >= */
3764 , TYPE_SMALLER /* < */
3765 , TYPE_SEQUAL /* <= */
3766 , TYPE_MATCH /* =~ */
3767 , TYPE_NOMATCH /* !~ */
3768} exptype_T;
3769
3770/*
3771 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003772 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3774 */
3775
3776/*
3777 * Handle zero level expression.
3778 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003779 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003780 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 * Return OK or FAIL.
3782 */
3783 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 char_u **nextcmd;
3788 int evaluate;
3789{
3790 int ret;
3791 char_u *p;
3792
3793 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (ret == FAIL || !ends_excmd(*p))
3796 {
3797 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003798 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 /*
3800 * Report the invalid expression unless the expression evaluation has
3801 * been cancelled due to an aborting error, an interrupt, or an
3802 * exception.
3803 */
3804 if (!aborting())
3805 EMSG2(_(e_invexpr2), arg);
3806 ret = FAIL;
3807 }
3808 if (nextcmd != NULL)
3809 *nextcmd = check_nextcmd(p);
3810
3811 return ret;
3812}
3813
3814/*
3815 * Handle top level expression:
3816 * expr1 ? expr0 : expr0
3817 *
3818 * "arg" must point to the first non-white of the expression.
3819 * "arg" is advanced to the next non-white after the recognized expression.
3820 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003821 * Note: "rettv.v_lock" is not set.
3822 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 * Return OK or FAIL.
3824 */
3825 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int evaluate;
3830{
3831 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003832 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 /*
3835 * Get the first variable.
3836 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 return FAIL;
3839
3840 if ((*arg)[0] == '?')
3841 {
3842 result = FALSE;
3843 if (evaluate)
3844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003845 int error = FALSE;
3846
3847 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003849 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003850 if (error)
3851 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
3853
3854 /*
3855 * Get the second variable.
3856 */
3857 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003858 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 return FAIL;
3860
3861 /*
3862 * Check for the ":".
3863 */
3864 if ((*arg)[0] != ':')
3865 {
3866 EMSG(_("E109: Missing ':' after '?'"));
3867 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return FAIL;
3870 }
3871
3872 /*
3873 * Get the third variable.
3874 */
3875 *arg = skipwhite(*arg + 1);
3876 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3877 {
3878 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return FAIL;
3881 }
3882 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003883 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885
3886 return OK;
3887}
3888
3889/*
3890 * Handle first level expression:
3891 * expr2 || expr2 || expr2 logical OR
3892 *
3893 * "arg" must point to the first non-white of the expression.
3894 * "arg" is advanced to the next non-white after the recognized expression.
3895 *
3896 * Return OK or FAIL.
3897 */
3898 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 int evaluate;
3903{
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 long result;
3906 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003907 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
3909 /*
3910 * Get the first variable.
3911 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003912 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 return FAIL;
3914
3915 /*
3916 * Repeat until there is no following "||".
3917 */
3918 first = TRUE;
3919 result = FALSE;
3920 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3921 {
3922 if (evaluate && first)
3923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003924 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003927 if (error)
3928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 first = FALSE;
3930 }
3931
3932 /*
3933 * Get the second variable.
3934 */
3935 *arg = skipwhite(*arg + 2);
3936 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3937 return FAIL;
3938
3939 /*
3940 * Compute the result.
3941 */
3942 if (evaluate && !result)
3943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003944 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003946 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003947 if (error)
3948 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 if (evaluate)
3951 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003952 rettv->v_type = VAR_NUMBER;
3953 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955 }
3956
3957 return OK;
3958}
3959
3960/*
3961 * Handle second level expression:
3962 * expr3 && expr3 && expr3 logical AND
3963 *
3964 * "arg" must point to the first non-white of the expression.
3965 * "arg" is advanced to the next non-white after the recognized expression.
3966 *
3967 * Return OK or FAIL.
3968 */
3969 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003970eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 int evaluate;
3974{
Bram Moolenaar33570922005-01-25 22:26:29 +00003975 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 long result;
3977 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003978 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980 /*
3981 * Get the first variable.
3982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003983 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 return FAIL;
3985
3986 /*
3987 * Repeat until there is no following "&&".
3988 */
3989 first = TRUE;
3990 result = TRUE;
3991 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3992 {
3993 if (evaluate && first)
3994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003995 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003998 if (error)
3999 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 first = FALSE;
4001 }
4002
4003 /*
4004 * Get the second variable.
4005 */
4006 *arg = skipwhite(*arg + 2);
4007 if (eval4(arg, &var2, evaluate && result) == FAIL)
4008 return FAIL;
4009
4010 /*
4011 * Compute the result.
4012 */
4013 if (evaluate && result)
4014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004017 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004018 if (error)
4019 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021 if (evaluate)
4022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 rettv->v_type = VAR_NUMBER;
4024 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026 }
4027
4028 return OK;
4029}
4030
4031/*
4032 * Handle third level expression:
4033 * var1 == var2
4034 * var1 =~ var2
4035 * var1 != var2
4036 * var1 !~ var2
4037 * var1 > var2
4038 * var1 >= var2
4039 * var1 < var2
4040 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004041 * var1 is var2
4042 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 *
4044 * "arg" must point to the first non-white of the expression.
4045 * "arg" is advanced to the next non-white after the recognized expression.
4046 *
4047 * Return OK or FAIL.
4048 */
4049 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004050eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 int evaluate;
4054{
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u *p;
4057 int i;
4058 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004059 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 int len = 2;
4061 long n1, n2;
4062 char_u *s1, *s2;
4063 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4064 regmatch_T regmatch;
4065 int ic;
4066 char_u *save_cpo;
4067
4068 /*
4069 * Get the first variable.
4070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004071 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 return FAIL;
4073
4074 p = *arg;
4075 switch (p[0])
4076 {
4077 case '=': if (p[1] == '=')
4078 type = TYPE_EQUAL;
4079 else if (p[1] == '~')
4080 type = TYPE_MATCH;
4081 break;
4082 case '!': if (p[1] == '=')
4083 type = TYPE_NEQUAL;
4084 else if (p[1] == '~')
4085 type = TYPE_NOMATCH;
4086 break;
4087 case '>': if (p[1] != '=')
4088 {
4089 type = TYPE_GREATER;
4090 len = 1;
4091 }
4092 else
4093 type = TYPE_GEQUAL;
4094 break;
4095 case '<': if (p[1] != '=')
4096 {
4097 type = TYPE_SMALLER;
4098 len = 1;
4099 }
4100 else
4101 type = TYPE_SEQUAL;
4102 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004103 case 'i': if (p[1] == 's')
4104 {
4105 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4106 len = 5;
4107 if (!vim_isIDc(p[len]))
4108 {
4109 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4110 type_is = TRUE;
4111 }
4112 }
4113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115
4116 /*
4117 * If there is a comparitive operator, use it.
4118 */
4119 if (type != TYPE_UNKNOWN)
4120 {
4121 /* extra question mark appended: ignore case */
4122 if (p[len] == '?')
4123 {
4124 ic = TRUE;
4125 ++len;
4126 }
4127 /* extra '#' appended: match case */
4128 else if (p[len] == '#')
4129 {
4130 ic = FALSE;
4131 ++len;
4132 }
4133 /* nothing appened: use 'ignorecase' */
4134 else
4135 ic = p_ic;
4136
4137 /*
4138 * Get the second variable.
4139 */
4140 *arg = skipwhite(p + len);
4141 if (eval5(arg, &var2, evaluate) == FAIL)
4142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return FAIL;
4145 }
4146
4147 if (evaluate)
4148 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004149 if (type_is && rettv->v_type != var2.v_type)
4150 {
4151 /* For "is" a different type always means FALSE, for "notis"
4152 * it means TRUE. */
4153 n1 = (type == TYPE_NEQUAL);
4154 }
4155 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4156 {
4157 if (type_is)
4158 {
4159 n1 = (rettv->v_type == var2.v_type
4160 && rettv->vval.v_list == var2.vval.v_list);
4161 if (type == TYPE_NEQUAL)
4162 n1 = !n1;
4163 }
4164 else if (rettv->v_type != var2.v_type
4165 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4166 {
4167 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004168 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004169 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004170 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004171 clear_tv(rettv);
4172 clear_tv(&var2);
4173 return FAIL;
4174 }
4175 else
4176 {
4177 /* Compare two Lists for being equal or unequal. */
4178 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4179 if (type == TYPE_NEQUAL)
4180 n1 = !n1;
4181 }
4182 }
4183
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004184 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4185 {
4186 if (type_is)
4187 {
4188 n1 = (rettv->v_type == var2.v_type
4189 && rettv->vval.v_dict == var2.vval.v_dict);
4190 if (type == TYPE_NEQUAL)
4191 n1 = !n1;
4192 }
4193 else if (rettv->v_type != var2.v_type
4194 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4195 {
4196 if (rettv->v_type != var2.v_type)
4197 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4198 else
4199 EMSG(_("E736: Invalid operation for Dictionary"));
4200 clear_tv(rettv);
4201 clear_tv(&var2);
4202 return FAIL;
4203 }
4204 else
4205 {
4206 /* Compare two Dictionaries for being equal or unequal. */
4207 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4208 if (type == TYPE_NEQUAL)
4209 n1 = !n1;
4210 }
4211 }
4212
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004213 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4214 {
4215 if (rettv->v_type != var2.v_type
4216 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4217 {
4218 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004219 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004221 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004222 clear_tv(rettv);
4223 clear_tv(&var2);
4224 return FAIL;
4225 }
4226 else
4227 {
4228 /* Compare two Funcrefs for being equal or unequal. */
4229 if (rettv->vval.v_string == NULL
4230 || var2.vval.v_string == NULL)
4231 n1 = FALSE;
4232 else
4233 n1 = STRCMP(rettv->vval.v_string,
4234 var2.vval.v_string) == 0;
4235 if (type == TYPE_NEQUAL)
4236 n1 = !n1;
4237 }
4238 }
4239
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 /*
4241 * If one of the two variables is a number, compare as a number.
4242 * When using "=~" or "!~", always compare as string.
4243 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004244 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 n1 = get_tv_number(rettv);
4248 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 switch (type)
4250 {
4251 case TYPE_EQUAL: n1 = (n1 == n2); break;
4252 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4253 case TYPE_GREATER: n1 = (n1 > n2); break;
4254 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4255 case TYPE_SMALLER: n1 = (n1 < n2); break;
4256 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4257 case TYPE_UNKNOWN:
4258 case TYPE_MATCH:
4259 case TYPE_NOMATCH: break; /* avoid gcc warning */
4260 }
4261 }
4262 else
4263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 s1 = get_tv_string_buf(rettv, buf1);
4265 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4267 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4268 else
4269 i = 0;
4270 n1 = FALSE;
4271 switch (type)
4272 {
4273 case TYPE_EQUAL: n1 = (i == 0); break;
4274 case TYPE_NEQUAL: n1 = (i != 0); break;
4275 case TYPE_GREATER: n1 = (i > 0); break;
4276 case TYPE_GEQUAL: n1 = (i >= 0); break;
4277 case TYPE_SMALLER: n1 = (i < 0); break;
4278 case TYPE_SEQUAL: n1 = (i <= 0); break;
4279
4280 case TYPE_MATCH:
4281 case TYPE_NOMATCH:
4282 /* avoid 'l' flag in 'cpoptions' */
4283 save_cpo = p_cpo;
4284 p_cpo = (char_u *)"";
4285 regmatch.regprog = vim_regcomp(s2,
4286 RE_MAGIC + RE_STRING);
4287 regmatch.rm_ic = ic;
4288 if (regmatch.regprog != NULL)
4289 {
4290 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4291 vim_free(regmatch.regprog);
4292 if (type == TYPE_NOMATCH)
4293 n1 = !n1;
4294 }
4295 p_cpo = save_cpo;
4296 break;
4297
4298 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4299 }
4300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 clear_tv(rettv);
4302 clear_tv(&var2);
4303 rettv->v_type = VAR_NUMBER;
4304 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 }
4307
4308 return OK;
4309}
4310
4311/*
4312 * Handle fourth level expression:
4313 * + number addition
4314 * - number subtraction
4315 * . string concatenation
4316 *
4317 * "arg" must point to the first non-white of the expression.
4318 * "arg" is advanced to the next non-white after the recognized expression.
4319 *
4320 * Return OK or FAIL.
4321 */
4322 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int evaluate;
4327{
Bram Moolenaar33570922005-01-25 22:26:29 +00004328 typval_T var2;
4329 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 int op;
4331 long n1, n2;
4332 char_u *s1, *s2;
4333 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4334 char_u *p;
4335
4336 /*
4337 * Get the first variable.
4338 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 return FAIL;
4341
4342 /*
4343 * Repeat computing, until no '+', '-' or '.' is following.
4344 */
4345 for (;;)
4346 {
4347 op = **arg;
4348 if (op != '+' && op != '-' && op != '.')
4349 break;
4350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (op != '+' || rettv->v_type != VAR_LIST)
4352 {
4353 /* For "list + ...", an illegal use of the first operand as
4354 * a number cannot be determined before evaluating the 2nd
4355 * operand: if this is also a list, all is ok.
4356 * For "something . ...", "something - ..." or "non-list + ...",
4357 * we know that the first operand needs to be a string or number
4358 * without evaluating the 2nd operand. So check before to avoid
4359 * side effects after an error. */
4360 if (evaluate && get_tv_string_chk(rettv) == NULL)
4361 {
4362 clear_tv(rettv);
4363 return FAIL;
4364 }
4365 }
4366
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 /*
4368 * Get the second variable.
4369 */
4370 *arg = skipwhite(*arg + 1);
4371 if (eval6(arg, &var2, evaluate) == FAIL)
4372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004373 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 return FAIL;
4375 }
4376
4377 if (evaluate)
4378 {
4379 /*
4380 * Compute the result.
4381 */
4382 if (op == '.')
4383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004384 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4385 s2 = get_tv_string_buf_chk(&var2, buf2);
4386 if (s2 == NULL) /* type error ? */
4387 {
4388 clear_tv(rettv);
4389 clear_tv(&var2);
4390 return FAIL;
4391 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004392 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 clear_tv(rettv);
4394 rettv->v_type = VAR_STRING;
4395 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004397 else if (op == '+' && rettv->v_type == VAR_LIST
4398 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 {
4400 /* concatenate Lists */
4401 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4402 &var3) == FAIL)
4403 {
4404 clear_tv(rettv);
4405 clear_tv(&var2);
4406 return FAIL;
4407 }
4408 clear_tv(rettv);
4409 *rettv = var3;
4410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 else
4412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004413 int error = FALSE;
4414
4415 n1 = get_tv_number_chk(rettv, &error);
4416 if (error)
4417 {
4418 /* This can only happen for "list + non-list".
4419 * For "non-list + ..." or "something - ...", we returned
4420 * before evaluating the 2nd operand. */
4421 clear_tv(rettv);
4422 return FAIL;
4423 }
4424 n2 = get_tv_number_chk(&var2, &error);
4425 if (error)
4426 {
4427 clear_tv(rettv);
4428 clear_tv(&var2);
4429 return FAIL;
4430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 if (op == '+')
4433 n1 = n1 + n2;
4434 else
4435 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 rettv->v_type = VAR_NUMBER;
4437 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004439 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 }
4442 return OK;
4443}
4444
4445/*
4446 * Handle fifth level expression:
4447 * * number multiplication
4448 * / number division
4449 * % number modulo
4450 *
4451 * "arg" must point to the first non-white of the expression.
4452 * "arg" is advanced to the next non-white after the recognized expression.
4453 *
4454 * Return OK or FAIL.
4455 */
4456 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 int evaluate;
4461{
Bram Moolenaar33570922005-01-25 22:26:29 +00004462 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 int op;
4464 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004465 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 /*
4468 * Get the first variable.
4469 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 return FAIL;
4472
4473 /*
4474 * Repeat computing, until no '*', '/' or '%' is following.
4475 */
4476 for (;;)
4477 {
4478 op = **arg;
4479 if (op != '*' && op != '/' && op != '%')
4480 break;
4481
4482 if (evaluate)
4483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004484 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004485 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004486 if (error)
4487 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 else
4490 n1 = 0;
4491
4492 /*
4493 * Get the second variable.
4494 */
4495 *arg = skipwhite(*arg + 1);
4496 if (eval7(arg, &var2, evaluate) == FAIL)
4497 return FAIL;
4498
4499 if (evaluate)
4500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004501 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004502 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004503 if (error)
4504 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
4506 /*
4507 * Compute the result.
4508 */
4509 if (op == '*')
4510 n1 = n1 * n2;
4511 else if (op == '/')
4512 {
4513 if (n2 == 0) /* give an error message? */
4514 n1 = 0x7fffffffL;
4515 else
4516 n1 = n1 / n2;
4517 }
4518 else
4519 {
4520 if (n2 == 0) /* give an error message? */
4521 n1 = 0;
4522 else
4523 n1 = n1 % n2;
4524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004525 rettv->v_type = VAR_NUMBER;
4526 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 }
4529
4530 return OK;
4531}
4532
4533/*
4534 * Handle sixth level expression:
4535 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004536 * "string" string constant
4537 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 * &option-name option value
4539 * @r register contents
4540 * identifier variable value
4541 * function() function call
4542 * $VAR environment variable
4543 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004544 * [expr, expr] List
4545 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 *
4547 * Also handle:
4548 * ! in front logical NOT
4549 * - in front unary minus
4550 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 * trailing [] subscript in String or List
4552 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 *
4554 * "arg" must point to the first non-white of the expression.
4555 * "arg" is advanced to the next non-white after the recognized expression.
4556 *
4557 * Return OK or FAIL.
4558 */
4559 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004560eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 int evaluate;
4564{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 long n;
4566 int len;
4567 char_u *s;
4568 int val;
4569 char_u *start_leader, *end_leader;
4570 int ret = OK;
4571 char_u *alias;
4572
4573 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004575 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
4579 /*
4580 * Skip '!' and '-' characters. They are handled later.
4581 */
4582 start_leader = *arg;
4583 while (**arg == '!' || **arg == '-' || **arg == '+')
4584 *arg = skipwhite(*arg + 1);
4585 end_leader = *arg;
4586
4587 switch (**arg)
4588 {
4589 /*
4590 * Number constant.
4591 */
4592 case '0':
4593 case '1':
4594 case '2':
4595 case '3':
4596 case '4':
4597 case '5':
4598 case '6':
4599 case '7':
4600 case '8':
4601 case '9':
4602 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4603 *arg += len;
4604 if (evaluate)
4605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004606 rettv->v_type = VAR_NUMBER;
4607 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609 break;
4610
4611 /*
4612 * String constant: "string".
4613 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 break;
4616
4617 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004618 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 break;
4622
4623 /*
4624 * List: [expr, expr]
4625 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 break;
4628
4629 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630 * Dictionary: {key: val, key: val}
4631 */
4632 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4633 break;
4634
4635 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004636 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004638 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 break;
4640
4641 /*
4642 * Environment variable: $VAR.
4643 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 break;
4646
4647 /*
4648 * Register contents: @r.
4649 */
4650 case '@': ++*arg;
4651 if (evaluate)
4652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004653 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004654 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 if (**arg != NUL)
4657 ++*arg;
4658 break;
4659
4660 /*
4661 * nested expression: (expression).
4662 */
4663 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (**arg == ')')
4666 ++*arg;
4667 else if (ret == OK)
4668 {
4669 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 ret = FAIL;
4672 }
4673 break;
4674
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 break;
4677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678
4679 if (ret == NOTDONE)
4680 {
4681 /*
4682 * Must be a variable or function name.
4683 * Can also be a curly-braces kind of name: {expr}.
4684 */
4685 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004686 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 if (alias != NULL)
4688 s = alias;
4689
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004690 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691 ret = FAIL;
4692 else
4693 {
4694 if (**arg == '(') /* recursive! */
4695 {
4696 /* If "s" is the name of a variable of type VAR_FUNC
4697 * use its contents. */
4698 s = deref_func_name(s, &len);
4699
4700 /* Invoke the function. */
4701 ret = get_func_tv(s, len, rettv, arg,
4702 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004703 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 /* Stop the expression evaluation when immediately
4705 * aborting on error, or when an interrupt occurred or
4706 * an exception was thrown but not caught. */
4707 if (aborting())
4708 {
4709 if (ret == OK)
4710 clear_tv(rettv);
4711 ret = FAIL;
4712 }
4713 }
4714 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004715 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004716 else
4717 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718 }
4719
4720 if (alias != NULL)
4721 vim_free(alias);
4722 }
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 *arg = skipwhite(*arg);
4725
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004726 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4727 * expr(expr). */
4728 if (ret == OK)
4729 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
4731 /*
4732 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4733 */
4734 if (ret == OK && evaluate && end_leader > start_leader)
4735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004736 int error = FALSE;
4737
4738 val = get_tv_number_chk(rettv, &error);
4739 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 clear_tv(rettv);
4742 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 else
4745 {
4746 while (end_leader > start_leader)
4747 {
4748 --end_leader;
4749 if (*end_leader == '!')
4750 val = !val;
4751 else if (*end_leader == '-')
4752 val = -val;
4753 }
4754 clear_tv(rettv);
4755 rettv->v_type = VAR_NUMBER;
4756 rettv->vval.v_number = val;
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
4759
4760 return ret;
4761}
4762
4763/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004764 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4765 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4767 */
4768 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004769eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004771 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004773 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774{
4775 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004776 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004778 long len = -1;
4779 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004785 if (verbose)
4786 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 return FAIL;
4788 }
4789
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004792 /*
4793 * dict.name
4794 */
4795 key = *arg + 1;
4796 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4797 ;
4798 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 }
4802 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804 /*
4805 * something[idx]
4806 *
4807 * Get the (first) variable from inside the [].
4808 */
4809 *arg = skipwhite(*arg + 1);
4810 if (**arg == ':')
4811 empty1 = TRUE;
4812 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4813 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4815 {
4816 /* not a number or string */
4817 clear_tv(&var1);
4818 return FAIL;
4819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820
4821 /*
4822 * Get the second variable from inside the [:].
4823 */
4824 if (**arg == ':')
4825 {
4826 range = TRUE;
4827 *arg = skipwhite(*arg + 1);
4828 if (**arg == ']')
4829 empty2 = TRUE;
4830 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004832 if (!empty1)
4833 clear_tv(&var1);
4834 return FAIL;
4835 }
4836 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4837 {
4838 /* not a number or string */
4839 if (!empty1)
4840 clear_tv(&var1);
4841 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004842 return FAIL;
4843 }
4844 }
4845
4846 /* Check for the ']'. */
4847 if (**arg != ']')
4848 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004849 if (verbose)
4850 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004851 clear_tv(&var1);
4852 if (range)
4853 clear_tv(&var2);
4854 return FAIL;
4855 }
4856 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004857 }
4858
4859 if (evaluate)
4860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004861 n1 = 0;
4862 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 n1 = get_tv_number(&var1);
4865 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004866 }
4867 if (range)
4868 {
4869 if (empty2)
4870 n2 = -1;
4871 else
4872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 n2 = get_tv_number(&var2);
4874 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 }
4876 }
4877
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 {
4880 case VAR_NUMBER:
4881 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883 len = (long)STRLEN(s);
4884 if (range)
4885 {
4886 /* The resulting variable is a substring. If the indexes
4887 * are out of range the result is empty. */
4888 if (n1 < 0)
4889 {
4890 n1 = len + n1;
4891 if (n1 < 0)
4892 n1 = 0;
4893 }
4894 if (n2 < 0)
4895 n2 = len + n2;
4896 else if (n2 >= len)
4897 n2 = len;
4898 if (n1 >= len || n2 < 0 || n1 > n2)
4899 s = NULL;
4900 else
4901 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4902 }
4903 else
4904 {
4905 /* The resulting variable is a string of a single
4906 * character. If the index is too big or negative the
4907 * result is empty. */
4908 if (n1 >= len || n1 < 0)
4909 s = NULL;
4910 else
4911 s = vim_strnsave(s + n1, 1);
4912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 clear_tv(rettv);
4914 rettv->v_type = VAR_STRING;
4915 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916 break;
4917
4918 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004919 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004920 if (n1 < 0)
4921 n1 = len + n1;
4922 if (!empty1 && (n1 < 0 || n1 >= len))
4923 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004924 /* For a range we allow invalid values and return an empty
4925 * list. A list index out of range is an error. */
4926 if (!range)
4927 {
4928 if (verbose)
4929 EMSGN(_(e_listidx), n1);
4930 return FAIL;
4931 }
4932 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004933 }
4934 if (range)
4935 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004936 list_T *l;
4937 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004938
4939 if (n2 < 0)
4940 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004941 else if (n2 >= len)
4942 n2 = len - 1;
4943 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004944 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 l = list_alloc();
4946 if (l == NULL)
4947 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004948 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004949 n1 <= n2; ++n1)
4950 {
4951 if (list_append_tv(l, &item->li_tv) == FAIL)
4952 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004953 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954 return FAIL;
4955 }
4956 item = item->li_next;
4957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004958 clear_tv(rettv);
4959 rettv->v_type = VAR_LIST;
4960 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004961 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004962 }
4963 else
4964 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004965 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004966 clear_tv(rettv);
4967 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004968 }
4969 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970
4971 case VAR_DICT:
4972 if (range)
4973 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004974 if (verbose)
4975 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 if (len == -1)
4977 clear_tv(&var1);
4978 return FAIL;
4979 }
4980 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004981 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982
4983 if (len == -1)
4984 {
4985 key = get_tv_string(&var1);
4986 if (*key == NUL)
4987 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004988 if (verbose)
4989 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 clear_tv(&var1);
4991 return FAIL;
4992 }
4993 }
4994
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004995 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004997 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004998 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 if (len == -1)
5000 clear_tv(&var1);
5001 if (item == NULL)
5002 return FAIL;
5003
5004 copy_tv(&item->di_tv, &var1);
5005 clear_tv(rettv);
5006 *rettv = var1;
5007 }
5008 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005009 }
5010 }
5011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005012 return OK;
5013}
5014
5015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 * Get an option value.
5017 * "arg" points to the '&' or '+' before the option name.
5018 * "arg" is advanced to character after the option name.
5019 * Return OK or FAIL.
5020 */
5021 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005022get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005024 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 int evaluate;
5026{
5027 char_u *option_end;
5028 long numval;
5029 char_u *stringval;
5030 int opt_type;
5031 int c;
5032 int working = (**arg == '+'); /* has("+option") */
5033 int ret = OK;
5034 int opt_flags;
5035
5036 /*
5037 * Isolate the option name and find its value.
5038 */
5039 option_end = find_option_end(arg, &opt_flags);
5040 if (option_end == NULL)
5041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005042 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 EMSG2(_("E112: Option name missing: %s"), *arg);
5044 return FAIL;
5045 }
5046
5047 if (!evaluate)
5048 {
5049 *arg = option_end;
5050 return OK;
5051 }
5052
5053 c = *option_end;
5054 *option_end = NUL;
5055 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
5058 if (opt_type == -3) /* invalid name */
5059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 EMSG2(_("E113: Unknown option: %s"), *arg);
5062 ret = FAIL;
5063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 if (opt_type == -2) /* hidden string option */
5067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->v_type = VAR_STRING;
5069 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 else if (opt_type == -1) /* hidden number option */
5072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 rettv->v_type = VAR_NUMBER;
5074 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 else if (opt_type == 1) /* number option */
5077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 rettv->v_type = VAR_NUMBER;
5079 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 else /* string option */
5082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 rettv->v_type = VAR_STRING;
5084 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 }
5087 else if (working && (opt_type == -2 || opt_type == -1))
5088 ret = FAIL;
5089
5090 *option_end = c; /* put back for error messages */
5091 *arg = option_end;
5092
5093 return ret;
5094}
5095
5096/*
5097 * Allocate a variable for a string constant.
5098 * Return OK or FAIL.
5099 */
5100 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 int evaluate;
5105{
5106 char_u *p;
5107 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 int extra = 0;
5109
5110 /*
5111 * Find the end of the string, skipping backslashed characters.
5112 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
5115 if (*p == '\\' && p[1] != NUL)
5116 {
5117 ++p;
5118 /* A "\<x>" form occupies at least 4 characters, and produces up
5119 * to 6 characters: reserve space for 2 extra */
5120 if (*p == '<')
5121 extra += 2;
5122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124
5125 if (*p != '"')
5126 {
5127 EMSG2(_("E114: Missing quote: %s"), *arg);
5128 return FAIL;
5129 }
5130
5131 /* If only parsing, set *arg and return here */
5132 if (!evaluate)
5133 {
5134 *arg = p + 1;
5135 return OK;
5136 }
5137
5138 /*
5139 * Copy the string into allocated memory, handling backslashed
5140 * characters.
5141 */
5142 name = alloc((unsigned)(p - *arg + extra));
5143 if (name == NULL)
5144 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 rettv->v_type = VAR_STRING;
5146 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
5150 if (*p == '\\')
5151 {
5152 switch (*++p)
5153 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 case 'b': *name++ = BS; ++p; break;
5155 case 'e': *name++ = ESC; ++p; break;
5156 case 'f': *name++ = FF; ++p; break;
5157 case 'n': *name++ = NL; ++p; break;
5158 case 'r': *name++ = CAR; ++p; break;
5159 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160
5161 case 'X': /* hex: "\x1", "\x12" */
5162 case 'x':
5163 case 'u': /* Unicode: "\u0023" */
5164 case 'U':
5165 if (vim_isxdigit(p[1]))
5166 {
5167 int n, nr;
5168 int c = toupper(*p);
5169
5170 if (c == 'X')
5171 n = 2;
5172 else
5173 n = 4;
5174 nr = 0;
5175 while (--n >= 0 && vim_isxdigit(p[1]))
5176 {
5177 ++p;
5178 nr = (nr << 4) + hex2nr(*p);
5179 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#ifdef FEAT_MBYTE
5182 /* For "\u" store the number according to
5183 * 'encoding'. */
5184 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 else
5187#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 break;
5191
5192 /* octal: "\1", "\12", "\123" */
5193 case '0':
5194 case '1':
5195 case '2':
5196 case '3':
5197 case '4':
5198 case '5':
5199 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 case '7': *name = *p++ - '0';
5201 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 *name = (*name << 3) + *p++ - '0';
5204 if (*p >= '0' && *p <= '7')
5205 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 break;
5209
5210 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (extra != 0)
5213 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216 }
5217 /* FALLTHROUGH */
5218
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 break;
5221 }
5222 }
5223 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 *arg = p + 1;
5229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 return OK;
5231}
5232
5233/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 * Return OK or FAIL.
5236 */
5237 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 int evaluate;
5242{
5243 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005244 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005245 int reduce = 0;
5246
5247 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 */
5250 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005253 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005255 break;
5256 ++reduce;
5257 ++p;
5258 }
5259 }
5260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005264 return FAIL;
5265 }
5266
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005268 if (!evaluate)
5269 {
5270 *arg = p + 1;
5271 return OK;
5272 }
5273
5274 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005276 */
5277 str = alloc((unsigned)((p - *arg) - reduce));
5278 if (str == NULL)
5279 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 rettv->v_type = VAR_STRING;
5281 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005282
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005284 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005286 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005288 break;
5289 ++p;
5290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005292 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005294 *arg = p + 1;
5295
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005296 return OK;
5297}
5298
5299/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 * Allocate a variable for a List and fill it from "*arg".
5301 * Return OK or FAIL.
5302 */
5303 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005304get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005306 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 int evaluate;
5308{
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 list_T *l = NULL;
5310 typval_T tv;
5311 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312
5313 if (evaluate)
5314 {
5315 l = list_alloc();
5316 if (l == NULL)
5317 return FAIL;
5318 }
5319
5320 *arg = skipwhite(*arg + 1);
5321 while (**arg != ']' && **arg != NUL)
5322 {
5323 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5324 goto failret;
5325 if (evaluate)
5326 {
5327 item = listitem_alloc();
5328 if (item != NULL)
5329 {
5330 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005331 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 list_append(l, item);
5333 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005334 else
5335 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 }
5337
5338 if (**arg == ']')
5339 break;
5340 if (**arg != ',')
5341 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 goto failret;
5344 }
5345 *arg = skipwhite(*arg + 1);
5346 }
5347
5348 if (**arg != ']')
5349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351failret:
5352 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005353 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 return FAIL;
5355 }
5356
5357 *arg = skipwhite(*arg + 1);
5358 if (evaluate)
5359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 rettv->v_type = VAR_LIST;
5361 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 ++l->lv_refcount;
5363 }
5364
5365 return OK;
5366}
5367
5368/*
5369 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005370 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005372 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373list_alloc()
5374{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005375 list_T *l;
5376
5377 l = (list_T *)alloc_clear(sizeof(list_T));
5378 if (l != NULL)
5379 {
5380 /* Prepend the list to the list of lists for garbage collection. */
5381 if (first_list != NULL)
5382 first_list->lv_used_prev = l;
5383 l->lv_used_prev = NULL;
5384 l->lv_used_next = first_list;
5385 first_list = l;
5386 }
5387 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388}
5389
5390/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005391 * Allocate an empty list for a return value.
5392 * Returns OK or FAIL.
5393 */
5394 static int
5395rettv_list_alloc(rettv)
5396 typval_T *rettv;
5397{
5398 list_T *l = list_alloc();
5399
5400 if (l == NULL)
5401 return FAIL;
5402
5403 rettv->vval.v_list = l;
5404 rettv->v_type = VAR_LIST;
5405 ++l->lv_refcount;
5406 return OK;
5407}
5408
5409/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 * Unreference a list: decrement the reference count and free it when it
5411 * becomes zero.
5412 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005413 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005417 if (l != NULL && --l->lv_refcount <= 0)
5418 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419}
5420
5421/*
5422 * Free a list, including all items it points to.
5423 * Ignores the reference count.
5424 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005425 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005426list_free(l, recurse)
5427 list_T *l;
5428 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429{
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005432 /* Remove the list from the list of lists for garbage collection. */
5433 if (l->lv_used_prev == NULL)
5434 first_list = l->lv_used_next;
5435 else
5436 l->lv_used_prev->lv_used_next = l->lv_used_next;
5437 if (l->lv_used_next != NULL)
5438 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5439
Bram Moolenaard9fba312005-06-26 22:34:35 +00005440 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005442 /* Remove the item before deleting it. */
5443 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005444 if (recurse || (item->li_tv.v_type != VAR_LIST
5445 && item->li_tv.v_type != VAR_DICT))
5446 clear_tv(&item->li_tv);
5447 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 }
5449 vim_free(l);
5450}
5451
5452/*
5453 * Allocate a list item.
5454 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456listitem_alloc()
5457{
Bram Moolenaar33570922005-01-25 22:26:29 +00005458 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459}
5460
5461/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005462 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 */
5464 static void
5465listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005466 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 vim_free(item);
5470}
5471
5472/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005473 * Remove a list item from a List and free it. Also clears the value.
5474 */
5475 static void
5476listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 list_T *l;
5478 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005479{
5480 list_remove(l, item, item);
5481 listitem_free(item);
5482}
5483
5484/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 * Get the number of items in a list.
5486 */
5487 static long
5488list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005489 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 if (l == NULL)
5492 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005493 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494}
5495
5496/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005497 * Return TRUE when two lists have exactly the same values.
5498 */
5499 static int
5500list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 list_T *l1;
5502 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005503 int ic; /* ignore case for strings */
5504{
Bram Moolenaar33570922005-01-25 22:26:29 +00005505 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005506
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005507 if (l1 == l2)
5508 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005509 if (list_len(l1) != list_len(l2))
5510 return FALSE;
5511
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005512 for (item1 = l1->lv_first, item2 = l2->lv_first;
5513 item1 != NULL && item2 != NULL;
5514 item1 = item1->li_next, item2 = item2->li_next)
5515 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5516 return FALSE;
5517 return item1 == NULL && item2 == NULL;
5518}
5519
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005520#if defined(FEAT_PYTHON) || defined(PROTO)
5521/*
5522 * Return the dictitem that an entry in a hashtable points to.
5523 */
5524 dictitem_T *
5525dict_lookup(hi)
5526 hashitem_T *hi;
5527{
5528 return HI2DI(hi);
5529}
5530#endif
5531
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005532/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005533 * Return TRUE when two dictionaries have exactly the same key/values.
5534 */
5535 static int
5536dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 dict_T *d1;
5538 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005539 int ic; /* ignore case for strings */
5540{
Bram Moolenaar33570922005-01-25 22:26:29 +00005541 hashitem_T *hi;
5542 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005543 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005544
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005545 if (d1 == d2)
5546 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005547 if (dict_len(d1) != dict_len(d2))
5548 return FALSE;
5549
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005550 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005553 if (!HASHITEM_EMPTY(hi))
5554 {
5555 item2 = dict_find(d2, hi->hi_key, -1);
5556 if (item2 == NULL)
5557 return FALSE;
5558 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5559 return FALSE;
5560 --todo;
5561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005562 }
5563 return TRUE;
5564}
5565
5566/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005567 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005568 * Compares the items just like "==" would compare them, but strings and
5569 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005570 */
5571 static int
5572tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 typval_T *tv1;
5574 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005575 int ic; /* ignore case */
5576{
5577 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005578 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005579 static int recursive = 0; /* cach recursive loops */
5580 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005581
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005582 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005583 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005584 /* Catch lists and dicts that have an endless loop by limiting
5585 * recursiveness to 1000. We guess they are equal then. */
5586 if (recursive >= 1000)
5587 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005588
5589 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005590 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005591 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005592 ++recursive;
5593 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5594 --recursive;
5595 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005596
5597 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005598 ++recursive;
5599 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5600 --recursive;
5601 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005602
5603 case VAR_FUNC:
5604 return (tv1->vval.v_string != NULL
5605 && tv2->vval.v_string != NULL
5606 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5607
5608 case VAR_NUMBER:
5609 return tv1->vval.v_number == tv2->vval.v_number;
5610
5611 case VAR_STRING:
5612 s1 = get_tv_string_buf(tv1, buf1);
5613 s2 = get_tv_string_buf(tv2, buf2);
5614 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005615 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005616
5617 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005618 return TRUE;
5619}
5620
5621/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 * Locate item with index "n" in list "l" and return it.
5623 * A negative index is counted from the end; -1 is the last item.
5624 * Returns NULL when "n" is out of range.
5625 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005626 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005628 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005629 long n;
5630{
Bram Moolenaar33570922005-01-25 22:26:29 +00005631 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 long idx;
5633
5634 if (l == NULL)
5635 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005636
5637 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005638 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005639 n = l->lv_len + n;
5640
5641 /* Check for index out of range. */
5642 if (n < 0 || n >= l->lv_len)
5643 return NULL;
5644
5645 /* When there is a cached index may start search from there. */
5646 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005648 if (n < l->lv_idx / 2)
5649 {
5650 /* closest to the start of the list */
5651 item = l->lv_first;
5652 idx = 0;
5653 }
5654 else if (n > (l->lv_idx + l->lv_len) / 2)
5655 {
5656 /* closest to the end of the list */
5657 item = l->lv_last;
5658 idx = l->lv_len - 1;
5659 }
5660 else
5661 {
5662 /* closest to the cached index */
5663 item = l->lv_idx_item;
5664 idx = l->lv_idx;
5665 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666 }
5667 else
5668 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005669 if (n < l->lv_len / 2)
5670 {
5671 /* closest to the start of the list */
5672 item = l->lv_first;
5673 idx = 0;
5674 }
5675 else
5676 {
5677 /* closest to the end of the list */
5678 item = l->lv_last;
5679 idx = l->lv_len - 1;
5680 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005682
5683 while (n > idx)
5684 {
5685 /* search forward */
5686 item = item->li_next;
5687 ++idx;
5688 }
5689 while (n < idx)
5690 {
5691 /* search backward */
5692 item = item->li_prev;
5693 --idx;
5694 }
5695
5696 /* cache the used index */
5697 l->lv_idx = idx;
5698 l->lv_idx_item = item;
5699
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700 return item;
5701}
5702
5703/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005704 * Get list item "l[idx]" as a number.
5705 */
5706 static long
5707list_find_nr(l, idx, errorp)
5708 list_T *l;
5709 long idx;
5710 int *errorp; /* set to TRUE when something wrong */
5711{
5712 listitem_T *li;
5713
5714 li = list_find(l, idx);
5715 if (li == NULL)
5716 {
5717 if (errorp != NULL)
5718 *errorp = TRUE;
5719 return -1L;
5720 }
5721 return get_tv_number_chk(&li->li_tv, errorp);
5722}
5723
5724/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005725 * Locate "item" list "l" and return its index.
5726 * Returns -1 when "item" is not in the list.
5727 */
5728 static long
5729list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 list_T *l;
5731 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005732{
5733 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005735
5736 if (l == NULL)
5737 return -1;
5738 idx = 0;
5739 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5740 ++idx;
5741 if (li == NULL)
5742 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005743 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005744}
5745
5746/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747 * Append item "item" to the end of list "l".
5748 */
5749 static void
5750list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 list_T *l;
5752 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753{
5754 if (l->lv_last == NULL)
5755 {
5756 /* empty list */
5757 l->lv_first = item;
5758 l->lv_last = item;
5759 item->li_prev = NULL;
5760 }
5761 else
5762 {
5763 l->lv_last->li_next = item;
5764 item->li_prev = l->lv_last;
5765 l->lv_last = item;
5766 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005767 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 item->li_next = NULL;
5769}
5770
5771/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005773 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774 */
5775 static int
5776list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005777 list_T *l;
5778 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005780 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781
Bram Moolenaar05159a02005-02-26 23:04:13 +00005782 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005784 copy_tv(tv, &li->li_tv);
5785 list_append(l, li);
5786 return OK;
5787}
5788
5789/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005790 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005791 * Return FAIL when out of memory.
5792 */
5793 int
5794list_append_dict(list, dict)
5795 list_T *list;
5796 dict_T *dict;
5797{
5798 listitem_T *li = listitem_alloc();
5799
5800 if (li == NULL)
5801 return FAIL;
5802 li->li_tv.v_type = VAR_DICT;
5803 li->li_tv.v_lock = 0;
5804 li->li_tv.vval.v_dict = dict;
5805 list_append(list, li);
5806 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 return OK;
5808}
5809
5810/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005811 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005812 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005813 * Returns FAIL when out of memory.
5814 */
5815 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005816list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005817 list_T *l;
5818 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005819 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005820{
5821 listitem_T *li = listitem_alloc();
5822
5823 if (li == NULL)
5824 return FAIL;
5825 list_append(l, li);
5826 li->li_tv.v_type = VAR_STRING;
5827 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005828 if (str == NULL)
5829 li->li_tv.vval.v_string = NULL;
5830 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005831 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005832 return FAIL;
5833 return OK;
5834}
5835
5836/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005837 * Append "n" to list "l".
5838 * Returns FAIL when out of memory.
5839 */
5840 static int
5841list_append_number(l, n)
5842 list_T *l;
5843 varnumber_T n;
5844{
5845 listitem_T *li;
5846
5847 li = listitem_alloc();
5848 if (li == NULL)
5849 return FAIL;
5850 li->li_tv.v_type = VAR_NUMBER;
5851 li->li_tv.v_lock = 0;
5852 li->li_tv.vval.v_number = n;
5853 list_append(l, li);
5854 return OK;
5855}
5856
5857/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005858 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005859 * If "item" is NULL append at the end.
5860 * Return FAIL when out of memory.
5861 */
5862 static int
5863list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 list_T *l;
5865 typval_T *tv;
5866 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005867{
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005869
5870 if (ni == NULL)
5871 return FAIL;
5872 copy_tv(tv, &ni->li_tv);
5873 if (item == NULL)
5874 /* Append new item at end of list. */
5875 list_append(l, ni);
5876 else
5877 {
5878 /* Insert new item before existing item. */
5879 ni->li_prev = item->li_prev;
5880 ni->li_next = item;
5881 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005882 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005883 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005884 ++l->lv_idx;
5885 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005886 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005887 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005888 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005889 l->lv_idx_item = NULL;
5890 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005891 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005892 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005893 }
5894 return OK;
5895}
5896
5897/*
5898 * Extend "l1" with "l2".
5899 * If "bef" is NULL append at the end, otherwise insert before this item.
5900 * Returns FAIL when out of memory.
5901 */
5902 static int
5903list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 list_T *l1;
5905 list_T *l2;
5906 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005907{
Bram Moolenaar33570922005-01-25 22:26:29 +00005908 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005909
5910 for (item = l2->lv_first; item != NULL; item = item->li_next)
5911 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5912 return FAIL;
5913 return OK;
5914}
5915
5916/*
5917 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5918 * Return FAIL when out of memory.
5919 */
5920 static int
5921list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 list_T *l1;
5923 list_T *l2;
5924 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005925{
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005927
5928 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005929 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005930 if (l == NULL)
5931 return FAIL;
5932 tv->v_type = VAR_LIST;
5933 tv->vval.v_list = l;
5934
5935 /* append all items from the second list */
5936 return list_extend(l, l2, NULL);
5937}
5938
5939/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005940 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 * Returns NULL when out of memory.
5944 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005946list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005949 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950{
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 list_T *copy;
5952 listitem_T *item;
5953 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954
5955 if (orig == NULL)
5956 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957
5958 copy = list_alloc();
5959 if (copy != NULL)
5960 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005961 if (copyID != 0)
5962 {
5963 /* Do this before adding the items, because one of the items may
5964 * refer back to this list. */
5965 orig->lv_copyID = copyID;
5966 orig->lv_copylist = copy;
5967 }
5968 for (item = orig->lv_first; item != NULL && !got_int;
5969 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 {
5971 ni = listitem_alloc();
5972 if (ni == NULL)
5973 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005974 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 {
5976 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5977 {
5978 vim_free(ni);
5979 break;
5980 }
5981 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005983 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 list_append(copy, ni);
5985 }
5986 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005987 if (item != NULL)
5988 {
5989 list_unref(copy);
5990 copy = NULL;
5991 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 }
5993
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 return copy;
5995}
5996
5997/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005999 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006002list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 list_T *l;
6004 listitem_T *item;
6005 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006{
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 /* notify watchers */
6010 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006012 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013 list_fix_watch(l, ip);
6014 if (ip == item2)
6015 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006017
6018 if (item2->li_next == NULL)
6019 l->lv_last = item->li_prev;
6020 else
6021 item2->li_next->li_prev = item->li_prev;
6022 if (item->li_prev == NULL)
6023 l->lv_first = item2->li_next;
6024 else
6025 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006026 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027}
6028
6029/*
6030 * Return an allocated string with the string representation of a list.
6031 * May return NULL.
6032 */
6033 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006034list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037{
6038 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039
6040 if (tv->vval.v_list == NULL)
6041 return NULL;
6042 ga_init2(&ga, (int)sizeof(char), 80);
6043 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006044 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006045 {
6046 vim_free(ga.ga_data);
6047 return NULL;
6048 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 ga_append(&ga, ']');
6050 ga_append(&ga, NUL);
6051 return (char_u *)ga.ga_data;
6052}
6053
6054/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006055 * Join list "l" into a string in "*gap", using separator "sep".
6056 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006057 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006058 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006059 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006060list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006061 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006063 char_u *sep;
6064 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006065 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006066{
6067 int first = TRUE;
6068 char_u *tofree;
6069 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006070 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006071 char_u *s;
6072
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006073 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006074 {
6075 if (first)
6076 first = FALSE;
6077 else
6078 ga_concat(gap, sep);
6079
6080 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006081 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006082 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006083 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006084 if (s != NULL)
6085 ga_concat(gap, s);
6086 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006087 if (s == NULL)
6088 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006089 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006090 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006091}
6092
6093/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006094 * Garbage collection for lists and dictionaries.
6095 *
6096 * We use reference counts to be able to free most items right away when they
6097 * are no longer used. But for composite items it's possible that it becomes
6098 * unused while the reference count is > 0: When there is a recursive
6099 * reference. Example:
6100 * :let l = [1, 2, 3]
6101 * :let d = {9: l}
6102 * :let l[1] = d
6103 *
6104 * Since this is quite unusual we handle this with garbage collection: every
6105 * once in a while find out which lists and dicts are not referenced from any
6106 * variable.
6107 *
6108 * Here is a good reference text about garbage collection (refers to Python
6109 * but it applies to all reference-counting mechanisms):
6110 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006111 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006112
6113/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006114 * Do garbage collection for lists and dicts.
6115 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006116 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006117 int
6118garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006119{
6120 dict_T *dd;
6121 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006122 int copyID = ++current_copyID;
6123 buf_T *buf;
6124 win_T *wp;
6125 int i;
6126 funccall_T *fc;
6127 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006128#ifdef FEAT_WINDOWS
6129 tabpage_T *tp;
6130#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006131
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006132 /* Only do this once. */
6133 want_garbage_collect = FALSE;
6134 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006135 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006136
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006137 /*
6138 * 1. Go through all accessible variables and mark all lists and dicts
6139 * with copyID.
6140 */
6141 /* script-local variables */
6142 for (i = 1; i <= ga_scripts.ga_len; ++i)
6143 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6144
6145 /* buffer-local variables */
6146 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6147 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6148
6149 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006150 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006151 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6152
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006153#ifdef FEAT_WINDOWS
6154 /* tabpage-local variables */
6155 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6156 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6157#endif
6158
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006159 /* global variables */
6160 set_ref_in_ht(&globvarht, copyID);
6161
6162 /* function-local variables */
6163 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6164 {
6165 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6166 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6167 }
6168
6169 /*
6170 * 2. Go through the list of dicts and free items without the copyID.
6171 */
6172 for (dd = first_dict; dd != NULL; )
6173 if (dd->dv_copyID != copyID)
6174 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006175 /* Free the Dictionary and ordinary items it contains, but don't
6176 * recurse into Lists and Dictionaries, they will be in the list
6177 * of dicts or list of lists. */
6178 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006179 did_free = TRUE;
6180
6181 /* restart, next dict may also have been freed */
6182 dd = first_dict;
6183 }
6184 else
6185 dd = dd->dv_used_next;
6186
6187 /*
6188 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006189 * But don't free a list that has a watcher (used in a for loop), these
6190 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006191 */
6192 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006193 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006194 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006195 /* Free the List and ordinary items it contains, but don't recurse
6196 * into Lists and Dictionaries, they will be in the list of dicts
6197 * or list of lists. */
6198 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006199 did_free = TRUE;
6200
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006201 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006202 ll = first_list;
6203 }
6204 else
6205 ll = ll->lv_used_next;
6206
6207 return did_free;
6208}
6209
6210/*
6211 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6212 */
6213 static void
6214set_ref_in_ht(ht, copyID)
6215 hashtab_T *ht;
6216 int copyID;
6217{
6218 int todo;
6219 hashitem_T *hi;
6220
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006221 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006222 for (hi = ht->ht_array; todo > 0; ++hi)
6223 if (!HASHITEM_EMPTY(hi))
6224 {
6225 --todo;
6226 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6227 }
6228}
6229
6230/*
6231 * Mark all lists and dicts referenced through list "l" with "copyID".
6232 */
6233 static void
6234set_ref_in_list(l, copyID)
6235 list_T *l;
6236 int copyID;
6237{
6238 listitem_T *li;
6239
6240 for (li = l->lv_first; li != NULL; li = li->li_next)
6241 set_ref_in_item(&li->li_tv, copyID);
6242}
6243
6244/*
6245 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6246 */
6247 static void
6248set_ref_in_item(tv, copyID)
6249 typval_T *tv;
6250 int copyID;
6251{
6252 dict_T *dd;
6253 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006254
6255 switch (tv->v_type)
6256 {
6257 case VAR_DICT:
6258 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006259 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006260 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006261 /* Didn't see this dict yet. */
6262 dd->dv_copyID = copyID;
6263 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006264 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006265 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006266
6267 case VAR_LIST:
6268 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006269 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006270 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006271 /* Didn't see this list yet. */
6272 ll->lv_copyID = copyID;
6273 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006274 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006275 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006276 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006277 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006278}
6279
6280/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006281 * Allocate an empty header for a dictionary.
6282 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006283 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006284dict_alloc()
6285{
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006289 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006290 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006291 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006292 if (first_dict != NULL)
6293 first_dict->dv_used_prev = d;
6294 d->dv_used_next = first_dict;
6295 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006296 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006297
Bram Moolenaar33570922005-01-25 22:26:29 +00006298 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006299 d->dv_lock = 0;
6300 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006301 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006302 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006303 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006304}
6305
6306/*
6307 * Unreference a Dictionary: decrement the reference count and free it when it
6308 * becomes zero.
6309 */
6310 static void
6311dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006314 if (d != NULL && --d->dv_refcount <= 0)
6315 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316}
6317
6318/*
6319 * Free a Dictionary, including all items it contains.
6320 * Ignores the reference count.
6321 */
6322 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006323dict_free(d, recurse)
6324 dict_T *d;
6325 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006326{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006327 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006328 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006329 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006331 /* Remove the dict from the list of dicts for garbage collection. */
6332 if (d->dv_used_prev == NULL)
6333 first_dict = d->dv_used_next;
6334 else
6335 d->dv_used_prev->dv_used_next = d->dv_used_next;
6336 if (d->dv_used_next != NULL)
6337 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6338
6339 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006340 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006341 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006343 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344 if (!HASHITEM_EMPTY(hi))
6345 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006346 /* Remove the item before deleting it, just in case there is
6347 * something recursive causing trouble. */
6348 di = HI2DI(hi);
6349 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006350 if (recurse || (di->di_tv.v_type != VAR_LIST
6351 && di->di_tv.v_type != VAR_DICT))
6352 clear_tv(&di->di_tv);
6353 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006354 --todo;
6355 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006356 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006358 vim_free(d);
6359}
6360
6361/*
6362 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006363 * The "key" is copied to the new item.
6364 * Note that the value of the item "di_tv" still needs to be initialized!
6365 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006366 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006368dictitem_alloc(key)
6369 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006370{
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006373 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006375 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006376 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006377 di->di_flags = 0;
6378 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006380}
6381
6382/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383 * Make a copy of a Dictionary item.
6384 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388{
Bram Moolenaar33570922005-01-25 22:26:29 +00006389 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006391 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6392 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393 if (di != NULL)
6394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006395 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006396 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006397 copy_tv(&org->di_tv, &di->di_tv);
6398 }
6399 return di;
6400}
6401
6402/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006403 * Remove item "item" from Dictionary "dict" and free it.
6404 */
6405 static void
6406dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 dict_T *dict;
6408 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006409{
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006411
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006413 if (HASHITEM_EMPTY(hi))
6414 EMSG2(_(e_intern2), "dictitem_remove()");
6415 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417 dictitem_free(item);
6418}
6419
6420/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421 * Free a dict item. Also clears the value.
6422 */
6423 static void
6424dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427 clear_tv(&item->di_tv);
6428 vim_free(item);
6429}
6430
6431/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6433 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006435 * Returns NULL when out of memory.
6436 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006437 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006440 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006441 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006442{
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 dict_T *copy;
6444 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006445 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006447
6448 if (orig == NULL)
6449 return NULL;
6450
6451 copy = dict_alloc();
6452 if (copy != NULL)
6453 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006454 if (copyID != 0)
6455 {
6456 orig->dv_copyID = copyID;
6457 orig->dv_copydict = copy;
6458 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006459 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006460 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006462 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006463 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006464 --todo;
6465
6466 di = dictitem_alloc(hi->hi_key);
6467 if (di == NULL)
6468 break;
6469 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006470 {
6471 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6472 copyID) == FAIL)
6473 {
6474 vim_free(di);
6475 break;
6476 }
6477 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006478 else
6479 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6480 if (dict_add(copy, di) == FAIL)
6481 {
6482 dictitem_free(di);
6483 break;
6484 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006487
Bram Moolenaare9a41262005-01-15 22:18:47 +00006488 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006489 if (todo > 0)
6490 {
6491 dict_unref(copy);
6492 copy = NULL;
6493 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 }
6495
6496 return copy;
6497}
6498
6499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006503 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 dict_T *d;
6506 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507{
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006509}
6510
Bram Moolenaar8c711452005-01-14 21:53:12 +00006511/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006512 * Add a number or string entry to dictionary "d".
6513 * When "str" is NULL use number "nr", otherwise use "str".
6514 * Returns FAIL when out of memory and when key already exists.
6515 */
6516 int
6517dict_add_nr_str(d, key, nr, str)
6518 dict_T *d;
6519 char *key;
6520 long nr;
6521 char_u *str;
6522{
6523 dictitem_T *item;
6524
6525 item = dictitem_alloc((char_u *)key);
6526 if (item == NULL)
6527 return FAIL;
6528 item->di_tv.v_lock = 0;
6529 if (str == NULL)
6530 {
6531 item->di_tv.v_type = VAR_NUMBER;
6532 item->di_tv.vval.v_number = nr;
6533 }
6534 else
6535 {
6536 item->di_tv.v_type = VAR_STRING;
6537 item->di_tv.vval.v_string = vim_strsave(str);
6538 }
6539 if (dict_add(d, item) == FAIL)
6540 {
6541 dictitem_free(item);
6542 return FAIL;
6543 }
6544 return OK;
6545}
6546
6547/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548 * Get the number of items in a Dictionary.
6549 */
6550 static long
6551dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 if (d == NULL)
6555 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006556 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557}
6558
6559/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006560 * Find item "key[len]" in Dictionary "d".
6561 * If "len" is negative use strlen(key).
6562 * Returns NULL when not found.
6563 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006565dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006567 char_u *key;
6568 int len;
6569{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006570#define AKEYLEN 200
6571 char_u buf[AKEYLEN];
6572 char_u *akey;
6573 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006575
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006576 if (len < 0)
6577 akey = key;
6578 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006579 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006580 tofree = akey = vim_strnsave(key, len);
6581 if (akey == NULL)
6582 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006583 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006584 else
6585 {
6586 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006587 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006588 akey = buf;
6589 }
6590
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006592 vim_free(tofree);
6593 if (HASHITEM_EMPTY(hi))
6594 return NULL;
6595 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006596}
6597
6598/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006599 * Get a string item from a dictionary.
6600 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006601 * Returns NULL if the entry doesn't exist or out of memory.
6602 */
6603 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006604get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006605 dict_T *d;
6606 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006607 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006608{
6609 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006610 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006611
6612 di = dict_find(d, key, -1);
6613 if (di == NULL)
6614 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006615 s = get_tv_string(&di->di_tv);
6616 if (save && s != NULL)
6617 s = vim_strsave(s);
6618 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006619}
6620
6621/*
6622 * Get a number item from a dictionary.
6623 * Returns 0 if the entry doesn't exist or out of memory.
6624 */
6625 long
6626get_dict_number(d, key)
6627 dict_T *d;
6628 char_u *key;
6629{
6630 dictitem_T *di;
6631
6632 di = dict_find(d, key, -1);
6633 if (di == NULL)
6634 return 0;
6635 return get_tv_number(&di->di_tv);
6636}
6637
6638/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006639 * Return an allocated string with the string representation of a Dictionary.
6640 * May return NULL.
6641 */
6642 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006643dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006644 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006645 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006646{
6647 garray_T ga;
6648 int first = TRUE;
6649 char_u *tofree;
6650 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006652 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006653 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006654 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006655
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006656 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006657 return NULL;
6658 ga_init2(&ga, (int)sizeof(char), 80);
6659 ga_append(&ga, '{');
6660
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006661 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006662 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006663 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006664 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006665 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006666 --todo;
6667
6668 if (first)
6669 first = FALSE;
6670 else
6671 ga_concat(&ga, (char_u *)", ");
6672
6673 tofree = string_quote(hi->hi_key, FALSE);
6674 if (tofree != NULL)
6675 {
6676 ga_concat(&ga, tofree);
6677 vim_free(tofree);
6678 }
6679 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006680 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006681 if (s != NULL)
6682 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006684 if (s == NULL)
6685 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006687 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006688 if (todo > 0)
6689 {
6690 vim_free(ga.ga_data);
6691 return NULL;
6692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006693
6694 ga_append(&ga, '}');
6695 ga_append(&ga, NUL);
6696 return (char_u *)ga.ga_data;
6697}
6698
6699/*
6700 * Allocate a variable for a Dictionary and fill it from "*arg".
6701 * Return OK or FAIL. Returns NOTDONE for {expr}.
6702 */
6703 static int
6704get_dict_tv(arg, rettv, evaluate)
6705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006706 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707 int evaluate;
6708{
Bram Moolenaar33570922005-01-25 22:26:29 +00006709 dict_T *d = NULL;
6710 typval_T tvkey;
6711 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00006712 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006713 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006714 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006715 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006716
6717 /*
6718 * First check if it's not a curly-braces thing: {expr}.
6719 * Must do this without evaluating, otherwise a function may be called
6720 * twice. Unfortunately this means we need to call eval1() twice for the
6721 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006722 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006723 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006724 if (*start != '}')
6725 {
6726 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6727 return FAIL;
6728 if (*start == '}')
6729 return NOTDONE;
6730 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006731
6732 if (evaluate)
6733 {
6734 d = dict_alloc();
6735 if (d == NULL)
6736 return FAIL;
6737 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006738 tvkey.v_type = VAR_UNKNOWN;
6739 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006740
6741 *arg = skipwhite(*arg + 1);
6742 while (**arg != '}' && **arg != NUL)
6743 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006744 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006745 goto failret;
6746 if (**arg != ':')
6747 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006748 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006749 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006750 goto failret;
6751 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006752 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006753 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006754 key = get_tv_string_buf_chk(&tvkey, buf);
6755 if (key == NULL || *key == NUL)
6756 {
6757 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6758 if (key != NULL)
6759 EMSG(_(e_emptykey));
6760 clear_tv(&tvkey);
6761 goto failret;
6762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006764
6765 *arg = skipwhite(*arg + 1);
6766 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6767 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006768 if (evaluate)
6769 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770 goto failret;
6771 }
6772 if (evaluate)
6773 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006774 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006775 if (item != NULL)
6776 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006777 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006778 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006779 clear_tv(&tv);
6780 goto failret;
6781 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006782 item = dictitem_alloc(key);
6783 clear_tv(&tvkey);
6784 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006786 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006787 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006788 if (dict_add(d, item) == FAIL)
6789 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006790 }
6791 }
6792
6793 if (**arg == '}')
6794 break;
6795 if (**arg != ',')
6796 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006797 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006798 goto failret;
6799 }
6800 *arg = skipwhite(*arg + 1);
6801 }
6802
6803 if (**arg != '}')
6804 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006805 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006806failret:
6807 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006808 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006809 return FAIL;
6810 }
6811
6812 *arg = skipwhite(*arg + 1);
6813 if (evaluate)
6814 {
6815 rettv->v_type = VAR_DICT;
6816 rettv->vval.v_dict = d;
6817 ++d->dv_refcount;
6818 }
6819
6820 return OK;
6821}
6822
Bram Moolenaar8c711452005-01-14 21:53:12 +00006823/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 * Return a string with the string representation of a variable.
6825 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006826 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006827 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006828 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006829 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006830 */
6831 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006832echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006833 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006835 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006836 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006837{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006838 static int recurse = 0;
6839 char_u *r = NULL;
6840
Bram Moolenaar33570922005-01-25 22:26:29 +00006841 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006842 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006843 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006844 *tofree = NULL;
6845 return NULL;
6846 }
6847 ++recurse;
6848
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849 switch (tv->v_type)
6850 {
6851 case VAR_FUNC:
6852 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006853 r = tv->vval.v_string;
6854 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006856 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006857 if (tv->vval.v_list == NULL)
6858 {
6859 *tofree = NULL;
6860 r = NULL;
6861 }
6862 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6863 {
6864 *tofree = NULL;
6865 r = (char_u *)"[...]";
6866 }
6867 else
6868 {
6869 tv->vval.v_list->lv_copyID = copyID;
6870 *tofree = list2string(tv, copyID);
6871 r = *tofree;
6872 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006873 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006874
Bram Moolenaar8c711452005-01-14 21:53:12 +00006875 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006876 if (tv->vval.v_dict == NULL)
6877 {
6878 *tofree = NULL;
6879 r = NULL;
6880 }
6881 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6882 {
6883 *tofree = NULL;
6884 r = (char_u *)"{...}";
6885 }
6886 else
6887 {
6888 tv->vval.v_dict->dv_copyID = copyID;
6889 *tofree = dict2string(tv, copyID);
6890 r = *tofree;
6891 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006892 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006894 case VAR_STRING:
6895 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006896 *tofree = NULL;
6897 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006898 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006899
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006900 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006903 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006904
6905 --recurse;
6906 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006907}
6908
6909/*
6910 * Return a string with the string representation of a variable.
6911 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6912 * "numbuf" is used for a number.
6913 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006914 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006915 */
6916 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006917tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006919 char_u **tofree;
6920 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006921 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006922{
6923 switch (tv->v_type)
6924 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006925 case VAR_FUNC:
6926 *tofree = string_quote(tv->vval.v_string, TRUE);
6927 return *tofree;
6928 case VAR_STRING:
6929 *tofree = string_quote(tv->vval.v_string, FALSE);
6930 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006931 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006932 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006933 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006934 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006935 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006936 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006937 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006938 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006939}
6940
6941/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 * Return string "str" in ' quotes, doubling ' characters.
6943 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006944 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006945 */
6946 static char_u *
6947string_quote(str, function)
6948 char_u *str;
6949 int function;
6950{
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006952 char_u *p, *r, *s;
6953
Bram Moolenaar33570922005-01-25 22:26:29 +00006954 len = (function ? 13 : 3);
6955 if (str != NULL)
6956 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006957 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 for (p = str; *p != NUL; mb_ptr_adv(p))
6959 if (*p == '\'')
6960 ++len;
6961 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006962 s = r = alloc(len);
6963 if (r != NULL)
6964 {
6965 if (function)
6966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006967 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006968 r += 10;
6969 }
6970 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006971 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 if (str != NULL)
6973 for (p = str; *p != NUL; )
6974 {
6975 if (*p == '\'')
6976 *r++ = '\'';
6977 MB_COPY_CHAR(p, r);
6978 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006979 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006980 if (function)
6981 *r++ = ')';
6982 *r++ = NUL;
6983 }
6984 return s;
6985}
6986
6987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 * Get the value of an environment variable.
6989 * "arg" is pointing to the '$'. It is advanced to after the name.
6990 * If the environment variable was not set, silently assume it is empty.
6991 * Always return OK.
6992 */
6993 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006994get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 int evaluate;
6998{
6999 char_u *string = NULL;
7000 int len;
7001 int cc;
7002 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007003 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 ++*arg;
7006 name = *arg;
7007 len = get_env_len(arg);
7008 if (evaluate)
7009 {
7010 if (len != 0)
7011 {
7012 cc = name[len];
7013 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007014 /* first try vim_getenv(), fast for normal environment vars */
7015 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007017 {
7018 if (!mustfree)
7019 string = vim_strsave(string);
7020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 else
7022 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007023 if (mustfree)
7024 vim_free(string);
7025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 /* next try expanding things like $VIM and ${HOME} */
7027 string = expand_env_save(name - 1);
7028 if (string != NULL && *string == '$')
7029 {
7030 vim_free(string);
7031 string = NULL;
7032 }
7033 }
7034 name[len] = cc;
7035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007036 rettv->v_type = VAR_STRING;
7037 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039
7040 return OK;
7041}
7042
7043/*
7044 * Array with names and number of arguments of all internal functions
7045 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7046 */
7047static struct fst
7048{
7049 char *f_name; /* function name */
7050 char f_min_argc; /* minimal number of arguments */
7051 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007052 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007053 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054} functions[] =
7055{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007056 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {"append", 2, 2, f_append},
7058 {"argc", 0, 0, f_argc},
7059 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007060 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007062 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {"bufexists", 1, 1, f_bufexists},
7064 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7065 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7066 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7067 {"buflisted", 1, 1, f_buflisted},
7068 {"bufloaded", 1, 1, f_bufloaded},
7069 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007070 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {"bufwinnr", 1, 1, f_bufwinnr},
7072 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007073 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007074 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007075 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"char2nr", 1, 1, f_char2nr},
7077 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007078 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007080#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007081 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007082 {"complete_add", 1, 1, f_complete_add},
7083 {"complete_check", 0, 0, f_complete_check},
7084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007086 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007087 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007089 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007090 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {"delete", 1, 1, f_delete},
7092 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007093 {"diff_filler", 1, 1, f_diff_filler},
7094 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007095 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007097 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {"eventhandler", 0, 0, f_eventhandler},
7099 {"executable", 1, 1, f_executable},
7100 {"exists", 1, 1, f_exists},
7101 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007102 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007103 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7105 {"filereadable", 1, 1, f_filereadable},
7106 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007107 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007108 {"finddir", 1, 3, f_finddir},
7109 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {"fnamemodify", 2, 2, f_fnamemodify},
7111 {"foldclosed", 1, 1, f_foldclosed},
7112 {"foldclosedend", 1, 1, f_foldclosedend},
7113 {"foldlevel", 1, 1, f_foldlevel},
7114 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007115 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007117 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007118 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007119 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007120 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {"getbufvar", 2, 2, f_getbufvar},
7122 {"getchar", 0, 1, f_getchar},
7123 {"getcharmod", 0, 0, f_getcharmod},
7124 {"getcmdline", 0, 0, f_getcmdline},
7125 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007126 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007128 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007129 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {"getfsize", 1, 1, f_getfsize},
7131 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007132 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007133 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007134 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007135 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007136 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007137 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007138 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007139 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007141 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {"getwinposx", 0, 0, f_getwinposx},
7143 {"getwinposy", 0, 0, f_getwinposy},
7144 {"getwinvar", 2, 2, f_getwinvar},
7145 {"glob", 1, 1, f_glob},
7146 {"globpath", 2, 2, f_globpath},
7147 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007149 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007150 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7152 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7153 {"histadd", 2, 2, f_histadd},
7154 {"histdel", 1, 2, f_histdel},
7155 {"histget", 1, 2, f_histget},
7156 {"histnr", 1, 1, f_histnr},
7157 {"hlID", 1, 1, f_hlID},
7158 {"hlexists", 1, 1, f_hlexists},
7159 {"hostname", 0, 0, f_hostname},
7160 {"iconv", 3, 3, f_iconv},
7161 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007162 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007163 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007165 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {"inputrestore", 0, 0, f_inputrestore},
7167 {"inputsave", 0, 0, f_inputsave},
7168 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007169 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007171 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007173 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007176 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {"libcall", 3, 3, f_libcall},
7178 {"libcallnr", 3, 3, f_libcallnr},
7179 {"line", 1, 1, f_line},
7180 {"line2byte", 1, 1, f_line2byte},
7181 {"lispindent", 1, 1, f_lispindent},
7182 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007183 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007184 {"maparg", 1, 3, f_maparg},
7185 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007186 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007187 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007188 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007189 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007190 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007191 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007192 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007193 {"max", 1, 1, f_max},
7194 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007195#ifdef vim_mkdir
7196 {"mkdir", 1, 3, f_mkdir},
7197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {"mode", 0, 0, f_mode},
7199 {"nextnonblank", 1, 1, f_nextnonblank},
7200 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007201 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007203 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007204 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007206 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007207 {"reltime", 0, 2, f_reltime},
7208 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {"remote_expr", 2, 3, f_remote_expr},
7210 {"remote_foreground", 1, 1, f_remote_foreground},
7211 {"remote_peek", 1, 2, f_remote_peek},
7212 {"remote_read", 1, 1, f_remote_read},
7213 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007214 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007216 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007218 {"reverse", 1, 1, f_reverse},
Bram Moolenaar76929292008-01-06 19:07:36 +00007219 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007220 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007221 {"searchpair", 3, 7, f_searchpair},
7222 {"searchpairpos", 3, 7, f_searchpairpos},
7223 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {"server2client", 2, 2, f_server2client},
7225 {"serverlist", 0, 0, f_serverlist},
7226 {"setbufvar", 3, 3, f_setbufvar},
7227 {"setcmdpos", 1, 1, f_setcmdpos},
7228 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007229 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007230 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007231 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007232 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007234 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007236 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007238 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007239 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007240 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007241 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007242 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007243 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244#ifdef HAVE_STRFTIME
7245 {"strftime", 1, 2, f_strftime},
7246#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007248 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {"strlen", 1, 1, f_strlen},
7250 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007251 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {"strtrans", 1, 1, f_strtrans},
7253 {"submatch", 1, 1, f_submatch},
7254 {"substitute", 4, 4, f_substitute},
7255 {"synID", 3, 3, f_synID},
7256 {"synIDattr", 2, 3, f_synIDattr},
7257 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007258 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007259 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007260 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007261 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007262 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007263 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007264 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007266 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {"tolower", 1, 1, f_tolower},
7268 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007269 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {"virtcol", 1, 1, f_virtcol},
7273 {"visualmode", 0, 1, f_visualmode},
7274 {"winbufnr", 1, 1, f_winbufnr},
7275 {"wincol", 0, 0, f_wincol},
7276 {"winheight", 1, 1, f_winheight},
7277 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007278 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007280 {"winrestview", 1, 1, f_winrestview},
7281 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007283 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284};
7285
7286#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7287
7288/*
7289 * Function given to ExpandGeneric() to obtain the list of internal
7290 * or user defined function names.
7291 */
7292 char_u *
7293get_function_name(xp, idx)
7294 expand_T *xp;
7295 int idx;
7296{
7297 static int intidx = -1;
7298 char_u *name;
7299
7300 if (idx == 0)
7301 intidx = -1;
7302 if (intidx < 0)
7303 {
7304 name = get_user_func_name(xp, idx);
7305 if (name != NULL)
7306 return name;
7307 }
7308 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7309 {
7310 STRCPY(IObuff, functions[intidx].f_name);
7311 STRCAT(IObuff, "(");
7312 if (functions[intidx].f_max_argc == 0)
7313 STRCAT(IObuff, ")");
7314 return IObuff;
7315 }
7316
7317 return NULL;
7318}
7319
7320/*
7321 * Function given to ExpandGeneric() to obtain the list of internal or
7322 * user defined variable or function names.
7323 */
7324/*ARGSUSED*/
7325 char_u *
7326get_expr_name(xp, idx)
7327 expand_T *xp;
7328 int idx;
7329{
7330 static int intidx = -1;
7331 char_u *name;
7332
7333 if (idx == 0)
7334 intidx = -1;
7335 if (intidx < 0)
7336 {
7337 name = get_function_name(xp, idx);
7338 if (name != NULL)
7339 return name;
7340 }
7341 return get_user_var_name(xp, ++intidx);
7342}
7343
7344#endif /* FEAT_CMDL_COMPL */
7345
7346/*
7347 * Find internal function in table above.
7348 * Return index, or -1 if not found
7349 */
7350 static int
7351find_internal_func(name)
7352 char_u *name; /* name of the function */
7353{
7354 int first = 0;
7355 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7356 int cmp;
7357 int x;
7358
7359 /*
7360 * Find the function name in the table. Binary search.
7361 */
7362 while (first <= last)
7363 {
7364 x = first + ((unsigned)(last - first) >> 1);
7365 cmp = STRCMP(name, functions[x].f_name);
7366 if (cmp < 0)
7367 last = x - 1;
7368 else if (cmp > 0)
7369 first = x + 1;
7370 else
7371 return x;
7372 }
7373 return -1;
7374}
7375
7376/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007377 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7378 * name it contains, otherwise return "name".
7379 */
7380 static char_u *
7381deref_func_name(name, lenp)
7382 char_u *name;
7383 int *lenp;
7384{
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 int cc;
7387
7388 cc = name[*lenp];
7389 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007390 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007393 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007395 {
7396 *lenp = 0;
7397 return (char_u *)""; /* just in case */
7398 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007399 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401 }
7402
7403 return name;
7404}
7405
7406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * Allocate a variable for the result of a function.
7408 * Return OK or FAIL.
7409 */
7410 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7412 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 char_u *name; /* name of the function */
7414 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 char_u **arg; /* argument, pointing to the '(' */
7417 linenr_T firstline; /* first line of range */
7418 linenr_T lastline; /* last line of range */
7419 int *doesrange; /* return: function handled range */
7420 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
7423 char_u *argp;
7424 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007425 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 int argcount = 0; /* number of arguments found */
7427
7428 /*
7429 * Get the arguments.
7430 */
7431 argp = *arg;
7432 while (argcount < MAX_FUNC_ARGS)
7433 {
7434 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7435 if (*argp == ')' || *argp == ',' || *argp == NUL)
7436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7438 {
7439 ret = FAIL;
7440 break;
7441 }
7442 ++argcount;
7443 if (*argp != ',')
7444 break;
7445 }
7446 if (*argp == ')')
7447 ++argp;
7448 else
7449 ret = FAIL;
7450
7451 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007452 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 {
7456 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007457 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
7462 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007463 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
7465 *arg = skipwhite(argp);
7466 return ret;
7467}
7468
7469
7470/*
7471 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007472 * Return OK when the function can't be called, FAIL otherwise.
7473 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 */
7475 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 char_u *name; /* name of the function */
7479 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007482 typval_T *argvars; /* vars for arguments, must have "argcount"
7483 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 linenr_T firstline; /* first line of range */
7485 linenr_T lastline; /* last line of range */
7486 int *doesrange; /* return: function handled range */
7487 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491#define ERROR_UNKNOWN 0
7492#define ERROR_TOOMANY 1
7493#define ERROR_TOOFEW 2
7494#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495#define ERROR_DICT 4
7496#define ERROR_NONE 5
7497#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 int error = ERROR_NONE;
7499 int i;
7500 int llen;
7501 ufunc_T *fp;
7502 int cc;
7503#define FLEN_FIXED 40
7504 char_u fname_buf[FLEN_FIXED + 1];
7505 char_u *fname;
7506
7507 /*
7508 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7509 * Change <SNR>123_name() to K_SNR 123_name().
7510 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7511 */
7512 cc = name[len];
7513 name[len] = NUL;
7514 llen = eval_fname_script(name);
7515 if (llen > 0)
7516 {
7517 fname_buf[0] = K_SPECIAL;
7518 fname_buf[1] = KS_EXTRA;
7519 fname_buf[2] = (int)KE_SNR;
7520 i = 3;
7521 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7522 {
7523 if (current_SID <= 0)
7524 error = ERROR_SCRIPT;
7525 else
7526 {
7527 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7528 i = (int)STRLEN(fname_buf);
7529 }
7530 }
7531 if (i + STRLEN(name + llen) < FLEN_FIXED)
7532 {
7533 STRCPY(fname_buf + i, name + llen);
7534 fname = fname_buf;
7535 }
7536 else
7537 {
7538 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7539 if (fname == NULL)
7540 error = ERROR_OTHER;
7541 else
7542 {
7543 mch_memmove(fname, fname_buf, (size_t)i);
7544 STRCPY(fname + i, name + llen);
7545 }
7546 }
7547 }
7548 else
7549 fname = name;
7550
7551 *doesrange = FALSE;
7552
7553
7554 /* execute the function if no errors detected and executing */
7555 if (evaluate && error == ERROR_NONE)
7556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 error = ERROR_UNKNOWN;
7559
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007560 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {
7562 /*
7563 * User defined function.
7564 */
7565 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007568 /* Trigger FuncUndefined event, may load the function. */
7569 if (fp == NULL
7570 && apply_autocmds(EVENT_FUNCUNDEFINED,
7571 fname, fname, TRUE, NULL)
7572 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007574 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 fp = find_func(fname);
7576 }
7577#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007578 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007579 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007580 {
7581 /* loaded a package, search for the function again */
7582 fp = find_func(fname);
7583 }
7584
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 if (fp != NULL)
7586 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007587 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007589 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007591 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007593 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007594 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 else
7596 {
7597 /*
7598 * Call the user function.
7599 * Save and restore search patterns, script variables and
7600 * redo buffer.
7601 */
7602 save_search_patterns();
7603 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007604 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007605 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007606 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007607 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7608 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7609 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007610 /* Function was unreferenced while being used, free it
7611 * now. */
7612 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 restoreRedobuff();
7614 restore_search_patterns();
7615 error = ERROR_NONE;
7616 }
7617 }
7618 }
7619 else
7620 {
7621 /*
7622 * Find the function name in the table, call its implementation.
7623 */
7624 i = find_internal_func(fname);
7625 if (i >= 0)
7626 {
7627 if (argcount < functions[i].f_min_argc)
7628 error = ERROR_TOOFEW;
7629 else if (argcount > functions[i].f_max_argc)
7630 error = ERROR_TOOMANY;
7631 else
7632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007634 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 error = ERROR_NONE;
7636 }
7637 }
7638 }
7639 /*
7640 * The function call (or "FuncUndefined" autocommand sequence) might
7641 * have been aborted by an error, an interrupt, or an explicitly thrown
7642 * exception that has not been caught so far. This situation can be
7643 * tested for by calling aborting(). For an error in an internal
7644 * function or for the "E132" error in call_user_func(), however, the
7645 * throw point at which the "force_abort" flag (temporarily reset by
7646 * emsg()) is normally updated has not been reached yet. We need to
7647 * update that flag first to make aborting() reliable.
7648 */
7649 update_force_abort();
7650 }
7651 if (error == ERROR_NONE)
7652 ret = OK;
7653
7654 /*
7655 * Report an error unless the argument evaluation or function call has been
7656 * cancelled due to an aborting error, an interrupt, or an exception.
7657 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 if (!aborting())
7659 {
7660 switch (error)
7661 {
7662 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007663 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 break;
7665 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007666 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 break;
7668 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007669 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 name);
7671 break;
7672 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007673 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 name);
7675 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007677 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007678 name);
7679 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 }
7681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
7683 name[len] = cc;
7684 if (fname != name && fname != fname_buf)
7685 vim_free(fname);
7686
7687 return ret;
7688}
7689
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007690/*
7691 * Give an error message with a function name. Handle <SNR> things.
7692 */
7693 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007694emsg_funcname(ermsg, name)
7695 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007696 char_u *name;
7697{
7698 char_u *p;
7699
7700 if (*name == K_SPECIAL)
7701 p = concat_str((char_u *)"<SNR>", name + 3);
7702 else
7703 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007704 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007705 if (p != name)
7706 vim_free(p);
7707}
7708
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709/*********************************************
7710 * Implementation of the built-in functions
7711 */
7712
7713/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007714 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 */
7716 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007717f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 typval_T *argvars;
7719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007724 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007726 if ((l = argvars[0].vval.v_list) != NULL
7727 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7728 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007729 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730 }
7731 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007732 EMSG(_(e_listreq));
7733}
7734
7735/*
7736 * "append(lnum, string/list)" function
7737 */
7738 static void
7739f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 typval_T *argvars;
7741 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007742{
7743 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007744 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 list_T *l = NULL;
7746 listitem_T *li = NULL;
7747 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007748 long added = 0;
7749
Bram Moolenaar0d660222005-01-07 21:51:51 +00007750 lnum = get_tv_lnum(argvars);
7751 if (lnum >= 0
7752 && lnum <= curbuf->b_ml.ml_line_count
7753 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007754 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007755 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007757 l = argvars[1].vval.v_list;
7758 if (l == NULL)
7759 return;
7760 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007763 for (;;)
7764 {
7765 if (l == NULL)
7766 tv = &argvars[1]; /* append a string */
7767 else if (li == NULL)
7768 break; /* end of list */
7769 else
7770 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007771 line = get_tv_string_chk(tv);
7772 if (line == NULL) /* type error */
7773 {
7774 rettv->vval.v_number = 1; /* Failed */
7775 break;
7776 }
7777 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007778 ++added;
7779 if (l == NULL)
7780 break;
7781 li = li->li_next;
7782 }
7783
7784 appended_lines_mark(lnum, added);
7785 if (curwin->w_cursor.lnum > lnum)
7786 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007788 else
7789 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
7791
7792/*
7793 * "argc()" function
7794 */
7795/* ARGSUSED */
7796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 typval_T *argvars;
7799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802}
7803
7804/*
7805 * "argidx()" function
7806 */
7807/* ARGSUSED */
7808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007809f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 typval_T *argvars;
7811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007813 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814}
7815
7816/*
7817 * "argv(nr)" function
7818 */
7819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007821 typval_T *argvars;
7822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 int idx;
7825
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007826 if (argvars[0].v_type != VAR_UNKNOWN)
7827 {
7828 idx = get_tv_number_chk(&argvars[0], NULL);
7829 if (idx >= 0 && idx < ARGCOUNT)
7830 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7831 else
7832 rettv->vval.v_string = NULL;
7833 rettv->v_type = VAR_STRING;
7834 }
7835 else if (rettv_list_alloc(rettv) == OK)
7836 for (idx = 0; idx < ARGCOUNT; ++idx)
7837 list_append_string(rettv->vval.v_list,
7838 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839}
7840
7841/*
7842 * "browse(save, title, initdir, default)" function
7843 */
7844/* ARGSUSED */
7845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 typval_T *argvars;
7848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850#ifdef FEAT_BROWSE
7851 int save;
7852 char_u *title;
7853 char_u *initdir;
7854 char_u *defname;
7855 char_u buf[NUMBUFLEN];
7856 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007857 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007859 save = get_tv_number_chk(&argvars[0], &error);
7860 title = get_tv_string_chk(&argvars[1]);
7861 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7862 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007864 if (error || title == NULL || initdir == NULL || defname == NULL)
7865 rettv->vval.v_string = NULL;
7866 else
7867 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007868 do_browse(save ? BROWSE_SAVE : 0,
7869 title, defname, NULL, initdir, NULL, curbuf);
7870#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007872#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007874}
7875
7876/*
7877 * "browsedir(title, initdir)" function
7878 */
7879/* ARGSUSED */
7880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 typval_T *argvars;
7883 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007884{
7885#ifdef FEAT_BROWSE
7886 char_u *title;
7887 char_u *initdir;
7888 char_u buf[NUMBUFLEN];
7889
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007890 title = get_tv_string_chk(&argvars[0]);
7891 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007893 if (title == NULL || initdir == NULL)
7894 rettv->vval.v_string = NULL;
7895 else
7896 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007897 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007899 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007901 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902}
7903
Bram Moolenaar33570922005-01-25 22:26:29 +00007904static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007905
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906/*
7907 * Find a buffer by number or exact name.
7908 */
7909 static buf_T *
7910find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912{
7913 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 if (avar->v_type == VAR_NUMBER)
7916 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007917 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007920 if (buf == NULL)
7921 {
7922 /* No full path name match, try a match with a URL or a "nofile"
7923 * buffer, these don't use the full path. */
7924 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7925 if (buf->b_fname != NULL
7926 && (path_with_url(buf->b_fname)
7927#ifdef FEAT_QUICKFIX
7928 || bt_nofile(buf)
7929#endif
7930 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007931 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007932 break;
7933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 }
7935 return buf;
7936}
7937
7938/*
7939 * "bufexists(expr)" function
7940 */
7941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 typval_T *argvars;
7944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007946 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947}
7948
7949/*
7950 * "buflisted(expr)" function
7951 */
7952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 typval_T *argvars;
7955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956{
7957 buf_T *buf;
7958
7959 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961}
7962
7963/*
7964 * "bufloaded(expr)" function
7965 */
7966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007967f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007968 typval_T *argvars;
7969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
7971 buf_T *buf;
7972
7973 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007974 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975}
7976
Bram Moolenaar33570922005-01-25 22:26:29 +00007977static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007978
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979/*
7980 * Get buffer by number or pattern.
7981 */
7982 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 int save_magic;
7988 char_u *save_cpo;
7989 buf_T *buf;
7990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007991 if (tv->v_type == VAR_NUMBER)
7992 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007993 if (tv->v_type != VAR_STRING)
7994 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 if (name == NULL || *name == NUL)
7996 return curbuf;
7997 if (name[0] == '$' && name[1] == NUL)
7998 return lastbuf;
7999
8000 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8001 save_magic = p_magic;
8002 p_magic = TRUE;
8003 save_cpo = p_cpo;
8004 p_cpo = (char_u *)"";
8005
8006 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8007 TRUE, FALSE));
8008
8009 p_magic = save_magic;
8010 p_cpo = save_cpo;
8011
8012 /* If not found, try expanding the name, like done for bufexists(). */
8013 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008014 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015
8016 return buf;
8017}
8018
8019/*
8020 * "bufname(expr)" function
8021 */
8022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008024 typval_T *argvars;
8025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 buf_T *buf;
8028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008029 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 buf = get_buf_tv(&argvars[0]);
8032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008034 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 --emsg_off;
8038}
8039
8040/*
8041 * "bufnr(expr)" function
8042 */
8043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 typval_T *argvars;
8046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008049 int error = FALSE;
8050 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008052 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008055 --emsg_off;
8056
8057 /* If the buffer isn't found and the second argument is not zero create a
8058 * new buffer. */
8059 if (buf == NULL
8060 && argvars[1].v_type != VAR_UNKNOWN
8061 && get_tv_number_chk(&argvars[1], &error) != 0
8062 && !error
8063 && (name = get_tv_string_chk(&argvars[0])) != NULL
8064 && !error)
8065 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008068 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071}
8072
8073/*
8074 * "bufwinnr(nr)" function
8075 */
8076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008077f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008078 typval_T *argvars;
8079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
8081#ifdef FEAT_WINDOWS
8082 win_T *wp;
8083 int winnr = 0;
8084#endif
8085 buf_T *buf;
8086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008087 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008089 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090#ifdef FEAT_WINDOWS
8091 for (wp = firstwin; wp; wp = wp->w_next)
8092 {
8093 ++winnr;
8094 if (wp->w_buffer == buf)
8095 break;
8096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100#endif
8101 --emsg_off;
8102}
8103
8104/*
8105 * "byte2line(byte)" function
8106 */
8107/*ARGSUSED*/
8108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 typval_T *argvars;
8111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
8113#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#else
8116 long boff = 0;
8117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008118 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008122 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 (linenr_T)0, &boff);
8124#endif
8125}
8126
8127/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008128 * "byteidx()" function
8129 */
8130/*ARGSUSED*/
8131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 typval_T *argvars;
8134 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008135{
8136#ifdef FEAT_MBYTE
8137 char_u *t;
8138#endif
8139 char_u *str;
8140 long idx;
8141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008142 str = get_tv_string_chk(&argvars[0]);
8143 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008144 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008145 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008146 return;
8147
8148#ifdef FEAT_MBYTE
8149 t = str;
8150 for ( ; idx > 0; idx--)
8151 {
8152 if (*t == NUL) /* EOL reached */
8153 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008154 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008155 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008156 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008157#else
8158 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008159 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008160#endif
8161}
8162
8163/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008164 * "call(func, arglist)" function
8165 */
8166 static void
8167f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008168 typval_T *argvars;
8169 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170{
8171 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008172 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008173 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008175 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008177
8178 rettv->vval.v_number = 0;
8179 if (argvars[1].v_type != VAR_LIST)
8180 {
8181 EMSG(_(e_listreq));
8182 return;
8183 }
8184 if (argvars[1].vval.v_list == NULL)
8185 return;
8186
8187 if (argvars[0].v_type == VAR_FUNC)
8188 func = argvars[0].vval.v_string;
8189 else
8190 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008191 if (*func == NUL)
8192 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008193
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 if (argvars[2].v_type != VAR_UNKNOWN)
8195 {
8196 if (argvars[2].v_type != VAR_DICT)
8197 {
8198 EMSG(_(e_dictreq));
8199 return;
8200 }
8201 selfdict = argvars[2].vval.v_dict;
8202 }
8203
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008204 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8205 item = item->li_next)
8206 {
8207 if (argc == MAX_FUNC_ARGS)
8208 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008209 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008210 break;
8211 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008212 /* Make a copy of each argument. This is needed to be able to set
8213 * v_lock to VAR_FIXED in the copy without changing the original list.
8214 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008215 copy_tv(&item->li_tv, &argv[argc++]);
8216 }
8217
8218 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008219 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008220 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8221 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008222
8223 /* Free the arguments. */
8224 while (argc > 0)
8225 clear_tv(&argv[--argc]);
8226}
8227
8228/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008229 * "changenr()" function
8230 */
8231/*ARGSUSED*/
8232 static void
8233f_changenr(argvars, rettv)
8234 typval_T *argvars;
8235 typval_T *rettv;
8236{
8237 rettv->vval.v_number = curbuf->b_u_seq_cur;
8238}
8239
8240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 * "char2nr(string)" function
8242 */
8243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008245 typval_T *argvars;
8246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
8248#ifdef FEAT_MBYTE
8249 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008250 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 else
8252#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254}
8255
8256/*
8257 * "cindent(lnum)" function
8258 */
8259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008260f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008261 typval_T *argvars;
8262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
8264#ifdef FEAT_CINDENT
8265 pos_T pos;
8266 linenr_T lnum;
8267
8268 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008269 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8271 {
8272 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 curwin->w_cursor = pos;
8275 }
8276 else
8277#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279}
8280
8281/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008282 * "clearmatches()" function
8283 */
8284/*ARGSUSED*/
8285 static void
8286f_clearmatches(argvars, rettv)
8287 typval_T *argvars;
8288 typval_T *rettv;
8289{
8290#ifdef FEAT_SEARCH_EXTRA
8291 clear_matches(curwin);
8292#endif
8293}
8294
8295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 * "col(string)" function
8297 */
8298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 typval_T *argvars;
8301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 colnr_T col = 0;
8304 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008305 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008307 fp = var2fpos(&argvars[0], FALSE, &fnum);
8308 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
8310 if (fp->col == MAXCOL)
8311 {
8312 /* '> can be MAXCOL, get the length of the line then */
8313 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008314 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 else
8316 col = MAXCOL;
8317 }
8318 else
8319 {
8320 col = fp->col + 1;
8321#ifdef FEAT_VIRTUALEDIT
8322 /* col(".") when the cursor is on the NUL at the end of the line
8323 * because of "coladd" can be seen as an extra column. */
8324 if (virtual_active() && fp == &curwin->w_cursor)
8325 {
8326 char_u *p = ml_get_cursor();
8327
8328 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8329 curwin->w_virtcol - curwin->w_cursor.coladd))
8330 {
8331# ifdef FEAT_MBYTE
8332 int l;
8333
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008334 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 col += l;
8336# else
8337 if (*p != NUL && p[1] == NUL)
8338 ++col;
8339# endif
8340 }
8341 }
8342#endif
8343 }
8344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008345 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
8347
Bram Moolenaar572cb562005-08-05 21:35:02 +00008348#if defined(FEAT_INS_EXPAND)
8349/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008350 * "complete()" function
8351 */
8352/*ARGSUSED*/
8353 static void
8354f_complete(argvars, rettv)
8355 typval_T *argvars;
8356 typval_T *rettv;
8357{
8358 int startcol;
8359
8360 if ((State & INSERT) == 0)
8361 {
8362 EMSG(_("E785: complete() can only be used in Insert mode"));
8363 return;
8364 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008365
8366 /* Check for undo allowed here, because if something was already inserted
8367 * the line was already saved for undo and this check isn't done. */
8368 if (!undo_allowed())
8369 return;
8370
Bram Moolenaarade00832006-03-10 21:46:58 +00008371 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8372 {
8373 EMSG(_(e_invarg));
8374 return;
8375 }
8376
8377 startcol = get_tv_number_chk(&argvars[0], NULL);
8378 if (startcol <= 0)
8379 return;
8380
8381 set_completion(startcol - 1, argvars[1].vval.v_list);
8382}
8383
8384/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008385 * "complete_add()" function
8386 */
8387/*ARGSUSED*/
8388 static void
8389f_complete_add(argvars, rettv)
8390 typval_T *argvars;
8391 typval_T *rettv;
8392{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008393 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008394}
8395
8396/*
8397 * "complete_check()" function
8398 */
8399/*ARGSUSED*/
8400 static void
8401f_complete_check(argvars, rettv)
8402 typval_T *argvars;
8403 typval_T *rettv;
8404{
8405 int saved = RedrawingDisabled;
8406
8407 RedrawingDisabled = 0;
8408 ins_compl_check_keys(0);
8409 rettv->vval.v_number = compl_interrupted;
8410 RedrawingDisabled = saved;
8411}
8412#endif
8413
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414/*
8415 * "confirm(message, buttons[, default [, type]])" function
8416 */
8417/*ARGSUSED*/
8418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008419f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008420 typval_T *argvars;
8421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
8423#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8424 char_u *message;
8425 char_u *buttons = NULL;
8426 char_u buf[NUMBUFLEN];
8427 char_u buf2[NUMBUFLEN];
8428 int def = 1;
8429 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008430 char_u *typestr;
8431 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008433 message = get_tv_string_chk(&argvars[0]);
8434 if (message == NULL)
8435 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008436 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8439 if (buttons == NULL)
8440 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008441 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008443 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008444 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008446 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8447 if (typestr == NULL)
8448 error = TRUE;
8449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 switch (TOUPPER_ASC(*typestr))
8452 {
8453 case 'E': type = VIM_ERROR; break;
8454 case 'Q': type = VIM_QUESTION; break;
8455 case 'I': type = VIM_INFO; break;
8456 case 'W': type = VIM_WARNING; break;
8457 case 'G': type = VIM_GENERIC; break;
8458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 }
8460 }
8461 }
8462 }
8463
8464 if (buttons == NULL || *buttons == NUL)
8465 buttons = (char_u *)_("&Ok");
8466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008467 if (error)
8468 rettv->vval.v_number = 0;
8469 else
8470 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 def, NULL);
8472#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008473 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474#endif
8475}
8476
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477/*
8478 * "copy()" function
8479 */
8480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008481f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 typval_T *argvars;
8483 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008485 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487
8488/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008489 * "count()" function
8490 */
8491 static void
8492f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 typval_T *argvars;
8494 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008495{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008496 long n = 0;
8497 int ic = FALSE;
8498
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008500 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 listitem_T *li;
8502 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008504
Bram Moolenaare9a41262005-01-15 22:18:47 +00008505 if ((l = argvars[0].vval.v_list) != NULL)
8506 {
8507 li = l->lv_first;
8508 if (argvars[2].v_type != VAR_UNKNOWN)
8509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510 int error = FALSE;
8511
8512 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008513 if (argvars[3].v_type != VAR_UNKNOWN)
8514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008515 idx = get_tv_number_chk(&argvars[3], &error);
8516 if (!error)
8517 {
8518 li = list_find(l, idx);
8519 if (li == NULL)
8520 EMSGN(_(e_listidx), idx);
8521 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523 if (error)
8524 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008525 }
8526
8527 for ( ; li != NULL; li = li->li_next)
8528 if (tv_equal(&li->li_tv, &argvars[1], ic))
8529 ++n;
8530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008531 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532 else if (argvars[0].v_type == VAR_DICT)
8533 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008534 int todo;
8535 dict_T *d;
8536 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008537
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008538 if ((d = argvars[0].vval.v_dict) != NULL)
8539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008540 int error = FALSE;
8541
Bram Moolenaare9a41262005-01-15 22:18:47 +00008542 if (argvars[2].v_type != VAR_UNKNOWN)
8543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008544 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008545 if (argvars[3].v_type != VAR_UNKNOWN)
8546 EMSG(_(e_invarg));
8547 }
8548
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008549 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008551 {
8552 if (!HASHITEM_EMPTY(hi))
8553 {
8554 --todo;
8555 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8556 ++n;
8557 }
8558 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008559 }
8560 }
8561 else
8562 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008563 rettv->vval.v_number = n;
8564}
8565
8566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8568 *
8569 * Checks the existence of a cscope connection.
8570 */
8571/*ARGSUSED*/
8572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008574 typval_T *argvars;
8575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577#ifdef FEAT_CSCOPE
8578 int num = 0;
8579 char_u *dbpath = NULL;
8580 char_u *prepend = NULL;
8581 char_u buf[NUMBUFLEN];
8582
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008583 if (argvars[0].v_type != VAR_UNKNOWN
8584 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 num = (int)get_tv_number(&argvars[0]);
8587 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008588 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 }
8591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#endif
8596}
8597
8598/*
8599 * "cursor(lnum, col)" function
8600 *
8601 * Moves the cursor to the specified line and column
8602 */
8603/*ARGSUSED*/
8604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008605f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 typval_T *argvars;
8607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008610#ifdef FEAT_VIRTUALEDIT
8611 long coladd = 0;
8612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
Bram Moolenaara5525202006-03-02 22:52:09 +00008614 if (argvars[1].v_type == VAR_UNKNOWN)
8615 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008616 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008617
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008618 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008619 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008620 line = pos.lnum;
8621 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008622#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008623 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008624#endif
8625 }
8626 else
8627 {
8628 line = get_tv_lnum(argvars);
8629 col = get_tv_number_chk(&argvars[1], NULL);
8630#ifdef FEAT_VIRTUALEDIT
8631 if (argvars[2].v_type != VAR_UNKNOWN)
8632 coladd = get_tv_number_chk(&argvars[2], NULL);
8633#endif
8634 }
8635 if (line < 0 || col < 0
8636#ifdef FEAT_VIRTUALEDIT
8637 || coladd < 0
8638#endif
8639 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 if (line > 0)
8642 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (col > 0)
8644 curwin->w_cursor.col = col - 1;
8645#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008646 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647#endif
8648
8649 /* Make sure the cursor is in a valid position. */
8650 check_cursor();
8651#ifdef FEAT_MBYTE
8652 /* Correct cursor for multi-byte character. */
8653 if (has_mbyte)
8654 mb_adjust_cursor();
8655#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008656
8657 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658}
8659
8660/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 */
8663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 typval_T *argvars;
8666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008668 int noref = 0;
8669
8670 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008671 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008672 if (noref < 0 || noref > 1)
8673 EMSG(_(e_invarg));
8674 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008675 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676}
8677
8678/*
8679 * "delete()" function
8680 */
8681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 typval_T *argvars;
8684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690}
8691
8692/*
8693 * "did_filetype()" function
8694 */
8695/*ARGSUSED*/
8696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008698 typval_T *argvars;
8699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705#endif
8706}
8707
8708/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008709 * "diff_filler()" function
8710 */
8711/*ARGSUSED*/
8712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 typval_T *argvars;
8715 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008716{
8717#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008718 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008719#endif
8720}
8721
8722/*
8723 * "diff_hlID()" function
8724 */
8725/*ARGSUSED*/
8726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 typval_T *argvars;
8729 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008730{
8731#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008732 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008733 static linenr_T prev_lnum = 0;
8734 static int changedtick = 0;
8735 static int fnum = 0;
8736 static int change_start = 0;
8737 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00008738 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008739 int filler_lines;
8740 int col;
8741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008742 if (lnum < 0) /* ignore type error in {lnum} arg */
8743 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008744 if (lnum != prev_lnum
8745 || changedtick != curbuf->b_changedtick
8746 || fnum != curbuf->b_fnum)
8747 {
8748 /* New line, buffer, change: need to get the values. */
8749 filler_lines = diff_check(curwin, lnum);
8750 if (filler_lines < 0)
8751 {
8752 if (filler_lines == -1)
8753 {
8754 change_start = MAXCOL;
8755 change_end = -1;
8756 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8757 hlID = HLF_ADD; /* added line */
8758 else
8759 hlID = HLF_CHD; /* changed line */
8760 }
8761 else
8762 hlID = HLF_ADD; /* added line */
8763 }
8764 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008765 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008766 prev_lnum = lnum;
8767 changedtick = curbuf->b_changedtick;
8768 fnum = curbuf->b_fnum;
8769 }
8770
8771 if (hlID == HLF_CHD || hlID == HLF_TXD)
8772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008774 if (col >= change_start && col <= change_end)
8775 hlID = HLF_TXD; /* changed text */
8776 else
8777 hlID = HLF_CHD; /* changed line */
8778 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008779 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008780#endif
8781}
8782
8783/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008784 * "empty({expr})" function
8785 */
8786 static void
8787f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008788 typval_T *argvars;
8789 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008790{
8791 int n;
8792
8793 switch (argvars[0].v_type)
8794 {
8795 case VAR_STRING:
8796 case VAR_FUNC:
8797 n = argvars[0].vval.v_string == NULL
8798 || *argvars[0].vval.v_string == NUL;
8799 break;
8800 case VAR_NUMBER:
8801 n = argvars[0].vval.v_number == 0;
8802 break;
8803 case VAR_LIST:
8804 n = argvars[0].vval.v_list == NULL
8805 || argvars[0].vval.v_list->lv_first == NULL;
8806 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008807 case VAR_DICT:
8808 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008810 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008811 default:
8812 EMSG2(_(e_intern2), "f_empty()");
8813 n = 0;
8814 }
8815
8816 rettv->vval.v_number = n;
8817}
8818
8819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 * "escape({string}, {chars})" function
8821 */
8822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *argvars;
8825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
8827 char_u buf[NUMBUFLEN];
8828
Bram Moolenaar758711c2005-02-02 23:11:38 +00008829 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8830 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
8834/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008835 * "eval()" function
8836 */
8837/*ARGSUSED*/
8838 static void
8839f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008840 typval_T *argvars;
8841 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008842{
8843 char_u *s;
8844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008845 s = get_tv_string_chk(&argvars[0]);
8846 if (s != NULL)
8847 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008849 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8850 {
8851 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008852 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008853 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008854 else if (*s != NUL)
8855 EMSG(_(e_trailing));
8856}
8857
8858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 * "eventhandler()" function
8860 */
8861/*ARGSUSED*/
8862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 typval_T *argvars;
8865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868}
8869
8870/*
8871 * "executable()" function
8872 */
8873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008875 typval_T *argvars;
8876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879}
8880
8881/*
8882 * "exists()" function
8883 */
8884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008885f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008886 typval_T *argvars;
8887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 char_u *p;
8890 char_u *name;
8891 int n = FALSE;
8892 int len = 0;
8893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 if (*p == '$') /* environment variable */
8896 {
8897 /* first try "normal" environment variables (fast) */
8898 if (mch_getenv(p + 1) != NULL)
8899 n = TRUE;
8900 else
8901 {
8902 /* try expanding things like $VIM and ${HOME} */
8903 p = expand_env_save(p);
8904 if (p != NULL && *p != '$')
8905 n = TRUE;
8906 vim_free(p);
8907 }
8908 }
8909 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008912 if (*skipwhite(p) != NUL)
8913 n = FALSE; /* trailing garbage */
8914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 else if (*p == '*') /* internal or user defined function */
8916 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008917 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919 else if (*p == ':')
8920 {
8921 n = cmd_exists(p + 1);
8922 }
8923 else if (*p == '#')
8924 {
8925#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008926 if (p[1] == '#')
8927 n = autocmd_supported(p + 2);
8928 else
8929 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930#endif
8931 }
8932 else /* internal variable */
8933 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008934 char_u *tofree;
8935 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008937 /* get_name_len() takes care of expanding curly braces */
8938 name = p;
8939 len = get_name_len(&p, &tofree, TRUE, FALSE);
8940 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008942 if (tofree != NULL)
8943 name = tofree;
8944 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8945 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008947 /* handle d.key, l[idx], f(expr) */
8948 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8949 if (n)
8950 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 }
8952 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008953 if (*p != NUL)
8954 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008956 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960}
8961
8962/*
8963 * "expand()" function
8964 */
8965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008967 typval_T *argvars;
8968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970 char_u *s;
8971 int len;
8972 char_u *errormsg;
8973 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8974 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 rettv->v_type = VAR_STRING;
8978 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 if (*s == '%' || *s == '#' || *s == '<')
8980 {
8981 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008982 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 --emsg_off;
8984 }
8985 else
8986 {
8987 /* When the optional second argument is non-zero, don't remove matches
8988 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008989 if (argvars[1].v_type != VAR_UNKNOWN
8990 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 if (!error)
8993 {
8994 ExpandInit(&xpc);
8995 xpc.xp_context = EXPAND_FILES;
8996 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008997 }
8998 else
8999 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 }
9001}
9002
9003/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009004 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009005 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009006 */
9007 static void
9008f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 typval_T *argvars;
9010 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009011{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009012 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 list_T *l1, *l2;
9016 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009017 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009018 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009019
Bram Moolenaare9a41262005-01-15 22:18:47 +00009020 l1 = argvars[0].vval.v_list;
9021 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009022 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9023 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009024 {
9025 if (argvars[2].v_type != VAR_UNKNOWN)
9026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009027 before = get_tv_number_chk(&argvars[2], &error);
9028 if (error)
9029 return; /* type error; errmsg already given */
9030
Bram Moolenaar758711c2005-02-02 23:11:38 +00009031 if (before == l1->lv_len)
9032 item = NULL;
9033 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009034 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009035 item = list_find(l1, before);
9036 if (item == NULL)
9037 {
9038 EMSGN(_(e_listidx), before);
9039 return;
9040 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 }
9042 }
9043 else
9044 item = NULL;
9045 list_extend(l1, l2, item);
9046
Bram Moolenaare9a41262005-01-15 22:18:47 +00009047 copy_tv(&argvars[0], rettv);
9048 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009049 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009050 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9051 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 dict_T *d1, *d2;
9053 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009054 char_u *action;
9055 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009056 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009057 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009058
9059 d1 = argvars[0].vval.v_dict;
9060 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009061 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9062 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009063 {
9064 /* Check the third argument. */
9065 if (argvars[2].v_type != VAR_UNKNOWN)
9066 {
9067 static char *(av[]) = {"keep", "force", "error"};
9068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 action = get_tv_string_chk(&argvars[2]);
9070 if (action == NULL)
9071 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009072 for (i = 0; i < 3; ++i)
9073 if (STRCMP(action, av[i]) == 0)
9074 break;
9075 if (i == 3)
9076 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009077 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009078 return;
9079 }
9080 }
9081 else
9082 action = (char_u *)"force";
9083
9084 /* Go over all entries in the second dict and add them to the
9085 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009086 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009087 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009089 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009091 --todo;
9092 di1 = dict_find(d1, hi2->hi_key, -1);
9093 if (di1 == NULL)
9094 {
9095 di1 = dictitem_copy(HI2DI(hi2));
9096 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9097 dictitem_free(di1);
9098 }
9099 else if (*action == 'e')
9100 {
9101 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9102 break;
9103 }
9104 else if (*action == 'f')
9105 {
9106 clear_tv(&di1->di_tv);
9107 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9108 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009109 }
9110 }
9111
Bram Moolenaare9a41262005-01-15 22:18:47 +00009112 copy_tv(&argvars[0], rettv);
9113 }
9114 }
9115 else
9116 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009117}
9118
9119/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009120 * "feedkeys()" function
9121 */
9122/*ARGSUSED*/
9123 static void
9124f_feedkeys(argvars, rettv)
9125 typval_T *argvars;
9126 typval_T *rettv;
9127{
9128 int remap = TRUE;
9129 char_u *keys, *flags;
9130 char_u nbuf[NUMBUFLEN];
9131 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009132 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009133
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009134 /* This is not allowed in the sandbox. If the commands would still be
9135 * executed in the sandbox it would be OK, but it probably happens later,
9136 * when "sandbox" is no longer set. */
9137 if (check_secure())
9138 return;
9139
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009140 rettv->vval.v_number = 0;
9141 keys = get_tv_string(&argvars[0]);
9142 if (*keys != NUL)
9143 {
9144 if (argvars[1].v_type != VAR_UNKNOWN)
9145 {
9146 flags = get_tv_string_buf(&argvars[1], nbuf);
9147 for ( ; *flags != NUL; ++flags)
9148 {
9149 switch (*flags)
9150 {
9151 case 'n': remap = FALSE; break;
9152 case 'm': remap = TRUE; break;
9153 case 't': typed = TRUE; break;
9154 }
9155 }
9156 }
9157
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009158 /* Need to escape K_SPECIAL and CSI before putting the string in the
9159 * typeahead buffer. */
9160 keys_esc = vim_strsave_escape_csi(keys);
9161 if (keys_esc != NULL)
9162 {
9163 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009164 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009165 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009166 if (vgetc_busy)
9167 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009168 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009169 }
9170}
9171
9172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 * "filereadable()" function
9174 */
9175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009177 typval_T *argvars;
9178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
9180 FILE *fd;
9181 char_u *p;
9182 int n;
9183
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9186 {
9187 n = TRUE;
9188 fclose(fd);
9189 }
9190 else
9191 n = FALSE;
9192
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
9195
9196/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009197 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 * rights to write into.
9199 */
9200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009202 typval_T *argvars;
9203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009205 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206}
9207
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009208static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009209
9210 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009211findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009212 typval_T *argvars;
9213 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009214 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009215{
9216#ifdef FEAT_SEARCHPATH
9217 char_u *fname;
9218 char_u *fresult = NULL;
9219 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9220 char_u *p;
9221 char_u pathbuf[NUMBUFLEN];
9222 int count = 1;
9223 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009224 int error = FALSE;
9225#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009226
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009227 rettv->vval.v_string = NULL;
9228 rettv->v_type = VAR_STRING;
9229
9230#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009232
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009233 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009235 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9236 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009237 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 else
9239 {
9240 if (*p != NUL)
9241 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009244 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009246 }
9247
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009248 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9249 error = TRUE;
9250
9251 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 do
9254 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009255 if (rettv->v_type == VAR_STRING)
9256 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009257 fresult = find_file_in_path_option(first ? fname : NULL,
9258 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009259 0, first, path,
9260 find_what,
9261 curbuf->b_ffname,
9262 find_what == FINDFILE_DIR
9263 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009265
9266 if (fresult != NULL && rettv->v_type == VAR_LIST)
9267 list_append_string(rettv->vval.v_list, fresult, -1);
9268
9269 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009270 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009271
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009272 if (rettv->v_type == VAR_STRING)
9273 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009274#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009275}
9276
Bram Moolenaar33570922005-01-25 22:26:29 +00009277static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9278static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009279
9280/*
9281 * Implementation of map() and filter().
9282 */
9283 static void
9284filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009285 typval_T *argvars;
9286 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009287 int map;
9288{
9289 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009291 listitem_T *li, *nli;
9292 list_T *l = NULL;
9293 dictitem_T *di;
9294 hashtab_T *ht;
9295 hashitem_T *hi;
9296 dict_T *d = NULL;
9297 typval_T save_val;
9298 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009299 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009300 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009301 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009302 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009303
9304 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009305 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009306 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009307 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009308 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009309 return;
9310 }
9311 else if (argvars[0].v_type == VAR_DICT)
9312 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009313 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009314 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009315 return;
9316 }
9317 else
9318 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009319 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009320 return;
9321 }
9322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 expr = get_tv_string_buf_chk(&argvars[1], buf);
9324 /* On type errors, the preceding call has already displayed an error
9325 * message. Avoid a misleading error message for an empty string that
9326 * was not passed as argument. */
9327 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 prepare_vimvar(VV_VAL, &save_val);
9330 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009331
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009332 /* We reset "did_emsg" to be able to detect whether an error
9333 * occurred during evaluation of the expression. */
9334 save_did_emsg = did_emsg;
9335 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009339 prepare_vimvar(VV_KEY, &save_key);
9340 vimvars[VV_KEY].vv_type = VAR_STRING;
9341
9342 ht = &d->dv_hashtab;
9343 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009344 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009347 if (!HASHITEM_EMPTY(hi))
9348 {
9349 --todo;
9350 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009351 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009352 break;
9353 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009354 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009355 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 break;
9357 if (!map && rem)
9358 dictitem_remove(d, di);
9359 clear_tv(&vimvars[VV_KEY].vv_tv);
9360 }
9361 }
9362 hash_unlock(ht);
9363
9364 restore_vimvar(VV_KEY, &save_key);
9365 }
9366 else
9367 {
9368 for (li = l->lv_first; li != NULL; li = nli)
9369 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009370 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009371 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009372 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009373 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009374 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009375 break;
9376 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009377 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009378 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009379 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009382
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009383 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009385
9386 copy_tv(&argvars[0], rettv);
9387}
9388
9389 static int
9390filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 char_u *expr;
9393 int map;
9394 int *remp;
9395{
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009397 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009398 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009399
Bram Moolenaar33570922005-01-25 22:26:29 +00009400 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009401 s = expr;
9402 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009403 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009404 if (*s != NUL) /* check for trailing chars after expr */
9405 {
9406 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009407 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009408 }
9409 if (map)
9410 {
9411 /* map(): replace the list item value */
9412 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009413 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009414 *tv = rettv;
9415 }
9416 else
9417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009418 int error = FALSE;
9419
Bram Moolenaare9a41262005-01-15 22:18:47 +00009420 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009422 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 /* On type error, nothing has been removed; return FAIL to stop the
9424 * loop. The error message was given by get_tv_number_chk(). */
9425 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009426 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009427 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009428 retval = OK;
9429theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009430 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009431 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009432}
9433
9434/*
9435 * "filter()" function
9436 */
9437 static void
9438f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 typval_T *argvars;
9440 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009441{
9442 filter_map(argvars, rettv, FALSE);
9443}
9444
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009445/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009446 * "finddir({fname}[, {path}[, {count}]])" function
9447 */
9448 static void
9449f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009450 typval_T *argvars;
9451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009452{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009453 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009454}
9455
9456/*
9457 * "findfile({fname}[, {path}[, {count}]])" function
9458 */
9459 static void
9460f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009461 typval_T *argvars;
9462 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009463{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009464 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465}
9466
9467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 * "fnamemodify({fname}, {mods})" function
9469 */
9470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009472 typval_T *argvars;
9473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 char_u *fname;
9476 char_u *mods;
9477 int usedlen = 0;
9478 int len;
9479 char_u *fbuf = NULL;
9480 char_u buf[NUMBUFLEN];
9481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009482 fname = get_tv_string_chk(&argvars[0]);
9483 mods = get_tv_string_buf_chk(&argvars[1], buf);
9484 if (fname == NULL || mods == NULL)
9485 fname = NULL;
9486 else
9487 {
9488 len = (int)STRLEN(fname);
9489 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 vim_free(fbuf);
9498}
9499
Bram Moolenaar33570922005-01-25 22:26:29 +00009500static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502/*
9503 * "foldclosed()" function
9504 */
9505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009507 typval_T *argvars;
9508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 int end;
9510{
9511#ifdef FEAT_FOLDING
9512 linenr_T lnum;
9513 linenr_T first, last;
9514
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9517 {
9518 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9519 {
9520 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 return;
9525 }
9526 }
9527#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529}
9530
9531/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009532 * "foldclosed()" function
9533 */
9534 static void
9535f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009536 typval_T *argvars;
9537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009538{
9539 foldclosed_both(argvars, rettv, FALSE);
9540}
9541
9542/*
9543 * "foldclosedend()" function
9544 */
9545 static void
9546f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009547 typval_T *argvars;
9548 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009549{
9550 foldclosed_both(argvars, rettv, TRUE);
9551}
9552
9553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 * "foldlevel()" function
9555 */
9556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009558 typval_T *argvars;
9559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560{
9561#ifdef FEAT_FOLDING
9562 linenr_T lnum;
9563
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 else
9568#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570}
9571
9572/*
9573 * "foldtext()" function
9574 */
9575/*ARGSUSED*/
9576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009578 typval_T *argvars;
9579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581#ifdef FEAT_FOLDING
9582 linenr_T lnum;
9583 char_u *s;
9584 char_u *r;
9585 int len;
9586 char *txt;
9587#endif
9588
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 rettv->v_type = VAR_STRING;
9590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009592 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9593 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9594 <= curbuf->b_ml.ml_line_count
9595 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 {
9597 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009598 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9599 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
9601 if (!linewhite(lnum))
9602 break;
9603 ++lnum;
9604 }
9605
9606 /* Find interesting text in this line. */
9607 s = skipwhite(ml_get(lnum));
9608 /* skip C comment-start */
9609 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009612 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009614 {
9615 s = skipwhite(ml_get(lnum + 1));
9616 if (*s == '*')
9617 s = skipwhite(s + 1);
9618 }
9619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 txt = _("+-%s%3ld lines: ");
9621 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 + 20 /* for %3ld */
9624 + STRLEN(s))); /* concatenated */
9625 if (r != NULL)
9626 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009627 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9628 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9629 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 len = (int)STRLEN(r);
9631 STRCAT(r, s);
9632 /* remove 'foldmarker' and 'commentstring' */
9633 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 }
9636 }
9637#endif
9638}
9639
9640/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009641 * "foldtextresult(lnum)" function
9642 */
9643/*ARGSUSED*/
9644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009646 typval_T *argvars;
9647 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009648{
9649#ifdef FEAT_FOLDING
9650 linenr_T lnum;
9651 char_u *text;
9652 char_u buf[51];
9653 foldinfo_T foldinfo;
9654 int fold_count;
9655#endif
9656
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 rettv->v_type = VAR_STRING;
9658 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009659#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009661 /* treat illegal types and illegal string values for {lnum} the same */
9662 if (lnum < 0)
9663 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009664 fold_count = foldedCount(curwin, lnum, &foldinfo);
9665 if (fold_count > 0)
9666 {
9667 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9668 &foldinfo, buf);
9669 if (text == buf)
9670 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009672 }
9673#endif
9674}
9675
9676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 * "foreground()" function
9678 */
9679/*ARGSUSED*/
9680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009682 typval_T *argvars;
9683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686#ifdef FEAT_GUI
9687 if (gui.in_use)
9688 gui_mch_set_foreground();
9689#else
9690# ifdef WIN32
9691 win32_set_foreground();
9692# endif
9693#endif
9694}
9695
9696/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009697 * "function()" function
9698 */
9699/*ARGSUSED*/
9700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009702 typval_T *argvars;
9703 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009704{
9705 char_u *s;
9706
Bram Moolenaara7043832005-01-21 11:56:39 +00009707 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009709 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009710 EMSG2(_(e_invarg2), s);
9711 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009712 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009713 else
9714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->vval.v_string = vim_strsave(s);
9716 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009717 }
9718}
9719
9720/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009721 * "garbagecollect()" function
9722 */
9723/*ARGSUSED*/
9724 static void
9725f_garbagecollect(argvars, rettv)
9726 typval_T *argvars;
9727 typval_T *rettv;
9728{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009729 /* This is postponed until we are back at the toplevel, because we may be
9730 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9731 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00009732
9733 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
9734 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009735}
9736
9737/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009738 * "get()" function
9739 */
9740 static void
9741f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009742 typval_T *argvars;
9743 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009744{
Bram Moolenaar33570922005-01-25 22:26:29 +00009745 listitem_T *li;
9746 list_T *l;
9747 dictitem_T *di;
9748 dict_T *d;
9749 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009750
Bram Moolenaare9a41262005-01-15 22:18:47 +00009751 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009752 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009753 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 int error = FALSE;
9756
9757 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9758 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009759 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009760 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009762 else if (argvars[0].v_type == VAR_DICT)
9763 {
9764 if ((d = argvars[0].vval.v_dict) != NULL)
9765 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009766 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009767 if (di != NULL)
9768 tv = &di->di_tv;
9769 }
9770 }
9771 else
9772 EMSG2(_(e_listdictarg), "get()");
9773
9774 if (tv == NULL)
9775 {
9776 if (argvars[2].v_type == VAR_UNKNOWN)
9777 rettv->vval.v_number = 0;
9778 else
9779 copy_tv(&argvars[2], rettv);
9780 }
9781 else
9782 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009783}
9784
Bram Moolenaar342337a2005-07-21 21:11:17 +00009785static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009786
9787/*
9788 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009789 * Return a range (from start to end) of lines in rettv from the specified
9790 * buffer.
9791 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009792 */
9793 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009794get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009795 buf_T *buf;
9796 linenr_T start;
9797 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009798 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009799 typval_T *rettv;
9800{
9801 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009802
Bram Moolenaar342337a2005-07-21 21:11:17 +00009803 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009804 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009805 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009806 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009807 }
9808 else
9809 rettv->vval.v_number = 0;
9810
9811 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9812 return;
9813
9814 if (!retlist)
9815 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009816 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9817 p = ml_get_buf(buf, start, FALSE);
9818 else
9819 p = (char_u *)"";
9820
9821 rettv->v_type = VAR_STRING;
9822 rettv->vval.v_string = vim_strsave(p);
9823 }
9824 else
9825 {
9826 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009827 return;
9828
9829 if (start < 1)
9830 start = 1;
9831 if (end > buf->b_ml.ml_line_count)
9832 end = buf->b_ml.ml_line_count;
9833 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009834 if (list_append_string(rettv->vval.v_list,
9835 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009836 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009837 }
9838}
9839
9840/*
9841 * "getbufline()" function
9842 */
9843 static void
9844f_getbufline(argvars, rettv)
9845 typval_T *argvars;
9846 typval_T *rettv;
9847{
9848 linenr_T lnum;
9849 linenr_T end;
9850 buf_T *buf;
9851
9852 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9853 ++emsg_off;
9854 buf = get_buf_tv(&argvars[0]);
9855 --emsg_off;
9856
Bram Moolenaar661b1822005-07-28 22:36:45 +00009857 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009858 if (argvars[2].v_type == VAR_UNKNOWN)
9859 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009860 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009861 end = get_tv_lnum_buf(&argvars[2], buf);
9862
Bram Moolenaar342337a2005-07-21 21:11:17 +00009863 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009864}
9865
Bram Moolenaar0d660222005-01-07 21:51:51 +00009866/*
9867 * "getbufvar()" function
9868 */
9869 static void
9870f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009871 typval_T *argvars;
9872 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009873{
9874 buf_T *buf;
9875 buf_T *save_curbuf;
9876 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009877 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9880 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009881 ++emsg_off;
9882 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009883
9884 rettv->v_type = VAR_STRING;
9885 rettv->vval.v_string = NULL;
9886
9887 if (buf != NULL && varname != NULL)
9888 {
9889 if (*varname == '&') /* buffer-local-option */
9890 {
9891 /* set curbuf to be our buf, temporarily */
9892 save_curbuf = curbuf;
9893 curbuf = buf;
9894
9895 get_option_tv(&varname, rettv, TRUE);
9896
9897 /* restore previous notion of curbuf */
9898 curbuf = save_curbuf;
9899 }
9900 else
9901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009902 if (*varname == NUL)
9903 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9904 * scope prefix before the NUL byte is required by
9905 * find_var_in_ht(). */
9906 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009907 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009908 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009909 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009910 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009911 }
9912 }
9913
9914 --emsg_off;
9915}
9916
9917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 * "getchar()" function
9919 */
9920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 typval_T *argvars;
9923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009926 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009928 /* Position the cursor. Needed after a message that ends in a space. */
9929 windgoto(msg_row, msg_col);
9930
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 ++no_mapping;
9932 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009933 for (;;)
9934 {
9935 if (argvars[0].v_type == VAR_UNKNOWN)
9936 /* getchar(): blocking wait. */
9937 n = safe_vgetc();
9938 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9939 /* getchar(1): only check if char avail */
9940 n = vpeekc();
9941 else if (error || vpeekc() == NUL)
9942 /* illegal argument or getchar(0) and no char avail: return zero */
9943 n = 0;
9944 else
9945 /* getchar(0) and char avail: return char */
9946 n = safe_vgetc();
9947 if (n == K_IGNORE)
9948 continue;
9949 break;
9950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 --no_mapping;
9952 --allow_keys;
9953
Bram Moolenaar219b8702006-11-01 14:32:36 +00009954 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9955 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9956 vimvars[VV_MOUSE_COL].vv_nr = 0;
9957
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009958 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 if (IS_SPECIAL(n) || mod_mask != 0)
9960 {
9961 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9962 int i = 0;
9963
9964 /* Turn a special key into three bytes, plus modifier. */
9965 if (mod_mask != 0)
9966 {
9967 temp[i++] = K_SPECIAL;
9968 temp[i++] = KS_MODIFIER;
9969 temp[i++] = mod_mask;
9970 }
9971 if (IS_SPECIAL(n))
9972 {
9973 temp[i++] = K_SPECIAL;
9974 temp[i++] = K_SECOND(n);
9975 temp[i++] = K_THIRD(n);
9976 }
9977#ifdef FEAT_MBYTE
9978 else if (has_mbyte)
9979 i += (*mb_char2bytes)(n, temp + i);
9980#endif
9981 else
9982 temp[i++] = n;
9983 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984 rettv->v_type = VAR_STRING;
9985 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009986
9987#ifdef FEAT_MOUSE
9988 if (n == K_LEFTMOUSE
9989 || n == K_LEFTMOUSE_NM
9990 || n == K_LEFTDRAG
9991 || n == K_LEFTRELEASE
9992 || n == K_LEFTRELEASE_NM
9993 || n == K_MIDDLEMOUSE
9994 || n == K_MIDDLEDRAG
9995 || n == K_MIDDLERELEASE
9996 || n == K_RIGHTMOUSE
9997 || n == K_RIGHTDRAG
9998 || n == K_RIGHTRELEASE
9999 || n == K_X1MOUSE
10000 || n == K_X1DRAG
10001 || n == K_X1RELEASE
10002 || n == K_X2MOUSE
10003 || n == K_X2DRAG
10004 || n == K_X2RELEASE
10005 || n == K_MOUSEDOWN
10006 || n == K_MOUSEUP)
10007 {
10008 int row = mouse_row;
10009 int col = mouse_col;
10010 win_T *win;
10011 linenr_T lnum;
10012# ifdef FEAT_WINDOWS
10013 win_T *wp;
10014# endif
10015 int n = 1;
10016
10017 if (row >= 0 && col >= 0)
10018 {
10019 /* Find the window at the mouse coordinates and compute the
10020 * text position. */
10021 win = mouse_find_win(&row, &col);
10022 (void)mouse_comp_pos(win, &row, &col, &lnum);
10023# ifdef FEAT_WINDOWS
10024 for (wp = firstwin; wp != win; wp = wp->w_next)
10025 ++n;
10026# endif
10027 vimvars[VV_MOUSE_WIN].vv_nr = n;
10028 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10029 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10030 }
10031 }
10032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 }
10034}
10035
10036/*
10037 * "getcharmod()" function
10038 */
10039/*ARGSUSED*/
10040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010041f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010042 typval_T *argvars;
10043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * "getcmdline()" function
10050 */
10051/*ARGSUSED*/
10052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010054 typval_T *argvars;
10055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010057 rettv->v_type = VAR_STRING;
10058 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059}
10060
10061/*
10062 * "getcmdpos()" function
10063 */
10064/*ARGSUSED*/
10065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010067 typval_T *argvars;
10068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071}
10072
10073/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010074 * "getcmdtype()" function
10075 */
10076/*ARGSUSED*/
10077 static void
10078f_getcmdtype(argvars, rettv)
10079 typval_T *argvars;
10080 typval_T *rettv;
10081{
10082 rettv->v_type = VAR_STRING;
10083 rettv->vval.v_string = alloc(2);
10084 if (rettv->vval.v_string != NULL)
10085 {
10086 rettv->vval.v_string[0] = get_cmdline_type();
10087 rettv->vval.v_string[1] = NUL;
10088 }
10089}
10090
10091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 * "getcwd()" function
10093 */
10094/*ARGSUSED*/
10095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010097 typval_T *argvars;
10098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099{
10100 char_u cwd[MAXPATHL];
10101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 else
10106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010109 if (rettv->vval.v_string != NULL)
10110 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111#endif
10112 }
10113}
10114
10115/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010116 * "getfontname()" function
10117 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010118/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010121 typval_T *argvars;
10122 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010123{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->v_type = VAR_STRING;
10125 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010126#ifdef FEAT_GUI
10127 if (gui.in_use)
10128 {
10129 GuiFont font;
10130 char_u *name = NULL;
10131
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010132 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010133 {
10134 /* Get the "Normal" font. Either the name saved by
10135 * hl_set_font_name() or from the font ID. */
10136 font = gui.norm_font;
10137 name = hl_get_font_name();
10138 }
10139 else
10140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010142 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10143 return;
10144 font = gui_mch_get_font(name, FALSE);
10145 if (font == NOFONT)
10146 return; /* Invalid font name, return empty string. */
10147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010149 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010150 gui_mch_free_font(font);
10151 }
10152#endif
10153}
10154
10155/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010156 * "getfperm({fname})" function
10157 */
10158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010160 typval_T *argvars;
10161 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010162{
10163 char_u *fname;
10164 struct stat st;
10165 char_u *perm = NULL;
10166 char_u flags[] = "rwx";
10167 int i;
10168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010172 if (mch_stat((char *)fname, &st) >= 0)
10173 {
10174 perm = vim_strsave((char_u *)"---------");
10175 if (perm != NULL)
10176 {
10177 for (i = 0; i < 9; i++)
10178 {
10179 if (st.st_mode & (1 << (8 - i)))
10180 perm[i] = flags[i % 3];
10181 }
10182 }
10183 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010185}
10186
10187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 * "getfsize({fname})" function
10189 */
10190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010192 typval_T *argvars;
10193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194{
10195 char_u *fname;
10196 struct stat st;
10197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010198 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
10202 if (mch_stat((char *)fname, &st) >= 0)
10203 {
10204 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010209
10210 /* non-perfect check for overflow */
10211 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10212 rettv->vval.v_number = -2;
10213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 }
10215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217}
10218
10219/*
10220 * "getftime({fname})" function
10221 */
10222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 typval_T *argvars;
10225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 char_u *fname;
10228 struct stat st;
10229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010230 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231
10232 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010235 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236}
10237
10238/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010239 * "getftype({fname})" function
10240 */
10241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010242f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 typval_T *argvars;
10244 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010245{
10246 char_u *fname;
10247 struct stat st;
10248 char_u *type = NULL;
10249 char *t;
10250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010254 if (mch_lstat((char *)fname, &st) >= 0)
10255 {
10256#ifdef S_ISREG
10257 if (S_ISREG(st.st_mode))
10258 t = "file";
10259 else if (S_ISDIR(st.st_mode))
10260 t = "dir";
10261# ifdef S_ISLNK
10262 else if (S_ISLNK(st.st_mode))
10263 t = "link";
10264# endif
10265# ifdef S_ISBLK
10266 else if (S_ISBLK(st.st_mode))
10267 t = "bdev";
10268# endif
10269# ifdef S_ISCHR
10270 else if (S_ISCHR(st.st_mode))
10271 t = "cdev";
10272# endif
10273# ifdef S_ISFIFO
10274 else if (S_ISFIFO(st.st_mode))
10275 t = "fifo";
10276# endif
10277# ifdef S_ISSOCK
10278 else if (S_ISSOCK(st.st_mode))
10279 t = "fifo";
10280# endif
10281 else
10282 t = "other";
10283#else
10284# ifdef S_IFMT
10285 switch (st.st_mode & S_IFMT)
10286 {
10287 case S_IFREG: t = "file"; break;
10288 case S_IFDIR: t = "dir"; break;
10289# ifdef S_IFLNK
10290 case S_IFLNK: t = "link"; break;
10291# endif
10292# ifdef S_IFBLK
10293 case S_IFBLK: t = "bdev"; break;
10294# endif
10295# ifdef S_IFCHR
10296 case S_IFCHR: t = "cdev"; break;
10297# endif
10298# ifdef S_IFIFO
10299 case S_IFIFO: t = "fifo"; break;
10300# endif
10301# ifdef S_IFSOCK
10302 case S_IFSOCK: t = "socket"; break;
10303# endif
10304 default: t = "other";
10305 }
10306# else
10307 if (mch_isdir(fname))
10308 t = "dir";
10309 else
10310 t = "file";
10311# endif
10312#endif
10313 type = vim_strsave((char_u *)t);
10314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010316}
10317
10318/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010319 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010320 */
10321 static void
10322f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010323 typval_T *argvars;
10324 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010325{
10326 linenr_T lnum;
10327 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010328 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010329
10330 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010331 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010332 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010333 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010334 retlist = FALSE;
10335 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010336 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010337 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010338 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010339 retlist = TRUE;
10340 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010341
Bram Moolenaar342337a2005-07-21 21:11:17 +000010342 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010343}
10344
10345/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010346 * "getmatches()" function
10347 */
10348/*ARGSUSED*/
10349 static void
10350f_getmatches(argvars, rettv)
10351 typval_T *argvars;
10352 typval_T *rettv;
10353{
10354#ifdef FEAT_SEARCH_EXTRA
10355 dict_T *dict;
10356 matchitem_T *cur = curwin->w_match_head;
10357
10358 rettv->vval.v_number = 0;
10359
10360 if (rettv_list_alloc(rettv) == OK)
10361 {
10362 while (cur != NULL)
10363 {
10364 dict = dict_alloc();
10365 if (dict == NULL)
10366 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010367 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10368 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10369 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10370 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10371 list_append_dict(rettv->vval.v_list, dict);
10372 cur = cur->next;
10373 }
10374 }
10375#endif
10376}
10377
10378/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000010379 * "getpid()" function
10380 */
10381/*ARGSUSED*/
10382 static void
10383f_getpid(argvars, rettv)
10384 typval_T *argvars;
10385 typval_T *rettv;
10386{
10387 rettv->vval.v_number = mch_get_pid();
10388}
10389
10390/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010391 * "getpos(string)" function
10392 */
10393 static void
10394f_getpos(argvars, rettv)
10395 typval_T *argvars;
10396 typval_T *rettv;
10397{
10398 pos_T *fp;
10399 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010400 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010401
10402 if (rettv_list_alloc(rettv) == OK)
10403 {
10404 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010405 fp = var2fpos(&argvars[0], TRUE, &fnum);
10406 if (fnum != -1)
10407 list_append_number(l, (varnumber_T)fnum);
10408 else
10409 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010410 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10411 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010412 list_append_number(l, (fp != NULL)
10413 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010414 : (varnumber_T)0);
10415 list_append_number(l,
10416#ifdef FEAT_VIRTUALEDIT
10417 (fp != NULL) ? (varnumber_T)fp->coladd :
10418#endif
10419 (varnumber_T)0);
10420 }
10421 else
10422 rettv->vval.v_number = FALSE;
10423}
10424
10425/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010426 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010427 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010428/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010429 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010430f_getqflist(argvars, rettv)
10431 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010432 typval_T *rettv;
10433{
10434#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010435 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010436#endif
10437
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010438 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010439#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010440 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010441 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010442 wp = NULL;
10443 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10444 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010445 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010446 if (wp == NULL)
10447 return;
10448 }
10449
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010450 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010451 }
10452#endif
10453}
10454
10455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 * "getreg()" function
10457 */
10458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010460 typval_T *argvars;
10461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463 char_u *strregname;
10464 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010465 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010468 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010470 strregname = get_tv_string_chk(&argvars[0]);
10471 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010472 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010476 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 regname = (strregname == NULL ? '"' : *strregname);
10478 if (regname == 0)
10479 regname = '"';
10480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 rettv->vval.v_string = error ? NULL :
10483 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484}
10485
10486/*
10487 * "getregtype()" function
10488 */
10489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010490f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010491 typval_T *argvars;
10492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493{
10494 char_u *strregname;
10495 int regname;
10496 char_u buf[NUMBUFLEN + 2];
10497 long reglen = 0;
10498
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010499 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010500 {
10501 strregname = get_tv_string_chk(&argvars[0]);
10502 if (strregname == NULL) /* type error; errmsg already given */
10503 {
10504 rettv->v_type = VAR_STRING;
10505 rettv->vval.v_string = NULL;
10506 return;
10507 }
10508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 else
10510 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512
10513 regname = (strregname == NULL ? '"' : *strregname);
10514 if (regname == 0)
10515 regname = '"';
10516
10517 buf[0] = NUL;
10518 buf[1] = NUL;
10519 switch (get_reg_type(regname, &reglen))
10520 {
10521 case MLINE: buf[0] = 'V'; break;
10522 case MCHAR: buf[0] = 'v'; break;
10523#ifdef FEAT_VISUAL
10524 case MBLOCK:
10525 buf[0] = Ctrl_V;
10526 sprintf((char *)buf + 1, "%ld", reglen + 1);
10527 break;
10528#endif
10529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 rettv->v_type = VAR_STRING;
10531 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532}
10533
10534/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010535 * "gettabwinvar()" function
10536 */
10537 static void
10538f_gettabwinvar(argvars, rettv)
10539 typval_T *argvars;
10540 typval_T *rettv;
10541{
10542 getwinvar(argvars, rettv, 1);
10543}
10544
10545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 * "getwinposx()" function
10547 */
10548/*ARGSUSED*/
10549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010551 typval_T *argvars;
10552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555#ifdef FEAT_GUI
10556 if (gui.in_use)
10557 {
10558 int x, y;
10559
10560 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 }
10563#endif
10564}
10565
10566/*
10567 * "getwinposy()" function
10568 */
10569/*ARGSUSED*/
10570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010571f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010572 typval_T *argvars;
10573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576#ifdef FEAT_GUI
10577 if (gui.in_use)
10578 {
10579 int x, y;
10580
10581 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 }
10584#endif
10585}
10586
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010587/*
10588 * Find window specifed by "vp" in tabpage "tp".
10589 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010590 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010591find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010592 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010593 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010594{
10595#ifdef FEAT_WINDOWS
10596 win_T *wp;
10597#endif
10598 int nr;
10599
10600 nr = get_tv_number_chk(vp, NULL);
10601
10602#ifdef FEAT_WINDOWS
10603 if (nr < 0)
10604 return NULL;
10605 if (nr == 0)
10606 return curwin;
10607
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010608 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10609 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010610 if (--nr <= 0)
10611 break;
10612 return wp;
10613#else
10614 if (nr == 0 || nr == 1)
10615 return curwin;
10616 return NULL;
10617#endif
10618}
10619
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620/*
10621 * "getwinvar()" function
10622 */
10623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 typval_T *argvars;
10626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010628 getwinvar(argvars, rettv, 0);
10629}
10630
10631/*
10632 * getwinvar() and gettabwinvar()
10633 */
10634 static void
10635getwinvar(argvars, rettv, off)
10636 typval_T *argvars;
10637 typval_T *rettv;
10638 int off; /* 1 for gettabwinvar() */
10639{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 win_T *win, *oldcurwin;
10641 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010643 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010645#ifdef FEAT_WINDOWS
10646 if (off == 1)
10647 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10648 else
10649 tp = curtab;
10650#endif
10651 win = find_win_by_nr(&argvars[off], tp);
10652 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010653 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010655 rettv->v_type = VAR_STRING;
10656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657
10658 if (win != NULL && varname != NULL)
10659 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010660 /* Set curwin to be our win, temporarily. Also set curbuf, so
10661 * that we can get buffer-local options. */
10662 oldcurwin = curwin;
10663 curwin = win;
10664 curbuf = win->w_buffer;
10665
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 else
10669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 if (*varname == NUL)
10671 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10672 * scope prefix before the NUL byte is required by
10673 * find_var_in_ht(). */
10674 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010676 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010678 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010680
10681 /* restore previous notion of curwin */
10682 curwin = oldcurwin;
10683 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 }
10685
10686 --emsg_off;
10687}
10688
10689/*
10690 * "glob()" function
10691 */
10692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010694 typval_T *argvars;
10695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696{
10697 expand_T xpc;
10698
10699 ExpandInit(&xpc);
10700 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701 rettv->v_type = VAR_STRING;
10702 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704}
10705
10706/*
10707 * "globpath()" function
10708 */
10709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010711 typval_T *argvars;
10712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713{
10714 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010715 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010718 if (file == NULL)
10719 rettv->vval.v_string = NULL;
10720 else
10721 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722}
10723
10724/*
10725 * "has()" function
10726 */
10727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010729 typval_T *argvars;
10730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731{
10732 int i;
10733 char_u *name;
10734 int n = FALSE;
10735 static char *(has_list[]) =
10736 {
10737#ifdef AMIGA
10738 "amiga",
10739# ifdef FEAT_ARP
10740 "arp",
10741# endif
10742#endif
10743#ifdef __BEOS__
10744 "beos",
10745#endif
10746#ifdef MSDOS
10747# ifdef DJGPP
10748 "dos32",
10749# else
10750 "dos16",
10751# endif
10752#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010753#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 "mac",
10755#endif
10756#if defined(MACOS_X_UNIX)
10757 "macunix",
10758#endif
10759#ifdef OS2
10760 "os2",
10761#endif
10762#ifdef __QNX__
10763 "qnx",
10764#endif
10765#ifdef RISCOS
10766 "riscos",
10767#endif
10768#ifdef UNIX
10769 "unix",
10770#endif
10771#ifdef VMS
10772 "vms",
10773#endif
10774#ifdef WIN16
10775 "win16",
10776#endif
10777#ifdef WIN32
10778 "win32",
10779#endif
10780#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10781 "win32unix",
10782#endif
10783#ifdef WIN64
10784 "win64",
10785#endif
10786#ifdef EBCDIC
10787 "ebcdic",
10788#endif
10789#ifndef CASE_INSENSITIVE_FILENAME
10790 "fname_case",
10791#endif
10792#ifdef FEAT_ARABIC
10793 "arabic",
10794#endif
10795#ifdef FEAT_AUTOCMD
10796 "autocmd",
10797#endif
10798#ifdef FEAT_BEVAL
10799 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010800# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10801 "balloon_multiline",
10802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803#endif
10804#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10805 "builtin_terms",
10806# ifdef ALL_BUILTIN_TCAPS
10807 "all_builtin_terms",
10808# endif
10809#endif
10810#ifdef FEAT_BYTEOFF
10811 "byte_offset",
10812#endif
10813#ifdef FEAT_CINDENT
10814 "cindent",
10815#endif
10816#ifdef FEAT_CLIENTSERVER
10817 "clientserver",
10818#endif
10819#ifdef FEAT_CLIPBOARD
10820 "clipboard",
10821#endif
10822#ifdef FEAT_CMDL_COMPL
10823 "cmdline_compl",
10824#endif
10825#ifdef FEAT_CMDHIST
10826 "cmdline_hist",
10827#endif
10828#ifdef FEAT_COMMENTS
10829 "comments",
10830#endif
10831#ifdef FEAT_CRYPT
10832 "cryptv",
10833#endif
10834#ifdef FEAT_CSCOPE
10835 "cscope",
10836#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010837#ifdef CURSOR_SHAPE
10838 "cursorshape",
10839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840#ifdef DEBUG
10841 "debug",
10842#endif
10843#ifdef FEAT_CON_DIALOG
10844 "dialog_con",
10845#endif
10846#ifdef FEAT_GUI_DIALOG
10847 "dialog_gui",
10848#endif
10849#ifdef FEAT_DIFF
10850 "diff",
10851#endif
10852#ifdef FEAT_DIGRAPHS
10853 "digraphs",
10854#endif
10855#ifdef FEAT_DND
10856 "dnd",
10857#endif
10858#ifdef FEAT_EMACS_TAGS
10859 "emacs_tags",
10860#endif
10861 "eval", /* always present, of course! */
10862#ifdef FEAT_EX_EXTRA
10863 "ex_extra",
10864#endif
10865#ifdef FEAT_SEARCH_EXTRA
10866 "extra_search",
10867#endif
10868#ifdef FEAT_FKMAP
10869 "farsi",
10870#endif
10871#ifdef FEAT_SEARCHPATH
10872 "file_in_path",
10873#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010874#if defined(UNIX) && !defined(USE_SYSTEM)
10875 "filterpipe",
10876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877#ifdef FEAT_FIND_ID
10878 "find_in_path",
10879#endif
10880#ifdef FEAT_FOLDING
10881 "folding",
10882#endif
10883#ifdef FEAT_FOOTER
10884 "footer",
10885#endif
10886#if !defined(USE_SYSTEM) && defined(UNIX)
10887 "fork",
10888#endif
10889#ifdef FEAT_GETTEXT
10890 "gettext",
10891#endif
10892#ifdef FEAT_GUI
10893 "gui",
10894#endif
10895#ifdef FEAT_GUI_ATHENA
10896# ifdef FEAT_GUI_NEXTAW
10897 "gui_neXtaw",
10898# else
10899 "gui_athena",
10900# endif
10901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902#ifdef FEAT_GUI_GTK
10903 "gui_gtk",
10904# ifdef HAVE_GTK2
10905 "gui_gtk2",
10906# endif
10907#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000010908#ifdef FEAT_GUI_GNOME
10909 "gui_gnome",
10910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911#ifdef FEAT_GUI_MAC
10912 "gui_mac",
10913#endif
10914#ifdef FEAT_GUI_MOTIF
10915 "gui_motif",
10916#endif
10917#ifdef FEAT_GUI_PHOTON
10918 "gui_photon",
10919#endif
10920#ifdef FEAT_GUI_W16
10921 "gui_win16",
10922#endif
10923#ifdef FEAT_GUI_W32
10924 "gui_win32",
10925#endif
10926#ifdef FEAT_HANGULIN
10927 "hangul_input",
10928#endif
10929#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10930 "iconv",
10931#endif
10932#ifdef FEAT_INS_EXPAND
10933 "insert_expand",
10934#endif
10935#ifdef FEAT_JUMPLIST
10936 "jumplist",
10937#endif
10938#ifdef FEAT_KEYMAP
10939 "keymap",
10940#endif
10941#ifdef FEAT_LANGMAP
10942 "langmap",
10943#endif
10944#ifdef FEAT_LIBCALL
10945 "libcall",
10946#endif
10947#ifdef FEAT_LINEBREAK
10948 "linebreak",
10949#endif
10950#ifdef FEAT_LISP
10951 "lispindent",
10952#endif
10953#ifdef FEAT_LISTCMDS
10954 "listcmds",
10955#endif
10956#ifdef FEAT_LOCALMAP
10957 "localmap",
10958#endif
10959#ifdef FEAT_MENU
10960 "menu",
10961#endif
10962#ifdef FEAT_SESSION
10963 "mksession",
10964#endif
10965#ifdef FEAT_MODIFY_FNAME
10966 "modify_fname",
10967#endif
10968#ifdef FEAT_MOUSE
10969 "mouse",
10970#endif
10971#ifdef FEAT_MOUSESHAPE
10972 "mouseshape",
10973#endif
10974#if defined(UNIX) || defined(VMS)
10975# ifdef FEAT_MOUSE_DEC
10976 "mouse_dec",
10977# endif
10978# ifdef FEAT_MOUSE_GPM
10979 "mouse_gpm",
10980# endif
10981# ifdef FEAT_MOUSE_JSB
10982 "mouse_jsbterm",
10983# endif
10984# ifdef FEAT_MOUSE_NET
10985 "mouse_netterm",
10986# endif
10987# ifdef FEAT_MOUSE_PTERM
10988 "mouse_pterm",
10989# endif
10990# ifdef FEAT_MOUSE_XTERM
10991 "mouse_xterm",
10992# endif
10993#endif
10994#ifdef FEAT_MBYTE
10995 "multi_byte",
10996#endif
10997#ifdef FEAT_MBYTE_IME
10998 "multi_byte_ime",
10999#endif
11000#ifdef FEAT_MULTI_LANG
11001 "multi_lang",
11002#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011003#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011004#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011005 "mzscheme",
11006#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008#ifdef FEAT_OLE
11009 "ole",
11010#endif
11011#ifdef FEAT_OSFILETYPE
11012 "osfiletype",
11013#endif
11014#ifdef FEAT_PATH_EXTRA
11015 "path_extra",
11016#endif
11017#ifdef FEAT_PERL
11018#ifndef DYNAMIC_PERL
11019 "perl",
11020#endif
11021#endif
11022#ifdef FEAT_PYTHON
11023#ifndef DYNAMIC_PYTHON
11024 "python",
11025#endif
11026#endif
11027#ifdef FEAT_POSTSCRIPT
11028 "postscript",
11029#endif
11030#ifdef FEAT_PRINTER
11031 "printer",
11032#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011033#ifdef FEAT_PROFILE
11034 "profile",
11035#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011036#ifdef FEAT_RELTIME
11037 "reltime",
11038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039#ifdef FEAT_QUICKFIX
11040 "quickfix",
11041#endif
11042#ifdef FEAT_RIGHTLEFT
11043 "rightleft",
11044#endif
11045#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11046 "ruby",
11047#endif
11048#ifdef FEAT_SCROLLBIND
11049 "scrollbind",
11050#endif
11051#ifdef FEAT_CMDL_INFO
11052 "showcmd",
11053 "cmdline_info",
11054#endif
11055#ifdef FEAT_SIGNS
11056 "signs",
11057#endif
11058#ifdef FEAT_SMARTINDENT
11059 "smartindent",
11060#endif
11061#ifdef FEAT_SNIFF
11062 "sniff",
11063#endif
11064#ifdef FEAT_STL_OPT
11065 "statusline",
11066#endif
11067#ifdef FEAT_SUN_WORKSHOP
11068 "sun_workshop",
11069#endif
11070#ifdef FEAT_NETBEANS_INTG
11071 "netbeans_intg",
11072#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011073#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011074 "spell",
11075#endif
11076#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 "syntax",
11078#endif
11079#if defined(USE_SYSTEM) || !defined(UNIX)
11080 "system",
11081#endif
11082#ifdef FEAT_TAG_BINS
11083 "tag_binary",
11084#endif
11085#ifdef FEAT_TAG_OLDSTATIC
11086 "tag_old_static",
11087#endif
11088#ifdef FEAT_TAG_ANYWHITE
11089 "tag_any_white",
11090#endif
11091#ifdef FEAT_TCL
11092# ifndef DYNAMIC_TCL
11093 "tcl",
11094# endif
11095#endif
11096#ifdef TERMINFO
11097 "terminfo",
11098#endif
11099#ifdef FEAT_TERMRESPONSE
11100 "termresponse",
11101#endif
11102#ifdef FEAT_TEXTOBJ
11103 "textobjects",
11104#endif
11105#ifdef HAVE_TGETENT
11106 "tgetent",
11107#endif
11108#ifdef FEAT_TITLE
11109 "title",
11110#endif
11111#ifdef FEAT_TOOLBAR
11112 "toolbar",
11113#endif
11114#ifdef FEAT_USR_CMDS
11115 "user-commands", /* was accidentally included in 5.4 */
11116 "user_commands",
11117#endif
11118#ifdef FEAT_VIMINFO
11119 "viminfo",
11120#endif
11121#ifdef FEAT_VERTSPLIT
11122 "vertsplit",
11123#endif
11124#ifdef FEAT_VIRTUALEDIT
11125 "virtualedit",
11126#endif
11127#ifdef FEAT_VISUAL
11128 "visual",
11129#endif
11130#ifdef FEAT_VISUALEXTRA
11131 "visualextra",
11132#endif
11133#ifdef FEAT_VREPLACE
11134 "vreplace",
11135#endif
11136#ifdef FEAT_WILDIGN
11137 "wildignore",
11138#endif
11139#ifdef FEAT_WILDMENU
11140 "wildmenu",
11141#endif
11142#ifdef FEAT_WINDOWS
11143 "windows",
11144#endif
11145#ifdef FEAT_WAK
11146 "winaltkeys",
11147#endif
11148#ifdef FEAT_WRITEBACKUP
11149 "writebackup",
11150#endif
11151#ifdef FEAT_XIM
11152 "xim",
11153#endif
11154#ifdef FEAT_XFONTSET
11155 "xfontset",
11156#endif
11157#ifdef USE_XSMP
11158 "xsmp",
11159#endif
11160#ifdef USE_XSMP_INTERACT
11161 "xsmp_interact",
11162#endif
11163#ifdef FEAT_XCLIPBOARD
11164 "xterm_clipboard",
11165#endif
11166#ifdef FEAT_XTERM_SAVE
11167 "xterm_save",
11168#endif
11169#if defined(UNIX) && defined(FEAT_X11)
11170 "X11",
11171#endif
11172 NULL
11173 };
11174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 for (i = 0; has_list[i] != NULL; ++i)
11177 if (STRICMP(name, has_list[i]) == 0)
11178 {
11179 n = TRUE;
11180 break;
11181 }
11182
11183 if (n == FALSE)
11184 {
11185 if (STRNICMP(name, "patch", 5) == 0)
11186 n = has_patch(atoi((char *)name + 5));
11187 else if (STRICMP(name, "vim_starting") == 0)
11188 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011189#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11190 else if (STRICMP(name, "balloon_multiline") == 0)
11191 n = multiline_balloon_available();
11192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193#ifdef DYNAMIC_TCL
11194 else if (STRICMP(name, "tcl") == 0)
11195 n = tcl_enabled(FALSE);
11196#endif
11197#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11198 else if (STRICMP(name, "iconv") == 0)
11199 n = iconv_enabled(FALSE);
11200#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011201#ifdef DYNAMIC_MZSCHEME
11202 else if (STRICMP(name, "mzscheme") == 0)
11203 n = mzscheme_enabled(FALSE);
11204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205#ifdef DYNAMIC_RUBY
11206 else if (STRICMP(name, "ruby") == 0)
11207 n = ruby_enabled(FALSE);
11208#endif
11209#ifdef DYNAMIC_PYTHON
11210 else if (STRICMP(name, "python") == 0)
11211 n = python_enabled(FALSE);
11212#endif
11213#ifdef DYNAMIC_PERL
11214 else if (STRICMP(name, "perl") == 0)
11215 n = perl_enabled(FALSE);
11216#endif
11217#ifdef FEAT_GUI
11218 else if (STRICMP(name, "gui_running") == 0)
11219 n = (gui.in_use || gui.starting);
11220# ifdef FEAT_GUI_W32
11221 else if (STRICMP(name, "gui_win32s") == 0)
11222 n = gui_is_win32s();
11223# endif
11224# ifdef FEAT_BROWSE
11225 else if (STRICMP(name, "browse") == 0)
11226 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11227# endif
11228#endif
11229#ifdef FEAT_SYN_HL
11230 else if (STRICMP(name, "syntax_items") == 0)
11231 n = syntax_present(curbuf);
11232#endif
11233#if defined(WIN3264)
11234 else if (STRICMP(name, "win95") == 0)
11235 n = mch_windows95();
11236#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011237#ifdef FEAT_NETBEANS_INTG
11238 else if (STRICMP(name, "netbeans_enabled") == 0)
11239 n = usingNetbeans;
11240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 }
11242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244}
11245
11246/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011247 * "has_key()" function
11248 */
11249 static void
11250f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011251 typval_T *argvars;
11252 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011253{
11254 rettv->vval.v_number = 0;
11255 if (argvars[0].v_type != VAR_DICT)
11256 {
11257 EMSG(_(e_dictreq));
11258 return;
11259 }
11260 if (argvars[0].vval.v_dict == NULL)
11261 return;
11262
11263 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011264 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011265}
11266
11267/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011268 * "haslocaldir()" function
11269 */
11270/*ARGSUSED*/
11271 static void
11272f_haslocaldir(argvars, rettv)
11273 typval_T *argvars;
11274 typval_T *rettv;
11275{
11276 rettv->vval.v_number = (curwin->w_localdir != NULL);
11277}
11278
11279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 * "hasmapto()" function
11281 */
11282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
11287 char_u *name;
11288 char_u *mode;
11289 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011290 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011292 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011293 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 mode = (char_u *)"nvo";
11295 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011298 if (argvars[2].v_type != VAR_UNKNOWN)
11299 abbr = get_tv_number(&argvars[2]);
11300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301
Bram Moolenaar2c932302006-03-18 21:42:09 +000011302 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306}
11307
11308/*
11309 * "histadd()" function
11310 */
11311/*ARGSUSED*/
11312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011314 typval_T *argvars;
11315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316{
11317#ifdef FEAT_CMDHIST
11318 int histype;
11319 char_u *str;
11320 char_u buf[NUMBUFLEN];
11321#endif
11322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 if (check_restricted() || check_secure())
11325 return;
11326#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11328 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 if (histype >= 0)
11330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 if (*str != NUL)
11333 {
11334 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 return;
11337 }
11338 }
11339#endif
11340}
11341
11342/*
11343 * "histdel()" function
11344 */
11345/*ARGSUSED*/
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 typval_T *argvars;
11349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350{
11351#ifdef FEAT_CMDHIST
11352 int n;
11353 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011354 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011356 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11357 if (str == NULL)
11358 n = 0;
11359 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011361 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011362 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 else
11367 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 get_tv_string_buf(&argvars[1], buf));
11370 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373#endif
11374}
11375
11376/*
11377 * "histget()" function
11378 */
11379/*ARGSUSED*/
11380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011382 typval_T *argvars;
11383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384{
11385#ifdef FEAT_CMDHIST
11386 int type;
11387 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11391 if (str == NULL)
11392 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011394 {
11395 type = get_histtype(str);
11396 if (argvars[1].v_type == VAR_UNKNOWN)
11397 idx = get_history_idx(type);
11398 else
11399 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11400 /* -1 on type error */
11401 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407}
11408
11409/*
11410 * "histnr()" function
11411 */
11412/*ARGSUSED*/
11413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011415 typval_T *argvars;
11416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417{
11418 int i;
11419
11420#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 char_u *history = get_tv_string_chk(&argvars[0]);
11422
11423 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 if (i >= HIST_CMD && i < HIST_COUNT)
11425 i = get_history_idx(i);
11426 else
11427#endif
11428 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430}
11431
11432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 * "highlightID(name)" function
11434 */
11435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *argvars;
11438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441}
11442
11443/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011444 * "highlight_exists()" function
11445 */
11446 static void
11447f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *argvars;
11449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011450{
11451 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11452}
11453
11454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 * "hostname()" function
11456 */
11457/*ARGSUSED*/
11458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011460 typval_T *argvars;
11461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462{
11463 char_u hostname[256];
11464
11465 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->v_type = VAR_STRING;
11467 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468}
11469
11470/*
11471 * iconv() function
11472 */
11473/*ARGSUSED*/
11474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011476 typval_T *argvars;
11477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478{
11479#ifdef FEAT_MBYTE
11480 char_u buf1[NUMBUFLEN];
11481 char_u buf2[NUMBUFLEN];
11482 char_u *from, *to, *str;
11483 vimconv_T vimconv;
11484#endif
11485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 rettv->v_type = VAR_STRING;
11487 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488
11489#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 str = get_tv_string(&argvars[0]);
11491 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11492 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 vimconv.vc_type = CONV_NONE;
11494 convert_setup(&vimconv, from, to);
11495
11496 /* If the encodings are equal, no conversion needed. */
11497 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501
11502 convert_setup(&vimconv, NULL, NULL);
11503 vim_free(from);
11504 vim_free(to);
11505#endif
11506}
11507
11508/*
11509 * "indent()" function
11510 */
11511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 typval_T *argvars;
11514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515{
11516 linenr_T lnum;
11517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523}
11524
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011525/*
11526 * "index()" function
11527 */
11528 static void
11529f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011530 typval_T *argvars;
11531 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011532{
Bram Moolenaar33570922005-01-25 22:26:29 +000011533 list_T *l;
11534 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011535 long idx = 0;
11536 int ic = FALSE;
11537
11538 rettv->vval.v_number = -1;
11539 if (argvars[0].v_type != VAR_LIST)
11540 {
11541 EMSG(_(e_listreq));
11542 return;
11543 }
11544 l = argvars[0].vval.v_list;
11545 if (l != NULL)
11546 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011547 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011548 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 int error = FALSE;
11551
Bram Moolenaar758711c2005-02-02 23:11:38 +000011552 /* Start at specified item. Use the cached index that list_find()
11553 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011555 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011556 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011557 ic = get_tv_number_chk(&argvars[3], &error);
11558 if (error)
11559 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011560 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011561
Bram Moolenaar758711c2005-02-02 23:11:38 +000011562 for ( ; item != NULL; item = item->li_next, ++idx)
11563 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011564 {
11565 rettv->vval.v_number = idx;
11566 break;
11567 }
11568 }
11569}
11570
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571static int inputsecret_flag = 0;
11572
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011573static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11574
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011576 * This function is used by f_input() and f_inputdialog() functions. The third
11577 * argument to f_input() specifies the type of completion to use at the
11578 * prompt. The third argument to f_inputdialog() specifies the value to return
11579 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 */
11581 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011582get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011583 typval_T *argvars;
11584 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011585 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011587 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 char_u *p = NULL;
11589 int c;
11590 char_u buf[NUMBUFLEN];
11591 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011593 int xp_type = EXPAND_NOTHING;
11594 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598
11599#ifdef NO_CONSOLE_INPUT
11600 /* While starting up, there is no place to enter text. */
11601 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603#endif
11604
11605 cmd_silent = FALSE; /* Want to see the prompt. */
11606 if (prompt != NULL)
11607 {
11608 /* Only the part of the message after the last NL is considered as
11609 * prompt for the command line */
11610 p = vim_strrchr(prompt, '\n');
11611 if (p == NULL)
11612 p = prompt;
11613 else
11614 {
11615 ++p;
11616 c = *p;
11617 *p = NUL;
11618 msg_start();
11619 msg_clr_eos();
11620 msg_puts_attr(prompt, echo_attr);
11621 msg_didout = FALSE;
11622 msg_starthere();
11623 *p = c;
11624 }
11625 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011627 if (argvars[1].v_type != VAR_UNKNOWN)
11628 {
11629 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11630 if (defstr != NULL)
11631 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011633 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011634 {
11635 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011636 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011637 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011638
Bram Moolenaar4463f292005-09-25 22:20:24 +000011639 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011640
Bram Moolenaar4463f292005-09-25 22:20:24 +000011641 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11642 if (xp_name == NULL)
11643 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011644
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011645 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011646
Bram Moolenaar4463f292005-09-25 22:20:24 +000011647 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11648 &xp_arg) == FAIL)
11649 return;
11650 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011651 }
11652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011653 if (defstr != NULL)
11654 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011655 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11656 xp_type, xp_arg);
11657
11658 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011660 /* since the user typed this, no need to wait for return */
11661 need_wait_return = FALSE;
11662 msg_didout = FALSE;
11663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 cmd_silent = cmd_silent_save;
11665}
11666
11667/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011668 * "input()" function
11669 * Also handles inputsecret() when inputsecret is set.
11670 */
11671 static void
11672f_input(argvars, rettv)
11673 typval_T *argvars;
11674 typval_T *rettv;
11675{
11676 get_user_input(argvars, rettv, FALSE);
11677}
11678
11679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 * "inputdialog()" function
11681 */
11682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *argvars;
11685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686{
11687#if defined(FEAT_GUI_TEXTDIALOG)
11688 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11689 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11690 {
11691 char_u *message;
11692 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 message = get_tv_string_chk(&argvars[0]);
11696 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011697 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011698 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 else
11700 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701 if (message != NULL && defstr != NULL
11702 && do_dialog(VIM_QUESTION, NULL, message,
11703 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 else
11706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 if (message != NULL && defstr != NULL
11708 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011709 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011710 rettv->vval.v_string = vim_strsave(
11711 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 }
11717 else
11718#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011719 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720}
11721
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011722/*
11723 * "inputlist()" function
11724 */
11725 static void
11726f_inputlist(argvars, rettv)
11727 typval_T *argvars;
11728 typval_T *rettv;
11729{
11730 listitem_T *li;
11731 int selected;
11732 int mouse_used;
11733
11734 rettv->vval.v_number = 0;
11735#ifdef NO_CONSOLE_INPUT
11736 /* While starting up, there is no place to enter text. */
11737 if (no_console_input())
11738 return;
11739#endif
11740 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11741 {
11742 EMSG2(_(e_listarg), "inputlist()");
11743 return;
11744 }
11745
11746 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011747 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011748 lines_left = Rows; /* avoid more prompt */
11749 msg_scroll = TRUE;
11750 msg_clr_eos();
11751
11752 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11753 {
11754 msg_puts(get_tv_string(&li->li_tv));
11755 msg_putchar('\n');
11756 }
11757
11758 /* Ask for choice. */
11759 selected = prompt_for_number(&mouse_used);
11760 if (mouse_used)
11761 selected -= lines_left;
11762
11763 rettv->vval.v_number = selected;
11764}
11765
11766
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11768
11769/*
11770 * "inputrestore()" function
11771 */
11772/*ARGSUSED*/
11773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011774f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011775 typval_T *argvars;
11776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777{
11778 if (ga_userinput.ga_len > 0)
11779 {
11780 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11782 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 }
11785 else if (p_verbose > 1)
11786 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011787 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 }
11790}
11791
11792/*
11793 * "inputsave()" function
11794 */
11795/*ARGSUSED*/
11796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011798 typval_T *argvars;
11799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800{
11801 /* Add an entry to the stack of typehead storage. */
11802 if (ga_grow(&ga_userinput, 1) == OK)
11803 {
11804 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11805 + ga_userinput.ga_len);
11806 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 }
11809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811}
11812
11813/*
11814 * "inputsecret()" function
11815 */
11816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011818 typval_T *argvars;
11819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820{
11821 ++cmdline_star;
11822 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011823 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 --cmdline_star;
11825 --inputsecret_flag;
11826}
11827
11828/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011829 * "insert()" function
11830 */
11831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011833 typval_T *argvars;
11834 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011835{
11836 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011837 listitem_T *item;
11838 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011839 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011840
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011841 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011842 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011843 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011844 else if ((l = argvars[0].vval.v_list) != NULL
11845 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011846 {
11847 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 before = get_tv_number_chk(&argvars[2], &error);
11849 if (error)
11850 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011851
Bram Moolenaar758711c2005-02-02 23:11:38 +000011852 if (before == l->lv_len)
11853 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011854 else
11855 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011856 item = list_find(l, before);
11857 if (item == NULL)
11858 {
11859 EMSGN(_(e_listidx), before);
11860 l = NULL;
11861 }
11862 }
11863 if (l != NULL)
11864 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011865 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011866 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011867 }
11868 }
11869}
11870
11871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 * "isdirectory()" function
11873 */
11874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011876 typval_T *argvars;
11877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880}
11881
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011882/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011883 * "islocked()" function
11884 */
11885 static void
11886f_islocked(argvars, rettv)
11887 typval_T *argvars;
11888 typval_T *rettv;
11889{
11890 lval_T lv;
11891 char_u *end;
11892 dictitem_T *di;
11893
11894 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011895 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11896 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011897 if (end != NULL && lv.ll_name != NULL)
11898 {
11899 if (*end != NUL)
11900 EMSG(_(e_trailing));
11901 else
11902 {
11903 if (lv.ll_tv == NULL)
11904 {
11905 if (check_changedtick(lv.ll_name))
11906 rettv->vval.v_number = 1; /* always locked */
11907 else
11908 {
11909 di = find_var(lv.ll_name, NULL);
11910 if (di != NULL)
11911 {
11912 /* Consider a variable locked when:
11913 * 1. the variable itself is locked
11914 * 2. the value of the variable is locked.
11915 * 3. the List or Dict value is locked.
11916 */
11917 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11918 || tv_islocked(&di->di_tv));
11919 }
11920 }
11921 }
11922 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011923 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011924 else if (lv.ll_newkey != NULL)
11925 EMSG2(_(e_dictkey), lv.ll_newkey);
11926 else if (lv.ll_list != NULL)
11927 /* List item. */
11928 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11929 else
11930 /* Dictionary item. */
11931 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11932 }
11933 }
11934
11935 clear_lval(&lv);
11936}
11937
Bram Moolenaar33570922005-01-25 22:26:29 +000011938static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011939
11940/*
11941 * Turn a dict into a list:
11942 * "what" == 0: list of keys
11943 * "what" == 1: list of values
11944 * "what" == 2: list of items
11945 */
11946 static void
11947dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 typval_T *argvars;
11949 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011950 int what;
11951{
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 list_T *l2;
11953 dictitem_T *di;
11954 hashitem_T *hi;
11955 listitem_T *li;
11956 listitem_T *li2;
11957 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011958 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011959
11960 rettv->vval.v_number = 0;
11961 if (argvars[0].v_type != VAR_DICT)
11962 {
11963 EMSG(_(e_dictreq));
11964 return;
11965 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011966 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011967 return;
11968
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011969 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011970 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011971
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011972 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011973 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011975 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011976 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011977 --todo;
11978 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011979
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011980 li = listitem_alloc();
11981 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011982 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011983 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011984
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011985 if (what == 0)
11986 {
11987 /* keys() */
11988 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011989 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011990 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11991 }
11992 else if (what == 1)
11993 {
11994 /* values() */
11995 copy_tv(&di->di_tv, &li->li_tv);
11996 }
11997 else
11998 {
11999 /* items() */
12000 l2 = list_alloc();
12001 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012002 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012003 li->li_tv.vval.v_list = l2;
12004 if (l2 == NULL)
12005 break;
12006 ++l2->lv_refcount;
12007
12008 li2 = listitem_alloc();
12009 if (li2 == NULL)
12010 break;
12011 list_append(l2, li2);
12012 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012013 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012014 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12015
12016 li2 = listitem_alloc();
12017 if (li2 == NULL)
12018 break;
12019 list_append(l2, li2);
12020 copy_tv(&di->di_tv, &li2->li_tv);
12021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012022 }
12023 }
12024}
12025
12026/*
12027 * "items(dict)" function
12028 */
12029 static void
12030f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012031 typval_T *argvars;
12032 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012033{
12034 dict_list(argvars, rettv, 2);
12035}
12036
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012038 * "join()" function
12039 */
12040 static void
12041f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 typval_T *argvars;
12043 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012044{
12045 garray_T ga;
12046 char_u *sep;
12047
12048 rettv->vval.v_number = 0;
12049 if (argvars[0].v_type != VAR_LIST)
12050 {
12051 EMSG(_(e_listreq));
12052 return;
12053 }
12054 if (argvars[0].vval.v_list == NULL)
12055 return;
12056 if (argvars[1].v_type == VAR_UNKNOWN)
12057 sep = (char_u *)" ";
12058 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012060
12061 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012062
12063 if (sep != NULL)
12064 {
12065 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012066 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 ga_append(&ga, NUL);
12068 rettv->vval.v_string = (char_u *)ga.ga_data;
12069 }
12070 else
12071 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012072}
12073
12074/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012075 * "keys()" function
12076 */
12077 static void
12078f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012079 typval_T *argvars;
12080 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012081{
12082 dict_list(argvars, rettv, 0);
12083}
12084
12085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 * "last_buffer_nr()" function.
12087 */
12088/*ARGSUSED*/
12089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012091 typval_T *argvars;
12092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093{
12094 int n = 0;
12095 buf_T *buf;
12096
12097 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12098 if (n < buf->b_fnum)
12099 n = buf->b_fnum;
12100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012102}
12103
12104/*
12105 * "len()" function
12106 */
12107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012109 typval_T *argvars;
12110 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012111{
12112 switch (argvars[0].v_type)
12113 {
12114 case VAR_STRING:
12115 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->vval.v_number = (varnumber_T)STRLEN(
12117 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012118 break;
12119 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012121 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012122 case VAR_DICT:
12123 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12124 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012125 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012126 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012127 break;
12128 }
12129}
12130
Bram Moolenaar33570922005-01-25 22:26:29 +000012131static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012132
12133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012135 typval_T *argvars;
12136 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012137 int type;
12138{
12139#ifdef FEAT_LIBCALL
12140 char_u *string_in;
12141 char_u **string_result;
12142 int nr_result;
12143#endif
12144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012145 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012146 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012148 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012150
12151 if (check_restricted() || check_secure())
12152 return;
12153
12154#ifdef FEAT_LIBCALL
12155 /* The first two args must be strings, otherwise its meaningless */
12156 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12157 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012158 string_in = NULL;
12159 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012160 string_in = argvars[2].vval.v_string;
12161 if (type == VAR_NUMBER)
12162 string_result = NULL;
12163 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012165 if (mch_libcall(argvars[0].vval.v_string,
12166 argvars[1].vval.v_string,
12167 string_in,
12168 argvars[2].vval.v_number,
12169 string_result,
12170 &nr_result) == OK
12171 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012173 }
12174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175}
12176
12177/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012178 * "libcall()" function
12179 */
12180 static void
12181f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012182 typval_T *argvars;
12183 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012184{
12185 libcall_common(argvars, rettv, VAR_STRING);
12186}
12187
12188/*
12189 * "libcallnr()" function
12190 */
12191 static void
12192f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012193 typval_T *argvars;
12194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012195{
12196 libcall_common(argvars, rettv, VAR_NUMBER);
12197}
12198
12199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 * "line(string)" function
12201 */
12202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012203f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012204 typval_T *argvars;
12205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206{
12207 linenr_T lnum = 0;
12208 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012209 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012211 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 if (fp != NULL)
12213 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215}
12216
12217/*
12218 * "line2byte(lnum)" function
12219 */
12220/*ARGSUSED*/
12221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012222f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012223 typval_T *argvars;
12224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225{
12226#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228#else
12229 linenr_T lnum;
12230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12236 if (rettv->vval.v_number >= 0)
12237 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238#endif
12239}
12240
12241/*
12242 * "lispindent(lnum)" function
12243 */
12244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012246 typval_T *argvars;
12247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248{
12249#ifdef FEAT_LISP
12250 pos_T pos;
12251 linenr_T lnum;
12252
12253 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12256 {
12257 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 curwin->w_cursor = pos;
12260 }
12261 else
12262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012263 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264}
12265
12266/*
12267 * "localtime()" function
12268 */
12269/*ARGSUSED*/
12270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012271f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012272 typval_T *argvars;
12273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276}
12277
Bram Moolenaar33570922005-01-25 22:26:29 +000012278static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279
12280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012281get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012282 typval_T *argvars;
12283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 int exact;
12285{
12286 char_u *keys;
12287 char_u *which;
12288 char_u buf[NUMBUFLEN];
12289 char_u *keys_buf = NULL;
12290 char_u *rhs;
12291 int mode;
12292 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012293 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294
12295 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296 rettv->v_type = VAR_STRING;
12297 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 if (*keys == NUL)
12301 return;
12302
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012303 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012305 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012306 if (argvars[2].v_type != VAR_UNKNOWN)
12307 abbr = get_tv_number(&argvars[2]);
12308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 else
12310 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012311 if (which == NULL)
12312 return;
12313
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 mode = get_map_mode(&which, 0);
12315
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012316 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012317 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 vim_free(keys_buf);
12319 if (rhs != NULL)
12320 {
12321 ga_init(&ga);
12322 ga.ga_itemsize = 1;
12323 ga.ga_growsize = 40;
12324
12325 while (*rhs != NUL)
12326 ga_concat(&ga, str2special(&rhs, FALSE));
12327
12328 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 }
12331}
12332
12333/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012334 * "map()" function
12335 */
12336 static void
12337f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012338 typval_T *argvars;
12339 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012340{
12341 filter_map(argvars, rettv, TRUE);
12342}
12343
12344/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012345 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 */
12347 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012348f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012349 typval_T *argvars;
12350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012352 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353}
12354
12355/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012356 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 */
12358 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012359f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012360 typval_T *argvars;
12361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012363 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364}
12365
Bram Moolenaar33570922005-01-25 22:26:29 +000012366static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367
12368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012370 typval_T *argvars;
12371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 int type;
12373{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012374 char_u *str = NULL;
12375 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 char_u *pat;
12377 regmatch_T regmatch;
12378 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012379 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 char_u *save_cpo;
12381 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012382 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012383 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012384 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012385 list_T *l = NULL;
12386 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012387 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012388 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389
12390 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12391 save_cpo = p_cpo;
12392 p_cpo = (char_u *)"";
12393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012394 rettv->vval.v_number = -1;
12395 if (type == 3)
12396 {
12397 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012398 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012399 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012400 }
12401 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403 rettv->v_type = VAR_STRING;
12404 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012407 if (argvars[0].v_type == VAR_LIST)
12408 {
12409 if ((l = argvars[0].vval.v_list) == NULL)
12410 goto theend;
12411 li = l->lv_first;
12412 }
12413 else
12414 expr = str = get_tv_string(&argvars[0]);
12415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012416 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12417 if (pat == NULL)
12418 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012419
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012420 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012422 int error = FALSE;
12423
12424 start = get_tv_number_chk(&argvars[2], &error);
12425 if (error)
12426 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012427 if (l != NULL)
12428 {
12429 li = list_find(l, start);
12430 if (li == NULL)
12431 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012432 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012433 }
12434 else
12435 {
12436 if (start < 0)
12437 start = 0;
12438 if (start > (long)STRLEN(str))
12439 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012440 /* When "count" argument is there ignore matches before "start",
12441 * otherwise skip part of the string. Differs when pattern is "^"
12442 * or "\<". */
12443 if (argvars[3].v_type != VAR_UNKNOWN)
12444 startcol = start;
12445 else
12446 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012447 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012448
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012449 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012450 nth = get_tv_number_chk(&argvars[3], &error);
12451 if (error)
12452 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 }
12454
12455 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12456 if (regmatch.regprog != NULL)
12457 {
12458 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012459
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012460 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012461 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012462 if (l != NULL)
12463 {
12464 if (li == NULL)
12465 {
12466 match = FALSE;
12467 break;
12468 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012469 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012470 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012471 if (str == NULL)
12472 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012473 }
12474
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012475 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012476
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012477 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012478 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012479 if (l == NULL && !match)
12480 break;
12481
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012482 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012483 if (l != NULL)
12484 {
12485 li = li->li_next;
12486 ++idx;
12487 }
12488 else
12489 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012490#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012491 startcol = (colnr_T)(regmatch.startp[0]
12492 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012493#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012494 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012495#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012496 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012497 }
12498
12499 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012501 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012502 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012503 int i;
12504
12505 /* return list with matched string and submatches */
12506 for (i = 0; i < NSUBEXP; ++i)
12507 {
12508 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012509 {
12510 if (list_append_string(rettv->vval.v_list,
12511 (char_u *)"", 0) == FAIL)
12512 break;
12513 }
12514 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012515 regmatch.startp[i],
12516 (int)(regmatch.endp[i] - regmatch.startp[i]))
12517 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012518 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012519 }
12520 }
12521 else if (type == 2)
12522 {
12523 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012524 if (l != NULL)
12525 copy_tv(&li->li_tv, rettv);
12526 else
12527 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012529 }
12530 else if (l != NULL)
12531 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 else
12533 {
12534 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536 (varnumber_T)(regmatch.startp[0] - str);
12537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012538 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012540 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541 }
12542 }
12543 vim_free(regmatch.regprog);
12544 }
12545
12546theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012547 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548 p_cpo = save_cpo;
12549}
12550
12551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012552 * "match()" function
12553 */
12554 static void
12555f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 typval_T *argvars;
12557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558{
12559 find_some_match(argvars, rettv, 1);
12560}
12561
12562/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012563 * "matchadd()" function
12564 */
12565 static void
12566f_matchadd(argvars, rettv)
12567 typval_T *argvars;
12568 typval_T *rettv;
12569{
12570#ifdef FEAT_SEARCH_EXTRA
12571 char_u buf[NUMBUFLEN];
12572 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12573 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12574 int prio = 10; /* default priority */
12575 int id = -1;
12576 int error = FALSE;
12577
12578 rettv->vval.v_number = -1;
12579
12580 if (grp == NULL || pat == NULL)
12581 return;
12582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012583 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012584 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012585 if (argvars[3].v_type != VAR_UNKNOWN)
12586 id = get_tv_number_chk(&argvars[3], &error);
12587 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012588 if (error == TRUE)
12589 return;
12590 if (id >= 1 && id <= 3)
12591 {
12592 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12593 return;
12594 }
12595
12596 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12597#endif
12598}
12599
12600/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012601 * "matcharg()" function
12602 */
12603 static void
12604f_matcharg(argvars, rettv)
12605 typval_T *argvars;
12606 typval_T *rettv;
12607{
12608 if (rettv_list_alloc(rettv) == OK)
12609 {
12610#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012611 int id = get_tv_number(&argvars[0]);
12612 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012613
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012614 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012615 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012616 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12617 {
12618 list_append_string(rettv->vval.v_list,
12619 syn_id2name(m->hlg_id), -1);
12620 list_append_string(rettv->vval.v_list, m->pattern, -1);
12621 }
12622 else
12623 {
12624 list_append_string(rettv->vval.v_list, NUL, -1);
12625 list_append_string(rettv->vval.v_list, NUL, -1);
12626 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012627 }
12628#endif
12629 }
12630}
12631
12632/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012633 * "matchdelete()" function
12634 */
12635 static void
12636f_matchdelete(argvars, rettv)
12637 typval_T *argvars;
12638 typval_T *rettv;
12639{
12640#ifdef FEAT_SEARCH_EXTRA
12641 rettv->vval.v_number = match_delete(curwin,
12642 (int)get_tv_number(&argvars[0]), TRUE);
12643#endif
12644}
12645
12646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012647 * "matchend()" function
12648 */
12649 static void
12650f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012651 typval_T *argvars;
12652 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012653{
12654 find_some_match(argvars, rettv, 0);
12655}
12656
12657/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012658 * "matchlist()" function
12659 */
12660 static void
12661f_matchlist(argvars, rettv)
12662 typval_T *argvars;
12663 typval_T *rettv;
12664{
12665 find_some_match(argvars, rettv, 3);
12666}
12667
12668/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012669 * "matchstr()" function
12670 */
12671 static void
12672f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012673 typval_T *argvars;
12674 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012675{
12676 find_some_match(argvars, rettv, 2);
12677}
12678
Bram Moolenaar33570922005-01-25 22:26:29 +000012679static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012680
12681 static void
12682max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012683 typval_T *argvars;
12684 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012685 int domax;
12686{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012687 long n = 0;
12688 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012690
12691 if (argvars[0].v_type == VAR_LIST)
12692 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 list_T *l;
12694 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012695
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012696 l = argvars[0].vval.v_list;
12697 if (l != NULL)
12698 {
12699 li = l->lv_first;
12700 if (li != NULL)
12701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012702 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012703 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012704 {
12705 li = li->li_next;
12706 if (li == NULL)
12707 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012708 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012709 if (domax ? i > n : i < n)
12710 n = i;
12711 }
12712 }
12713 }
12714 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012715 else if (argvars[0].v_type == VAR_DICT)
12716 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012717 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012718 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012719 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012720 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012721
12722 d = argvars[0].vval.v_dict;
12723 if (d != NULL)
12724 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012725 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012726 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012727 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012728 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012729 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012730 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012732 if (first)
12733 {
12734 n = i;
12735 first = FALSE;
12736 }
12737 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012738 n = i;
12739 }
12740 }
12741 }
12742 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012743 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012744 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012746}
12747
12748/*
12749 * "max()" function
12750 */
12751 static void
12752f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012753 typval_T *argvars;
12754 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012755{
12756 max_min(argvars, rettv, TRUE);
12757}
12758
12759/*
12760 * "min()" function
12761 */
12762 static void
12763f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *argvars;
12765 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012766{
12767 max_min(argvars, rettv, FALSE);
12768}
12769
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012770static int mkdir_recurse __ARGS((char_u *dir, int prot));
12771
12772/*
12773 * Create the directory in which "dir" is located, and higher levels when
12774 * needed.
12775 */
12776 static int
12777mkdir_recurse(dir, prot)
12778 char_u *dir;
12779 int prot;
12780{
12781 char_u *p;
12782 char_u *updir;
12783 int r = FAIL;
12784
12785 /* Get end of directory name in "dir".
12786 * We're done when it's "/" or "c:/". */
12787 p = gettail_sep(dir);
12788 if (p <= get_past_head(dir))
12789 return OK;
12790
12791 /* If the directory exists we're done. Otherwise: create it.*/
12792 updir = vim_strnsave(dir, (int)(p - dir));
12793 if (updir == NULL)
12794 return FAIL;
12795 if (mch_isdir(updir))
12796 r = OK;
12797 else if (mkdir_recurse(updir, prot) == OK)
12798 r = vim_mkdir_emsg(updir, prot);
12799 vim_free(updir);
12800 return r;
12801}
12802
12803#ifdef vim_mkdir
12804/*
12805 * "mkdir()" function
12806 */
12807 static void
12808f_mkdir(argvars, rettv)
12809 typval_T *argvars;
12810 typval_T *rettv;
12811{
12812 char_u *dir;
12813 char_u buf[NUMBUFLEN];
12814 int prot = 0755;
12815
12816 rettv->vval.v_number = FAIL;
12817 if (check_restricted() || check_secure())
12818 return;
12819
12820 dir = get_tv_string_buf(&argvars[0], buf);
12821 if (argvars[1].v_type != VAR_UNKNOWN)
12822 {
12823 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 prot = get_tv_number_chk(&argvars[2], NULL);
12825 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012826 mkdir_recurse(dir, prot);
12827 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012828 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012829}
12830#endif
12831
Bram Moolenaar0d660222005-01-07 21:51:51 +000012832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 * "mode()" function
12834 */
12835/*ARGSUSED*/
12836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 typval_T *argvars;
12839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840{
12841 char_u buf[2];
12842
12843#ifdef FEAT_VISUAL
12844 if (VIsual_active)
12845 {
12846 if (VIsual_select)
12847 buf[0] = VIsual_mode + 's' - 'v';
12848 else
12849 buf[0] = VIsual_mode;
12850 }
12851 else
12852#endif
12853 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12854 buf[0] = 'r';
12855 else if (State & INSERT)
12856 {
12857 if (State & REPLACE_FLAG)
12858 buf[0] = 'R';
12859 else
12860 buf[0] = 'i';
12861 }
12862 else if (State & CMDLINE)
12863 buf[0] = 'c';
12864 else
12865 buf[0] = 'n';
12866
12867 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 rettv->vval.v_string = vim_strsave(buf);
12869 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870}
12871
12872/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012873 * "nextnonblank()" function
12874 */
12875 static void
12876f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 typval_T *argvars;
12878 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012879{
12880 linenr_T lnum;
12881
12882 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012884 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012885 {
12886 lnum = 0;
12887 break;
12888 }
12889 if (*skipwhite(ml_get(lnum)) != NUL)
12890 break;
12891 }
12892 rettv->vval.v_number = lnum;
12893}
12894
12895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 * "nr2char()" function
12897 */
12898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012900 typval_T *argvars;
12901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902{
12903 char_u buf[NUMBUFLEN];
12904
12905#ifdef FEAT_MBYTE
12906 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 else
12909#endif
12910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 buf[1] = NUL;
12913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 rettv->v_type = VAR_STRING;
12915 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012916}
12917
12918/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012919 * "pathshorten()" function
12920 */
12921 static void
12922f_pathshorten(argvars, rettv)
12923 typval_T *argvars;
12924 typval_T *rettv;
12925{
12926 char_u *p;
12927
12928 rettv->v_type = VAR_STRING;
12929 p = get_tv_string_chk(&argvars[0]);
12930 if (p == NULL)
12931 rettv->vval.v_string = NULL;
12932 else
12933 {
12934 p = vim_strsave(p);
12935 rettv->vval.v_string = p;
12936 if (p != NULL)
12937 shorten_dir(p);
12938 }
12939}
12940
12941/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012942 * "prevnonblank()" function
12943 */
12944 static void
12945f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012946 typval_T *argvars;
12947 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012948{
12949 linenr_T lnum;
12950
12951 lnum = get_tv_lnum(argvars);
12952 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12953 lnum = 0;
12954 else
12955 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12956 --lnum;
12957 rettv->vval.v_number = lnum;
12958}
12959
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012960#ifdef HAVE_STDARG_H
12961/* This dummy va_list is here because:
12962 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12963 * - locally in the function results in a "used before set" warning
12964 * - using va_start() to initialize it gives "function with fixed args" error */
12965static va_list ap;
12966#endif
12967
Bram Moolenaar8c711452005-01-14 21:53:12 +000012968/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012969 * "printf()" function
12970 */
12971 static void
12972f_printf(argvars, rettv)
12973 typval_T *argvars;
12974 typval_T *rettv;
12975{
12976 rettv->v_type = VAR_STRING;
12977 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012978#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012979 {
12980 char_u buf[NUMBUFLEN];
12981 int len;
12982 char_u *s;
12983 int saved_did_emsg = did_emsg;
12984 char *fmt;
12985
12986 /* Get the required length, allocate the buffer and do it for real. */
12987 did_emsg = FALSE;
12988 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012989 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012990 if (!did_emsg)
12991 {
12992 s = alloc(len + 1);
12993 if (s != NULL)
12994 {
12995 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012996 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012997 }
12998 }
12999 did_emsg |= saved_did_emsg;
13000 }
13001#endif
13002}
13003
13004/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013005 * "pumvisible()" function
13006 */
13007/*ARGSUSED*/
13008 static void
13009f_pumvisible(argvars, rettv)
13010 typval_T *argvars;
13011 typval_T *rettv;
13012{
13013 rettv->vval.v_number = 0;
13014#ifdef FEAT_INS_EXPAND
13015 if (pum_visible())
13016 rettv->vval.v_number = 1;
13017#endif
13018}
13019
13020/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013021 * "range()" function
13022 */
13023 static void
13024f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013025 typval_T *argvars;
13026 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013027{
13028 long start;
13029 long end;
13030 long stride = 1;
13031 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013034 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013035 if (argvars[1].v_type == VAR_UNKNOWN)
13036 {
13037 end = start - 1;
13038 start = 0;
13039 }
13040 else
13041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013042 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013043 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013044 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013045 }
13046
13047 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013048 if (error)
13049 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013050 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013051 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013052 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013053 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013054 else
13055 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013056 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013057 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013058 if (list_append_number(rettv->vval.v_list,
13059 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013060 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013061 }
13062}
13063
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013064/*
13065 * "readfile()" function
13066 */
13067 static void
13068f_readfile(argvars, rettv)
13069 typval_T *argvars;
13070 typval_T *rettv;
13071{
13072 int binary = FALSE;
13073 char_u *fname;
13074 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013075 listitem_T *li;
13076#define FREAD_SIZE 200 /* optimized for text lines */
13077 char_u buf[FREAD_SIZE];
13078 int readlen; /* size of last fread() */
13079 int buflen; /* nr of valid chars in buf[] */
13080 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13081 int tolist; /* first byte in buf[] still to be put in list */
13082 int chop; /* how many CR to chop off */
13083 char_u *prev = NULL; /* previously read bytes, if any */
13084 int prevlen = 0; /* length of "prev" if not NULL */
13085 char_u *s;
13086 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013087 long maxline = MAXLNUM;
13088 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013090 if (argvars[1].v_type != VAR_UNKNOWN)
13091 {
13092 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13093 binary = TRUE;
13094 if (argvars[2].v_type != VAR_UNKNOWN)
13095 maxline = get_tv_number(&argvars[2]);
13096 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013097
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013098 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013099 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013100
13101 /* Always open the file in binary mode, library functions have a mind of
13102 * their own about CR-LF conversion. */
13103 fname = get_tv_string(&argvars[0]);
13104 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13105 {
13106 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13107 return;
13108 }
13109
13110 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013111 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013112 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013113 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013114 buflen = filtd + readlen;
13115 tolist = 0;
13116 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13117 {
13118 if (buf[filtd] == '\n' || readlen <= 0)
13119 {
13120 /* Only when in binary mode add an empty list item when the
13121 * last line ends in a '\n'. */
13122 if (!binary && readlen == 0 && filtd == 0)
13123 break;
13124
13125 /* Found end-of-line or end-of-file: add a text line to the
13126 * list. */
13127 chop = 0;
13128 if (!binary)
13129 while (filtd - chop - 1 >= tolist
13130 && buf[filtd - chop - 1] == '\r')
13131 ++chop;
13132 len = filtd - tolist - chop;
13133 if (prev == NULL)
13134 s = vim_strnsave(buf + tolist, len);
13135 else
13136 {
13137 s = alloc((unsigned)(prevlen + len + 1));
13138 if (s != NULL)
13139 {
13140 mch_memmove(s, prev, prevlen);
13141 vim_free(prev);
13142 prev = NULL;
13143 mch_memmove(s + prevlen, buf + tolist, len);
13144 s[prevlen + len] = NUL;
13145 }
13146 }
13147 tolist = filtd + 1;
13148
13149 li = listitem_alloc();
13150 if (li == NULL)
13151 {
13152 vim_free(s);
13153 break;
13154 }
13155 li->li_tv.v_type = VAR_STRING;
13156 li->li_tv.v_lock = 0;
13157 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013158 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013159
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013160 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013161 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013162 if (readlen <= 0)
13163 break;
13164 }
13165 else if (buf[filtd] == NUL)
13166 buf[filtd] = '\n';
13167 }
13168 if (readlen <= 0)
13169 break;
13170
13171 if (tolist == 0)
13172 {
13173 /* "buf" is full, need to move text to an allocated buffer */
13174 if (prev == NULL)
13175 {
13176 prev = vim_strnsave(buf, buflen);
13177 prevlen = buflen;
13178 }
13179 else
13180 {
13181 s = alloc((unsigned)(prevlen + buflen));
13182 if (s != NULL)
13183 {
13184 mch_memmove(s, prev, prevlen);
13185 mch_memmove(s + prevlen, buf, buflen);
13186 vim_free(prev);
13187 prev = s;
13188 prevlen += buflen;
13189 }
13190 }
13191 filtd = 0;
13192 }
13193 else
13194 {
13195 mch_memmove(buf, buf + tolist, buflen - tolist);
13196 filtd -= tolist;
13197 }
13198 }
13199
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013200 /*
13201 * For a negative line count use only the lines at the end of the file,
13202 * free the rest.
13203 */
13204 if (maxline < 0)
13205 while (cnt > -maxline)
13206 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013207 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013208 --cnt;
13209 }
13210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013211 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013212 fclose(fd);
13213}
13214
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013215#if defined(FEAT_RELTIME)
13216static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13217
13218/*
13219 * Convert a List to proftime_T.
13220 * Return FAIL when there is something wrong.
13221 */
13222 static int
13223list2proftime(arg, tm)
13224 typval_T *arg;
13225 proftime_T *tm;
13226{
13227 long n1, n2;
13228 int error = FALSE;
13229
13230 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13231 || arg->vval.v_list->lv_len != 2)
13232 return FAIL;
13233 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13234 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13235# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013236 tm->HighPart = n1;
13237 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013238# else
13239 tm->tv_sec = n1;
13240 tm->tv_usec = n2;
13241# endif
13242 return error ? FAIL : OK;
13243}
13244#endif /* FEAT_RELTIME */
13245
13246/*
13247 * "reltime()" function
13248 */
13249 static void
13250f_reltime(argvars, rettv)
13251 typval_T *argvars;
13252 typval_T *rettv;
13253{
13254#ifdef FEAT_RELTIME
13255 proftime_T res;
13256 proftime_T start;
13257
13258 if (argvars[0].v_type == VAR_UNKNOWN)
13259 {
13260 /* No arguments: get current time. */
13261 profile_start(&res);
13262 }
13263 else if (argvars[1].v_type == VAR_UNKNOWN)
13264 {
13265 if (list2proftime(&argvars[0], &res) == FAIL)
13266 return;
13267 profile_end(&res);
13268 }
13269 else
13270 {
13271 /* Two arguments: compute the difference. */
13272 if (list2proftime(&argvars[0], &start) == FAIL
13273 || list2proftime(&argvars[1], &res) == FAIL)
13274 return;
13275 profile_sub(&res, &start);
13276 }
13277
13278 if (rettv_list_alloc(rettv) == OK)
13279 {
13280 long n1, n2;
13281
13282# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013283 n1 = res.HighPart;
13284 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013285# else
13286 n1 = res.tv_sec;
13287 n2 = res.tv_usec;
13288# endif
13289 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13290 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13291 }
13292#endif
13293}
13294
13295/*
13296 * "reltimestr()" function
13297 */
13298 static void
13299f_reltimestr(argvars, rettv)
13300 typval_T *argvars;
13301 typval_T *rettv;
13302{
13303#ifdef FEAT_RELTIME
13304 proftime_T tm;
13305#endif
13306
13307 rettv->v_type = VAR_STRING;
13308 rettv->vval.v_string = NULL;
13309#ifdef FEAT_RELTIME
13310 if (list2proftime(&argvars[0], &tm) == OK)
13311 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13312#endif
13313}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013314
Bram Moolenaar0d660222005-01-07 21:51:51 +000013315#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13316static void make_connection __ARGS((void));
13317static int check_connection __ARGS((void));
13318
13319 static void
13320make_connection()
13321{
13322 if (X_DISPLAY == NULL
13323# ifdef FEAT_GUI
13324 && !gui.in_use
13325# endif
13326 )
13327 {
13328 x_force_connect = TRUE;
13329 setup_term_clip();
13330 x_force_connect = FALSE;
13331 }
13332}
13333
13334 static int
13335check_connection()
13336{
13337 make_connection();
13338 if (X_DISPLAY == NULL)
13339 {
13340 EMSG(_("E240: No connection to Vim server"));
13341 return FAIL;
13342 }
13343 return OK;
13344}
13345#endif
13346
13347#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013348static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013349
13350 static void
13351remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013352 typval_T *argvars;
13353 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013354 int expr;
13355{
13356 char_u *server_name;
13357 char_u *keys;
13358 char_u *r = NULL;
13359 char_u buf[NUMBUFLEN];
13360# ifdef WIN32
13361 HWND w;
13362# else
13363 Window w;
13364# endif
13365
13366 if (check_restricted() || check_secure())
13367 return;
13368
13369# ifdef FEAT_X11
13370 if (check_connection() == FAIL)
13371 return;
13372# endif
13373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013374 server_name = get_tv_string_chk(&argvars[0]);
13375 if (server_name == NULL)
13376 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013377 keys = get_tv_string_buf(&argvars[1], buf);
13378# ifdef WIN32
13379 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13380# else
13381 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13382 < 0)
13383# endif
13384 {
13385 if (r != NULL)
13386 EMSG(r); /* sending worked but evaluation failed */
13387 else
13388 EMSG2(_("E241: Unable to send to %s"), server_name);
13389 return;
13390 }
13391
13392 rettv->vval.v_string = r;
13393
13394 if (argvars[2].v_type != VAR_UNKNOWN)
13395 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013396 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013397 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013398 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013399
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013400 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013401 v.di_tv.v_type = VAR_STRING;
13402 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 idvar = get_tv_string_chk(&argvars[2]);
13404 if (idvar != NULL)
13405 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013407 }
13408}
13409#endif
13410
13411/*
13412 * "remote_expr()" function
13413 */
13414/*ARGSUSED*/
13415 static void
13416f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 typval_T *argvars;
13418 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013419{
13420 rettv->v_type = VAR_STRING;
13421 rettv->vval.v_string = NULL;
13422#ifdef FEAT_CLIENTSERVER
13423 remote_common(argvars, rettv, TRUE);
13424#endif
13425}
13426
13427/*
13428 * "remote_foreground()" function
13429 */
13430/*ARGSUSED*/
13431 static void
13432f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013433 typval_T *argvars;
13434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013435{
13436 rettv->vval.v_number = 0;
13437#ifdef FEAT_CLIENTSERVER
13438# ifdef WIN32
13439 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 {
13441 char_u *server_name = get_tv_string_chk(&argvars[0]);
13442
13443 if (server_name != NULL)
13444 serverForeground(server_name);
13445 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013446# else
13447 /* Send a foreground() expression to the server. */
13448 argvars[1].v_type = VAR_STRING;
13449 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13450 argvars[2].v_type = VAR_UNKNOWN;
13451 remote_common(argvars, rettv, TRUE);
13452 vim_free(argvars[1].vval.v_string);
13453# endif
13454#endif
13455}
13456
13457/*ARGSUSED*/
13458 static void
13459f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013460 typval_T *argvars;
13461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013462{
13463#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013464 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013465 char_u *s = NULL;
13466# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013467 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013468# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013469 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013470
13471 if (check_restricted() || check_secure())
13472 {
13473 rettv->vval.v_number = -1;
13474 return;
13475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013476 serverid = get_tv_string_chk(&argvars[0]);
13477 if (serverid == NULL)
13478 {
13479 rettv->vval.v_number = -1;
13480 return; /* type error; errmsg already given */
13481 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013482# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013483 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013484 if (n == 0)
13485 rettv->vval.v_number = -1;
13486 else
13487 {
13488 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13489 rettv->vval.v_number = (s != NULL);
13490 }
13491# else
13492 rettv->vval.v_number = 0;
13493 if (check_connection() == FAIL)
13494 return;
13495
13496 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013498# endif
13499
13500 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013502 char_u *retvar;
13503
Bram Moolenaar33570922005-01-25 22:26:29 +000013504 v.di_tv.v_type = VAR_STRING;
13505 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 retvar = get_tv_string_chk(&argvars[1]);
13507 if (retvar != NULL)
13508 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013509 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013510 }
13511#else
13512 rettv->vval.v_number = -1;
13513#endif
13514}
13515
13516/*ARGSUSED*/
13517 static void
13518f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013519 typval_T *argvars;
13520 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013521{
13522 char_u *r = NULL;
13523
13524#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013525 char_u *serverid = get_tv_string_chk(&argvars[0]);
13526
13527 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013528 {
13529# ifdef WIN32
13530 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013531 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013532
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013533 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013534 if (n != 0)
13535 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13536 if (r == NULL)
13537# else
13538 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013539 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013540# endif
13541 EMSG(_("E277: Unable to read a server reply"));
13542 }
13543#endif
13544 rettv->v_type = VAR_STRING;
13545 rettv->vval.v_string = r;
13546}
13547
13548/*
13549 * "remote_send()" function
13550 */
13551/*ARGSUSED*/
13552 static void
13553f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013554 typval_T *argvars;
13555 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013556{
13557 rettv->v_type = VAR_STRING;
13558 rettv->vval.v_string = NULL;
13559#ifdef FEAT_CLIENTSERVER
13560 remote_common(argvars, rettv, FALSE);
13561#endif
13562}
13563
13564/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013565 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013566 */
13567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013571{
Bram Moolenaar33570922005-01-25 22:26:29 +000013572 list_T *l;
13573 listitem_T *item, *item2;
13574 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013575 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013576 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013577 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013578 dict_T *d;
13579 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013580
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013581 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013582 if (argvars[0].v_type == VAR_DICT)
13583 {
13584 if (argvars[2].v_type != VAR_UNKNOWN)
13585 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013586 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013587 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013589 key = get_tv_string_chk(&argvars[1]);
13590 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013592 di = dict_find(d, key, -1);
13593 if (di == NULL)
13594 EMSG2(_(e_dictkey), key);
13595 else
13596 {
13597 *rettv = di->di_tv;
13598 init_tv(&di->di_tv);
13599 dictitem_remove(d, di);
13600 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013602 }
13603 }
13604 else if (argvars[0].v_type != VAR_LIST)
13605 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013606 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013607 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013609 int error = FALSE;
13610
13611 idx = get_tv_number_chk(&argvars[1], &error);
13612 if (error)
13613 ; /* type error: do nothing, errmsg already given */
13614 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013615 EMSGN(_(e_listidx), idx);
13616 else
13617 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013618 if (argvars[2].v_type == VAR_UNKNOWN)
13619 {
13620 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013621 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013622 *rettv = item->li_tv;
13623 vim_free(item);
13624 }
13625 else
13626 {
13627 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 end = get_tv_number_chk(&argvars[2], &error);
13629 if (error)
13630 ; /* type error: do nothing */
13631 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013632 EMSGN(_(e_listidx), end);
13633 else
13634 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013635 int cnt = 0;
13636
13637 for (li = item; li != NULL; li = li->li_next)
13638 {
13639 ++cnt;
13640 if (li == item2)
13641 break;
13642 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013643 if (li == NULL) /* didn't find "item2" after "item" */
13644 EMSG(_(e_invrange));
13645 else
13646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013647 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013648 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013649 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013650 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013651 l->lv_first = item;
13652 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013653 item->li_prev = NULL;
13654 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013655 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013656 }
13657 }
13658 }
13659 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013660 }
13661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662}
13663
13664/*
13665 * "rename({from}, {to})" function
13666 */
13667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013669 typval_T *argvars;
13670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671{
13672 char_u buf[NUMBUFLEN];
13673
13674 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013675 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13678 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679}
13680
13681/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013682 * "repeat()" function
13683 */
13684/*ARGSUSED*/
13685 static void
13686f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013687 typval_T *argvars;
13688 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013689{
13690 char_u *p;
13691 int n;
13692 int slen;
13693 int len;
13694 char_u *r;
13695 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013696
13697 n = get_tv_number(&argvars[1]);
13698 if (argvars[0].v_type == VAR_LIST)
13699 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013700 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013701 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013702 if (list_extend(rettv->vval.v_list,
13703 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013704 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013705 }
13706 else
13707 {
13708 p = get_tv_string(&argvars[0]);
13709 rettv->v_type = VAR_STRING;
13710 rettv->vval.v_string = NULL;
13711
13712 slen = (int)STRLEN(p);
13713 len = slen * n;
13714 if (len <= 0)
13715 return;
13716
13717 r = alloc(len + 1);
13718 if (r != NULL)
13719 {
13720 for (i = 0; i < n; i++)
13721 mch_memmove(r + i * slen, p, (size_t)slen);
13722 r[len] = NUL;
13723 }
13724
13725 rettv->vval.v_string = r;
13726 }
13727}
13728
13729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 * "resolve()" function
13731 */
13732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 typval_T *argvars;
13735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736{
13737 char_u *p;
13738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740#ifdef FEAT_SHORTCUT
13741 {
13742 char_u *v = NULL;
13743
13744 v = mch_resolve_shortcut(p);
13745 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 }
13750#else
13751# ifdef HAVE_READLINK
13752 {
13753 char_u buf[MAXPATHL + 1];
13754 char_u *cpy;
13755 int len;
13756 char_u *remain = NULL;
13757 char_u *q;
13758 int is_relative_to_current = FALSE;
13759 int has_trailing_pathsep = FALSE;
13760 int limit = 100;
13761
13762 p = vim_strsave(p);
13763
13764 if (p[0] == '.' && (vim_ispathsep(p[1])
13765 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13766 is_relative_to_current = TRUE;
13767
13768 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013769 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 has_trailing_pathsep = TRUE;
13771
13772 q = getnextcomp(p);
13773 if (*q != NUL)
13774 {
13775 /* Separate the first path component in "p", and keep the
13776 * remainder (beginning with the path separator). */
13777 remain = vim_strsave(q - 1);
13778 q[-1] = NUL;
13779 }
13780
13781 for (;;)
13782 {
13783 for (;;)
13784 {
13785 len = readlink((char *)p, (char *)buf, MAXPATHL);
13786 if (len <= 0)
13787 break;
13788 buf[len] = NUL;
13789
13790 if (limit-- == 0)
13791 {
13792 vim_free(p);
13793 vim_free(remain);
13794 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 goto fail;
13797 }
13798
13799 /* Ensure that the result will have a trailing path separator
13800 * if the argument has one. */
13801 if (remain == NULL && has_trailing_pathsep)
13802 add_pathsep(buf);
13803
13804 /* Separate the first path component in the link value and
13805 * concatenate the remainders. */
13806 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13807 if (*q != NUL)
13808 {
13809 if (remain == NULL)
13810 remain = vim_strsave(q - 1);
13811 else
13812 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013813 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 if (cpy != NULL)
13815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 vim_free(remain);
13817 remain = cpy;
13818 }
13819 }
13820 q[-1] = NUL;
13821 }
13822
13823 q = gettail(p);
13824 if (q > p && *q == NUL)
13825 {
13826 /* Ignore trailing path separator. */
13827 q[-1] = NUL;
13828 q = gettail(p);
13829 }
13830 if (q > p && !mch_isFullName(buf))
13831 {
13832 /* symlink is relative to directory of argument */
13833 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13834 if (cpy != NULL)
13835 {
13836 STRCPY(cpy, p);
13837 STRCPY(gettail(cpy), buf);
13838 vim_free(p);
13839 p = cpy;
13840 }
13841 }
13842 else
13843 {
13844 vim_free(p);
13845 p = vim_strsave(buf);
13846 }
13847 }
13848
13849 if (remain == NULL)
13850 break;
13851
13852 /* Append the first path component of "remain" to "p". */
13853 q = getnextcomp(remain + 1);
13854 len = q - remain - (*q != NUL);
13855 cpy = vim_strnsave(p, STRLEN(p) + len);
13856 if (cpy != NULL)
13857 {
13858 STRNCAT(cpy, remain, len);
13859 vim_free(p);
13860 p = cpy;
13861 }
13862 /* Shorten "remain". */
13863 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013864 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 else
13866 {
13867 vim_free(remain);
13868 remain = NULL;
13869 }
13870 }
13871
13872 /* If the result is a relative path name, make it explicitly relative to
13873 * the current directory if and only if the argument had this form. */
13874 if (!vim_ispathsep(*p))
13875 {
13876 if (is_relative_to_current
13877 && *p != NUL
13878 && !(p[0] == '.'
13879 && (p[1] == NUL
13880 || vim_ispathsep(p[1])
13881 || (p[1] == '.'
13882 && (p[2] == NUL
13883 || vim_ispathsep(p[2]))))))
13884 {
13885 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013886 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 if (cpy != NULL)
13888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 vim_free(p);
13890 p = cpy;
13891 }
13892 }
13893 else if (!is_relative_to_current)
13894 {
13895 /* Strip leading "./". */
13896 q = p;
13897 while (q[0] == '.' && vim_ispathsep(q[1]))
13898 q += 2;
13899 if (q > p)
13900 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13901 }
13902 }
13903
13904 /* Ensure that the result will have no trailing path separator
13905 * if the argument had none. But keep "/" or "//". */
13906 if (!has_trailing_pathsep)
13907 {
13908 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013909 if (after_pathsep(p, q))
13910 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 }
13912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 }
13915# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013916 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917# endif
13918#endif
13919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921
13922#ifdef HAVE_READLINK
13923fail:
13924#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013925 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926}
13927
13928/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013929 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 */
13931 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013932f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013933 typval_T *argvars;
13934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935{
Bram Moolenaar33570922005-01-25 22:26:29 +000013936 list_T *l;
13937 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938
Bram Moolenaar0d660222005-01-07 21:51:51 +000013939 rettv->vval.v_number = 0;
13940 if (argvars[0].v_type != VAR_LIST)
13941 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013942 else if ((l = argvars[0].vval.v_list) != NULL
13943 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013944 {
13945 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013946 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013947 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013948 while (li != NULL)
13949 {
13950 ni = li->li_prev;
13951 list_append(l, li);
13952 li = ni;
13953 }
13954 rettv->vval.v_list = l;
13955 rettv->v_type = VAR_LIST;
13956 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000013957 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959}
13960
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013961#define SP_NOMOVE 0x01 /* don't move cursor */
13962#define SP_REPEAT 0x02 /* repeat to find outer pair */
13963#define SP_RETCOUNT 0x04 /* return matchcount */
13964#define SP_SETPCMARK 0x08 /* set previous context mark */
13965#define SP_START 0x10 /* accept match at start position */
13966#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13967#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013968
Bram Moolenaar33570922005-01-25 22:26:29 +000013969static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013970
13971/*
13972 * Get flags for a search function.
13973 * Possibly sets "p_ws".
13974 * Returns BACKWARD, FORWARD or zero (for an error).
13975 */
13976 static int
13977get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013978 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013979 int *flagsp;
13980{
13981 int dir = FORWARD;
13982 char_u *flags;
13983 char_u nbuf[NUMBUFLEN];
13984 int mask;
13985
13986 if (varp->v_type != VAR_UNKNOWN)
13987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988 flags = get_tv_string_buf_chk(varp, nbuf);
13989 if (flags == NULL)
13990 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013991 while (*flags != NUL)
13992 {
13993 switch (*flags)
13994 {
13995 case 'b': dir = BACKWARD; break;
13996 case 'w': p_ws = TRUE; break;
13997 case 'W': p_ws = FALSE; break;
13998 default: mask = 0;
13999 if (flagsp != NULL)
14000 switch (*flags)
14001 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014002 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014003 case 'e': mask = SP_END; break;
14004 case 'm': mask = SP_RETCOUNT; break;
14005 case 'n': mask = SP_NOMOVE; break;
14006 case 'p': mask = SP_SUBPAT; break;
14007 case 'r': mask = SP_REPEAT; break;
14008 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014009 }
14010 if (mask == 0)
14011 {
14012 EMSG2(_(e_invarg2), flags);
14013 dir = 0;
14014 }
14015 else
14016 *flagsp |= mask;
14017 }
14018 if (dir == 0)
14019 break;
14020 ++flags;
14021 }
14022 }
14023 return dir;
14024}
14025
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014027 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014029 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014030search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014031 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014032 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014033 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014035 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036 char_u *pat;
14037 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014038 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 int save_p_ws = p_ws;
14040 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014041 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014042 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014043 proftime_T tm;
14044#ifdef FEAT_RELTIME
14045 long time_limit = 0;
14046#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014047 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014048 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014050 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014051 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014052 if (dir == 0)
14053 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014054 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014055 if (flags & SP_START)
14056 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014057 if (flags & SP_END)
14058 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014059
Bram Moolenaar76929292008-01-06 19:07:36 +000014060 /* Optional arguments: line number to stop searching and timeout. */
14061 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014062 {
14063 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14064 if (lnum_stop < 0)
14065 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014066#ifdef FEAT_RELTIME
14067 if (argvars[3].v_type != VAR_UNKNOWN)
14068 {
14069 time_limit = get_tv_number_chk(&argvars[3], NULL);
14070 if (time_limit < 0)
14071 goto theend;
14072 }
14073#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014074 }
14075
Bram Moolenaar76929292008-01-06 19:07:36 +000014076#ifdef FEAT_RELTIME
14077 /* Set the time limit, if there is one. */
14078 profile_setlimit(time_limit, &tm);
14079#endif
14080
Bram Moolenaar231334e2005-07-25 20:46:57 +000014081 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014082 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014083 * Check to make sure only those flags are set.
14084 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14085 * flags cannot be set. Check for that condition also.
14086 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014087 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014088 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014091 goto theend;
14092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014094 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014095 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014096 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014097 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014099 if (flags & SP_SUBPAT)
14100 retval = subpatnum;
14101 else
14102 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014103 if (flags & SP_SETPCMARK)
14104 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014106 if (match_pos != NULL)
14107 {
14108 /* Store the match cursor position */
14109 match_pos->lnum = pos.lnum;
14110 match_pos->col = pos.col + 1;
14111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 /* "/$" will put the cursor after the end of the line, may need to
14113 * correct that here */
14114 check_cursor();
14115 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014116
14117 /* If 'n' flag is used: restore cursor position. */
14118 if (flags & SP_NOMOVE)
14119 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014120 else
14121 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014122theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014124
14125 return retval;
14126}
14127
14128/*
14129 * "search()" function
14130 */
14131 static void
14132f_search(argvars, rettv)
14133 typval_T *argvars;
14134 typval_T *rettv;
14135{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014136 int flags = 0;
14137
14138 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139}
14140
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014142 * "searchdecl()" function
14143 */
14144 static void
14145f_searchdecl(argvars, rettv)
14146 typval_T *argvars;
14147 typval_T *rettv;
14148{
14149 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014150 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014151 int error = FALSE;
14152 char_u *name;
14153
14154 rettv->vval.v_number = 1; /* default: FAIL */
14155
14156 name = get_tv_string_chk(&argvars[0]);
14157 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014158 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014159 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014160 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14161 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14162 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014163 if (!error && name != NULL)
14164 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014165 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014166}
14167
14168/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014169 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014171 static int
14172searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014174 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175{
14176 char_u *spat, *mpat, *epat;
14177 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 int dir;
14180 int flags = 0;
14181 char_u nbuf1[NUMBUFLEN];
14182 char_u nbuf2[NUMBUFLEN];
14183 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014184 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014185 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014186 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 spat = get_tv_string_chk(&argvars[0]);
14190 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14191 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14192 if (spat == NULL || mpat == NULL || epat == NULL)
14193 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 /* Handle the optional fourth argument: flags */
14196 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014197 if (dir == 0)
14198 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014199
14200 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014201 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14202 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014203 if ((flags & (SP_END | SP_SUBPAT)) != 0
14204 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014205 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014206 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014207 goto theend;
14208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014210 /* Using 'r' implies 'W', otherwise it doesn't work. */
14211 if (flags & SP_REPEAT)
14212 p_ws = FALSE;
14213
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014214 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014215 if (argvars[3].v_type == VAR_UNKNOWN
14216 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 skip = (char_u *)"";
14218 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014220 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014221 if (argvars[5].v_type != VAR_UNKNOWN)
14222 {
14223 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14224 if (lnum_stop < 0)
14225 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014226#ifdef FEAT_RELTIME
14227 if (argvars[6].v_type != VAR_UNKNOWN)
14228 {
14229 time_limit = get_tv_number_chk(&argvars[6], NULL);
14230 if (time_limit < 0)
14231 goto theend;
14232 }
14233#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014234 }
14235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 if (skip == NULL)
14237 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014239 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014240 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014241
14242theend:
14243 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014244
14245 return retval;
14246}
14247
14248/*
14249 * "searchpair()" function
14250 */
14251 static void
14252f_searchpair(argvars, rettv)
14253 typval_T *argvars;
14254 typval_T *rettv;
14255{
14256 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14257}
14258
14259/*
14260 * "searchpairpos()" function
14261 */
14262 static void
14263f_searchpairpos(argvars, rettv)
14264 typval_T *argvars;
14265 typval_T *rettv;
14266{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014267 pos_T match_pos;
14268 int lnum = 0;
14269 int col = 0;
14270
14271 rettv->vval.v_number = 0;
14272
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014273 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014274 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014275
14276 if (searchpair_cmn(argvars, &match_pos) > 0)
14277 {
14278 lnum = match_pos.lnum;
14279 col = match_pos.col;
14280 }
14281
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014282 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14283 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014284}
14285
14286/*
14287 * Search for a start/middle/end thing.
14288 * Used by searchpair(), see its documentation for the details.
14289 * Returns 0 or -1 for no match,
14290 */
14291 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014292do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14293 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014294 char_u *spat; /* start pattern */
14295 char_u *mpat; /* middle pattern */
14296 char_u *epat; /* end pattern */
14297 int dir; /* BACKWARD or FORWARD */
14298 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014299 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014300 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014301 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014302 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014303{
14304 char_u *save_cpo;
14305 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14306 long retval = 0;
14307 pos_T pos;
14308 pos_T firstpos;
14309 pos_T foundpos;
14310 pos_T save_cursor;
14311 pos_T save_pos;
14312 int n;
14313 int r;
14314 int nest = 1;
14315 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014316 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014317 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014318
14319 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14320 save_cpo = p_cpo;
14321 p_cpo = (char_u *)"";
14322
Bram Moolenaar76929292008-01-06 19:07:36 +000014323#ifdef FEAT_RELTIME
14324 /* Set the time limit, if there is one. */
14325 profile_setlimit(time_limit, &tm);
14326#endif
14327
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014328 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14329 * start/middle/end (pat3, for the top pair). */
14330 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14331 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14332 if (pat2 == NULL || pat3 == NULL)
14333 goto theend;
14334 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14335 if (*mpat == NUL)
14336 STRCPY(pat3, pat2);
14337 else
14338 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14339 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014340 if (flags & SP_START)
14341 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014342
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 save_cursor = curwin->w_cursor;
14344 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014345 clearpos(&firstpos);
14346 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347 pat = pat3;
14348 for (;;)
14349 {
14350 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014351 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14353 /* didn't find it or found the first match again: FAIL */
14354 break;
14355
14356 if (firstpos.lnum == 0)
14357 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014358 if (equalpos(pos, foundpos))
14359 {
14360 /* Found the same position again. Can happen with a pattern that
14361 * has "\zs" at the end and searching backwards. Advance one
14362 * character and try again. */
14363 if (dir == BACKWARD)
14364 decl(&pos);
14365 else
14366 incl(&pos);
14367 }
14368 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014370 /* clear the start flag to avoid getting stuck here */
14371 options &= ~SEARCH_START;
14372
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 /* If the skip pattern matches, ignore this match. */
14374 if (*skip != NUL)
14375 {
14376 save_pos = curwin->w_cursor;
14377 curwin->w_cursor = pos;
14378 r = eval_to_bool(skip, &err, NULL, FALSE);
14379 curwin->w_cursor = save_pos;
14380 if (err)
14381 {
14382 /* Evaluating {skip} caused an error, break here. */
14383 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014384 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 break;
14386 }
14387 if (r)
14388 continue;
14389 }
14390
14391 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14392 {
14393 /* Found end when searching backwards or start when searching
14394 * forward: nested pair. */
14395 ++nest;
14396 pat = pat2; /* nested, don't search for middle */
14397 }
14398 else
14399 {
14400 /* Found end when searching forward or start when searching
14401 * backward: end of (nested) pair; or found middle in outer pair. */
14402 if (--nest == 1)
14403 pat = pat3; /* outer level, search for middle */
14404 }
14405
14406 if (nest == 0)
14407 {
14408 /* Found the match: return matchcount or line number. */
14409 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014410 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014412 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014413 if (flags & SP_SETPCMARK)
14414 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 curwin->w_cursor = pos;
14416 if (!(flags & SP_REPEAT))
14417 break;
14418 nest = 1; /* search for next unmatched */
14419 }
14420 }
14421
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014422 if (match_pos != NULL)
14423 {
14424 /* Store the match cursor position */
14425 match_pos->lnum = curwin->w_cursor.lnum;
14426 match_pos->col = curwin->w_cursor.col + 1;
14427 }
14428
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014430 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 curwin->w_cursor = save_cursor;
14432
14433theend:
14434 vim_free(pat2);
14435 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014437
14438 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439}
14440
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014441/*
14442 * "searchpos()" function
14443 */
14444 static void
14445f_searchpos(argvars, rettv)
14446 typval_T *argvars;
14447 typval_T *rettv;
14448{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014449 pos_T match_pos;
14450 int lnum = 0;
14451 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014452 int n;
14453 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014454
14455 rettv->vval.v_number = 0;
14456
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014457 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014458 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014459
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014460 n = search_cmn(argvars, &match_pos, &flags);
14461 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014462 {
14463 lnum = match_pos.lnum;
14464 col = match_pos.col;
14465 }
14466
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014467 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14468 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014469 if (flags & SP_SUBPAT)
14470 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014471}
14472
14473
Bram Moolenaar0d660222005-01-07 21:51:51 +000014474/*ARGSUSED*/
14475 static void
14476f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014477 typval_T *argvars;
14478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480#ifdef FEAT_CLIENTSERVER
14481 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014482 char_u *server = get_tv_string_chk(&argvars[0]);
14483 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484
Bram Moolenaar0d660222005-01-07 21:51:51 +000014485 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014486 if (server == NULL || reply == NULL)
14487 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014488 if (check_restricted() || check_secure())
14489 return;
14490# ifdef FEAT_X11
14491 if (check_connection() == FAIL)
14492 return;
14493# endif
14494
14495 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014497 EMSG(_("E258: Unable to send to client"));
14498 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014500 rettv->vval.v_number = 0;
14501#else
14502 rettv->vval.v_number = -1;
14503#endif
14504}
14505
14506/*ARGSUSED*/
14507 static void
14508f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014509 typval_T *argvars;
14510 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014511{
14512 char_u *r = NULL;
14513
14514#ifdef FEAT_CLIENTSERVER
14515# ifdef WIN32
14516 r = serverGetVimNames();
14517# else
14518 make_connection();
14519 if (X_DISPLAY != NULL)
14520 r = serverGetVimNames(X_DISPLAY);
14521# endif
14522#endif
14523 rettv->v_type = VAR_STRING;
14524 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525}
14526
14527/*
14528 * "setbufvar()" function
14529 */
14530/*ARGSUSED*/
14531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014532f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014533 typval_T *argvars;
14534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535{
14536 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014539 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 char_u nbuf[NUMBUFLEN];
14541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014542 rettv->vval.v_number = 0;
14543
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 if (check_restricted() || check_secure())
14545 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014546 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14547 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014548 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549 varp = &argvars[2];
14550
14551 if (buf != NULL && varname != NULL && varp != NULL)
14552 {
14553 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555
14556 if (*varname == '&')
14557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 long numval;
14559 char_u *strval;
14560 int error = FALSE;
14561
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014563 numval = get_tv_number_chk(varp, &error);
14564 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014565 if (!error && strval != NULL)
14566 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 }
14568 else
14569 {
14570 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14571 if (bufvarname != NULL)
14572 {
14573 STRCPY(bufvarname, "b:");
14574 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014575 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 vim_free(bufvarname);
14577 }
14578 }
14579
14580 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583}
14584
14585/*
14586 * "setcmdpos()" function
14587 */
14588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014589f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014590 typval_T *argvars;
14591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014593 int pos = (int)get_tv_number(&argvars[0]) - 1;
14594
14595 if (pos >= 0)
14596 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597}
14598
14599/*
14600 * "setline()" function
14601 */
14602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014603f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014604 typval_T *argvars;
14605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606{
14607 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014608 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014609 list_T *l = NULL;
14610 listitem_T *li = NULL;
14611 long added = 0;
14612 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014614 lnum = get_tv_lnum(&argvars[0]);
14615 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014617 l = argvars[1].vval.v_list;
14618 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014620 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014622
14623 rettv->vval.v_number = 0; /* OK */
14624 for (;;)
14625 {
14626 if (l != NULL)
14627 {
14628 /* list argument, get next string */
14629 if (li == NULL)
14630 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014631 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014632 li = li->li_next;
14633 }
14634
14635 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014637 break;
14638 if (lnum <= curbuf->b_ml.ml_line_count)
14639 {
14640 /* existing line, replace it */
14641 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14642 {
14643 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014644 if (lnum == curwin->w_cursor.lnum)
14645 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014646 rettv->vval.v_number = 0; /* OK */
14647 }
14648 }
14649 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14650 {
14651 /* lnum is one past the last line, append the line */
14652 ++added;
14653 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14654 rettv->vval.v_number = 0; /* OK */
14655 }
14656
14657 if (l == NULL) /* only one string argument */
14658 break;
14659 ++lnum;
14660 }
14661
14662 if (added > 0)
14663 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664}
14665
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000014666static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
14667
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014669 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014670 */
14671/*ARGSUSED*/
14672 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014673set_qf_ll_list(wp, list_arg, action_arg, rettv)
14674 win_T *wp;
14675 typval_T *list_arg;
14676 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014677 typval_T *rettv;
14678{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014679#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014680 char_u *act;
14681 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014682#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014683
Bram Moolenaar2641f772005-03-25 21:58:17 +000014684 rettv->vval.v_number = -1;
14685
14686#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014687 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014688 EMSG(_(e_listreq));
14689 else
14690 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014691 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014692
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014693 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014694 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014695 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014696 if (act == NULL)
14697 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014698 if (*act == 'a' || *act == 'r')
14699 action = *act;
14700 }
14701
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014702 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014703 rettv->vval.v_number = 0;
14704 }
14705#endif
14706}
14707
14708/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014709 * "setloclist()" function
14710 */
14711/*ARGSUSED*/
14712 static void
14713f_setloclist(argvars, rettv)
14714 typval_T *argvars;
14715 typval_T *rettv;
14716{
14717 win_T *win;
14718
14719 rettv->vval.v_number = -1;
14720
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014721 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014722 if (win != NULL)
14723 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14724}
14725
14726/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014727 * "setmatches()" function
14728 */
14729 static void
14730f_setmatches(argvars, rettv)
14731 typval_T *argvars;
14732 typval_T *rettv;
14733{
14734#ifdef FEAT_SEARCH_EXTRA
14735 list_T *l;
14736 listitem_T *li;
14737 dict_T *d;
14738
14739 rettv->vval.v_number = -1;
14740 if (argvars[0].v_type != VAR_LIST)
14741 {
14742 EMSG(_(e_listreq));
14743 return;
14744 }
14745 if ((l = argvars[0].vval.v_list) != NULL)
14746 {
14747
14748 /* To some extent make sure that we are dealing with a list from
14749 * "getmatches()". */
14750 li = l->lv_first;
14751 while (li != NULL)
14752 {
14753 if (li->li_tv.v_type != VAR_DICT
14754 || (d = li->li_tv.vval.v_dict) == NULL)
14755 {
14756 EMSG(_(e_invarg));
14757 return;
14758 }
14759 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14760 && dict_find(d, (char_u *)"pattern", -1) != NULL
14761 && dict_find(d, (char_u *)"priority", -1) != NULL
14762 && dict_find(d, (char_u *)"id", -1) != NULL))
14763 {
14764 EMSG(_(e_invarg));
14765 return;
14766 }
14767 li = li->li_next;
14768 }
14769
14770 clear_matches(curwin);
14771 li = l->lv_first;
14772 while (li != NULL)
14773 {
14774 d = li->li_tv.vval.v_dict;
14775 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14776 get_dict_string(d, (char_u *)"pattern", FALSE),
14777 (int)get_dict_number(d, (char_u *)"priority"),
14778 (int)get_dict_number(d, (char_u *)"id"));
14779 li = li->li_next;
14780 }
14781 rettv->vval.v_number = 0;
14782 }
14783#endif
14784}
14785
14786/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014787 * "setpos()" function
14788 */
14789/*ARGSUSED*/
14790 static void
14791f_setpos(argvars, rettv)
14792 typval_T *argvars;
14793 typval_T *rettv;
14794{
14795 pos_T pos;
14796 int fnum;
14797 char_u *name;
14798
Bram Moolenaar08250432008-02-13 11:42:46 +000014799 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014800 name = get_tv_string_chk(argvars);
14801 if (name != NULL)
14802 {
14803 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14804 {
14805 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000014806 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014807 {
Bram Moolenaar08250432008-02-13 11:42:46 +000014808 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014809 if (fnum == curbuf->b_fnum)
14810 {
14811 curwin->w_cursor = pos;
14812 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000014813 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014814 }
14815 else
14816 EMSG(_(e_invarg));
14817 }
Bram Moolenaar08250432008-02-13 11:42:46 +000014818 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
14819 {
14820 /* set mark */
14821 if (setmark_pos(name[1], &pos, fnum) == OK)
14822 rettv->vval.v_number = 0;
14823 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014824 else
14825 EMSG(_(e_invarg));
14826 }
14827 }
14828}
14829
14830/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014831 * "setqflist()" function
14832 */
14833/*ARGSUSED*/
14834 static void
14835f_setqflist(argvars, rettv)
14836 typval_T *argvars;
14837 typval_T *rettv;
14838{
14839 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14840}
14841
14842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843 * "setreg()" function
14844 */
14845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014847 typval_T *argvars;
14848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849{
14850 int regname;
14851 char_u *strregname;
14852 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014853 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 int append;
14855 char_u yank_type;
14856 long block_len;
14857
14858 block_len = -1;
14859 yank_type = MAUTO;
14860 append = FALSE;
14861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014862 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014863 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014865 if (strregname == NULL)
14866 return; /* type error; errmsg already given */
14867 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868 if (regname == 0 || regname == '@')
14869 regname = '"';
14870 else if (regname == '=')
14871 return;
14872
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014875 stropt = get_tv_string_chk(&argvars[2]);
14876 if (stropt == NULL)
14877 return; /* type error */
14878 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879 switch (*stropt)
14880 {
14881 case 'a': case 'A': /* append */
14882 append = TRUE;
14883 break;
14884 case 'v': case 'c': /* character-wise selection */
14885 yank_type = MCHAR;
14886 break;
14887 case 'V': case 'l': /* line-wise selection */
14888 yank_type = MLINE;
14889 break;
14890#ifdef FEAT_VISUAL
14891 case 'b': case Ctrl_V: /* block-wise selection */
14892 yank_type = MBLOCK;
14893 if (VIM_ISDIGIT(stropt[1]))
14894 {
14895 ++stropt;
14896 block_len = getdigits(&stropt) - 1;
14897 --stropt;
14898 }
14899 break;
14900#endif
14901 }
14902 }
14903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014904 strval = get_tv_string_chk(&argvars[1]);
14905 if (strval != NULL)
14906 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909}
14910
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014911/*
14912 * "settabwinvar()" function
14913 */
14914 static void
14915f_settabwinvar(argvars, rettv)
14916 typval_T *argvars;
14917 typval_T *rettv;
14918{
14919 setwinvar(argvars, rettv, 1);
14920}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921
14922/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014923 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014926f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014927 typval_T *argvars;
14928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014930 setwinvar(argvars, rettv, 0);
14931}
14932
14933/*
14934 * "setwinvar()" and "settabwinvar()" functions
14935 */
14936 static void
14937setwinvar(argvars, rettv, off)
14938 typval_T *argvars;
14939 typval_T *rettv;
14940 int off;
14941{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 win_T *win;
14943#ifdef FEAT_WINDOWS
14944 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014945 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946#endif
14947 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014948 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014950 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014952 rettv->vval.v_number = 0;
14953
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954 if (check_restricted() || check_secure())
14955 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014956
14957#ifdef FEAT_WINDOWS
14958 if (off == 1)
14959 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14960 else
14961 tp = curtab;
14962#endif
14963 win = find_win_by_nr(&argvars[off], tp);
14964 varname = get_tv_string_chk(&argvars[off + 1]);
14965 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966
14967 if (win != NULL && varname != NULL && varp != NULL)
14968 {
14969#ifdef FEAT_WINDOWS
14970 /* set curwin to be our win, temporarily */
14971 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014972 save_curtab = curtab;
14973 goto_tabpage_tp(tp);
14974 if (!win_valid(win))
14975 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 curwin = win;
14977 curbuf = curwin->w_buffer;
14978#endif
14979
14980 if (*varname == '&')
14981 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014982 long numval;
14983 char_u *strval;
14984 int error = FALSE;
14985
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014987 numval = get_tv_number_chk(varp, &error);
14988 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014989 if (!error && strval != NULL)
14990 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991 }
14992 else
14993 {
14994 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14995 if (winvarname != NULL)
14996 {
14997 STRCPY(winvarname, "w:");
14998 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014999 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000 vim_free(winvarname);
15001 }
15002 }
15003
15004#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015005 /* Restore current tabpage and window, if still valid (autocomands can
15006 * make them invalid). */
15007 if (valid_tabpage(save_curtab))
15008 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 if (win_valid(save_curwin))
15010 {
15011 curwin = save_curwin;
15012 curbuf = curwin->w_buffer;
15013 }
15014#endif
15015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016}
15017
15018/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015019 * "shellescape({string})" function
15020 */
15021 static void
15022f_shellescape(argvars, rettv)
15023 typval_T *argvars;
15024 typval_T *rettv;
15025{
15026 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
15027 rettv->v_type = VAR_STRING;
15028}
15029
15030/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015031 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 */
15033 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015034f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015035 typval_T *argvars;
15036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015038 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039
Bram Moolenaar0d660222005-01-07 21:51:51 +000015040 p = get_tv_string(&argvars[0]);
15041 rettv->vval.v_string = vim_strsave(p);
15042 simplify_filename(rettv->vval.v_string); /* simplify in place */
15043 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044}
15045
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046static int
15047#ifdef __BORLANDC__
15048 _RTLENTRYF
15049#endif
15050 item_compare __ARGS((const void *s1, const void *s2));
15051static int
15052#ifdef __BORLANDC__
15053 _RTLENTRYF
15054#endif
15055 item_compare2 __ARGS((const void *s1, const void *s2));
15056
15057static int item_compare_ic;
15058static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015059static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060#define ITEM_COMPARE_FAIL 999
15061
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015063 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065 static int
15066#ifdef __BORLANDC__
15067_RTLENTRYF
15068#endif
15069item_compare(s1, s2)
15070 const void *s1;
15071 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015073 char_u *p1, *p2;
15074 char_u *tofree1, *tofree2;
15075 int res;
15076 char_u numbuf1[NUMBUFLEN];
15077 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015079 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15080 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015081 if (p1 == NULL)
15082 p1 = (char_u *)"";
15083 if (p2 == NULL)
15084 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 if (item_compare_ic)
15086 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088 res = STRCMP(p1, p2);
15089 vim_free(tofree1);
15090 vim_free(tofree2);
15091 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092}
15093
15094 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095#ifdef __BORLANDC__
15096_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015098item_compare2(s1, s2)
15099 const void *s1;
15100 const void *s2;
15101{
15102 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015103 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015104 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015107 /* shortcut after failure in previous call; compare all items equal */
15108 if (item_compare_func_err)
15109 return 0;
15110
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015111 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15112 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015113 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15114 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015115
15116 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015117 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015118 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119 clear_tv(&argv[0]);
15120 clear_tv(&argv[1]);
15121
15122 if (res == FAIL)
15123 res = ITEM_COMPARE_FAIL;
15124 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015125 /* return value has wrong type */
15126 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15127 if (item_compare_func_err)
15128 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129 clear_tv(&rettv);
15130 return res;
15131}
15132
15133/*
15134 * "sort({list})" function
15135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015137f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 typval_T *argvars;
15139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140{
Bram Moolenaar33570922005-01-25 22:26:29 +000015141 list_T *l;
15142 listitem_T *li;
15143 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144 long len;
15145 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146
Bram Moolenaar0d660222005-01-07 21:51:51 +000015147 rettv->vval.v_number = 0;
15148 if (argvars[0].v_type != VAR_LIST)
15149 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 else
15151 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015152 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015153 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154 return;
15155 rettv->vval.v_list = l;
15156 rettv->v_type = VAR_LIST;
15157 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 len = list_len(l);
15160 if (len <= 1)
15161 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163 item_compare_ic = FALSE;
15164 item_compare_func = NULL;
15165 if (argvars[1].v_type != VAR_UNKNOWN)
15166 {
15167 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015168 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169 else
15170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015171 int error = FALSE;
15172
15173 i = get_tv_number_chk(&argvars[1], &error);
15174 if (error)
15175 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 if (i == 1)
15177 item_compare_ic = TRUE;
15178 else
15179 item_compare_func = get_tv_string(&argvars[1]);
15180 }
15181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182
Bram Moolenaar0d660222005-01-07 21:51:51 +000015183 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015184 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015185 if (ptrs == NULL)
15186 return;
15187 i = 0;
15188 for (li = l->lv_first; li != NULL; li = li->li_next)
15189 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015191 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192 /* test the compare function */
15193 if (item_compare_func != NULL
15194 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15195 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015196 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198 {
15199 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015200 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201 item_compare_func == NULL ? item_compare : item_compare2);
15202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 if (!item_compare_func_err)
15204 {
15205 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015206 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015207 l->lv_len = 0;
15208 for (i = 0; i < len; ++i)
15209 list_append(l, ptrs[i]);
15210 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211 }
15212
15213 vim_free(ptrs);
15214 }
15215}
15216
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015217/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015218 * "soundfold({word})" function
15219 */
15220 static void
15221f_soundfold(argvars, rettv)
15222 typval_T *argvars;
15223 typval_T *rettv;
15224{
15225 char_u *s;
15226
15227 rettv->v_type = VAR_STRING;
15228 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015229#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015230 rettv->vval.v_string = eval_soundfold(s);
15231#else
15232 rettv->vval.v_string = vim_strsave(s);
15233#endif
15234}
15235
15236/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015237 * "spellbadword()" function
15238 */
15239/* ARGSUSED */
15240 static void
15241f_spellbadword(argvars, rettv)
15242 typval_T *argvars;
15243 typval_T *rettv;
15244{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015245 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015246 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015247 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015248
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015249 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015250 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015251
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015252#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015253 if (argvars[0].v_type == VAR_UNKNOWN)
15254 {
15255 /* Find the start and length of the badly spelled word. */
15256 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15257 if (len != 0)
15258 word = ml_get_cursor();
15259 }
15260 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15261 {
15262 char_u *str = get_tv_string_chk(&argvars[0]);
15263 int capcol = -1;
15264
15265 if (str != NULL)
15266 {
15267 /* Check the argument for spelling. */
15268 while (*str != NUL)
15269 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015270 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015271 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015272 {
15273 word = str;
15274 break;
15275 }
15276 str += len;
15277 }
15278 }
15279 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015280#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015281
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015282 list_append_string(rettv->vval.v_list, word, len);
15283 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015284 attr == HLF_SPB ? "bad" :
15285 attr == HLF_SPR ? "rare" :
15286 attr == HLF_SPL ? "local" :
15287 attr == HLF_SPC ? "caps" :
15288 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015289}
15290
15291/*
15292 * "spellsuggest()" function
15293 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015294/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015295 static void
15296f_spellsuggest(argvars, rettv)
15297 typval_T *argvars;
15298 typval_T *rettv;
15299{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015300#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015301 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015302 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015303 int maxcount;
15304 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015305 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015306 listitem_T *li;
15307 int need_capital = FALSE;
15308#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015309
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015310 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015311 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015312
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015313#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015314 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15315 {
15316 str = get_tv_string(&argvars[0]);
15317 if (argvars[1].v_type != VAR_UNKNOWN)
15318 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015319 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015320 if (maxcount <= 0)
15321 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015322 if (argvars[2].v_type != VAR_UNKNOWN)
15323 {
15324 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15325 if (typeerr)
15326 return;
15327 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015328 }
15329 else
15330 maxcount = 25;
15331
Bram Moolenaar4770d092006-01-12 23:22:24 +000015332 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015333
15334 for (i = 0; i < ga.ga_len; ++i)
15335 {
15336 str = ((char_u **)ga.ga_data)[i];
15337
15338 li = listitem_alloc();
15339 if (li == NULL)
15340 vim_free(str);
15341 else
15342 {
15343 li->li_tv.v_type = VAR_STRING;
15344 li->li_tv.v_lock = 0;
15345 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015346 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015347 }
15348 }
15349 ga_clear(&ga);
15350 }
15351#endif
15352}
15353
Bram Moolenaar0d660222005-01-07 21:51:51 +000015354 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015355f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015356 typval_T *argvars;
15357 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015358{
15359 char_u *str;
15360 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015361 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015362 regmatch_T regmatch;
15363 char_u patbuf[NUMBUFLEN];
15364 char_u *save_cpo;
15365 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015366 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015367 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015368 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015369
15370 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15371 save_cpo = p_cpo;
15372 p_cpo = (char_u *)"";
15373
15374 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015375 if (argvars[1].v_type != VAR_UNKNOWN)
15376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015377 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15378 if (pat == NULL)
15379 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015380 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015381 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015382 }
15383 if (pat == NULL || *pat == NUL)
15384 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015385
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015386 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015388 if (typeerr)
15389 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390
Bram Moolenaar0d660222005-01-07 21:51:51 +000015391 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15392 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015394 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015395 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015396 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015397 if (*str == NUL)
15398 match = FALSE; /* empty item at the end */
15399 else
15400 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015401 if (match)
15402 end = regmatch.startp[0];
15403 else
15404 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015405 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15406 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015407 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015408 if (list_append_string(rettv->vval.v_list, str,
15409 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015410 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015411 }
15412 if (!match)
15413 break;
15414 /* Advance to just after the match. */
15415 if (regmatch.endp[0] > str)
15416 col = 0;
15417 else
15418 {
15419 /* Don't get stuck at the same match. */
15420#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015421 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015422#else
15423 col = 1;
15424#endif
15425 }
15426 str = regmatch.endp[0];
15427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428
Bram Moolenaar0d660222005-01-07 21:51:51 +000015429 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431
Bram Moolenaar0d660222005-01-07 21:51:51 +000015432 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433}
15434
Bram Moolenaar2c932302006-03-18 21:42:09 +000015435/*
15436 * "str2nr()" function
15437 */
15438 static void
15439f_str2nr(argvars, rettv)
15440 typval_T *argvars;
15441 typval_T *rettv;
15442{
15443 int base = 10;
15444 char_u *p;
15445 long n;
15446
15447 if (argvars[1].v_type != VAR_UNKNOWN)
15448 {
15449 base = get_tv_number(&argvars[1]);
15450 if (base != 8 && base != 10 && base != 16)
15451 {
15452 EMSG(_(e_invarg));
15453 return;
15454 }
15455 }
15456
15457 p = skipwhite(get_tv_string(&argvars[0]));
15458 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15459 rettv->vval.v_number = n;
15460}
15461
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462#ifdef HAVE_STRFTIME
15463/*
15464 * "strftime({format}[, {time}])" function
15465 */
15466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015467f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015468 typval_T *argvars;
15469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470{
15471 char_u result_buf[256];
15472 struct tm *curtime;
15473 time_t seconds;
15474 char_u *p;
15475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015476 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015479 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 seconds = time(NULL);
15481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015482 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 curtime = localtime(&seconds);
15484 /* MSVC returns NULL for an invalid value of seconds. */
15485 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 else
15488 {
15489# ifdef FEAT_MBYTE
15490 vimconv_T conv;
15491 char_u *enc;
15492
15493 conv.vc_type = CONV_NONE;
15494 enc = enc_locale();
15495 convert_setup(&conv, p_enc, enc);
15496 if (conv.vc_type != CONV_NONE)
15497 p = string_convert(&conv, p, NULL);
15498# endif
15499 if (p != NULL)
15500 (void)strftime((char *)result_buf, sizeof(result_buf),
15501 (char *)p, curtime);
15502 else
15503 result_buf[0] = NUL;
15504
15505# ifdef FEAT_MBYTE
15506 if (conv.vc_type != CONV_NONE)
15507 vim_free(p);
15508 convert_setup(&conv, enc, p_enc);
15509 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015510 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 else
15512# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015513 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514
15515# ifdef FEAT_MBYTE
15516 /* Release conversion descriptors */
15517 convert_setup(&conv, NULL, NULL);
15518 vim_free(enc);
15519# endif
15520 }
15521}
15522#endif
15523
15524/*
15525 * "stridx()" function
15526 */
15527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015529 typval_T *argvars;
15530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531{
15532 char_u buf[NUMBUFLEN];
15533 char_u *needle;
15534 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015535 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015537 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015539 needle = get_tv_string_chk(&argvars[1]);
15540 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015541 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015542 if (needle == NULL || haystack == NULL)
15543 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544
Bram Moolenaar33570922005-01-25 22:26:29 +000015545 if (argvars[2].v_type != VAR_UNKNOWN)
15546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015547 int error = FALSE;
15548
15549 start_idx = get_tv_number_chk(&argvars[2], &error);
15550 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015551 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015552 if (start_idx >= 0)
15553 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015554 }
15555
15556 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15557 if (pos != NULL)
15558 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559}
15560
15561/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015562 * "string()" function
15563 */
15564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 typval_T *argvars;
15567 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015568{
15569 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015570 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015572 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015573 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015574 /* Make a copy if we have a value but it's not in allocate memory. */
15575 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015576 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577}
15578
15579/*
15580 * "strlen()" function
15581 */
15582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015584 typval_T *argvars;
15585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015587 rettv->vval.v_number = (varnumber_T)(STRLEN(
15588 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589}
15590
15591/*
15592 * "strpart()" function
15593 */
15594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015595f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015596 typval_T *argvars;
15597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598{
15599 char_u *p;
15600 int n;
15601 int len;
15602 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015603 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015605 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606 slen = (int)STRLEN(p);
15607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015608 n = get_tv_number_chk(&argvars[1], &error);
15609 if (error)
15610 len = 0;
15611 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015612 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613 else
15614 len = slen - n; /* default len: all bytes that are available. */
15615
15616 /*
15617 * Only return the overlap between the specified part and the actual
15618 * string.
15619 */
15620 if (n < 0)
15621 {
15622 len += n;
15623 n = 0;
15624 }
15625 else if (n > slen)
15626 n = slen;
15627 if (len < 0)
15628 len = 0;
15629 else if (n + len > slen)
15630 len = slen - n;
15631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015632 rettv->v_type = VAR_STRING;
15633 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634}
15635
15636/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637 * "strridx()" function
15638 */
15639 static void
15640f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015641 typval_T *argvars;
15642 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015643{
15644 char_u buf[NUMBUFLEN];
15645 char_u *needle;
15646 char_u *haystack;
15647 char_u *rest;
15648 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015649 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015651 needle = get_tv_string_chk(&argvars[1]);
15652 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015653
15654 rettv->vval.v_number = -1;
15655 if (needle == NULL || haystack == NULL)
15656 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015657
15658 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015659 if (argvars[2].v_type != VAR_UNKNOWN)
15660 {
15661 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015662 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015663 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015664 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015665 }
15666 else
15667 end_idx = haystack_len;
15668
Bram Moolenaar0d660222005-01-07 21:51:51 +000015669 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015670 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015671 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015672 lastmatch = haystack + end_idx;
15673 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015674 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015675 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015676 for (rest = haystack; *rest != '\0'; ++rest)
15677 {
15678 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015679 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680 break;
15681 lastmatch = rest;
15682 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015683 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015684
15685 if (lastmatch == NULL)
15686 rettv->vval.v_number = -1;
15687 else
15688 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15689}
15690
15691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 * "strtrans()" function
15693 */
15694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 typval_T *argvars;
15697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015699 rettv->v_type = VAR_STRING;
15700 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701}
15702
15703/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015704 * "submatch()" function
15705 */
15706 static void
15707f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015708 typval_T *argvars;
15709 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015710{
15711 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015712 rettv->vval.v_string =
15713 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015714}
15715
15716/*
15717 * "substitute()" function
15718 */
15719 static void
15720f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015721 typval_T *argvars;
15722 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015723{
15724 char_u patbuf[NUMBUFLEN];
15725 char_u subbuf[NUMBUFLEN];
15726 char_u flagsbuf[NUMBUFLEN];
15727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 char_u *str = get_tv_string_chk(&argvars[0]);
15729 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15730 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15731 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15732
Bram Moolenaar0d660222005-01-07 21:51:51 +000015733 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015734 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15735 rettv->vval.v_string = NULL;
15736 else
15737 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738}
15739
15740/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015741 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 */
15743/*ARGSUSED*/
15744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015745f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015746 typval_T *argvars;
15747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748{
15749 int id = 0;
15750#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015751 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 long col;
15753 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015754 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015756 lnum = get_tv_lnum(argvars); /* -1 on type error */
15757 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15758 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015760 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015761 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015762 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763#endif
15764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015765 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766}
15767
15768/*
15769 * "synIDattr(id, what [, mode])" function
15770 */
15771/*ARGSUSED*/
15772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015773f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 typval_T *argvars;
15775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776{
15777 char_u *p = NULL;
15778#ifdef FEAT_SYN_HL
15779 int id;
15780 char_u *what;
15781 char_u *mode;
15782 char_u modebuf[NUMBUFLEN];
15783 int modec;
15784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785 id = get_tv_number(&argvars[0]);
15786 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015787 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015789 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 modec = TOLOWER_ASC(mode[0]);
15791 if (modec != 't' && modec != 'c'
15792#ifdef FEAT_GUI
15793 && modec != 'g'
15794#endif
15795 )
15796 modec = 0; /* replace invalid with current */
15797 }
15798 else
15799 {
15800#ifdef FEAT_GUI
15801 if (gui.in_use)
15802 modec = 'g';
15803 else
15804#endif
15805 if (t_colors > 1)
15806 modec = 'c';
15807 else
15808 modec = 't';
15809 }
15810
15811
15812 switch (TOLOWER_ASC(what[0]))
15813 {
15814 case 'b':
15815 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15816 p = highlight_color(id, what, modec);
15817 else /* bold */
15818 p = highlight_has_attr(id, HL_BOLD, modec);
15819 break;
15820
15821 case 'f': /* fg[#] */
15822 p = highlight_color(id, what, modec);
15823 break;
15824
15825 case 'i':
15826 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15827 p = highlight_has_attr(id, HL_INVERSE, modec);
15828 else /* italic */
15829 p = highlight_has_attr(id, HL_ITALIC, modec);
15830 break;
15831
15832 case 'n': /* name */
15833 p = get_highlight_name(NULL, id - 1);
15834 break;
15835
15836 case 'r': /* reverse */
15837 p = highlight_has_attr(id, HL_INVERSE, modec);
15838 break;
15839
15840 case 's': /* standout */
15841 p = highlight_has_attr(id, HL_STANDOUT, modec);
15842 break;
15843
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015844 case 'u':
15845 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15846 /* underline */
15847 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15848 else
15849 /* undercurl */
15850 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 break;
15852 }
15853
15854 if (p != NULL)
15855 p = vim_strsave(p);
15856#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015857 rettv->v_type = VAR_STRING;
15858 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859}
15860
15861/*
15862 * "synIDtrans(id)" function
15863 */
15864/*ARGSUSED*/
15865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015866f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015867 typval_T *argvars;
15868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869{
15870 int id;
15871
15872#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015873 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874
15875 if (id > 0)
15876 id = syn_get_final_id(id);
15877 else
15878#endif
15879 id = 0;
15880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015881 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882}
15883
15884/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015885 * "synstack(lnum, col)" function
15886 */
15887/*ARGSUSED*/
15888 static void
15889f_synstack(argvars, rettv)
15890 typval_T *argvars;
15891 typval_T *rettv;
15892{
15893#ifdef FEAT_SYN_HL
15894 long lnum;
15895 long col;
15896 int i;
15897 int id;
15898#endif
15899
15900 rettv->v_type = VAR_LIST;
15901 rettv->vval.v_list = NULL;
15902
15903#ifdef FEAT_SYN_HL
15904 lnum = get_tv_lnum(argvars); /* -1 on type error */
15905 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15906
15907 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
15908 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
15909 && rettv_list_alloc(rettv) != FAIL)
15910 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000015911 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000015912 for (i = 0; ; ++i)
15913 {
15914 id = syn_get_stack_item(i);
15915 if (id < 0)
15916 break;
15917 if (list_append_number(rettv->vval.v_list, id) == FAIL)
15918 break;
15919 }
15920 }
15921#endif
15922}
15923
15924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 * "system()" function
15926 */
15927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015928f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015929 typval_T *argvars;
15930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015932 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015934 char_u *infile = NULL;
15935 char_u buf[NUMBUFLEN];
15936 int err = FALSE;
15937 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015939 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015940 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015941
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015942 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015943 {
15944 /*
15945 * Write the string to a temp file, to be used for input of the shell
15946 * command.
15947 */
15948 if ((infile = vim_tempname('i')) == NULL)
15949 {
15950 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015951 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015952 }
15953
15954 fd = mch_fopen((char *)infile, WRITEBIN);
15955 if (fd == NULL)
15956 {
15957 EMSG2(_(e_notopen), infile);
15958 goto done;
15959 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015960 p = get_tv_string_buf_chk(&argvars[1], buf);
15961 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015962 {
15963 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015964 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015965 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015966 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15967 err = TRUE;
15968 if (fclose(fd) != 0)
15969 err = TRUE;
15970 if (err)
15971 {
15972 EMSG(_("E677: Error writing temp file"));
15973 goto done;
15974 }
15975 }
15976
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015977 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15978 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015979
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980#ifdef USE_CR
15981 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015982 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983 {
15984 char_u *s;
15985
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015986 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987 {
15988 if (*s == CAR)
15989 *s = NL;
15990 }
15991 }
15992#else
15993# ifdef USE_CRNL
15994 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015995 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996 {
15997 char_u *s, *d;
15998
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015999 d = res;
16000 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016001 {
16002 if (s[0] == CAR && s[1] == NL)
16003 ++s;
16004 *d++ = *s;
16005 }
16006 *d = NUL;
16007 }
16008# endif
16009#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016010
16011done:
16012 if (infile != NULL)
16013 {
16014 mch_remove(infile);
16015 vim_free(infile);
16016 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016017 rettv->v_type = VAR_STRING;
16018 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019}
16020
16021/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016022 * "tabpagebuflist()" function
16023 */
16024/* ARGSUSED */
16025 static void
16026f_tabpagebuflist(argvars, rettv)
16027 typval_T *argvars;
16028 typval_T *rettv;
16029{
16030#ifndef FEAT_WINDOWS
16031 rettv->vval.v_number = 0;
16032#else
16033 tabpage_T *tp;
16034 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016035
16036 if (argvars[0].v_type == VAR_UNKNOWN)
16037 wp = firstwin;
16038 else
16039 {
16040 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16041 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016042 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016043 }
16044 if (wp == NULL)
16045 rettv->vval.v_number = 0;
16046 else
16047 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016048 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016049 rettv->vval.v_number = 0;
16050 else
16051 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016052 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016053 if (list_append_number(rettv->vval.v_list,
16054 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016055 break;
16056 }
16057 }
16058#endif
16059}
16060
16061
16062/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016063 * "tabpagenr()" function
16064 */
16065/* ARGSUSED */
16066 static void
16067f_tabpagenr(argvars, rettv)
16068 typval_T *argvars;
16069 typval_T *rettv;
16070{
16071 int nr = 1;
16072#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016073 char_u *arg;
16074
16075 if (argvars[0].v_type != VAR_UNKNOWN)
16076 {
16077 arg = get_tv_string_chk(&argvars[0]);
16078 nr = 0;
16079 if (arg != NULL)
16080 {
16081 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016082 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016083 else
16084 EMSG2(_(e_invexpr2), arg);
16085 }
16086 }
16087 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016088 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016089#endif
16090 rettv->vval.v_number = nr;
16091}
16092
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016093
16094#ifdef FEAT_WINDOWS
16095static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16096
16097/*
16098 * Common code for tabpagewinnr() and winnr().
16099 */
16100 static int
16101get_winnr(tp, argvar)
16102 tabpage_T *tp;
16103 typval_T *argvar;
16104{
16105 win_T *twin;
16106 int nr = 1;
16107 win_T *wp;
16108 char_u *arg;
16109
16110 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16111 if (argvar->v_type != VAR_UNKNOWN)
16112 {
16113 arg = get_tv_string_chk(argvar);
16114 if (arg == NULL)
16115 nr = 0; /* type error; errmsg already given */
16116 else if (STRCMP(arg, "$") == 0)
16117 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16118 else if (STRCMP(arg, "#") == 0)
16119 {
16120 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16121 if (twin == NULL)
16122 nr = 0;
16123 }
16124 else
16125 {
16126 EMSG2(_(e_invexpr2), arg);
16127 nr = 0;
16128 }
16129 }
16130
16131 if (nr > 0)
16132 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16133 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016134 {
16135 if (wp == NULL)
16136 {
16137 /* didn't find it in this tabpage */
16138 nr = 0;
16139 break;
16140 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016141 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016142 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016143 return nr;
16144}
16145#endif
16146
16147/*
16148 * "tabpagewinnr()" function
16149 */
16150/* ARGSUSED */
16151 static void
16152f_tabpagewinnr(argvars, rettv)
16153 typval_T *argvars;
16154 typval_T *rettv;
16155{
16156 int nr = 1;
16157#ifdef FEAT_WINDOWS
16158 tabpage_T *tp;
16159
16160 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16161 if (tp == NULL)
16162 nr = 0;
16163 else
16164 nr = get_winnr(tp, &argvars[1]);
16165#endif
16166 rettv->vval.v_number = nr;
16167}
16168
16169
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016170/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016171 * "tagfiles()" function
16172 */
16173/*ARGSUSED*/
16174 static void
16175f_tagfiles(argvars, rettv)
16176 typval_T *argvars;
16177 typval_T *rettv;
16178{
16179 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016180 tagname_T tn;
16181 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016182
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016183 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016184 {
16185 rettv->vval.v_number = 0;
16186 return;
16187 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016188
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016189 for (first = TRUE; ; first = FALSE)
16190 if (get_tagfname(&tn, first, fname) == FAIL
16191 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016192 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016193 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016194}
16195
16196/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016197 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016198 */
16199 static void
16200f_taglist(argvars, rettv)
16201 typval_T *argvars;
16202 typval_T *rettv;
16203{
16204 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016205
16206 tag_pattern = get_tv_string(&argvars[0]);
16207
16208 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016209 if (*tag_pattern == NUL)
16210 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016211
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016212 if (rettv_list_alloc(rettv) == OK)
16213 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016214}
16215
16216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 * "tempname()" function
16218 */
16219/*ARGSUSED*/
16220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016221f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 typval_T *argvars;
16223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224{
16225 static int x = 'A';
16226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016227 rettv->v_type = VAR_STRING;
16228 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229
16230 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16231 * names. Skip 'I' and 'O', they are used for shell redirection. */
16232 do
16233 {
16234 if (x == 'Z')
16235 x = '0';
16236 else if (x == '9')
16237 x = 'A';
16238 else
16239 {
16240#ifdef EBCDIC
16241 if (x == 'I')
16242 x = 'J';
16243 else if (x == 'R')
16244 x = 'S';
16245 else
16246#endif
16247 ++x;
16248 }
16249 } while (x == 'I' || x == 'O');
16250}
16251
16252/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016253 * "test(list)" function: Just checking the walls...
16254 */
16255/*ARGSUSED*/
16256 static void
16257f_test(argvars, rettv)
16258 typval_T *argvars;
16259 typval_T *rettv;
16260{
16261 /* Used for unit testing. Change the code below to your liking. */
16262#if 0
16263 listitem_T *li;
16264 list_T *l;
16265 char_u *bad, *good;
16266
16267 if (argvars[0].v_type != VAR_LIST)
16268 return;
16269 l = argvars[0].vval.v_list;
16270 if (l == NULL)
16271 return;
16272 li = l->lv_first;
16273 if (li == NULL)
16274 return;
16275 bad = get_tv_string(&li->li_tv);
16276 li = li->li_next;
16277 if (li == NULL)
16278 return;
16279 good = get_tv_string(&li->li_tv);
16280 rettv->vval.v_number = test_edit_score(bad, good);
16281#endif
16282}
16283
16284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 * "tolower(string)" function
16286 */
16287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016289 typval_T *argvars;
16290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291{
16292 char_u *p;
16293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016294 p = vim_strsave(get_tv_string(&argvars[0]));
16295 rettv->v_type = VAR_STRING;
16296 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297
16298 if (p != NULL)
16299 while (*p != NUL)
16300 {
16301#ifdef FEAT_MBYTE
16302 int l;
16303
16304 if (enc_utf8)
16305 {
16306 int c, lc;
16307
16308 c = utf_ptr2char(p);
16309 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016310 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 /* TODO: reallocate string when byte count changes. */
16312 if (utf_char2len(lc) == l)
16313 utf_char2bytes(lc, p);
16314 p += l;
16315 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016316 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 p += l; /* skip multi-byte character */
16318 else
16319#endif
16320 {
16321 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16322 ++p;
16323 }
16324 }
16325}
16326
16327/*
16328 * "toupper(string)" function
16329 */
16330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016331f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016332 typval_T *argvars;
16333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016335 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016336 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337}
16338
16339/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016340 * "tr(string, fromstr, tostr)" function
16341 */
16342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016343f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016344 typval_T *argvars;
16345 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016346{
16347 char_u *instr;
16348 char_u *fromstr;
16349 char_u *tostr;
16350 char_u *p;
16351#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016352 int inlen;
16353 int fromlen;
16354 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016355 int idx;
16356 char_u *cpstr;
16357 int cplen;
16358 int first = TRUE;
16359#endif
16360 char_u buf[NUMBUFLEN];
16361 char_u buf2[NUMBUFLEN];
16362 garray_T ga;
16363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016364 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016365 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16366 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016367
16368 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016369 rettv->v_type = VAR_STRING;
16370 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016371 if (fromstr == NULL || tostr == NULL)
16372 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016373 ga_init2(&ga, (int)sizeof(char), 80);
16374
16375#ifdef FEAT_MBYTE
16376 if (!has_mbyte)
16377#endif
16378 /* not multi-byte: fromstr and tostr must be the same length */
16379 if (STRLEN(fromstr) != STRLEN(tostr))
16380 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016381#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016382error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016383#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016384 EMSG2(_(e_invarg2), fromstr);
16385 ga_clear(&ga);
16386 return;
16387 }
16388
16389 /* fromstr and tostr have to contain the same number of chars */
16390 while (*instr != NUL)
16391 {
16392#ifdef FEAT_MBYTE
16393 if (has_mbyte)
16394 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016395 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016396 cpstr = instr;
16397 cplen = inlen;
16398 idx = 0;
16399 for (p = fromstr; *p != NUL; p += fromlen)
16400 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016401 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016402 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16403 {
16404 for (p = tostr; *p != NUL; p += tolen)
16405 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016406 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016407 if (idx-- == 0)
16408 {
16409 cplen = tolen;
16410 cpstr = p;
16411 break;
16412 }
16413 }
16414 if (*p == NUL) /* tostr is shorter than fromstr */
16415 goto error;
16416 break;
16417 }
16418 ++idx;
16419 }
16420
16421 if (first && cpstr == instr)
16422 {
16423 /* Check that fromstr and tostr have the same number of
16424 * (multi-byte) characters. Done only once when a character
16425 * of instr doesn't appear in fromstr. */
16426 first = FALSE;
16427 for (p = tostr; *p != NUL; p += tolen)
16428 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016429 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016430 --idx;
16431 }
16432 if (idx != 0)
16433 goto error;
16434 }
16435
16436 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016437 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016438 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016439
16440 instr += inlen;
16441 }
16442 else
16443#endif
16444 {
16445 /* When not using multi-byte chars we can do it faster. */
16446 p = vim_strchr(fromstr, *instr);
16447 if (p != NULL)
16448 ga_append(&ga, tostr[p - fromstr]);
16449 else
16450 ga_append(&ga, *instr);
16451 ++instr;
16452 }
16453 }
16454
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016455 /* add a terminating NUL */
16456 ga_grow(&ga, 1);
16457 ga_append(&ga, NUL);
16458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016460}
16461
16462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 * "type(expr)" function
16464 */
16465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016466f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016467 typval_T *argvars;
16468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016470 int n;
16471
16472 switch (argvars[0].v_type)
16473 {
16474 case VAR_NUMBER: n = 0; break;
16475 case VAR_STRING: n = 1; break;
16476 case VAR_FUNC: n = 2; break;
16477 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016478 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016479 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16480 }
16481 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482}
16483
16484/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016485 * "values(dict)" function
16486 */
16487 static void
16488f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016489 typval_T *argvars;
16490 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016491{
16492 dict_list(argvars, rettv, 1);
16493}
16494
16495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 * "virtcol(string)" function
16497 */
16498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016499f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016500 typval_T *argvars;
16501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502{
16503 colnr_T vcol = 0;
16504 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016505 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016507 fp = var2fpos(&argvars[0], FALSE, &fnum);
16508 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16509 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 {
16511 getvvcol(curwin, fp, NULL, NULL, &vcol);
16512 ++vcol;
16513 }
16514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016515 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516}
16517
16518/*
16519 * "visualmode()" function
16520 */
16521/*ARGSUSED*/
16522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016523f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 typval_T *argvars;
16525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526{
16527#ifdef FEAT_VISUAL
16528 char_u str[2];
16529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016530 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 str[0] = curbuf->b_visual_mode_eval;
16532 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016533 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534
16535 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016536 if ((argvars[0].v_type == VAR_NUMBER
16537 && argvars[0].vval.v_number != 0)
16538 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016539 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 curbuf->b_visual_mode_eval = NUL;
16541#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543#endif
16544}
16545
16546/*
16547 * "winbufnr(nr)" function
16548 */
16549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016550f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016551 typval_T *argvars;
16552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553{
16554 win_T *wp;
16555
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016556 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016560 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561}
16562
16563/*
16564 * "wincol()" function
16565 */
16566/*ARGSUSED*/
16567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016568f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016569 typval_T *argvars;
16570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571{
16572 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016573 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574}
16575
16576/*
16577 * "winheight(nr)" function
16578 */
16579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016580f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016581 typval_T *argvars;
16582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583{
16584 win_T *wp;
16585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016586 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016588 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016590 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591}
16592
16593/*
16594 * "winline()" function
16595 */
16596/*ARGSUSED*/
16597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016598f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016599 typval_T *argvars;
16600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601{
16602 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016603 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604}
16605
16606/*
16607 * "winnr()" function
16608 */
16609/* ARGSUSED */
16610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016611f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016612 typval_T *argvars;
16613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614{
16615 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016616
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016618 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016620 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621}
16622
16623/*
16624 * "winrestcmd()" function
16625 */
16626/* ARGSUSED */
16627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016628f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016629 typval_T *argvars;
16630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631{
16632#ifdef FEAT_WINDOWS
16633 win_T *wp;
16634 int winnr = 1;
16635 garray_T ga;
16636 char_u buf[50];
16637
16638 ga_init2(&ga, (int)sizeof(char), 70);
16639 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16640 {
16641 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16642 ga_concat(&ga, buf);
16643# ifdef FEAT_VERTSPLIT
16644 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16645 ga_concat(&ga, buf);
16646# endif
16647 ++winnr;
16648 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016649 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016653 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016655 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656}
16657
16658/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016659 * "winrestview()" function
16660 */
16661/* ARGSUSED */
16662 static void
16663f_winrestview(argvars, rettv)
16664 typval_T *argvars;
16665 typval_T *rettv;
16666{
16667 dict_T *dict;
16668
16669 if (argvars[0].v_type != VAR_DICT
16670 || (dict = argvars[0].vval.v_dict) == NULL)
16671 EMSG(_(e_invarg));
16672 else
16673 {
16674 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16675 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16676#ifdef FEAT_VIRTUALEDIT
16677 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16678#endif
16679 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016680 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016681
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016682 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016683#ifdef FEAT_DIFF
16684 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16685#endif
16686 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16687 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16688
16689 check_cursor();
16690 changed_cline_bef_curs();
16691 invalidate_botline();
16692 redraw_later(VALID);
16693
16694 if (curwin->w_topline == 0)
16695 curwin->w_topline = 1;
16696 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16697 curwin->w_topline = curbuf->b_ml.ml_line_count;
16698#ifdef FEAT_DIFF
16699 check_topfill(curwin, TRUE);
16700#endif
16701 }
16702}
16703
16704/*
16705 * "winsaveview()" function
16706 */
16707/* ARGSUSED */
16708 static void
16709f_winsaveview(argvars, rettv)
16710 typval_T *argvars;
16711 typval_T *rettv;
16712{
16713 dict_T *dict;
16714
16715 dict = dict_alloc();
16716 if (dict == NULL)
16717 return;
16718 rettv->v_type = VAR_DICT;
16719 rettv->vval.v_dict = dict;
16720 ++dict->dv_refcount;
16721
16722 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16723 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16724#ifdef FEAT_VIRTUALEDIT
16725 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16726#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016727 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016728 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16729
16730 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16731#ifdef FEAT_DIFF
16732 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16733#endif
16734 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16735 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16736}
16737
16738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 * "winwidth(nr)" function
16740 */
16741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016742f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016743 typval_T *argvars;
16744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745{
16746 win_T *wp;
16747
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016748 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016750 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 else
16752#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016753 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016755 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756#endif
16757}
16758
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016760 * "writefile()" function
16761 */
16762 static void
16763f_writefile(argvars, rettv)
16764 typval_T *argvars;
16765 typval_T *rettv;
16766{
16767 int binary = FALSE;
16768 char_u *fname;
16769 FILE *fd;
16770 listitem_T *li;
16771 char_u *s;
16772 int ret = 0;
16773 int c;
16774
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016775 if (check_restricted() || check_secure())
16776 return;
16777
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016778 if (argvars[0].v_type != VAR_LIST)
16779 {
16780 EMSG2(_(e_listarg), "writefile()");
16781 return;
16782 }
16783 if (argvars[0].vval.v_list == NULL)
16784 return;
16785
16786 if (argvars[2].v_type != VAR_UNKNOWN
16787 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16788 binary = TRUE;
16789
16790 /* Always open the file in binary mode, library functions have a mind of
16791 * their own about CR-LF conversion. */
16792 fname = get_tv_string(&argvars[1]);
16793 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16794 {
16795 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16796 ret = -1;
16797 }
16798 else
16799 {
16800 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16801 li = li->li_next)
16802 {
16803 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16804 {
16805 if (*s == '\n')
16806 c = putc(NUL, fd);
16807 else
16808 c = putc(*s, fd);
16809 if (c == EOF)
16810 {
16811 ret = -1;
16812 break;
16813 }
16814 }
16815 if (!binary || li->li_next != NULL)
16816 if (putc('\n', fd) == EOF)
16817 {
16818 ret = -1;
16819 break;
16820 }
16821 if (ret < 0)
16822 {
16823 EMSG(_(e_write));
16824 break;
16825 }
16826 }
16827 fclose(fd);
16828 }
16829
16830 rettv->vval.v_number = ret;
16831}
16832
16833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016835 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 */
16837 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016838var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016839 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016840 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016841 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016843 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016845 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846
Bram Moolenaara5525202006-03-02 22:52:09 +000016847 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016848 if (varp->v_type == VAR_LIST)
16849 {
16850 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016851 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016852 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016853 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016854
16855 l = varp->vval.v_list;
16856 if (l == NULL)
16857 return NULL;
16858
16859 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016860 pos.lnum = list_find_nr(l, 0L, &error);
16861 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016862 return NULL; /* invalid line number */
16863
16864 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016865 pos.col = list_find_nr(l, 1L, &error);
16866 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016867 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016868 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016869
16870 /* We accept "$" for the column number: last column. */
16871 li = list_find(l, 1L);
16872 if (li != NULL && li->li_tv.v_type == VAR_STRING
16873 && li->li_tv.vval.v_string != NULL
16874 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16875 pos.col = len + 1;
16876
Bram Moolenaara5525202006-03-02 22:52:09 +000016877 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016878 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016879 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016880 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016881
Bram Moolenaara5525202006-03-02 22:52:09 +000016882#ifdef FEAT_VIRTUALEDIT
16883 /* Get the virtual offset. Defaults to zero. */
16884 pos.coladd = list_find_nr(l, 2L, &error);
16885 if (error)
16886 pos.coladd = 0;
16887#endif
16888
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016889 return &pos;
16890 }
16891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016892 name = get_tv_string_chk(varp);
16893 if (name == NULL)
16894 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 if (name[0] == '.') /* cursor */
16896 return &curwin->w_cursor;
16897 if (name[0] == '\'') /* mark */
16898 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016899 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16901 return NULL;
16902 return pp;
16903 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016904
16905#ifdef FEAT_VIRTUALEDIT
16906 pos.coladd = 0;
16907#endif
16908
Bram Moolenaar477933c2007-07-17 14:32:23 +000016909 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016910 {
16911 pos.col = 0;
16912 if (name[1] == '0') /* "w0": first visible line */
16913 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016914 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016915 pos.lnum = curwin->w_topline;
16916 return &pos;
16917 }
16918 else if (name[1] == '$') /* "w$": last visible line */
16919 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016920 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016921 pos.lnum = curwin->w_botline - 1;
16922 return &pos;
16923 }
16924 }
16925 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016927 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 {
16929 pos.lnum = curbuf->b_ml.ml_line_count;
16930 pos.col = 0;
16931 }
16932 else
16933 {
16934 pos.lnum = curwin->w_cursor.lnum;
16935 pos.col = (colnr_T)STRLEN(ml_get_curline());
16936 }
16937 return &pos;
16938 }
16939 return NULL;
16940}
16941
16942/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016943 * Convert list in "arg" into a position and optional file number.
16944 * When "fnump" is NULL there is no file number, only 3 items.
16945 * Note that the column is passed on as-is, the caller may want to decrement
16946 * it to use 1 for the first column.
16947 * Return FAIL when conversion is not possible, doesn't check the position for
16948 * validity.
16949 */
16950 static int
16951list2fpos(arg, posp, fnump)
16952 typval_T *arg;
16953 pos_T *posp;
16954 int *fnump;
16955{
16956 list_T *l = arg->vval.v_list;
16957 long i = 0;
16958 long n;
16959
Bram Moolenaarbde35262006-07-23 20:12:24 +000016960 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16961 * when "fnump" isn't NULL and "coladd" is optional. */
16962 if (arg->v_type != VAR_LIST
16963 || l == NULL
16964 || l->lv_len < (fnump == NULL ? 2 : 3)
16965 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016966 return FAIL;
16967
16968 if (fnump != NULL)
16969 {
16970 n = list_find_nr(l, i++, NULL); /* fnum */
16971 if (n < 0)
16972 return FAIL;
16973 if (n == 0)
16974 n = curbuf->b_fnum; /* current buffer */
16975 *fnump = n;
16976 }
16977
16978 n = list_find_nr(l, i++, NULL); /* lnum */
16979 if (n < 0)
16980 return FAIL;
16981 posp->lnum = n;
16982
16983 n = list_find_nr(l, i++, NULL); /* col */
16984 if (n < 0)
16985 return FAIL;
16986 posp->col = n;
16987
16988#ifdef FEAT_VIRTUALEDIT
16989 n = list_find_nr(l, i, NULL);
16990 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016991 posp->coladd = 0;
16992 else
16993 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016994#endif
16995
16996 return OK;
16997}
16998
16999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 * Get the length of an environment variable name.
17001 * Advance "arg" to the first character after the name.
17002 * Return 0 for error.
17003 */
17004 static int
17005get_env_len(arg)
17006 char_u **arg;
17007{
17008 char_u *p;
17009 int len;
17010
17011 for (p = *arg; vim_isIDc(*p); ++p)
17012 ;
17013 if (p == *arg) /* no name found */
17014 return 0;
17015
17016 len = (int)(p - *arg);
17017 *arg = p;
17018 return len;
17019}
17020
17021/*
17022 * Get the length of the name of a function or internal variable.
17023 * "arg" is advanced to the first non-white character after the name.
17024 * Return 0 if something is wrong.
17025 */
17026 static int
17027get_id_len(arg)
17028 char_u **arg;
17029{
17030 char_u *p;
17031 int len;
17032
17033 /* Find the end of the name. */
17034 for (p = *arg; eval_isnamec(*p); ++p)
17035 ;
17036 if (p == *arg) /* no name found */
17037 return 0;
17038
17039 len = (int)(p - *arg);
17040 *arg = skipwhite(p);
17041
17042 return len;
17043}
17044
17045/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017046 * Get the length of the name of a variable or function.
17047 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017049 * Return -1 if curly braces expansion failed.
17050 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 * If the name contains 'magic' {}'s, expand them and return the
17052 * expanded name in an allocated string via 'alias' - caller must free.
17053 */
17054 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017055get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 char_u **arg;
17057 char_u **alias;
17058 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017059 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060{
17061 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 char_u *p;
17063 char_u *expr_start;
17064 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065
17066 *alias = NULL; /* default to no alias */
17067
17068 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17069 && (*arg)[2] == (int)KE_SNR)
17070 {
17071 /* hard coded <SNR>, already translated */
17072 *arg += 3;
17073 return get_id_len(arg) + 3;
17074 }
17075 len = eval_fname_script(*arg);
17076 if (len > 0)
17077 {
17078 /* literal "<SID>", "s:" or "<SNR>" */
17079 *arg += len;
17080 }
17081
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017083 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017085 p = find_name_end(*arg, &expr_start, &expr_end,
17086 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 if (expr_start != NULL)
17088 {
17089 char_u *temp_string;
17090
17091 if (!evaluate)
17092 {
17093 len += (int)(p - *arg);
17094 *arg = skipwhite(p);
17095 return len;
17096 }
17097
17098 /*
17099 * Include any <SID> etc in the expanded string:
17100 * Thus the -len here.
17101 */
17102 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17103 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017104 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 *alias = temp_string;
17106 *arg = skipwhite(p);
17107 return (int)STRLEN(temp_string);
17108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109
17110 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017111 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 EMSG2(_(e_invexpr2), *arg);
17113
17114 return len;
17115}
17116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017117/*
17118 * Find the end of a variable or function name, taking care of magic braces.
17119 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17120 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017121 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017122 * Return a pointer to just after the name. Equal to "arg" if there is no
17123 * valid name.
17124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017126find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 char_u *arg;
17128 char_u **expr_start;
17129 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017130 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017132 int mb_nest = 0;
17133 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 char_u *p;
17135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017136 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017138 *expr_start = NULL;
17139 *expr_end = NULL;
17140 }
17141
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017142 /* Quick check for valid starting character. */
17143 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17144 return arg;
17145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017146 for (p = arg; *p != NUL
17147 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017148 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017149 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017150 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017151 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017152 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017153 if (*p == '\'')
17154 {
17155 /* skip over 'string' to avoid counting [ and ] inside it. */
17156 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17157 ;
17158 if (*p == NUL)
17159 break;
17160 }
17161 else if (*p == '"')
17162 {
17163 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17164 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17165 if (*p == '\\' && p[1] != NUL)
17166 ++p;
17167 if (*p == NUL)
17168 break;
17169 }
17170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017171 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173 if (*p == '[')
17174 ++br_nest;
17175 else if (*p == ']')
17176 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017179 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017181 if (*p == '{')
17182 {
17183 mb_nest++;
17184 if (expr_start != NULL && *expr_start == NULL)
17185 *expr_start = p;
17186 }
17187 else if (*p == '}')
17188 {
17189 mb_nest--;
17190 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17191 *expr_end = p;
17192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 }
17195
17196 return p;
17197}
17198
17199/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017200 * Expands out the 'magic' {}'s in a variable/function name.
17201 * Note that this can call itself recursively, to deal with
17202 * constructs like foo{bar}{baz}{bam}
17203 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17204 * "in_start" ^
17205 * "expr_start" ^
17206 * "expr_end" ^
17207 * "in_end" ^
17208 *
17209 * Returns a new allocated string, which the caller must free.
17210 * Returns NULL for failure.
17211 */
17212 static char_u *
17213make_expanded_name(in_start, expr_start, expr_end, in_end)
17214 char_u *in_start;
17215 char_u *expr_start;
17216 char_u *expr_end;
17217 char_u *in_end;
17218{
17219 char_u c1;
17220 char_u *retval = NULL;
17221 char_u *temp_result;
17222 char_u *nextcmd = NULL;
17223
17224 if (expr_end == NULL || in_end == NULL)
17225 return NULL;
17226 *expr_start = NUL;
17227 *expr_end = NUL;
17228 c1 = *in_end;
17229 *in_end = NUL;
17230
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017231 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017232 if (temp_result != NULL && nextcmd == NULL)
17233 {
17234 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17235 + (in_end - expr_end) + 1));
17236 if (retval != NULL)
17237 {
17238 STRCPY(retval, in_start);
17239 STRCAT(retval, temp_result);
17240 STRCAT(retval, expr_end + 1);
17241 }
17242 }
17243 vim_free(temp_result);
17244
17245 *in_end = c1; /* put char back for error messages */
17246 *expr_start = '{';
17247 *expr_end = '}';
17248
17249 if (retval != NULL)
17250 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017251 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017252 if (expr_start != NULL)
17253 {
17254 /* Further expansion! */
17255 temp_result = make_expanded_name(retval, expr_start,
17256 expr_end, temp_result);
17257 vim_free(retval);
17258 retval = temp_result;
17259 }
17260 }
17261
17262 return retval;
17263}
17264
17265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017267 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 */
17269 static int
17270eval_isnamec(c)
17271 int c;
17272{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017273 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17274}
17275
17276/*
17277 * Return TRUE if character "c" can be used as the first character in a
17278 * variable or function name (excluding '{' and '}').
17279 */
17280 static int
17281eval_isnamec1(c)
17282 int c;
17283{
17284 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285}
17286
17287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 * Set number v: variable to "val".
17289 */
17290 void
17291set_vim_var_nr(idx, val)
17292 int idx;
17293 long val;
17294{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017295 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296}
17297
17298/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017299 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 */
17301 long
17302get_vim_var_nr(idx)
17303 int idx;
17304{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017305 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306}
17307
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017308#if defined(FEAT_AUTOCMD) || defined(PROTO)
17309/*
17310 * Get string v: variable value. Uses a static buffer, can only be used once.
17311 */
17312 char_u *
17313get_vim_var_str(idx)
17314 int idx;
17315{
17316 return get_tv_string(&vimvars[idx].vv_tv);
17317}
17318#endif
17319
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320/*
17321 * Set v:count, v:count1 and v:prevcount.
17322 */
17323 void
17324set_vcount(count, count1)
17325 long count;
17326 long count1;
17327{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017328 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17329 vimvars[VV_COUNT].vv_nr = count;
17330 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331}
17332
17333/*
17334 * Set string v: variable to a copy of "val".
17335 */
17336 void
17337set_vim_var_string(idx, val, len)
17338 int idx;
17339 char_u *val;
17340 int len; /* length of "val" to use or -1 (whole string) */
17341{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017342 /* Need to do this (at least) once, since we can't initialize a union.
17343 * Will always be invoked when "v:progname" is set. */
17344 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17345
Bram Moolenaare9a41262005-01-15 22:18:47 +000017346 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017348 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017350 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017352 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353}
17354
17355/*
17356 * Set v:register if needed.
17357 */
17358 void
17359set_reg_var(c)
17360 int c;
17361{
17362 char_u regname;
17363
17364 if (c == 0 || c == ' ')
17365 regname = '"';
17366 else
17367 regname = c;
17368 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017369 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 set_vim_var_string(VV_REG, &regname, 1);
17371}
17372
17373/*
17374 * Get or set v:exception. If "oldval" == NULL, return the current value.
17375 * Otherwise, restore the value to "oldval" and return NULL.
17376 * Must always be called in pairs to save and restore v:exception! Does not
17377 * take care of memory allocations.
17378 */
17379 char_u *
17380v_exception(oldval)
17381 char_u *oldval;
17382{
17383 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017384 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385
Bram Moolenaare9a41262005-01-15 22:18:47 +000017386 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 return NULL;
17388}
17389
17390/*
17391 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17392 * Otherwise, restore the value to "oldval" and return NULL.
17393 * Must always be called in pairs to save and restore v:throwpoint! Does not
17394 * take care of memory allocations.
17395 */
17396 char_u *
17397v_throwpoint(oldval)
17398 char_u *oldval;
17399{
17400 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017401 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402
Bram Moolenaare9a41262005-01-15 22:18:47 +000017403 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 return NULL;
17405}
17406
17407#if defined(FEAT_AUTOCMD) || defined(PROTO)
17408/*
17409 * Set v:cmdarg.
17410 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17411 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17412 * Must always be called in pairs!
17413 */
17414 char_u *
17415set_cmdarg(eap, oldarg)
17416 exarg_T *eap;
17417 char_u *oldarg;
17418{
17419 char_u *oldval;
17420 char_u *newval;
17421 unsigned len;
17422
Bram Moolenaare9a41262005-01-15 22:18:47 +000017423 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017424 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017426 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017427 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017428 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 }
17430
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017431 if (eap->force_bin == FORCE_BIN)
17432 len = 6;
17433 else if (eap->force_bin == FORCE_NOBIN)
17434 len = 8;
17435 else
17436 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017437
17438 if (eap->read_edit)
17439 len += 7;
17440
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017441 if (eap->force_ff != 0)
17442 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17443# ifdef FEAT_MBYTE
17444 if (eap->force_enc != 0)
17445 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017446 if (eap->bad_char != 0)
17447 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017448# endif
17449
17450 newval = alloc(len + 1);
17451 if (newval == NULL)
17452 return NULL;
17453
17454 if (eap->force_bin == FORCE_BIN)
17455 sprintf((char *)newval, " ++bin");
17456 else if (eap->force_bin == FORCE_NOBIN)
17457 sprintf((char *)newval, " ++nobin");
17458 else
17459 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017460
17461 if (eap->read_edit)
17462 STRCAT(newval, " ++edit");
17463
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017464 if (eap->force_ff != 0)
17465 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17466 eap->cmd + eap->force_ff);
17467# ifdef FEAT_MBYTE
17468 if (eap->force_enc != 0)
17469 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17470 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017471 if (eap->bad_char != 0)
17472 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17473 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017474# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017475 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017476 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477}
17478#endif
17479
17480/*
17481 * Get the value of internal variable "name".
17482 * Return OK or FAIL.
17483 */
17484 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017485get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 char_u *name;
17487 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017488 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017489 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490{
17491 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 typval_T *tv = NULL;
17493 typval_T atv;
17494 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496
17497 /* truncate the name, so that we can use strcmp() */
17498 cc = name[len];
17499 name[len] = NUL;
17500
17501 /*
17502 * Check for "b:changedtick".
17503 */
17504 if (STRCMP(name, "b:changedtick") == 0)
17505 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017506 atv.v_type = VAR_NUMBER;
17507 atv.vval.v_number = curbuf->b_changedtick;
17508 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 }
17510
17511 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 * Check for user-defined variables.
17513 */
17514 else
17515 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017516 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017518 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 }
17520
Bram Moolenaare9a41262005-01-15 22:18:47 +000017521 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017523 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017524 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 ret = FAIL;
17526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017527 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017528 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529
17530 name[len] = cc;
17531
17532 return ret;
17533}
17534
17535/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017536 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17537 * Also handle function call with Funcref variable: func(expr)
17538 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17539 */
17540 static int
17541handle_subscript(arg, rettv, evaluate, verbose)
17542 char_u **arg;
17543 typval_T *rettv;
17544 int evaluate; /* do more than finding the end */
17545 int verbose; /* give error messages */
17546{
17547 int ret = OK;
17548 dict_T *selfdict = NULL;
17549 char_u *s;
17550 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017551 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017552
17553 while (ret == OK
17554 && (**arg == '['
17555 || (**arg == '.' && rettv->v_type == VAR_DICT)
17556 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17557 && !vim_iswhite(*(*arg - 1)))
17558 {
17559 if (**arg == '(')
17560 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017561 /* need to copy the funcref so that we can clear rettv */
17562 functv = *rettv;
17563 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017564
17565 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017566 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017567 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017568 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17569 &len, evaluate, selfdict);
17570
17571 /* Clear the funcref afterwards, so that deleting it while
17572 * evaluating the arguments is possible (see test55). */
17573 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017574
17575 /* Stop the expression evaluation when immediately aborting on
17576 * error, or when an interrupt occurred or an exception was thrown
17577 * but not caught. */
17578 if (aborting())
17579 {
17580 if (ret == OK)
17581 clear_tv(rettv);
17582 ret = FAIL;
17583 }
17584 dict_unref(selfdict);
17585 selfdict = NULL;
17586 }
17587 else /* **arg == '[' || **arg == '.' */
17588 {
17589 dict_unref(selfdict);
17590 if (rettv->v_type == VAR_DICT)
17591 {
17592 selfdict = rettv->vval.v_dict;
17593 if (selfdict != NULL)
17594 ++selfdict->dv_refcount;
17595 }
17596 else
17597 selfdict = NULL;
17598 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17599 {
17600 clear_tv(rettv);
17601 ret = FAIL;
17602 }
17603 }
17604 }
17605 dict_unref(selfdict);
17606 return ret;
17607}
17608
17609/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017610 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17611 * value).
17612 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017613 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017614alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017615{
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017617}
17618
17619/*
17620 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 * The string "s" must have been allocated, it is consumed.
17622 * Return NULL for out of memory, the variable otherwise.
17623 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017625alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 char_u *s;
17627{
Bram Moolenaar33570922005-01-25 22:26:29 +000017628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630 rettv = alloc_tv();
17631 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017633 rettv->v_type = VAR_STRING;
17634 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 }
17636 else
17637 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017638 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639}
17640
17641/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017642 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017644 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017645free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017646 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647{
17648 if (varp != NULL)
17649 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017650 switch (varp->v_type)
17651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017652 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017653 func_unref(varp->vval.v_string);
17654 /*FALLTHROUGH*/
17655 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017656 vim_free(varp->vval.v_string);
17657 break;
17658 case VAR_LIST:
17659 list_unref(varp->vval.v_list);
17660 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017661 case VAR_DICT:
17662 dict_unref(varp->vval.v_dict);
17663 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017664 case VAR_NUMBER:
17665 case VAR_UNKNOWN:
17666 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017667 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017668 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017669 break;
17670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 vim_free(varp);
17672 }
17673}
17674
17675/*
17676 * Free the memory for a variable value and set the value to NULL or 0.
17677 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017678 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017680 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681{
17682 if (varp != NULL)
17683 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017684 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017686 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017687 func_unref(varp->vval.v_string);
17688 /*FALLTHROUGH*/
17689 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017690 vim_free(varp->vval.v_string);
17691 varp->vval.v_string = NULL;
17692 break;
17693 case VAR_LIST:
17694 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017695 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017696 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017697 case VAR_DICT:
17698 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017699 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017700 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017701 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017702 varp->vval.v_number = 0;
17703 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017704 case VAR_UNKNOWN:
17705 break;
17706 default:
17707 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017709 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 }
17711}
17712
17713/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017714 * Set the value of a variable to NULL without freeing items.
17715 */
17716 static void
17717init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017718 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719{
17720 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017721 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017722}
17723
17724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 * Get the number value of a variable.
17726 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017727 * For incompatible types, return 0.
17728 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17729 * caller of incompatible types: it sets *denote to TRUE if "denote"
17730 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 */
17732 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017733get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017734 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017736 int error = FALSE;
17737
17738 return get_tv_number_chk(varp, &error); /* return 0L on error */
17739}
17740
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017741 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017742get_tv_number_chk(varp, denote)
17743 typval_T *varp;
17744 int *denote;
17745{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017746 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017748 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017750 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017751 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017752 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017753 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017754 break;
17755 case VAR_STRING:
17756 if (varp->vval.v_string != NULL)
17757 vim_str2nr(varp->vval.v_string, NULL, NULL,
17758 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017759 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017760 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017761 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017762 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017763 case VAR_DICT:
17764 EMSG(_("E728: Using a Dictionary as a number"));
17765 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017766 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017767 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017768 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017770 if (denote == NULL) /* useful for values that must be unsigned */
17771 n = -1;
17772 else
17773 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017774 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775}
17776
17777/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017778 * Get the lnum from the first argument.
17779 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017780 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 */
17782 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017783get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017784 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785{
Bram Moolenaar33570922005-01-25 22:26:29 +000017786 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 linenr_T lnum;
17788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017789 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790 if (lnum == 0) /* no valid number, try using line() */
17791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017792 rettv.v_type = VAR_NUMBER;
17793 f_line(argvars, &rettv);
17794 lnum = rettv.vval.v_number;
17795 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 }
17797 return lnum;
17798}
17799
17800/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017801 * Get the lnum from the first argument.
17802 * Also accepts "$", then "buf" is used.
17803 * Returns 0 on error.
17804 */
17805 static linenr_T
17806get_tv_lnum_buf(argvars, buf)
17807 typval_T *argvars;
17808 buf_T *buf;
17809{
17810 if (argvars[0].v_type == VAR_STRING
17811 && argvars[0].vval.v_string != NULL
17812 && argvars[0].vval.v_string[0] == '$'
17813 && buf != NULL)
17814 return buf->b_ml.ml_line_count;
17815 return get_tv_number_chk(&argvars[0], NULL);
17816}
17817
17818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819 * Get the string value of a variable.
17820 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017821 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17822 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 * If the String variable has never been set, return an empty string.
17824 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017825 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17826 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 */
17828 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017829get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017830 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831{
17832 static char_u mybuf[NUMBUFLEN];
17833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017834 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835}
17836
17837 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017839 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017840 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017842 char_u *res = get_tv_string_buf_chk(varp, buf);
17843
17844 return res != NULL ? res : (char_u *)"";
17845}
17846
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017847 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017848get_tv_string_chk(varp)
17849 typval_T *varp;
17850{
17851 static char_u mybuf[NUMBUFLEN];
17852
17853 return get_tv_string_buf_chk(varp, mybuf);
17854}
17855
17856 static char_u *
17857get_tv_string_buf_chk(varp, buf)
17858 typval_T *varp;
17859 char_u *buf;
17860{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017861 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017863 case VAR_NUMBER:
17864 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17865 return buf;
17866 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017867 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017868 break;
17869 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017870 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017871 break;
17872 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017873 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017874 break;
17875 case VAR_STRING:
17876 if (varp->vval.v_string != NULL)
17877 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017878 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017879 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017880 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017881 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017883 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884}
17885
17886/*
17887 * Find variable "name" in the list of variables.
17888 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017889 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017890 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017891 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017894find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017896 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017899 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900
Bram Moolenaara7043832005-01-21 11:56:39 +000017901 ht = find_var_ht(name, &varname);
17902 if (htp != NULL)
17903 *htp = ht;
17904 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017906 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907}
17908
17909/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017910 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017911 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017913 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017914find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017915 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017916 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017917 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017918{
Bram Moolenaar33570922005-01-25 22:26:29 +000017919 hashitem_T *hi;
17920
17921 if (*varname == NUL)
17922 {
17923 /* Must be something like "s:", otherwise "ht" would be NULL. */
17924 switch (varname[-2])
17925 {
17926 case 's': return &SCRIPT_SV(current_SID).sv_var;
17927 case 'g': return &globvars_var;
17928 case 'v': return &vimvars_var;
17929 case 'b': return &curbuf->b_bufvar;
17930 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017931#ifdef FEAT_WINDOWS
17932 case 't': return &curtab->tp_winvar;
17933#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017934 case 'l': return current_funccal == NULL
17935 ? NULL : &current_funccal->l_vars_var;
17936 case 'a': return current_funccal == NULL
17937 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017938 }
17939 return NULL;
17940 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017941
17942 hi = hash_find(ht, varname);
17943 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017944 {
17945 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017946 * worked find the variable again. Don't auto-load a script if it was
17947 * loaded already, otherwise it would be loaded every time when
17948 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017949 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017950 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017951 hi = hash_find(ht, varname);
17952 if (HASHITEM_EMPTY(hi))
17953 return NULL;
17954 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017955 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017956}
17957
17958/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017959 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017960 * Set "varname" to the start of name without ':'.
17961 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017962 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017963find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 char_u *name;
17965 char_u **varname;
17966{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017967 hashitem_T *hi;
17968
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 if (name[1] != ':')
17970 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017971 /* The name must not start with a colon or #. */
17972 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 return NULL;
17974 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017975
17976 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017977 hi = hash_find(&compat_hashtab, name);
17978 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017979 return &compat_hashtab;
17980
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017982 return &globvarht; /* global variable */
17983 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984 }
17985 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017986 if (*name == 'g') /* global variable */
17987 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017988 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17989 */
17990 if (vim_strchr(name + 2, ':') != NULL
17991 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017992 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017996 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017997#ifdef FEAT_WINDOWS
17998 if (*name == 't') /* tab page variable */
17999 return &curtab->tp_vars.dv_hashtab;
18000#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018001 if (*name == 'v') /* v: variable */
18002 return &vimvarht;
18003 if (*name == 'a' && current_funccal != NULL) /* function argument */
18004 return &current_funccal->l_avars.dv_hashtab;
18005 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18006 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 if (*name == 's' /* script variable */
18008 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18009 return &SCRIPT_VARS(current_SID);
18010 return NULL;
18011}
18012
18013/*
18014 * Get the string value of a (global/local) variable.
18015 * Returns NULL when it doesn't exist.
18016 */
18017 char_u *
18018get_var_value(name)
18019 char_u *name;
18020{
Bram Moolenaar33570922005-01-25 22:26:29 +000018021 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022
Bram Moolenaara7043832005-01-21 11:56:39 +000018023 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 if (v == NULL)
18025 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018026 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027}
18028
18029/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018030 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 * sourcing this script and when executing functions defined in the script.
18032 */
18033 void
18034new_script_vars(id)
18035 scid_T id;
18036{
Bram Moolenaara7043832005-01-21 11:56:39 +000018037 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018038 hashtab_T *ht;
18039 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018040
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18042 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018043 /* Re-allocating ga_data means that an ht_array pointing to
18044 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018046 for (i = 1; i <= ga_scripts.ga_len; ++i)
18047 {
18048 ht = &SCRIPT_VARS(i);
18049 if (ht->ht_mask == HT_INIT_SIZE - 1)
18050 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018051 sv = &SCRIPT_SV(i);
18052 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018053 }
18054
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 while (ga_scripts.ga_len < id)
18056 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018057 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18058 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 }
18061 }
18062}
18063
18064/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018065 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18066 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 */
18068 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018069init_var_dict(dict, dict_var)
18070 dict_T *dict;
18071 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072{
Bram Moolenaar33570922005-01-25 22:26:29 +000018073 hash_init(&dict->dv_hashtab);
18074 dict->dv_refcount = 99999;
18075 dict_var->di_tv.vval.v_dict = dict;
18076 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018077 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018078 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18079 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080}
18081
18082/*
18083 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018084 * Frees all allocated variables and the value they contain.
18085 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 */
18087 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018088vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018089 hashtab_T *ht;
18090{
18091 vars_clear_ext(ht, TRUE);
18092}
18093
18094/*
18095 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18096 */
18097 static void
18098vars_clear_ext(ht, free_val)
18099 hashtab_T *ht;
18100 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101{
Bram Moolenaara7043832005-01-21 11:56:39 +000018102 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018103 hashitem_T *hi;
18104 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105
Bram Moolenaar33570922005-01-25 22:26:29 +000018106 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018107 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018108 for (hi = ht->ht_array; todo > 0; ++hi)
18109 {
18110 if (!HASHITEM_EMPTY(hi))
18111 {
18112 --todo;
18113
Bram Moolenaar33570922005-01-25 22:26:29 +000018114 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018115 * ht_array might change then. hash_clear() takes care of it
18116 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018117 v = HI2DI(hi);
18118 if (free_val)
18119 clear_tv(&v->di_tv);
18120 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18121 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018122 }
18123 }
18124 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018125 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126}
18127
Bram Moolenaara7043832005-01-21 11:56:39 +000018128/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018129 * Delete a variable from hashtab "ht" at item "hi".
18130 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018133delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018134 hashtab_T *ht;
18135 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136{
Bram Moolenaar33570922005-01-25 22:26:29 +000018137 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018138
18139 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018140 clear_tv(&di->di_tv);
18141 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142}
18143
18144/*
18145 * List the value of one internal variable.
18146 */
18147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018148list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018149 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018151 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018153 char_u *tofree;
18154 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018155 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018156
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018157 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018158 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018159 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018160 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161}
18162
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018164list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 char_u *prefix;
18166 char_u *name;
18167 int type;
18168 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018169 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170{
Bram Moolenaar31859182007-08-14 20:41:13 +000018171 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18172 msg_start();
18173 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 if (name != NULL) /* "a:" vars don't have a name stored */
18175 msg_puts(name);
18176 msg_putchar(' ');
18177 msg_advance(22);
18178 if (type == VAR_NUMBER)
18179 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018180 else if (type == VAR_FUNC)
18181 msg_putchar('*');
18182 else if (type == VAR_LIST)
18183 {
18184 msg_putchar('[');
18185 if (*string == '[')
18186 ++string;
18187 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018188 else if (type == VAR_DICT)
18189 {
18190 msg_putchar('{');
18191 if (*string == '{')
18192 ++string;
18193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 else
18195 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018196
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018198
18199 if (type == VAR_FUNC)
18200 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018201 if (*first)
18202 {
18203 msg_clr_eos();
18204 *first = FALSE;
18205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206}
18207
18208/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018209 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210 * If the variable already exists, the value is updated.
18211 * Otherwise the variable is created.
18212 */
18213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018214set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018216 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018217 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218{
Bram Moolenaar33570922005-01-25 22:26:29 +000018219 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018221 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018222 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018224 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018225 {
18226 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18227 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18228 ? name[2] : name[0]))
18229 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018230 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018231 return;
18232 }
18233 if (function_exists(name))
18234 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018235 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018236 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018237 return;
18238 }
18239 }
18240
Bram Moolenaara7043832005-01-21 11:56:39 +000018241 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018242 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018243 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018244 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018245 return;
18246 }
18247
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018248 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018249 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018251 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018252 if (var_check_ro(v->di_flags, name)
18253 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018254 return;
18255 if (v->di_tv.v_type != tv->v_type
18256 && !((v->di_tv.v_type == VAR_STRING
18257 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018258 && (tv->v_type == VAR_STRING
18259 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018260 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018261 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018262 return;
18263 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018264
18265 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018266 * Handle setting internal v: variables separately: we don't change
18267 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018268 */
18269 if (ht == &vimvarht)
18270 {
18271 if (v->di_tv.v_type == VAR_STRING)
18272 {
18273 vim_free(v->di_tv.vval.v_string);
18274 if (copy || tv->v_type != VAR_STRING)
18275 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18276 else
18277 {
18278 /* Take over the string to avoid an extra alloc/free. */
18279 v->di_tv.vval.v_string = tv->vval.v_string;
18280 tv->vval.v_string = NULL;
18281 }
18282 }
18283 else if (v->di_tv.v_type != VAR_NUMBER)
18284 EMSG2(_(e_intern2), "set_var()");
18285 else
18286 v->di_tv.vval.v_number = get_tv_number(tv);
18287 return;
18288 }
18289
18290 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291 }
18292 else /* add a new variable */
18293 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018294 /* Can't add "v:" variable. */
18295 if (ht == &vimvarht)
18296 {
18297 EMSG2(_(e_illvar), name);
18298 return;
18299 }
18300
Bram Moolenaar92124a32005-06-17 22:03:40 +000018301 /* Make sure the variable name is valid. */
18302 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018303 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18304 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018305 {
18306 EMSG2(_(e_illvar), varname);
18307 return;
18308 }
18309
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018310 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18311 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018312 if (v == NULL)
18313 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018314 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018315 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018317 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 return;
18319 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018320 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018323 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018324 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018325 else
18326 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018327 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018328 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018329 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331}
18332
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018333/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018334 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018335 * Also give an error message.
18336 */
18337 static int
18338var_check_ro(flags, name)
18339 int flags;
18340 char_u *name;
18341{
18342 if (flags & DI_FLAGS_RO)
18343 {
18344 EMSG2(_(e_readonlyvar), name);
18345 return TRUE;
18346 }
18347 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18348 {
18349 EMSG2(_(e_readonlysbx), name);
18350 return TRUE;
18351 }
18352 return FALSE;
18353}
18354
18355/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018356 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18357 * Also give an error message.
18358 */
18359 static int
18360var_check_fixed(flags, name)
18361 int flags;
18362 char_u *name;
18363{
18364 if (flags & DI_FLAGS_FIX)
18365 {
18366 EMSG2(_("E795: Cannot delete variable %s"), name);
18367 return TRUE;
18368 }
18369 return FALSE;
18370}
18371
18372/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018373 * Return TRUE if typeval "tv" is set to be locked (immutable).
18374 * Also give an error message, using "name".
18375 */
18376 static int
18377tv_check_lock(lock, name)
18378 int lock;
18379 char_u *name;
18380{
18381 if (lock & VAR_LOCKED)
18382 {
18383 EMSG2(_("E741: Value is locked: %s"),
18384 name == NULL ? (char_u *)_("Unknown") : name);
18385 return TRUE;
18386 }
18387 if (lock & VAR_FIXED)
18388 {
18389 EMSG2(_("E742: Cannot change value of %s"),
18390 name == NULL ? (char_u *)_("Unknown") : name);
18391 return TRUE;
18392 }
18393 return FALSE;
18394}
18395
18396/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018397 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018398 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018399 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018402copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018403 typval_T *from;
18404 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018406 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018407 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018408 switch (from->v_type)
18409 {
18410 case VAR_NUMBER:
18411 to->vval.v_number = from->vval.v_number;
18412 break;
18413 case VAR_STRING:
18414 case VAR_FUNC:
18415 if (from->vval.v_string == NULL)
18416 to->vval.v_string = NULL;
18417 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018418 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018419 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018420 if (from->v_type == VAR_FUNC)
18421 func_ref(to->vval.v_string);
18422 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018423 break;
18424 case VAR_LIST:
18425 if (from->vval.v_list == NULL)
18426 to->vval.v_list = NULL;
18427 else
18428 {
18429 to->vval.v_list = from->vval.v_list;
18430 ++to->vval.v_list->lv_refcount;
18431 }
18432 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018433 case VAR_DICT:
18434 if (from->vval.v_dict == NULL)
18435 to->vval.v_dict = NULL;
18436 else
18437 {
18438 to->vval.v_dict = from->vval.v_dict;
18439 ++to->vval.v_dict->dv_refcount;
18440 }
18441 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018442 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018443 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018444 break;
18445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446}
18447
18448/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018449 * Make a copy of an item.
18450 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018451 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18452 * reference to an already copied list/dict can be used.
18453 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018454 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018455 static int
18456item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018457 typval_T *from;
18458 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018459 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018460 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018461{
18462 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018463 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018464
Bram Moolenaar33570922005-01-25 22:26:29 +000018465 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018466 {
18467 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018468 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018469 }
18470 ++recurse;
18471
18472 switch (from->v_type)
18473 {
18474 case VAR_NUMBER:
18475 case VAR_STRING:
18476 case VAR_FUNC:
18477 copy_tv(from, to);
18478 break;
18479 case VAR_LIST:
18480 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018481 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018482 if (from->vval.v_list == NULL)
18483 to->vval.v_list = NULL;
18484 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18485 {
18486 /* use the copy made earlier */
18487 to->vval.v_list = from->vval.v_list->lv_copylist;
18488 ++to->vval.v_list->lv_refcount;
18489 }
18490 else
18491 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18492 if (to->vval.v_list == NULL)
18493 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018494 break;
18495 case VAR_DICT:
18496 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018497 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018498 if (from->vval.v_dict == NULL)
18499 to->vval.v_dict = NULL;
18500 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18501 {
18502 /* use the copy made earlier */
18503 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18504 ++to->vval.v_dict->dv_refcount;
18505 }
18506 else
18507 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18508 if (to->vval.v_dict == NULL)
18509 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018510 break;
18511 default:
18512 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018513 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018514 }
18515 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018516 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018517}
18518
18519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 * ":echo expr1 ..." print each argument separated with a space, add a
18521 * newline at the end.
18522 * ":echon expr1 ..." print each argument plain.
18523 */
18524 void
18525ex_echo(eap)
18526 exarg_T *eap;
18527{
18528 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018529 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018530 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 char_u *p;
18532 int needclr = TRUE;
18533 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018534 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535
18536 if (eap->skip)
18537 ++emsg_skip;
18538 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18539 {
18540 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018541 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 {
18543 /*
18544 * Report the invalid expression unless the expression evaluation
18545 * has been cancelled due to an aborting error, an interrupt, or an
18546 * exception.
18547 */
18548 if (!aborting())
18549 EMSG2(_(e_invexpr2), p);
18550 break;
18551 }
18552 if (!eap->skip)
18553 {
18554 if (atstart)
18555 {
18556 atstart = FALSE;
18557 /* Call msg_start() after eval1(), evaluating the expression
18558 * may cause a message to appear. */
18559 if (eap->cmdidx == CMD_echo)
18560 msg_start();
18561 }
18562 else if (eap->cmdidx == CMD_echo)
18563 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018564 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018565 if (p != NULL)
18566 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018568 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018570 if (*p != TAB && needclr)
18571 {
18572 /* remove any text still there from the command */
18573 msg_clr_eos();
18574 needclr = FALSE;
18575 }
18576 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 }
18578 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018579 {
18580#ifdef FEAT_MBYTE
18581 if (has_mbyte)
18582 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018583 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018584
18585 (void)msg_outtrans_len_attr(p, i, echo_attr);
18586 p += i - 1;
18587 }
18588 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018590 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018593 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 arg = skipwhite(arg);
18597 }
18598 eap->nextcmd = check_nextcmd(arg);
18599
18600 if (eap->skip)
18601 --emsg_skip;
18602 else
18603 {
18604 /* remove text that may still be there from the command */
18605 if (needclr)
18606 msg_clr_eos();
18607 if (eap->cmdidx == CMD_echo)
18608 msg_end();
18609 }
18610}
18611
18612/*
18613 * ":echohl {name}".
18614 */
18615 void
18616ex_echohl(eap)
18617 exarg_T *eap;
18618{
18619 int id;
18620
18621 id = syn_name2id(eap->arg);
18622 if (id == 0)
18623 echo_attr = 0;
18624 else
18625 echo_attr = syn_id2attr(id);
18626}
18627
18628/*
18629 * ":execute expr1 ..." execute the result of an expression.
18630 * ":echomsg expr1 ..." Print a message
18631 * ":echoerr expr1 ..." Print an error
18632 * Each gets spaces around each argument and a newline at the end for
18633 * echo commands
18634 */
18635 void
18636ex_execute(eap)
18637 exarg_T *eap;
18638{
18639 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018640 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 int ret = OK;
18642 char_u *p;
18643 garray_T ga;
18644 int len;
18645 int save_did_emsg;
18646
18647 ga_init2(&ga, 1, 80);
18648
18649 if (eap->skip)
18650 ++emsg_skip;
18651 while (*arg != NUL && *arg != '|' && *arg != '\n')
18652 {
18653 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018654 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 {
18656 /*
18657 * Report the invalid expression unless the expression evaluation
18658 * has been cancelled due to an aborting error, an interrupt, or an
18659 * exception.
18660 */
18661 if (!aborting())
18662 EMSG2(_(e_invexpr2), p);
18663 ret = FAIL;
18664 break;
18665 }
18666
18667 if (!eap->skip)
18668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 len = (int)STRLEN(p);
18671 if (ga_grow(&ga, len + 2) == FAIL)
18672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018673 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 ret = FAIL;
18675 break;
18676 }
18677 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 ga.ga_len += len;
18681 }
18682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018683 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 arg = skipwhite(arg);
18685 }
18686
18687 if (ret != FAIL && ga.ga_data != NULL)
18688 {
18689 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018692 out_flush();
18693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 else if (eap->cmdidx == CMD_echoerr)
18695 {
18696 /* We don't want to abort following commands, restore did_emsg. */
18697 save_did_emsg = did_emsg;
18698 EMSG((char_u *)ga.ga_data);
18699 if (!force_abort)
18700 did_emsg = save_did_emsg;
18701 }
18702 else if (eap->cmdidx == CMD_execute)
18703 do_cmdline((char_u *)ga.ga_data,
18704 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18705 }
18706
18707 ga_clear(&ga);
18708
18709 if (eap->skip)
18710 --emsg_skip;
18711
18712 eap->nextcmd = check_nextcmd(arg);
18713}
18714
18715/*
18716 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18717 * "arg" points to the "&" or '+' when called, to "option" when returning.
18718 * Returns NULL when no option name found. Otherwise pointer to the char
18719 * after the option name.
18720 */
18721 static char_u *
18722find_option_end(arg, opt_flags)
18723 char_u **arg;
18724 int *opt_flags;
18725{
18726 char_u *p = *arg;
18727
18728 ++p;
18729 if (*p == 'g' && p[1] == ':')
18730 {
18731 *opt_flags = OPT_GLOBAL;
18732 p += 2;
18733 }
18734 else if (*p == 'l' && p[1] == ':')
18735 {
18736 *opt_flags = OPT_LOCAL;
18737 p += 2;
18738 }
18739 else
18740 *opt_flags = 0;
18741
18742 if (!ASCII_ISALPHA(*p))
18743 return NULL;
18744 *arg = p;
18745
18746 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18747 p += 4; /* termcap option */
18748 else
18749 while (ASCII_ISALPHA(*p))
18750 ++p;
18751 return p;
18752}
18753
18754/*
18755 * ":function"
18756 */
18757 void
18758ex_function(eap)
18759 exarg_T *eap;
18760{
18761 char_u *theline;
18762 int j;
18763 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 char_u *name = NULL;
18766 char_u *p;
18767 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018768 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 garray_T newargs;
18770 garray_T newlines;
18771 int varargs = FALSE;
18772 int mustend = FALSE;
18773 int flags = 0;
18774 ufunc_T *fp;
18775 int indent;
18776 int nesting;
18777 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018778 dictitem_T *v;
18779 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018780 static int func_nr = 0; /* number for nameless function */
18781 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018782 hashtab_T *ht;
18783 int todo;
18784 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018785 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786
18787 /*
18788 * ":function" without argument: list functions.
18789 */
18790 if (ends_excmd(*eap->arg))
18791 {
18792 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018793 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018794 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018795 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018796 {
18797 if (!HASHITEM_EMPTY(hi))
18798 {
18799 --todo;
18800 fp = HI2UF(hi);
18801 if (!isdigit(*fp->uf_name))
18802 list_func_head(fp, FALSE);
18803 }
18804 }
18805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 eap->nextcmd = check_nextcmd(eap->arg);
18807 return;
18808 }
18809
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018810 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018811 * ":function /pat": list functions matching pattern.
18812 */
18813 if (*eap->arg == '/')
18814 {
18815 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18816 if (!eap->skip)
18817 {
18818 regmatch_T regmatch;
18819
18820 c = *p;
18821 *p = NUL;
18822 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18823 *p = c;
18824 if (regmatch.regprog != NULL)
18825 {
18826 regmatch.rm_ic = p_ic;
18827
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018828 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018829 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18830 {
18831 if (!HASHITEM_EMPTY(hi))
18832 {
18833 --todo;
18834 fp = HI2UF(hi);
18835 if (!isdigit(*fp->uf_name)
18836 && vim_regexec(&regmatch, fp->uf_name, 0))
18837 list_func_head(fp, FALSE);
18838 }
18839 }
18840 }
18841 }
18842 if (*p == '/')
18843 ++p;
18844 eap->nextcmd = check_nextcmd(p);
18845 return;
18846 }
18847
18848 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018849 * Get the function name. There are these situations:
18850 * func normal function name
18851 * "name" == func, "fudi.fd_dict" == NULL
18852 * dict.func new dictionary entry
18853 * "name" == NULL, "fudi.fd_dict" set,
18854 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18855 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018856 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018857 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18858 * dict.func existing dict entry that's not a Funcref
18859 * "name" == NULL, "fudi.fd_dict" set,
18860 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018863 name = trans_function_name(&p, eap->skip, 0, &fudi);
18864 paren = (vim_strchr(p, '(') != NULL);
18865 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 {
18867 /*
18868 * Return on an invalid expression in braces, unless the expression
18869 * evaluation has been cancelled due to an aborting error, an
18870 * interrupt, or an exception.
18871 */
18872 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018873 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018874 if (!eap->skip && fudi.fd_newkey != NULL)
18875 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018876 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 else
18880 eap->skip = TRUE;
18881 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018882
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 /* An error in a function call during evaluation of an expression in magic
18884 * braces should not cause the function not to be defined. */
18885 saved_did_emsg = did_emsg;
18886 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887
18888 /*
18889 * ":function func" with only function name: list function.
18890 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018891 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 {
18893 if (!ends_excmd(*skipwhite(p)))
18894 {
18895 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018896 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 }
18898 eap->nextcmd = check_nextcmd(p);
18899 if (eap->nextcmd != NULL)
18900 *p = NUL;
18901 if (!eap->skip && !got_int)
18902 {
18903 fp = find_func(name);
18904 if (fp != NULL)
18905 {
18906 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018907 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018909 if (FUNCLINE(fp, j) == NULL)
18910 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 msg_putchar('\n');
18912 msg_outnum((long)(j + 1));
18913 if (j < 9)
18914 msg_putchar(' ');
18915 if (j < 99)
18916 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018917 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 out_flush(); /* show a line at a time */
18919 ui_breakcheck();
18920 }
18921 if (!got_int)
18922 {
18923 msg_putchar('\n');
18924 msg_puts((char_u *)" endfunction");
18925 }
18926 }
18927 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018928 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018930 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 }
18932
18933 /*
18934 * ":function name(arg1, arg2)" Define function.
18935 */
18936 p = skipwhite(p);
18937 if (*p != '(')
18938 {
18939 if (!eap->skip)
18940 {
18941 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018942 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 }
18944 /* attempt to continue by skipping some text */
18945 if (vim_strchr(p, '(') != NULL)
18946 p = vim_strchr(p, '(');
18947 }
18948 p = skipwhite(p + 1);
18949
18950 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18951 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18952
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018953 if (!eap->skip)
18954 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018955 /* Check the name of the function. Unless it's a dictionary function
18956 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018957 if (name != NULL)
18958 arg = name;
18959 else
18960 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018961 if (arg != NULL && (fudi.fd_di == NULL
18962 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018963 {
18964 if (*arg == K_SPECIAL)
18965 j = 3;
18966 else
18967 j = 0;
18968 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18969 : eval_isnamec(arg[j])))
18970 ++j;
18971 if (arg[j] != NUL)
18972 emsg_funcname(_(e_invarg2), arg);
18973 }
18974 }
18975
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 /*
18977 * Isolate the arguments: "arg1, arg2, ...)"
18978 */
18979 while (*p != ')')
18980 {
18981 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18982 {
18983 varargs = TRUE;
18984 p += 3;
18985 mustend = TRUE;
18986 }
18987 else
18988 {
18989 arg = p;
18990 while (ASCII_ISALNUM(*p) || *p == '_')
18991 ++p;
18992 if (arg == p || isdigit(*arg)
18993 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18994 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18995 {
18996 if (!eap->skip)
18997 EMSG2(_("E125: Illegal argument: %s"), arg);
18998 break;
18999 }
19000 if (ga_grow(&newargs, 1) == FAIL)
19001 goto erret;
19002 c = *p;
19003 *p = NUL;
19004 arg = vim_strsave(arg);
19005 if (arg == NULL)
19006 goto erret;
19007 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19008 *p = c;
19009 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 if (*p == ',')
19011 ++p;
19012 else
19013 mustend = TRUE;
19014 }
19015 p = skipwhite(p);
19016 if (mustend && *p != ')')
19017 {
19018 if (!eap->skip)
19019 EMSG2(_(e_invarg2), eap->arg);
19020 break;
19021 }
19022 }
19023 ++p; /* skip the ')' */
19024
Bram Moolenaare9a41262005-01-15 22:18:47 +000019025 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 for (;;)
19027 {
19028 p = skipwhite(p);
19029 if (STRNCMP(p, "range", 5) == 0)
19030 {
19031 flags |= FC_RANGE;
19032 p += 5;
19033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019034 else if (STRNCMP(p, "dict", 4) == 0)
19035 {
19036 flags |= FC_DICT;
19037 p += 4;
19038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 else if (STRNCMP(p, "abort", 5) == 0)
19040 {
19041 flags |= FC_ABORT;
19042 p += 5;
19043 }
19044 else
19045 break;
19046 }
19047
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019048 /* When there is a line break use what follows for the function body.
19049 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19050 if (*p == '\n')
19051 line_arg = p + 1;
19052 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053 EMSG(_(e_trailing));
19054
19055 /*
19056 * Read the body of the function, until ":endfunction" is found.
19057 */
19058 if (KeyTyped)
19059 {
19060 /* Check if the function already exists, don't let the user type the
19061 * whole function before telling him it doesn't work! For a script we
19062 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019063 if (!eap->skip && !eap->forceit)
19064 {
19065 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19066 EMSG(_(e_funcdict));
19067 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019068 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019071 if (!eap->skip && did_emsg)
19072 goto erret;
19073
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 msg_putchar('\n'); /* don't overwrite the function name */
19075 cmdline_row = msg_row;
19076 }
19077
19078 indent = 2;
19079 nesting = 0;
19080 for (;;)
19081 {
19082 msg_scroll = TRUE;
19083 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019084 sourcing_lnum_off = sourcing_lnum;
19085
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019086 if (line_arg != NULL)
19087 {
19088 /* Use eap->arg, split up in parts by line breaks. */
19089 theline = line_arg;
19090 p = vim_strchr(theline, '\n');
19091 if (p == NULL)
19092 line_arg += STRLEN(line_arg);
19093 else
19094 {
19095 *p = NUL;
19096 line_arg = p + 1;
19097 }
19098 }
19099 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 theline = getcmdline(':', 0L, indent);
19101 else
19102 theline = eap->getline(':', eap->cookie, indent);
19103 if (KeyTyped)
19104 lines_left = Rows - 1;
19105 if (theline == NULL)
19106 {
19107 EMSG(_("E126: Missing :endfunction"));
19108 goto erret;
19109 }
19110
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019111 /* Detect line continuation: sourcing_lnum increased more than one. */
19112 if (sourcing_lnum > sourcing_lnum_off + 1)
19113 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19114 else
19115 sourcing_lnum_off = 0;
19116
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 if (skip_until != NULL)
19118 {
19119 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19120 * don't check for ":endfunc". */
19121 if (STRCMP(theline, skip_until) == 0)
19122 {
19123 vim_free(skip_until);
19124 skip_until = NULL;
19125 }
19126 }
19127 else
19128 {
19129 /* skip ':' and blanks*/
19130 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19131 ;
19132
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019133 /* Check for "endfunction". */
19134 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019136 if (line_arg == NULL)
19137 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 break;
19139 }
19140
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019141 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 * at "end". */
19143 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19144 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019145 else if (STRNCMP(p, "if", 2) == 0
19146 || STRNCMP(p, "wh", 2) == 0
19147 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148 || STRNCMP(p, "try", 3) == 0)
19149 indent += 2;
19150
19151 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019152 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019154 if (*p == '!')
19155 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 p += eval_fname_script(p);
19157 if (ASCII_ISALPHA(*p))
19158 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019159 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 if (*skipwhite(p) == '(')
19161 {
19162 ++nesting;
19163 indent += 2;
19164 }
19165 }
19166 }
19167
19168 /* Check for ":append" or ":insert". */
19169 p = skip_range(p, NULL);
19170 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19171 || (p[0] == 'i'
19172 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19173 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19174 skip_until = vim_strsave((char_u *)".");
19175
19176 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19177 arg = skipwhite(skiptowhite(p));
19178 if (arg[0] == '<' && arg[1] =='<'
19179 && ((p[0] == 'p' && p[1] == 'y'
19180 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19181 || (p[0] == 'p' && p[1] == 'e'
19182 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19183 || (p[0] == 't' && p[1] == 'c'
19184 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19185 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19186 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019187 || (p[0] == 'm' && p[1] == 'z'
19188 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 ))
19190 {
19191 /* ":python <<" continues until a dot, like ":append" */
19192 p = skipwhite(arg + 2);
19193 if (*p == NUL)
19194 skip_until = vim_strsave((char_u *)".");
19195 else
19196 skip_until = vim_strsave(p);
19197 }
19198 }
19199
19200 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019201 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019202 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019203 if (line_arg == NULL)
19204 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019206 }
19207
19208 /* Copy the line to newly allocated memory. get_one_sourceline()
19209 * allocates 250 bytes per line, this saves 80% on average. The cost
19210 * is an extra alloc/free. */
19211 p = vim_strsave(theline);
19212 if (p != NULL)
19213 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019214 if (line_arg == NULL)
19215 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019216 theline = p;
19217 }
19218
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019219 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19220
19221 /* Add NULL lines for continuation lines, so that the line count is
19222 * equal to the index in the growarray. */
19223 while (sourcing_lnum_off-- > 0)
19224 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019225
19226 /* Check for end of eap->arg. */
19227 if (line_arg != NULL && *line_arg == NUL)
19228 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 }
19230
19231 /* Don't define the function when skipping commands or when an error was
19232 * detected. */
19233 if (eap->skip || did_emsg)
19234 goto erret;
19235
19236 /*
19237 * If there are no errors, add the function
19238 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019239 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019240 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019241 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019242 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019243 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019244 emsg_funcname("E707: Function name conflicts with variable: %s",
19245 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019246 goto erret;
19247 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019248
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019249 fp = find_func(name);
19250 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019252 if (!eap->forceit)
19253 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019254 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019255 goto erret;
19256 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019257 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019258 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019259 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019260 name);
19261 goto erret;
19262 }
19263 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019264 ga_clear_strings(&(fp->uf_args));
19265 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019266 vim_free(name);
19267 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 }
19270 else
19271 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019272 char numbuf[20];
19273
19274 fp = NULL;
19275 if (fudi.fd_newkey == NULL && !eap->forceit)
19276 {
19277 EMSG(_(e_funcdict));
19278 goto erret;
19279 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019280 if (fudi.fd_di == NULL)
19281 {
19282 /* Can't add a function to a locked dictionary */
19283 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19284 goto erret;
19285 }
19286 /* Can't change an existing function if it is locked */
19287 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19288 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019289
19290 /* Give the function a sequential number. Can only be used with a
19291 * Funcref! */
19292 vim_free(name);
19293 sprintf(numbuf, "%d", ++func_nr);
19294 name = vim_strsave((char_u *)numbuf);
19295 if (name == NULL)
19296 goto erret;
19297 }
19298
19299 if (fp == NULL)
19300 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019301 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019302 {
19303 int slen, plen;
19304 char_u *scriptname;
19305
19306 /* Check that the autoload name matches the script name. */
19307 j = FAIL;
19308 if (sourcing_name != NULL)
19309 {
19310 scriptname = autoload_name(name);
19311 if (scriptname != NULL)
19312 {
19313 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019314 plen = (int)STRLEN(p);
19315 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019316 if (slen > plen && fnamecmp(p,
19317 sourcing_name + slen - plen) == 0)
19318 j = OK;
19319 vim_free(scriptname);
19320 }
19321 }
19322 if (j == FAIL)
19323 {
19324 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19325 goto erret;
19326 }
19327 }
19328
19329 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 if (fp == NULL)
19331 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019332
19333 if (fudi.fd_dict != NULL)
19334 {
19335 if (fudi.fd_di == NULL)
19336 {
19337 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019338 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019339 if (fudi.fd_di == NULL)
19340 {
19341 vim_free(fp);
19342 goto erret;
19343 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019344 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19345 {
19346 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019347 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019348 goto erret;
19349 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019350 }
19351 else
19352 /* overwrite existing dict entry */
19353 clear_tv(&fudi.fd_di->di_tv);
19354 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019355 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019356 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019357 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019358
19359 /* behave like "dict" was used */
19360 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019361 }
19362
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019364 STRCPY(fp->uf_name, name);
19365 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019367 fp->uf_args = newargs;
19368 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019369#ifdef FEAT_PROFILE
19370 fp->uf_tml_count = NULL;
19371 fp->uf_tml_total = NULL;
19372 fp->uf_tml_self = NULL;
19373 fp->uf_profiling = FALSE;
19374 if (prof_def_func())
19375 func_do_profile(fp);
19376#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019377 fp->uf_varargs = varargs;
19378 fp->uf_flags = flags;
19379 fp->uf_calls = 0;
19380 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019381 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382
19383erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 ga_clear_strings(&newargs);
19385 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019386ret_free:
19387 vim_free(skip_until);
19388 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391}
19392
19393/*
19394 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019395 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019397 * flags:
19398 * TFN_INT: internal function name OK
19399 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 * Advances "pp" to just after the function name (if no error).
19401 */
19402 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019403trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 char_u **pp;
19405 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019406 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019407 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019409 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 char_u *start;
19411 char_u *end;
19412 int lead;
19413 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019415 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019416
19417 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019418 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019420
19421 /* Check for hard coded <SNR>: already translated function ID (from a user
19422 * command). */
19423 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19424 && (*pp)[2] == (int)KE_SNR)
19425 {
19426 *pp += 3;
19427 len = get_id_len(pp) + 3;
19428 return vim_strnsave(start, len);
19429 }
19430
19431 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19432 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019434 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019436
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019437 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19438 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019439 if (end == start)
19440 {
19441 if (!skip)
19442 EMSG(_("E129: Function name required"));
19443 goto theend;
19444 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019445 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019446 {
19447 /*
19448 * Report an invalid expression in braces, unless the expression
19449 * evaluation has been cancelled due to an aborting error, an
19450 * interrupt, or an exception.
19451 */
19452 if (!aborting())
19453 {
19454 if (end != NULL)
19455 EMSG2(_(e_invarg2), start);
19456 }
19457 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019458 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019459 goto theend;
19460 }
19461
19462 if (lv.ll_tv != NULL)
19463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019464 if (fdp != NULL)
19465 {
19466 fdp->fd_dict = lv.ll_dict;
19467 fdp->fd_newkey = lv.ll_newkey;
19468 lv.ll_newkey = NULL;
19469 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019470 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019471 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19472 {
19473 name = vim_strsave(lv.ll_tv->vval.v_string);
19474 *pp = end;
19475 }
19476 else
19477 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019478 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19479 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019480 EMSG(_(e_funcref));
19481 else
19482 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019483 name = NULL;
19484 }
19485 goto theend;
19486 }
19487
19488 if (lv.ll_name == NULL)
19489 {
19490 /* Error found, but continue after the function name. */
19491 *pp = end;
19492 goto theend;
19493 }
19494
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019495 /* Check if the name is a Funcref. If so, use the value. */
19496 if (lv.ll_exp_name != NULL)
19497 {
19498 len = (int)STRLEN(lv.ll_exp_name);
19499 name = deref_func_name(lv.ll_exp_name, &len);
19500 if (name == lv.ll_exp_name)
19501 name = NULL;
19502 }
19503 else
19504 {
19505 len = (int)(end - *pp);
19506 name = deref_func_name(*pp, &len);
19507 if (name == *pp)
19508 name = NULL;
19509 }
19510 if (name != NULL)
19511 {
19512 name = vim_strsave(name);
19513 *pp = end;
19514 goto theend;
19515 }
19516
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019517 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019518 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019519 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019520 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19521 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19522 {
19523 /* When there was "s:" already or the name expanded to get a
19524 * leading "s:" then remove it. */
19525 lv.ll_name += 2;
19526 len -= 2;
19527 lead = 2;
19528 }
19529 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019530 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019531 {
19532 if (lead == 2) /* skip over "s:" */
19533 lv.ll_name += 2;
19534 len = (int)(end - lv.ll_name);
19535 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019536
19537 /*
19538 * Copy the function name to allocated memory.
19539 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19540 * Accept <SNR>123_name() outside a script.
19541 */
19542 if (skip)
19543 lead = 0; /* do nothing */
19544 else if (lead > 0)
19545 {
19546 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019547 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19548 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019549 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019550 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019551 if (current_SID <= 0)
19552 {
19553 EMSG(_(e_usingsid));
19554 goto theend;
19555 }
19556 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19557 lead += (int)STRLEN(sid_buf);
19558 }
19559 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019560 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019561 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019562 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019563 goto theend;
19564 }
19565 name = alloc((unsigned)(len + lead + 1));
19566 if (name != NULL)
19567 {
19568 if (lead > 0)
19569 {
19570 name[0] = K_SPECIAL;
19571 name[1] = KS_EXTRA;
19572 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019573 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019574 STRCPY(name + 3, sid_buf);
19575 }
19576 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19577 name[len + lead] = NUL;
19578 }
19579 *pp = end;
19580
19581theend:
19582 clear_lval(&lv);
19583 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584}
19585
19586/*
19587 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19588 * Return 2 if "p" starts with "s:".
19589 * Return 0 otherwise.
19590 */
19591 static int
19592eval_fname_script(p)
19593 char_u *p;
19594{
19595 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19596 || STRNICMP(p + 1, "SNR>", 4) == 0))
19597 return 5;
19598 if (p[0] == 's' && p[1] == ':')
19599 return 2;
19600 return 0;
19601}
19602
19603/*
19604 * Return TRUE if "p" starts with "<SID>" or "s:".
19605 * Only works if eval_fname_script() returned non-zero for "p"!
19606 */
19607 static int
19608eval_fname_sid(p)
19609 char_u *p;
19610{
19611 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19612}
19613
19614/*
19615 * List the head of the function: "name(arg1, arg2)".
19616 */
19617 static void
19618list_func_head(fp, indent)
19619 ufunc_T *fp;
19620 int indent;
19621{
19622 int j;
19623
19624 msg_start();
19625 if (indent)
19626 MSG_PUTS(" ");
19627 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019628 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 {
19630 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019631 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 }
19633 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019634 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019636 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 {
19638 if (j)
19639 MSG_PUTS(", ");
19640 msg_puts(FUNCARG(fp, j));
19641 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019642 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 {
19644 if (j)
19645 MSG_PUTS(", ");
19646 MSG_PUTS("...");
19647 }
19648 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019649 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019650 if (p_verbose > 0)
19651 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652}
19653
19654/*
19655 * Find a function by name, return pointer to it in ufuncs.
19656 * Return NULL for unknown function.
19657 */
19658 static ufunc_T *
19659find_func(name)
19660 char_u *name;
19661{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019662 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019664 hi = hash_find(&func_hashtab, name);
19665 if (!HASHITEM_EMPTY(hi))
19666 return HI2UF(hi);
19667 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668}
19669
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019670#if defined(EXITFREE) || defined(PROTO)
19671 void
19672free_all_functions()
19673{
19674 hashitem_T *hi;
19675
19676 /* Need to start all over every time, because func_free() may change the
19677 * hash table. */
19678 while (func_hashtab.ht_used > 0)
19679 for (hi = func_hashtab.ht_array; ; ++hi)
19680 if (!HASHITEM_EMPTY(hi))
19681 {
19682 func_free(HI2UF(hi));
19683 break;
19684 }
19685}
19686#endif
19687
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019688/*
19689 * Return TRUE if a function "name" exists.
19690 */
19691 static int
19692function_exists(name)
19693 char_u *name;
19694{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019695 char_u *nm = name;
19696 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019697 int n = FALSE;
19698
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019699 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019700 nm = skipwhite(nm);
19701
19702 /* Only accept "funcname", "funcname ", "funcname (..." and
19703 * "funcname(...", not "funcname!...". */
19704 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019705 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019706 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019707 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019708 else
19709 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019710 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019711 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019712 return n;
19713}
19714
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019715/*
19716 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019717 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019718 */
19719 static int
19720builtin_function(name)
19721 char_u *name;
19722{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019723 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19724 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019725}
19726
Bram Moolenaar05159a02005-02-26 23:04:13 +000019727#if defined(FEAT_PROFILE) || defined(PROTO)
19728/*
19729 * Start profiling function "fp".
19730 */
19731 static void
19732func_do_profile(fp)
19733 ufunc_T *fp;
19734{
19735 fp->uf_tm_count = 0;
19736 profile_zero(&fp->uf_tm_self);
19737 profile_zero(&fp->uf_tm_total);
19738 if (fp->uf_tml_count == NULL)
19739 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19740 (sizeof(int) * fp->uf_lines.ga_len));
19741 if (fp->uf_tml_total == NULL)
19742 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19743 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19744 if (fp->uf_tml_self == NULL)
19745 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19746 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19747 fp->uf_tml_idx = -1;
19748 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19749 || fp->uf_tml_self == NULL)
19750 return; /* out of memory */
19751
19752 fp->uf_profiling = TRUE;
19753}
19754
19755/*
19756 * Dump the profiling results for all functions in file "fd".
19757 */
19758 void
19759func_dump_profile(fd)
19760 FILE *fd;
19761{
19762 hashitem_T *hi;
19763 int todo;
19764 ufunc_T *fp;
19765 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019766 ufunc_T **sorttab;
19767 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019768
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019769 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019770 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19771
Bram Moolenaar05159a02005-02-26 23:04:13 +000019772 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19773 {
19774 if (!HASHITEM_EMPTY(hi))
19775 {
19776 --todo;
19777 fp = HI2UF(hi);
19778 if (fp->uf_profiling)
19779 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019780 if (sorttab != NULL)
19781 sorttab[st_len++] = fp;
19782
Bram Moolenaar05159a02005-02-26 23:04:13 +000019783 if (fp->uf_name[0] == K_SPECIAL)
19784 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19785 else
19786 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19787 if (fp->uf_tm_count == 1)
19788 fprintf(fd, "Called 1 time\n");
19789 else
19790 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19791 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19792 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19793 fprintf(fd, "\n");
19794 fprintf(fd, "count total (s) self (s)\n");
19795
19796 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19797 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019798 if (FUNCLINE(fp, i) == NULL)
19799 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019800 prof_func_line(fd, fp->uf_tml_count[i],
19801 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019802 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19803 }
19804 fprintf(fd, "\n");
19805 }
19806 }
19807 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019808
19809 if (sorttab != NULL && st_len > 0)
19810 {
19811 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19812 prof_total_cmp);
19813 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19814 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19815 prof_self_cmp);
19816 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19817 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019818}
Bram Moolenaar73830342005-02-28 22:48:19 +000019819
19820 static void
19821prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19822 FILE *fd;
19823 ufunc_T **sorttab;
19824 int st_len;
19825 char *title;
19826 int prefer_self; /* when equal print only self time */
19827{
19828 int i;
19829 ufunc_T *fp;
19830
19831 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19832 fprintf(fd, "count total (s) self (s) function\n");
19833 for (i = 0; i < 20 && i < st_len; ++i)
19834 {
19835 fp = sorttab[i];
19836 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19837 prefer_self);
19838 if (fp->uf_name[0] == K_SPECIAL)
19839 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19840 else
19841 fprintf(fd, " %s()\n", fp->uf_name);
19842 }
19843 fprintf(fd, "\n");
19844}
19845
19846/*
19847 * Print the count and times for one function or function line.
19848 */
19849 static void
19850prof_func_line(fd, count, total, self, prefer_self)
19851 FILE *fd;
19852 int count;
19853 proftime_T *total;
19854 proftime_T *self;
19855 int prefer_self; /* when equal print only self time */
19856{
19857 if (count > 0)
19858 {
19859 fprintf(fd, "%5d ", count);
19860 if (prefer_self && profile_equal(total, self))
19861 fprintf(fd, " ");
19862 else
19863 fprintf(fd, "%s ", profile_msg(total));
19864 if (!prefer_self && profile_equal(total, self))
19865 fprintf(fd, " ");
19866 else
19867 fprintf(fd, "%s ", profile_msg(self));
19868 }
19869 else
19870 fprintf(fd, " ");
19871}
19872
19873/*
19874 * Compare function for total time sorting.
19875 */
19876 static int
19877#ifdef __BORLANDC__
19878_RTLENTRYF
19879#endif
19880prof_total_cmp(s1, s2)
19881 const void *s1;
19882 const void *s2;
19883{
19884 ufunc_T *p1, *p2;
19885
19886 p1 = *(ufunc_T **)s1;
19887 p2 = *(ufunc_T **)s2;
19888 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19889}
19890
19891/*
19892 * Compare function for self time sorting.
19893 */
19894 static int
19895#ifdef __BORLANDC__
19896_RTLENTRYF
19897#endif
19898prof_self_cmp(s1, s2)
19899 const void *s1;
19900 const void *s2;
19901{
19902 ufunc_T *p1, *p2;
19903
19904 p1 = *(ufunc_T **)s1;
19905 p2 = *(ufunc_T **)s2;
19906 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19907}
19908
Bram Moolenaar05159a02005-02-26 23:04:13 +000019909#endif
19910
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019911/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019912 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019913 * Return TRUE if a package was loaded.
19914 */
19915 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019916script_autoload(name, reload)
19917 char_u *name;
19918 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019919{
19920 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019921 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019922 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019923 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019924
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019925 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019926 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019927 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019928 return FALSE;
19929
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019930 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019931
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019932 /* Find the name in the list of previously loaded package names. Skip
19933 * "autoload/", it's always the same. */
19934 for (i = 0; i < ga_loaded.ga_len; ++i)
19935 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19936 break;
19937 if (!reload && i < ga_loaded.ga_len)
19938 ret = FALSE; /* was loaded already */
19939 else
19940 {
19941 /* Remember the name if it wasn't loaded already. */
19942 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19943 {
19944 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19945 tofree = NULL;
19946 }
19947
19948 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019949 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019950 ret = TRUE;
19951 }
19952
19953 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019954 return ret;
19955}
19956
19957/*
19958 * Return the autoload script name for a function or variable name.
19959 * Returns NULL when out of memory.
19960 */
19961 static char_u *
19962autoload_name(name)
19963 char_u *name;
19964{
19965 char_u *p;
19966 char_u *scriptname;
19967
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019968 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019969 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19970 if (scriptname == NULL)
19971 return FALSE;
19972 STRCPY(scriptname, "autoload/");
19973 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019974 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019975 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019976 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019977 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019978 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019979}
19980
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19982
19983/*
19984 * Function given to ExpandGeneric() to obtain the list of user defined
19985 * function names.
19986 */
19987 char_u *
19988get_user_func_name(xp, idx)
19989 expand_T *xp;
19990 int idx;
19991{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019992 static long_u done;
19993 static hashitem_T *hi;
19994 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995
19996 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019998 done = 0;
19999 hi = func_hashtab.ht_array;
20000 }
20001 if (done < func_hashtab.ht_used)
20002 {
20003 if (done++ > 0)
20004 ++hi;
20005 while (HASHITEM_EMPTY(hi))
20006 ++hi;
20007 fp = HI2UF(hi);
20008
20009 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20010 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011
20012 cat_func_name(IObuff, fp);
20013 if (xp->xp_context != EXPAND_USER_FUNC)
20014 {
20015 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020016 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 STRCAT(IObuff, ")");
20018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 return IObuff;
20020 }
20021 return NULL;
20022}
20023
20024#endif /* FEAT_CMDL_COMPL */
20025
20026/*
20027 * Copy the function name of "fp" to buffer "buf".
20028 * "buf" must be able to hold the function name plus three bytes.
20029 * Takes care of script-local function names.
20030 */
20031 static void
20032cat_func_name(buf, fp)
20033 char_u *buf;
20034 ufunc_T *fp;
20035{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020036 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 {
20038 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020039 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 }
20041 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020042 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043}
20044
20045/*
20046 * ":delfunction {name}"
20047 */
20048 void
20049ex_delfunction(eap)
20050 exarg_T *eap;
20051{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020052 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 char_u *p;
20054 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056
20057 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020058 name = trans_function_name(&p, eap->skip, 0, &fudi);
20059 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020061 {
20062 if (fudi.fd_dict != NULL && !eap->skip)
20063 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 if (!ends_excmd(*skipwhite(p)))
20067 {
20068 vim_free(name);
20069 EMSG(_(e_trailing));
20070 return;
20071 }
20072 eap->nextcmd = check_nextcmd(p);
20073 if (eap->nextcmd != NULL)
20074 *p = NUL;
20075
20076 if (!eap->skip)
20077 fp = find_func(name);
20078 vim_free(name);
20079
20080 if (!eap->skip)
20081 {
20082 if (fp == NULL)
20083 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020084 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 return;
20086 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020087 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 {
20089 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20090 return;
20091 }
20092
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020093 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020095 /* Delete the dict item that refers to the function, it will
20096 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020097 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020099 else
20100 func_free(fp);
20101 }
20102}
20103
20104/*
20105 * Free a function and remove it from the list of functions.
20106 */
20107 static void
20108func_free(fp)
20109 ufunc_T *fp;
20110{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020111 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020112
20113 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020114 ga_clear_strings(&(fp->uf_args));
20115 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020116#ifdef FEAT_PROFILE
20117 vim_free(fp->uf_tml_count);
20118 vim_free(fp->uf_tml_total);
20119 vim_free(fp->uf_tml_self);
20120#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020121
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020122 /* remove the function from the function hashtable */
20123 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20124 if (HASHITEM_EMPTY(hi))
20125 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020126 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020127 hash_remove(&func_hashtab, hi);
20128
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020129 vim_free(fp);
20130}
20131
20132/*
20133 * Unreference a Function: decrement the reference count and free it when it
20134 * becomes zero. Only for numbered functions.
20135 */
20136 static void
20137func_unref(name)
20138 char_u *name;
20139{
20140 ufunc_T *fp;
20141
20142 if (name != NULL && isdigit(*name))
20143 {
20144 fp = find_func(name);
20145 if (fp == NULL)
20146 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020147 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020148 {
20149 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020150 * when "uf_calls" becomes zero. */
20151 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020152 func_free(fp);
20153 }
20154 }
20155}
20156
20157/*
20158 * Count a reference to a Function.
20159 */
20160 static void
20161func_ref(name)
20162 char_u *name;
20163{
20164 ufunc_T *fp;
20165
20166 if (name != NULL && isdigit(*name))
20167 {
20168 fp = find_func(name);
20169 if (fp == NULL)
20170 EMSG2(_(e_intern2), "func_ref()");
20171 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020172 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 }
20174}
20175
20176/*
20177 * Call a user function.
20178 */
20179 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020180call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 ufunc_T *fp; /* pointer to function */
20182 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020183 typval_T *argvars; /* arguments */
20184 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 linenr_T firstline; /* first line of range */
20186 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020187 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188{
Bram Moolenaar33570922005-01-25 22:26:29 +000020189 char_u *save_sourcing_name;
20190 linenr_T save_sourcing_lnum;
20191 scid_T save_current_SID;
20192 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020193 int save_did_emsg;
20194 static int depth = 0;
20195 dictitem_T *v;
20196 int fixvar_idx = 0; /* index in fixvar[] */
20197 int i;
20198 int ai;
20199 char_u numbuf[NUMBUFLEN];
20200 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020201#ifdef FEAT_PROFILE
20202 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020203 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205
20206 /* If depth of calling is getting too high, don't execute the function */
20207 if (depth >= p_mfd)
20208 {
20209 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020210 rettv->v_type = VAR_NUMBER;
20211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 return;
20213 }
20214 ++depth;
20215
20216 line_breakcheck(); /* check for CTRL-C hit */
20217
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020218 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020219 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020221 fc.rettv = rettv;
20222 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 fc.linenr = 0;
20224 fc.returned = FALSE;
20225 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020227 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 fc.dbg_tick = debug_tick;
20229
Bram Moolenaar33570922005-01-25 22:26:29 +000020230 /*
20231 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20232 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20233 * each argument variable and saves a lot of time.
20234 */
20235 /*
20236 * Init l: variables.
20237 */
20238 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020239 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020240 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020241 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20242 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020244 name = v->di_key;
20245 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020246 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20247 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20248 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020249 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020250 v->di_tv.vval.v_dict = selfdict;
20251 ++selfdict->dv_refcount;
20252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020253
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 /*
20255 * Init a: variables.
20256 * Set a:0 to "argcount".
20257 * Set a:000 to a list with room for the "..." arguments.
20258 */
20259 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20260 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020261 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020262 v = &fc.fixvar[fixvar_idx++].var;
20263 STRCPY(v->di_key, "000");
20264 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20265 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20266 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020267 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020268 v->di_tv.vval.v_list = &fc.l_varlist;
20269 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20270 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020271 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020272
20273 /*
20274 * Set a:firstline to "firstline" and a:lastline to "lastline".
20275 * Set a:name to named arguments.
20276 * Set a:N to the "..." arguments.
20277 */
20278 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20279 (varnumber_T)firstline);
20280 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20281 (varnumber_T)lastline);
20282 for (i = 0; i < argcount; ++i)
20283 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020284 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020285 if (ai < 0)
20286 /* named argument a:name */
20287 name = FUNCARG(fp, i);
20288 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020289 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020290 /* "..." argument a:1, a:2, etc. */
20291 sprintf((char *)numbuf, "%d", ai + 1);
20292 name = numbuf;
20293 }
20294 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20295 {
20296 v = &fc.fixvar[fixvar_idx++].var;
20297 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20298 }
20299 else
20300 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020301 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20302 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 if (v == NULL)
20304 break;
20305 v->di_flags = DI_FLAGS_RO;
20306 }
20307 STRCPY(v->di_key, name);
20308 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20309
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020310 /* Note: the values are copied directly to avoid alloc/free.
20311 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020312 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020313 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020314
20315 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20316 {
20317 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20318 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020319 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020320 }
20321 }
20322
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 /* Don't redraw while executing the function. */
20324 ++RedrawingDisabled;
20325 save_sourcing_name = sourcing_name;
20326 save_sourcing_lnum = sourcing_lnum;
20327 sourcing_lnum = 1;
20328 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020329 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 if (sourcing_name != NULL)
20331 {
20332 if (save_sourcing_name != NULL
20333 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20334 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20335 else
20336 STRCPY(sourcing_name, "function ");
20337 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20338
20339 if (p_verbose >= 12)
20340 {
20341 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020342 verbose_enter_scroll();
20343
Bram Moolenaar555b2802005-05-19 21:08:39 +000020344 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 if (p_verbose >= 14)
20346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020348 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020349 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020350 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351
20352 msg_puts((char_u *)"(");
20353 for (i = 0; i < argcount; ++i)
20354 {
20355 if (i > 0)
20356 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020357 if (argvars[i].v_type == VAR_NUMBER)
20358 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 else
20360 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020361 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20362 if (s != NULL)
20363 {
20364 trunc_string(s, buf, MSG_BUF_CLEN);
20365 msg_puts(buf);
20366 vim_free(tofree);
20367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 }
20369 }
20370 msg_puts((char_u *)")");
20371 }
20372 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020373
20374 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 --no_wait_return;
20376 }
20377 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020378#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020379 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020380 {
20381 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20382 func_do_profile(fp);
20383 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020384 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020385 {
20386 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020387 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020388 profile_zero(&fp->uf_tm_children);
20389 }
20390 script_prof_save(&wait_start);
20391 }
20392#endif
20393
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020395 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 save_did_emsg = did_emsg;
20397 did_emsg = FALSE;
20398
20399 /* call do_cmdline() to execute the lines */
20400 do_cmdline(NULL, get_func_line, (void *)&fc,
20401 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20402
20403 --RedrawingDisabled;
20404
20405 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020406 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020408 clear_tv(rettv);
20409 rettv->v_type = VAR_NUMBER;
20410 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 }
20412
Bram Moolenaar05159a02005-02-26 23:04:13 +000020413#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020414 if (do_profiling == PROF_YES && (fp->uf_profiling
20415 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020416 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020417 profile_end(&call_start);
20418 profile_sub_wait(&wait_start, &call_start);
20419 profile_add(&fp->uf_tm_total, &call_start);
20420 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020421 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020422 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020423 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20424 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020425 }
20426 }
20427#endif
20428
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 /* when being verbose, mention the return value */
20430 if (p_verbose >= 12)
20431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020433 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020436 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020437 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020438 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20439 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020442 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020443 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020444 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020445 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020446
Bram Moolenaar555b2802005-05-19 21:08:39 +000020447 /* The value may be very long. Skip the middle part, so that we
20448 * have some idea how it starts and ends. smsg() would always
20449 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020450 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20451 if (s != NULL)
20452 {
20453 trunc_string(s, buf, MSG_BUF_CLEN);
20454 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20455 vim_free(tofree);
20456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 }
20458 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020459
20460 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 --no_wait_return;
20462 }
20463
20464 vim_free(sourcing_name);
20465 sourcing_name = save_sourcing_name;
20466 sourcing_lnum = save_sourcing_lnum;
20467 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020468#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020469 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020470 script_prof_restore(&wait_start);
20471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472
20473 if (p_verbose >= 12 && sourcing_name != NULL)
20474 {
20475 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020476 verbose_enter_scroll();
20477
Bram Moolenaar555b2802005-05-19 21:08:39 +000020478 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020480
20481 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 --no_wait_return;
20483 }
20484
20485 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020486 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487
Bram Moolenaar33570922005-01-25 22:26:29 +000020488 /* The a: variables typevals were not alloced, only free the allocated
20489 * variables. */
20490 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20491
20492 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 --depth;
20494}
20495
20496/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020497 * Add a number variable "name" to dict "dp" with value "nr".
20498 */
20499 static void
20500add_nr_var(dp, v, name, nr)
20501 dict_T *dp;
20502 dictitem_T *v;
20503 char *name;
20504 varnumber_T nr;
20505{
20506 STRCPY(v->di_key, name);
20507 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20508 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20509 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020510 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020511 v->di_tv.vval.v_number = nr;
20512}
20513
20514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 * ":return [expr]"
20516 */
20517 void
20518ex_return(eap)
20519 exarg_T *eap;
20520{
20521 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020522 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 int returning = FALSE;
20524
20525 if (current_funccal == NULL)
20526 {
20527 EMSG(_("E133: :return not inside a function"));
20528 return;
20529 }
20530
20531 if (eap->skip)
20532 ++emsg_skip;
20533
20534 eap->nextcmd = NULL;
20535 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020536 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 {
20538 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020539 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020541 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542 }
20543 /* It's safer to return also on error. */
20544 else if (!eap->skip)
20545 {
20546 /*
20547 * Return unless the expression evaluation has been cancelled due to an
20548 * aborting error, an interrupt, or an exception.
20549 */
20550 if (!aborting())
20551 returning = do_return(eap, FALSE, TRUE, NULL);
20552 }
20553
20554 /* When skipping or the return gets pending, advance to the next command
20555 * in this line (!returning). Otherwise, ignore the rest of the line.
20556 * Following lines will be ignored by get_func_line(). */
20557 if (returning)
20558 eap->nextcmd = NULL;
20559 else if (eap->nextcmd == NULL) /* no argument */
20560 eap->nextcmd = check_nextcmd(arg);
20561
20562 if (eap->skip)
20563 --emsg_skip;
20564}
20565
20566/*
20567 * Return from a function. Possibly makes the return pending. Also called
20568 * for a pending return at the ":endtry" or after returning from an extra
20569 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020570 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020571 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 * FALSE when the return gets pending.
20573 */
20574 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020575do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 exarg_T *eap;
20577 int reanimate;
20578 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020579 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580{
20581 int idx;
20582 struct condstack *cstack = eap->cstack;
20583
20584 if (reanimate)
20585 /* Undo the return. */
20586 current_funccal->returned = FALSE;
20587
20588 /*
20589 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20590 * not in its finally clause (which then is to be executed next) is found.
20591 * In this case, make the ":return" pending for execution at the ":endtry".
20592 * Otherwise, return normally.
20593 */
20594 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20595 if (idx >= 0)
20596 {
20597 cstack->cs_pending[idx] = CSTP_RETURN;
20598
20599 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020600 /* A pending return again gets pending. "rettv" points to an
20601 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020603 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604 else
20605 {
20606 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020607 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020609 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 {
20613 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020614 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020615 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 else
20617 EMSG(_(e_outofmem));
20618 }
20619 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020620 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621
20622 if (reanimate)
20623 {
20624 /* The pending return value could be overwritten by a ":return"
20625 * without argument in a finally clause; reset the default
20626 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020627 current_funccal->rettv->v_type = VAR_NUMBER;
20628 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 }
20630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020631 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 }
20633 else
20634 {
20635 current_funccal->returned = TRUE;
20636
20637 /* If the return is carried out now, store the return value. For
20638 * a return immediately after reanimation, the value is already
20639 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020640 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020642 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020643 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020645 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 }
20647 }
20648
20649 return idx < 0;
20650}
20651
20652/*
20653 * Free the variable with a pending return value.
20654 */
20655 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020656discard_pending_return(rettv)
20657 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658{
Bram Moolenaar33570922005-01-25 22:26:29 +000020659 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660}
20661
20662/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020663 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 * is an allocated string. Used by report_pending() for verbose messages.
20665 */
20666 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020667get_return_cmd(rettv)
20668 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020670 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020671 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020672 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020674 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020675 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020676 if (s == NULL)
20677 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020678
20679 STRCPY(IObuff, ":return ");
20680 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20681 if (STRLEN(s) + 8 >= IOSIZE)
20682 STRCPY(IObuff + IOSIZE - 4, "...");
20683 vim_free(tofree);
20684 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685}
20686
20687/*
20688 * Get next function line.
20689 * Called by do_cmdline() to get the next line.
20690 * Returns allocated string, or NULL for end of function.
20691 */
20692/* ARGSUSED */
20693 char_u *
20694get_func_line(c, cookie, indent)
20695 int c; /* not used */
20696 void *cookie;
20697 int indent; /* not used */
20698{
Bram Moolenaar33570922005-01-25 22:26:29 +000020699 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020700 ufunc_T *fp = fcp->func;
20701 char_u *retval;
20702 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020703
20704 /* If breakpoints have been added/deleted need to check for it. */
20705 if (fcp->dbg_tick != debug_tick)
20706 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020707 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 sourcing_lnum);
20709 fcp->dbg_tick = debug_tick;
20710 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020711#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020712 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020713 func_line_end(cookie);
20714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715
Bram Moolenaar05159a02005-02-26 23:04:13 +000020716 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020717 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20718 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 retval = NULL;
20720 else
20721 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020722 /* Skip NULL lines (continuation lines). */
20723 while (fcp->linenr < gap->ga_len
20724 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20725 ++fcp->linenr;
20726 if (fcp->linenr >= gap->ga_len)
20727 retval = NULL;
20728 else
20729 {
20730 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20731 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020732#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020733 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020734 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020735#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 }
20738
20739 /* Did we encounter a breakpoint? */
20740 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20741 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020742 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020744 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 sourcing_lnum);
20746 fcp->dbg_tick = debug_tick;
20747 }
20748
20749 return retval;
20750}
20751
Bram Moolenaar05159a02005-02-26 23:04:13 +000020752#if defined(FEAT_PROFILE) || defined(PROTO)
20753/*
20754 * Called when starting to read a function line.
20755 * "sourcing_lnum" must be correct!
20756 * When skipping lines it may not actually be executed, but we won't find out
20757 * until later and we need to store the time now.
20758 */
20759 void
20760func_line_start(cookie)
20761 void *cookie;
20762{
20763 funccall_T *fcp = (funccall_T *)cookie;
20764 ufunc_T *fp = fcp->func;
20765
20766 if (fp->uf_profiling && sourcing_lnum >= 1
20767 && sourcing_lnum <= fp->uf_lines.ga_len)
20768 {
20769 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020770 /* Skip continuation lines. */
20771 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20772 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020773 fp->uf_tml_execed = FALSE;
20774 profile_start(&fp->uf_tml_start);
20775 profile_zero(&fp->uf_tml_children);
20776 profile_get_wait(&fp->uf_tml_wait);
20777 }
20778}
20779
20780/*
20781 * Called when actually executing a function line.
20782 */
20783 void
20784func_line_exec(cookie)
20785 void *cookie;
20786{
20787 funccall_T *fcp = (funccall_T *)cookie;
20788 ufunc_T *fp = fcp->func;
20789
20790 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20791 fp->uf_tml_execed = TRUE;
20792}
20793
20794/*
20795 * Called when done with a function line.
20796 */
20797 void
20798func_line_end(cookie)
20799 void *cookie;
20800{
20801 funccall_T *fcp = (funccall_T *)cookie;
20802 ufunc_T *fp = fcp->func;
20803
20804 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20805 {
20806 if (fp->uf_tml_execed)
20807 {
20808 ++fp->uf_tml_count[fp->uf_tml_idx];
20809 profile_end(&fp->uf_tml_start);
20810 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020811 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020812 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20813 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020814 }
20815 fp->uf_tml_idx = -1;
20816 }
20817}
20818#endif
20819
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820/*
20821 * Return TRUE if the currently active function should be ended, because a
20822 * return was encountered or an error occured. Used inside a ":while".
20823 */
20824 int
20825func_has_ended(cookie)
20826 void *cookie;
20827{
Bram Moolenaar33570922005-01-25 22:26:29 +000020828 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829
20830 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20831 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020832 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 || fcp->returned);
20834}
20835
20836/*
20837 * return TRUE if cookie indicates a function which "abort"s on errors.
20838 */
20839 int
20840func_has_abort(cookie)
20841 void *cookie;
20842{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020843 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844}
20845
20846#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20847typedef enum
20848{
20849 VAR_FLAVOUR_DEFAULT,
20850 VAR_FLAVOUR_SESSION,
20851 VAR_FLAVOUR_VIMINFO
20852} var_flavour_T;
20853
20854static var_flavour_T var_flavour __ARGS((char_u *varname));
20855
20856 static var_flavour_T
20857var_flavour(varname)
20858 char_u *varname;
20859{
20860 char_u *p = varname;
20861
20862 if (ASCII_ISUPPER(*p))
20863 {
20864 while (*(++p))
20865 if (ASCII_ISLOWER(*p))
20866 return VAR_FLAVOUR_SESSION;
20867 return VAR_FLAVOUR_VIMINFO;
20868 }
20869 else
20870 return VAR_FLAVOUR_DEFAULT;
20871}
20872#endif
20873
20874#if defined(FEAT_VIMINFO) || defined(PROTO)
20875/*
20876 * Restore global vars that start with a capital from the viminfo file
20877 */
20878 int
20879read_viminfo_varlist(virp, writing)
20880 vir_T *virp;
20881 int writing;
20882{
20883 char_u *tab;
20884 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020885 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886
20887 if (!writing && (find_viminfo_parameter('!') != NULL))
20888 {
20889 tab = vim_strchr(virp->vir_line + 1, '\t');
20890 if (tab != NULL)
20891 {
20892 *tab++ = '\0'; /* isolate the variable name */
20893 if (*tab == 'S') /* string var */
20894 is_string = TRUE;
20895
20896 tab = vim_strchr(tab, '\t');
20897 if (tab != NULL)
20898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 if (is_string)
20900 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020901 tv.v_type = VAR_STRING;
20902 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 }
20905 else
20906 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020907 tv.v_type = VAR_NUMBER;
20908 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020910 set_var(virp->vir_line + 1, &tv, FALSE);
20911 if (is_string)
20912 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 }
20914 }
20915 }
20916
20917 return viminfo_readline(virp);
20918}
20919
20920/*
20921 * Write global vars that start with a capital to the viminfo file
20922 */
20923 void
20924write_viminfo_varlist(fp)
20925 FILE *fp;
20926{
Bram Moolenaar33570922005-01-25 22:26:29 +000020927 hashitem_T *hi;
20928 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020929 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020930 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020931 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020932 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020933 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934
20935 if (find_viminfo_parameter('!') == NULL)
20936 return;
20937
20938 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020939
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020940 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020941 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020943 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020945 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020946 this_var = HI2DI(hi);
20947 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020948 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020949 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020950 {
20951 case VAR_STRING: s = "STR"; break;
20952 case VAR_NUMBER: s = "NUM"; break;
20953 default: continue;
20954 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020955 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020956 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020957 if (p != NULL)
20958 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020959 vim_free(tofree);
20960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961 }
20962 }
20963}
20964#endif
20965
20966#if defined(FEAT_SESSION) || defined(PROTO)
20967 int
20968store_session_globals(fd)
20969 FILE *fd;
20970{
Bram Moolenaar33570922005-01-25 22:26:29 +000020971 hashitem_T *hi;
20972 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020973 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 char_u *p, *t;
20975
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020976 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020977 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020979 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020981 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020982 this_var = HI2DI(hi);
20983 if ((this_var->di_tv.v_type == VAR_NUMBER
20984 || this_var->di_tv.v_type == VAR_STRING)
20985 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020986 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020987 /* Escape special characters with a backslash. Turn a LF and
20988 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020989 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020990 (char_u *)"\\\"\n\r");
20991 if (p == NULL) /* out of memory */
20992 break;
20993 for (t = p; *t != NUL; ++t)
20994 if (*t == '\n')
20995 *t = 'n';
20996 else if (*t == '\r')
20997 *t = 'r';
20998 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020999 this_var->di_key,
21000 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21001 : ' ',
21002 p,
21003 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21004 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021005 || put_eol(fd) == FAIL)
21006 {
21007 vim_free(p);
21008 return FAIL;
21009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 vim_free(p);
21011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 }
21013 }
21014 return OK;
21015}
21016#endif
21017
Bram Moolenaar661b1822005-07-28 22:36:45 +000021018/*
21019 * Display script name where an item was last set.
21020 * Should only be invoked when 'verbose' is non-zero.
21021 */
21022 void
21023last_set_msg(scriptID)
21024 scid_T scriptID;
21025{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021026 char_u *p;
21027
Bram Moolenaar661b1822005-07-28 22:36:45 +000021028 if (scriptID != 0)
21029 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021030 p = home_replace_save(NULL, get_scriptname(scriptID));
21031 if (p != NULL)
21032 {
21033 verbose_enter();
21034 MSG_PUTS(_("\n\tLast set from "));
21035 MSG_PUTS(p);
21036 vim_free(p);
21037 verbose_leave();
21038 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021039 }
21040}
21041
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042#endif /* FEAT_EVAL */
21043
21044#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21045
21046
21047#ifdef WIN3264
21048/*
21049 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21050 */
21051static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21052static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21053static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21054
21055/*
21056 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021057 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 */
21059 static int
21060get_short_pathname(fnamep, bufp, fnamelen)
21061 char_u **fnamep;
21062 char_u **bufp;
21063 int *fnamelen;
21064{
21065 int l,len;
21066 char_u *newbuf;
21067
21068 len = *fnamelen;
21069
21070 l = GetShortPathName(*fnamep, *fnamep, len);
21071 if (l > len - 1)
21072 {
21073 /* If that doesn't work (not enough space), then save the string
21074 * and try again with a new buffer big enough
21075 */
21076 newbuf = vim_strnsave(*fnamep, l);
21077 if (newbuf == NULL)
21078 return 0;
21079
21080 vim_free(*bufp);
21081 *fnamep = *bufp = newbuf;
21082
21083 l = GetShortPathName(*fnamep,*fnamep,l+1);
21084
21085 /* Really should always succeed, as the buffer is big enough */
21086 }
21087
21088 *fnamelen = l;
21089 return 1;
21090}
21091
21092/*
21093 * Create a short path name. Returns the length of the buffer it needs.
21094 * Doesn't copy over the end of the buffer passed in.
21095 */
21096 static int
21097shortpath_for_invalid_fname(fname, bufp, fnamelen)
21098 char_u **fname;
21099 char_u **bufp;
21100 int *fnamelen;
21101{
21102 char_u *s, *p, *pbuf2, *pbuf3;
21103 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021104 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105
21106 /* Make a copy */
21107 len2 = *fnamelen;
21108 pbuf2 = vim_strnsave(*fname, len2);
21109 pbuf3 = NULL;
21110
21111 s = pbuf2 + len2 - 1; /* Find the end */
21112 slen = 1;
21113 plen = len2;
21114
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021115 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 {
21117 --s;
21118 ++slen;
21119 --plen;
21120 }
21121
21122 do
21123 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021124 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021125 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 {
21127 --s;
21128 ++slen;
21129 --plen;
21130 }
21131 if (s <= pbuf2)
21132 break;
21133
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021134 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 ch = *s;
21136 *s = 0; /* get_short_pathname requires a null-terminated string */
21137
21138 /* Try it in situ */
21139 p = pbuf2;
21140 if (!get_short_pathname(&p, &pbuf3, &plen))
21141 {
21142 vim_free(pbuf2);
21143 return -1;
21144 }
21145 *s = ch; /* Preserve the string */
21146 } while (plen == 0);
21147
21148 if (plen > 0)
21149 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021150 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 *fnamelen = len = plen + slen;
21152 vim_free(*bufp);
21153 if (len > len2)
21154 {
21155 /* If there's not enough space in the currently allocated string,
21156 * then copy it to a buffer big enough.
21157 */
21158 *fname= *bufp = vim_strnsave(p, len);
21159 if (*fname == NULL)
21160 return -1;
21161 }
21162 else
21163 {
21164 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21165 *fname = *bufp = pbuf2;
21166 if (p != pbuf2)
21167 strncpy(*fname, p, plen);
21168 pbuf2 = NULL;
21169 }
21170 /* Concat the next bit */
21171 strncpy(*fname + plen, s, slen);
21172 (*fname)[len] = '\0';
21173 }
21174 vim_free(pbuf3);
21175 vim_free(pbuf2);
21176 return 0;
21177}
21178
21179/*
21180 * Get a pathname for a partial path.
21181 */
21182 static int
21183shortpath_for_partial(fnamep, bufp, fnamelen)
21184 char_u **fnamep;
21185 char_u **bufp;
21186 int *fnamelen;
21187{
21188 int sepcount, len, tflen;
21189 char_u *p;
21190 char_u *pbuf, *tfname;
21191 int hasTilde;
21192
21193 /* Count up the path seperators from the RHS.. so we know which part
21194 * of the path to return.
21195 */
21196 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021197 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 if (vim_ispathsep(*p))
21199 ++sepcount;
21200
21201 /* Need full path first (use expand_env() to remove a "~/") */
21202 hasTilde = (**fnamep == '~');
21203 if (hasTilde)
21204 pbuf = tfname = expand_env_save(*fnamep);
21205 else
21206 pbuf = tfname = FullName_save(*fnamep, FALSE);
21207
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021208 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209
21210 if (!get_short_pathname(&tfname, &pbuf, &len))
21211 return -1;
21212
21213 if (len == 0)
21214 {
21215 /* Don't have a valid filename, so shorten the rest of the
21216 * path if we can. This CAN give us invalid 8.3 filenames, but
21217 * there's not a lot of point in guessing what it might be.
21218 */
21219 len = tflen;
21220 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21221 return -1;
21222 }
21223
21224 /* Count the paths backward to find the beginning of the desired string. */
21225 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021226 {
21227#ifdef FEAT_MBYTE
21228 if (has_mbyte)
21229 p -= mb_head_off(tfname, p);
21230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 if (vim_ispathsep(*p))
21232 {
21233 if (sepcount == 0 || (hasTilde && sepcount == 1))
21234 break;
21235 else
21236 sepcount --;
21237 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 if (hasTilde)
21240 {
21241 --p;
21242 if (p >= tfname)
21243 *p = '~';
21244 else
21245 return -1;
21246 }
21247 else
21248 ++p;
21249
21250 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21251 vim_free(*bufp);
21252 *fnamelen = (int)STRLEN(p);
21253 *bufp = pbuf;
21254 *fnamep = p;
21255
21256 return 0;
21257}
21258#endif /* WIN3264 */
21259
21260/*
21261 * Adjust a filename, according to a string of modifiers.
21262 * *fnamep must be NUL terminated when called. When returning, the length is
21263 * determined by *fnamelen.
21264 * Returns valid flags.
21265 * When there is an error, *fnamep is set to NULL.
21266 */
21267 int
21268modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21269 char_u *src; /* string with modifiers */
21270 int *usedlen; /* characters after src that are used */
21271 char_u **fnamep; /* file name so far */
21272 char_u **bufp; /* buffer for allocated file name or NULL */
21273 int *fnamelen; /* length of fnamep */
21274{
21275 int valid = 0;
21276 char_u *tail;
21277 char_u *s, *p, *pbuf;
21278 char_u dirname[MAXPATHL];
21279 int c;
21280 int has_fullname = 0;
21281#ifdef WIN3264
21282 int has_shortname = 0;
21283#endif
21284
21285repeat:
21286 /* ":p" - full path/file_name */
21287 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21288 {
21289 has_fullname = 1;
21290
21291 valid |= VALID_PATH;
21292 *usedlen += 2;
21293
21294 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21295 if ((*fnamep)[0] == '~'
21296#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21297 && ((*fnamep)[1] == '/'
21298# ifdef BACKSLASH_IN_FILENAME
21299 || (*fnamep)[1] == '\\'
21300# endif
21301 || (*fnamep)[1] == NUL)
21302
21303#endif
21304 )
21305 {
21306 *fnamep = expand_env_save(*fnamep);
21307 vim_free(*bufp); /* free any allocated file name */
21308 *bufp = *fnamep;
21309 if (*fnamep == NULL)
21310 return -1;
21311 }
21312
21313 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021314 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 {
21316 if (vim_ispathsep(*p)
21317 && p[1] == '.'
21318 && (p[2] == NUL
21319 || vim_ispathsep(p[2])
21320 || (p[2] == '.'
21321 && (p[3] == NUL || vim_ispathsep(p[3])))))
21322 break;
21323 }
21324
21325 /* FullName_save() is slow, don't use it when not needed. */
21326 if (*p != NUL || !vim_isAbsName(*fnamep))
21327 {
21328 *fnamep = FullName_save(*fnamep, *p != NUL);
21329 vim_free(*bufp); /* free any allocated file name */
21330 *bufp = *fnamep;
21331 if (*fnamep == NULL)
21332 return -1;
21333 }
21334
21335 /* Append a path separator to a directory. */
21336 if (mch_isdir(*fnamep))
21337 {
21338 /* Make room for one or two extra characters. */
21339 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21340 vim_free(*bufp); /* free any allocated file name */
21341 *bufp = *fnamep;
21342 if (*fnamep == NULL)
21343 return -1;
21344 add_pathsep(*fnamep);
21345 }
21346 }
21347
21348 /* ":." - path relative to the current directory */
21349 /* ":~" - path relative to the home directory */
21350 /* ":8" - shortname path - postponed till after */
21351 while (src[*usedlen] == ':'
21352 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21353 {
21354 *usedlen += 2;
21355 if (c == '8')
21356 {
21357#ifdef WIN3264
21358 has_shortname = 1; /* Postpone this. */
21359#endif
21360 continue;
21361 }
21362 pbuf = NULL;
21363 /* Need full path first (use expand_env() to remove a "~/") */
21364 if (!has_fullname)
21365 {
21366 if (c == '.' && **fnamep == '~')
21367 p = pbuf = expand_env_save(*fnamep);
21368 else
21369 p = pbuf = FullName_save(*fnamep, FALSE);
21370 }
21371 else
21372 p = *fnamep;
21373
21374 has_fullname = 0;
21375
21376 if (p != NULL)
21377 {
21378 if (c == '.')
21379 {
21380 mch_dirname(dirname, MAXPATHL);
21381 s = shorten_fname(p, dirname);
21382 if (s != NULL)
21383 {
21384 *fnamep = s;
21385 if (pbuf != NULL)
21386 {
21387 vim_free(*bufp); /* free any allocated file name */
21388 *bufp = pbuf;
21389 pbuf = NULL;
21390 }
21391 }
21392 }
21393 else
21394 {
21395 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21396 /* Only replace it when it starts with '~' */
21397 if (*dirname == '~')
21398 {
21399 s = vim_strsave(dirname);
21400 if (s != NULL)
21401 {
21402 *fnamep = s;
21403 vim_free(*bufp);
21404 *bufp = s;
21405 }
21406 }
21407 }
21408 vim_free(pbuf);
21409 }
21410 }
21411
21412 tail = gettail(*fnamep);
21413 *fnamelen = (int)STRLEN(*fnamep);
21414
21415 /* ":h" - head, remove "/file_name", can be repeated */
21416 /* Don't remove the first "/" or "c:\" */
21417 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21418 {
21419 valid |= VALID_HEAD;
21420 *usedlen += 2;
21421 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021422 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021423 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 *fnamelen = (int)(tail - *fnamep);
21425#ifdef VMS
21426 if (*fnamelen > 0)
21427 *fnamelen += 1; /* the path separator is part of the path */
21428#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021429 if (*fnamelen == 0)
21430 {
21431 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21432 p = vim_strsave((char_u *)".");
21433 if (p == NULL)
21434 return -1;
21435 vim_free(*bufp);
21436 *bufp = *fnamep = tail = p;
21437 *fnamelen = 1;
21438 }
21439 else
21440 {
21441 while (tail > s && !after_pathsep(s, tail))
21442 mb_ptr_back(*fnamep, tail);
21443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444 }
21445
21446 /* ":8" - shortname */
21447 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21448 {
21449 *usedlen += 2;
21450#ifdef WIN3264
21451 has_shortname = 1;
21452#endif
21453 }
21454
21455#ifdef WIN3264
21456 /* Check shortname after we have done 'heads' and before we do 'tails'
21457 */
21458 if (has_shortname)
21459 {
21460 pbuf = NULL;
21461 /* Copy the string if it is shortened by :h */
21462 if (*fnamelen < (int)STRLEN(*fnamep))
21463 {
21464 p = vim_strnsave(*fnamep, *fnamelen);
21465 if (p == 0)
21466 return -1;
21467 vim_free(*bufp);
21468 *bufp = *fnamep = p;
21469 }
21470
21471 /* Split into two implementations - makes it easier. First is where
21472 * there isn't a full name already, second is where there is.
21473 */
21474 if (!has_fullname && !vim_isAbsName(*fnamep))
21475 {
21476 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21477 return -1;
21478 }
21479 else
21480 {
21481 int l;
21482
21483 /* Simple case, already have the full-name
21484 * Nearly always shorter, so try first time. */
21485 l = *fnamelen;
21486 if (!get_short_pathname(fnamep, bufp, &l))
21487 return -1;
21488
21489 if (l == 0)
21490 {
21491 /* Couldn't find the filename.. search the paths.
21492 */
21493 l = *fnamelen;
21494 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21495 return -1;
21496 }
21497 *fnamelen = l;
21498 }
21499 }
21500#endif /* WIN3264 */
21501
21502 /* ":t" - tail, just the basename */
21503 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21504 {
21505 *usedlen += 2;
21506 *fnamelen -= (int)(tail - *fnamep);
21507 *fnamep = tail;
21508 }
21509
21510 /* ":e" - extension, can be repeated */
21511 /* ":r" - root, without extension, can be repeated */
21512 while (src[*usedlen] == ':'
21513 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21514 {
21515 /* find a '.' in the tail:
21516 * - for second :e: before the current fname
21517 * - otherwise: The last '.'
21518 */
21519 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21520 s = *fnamep - 2;
21521 else
21522 s = *fnamep + *fnamelen - 1;
21523 for ( ; s > tail; --s)
21524 if (s[0] == '.')
21525 break;
21526 if (src[*usedlen + 1] == 'e') /* :e */
21527 {
21528 if (s > tail)
21529 {
21530 *fnamelen += (int)(*fnamep - (s + 1));
21531 *fnamep = s + 1;
21532#ifdef VMS
21533 /* cut version from the extension */
21534 s = *fnamep + *fnamelen - 1;
21535 for ( ; s > *fnamep; --s)
21536 if (s[0] == ';')
21537 break;
21538 if (s > *fnamep)
21539 *fnamelen = s - *fnamep;
21540#endif
21541 }
21542 else if (*fnamep <= tail)
21543 *fnamelen = 0;
21544 }
21545 else /* :r */
21546 {
21547 if (s > tail) /* remove one extension */
21548 *fnamelen = (int)(s - *fnamep);
21549 }
21550 *usedlen += 2;
21551 }
21552
21553 /* ":s?pat?foo?" - substitute */
21554 /* ":gs?pat?foo?" - global substitute */
21555 if (src[*usedlen] == ':'
21556 && (src[*usedlen + 1] == 's'
21557 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21558 {
21559 char_u *str;
21560 char_u *pat;
21561 char_u *sub;
21562 int sep;
21563 char_u *flags;
21564 int didit = FALSE;
21565
21566 flags = (char_u *)"";
21567 s = src + *usedlen + 2;
21568 if (src[*usedlen + 1] == 'g')
21569 {
21570 flags = (char_u *)"g";
21571 ++s;
21572 }
21573
21574 sep = *s++;
21575 if (sep)
21576 {
21577 /* find end of pattern */
21578 p = vim_strchr(s, sep);
21579 if (p != NULL)
21580 {
21581 pat = vim_strnsave(s, (int)(p - s));
21582 if (pat != NULL)
21583 {
21584 s = p + 1;
21585 /* find end of substitution */
21586 p = vim_strchr(s, sep);
21587 if (p != NULL)
21588 {
21589 sub = vim_strnsave(s, (int)(p - s));
21590 str = vim_strnsave(*fnamep, *fnamelen);
21591 if (sub != NULL && str != NULL)
21592 {
21593 *usedlen = (int)(p + 1 - src);
21594 s = do_string_sub(str, pat, sub, flags);
21595 if (s != NULL)
21596 {
21597 *fnamep = s;
21598 *fnamelen = (int)STRLEN(s);
21599 vim_free(*bufp);
21600 *bufp = s;
21601 didit = TRUE;
21602 }
21603 }
21604 vim_free(sub);
21605 vim_free(str);
21606 }
21607 vim_free(pat);
21608 }
21609 }
21610 /* after using ":s", repeat all the modifiers */
21611 if (didit)
21612 goto repeat;
21613 }
21614 }
21615
21616 return valid;
21617}
21618
21619/*
21620 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21621 * "flags" can be "g" to do a global substitute.
21622 * Returns an allocated string, NULL for error.
21623 */
21624 char_u *
21625do_string_sub(str, pat, sub, flags)
21626 char_u *str;
21627 char_u *pat;
21628 char_u *sub;
21629 char_u *flags;
21630{
21631 int sublen;
21632 regmatch_T regmatch;
21633 int i;
21634 int do_all;
21635 char_u *tail;
21636 garray_T ga;
21637 char_u *ret;
21638 char_u *save_cpo;
21639
21640 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21641 save_cpo = p_cpo;
21642 p_cpo = (char_u *)"";
21643
21644 ga_init2(&ga, 1, 200);
21645
21646 do_all = (flags[0] == 'g');
21647
21648 regmatch.rm_ic = p_ic;
21649 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21650 if (regmatch.regprog != NULL)
21651 {
21652 tail = str;
21653 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21654 {
21655 /*
21656 * Get some space for a temporary buffer to do the substitution
21657 * into. It will contain:
21658 * - The text up to where the match is.
21659 * - The substituted text.
21660 * - The text after the match.
21661 */
21662 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21663 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21664 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21665 {
21666 ga_clear(&ga);
21667 break;
21668 }
21669
21670 /* copy the text up to where the match is */
21671 i = (int)(regmatch.startp[0] - tail);
21672 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21673 /* add the substituted text */
21674 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21675 + ga.ga_len + i, TRUE, TRUE, FALSE);
21676 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 /* avoid getting stuck on a match with an empty string */
21678 if (tail == regmatch.endp[0])
21679 {
21680 if (*tail == NUL)
21681 break;
21682 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21683 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 }
21685 else
21686 {
21687 tail = regmatch.endp[0];
21688 if (*tail == NUL)
21689 break;
21690 }
21691 if (!do_all)
21692 break;
21693 }
21694
21695 if (ga.ga_data != NULL)
21696 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21697
21698 vim_free(regmatch.regprog);
21699 }
21700
21701 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21702 ga_clear(&ga);
21703 p_cpo = save_cpo;
21704
21705 return ret;
21706}
21707
21708#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */