blob: 26277fcf1938e09150f2ac848e5f43b204470bb7 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169 proftime_T uf_tm_children; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count; /* nr of times line was executed */
172 proftime_T *uf_tml_total; /* time spend in a line + children */
173 proftime_T *uf_tml_self; /* time spend in a line itself */
174 proftime_T uf_tml_start; /* start time for current line */
175 proftime_T uf_tml_children; /* time spent in children for this line */
176 proftime_T uf_tml_wait; /* start wait time for current line */
177 int uf_tml_idx; /* index of line being timed; -1 if none */
178 int uf_tml_execed; /* line being timed was executed */
179#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000180 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000182 int uf_refcount; /* for numbered function: reference count */
183 char_u uf_name[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
185 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186};
187
188/* function flags */
189#define FC_ABORT 1 /* abort function on error */
190#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000191#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000194 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000196static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000198/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000199static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000344 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000345 {VV_NAME("mouse_win", VAR_NUMBER), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000348};
349
350/* shorthand */
351#define vv_type vv_di.di_tv.v_type
352#define vv_nr vv_di.di_tv.vval.v_number
353#define vv_str vv_di.di_tv.vval.v_string
354#define vv_tv vv_di.di_tv
355
356/*
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
359 */
360static dict_T vimvardict;
361static dictitem_T vimvars_var;
362#define vimvarht vimvardict.dv_hashtab
363
Bram Moolenaara40058a2005-07-11 22:42:07 +0000364static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
365static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
366#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
368#endif
369static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
370static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
371static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
373static void list_glob_vars __ARGS((int *first));
374static void list_buf_vars __ARGS((int *first));
375static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000377static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000378#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_vim_vars __ARGS((int *first));
380static void list_script_vars __ARGS((int *first));
381static void list_func_vars __ARGS((int *first));
382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
384static int check_changedtick __ARGS((char_u *arg));
385static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
386static void clear_lval __ARGS((lval_T *lp));
387static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
388static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
389static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
390static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
391static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
392static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
393static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
394static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
395static void item_lock __ARGS((typval_T *tv, int deep, int lock));
396static int tv_islocked __ARGS((typval_T *tv));
397
Bram Moolenaar33570922005-01-25 22:26:29 +0000398static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
399static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000407static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000412static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *listitem_alloc __ARGS((void));
414static void listitem_free __ARGS((listitem_T *item));
415static void listitem_remove __ARGS((list_T *l, listitem_T *item));
416static long list_len __ARGS((list_T *l));
417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000421static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void list_append __ARGS((list_T *l, listitem_T *item));
424static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_string __ARGS((list_T *l, char_u *str, int len));
426static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000434static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
435static void set_ref_in_list __ARGS((list_T *l, int copyID));
436static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000438static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_alloc __ARGS((char_u *key));
440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
442static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int dict_add __ARGS((dict_T *d, dictitem_T *item));
445static long dict_len __ARGS((dict_T *d));
446static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
450static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static char_u *string_quote __ARGS((char_u *str, int function));
452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
454static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
455static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
456static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
459static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000475static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000478static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000480#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000481static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000482static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
490static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000503static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000517static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000519static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000525static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000533static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000534static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000535static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000538static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000546static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000560static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000566static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000582static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000583static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000584static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000586static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000590#ifdef vim_mkdir
591static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
592#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000596static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000598static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000599static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000601static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000602static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000604static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000615static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000617static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000624static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000626static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000627static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000629static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000631static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000634static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000638static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639#ifdef HAVE_STRFTIME
640static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
642static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000654static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000655static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000656static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000657static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000658static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000660static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000674static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000677static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000679static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000680static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static int get_env_len __ARGS((char_u **arg));
682static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000683static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000684static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
685#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
686#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
687 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000688static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000690static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000691static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
692static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static typval_T *alloc_tv __ARGS((void));
694static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void init_tv __ARGS((typval_T *varp));
696static long get_tv_number __ARGS((typval_T *varp));
697static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000698static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static char_u *get_tv_string __ARGS((typval_T *varp));
700static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000701static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000703static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
705static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
706static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000707static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
708static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
710static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000711static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000712static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000714static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
716static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
717static int eval_fname_script __ARGS((char_u *p));
718static int eval_fname_sid __ARGS((char_u *p));
719static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static ufunc_T *find_func __ARGS((char_u *name));
721static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000722static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000723#ifdef FEAT_PROFILE
724static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000725static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
726static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
727static int
728# ifdef __BORLANDC__
729 _RTLENTRYF
730# endif
731 prof_total_cmp __ARGS((const void *s1, const void *s2));
732static int
733# ifdef __BORLANDC__
734 _RTLENTRYF
735# endif
736 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000737#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000738static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000739static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000740static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void func_free __ARGS((ufunc_T *fp));
742static void func_unref __ARGS((char_u *name));
743static void func_ref __ARGS((char_u *name));
744static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
745static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000746static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
747static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000748static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000749static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000750static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000752/* Character used as separated in autoload function/variable names. */
753#define AUTOLOAD_CHAR '#'
754
Bram Moolenaar33570922005-01-25 22:26:29 +0000755/*
756 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000757 */
758 void
759eval_init()
760{
Bram Moolenaar33570922005-01-25 22:26:29 +0000761 int i;
762 struct vimvar *p;
763
764 init_var_dict(&globvardict, &globvars_var);
765 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000766 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000767 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768
769 for (i = 0; i < VV_LEN; ++i)
770 {
771 p = &vimvars[i];
772 STRCPY(p->vv_di.di_key, p->vv_name);
773 if (p->vv_flags & VV_RO)
774 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
775 else if (p->vv_flags & VV_RO_SBX)
776 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
777 else
778 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779
780 /* add to v: scope dict, unless the value is not always available */
781 if (p->vv_type != VAR_UNKNOWN)
782 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000784 /* add to compat scope dict */
785 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000787}
788
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000789#if defined(EXITFREE) || defined(PROTO)
790 void
791eval_clear()
792{
793 int i;
794 struct vimvar *p;
795
796 for (i = 0; i < VV_LEN; ++i)
797 {
798 p = &vimvars[i];
799 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000800 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000801 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000802 p->vv_di.di_tv.vval.v_string = NULL;
803 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000804 }
805 hash_clear(&vimvarht);
806 hash_clear(&compat_hashtab);
807
808 /* script-local variables */
809 for (i = 1; i <= ga_scripts.ga_len; ++i)
810 vars_clear(&SCRIPT_VARS(i));
811 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000812 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000813
814 /* global variables */
815 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000816
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000817 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000818 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 hash_clear(&func_hashtab);
820
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000821 /* autoloaded script names */
822 ga_clear_strings(&ga_loaded);
823
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000824 /* unreferenced lists and dicts */
825 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000826}
827#endif
828
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * Return the name of the executed function.
831 */
832 char_u *
833func_name(cookie)
834 void *cookie;
835{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000836 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837}
838
839/*
840 * Return the address holding the next breakpoint line for a funccall cookie.
841 */
842 linenr_T *
843func_breakpoint(cookie)
844 void *cookie;
845{
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847}
848
849/*
850 * Return the address holding the debug tick for a funccall cookie.
851 */
852 int *
853func_dbg_tick(cookie)
854 void *cookie;
855{
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
859/*
860 * Return the nesting level for a funccall cookie.
861 */
862 int
863func_level(cookie)
864 void *cookie;
865{
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867}
868
869/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000870funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872/*
873 * Return TRUE when a function was ended by a ":return" command.
874 */
875 int
876current_func_returned()
877{
878 return current_funccal->returned;
879}
880
881
882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * Set an internal variable to a string value. Creates the variable if it does
884 * not already exist.
885 */
886 void
887set_internal_string_var(name, value)
888 char_u *name;
889 char_u *value;
890{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000891 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894 val = vim_strsave(value);
895 if (val != NULL)
896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000897 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000898 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000900 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000901 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903 }
904}
905
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000906static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000907static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908static char_u *redir_endp = NULL;
909static char_u *redir_varname = NULL;
910
911/*
912 * Start recording command output to a variable
913 * Returns OK if successfully completed the setup. FAIL otherwise.
914 */
915 int
916var_redir_start(name, append)
917 char_u *name;
918 int append; /* append to an existing variable */
919{
920 int save_emsg;
921 int err;
922 typval_T tv;
923
924 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000925 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000926 {
927 EMSG(_(e_invarg));
928 return FAIL;
929 }
930
931 redir_varname = vim_strsave(name);
932 if (redir_varname == NULL)
933 return FAIL;
934
935 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
936 if (redir_lval == NULL)
937 {
938 var_redir_stop();
939 return FAIL;
940 }
941
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000942 /* The output is stored in growarray "redir_ga" until redirection ends. */
943 ga_init2(&redir_ga, (int)sizeof(char), 500);
944
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000946 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
947 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000948 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
949 {
950 if (redir_endp != NULL && *redir_endp != NUL)
951 /* Trailing characters are present after the variable name */
952 EMSG(_(e_trailing));
953 else
954 EMSG(_(e_invarg));
955 var_redir_stop();
956 return FAIL;
957 }
958
959 /* check if we can write to the variable: set it to or append an empty
960 * string */
961 save_emsg = did_emsg;
962 did_emsg = FALSE;
963 tv.v_type = VAR_STRING;
964 tv.vval.v_string = (char_u *)"";
965 if (append)
966 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
967 else
968 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
969 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000970 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000971 if (err)
972 {
973 var_redir_stop();
974 return FAIL;
975 }
976 if (redir_lval->ll_newkey != NULL)
977 {
978 /* Dictionary item was created, don't do it again. */
979 vim_free(redir_lval->ll_newkey);
980 redir_lval->ll_newkey = NULL;
981 }
982
983 return OK;
984}
985
986/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000987 * Append "value[value_len]" to the variable set by var_redir_start().
988 * The actual appending is postponed until redirection ends, because the value
989 * appended may in fact be the string we write to, changing it may cause freed
990 * memory to be used:
991 * :redir => foo
992 * :let foo
993 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994 */
995 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000996var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000997 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000998 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001000 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001
1002 if (redir_lval == NULL)
1003 return;
1004
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001005 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001006 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001008 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001010 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001011 {
1012 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001013 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001014 }
1015 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017}
1018
1019/*
1020 * Stop redirecting command output to a variable.
1021 */
1022 void
1023var_redir_stop()
1024{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001025 typval_T tv;
1026
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027 if (redir_lval != NULL)
1028 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001029 /* Append the trailing NUL. */
1030 ga_append(&redir_ga, NUL);
1031
1032 /* Assign the text to the variable. */
1033 tv.v_type = VAR_STRING;
1034 tv.vval.v_string = redir_ga.ga_data;
1035 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1036 vim_free(tv.vval.v_string);
1037
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 clear_lval(redir_lval);
1039 vim_free(redir_lval);
1040 redir_lval = NULL;
1041 }
1042 vim_free(redir_varname);
1043 redir_varname = NULL;
1044}
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046# if defined(FEAT_MBYTE) || defined(PROTO)
1047 int
1048eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1049 char_u *enc_from;
1050 char_u *enc_to;
1051 char_u *fname_from;
1052 char_u *fname_to;
1053{
1054 int err = FALSE;
1055
1056 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1057 set_vim_var_string(VV_CC_TO, enc_to, -1);
1058 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1059 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1060 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1061 err = TRUE;
1062 set_vim_var_string(VV_CC_FROM, NULL, -1);
1063 set_vim_var_string(VV_CC_TO, NULL, -1);
1064 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1065 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1066
1067 if (err)
1068 return FAIL;
1069 return OK;
1070}
1071# endif
1072
1073# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1074 int
1075eval_printexpr(fname, args)
1076 char_u *fname;
1077 char_u *args;
1078{
1079 int err = FALSE;
1080
1081 set_vim_var_string(VV_FNAME_IN, fname, -1);
1082 set_vim_var_string(VV_CMDARG, args, -1);
1083 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1084 err = TRUE;
1085 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1086 set_vim_var_string(VV_CMDARG, NULL, -1);
1087
1088 if (err)
1089 {
1090 mch_remove(fname);
1091 return FAIL;
1092 }
1093 return OK;
1094}
1095# endif
1096
1097# if defined(FEAT_DIFF) || defined(PROTO)
1098 void
1099eval_diff(origfile, newfile, outfile)
1100 char_u *origfile;
1101 char_u *newfile;
1102 char_u *outfile;
1103{
1104 int err = FALSE;
1105
1106 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1107 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1108 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1109 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1112 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1113}
1114
1115 void
1116eval_patch(origfile, difffile, outfile)
1117 char_u *origfile;
1118 char_u *difffile;
1119 char_u *outfile;
1120{
1121 int err;
1122
1123 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1124 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1125 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1126 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1127 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1128 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1129 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1130}
1131# endif
1132
1133/*
1134 * Top level evaluation function, returning a boolean.
1135 * Sets "error" to TRUE if there was an error.
1136 * Return TRUE or FALSE.
1137 */
1138 int
1139eval_to_bool(arg, error, nextcmd, skip)
1140 char_u *arg;
1141 int *error;
1142 char_u **nextcmd;
1143 int skip; /* only parse, don't execute */
1144{
Bram Moolenaar33570922005-01-25 22:26:29 +00001145 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 int retval = FALSE;
1147
1148 if (skip)
1149 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 else
1153 {
1154 *error = FALSE;
1155 if (!skip)
1156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001157 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160 }
1161 if (skip)
1162 --emsg_skip;
1163
1164 return retval;
1165}
1166
1167/*
1168 * Top level evaluation function, returning a string. If "skip" is TRUE,
1169 * only parsing to "nextcmd" is done, without reporting errors. Return
1170 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1171 */
1172 char_u *
1173eval_to_string_skip(arg, nextcmd, skip)
1174 char_u *arg;
1175 char_u **nextcmd;
1176 int skip; /* only parse, don't execute */
1177{
Bram Moolenaar33570922005-01-25 22:26:29 +00001178 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 char_u *retval;
1180
1181 if (skip)
1182 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 retval = NULL;
1185 else
1186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187 retval = vim_strsave(get_tv_string(&tv));
1188 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 if (skip)
1191 --emsg_skip;
1192
1193 return retval;
1194}
1195
1196/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001197 * Skip over an expression at "*pp".
1198 * Return FAIL for an error, OK otherwise.
1199 */
1200 int
1201skip_expr(pp)
1202 char_u **pp;
1203{
Bram Moolenaar33570922005-01-25 22:26:29 +00001204 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001205
1206 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001208}
1209
1210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 * Top level evaluation function, returning a string.
1212 * Return pointer to allocated memory, or NULL for failure.
1213 */
1214 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001215eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *arg;
1217 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001218 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001222 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 retval = NULL;
1226 else
1227 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001228 if (dolist && tv.v_type == VAR_LIST)
1229 {
1230 ga_init2(&ga, (int)sizeof(char), 80);
1231 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1232 ga_append(&ga, NUL);
1233 retval = (char_u *)ga.ga_data;
1234 }
1235 else
1236 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239
1240 return retval;
1241}
1242
1243/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001244 * Call eval_to_string() without using current local variables and using
1245 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 */
1247 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001248eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 char_u *arg;
1250 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001251 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 char_u *retval;
1254 void *save_funccalp;
1255
1256 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001257 if (use_sandbox)
1258 ++sandbox;
1259 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001260 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001261 if (use_sandbox)
1262 --sandbox;
1263 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 restore_funccal(save_funccalp);
1265 return retval;
1266}
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268/*
1269 * Top level evaluation function, returning a number.
1270 * Evaluates "expr" silently.
1271 * Returns -1 for an error.
1272 */
1273 int
1274eval_to_number(expr)
1275 char_u *expr;
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001279 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 ++emsg_off;
1282
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 retval = -1;
1285 else
1286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001287 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 --emsg_off;
1291
1292 return retval;
1293}
1294
Bram Moolenaara40058a2005-07-11 22:42:07 +00001295/*
1296 * Prepare v: variable "idx" to be used.
1297 * Save the current typeval in "save_tv".
1298 * When not used yet add the variable to the v: hashtable.
1299 */
1300 static void
1301prepare_vimvar(idx, save_tv)
1302 int idx;
1303 typval_T *save_tv;
1304{
1305 *save_tv = vimvars[idx].vv_tv;
1306 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1307 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1308}
1309
1310/*
1311 * Restore v: variable "idx" to typeval "save_tv".
1312 * When no longer defined, remove the variable from the v: hashtable.
1313 */
1314 static void
1315restore_vimvar(idx, save_tv)
1316 int idx;
1317 typval_T *save_tv;
1318{
1319 hashitem_T *hi;
1320
Bram Moolenaara40058a2005-07-11 22:42:07 +00001321 vimvars[idx].vv_tv = *save_tv;
1322 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1323 {
1324 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1325 if (HASHITEM_EMPTY(hi))
1326 EMSG2(_(e_intern2), "restore_vimvar()");
1327 else
1328 hash_remove(&vimvarht, hi);
1329 }
1330}
1331
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001332#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001333/*
1334 * Evaluate an expression to a list with suggestions.
1335 * For the "expr:" part of 'spellsuggest'.
1336 */
1337 list_T *
1338eval_spell_expr(badword, expr)
1339 char_u *badword;
1340 char_u *expr;
1341{
1342 typval_T save_val;
1343 typval_T rettv;
1344 list_T *list = NULL;
1345 char_u *p = skipwhite(expr);
1346
1347 /* Set "v:val" to the bad word. */
1348 prepare_vimvar(VV_VAL, &save_val);
1349 vimvars[VV_VAL].vv_type = VAR_STRING;
1350 vimvars[VV_VAL].vv_str = badword;
1351 if (p_verbose == 0)
1352 ++emsg_off;
1353
1354 if (eval1(&p, &rettv, TRUE) == OK)
1355 {
1356 if (rettv.v_type != VAR_LIST)
1357 clear_tv(&rettv);
1358 else
1359 list = rettv.vval.v_list;
1360 }
1361
1362 if (p_verbose == 0)
1363 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001364 restore_vimvar(VV_VAL, &save_val);
1365
1366 return list;
1367}
1368
1369/*
1370 * "list" is supposed to contain two items: a word and a number. Return the
1371 * word in "pp" and the number as the return value.
1372 * Return -1 if anything isn't right.
1373 * Used to get the good word and score from the eval_spell_expr() result.
1374 */
1375 int
1376get_spellword(list, pp)
1377 list_T *list;
1378 char_u **pp;
1379{
1380 listitem_T *li;
1381
1382 li = list->lv_first;
1383 if (li == NULL)
1384 return -1;
1385 *pp = get_tv_string(&li->li_tv);
1386
1387 li = li->li_next;
1388 if (li == NULL)
1389 return -1;
1390 return get_tv_number(&li->li_tv);
1391}
1392#endif
1393
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001394/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001395 * Top level evaluation function.
1396 * Returns an allocated typval_T with the result.
1397 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001398 */
1399 typval_T *
1400eval_expr(arg, nextcmd)
1401 char_u *arg;
1402 char_u **nextcmd;
1403{
1404 typval_T *tv;
1405
1406 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001407 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001408 {
1409 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001410 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001411 }
1412
1413 return tv;
1414}
1415
1416
Bram Moolenaar4f688582007-07-24 12:34:30 +00001417#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1418 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 static int
1425call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 char_u *func;
1427 int argc;
1428 char_u **argv;
1429 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 long n;
1434 int len;
1435 int i;
1436 int doesrange;
1437 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001440 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
1444 for (i = 0; i < argc; i++)
1445 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001446 /* Pass a NULL or empty argument as an empty string */
1447 if (argv[i] == NULL || *argv[i] == NUL)
1448 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001449 argvars[i].v_type = VAR_STRING;
1450 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001451 continue;
1452 }
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 /* Recognize a number argument, the others must be strings. */
1455 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1456 if (len != 0 && len == (int)STRLEN(argv[i]))
1457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001458 argvars[i].v_type = VAR_NUMBER;
1459 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461 else
1462 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001463 argvars[i].v_type = VAR_STRING;
1464 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
1466 }
1467
1468 if (safe)
1469 {
1470 save_funccalp = save_funccal();
1471 ++sandbox;
1472 }
1473
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001474 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1475 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (safe)
1479 {
1480 --sandbox;
1481 restore_funccal(save_funccalp);
1482 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001483 vim_free(argvars);
1484
1485 if (ret == FAIL)
1486 clear_tv(rettv);
1487
1488 return ret;
1489}
1490
Bram Moolenaar4f688582007-07-24 12:34:30 +00001491# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001492/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001493 * Call vimL function "func" and return the result as a string.
1494 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001495 * Uses argv[argc] for the function arguments.
1496 */
1497 void *
1498call_func_retstr(func, argc, argv, safe)
1499 char_u *func;
1500 int argc;
1501 char_u **argv;
1502 int safe; /* use the sandbox */
1503{
1504 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001505 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001506
1507 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1508 return NULL;
1509
1510 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001511 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 return retval;
1513}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001514# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001515
Bram Moolenaar4f688582007-07-24 12:34:30 +00001516# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001517/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001518 * Call vimL function "func" and return the result as a number.
1519 * Returns -1 when calling the function fails.
1520 * Uses argv[argc] for the function arguments.
1521 */
1522 long
1523call_func_retnr(func, argc, argv, safe)
1524 char_u *func;
1525 int argc;
1526 char_u **argv;
1527 int safe; /* use the sandbox */
1528{
1529 typval_T rettv;
1530 long retval;
1531
1532 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1533 return -1;
1534
1535 retval = get_tv_number_chk(&rettv, NULL);
1536 clear_tv(&rettv);
1537 return retval;
1538}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001539# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001540
1541/*
1542 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001543 * Uses argv[argc] for the function arguments.
1544 */
1545 void *
1546call_func_retlist(func, argc, argv, safe)
1547 char_u *func;
1548 int argc;
1549 char_u **argv;
1550 int safe; /* use the sandbox */
1551{
1552 typval_T rettv;
1553
1554 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1555 return NULL;
1556
1557 if (rettv.v_type != VAR_LIST)
1558 {
1559 clear_tv(&rettv);
1560 return NULL;
1561 }
1562
1563 return rettv.vval.v_list;
1564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#endif
1566
Bram Moolenaar4f688582007-07-24 12:34:30 +00001567
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568/*
1569 * Save the current function call pointer, and set it to NULL.
1570 * Used when executing autocommands and for ":source".
1571 */
1572 void *
1573save_funccal()
1574{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001575 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 current_funccal = NULL;
1578 return (void *)fc;
1579}
1580
1581 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001582restore_funccal(vfc)
1583 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001585 funccall_T *fc = (funccall_T *)vfc;
1586
1587 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588}
1589
Bram Moolenaar05159a02005-02-26 23:04:13 +00001590#if defined(FEAT_PROFILE) || defined(PROTO)
1591/*
1592 * Prepare profiling for entering a child or something else that is not
1593 * counted for the script/function itself.
1594 * Should always be called in pair with prof_child_exit().
1595 */
1596 void
1597prof_child_enter(tm)
1598 proftime_T *tm; /* place to store waittime */
1599{
1600 funccall_T *fc = current_funccal;
1601
1602 if (fc != NULL && fc->func->uf_profiling)
1603 profile_start(&fc->prof_child);
1604 script_prof_save(tm);
1605}
1606
1607/*
1608 * Take care of time spent in a child.
1609 * Should always be called after prof_child_enter().
1610 */
1611 void
1612prof_child_exit(tm)
1613 proftime_T *tm; /* where waittime was stored */
1614{
1615 funccall_T *fc = current_funccal;
1616
1617 if (fc != NULL && fc->func->uf_profiling)
1618 {
1619 profile_end(&fc->prof_child);
1620 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1621 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1622 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1623 }
1624 script_prof_restore(tm);
1625}
1626#endif
1627
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629#ifdef FEAT_FOLDING
1630/*
1631 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1632 * it in "*cp". Doesn't give error messages.
1633 */
1634 int
1635eval_foldexpr(arg, cp)
1636 char_u *arg;
1637 int *cp;
1638{
Bram Moolenaar33570922005-01-25 22:26:29 +00001639 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 int retval;
1641 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001642 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1643 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001646 if (use_sandbox)
1647 ++sandbox;
1648 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 retval = 0;
1652 else
1653 {
1654 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 if (tv.v_type == VAR_NUMBER)
1656 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001657 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 retval = 0;
1659 else
1660 {
1661 /* If the result is a string, check if there is a non-digit before
1662 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (!VIM_ISDIGIT(*s) && *s != '-')
1665 *cp = *s++;
1666 retval = atol((char *)s);
1667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001671 if (use_sandbox)
1672 --sandbox;
1673 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675 return retval;
1676}
1677#endif
1678
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 * ":let" list all variable values
1681 * ":let var1 var2" list variable values
1682 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 * ":let var += expr" assignment command.
1684 * ":let var -= expr" assignment command.
1685 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001686 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
1688 void
1689ex_let(eap)
1690 exarg_T *eap;
1691{
1692 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 int var_count = 0;
1697 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001699 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001700 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaardb552d602006-03-23 22:59:57 +00001702 argend = skip_var_list(arg, &var_count, &semicolon);
1703 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001705 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1706 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001707 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001710 /*
1711 * ":let" without "=": list variables
1712 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 if (*arg == '[')
1714 EMSG(_(e_invarg));
1715 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001717 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001721 list_glob_vars(&first);
1722 list_buf_vars(&first);
1723 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001724#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001725 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001726#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001727 list_script_vars(&first);
1728 list_func_vars(&first);
1729 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 eap->nextcmd = check_nextcmd(arg);
1732 }
1733 else
1734 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 op[0] = '=';
1736 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001737 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 {
1739 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1740 op[0] = expr[-1]; /* +=, -= or .= */
1741 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (eap->skip)
1745 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (eap->skip)
1748 {
1749 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 --emsg_skip;
1752 }
1753 else if (i != FAIL)
1754 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759 }
1760}
1761
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762/*
1763 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1764 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 * When "nextchars" is not NULL it points to a string with characters that
1766 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1767 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 * Returns OK or FAIL;
1769 */
1770 static int
1771ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1772 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001773 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 int copy; /* copy values from "tv", don't move */
1775 int semicolon; /* from skip_var_list() */
1776 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778{
1779 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 listitem_T *item;
1783 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784
1785 if (*arg != '[')
1786 {
1787 /*
1788 * ":let var = expr" or ":for var in list"
1789 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001790 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 return FAIL;
1792 return OK;
1793 }
1794
1795 /*
1796 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1797 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001798 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 {
1800 EMSG(_(e_listreq));
1801 return FAIL;
1802 }
1803
1804 i = list_len(l);
1805 if (semicolon == 0 && var_count < i)
1806 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001807 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808 return FAIL;
1809 }
1810 if (var_count - semicolon > i)
1811 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001812 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 return FAIL;
1814 }
1815
1816 item = l->lv_first;
1817 while (*arg != ']')
1818 {
1819 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 item = item->li_next;
1822 if (arg == NULL)
1823 return FAIL;
1824
1825 arg = skipwhite(arg);
1826 if (*arg == ';')
1827 {
1828 /* Put the rest of the list (may be empty) in the var after ';'.
1829 * Create a new list for this. */
1830 l = list_alloc();
1831 if (l == NULL)
1832 return FAIL;
1833 while (item != NULL)
1834 {
1835 list_append_tv(l, &item->li_tv);
1836 item = item->li_next;
1837 }
1838
1839 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001840 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 ltv.vval.v_list = l;
1842 l->lv_refcount = 1;
1843
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1845 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 clear_tv(&ltv);
1847 if (arg == NULL)
1848 return FAIL;
1849 break;
1850 }
1851 else if (*arg != ',' && *arg != ']')
1852 {
1853 EMSG2(_(e_intern2), "ex_let_vars()");
1854 return FAIL;
1855 }
1856 }
1857
1858 return OK;
1859}
1860
1861/*
1862 * Skip over assignable variable "var" or list of variables "[var, var]".
1863 * Used for ":let varvar = expr" and ":for varvar in expr".
1864 * For "[var, var]" increment "*var_count" for each variable.
1865 * for "[var, var; var]" set "semicolon".
1866 * Return NULL for an error.
1867 */
1868 static char_u *
1869skip_var_list(arg, var_count, semicolon)
1870 char_u *arg;
1871 int *var_count;
1872 int *semicolon;
1873{
1874 char_u *p, *s;
1875
1876 if (*arg == '[')
1877 {
1878 /* "[var, var]": find the matching ']'. */
1879 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001880 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 {
1882 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1883 s = skip_var_one(p);
1884 if (s == p)
1885 {
1886 EMSG2(_(e_invarg2), p);
1887 return NULL;
1888 }
1889 ++*var_count;
1890
1891 p = skipwhite(s);
1892 if (*p == ']')
1893 break;
1894 else if (*p == ';')
1895 {
1896 if (*semicolon == 1)
1897 {
1898 EMSG(_("Double ; in list of variables"));
1899 return NULL;
1900 }
1901 *semicolon = 1;
1902 }
1903 else if (*p != ',')
1904 {
1905 EMSG2(_(e_invarg2), p);
1906 return NULL;
1907 }
1908 }
1909 return p + 1;
1910 }
1911 else
1912 return skip_var_one(arg);
1913}
1914
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001915/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001916 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001917 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001918 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 static char_u *
1920skip_var_one(arg)
1921 char_u *arg;
1922{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001923 if (*arg == '@' && arg[1] != NUL)
1924 return arg + 2;
1925 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1926 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927}
1928
Bram Moolenaara7043832005-01-21 11:56:39 +00001929/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 * List variables for hashtab "ht" with prefix "prefix".
1931 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001932 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001934list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001938 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001939{
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 hashitem_T *hi;
1941 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001942 int todo;
1943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001944 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001945 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1946 {
1947 if (!HASHITEM_EMPTY(hi))
1948 {
1949 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001950 di = HI2DI(hi);
1951 if (empty || di->di_tv.v_type != VAR_STRING
1952 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001953 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001954 }
1955 }
1956}
1957
1958/*
1959 * List global variables.
1960 */
1961 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001962list_glob_vars(first)
1963 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001964{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001966}
1967
1968/*
1969 * List buffer variables.
1970 */
1971 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001972list_buf_vars(first)
1973 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001974{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001975 char_u numbuf[NUMBUFLEN];
1976
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001977 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1978 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001979
1980 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001981 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1982 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001983}
1984
1985/*
1986 * List window variables.
1987 */
1988 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001989list_win_vars(first)
1990 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001991{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001992 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1993 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001994}
1995
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001996#ifdef FEAT_WINDOWS
1997/*
1998 * List tab page variables.
1999 */
2000 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002001list_tab_vars(first)
2002 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002003{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002004 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2005 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002006}
2007#endif
2008
Bram Moolenaara7043832005-01-21 11:56:39 +00002009/*
2010 * List Vim variables.
2011 */
2012 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002013list_vim_vars(first)
2014 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002016 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017}
2018
2019/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002020 * List script-local variables, if there is a script.
2021 */
2022 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002023list_script_vars(first)
2024 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002025{
2026 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002027 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2028 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002029}
2030
2031/*
2032 * List function variables, if there is a function.
2033 */
2034 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002035list_func_vars(first)
2036 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002037{
2038 if (current_funccal != NULL)
2039 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002040 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002041}
2042
2043/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 * List variables in "arg".
2045 */
2046 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002047list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 exarg_T *eap;
2049 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002050 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051{
2052 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 char_u *name_start;
2056 char_u *arg_subsc;
2057 char_u *tofree;
2058 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059
2060 while (!ends_excmd(*arg) && !got_int)
2061 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002064 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2066 {
2067 emsg_severe = TRUE;
2068 EMSG(_(e_trailing));
2069 break;
2070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 /* get_name_len() takes care of expanding curly braces */
2075 name_start = name = arg;
2076 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2077 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002079 /* This is mainly to keep test 49 working: when expanding
2080 * curly braces fails overrule the exception error message. */
2081 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002082 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 emsg_severe = TRUE;
2084 EMSG2(_(e_invarg2), arg);
2085 break;
2086 }
2087 error = TRUE;
2088 }
2089 else
2090 {
2091 if (tofree != NULL)
2092 name = tofree;
2093 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 else
2096 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097 /* handle d.key, l[idx], f(expr) */
2098 arg_subsc = arg;
2099 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002100 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002105 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107 case 'g': list_glob_vars(first); break;
2108 case 'b': list_buf_vars(first); break;
2109 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002110#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002112#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 case 'v': list_vim_vars(first); break;
2114 case 's': list_script_vars(first); break;
2115 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116 default:
2117 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002118 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 }
2120 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121 {
2122 char_u numbuf[NUMBUFLEN];
2123 char_u *tf;
2124 int c;
2125 char_u *s;
2126
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002127 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002128 c = *arg;
2129 *arg = NUL;
2130 list_one_var_a((char_u *)"",
2131 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 tv.v_type,
2133 s == NULL ? (char_u *)"" : s,
2134 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135 *arg = c;
2136 vim_free(tf);
2137 }
2138 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 }
2141 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142
2143 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002144 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145
2146 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 }
2148
2149 return arg;
2150}
2151
2152/*
2153 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2154 * Returns a pointer to the char just after the var name.
2155 * Returns NULL if there is an error.
2156 */
2157 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002158ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164{
2165 int c1;
2166 char_u *name;
2167 char_u *p;
2168 char_u *arg_end = NULL;
2169 int len;
2170 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172
2173 /*
2174 * ":let $VAR = expr": Set environment variable.
2175 */
2176 if (*arg == '$')
2177 {
2178 /* Find the end of the name. */
2179 ++arg;
2180 name = arg;
2181 len = get_env_len(&arg);
2182 if (len == 0)
2183 EMSG2(_(e_invarg2), name - 1);
2184 else
2185 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 if (op != NULL && (*op == '+' || *op == '-'))
2187 EMSG2(_(e_letwrong), op);
2188 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002189 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 EMSG(_(e_letunexp));
2191 else
2192 {
2193 c1 = name[len];
2194 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002195 p = get_tv_string_chk(tv);
2196 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002197 {
2198 int mustfree = FALSE;
2199 char_u *s = vim_getenv(name, &mustfree);
2200
2201 if (s != NULL)
2202 {
2203 p = tofree = concat_str(s, p);
2204 if (mustfree)
2205 vim_free(s);
2206 }
2207 }
2208 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 if (STRICMP(name, "HOME") == 0)
2212 init_homedir();
2213 else if (didset_vim && STRICMP(name, "VIM") == 0)
2214 didset_vim = FALSE;
2215 else if (didset_vimruntime
2216 && STRICMP(name, "VIMRUNTIME") == 0)
2217 didset_vimruntime = FALSE;
2218 arg_end = arg;
2219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002221 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
2223 }
2224 }
2225
2226 /*
2227 * ":let &option = expr": Set option value.
2228 * ":let &l:option = expr": Set local option value.
2229 * ":let &g:option = expr": Set global option value.
2230 */
2231 else if (*arg == '&')
2232 {
2233 /* Find the end of the name. */
2234 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002235 if (p == NULL || (endchars != NULL
2236 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 EMSG(_(e_letunexp));
2238 else
2239 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240 long n;
2241 int opt_type;
2242 long numval;
2243 char_u *stringval = NULL;
2244 char_u *s;
2245
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 c1 = *p;
2247 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248
2249 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002250 s = get_tv_string_chk(tv); /* != NULL if number or string */
2251 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002252 {
2253 opt_type = get_option_value(arg, &numval,
2254 &stringval, opt_flags);
2255 if ((opt_type == 1 && *op == '.')
2256 || (opt_type == 0 && *op != '.'))
2257 EMSG2(_(e_letwrong), op);
2258 else
2259 {
2260 if (opt_type == 1) /* number */
2261 {
2262 if (*op == '+')
2263 n = numval + n;
2264 else
2265 n = numval - n;
2266 }
2267 else if (opt_type == 0 && stringval != NULL) /* string */
2268 {
2269 s = concat_str(stringval, s);
2270 vim_free(stringval);
2271 stringval = s;
2272 }
2273 }
2274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002275 if (s != NULL)
2276 {
2277 set_option_value(arg, n, s, opt_flags);
2278 arg_end = p;
2279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002281 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283 }
2284
2285 /*
2286 * ":let @r = expr": Set register contents.
2287 */
2288 else if (*arg == '@')
2289 {
2290 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002291 if (op != NULL && (*op == '+' || *op == '-'))
2292 EMSG2(_(e_letwrong), op);
2293 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002294 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 EMSG(_(e_letunexp));
2296 else
2297 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002298 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 char_u *s;
2300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002301 p = get_tv_string_chk(tv);
2302 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002304 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305 if (s != NULL)
2306 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002307 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 vim_free(s);
2309 }
2310 }
2311 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002312 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002313 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 arg_end = arg + 1;
2315 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002316 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318 }
2319
2320 /*
2321 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002324 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002328 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2332 EMSG(_(e_letunexp));
2333 else
2334 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 arg_end = p;
2337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
2341
2342 else
2343 EMSG2(_(e_invarg2), arg);
2344
2345 return arg_end;
2346}
2347
2348/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2350 */
2351 static int
2352check_changedtick(arg)
2353 char_u *arg;
2354{
2355 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2356 {
2357 EMSG2(_(e_readonlyvar), arg);
2358 return TRUE;
2359 }
2360 return FALSE;
2361}
2362
2363/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 * Get an lval: variable, Dict item or List item that can be assigned a value
2365 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2366 * "name.key", "name.key[expr]" etc.
2367 * Indexing only works if "name" is an existing List or Dictionary.
2368 * "name" points to the start of the name.
2369 * If "rettv" is not NULL it points to the value to be assigned.
2370 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2371 * wrong; must end in space or cmd separator.
2372 *
2373 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002374 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 */
2377 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002378get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002380 typval_T *rettv;
2381 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 int unlet;
2383 int skip;
2384 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002385 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 char_u *expr_start, *expr_end;
2389 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002390 dictitem_T *v;
2391 typval_T var1;
2392 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002394 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002397 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002400 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401
2402 if (skip)
2403 {
2404 /* When skipping just find the end of the name. */
2405 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002406 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 }
2408
2409 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002410 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (expr_start != NULL)
2412 {
2413 /* Don't expand the name when we already know there is an error. */
2414 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2415 && *p != '[' && *p != '.')
2416 {
2417 EMSG(_(e_trailing));
2418 return NULL;
2419 }
2420
2421 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2422 if (lp->ll_exp_name == NULL)
2423 {
2424 /* Report an invalid expression in braces, unless the
2425 * expression evaluation has been cancelled due to an
2426 * aborting error, an interrupt, or an exception. */
2427 if (!aborting() && !quiet)
2428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002429 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 EMSG2(_(e_invarg2), name);
2431 return NULL;
2432 }
2433 }
2434 lp->ll_name = lp->ll_exp_name;
2435 }
2436 else
2437 lp->ll_name = name;
2438
2439 /* Without [idx] or .key we are done. */
2440 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2441 return p;
2442
2443 cc = *p;
2444 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002445 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (v == NULL && !quiet)
2447 EMSG2(_(e_undefvar), lp->ll_name);
2448 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 if (v == NULL)
2450 return NULL;
2451
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 /*
2453 * Loop until no more [idx] or .key is following.
2454 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002455 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2459 && !(lp->ll_tv->v_type == VAR_DICT
2460 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (!quiet)
2463 EMSG(_("E689: Can only index a List or Dictionary"));
2464 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (!quiet)
2469 EMSG(_("E708: [:] must come last"));
2470 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002472
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 len = -1;
2474 if (*p == '.')
2475 {
2476 key = p + 1;
2477 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2478 ;
2479 if (len == 0)
2480 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 if (!quiet)
2482 EMSG(_(e_emptykey));
2483 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 }
2485 p = key + len;
2486 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002487 else
2488 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002490 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 if (*p == ':')
2492 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002493 else
2494 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 empty1 = FALSE;
2496 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 if (get_tv_string_chk(&var1) == NULL)
2499 {
2500 /* not a number or string */
2501 clear_tv(&var1);
2502 return NULL;
2503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
2505
2506 /* Optionally get the second index [ :expr]. */
2507 if (*p == ':')
2508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 if (!empty1)
2514 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 if (rettv != NULL && (rettv->v_type != VAR_LIST
2518 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (!quiet)
2521 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 if (!empty1)
2523 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 }
2526 p = skipwhite(p + 1);
2527 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 else
2530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2533 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 if (!empty1)
2535 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002538 if (get_tv_string_chk(&var2) == NULL)
2539 {
2540 /* not a number or string */
2541 if (!empty1)
2542 clear_tv(&var1);
2543 clear_tv(&var2);
2544 return NULL;
2545 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002548 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002551
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (!quiet)
2555 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 if (!empty1)
2557 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 }
2562
2563 /* Skip to past ']'. */
2564 ++p;
2565 }
2566
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 {
2569 if (len == -1)
2570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002572 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 if (*key == NUL)
2574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (!quiet)
2576 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 }
2580 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 lp->ll_list = NULL;
2582 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002583 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002590 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 if (len == -1)
2592 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 }
2595 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 if (len == -1)
2600 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 p = NULL;
2603 break;
2604 }
2605 if (len == -1)
2606 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 }
2609 else
2610 {
2611 /*
2612 * Get the number and item for the only or first index of the List.
2613 */
2614 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 else
2617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002618 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 clear_tv(&var1);
2620 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 lp->ll_list = lp->ll_tv->vval.v_list;
2623 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2624 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002626 if (lp->ll_n1 < 0)
2627 {
2628 lp->ll_n1 = 0;
2629 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2630 }
2631 }
2632 if (lp->ll_li == NULL)
2633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 }
2638
2639 /*
2640 * May need to find the item or absolute index for the second
2641 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 * When no index given: "lp->ll_empty2" is TRUE.
2643 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002647 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2658 if (lp->ll_n1 < 0)
2659 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2660 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 }
2663
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
2667
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return p;
2669}
2670
2671/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002672 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 */
2674 static void
2675clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002676 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677{
2678 vim_free(lp->ll_exp_name);
2679 vim_free(lp->ll_newkey);
2680}
2681
2682/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002683 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 */
2687 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002689 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002691 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694{
2695 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002696 listitem_T *ri;
2697 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698
2699 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 cc = *endp;
2704 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 if (op != NULL && *op != '=')
2706 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002707 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002709 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002710 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002711 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002712 {
2713 if (tv_op(&tv, rettv, op) == OK)
2714 set_var(lp->ll_name, &tv, FALSE);
2715 clear_tv(&tv);
2716 }
2717 }
2718 else
2719 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002721 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002723 else if (tv_check_lock(lp->ll_newkey == NULL
2724 ? lp->ll_tv->v_lock
2725 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2726 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 else if (lp->ll_range)
2728 {
2729 /*
2730 * Assign the List values to the list items.
2731 */
2732 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 if (op != NULL && *op != '=')
2735 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2736 else
2737 {
2738 clear_tv(&lp->ll_li->li_tv);
2739 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 ri = ri->li_next;
2742 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2743 break;
2744 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002747 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 ri = NULL;
2750 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_li = lp->ll_li->li_next;
2754 ++lp->ll_n1;
2755 }
2756 if (ri != NULL)
2757 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002758 else if (lp->ll_empty2
2759 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 : lp->ll_n1 != lp->ll_n2)
2761 EMSG(_("E711: List value has not enough items"));
2762 }
2763 else
2764 {
2765 /*
2766 * Assign to a List or Dictionary item.
2767 */
2768 if (lp->ll_newkey != NULL)
2769 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002770 if (op != NULL && *op != '=')
2771 {
2772 EMSG2(_(e_letwrong), op);
2773 return;
2774 }
2775
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002777 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (di == NULL)
2779 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002780 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2781 {
2782 vim_free(di);
2783 return;
2784 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787 else if (op != NULL && *op != '=')
2788 {
2789 tv_op(lp->ll_tv, rettv, op);
2790 return;
2791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002792 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /*
2796 * Assign the value to the variable or list item.
2797 */
2798 if (copy)
2799 copy_tv(rettv, lp->ll_tv);
2800 else
2801 {
2802 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002803 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002805 }
2806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807}
2808
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002809/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002810 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2811 * Returns OK or FAIL.
2812 */
2813 static int
2814tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002815 typval_T *tv1;
2816 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 char_u *op;
2818{
2819 long n;
2820 char_u numbuf[NUMBUFLEN];
2821 char_u *s;
2822
2823 /* Can't do anything with a Funcref or a Dict on the right. */
2824 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2825 {
2826 switch (tv1->v_type)
2827 {
2828 case VAR_DICT:
2829 case VAR_FUNC:
2830 break;
2831
2832 case VAR_LIST:
2833 if (*op != '+' || tv2->v_type != VAR_LIST)
2834 break;
2835 /* List += List */
2836 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2837 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2838 return OK;
2839
2840 case VAR_NUMBER:
2841 case VAR_STRING:
2842 if (tv2->v_type == VAR_LIST)
2843 break;
2844 if (*op == '+' || *op == '-')
2845 {
2846 /* nr += nr or nr -= nr*/
2847 n = get_tv_number(tv1);
2848 if (*op == '+')
2849 n += get_tv_number(tv2);
2850 else
2851 n -= get_tv_number(tv2);
2852 clear_tv(tv1);
2853 tv1->v_type = VAR_NUMBER;
2854 tv1->vval.v_number = n;
2855 }
2856 else
2857 {
2858 /* str .= str */
2859 s = get_tv_string(tv1);
2860 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2861 clear_tv(tv1);
2862 tv1->v_type = VAR_STRING;
2863 tv1->vval.v_string = s;
2864 }
2865 return OK;
2866 }
2867 }
2868
2869 EMSG2(_(e_letwrong), op);
2870 return FAIL;
2871}
2872
2873/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874 * Add a watcher to a list.
2875 */
2876 static void
2877list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 list_T *l;
2879 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002880{
2881 lw->lw_next = l->lv_watch;
2882 l->lv_watch = lw;
2883}
2884
2885/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002886 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002887 * No warning when it isn't found...
2888 */
2889 static void
2890list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 list_T *l;
2892 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002893{
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895
2896 lwp = &l->lv_watch;
2897 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2898 {
2899 if (lw == lwrem)
2900 {
2901 *lwp = lw->lw_next;
2902 break;
2903 }
2904 lwp = &lw->lw_next;
2905 }
2906}
2907
2908/*
2909 * Just before removing an item from a list: advance watchers to the next
2910 * item.
2911 */
2912 static void
2913list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 list_T *l;
2915 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916{
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918
2919 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2920 if (lw->lw_item == item)
2921 lw->lw_item = item->li_next;
2922}
2923
2924/*
2925 * Evaluate the expression used in a ":for var in expr" command.
2926 * "arg" points to "var".
2927 * Set "*errp" to TRUE for an error, FALSE otherwise;
2928 * Return a pointer that holds the info. Null when there is an error.
2929 */
2930 void *
2931eval_for_line(arg, errp, nextcmdp, skip)
2932 char_u *arg;
2933 int *errp;
2934 char_u **nextcmdp;
2935 int skip;
2936{
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002938 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 typval_T tv;
2940 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941
2942 *errp = TRUE; /* default: there is an error */
2943
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002945 if (fi == NULL)
2946 return NULL;
2947
2948 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2949 if (expr == NULL)
2950 return fi;
2951
2952 expr = skipwhite(expr);
2953 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2954 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002955 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002956 return fi;
2957 }
2958
2959 if (skip)
2960 ++emsg_skip;
2961 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2962 {
2963 *errp = FALSE;
2964 if (!skip)
2965 {
2966 l = tv.vval.v_list;
2967 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002968 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002970 clear_tv(&tv);
2971 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 else
2973 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002974 /* No need to increment the refcount, it's already set for the
2975 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976 fi->fi_list = l;
2977 list_add_watch(l, &fi->fi_lw);
2978 fi->fi_lw.lw_item = l->lv_first;
2979 }
2980 }
2981 }
2982 if (skip)
2983 --emsg_skip;
2984
2985 return fi;
2986}
2987
2988/*
2989 * Use the first item in a ":for" list. Advance to the next.
2990 * Assign the values to the variable (list). "arg" points to the first one.
2991 * Return TRUE when a valid item was found, FALSE when at end of list or
2992 * something wrong.
2993 */
2994 int
2995next_for_item(fi_void, arg)
2996 void *fi_void;
2997 char_u *arg;
2998{
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003001 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002
3003 item = fi->fi_lw.lw_item;
3004 if (item == NULL)
3005 result = FALSE;
3006 else
3007 {
3008 fi->fi_lw.lw_item = item->li_next;
3009 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3010 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3011 }
3012 return result;
3013}
3014
3015/*
3016 * Free the structure used to store info used by ":for".
3017 */
3018 void
3019free_for_info(fi_void)
3020 void *fi_void;
3021{
Bram Moolenaar33570922005-01-25 22:26:29 +00003022 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003023
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003024 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003025 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003026 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003027 list_unref(fi->fi_list);
3028 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003029 vim_free(fi);
3030}
3031
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3033
3034 void
3035set_context_for_expression(xp, arg, cmdidx)
3036 expand_T *xp;
3037 char_u *arg;
3038 cmdidx_T cmdidx;
3039{
3040 int got_eq = FALSE;
3041 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003042 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044 if (cmdidx == CMD_let)
3045 {
3046 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003047 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048 {
3049 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003050 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003051 {
3052 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003053 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003054 if (vim_iswhite(*p))
3055 break;
3056 }
3057 return;
3058 }
3059 }
3060 else
3061 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3062 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 while ((xp->xp_pattern = vim_strpbrk(arg,
3064 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3065 {
3066 c = *xp->xp_pattern;
3067 if (c == '&')
3068 {
3069 c = xp->xp_pattern[1];
3070 if (c == '&')
3071 {
3072 ++xp->xp_pattern;
3073 xp->xp_context = cmdidx != CMD_let || got_eq
3074 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3075 }
3076 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003079 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3080 xp->xp_pattern += 2;
3081
3082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
3084 else if (c == '$')
3085 {
3086 /* environment variable */
3087 xp->xp_context = EXPAND_ENV_VARS;
3088 }
3089 else if (c == '=')
3090 {
3091 got_eq = TRUE;
3092 xp->xp_context = EXPAND_EXPRESSION;
3093 }
3094 else if (c == '<'
3095 && xp->xp_context == EXPAND_FUNCTIONS
3096 && vim_strchr(xp->xp_pattern, '(') == NULL)
3097 {
3098 /* Function name can start with "<SNR>" */
3099 break;
3100 }
3101 else if (cmdidx != CMD_let || got_eq)
3102 {
3103 if (c == '"') /* string */
3104 {
3105 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3106 if (c == '\\' && xp->xp_pattern[1] != NUL)
3107 ++xp->xp_pattern;
3108 xp->xp_context = EXPAND_NOTHING;
3109 }
3110 else if (c == '\'') /* literal string */
3111 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003112 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3114 /* skip */ ;
3115 xp->xp_context = EXPAND_NOTHING;
3116 }
3117 else if (c == '|')
3118 {
3119 if (xp->xp_pattern[1] == '|')
3120 {
3121 ++xp->xp_pattern;
3122 xp->xp_context = EXPAND_EXPRESSION;
3123 }
3124 else
3125 xp->xp_context = EXPAND_COMMANDS;
3126 }
3127 else
3128 xp->xp_context = EXPAND_EXPRESSION;
3129 }
3130 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131 /* Doesn't look like something valid, expand as an expression
3132 * anyway. */
3133 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 arg = xp->xp_pattern;
3135 if (*arg != NUL)
3136 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3137 /* skip */ ;
3138 }
3139 xp->xp_pattern = arg;
3140}
3141
3142#endif /* FEAT_CMDL_COMPL */
3143
3144/*
3145 * ":1,25call func(arg1, arg2)" function call.
3146 */
3147 void
3148ex_call(eap)
3149 exarg_T *eap;
3150{
3151 char_u *arg = eap->arg;
3152 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 linenr_T lnum;
3158 int doesrange;
3159 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003162 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003163 if (fudi.fd_newkey != NULL)
3164 {
3165 /* Still need to give an error message for missing key. */
3166 EMSG2(_(e_dictkey), fudi.fd_newkey);
3167 vim_free(fudi.fd_newkey);
3168 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003169 if (tofree == NULL)
3170 return;
3171
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003172 /* Increase refcount on dictionary, it could get deleted when evaluating
3173 * the arguments. */
3174 if (fudi.fd_dict != NULL)
3175 ++fudi.fd_dict->dv_refcount;
3176
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003177 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003178 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaar532c7802005-01-27 14:44:31 +00003181 /* Skip white space to allow ":call func ()". Not good, but required for
3182 * backward compatibility. */
3183 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003184 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186 if (*startarg != '(')
3187 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003188 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 goto end;
3190 }
3191
3192 /*
3193 * When skipping, evaluate the function once, to find the end of the
3194 * arguments.
3195 * When the function takes a range, this is discovered after the first
3196 * call, and the loop is broken.
3197 */
3198 if (eap->skip)
3199 {
3200 ++emsg_skip;
3201 lnum = eap->line2; /* do it once, also with an invalid range */
3202 }
3203 else
3204 lnum = eap->line1;
3205 for ( ; lnum <= eap->line2; ++lnum)
3206 {
3207 if (!eap->skip && eap->addr_count > 0)
3208 {
3209 curwin->w_cursor.lnum = lnum;
3210 curwin->w_cursor.col = 0;
3211 }
3212 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003213 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003214 eap->line1, eap->line2, &doesrange,
3215 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
3217 failed = TRUE;
3218 break;
3219 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003220
3221 /* Handle a function returning a Funcref, Dictionary or List. */
3222 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3223 {
3224 failed = TRUE;
3225 break;
3226 }
3227
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003228 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (doesrange || eap->skip)
3230 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003233 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003234 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003235 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (aborting())
3237 break;
3238 }
3239 if (eap->skip)
3240 --emsg_skip;
3241
3242 if (!failed)
3243 {
3244 /* Check for trailing illegal characters and a following command. */
3245 if (!ends_excmd(*arg))
3246 {
3247 emsg_severe = TRUE;
3248 EMSG(_(e_trailing));
3249 }
3250 else
3251 eap->nextcmd = check_nextcmd(arg);
3252 }
3253
3254end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003255 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003256 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
3258
3259/*
3260 * ":unlet[!] var1 ... " command.
3261 */
3262 void
3263ex_unlet(eap)
3264 exarg_T *eap;
3265{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266 ex_unletlock(eap, eap->arg, 0);
3267}
3268
3269/*
3270 * ":lockvar" and ":unlockvar" commands
3271 */
3272 void
3273ex_lockvar(eap)
3274 exarg_T *eap;
3275{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277 int deep = 2;
3278
3279 if (eap->forceit)
3280 deep = -1;
3281 else if (vim_isdigit(*arg))
3282 {
3283 deep = getdigits(&arg);
3284 arg = skipwhite(arg);
3285 }
3286
3287 ex_unletlock(eap, arg, deep);
3288}
3289
3290/*
3291 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3292 */
3293 static void
3294ex_unletlock(eap, argstart, deep)
3295 exarg_T *eap;
3296 char_u *argstart;
3297 int deep;
3298{
3299 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003302 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
3304 do
3305 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003307 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3308 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003309 if (lv.ll_name == NULL)
3310 error = TRUE; /* error but continue parsing */
3311 if (name_end == NULL || (!vim_iswhite(*name_end)
3312 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003314 if (name_end != NULL)
3315 {
3316 emsg_severe = TRUE;
3317 EMSG(_(e_trailing));
3318 }
3319 if (!(eap->skip || error))
3320 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 break;
3322 }
3323
3324 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003325 {
3326 if (eap->cmdidx == CMD_unlet)
3327 {
3328 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3329 error = TRUE;
3330 }
3331 else
3332 {
3333 if (do_lock_var(&lv, name_end, deep,
3334 eap->cmdidx == CMD_lockvar) == FAIL)
3335 error = TRUE;
3336 }
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003339 if (!eap->skip)
3340 clear_lval(&lv);
3341
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 arg = skipwhite(name_end);
3343 } while (!ends_excmd(*arg));
3344
3345 eap->nextcmd = check_nextcmd(arg);
3346}
3347
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003349do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003352 int forceit;
3353{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003354 int ret = OK;
3355 int cc;
3356
3357 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003358 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003359 cc = *name_end;
3360 *name_end = NUL;
3361
3362 /* Normal name or expanded name. */
3363 if (check_changedtick(lp->ll_name))
3364 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003365 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003366 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003367 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003368 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003369 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3370 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003371 else if (lp->ll_range)
3372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003374
3375 /* Delete a range of List items. */
3376 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3377 {
3378 li = lp->ll_li->li_next;
3379 listitem_remove(lp->ll_list, lp->ll_li);
3380 lp->ll_li = li;
3381 ++lp->ll_n1;
3382 }
3383 }
3384 else
3385 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003386 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003387 /* unlet a List item. */
3388 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003389 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003390 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003391 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003392 }
3393
3394 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003395}
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397/*
3398 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003399 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 */
3401 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003402do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003404 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaar33570922005-01-25 22:26:29 +00003406 hashtab_T *ht;
3407 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003408 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 ht = find_var_ht(name, &varname);
3411 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 hi = hash_find(ht, varname);
3414 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003415 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003416 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3417 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003418 if (var_check_ro(HI2DI(hi)->di_flags, name))
3419 return FAIL;
3420 delete_var(ht, hi);
3421 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003424 if (forceit)
3425 return OK;
3426 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 return FAIL;
3428}
3429
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003430/*
3431 * Lock or unlock variable indicated by "lp".
3432 * "deep" is the levels to go (-1 for unlimited);
3433 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3434 */
3435 static int
3436do_lock_var(lp, name_end, deep, lock)
3437 lval_T *lp;
3438 char_u *name_end;
3439 int deep;
3440 int lock;
3441{
3442 int ret = OK;
3443 int cc;
3444 dictitem_T *di;
3445
3446 if (deep == 0) /* nothing to do */
3447 return OK;
3448
3449 if (lp->ll_tv == NULL)
3450 {
3451 cc = *name_end;
3452 *name_end = NUL;
3453
3454 /* Normal name or expanded name. */
3455 if (check_changedtick(lp->ll_name))
3456 ret = FAIL;
3457 else
3458 {
3459 di = find_var(lp->ll_name, NULL);
3460 if (di == NULL)
3461 ret = FAIL;
3462 else
3463 {
3464 if (lock)
3465 di->di_flags |= DI_FLAGS_LOCK;
3466 else
3467 di->di_flags &= ~DI_FLAGS_LOCK;
3468 item_lock(&di->di_tv, deep, lock);
3469 }
3470 }
3471 *name_end = cc;
3472 }
3473 else if (lp->ll_range)
3474 {
3475 listitem_T *li = lp->ll_li;
3476
3477 /* (un)lock a range of List items. */
3478 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3479 {
3480 item_lock(&li->li_tv, deep, lock);
3481 li = li->li_next;
3482 ++lp->ll_n1;
3483 }
3484 }
3485 else if (lp->ll_list != NULL)
3486 /* (un)lock a List item. */
3487 item_lock(&lp->ll_li->li_tv, deep, lock);
3488 else
3489 /* un(lock) a Dictionary item. */
3490 item_lock(&lp->ll_di->di_tv, deep, lock);
3491
3492 return ret;
3493}
3494
3495/*
3496 * Lock or unlock an item. "deep" is nr of levels to go.
3497 */
3498 static void
3499item_lock(tv, deep, lock)
3500 typval_T *tv;
3501 int deep;
3502 int lock;
3503{
3504 static int recurse = 0;
3505 list_T *l;
3506 listitem_T *li;
3507 dict_T *d;
3508 hashitem_T *hi;
3509 int todo;
3510
3511 if (recurse >= DICT_MAXNEST)
3512 {
3513 EMSG(_("E743: variable nested too deep for (un)lock"));
3514 return;
3515 }
3516 if (deep == 0)
3517 return;
3518 ++recurse;
3519
3520 /* lock/unlock the item itself */
3521 if (lock)
3522 tv->v_lock |= VAR_LOCKED;
3523 else
3524 tv->v_lock &= ~VAR_LOCKED;
3525
3526 switch (tv->v_type)
3527 {
3528 case VAR_LIST:
3529 if ((l = tv->vval.v_list) != NULL)
3530 {
3531 if (lock)
3532 l->lv_lock |= VAR_LOCKED;
3533 else
3534 l->lv_lock &= ~VAR_LOCKED;
3535 if (deep < 0 || deep > 1)
3536 /* recursive: lock/unlock the items the List contains */
3537 for (li = l->lv_first; li != NULL; li = li->li_next)
3538 item_lock(&li->li_tv, deep - 1, lock);
3539 }
3540 break;
3541 case VAR_DICT:
3542 if ((d = tv->vval.v_dict) != NULL)
3543 {
3544 if (lock)
3545 d->dv_lock |= VAR_LOCKED;
3546 else
3547 d->dv_lock &= ~VAR_LOCKED;
3548 if (deep < 0 || deep > 1)
3549 {
3550 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003551 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3553 {
3554 if (!HASHITEM_EMPTY(hi))
3555 {
3556 --todo;
3557 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3558 }
3559 }
3560 }
3561 }
3562 }
3563 --recurse;
3564}
3565
Bram Moolenaara40058a2005-07-11 22:42:07 +00003566/*
3567 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3568 * it refers to a List or Dictionary that is locked.
3569 */
3570 static int
3571tv_islocked(tv)
3572 typval_T *tv;
3573{
3574 return (tv->v_lock & VAR_LOCKED)
3575 || (tv->v_type == VAR_LIST
3576 && tv->vval.v_list != NULL
3577 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3578 || (tv->v_type == VAR_DICT
3579 && tv->vval.v_dict != NULL
3580 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3581}
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3584/*
3585 * Delete all "menutrans_" variables.
3586 */
3587 void
3588del_menutrans_vars()
3589{
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003591 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003596 {
3597 if (!HASHITEM_EMPTY(hi))
3598 {
3599 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3601 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003602 }
3603 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605}
3606#endif
3607
3608#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3609
3610/*
3611 * Local string buffer for the next two functions to store a variable name
3612 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3613 * get_user_var_name().
3614 */
3615
3616static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3617
3618static char_u *varnamebuf = NULL;
3619static int varnamebuflen = 0;
3620
3621/*
3622 * Function to concatenate a prefix and a variable name.
3623 */
3624 static char_u *
3625cat_prefix_varname(prefix, name)
3626 int prefix;
3627 char_u *name;
3628{
3629 int len;
3630
3631 len = (int)STRLEN(name) + 3;
3632 if (len > varnamebuflen)
3633 {
3634 vim_free(varnamebuf);
3635 len += 10; /* some additional space */
3636 varnamebuf = alloc(len);
3637 if (varnamebuf == NULL)
3638 {
3639 varnamebuflen = 0;
3640 return NULL;
3641 }
3642 varnamebuflen = len;
3643 }
3644 *varnamebuf = prefix;
3645 varnamebuf[1] = ':';
3646 STRCPY(varnamebuf + 2, name);
3647 return varnamebuf;
3648}
3649
3650/*
3651 * Function given to ExpandGeneric() to obtain the list of user defined
3652 * (global/buffer/window/built-in) variable names.
3653 */
3654/*ARGSUSED*/
3655 char_u *
3656get_user_var_name(xp, idx)
3657 expand_T *xp;
3658 int idx;
3659{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003660 static long_u gdone;
3661 static long_u bdone;
3662 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003663#ifdef FEAT_WINDOWS
3664 static long_u tdone;
3665#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003666 static int vidx;
3667 static hashitem_T *hi;
3668 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003671 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003672 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003673#ifdef FEAT_WINDOWS
3674 tdone = 0;
3675#endif
3676 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003677
3678 /* Global variables */
3679 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003683 else
3684 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003685 while (HASHITEM_EMPTY(hi))
3686 ++hi;
3687 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3688 return cat_prefix_varname('g', hi->hi_key);
3689 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003691
3692 /* b: variables */
3693 ht = &curbuf->b_vars.dv_hashtab;
3694 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003696 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003697 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003698 else
3699 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003700 while (HASHITEM_EMPTY(hi))
3701 ++hi;
3702 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003704 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003706 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 return (char_u *)"b:changedtick";
3708 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003709
3710 /* w: variables */
3711 ht = &curwin->w_vars.dv_hashtab;
3712 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003714 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003716 else
3717 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003718 while (HASHITEM_EMPTY(hi))
3719 ++hi;
3720 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003722
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003723#ifdef FEAT_WINDOWS
3724 /* t: variables */
3725 ht = &curtab->tp_vars.dv_hashtab;
3726 if (tdone < ht->ht_used)
3727 {
3728 if (tdone++ == 0)
3729 hi = ht->ht_array;
3730 else
3731 ++hi;
3732 while (HASHITEM_EMPTY(hi))
3733 ++hi;
3734 return cat_prefix_varname('t', hi->hi_key);
3735 }
3736#endif
3737
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 /* v: variables */
3739 if (vidx < VV_LEN)
3740 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 vim_free(varnamebuf);
3743 varnamebuf = NULL;
3744 varnamebuflen = 0;
3745 return NULL;
3746}
3747
3748#endif /* FEAT_CMDL_COMPL */
3749
3750/*
3751 * types for expressions.
3752 */
3753typedef enum
3754{
3755 TYPE_UNKNOWN = 0
3756 , TYPE_EQUAL /* == */
3757 , TYPE_NEQUAL /* != */
3758 , TYPE_GREATER /* > */
3759 , TYPE_GEQUAL /* >= */
3760 , TYPE_SMALLER /* < */
3761 , TYPE_SEQUAL /* <= */
3762 , TYPE_MATCH /* =~ */
3763 , TYPE_NOMATCH /* !~ */
3764} exptype_T;
3765
3766/*
3767 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3770 */
3771
3772/*
3773 * Handle zero level expression.
3774 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003776 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 * Return OK or FAIL.
3778 */
3779 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003780eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 char_u **nextcmd;
3784 int evaluate;
3785{
3786 int ret;
3787 char_u *p;
3788
3789 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003790 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (ret == FAIL || !ends_excmd(*p))
3792 {
3793 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 /*
3796 * Report the invalid expression unless the expression evaluation has
3797 * been cancelled due to an aborting error, an interrupt, or an
3798 * exception.
3799 */
3800 if (!aborting())
3801 EMSG2(_(e_invexpr2), arg);
3802 ret = FAIL;
3803 }
3804 if (nextcmd != NULL)
3805 *nextcmd = check_nextcmd(p);
3806
3807 return ret;
3808}
3809
3810/*
3811 * Handle top level expression:
3812 * expr1 ? expr0 : expr0
3813 *
3814 * "arg" must point to the first non-white of the expression.
3815 * "arg" is advanced to the next non-white after the recognized expression.
3816 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003817 * Note: "rettv.v_lock" is not set.
3818 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 * Return OK or FAIL.
3820 */
3821 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 int evaluate;
3826{
3827 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830 /*
3831 * Get the first variable.
3832 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 return FAIL;
3835
3836 if ((*arg)[0] == '?')
3837 {
3838 result = FALSE;
3839 if (evaluate)
3840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003841 int error = FALSE;
3842
3843 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003845 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003846 if (error)
3847 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849
3850 /*
3851 * Get the second variable.
3852 */
3853 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return FAIL;
3856
3857 /*
3858 * Check for the ":".
3859 */
3860 if ((*arg)[0] != ':')
3861 {
3862 EMSG(_("E109: Missing ':' after '?'"));
3863 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 return FAIL;
3866 }
3867
3868 /*
3869 * Get the third variable.
3870 */
3871 *arg = skipwhite(*arg + 1);
3872 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3873 {
3874 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003875 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return FAIL;
3877 }
3878 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881
3882 return OK;
3883}
3884
3885/*
3886 * Handle first level expression:
3887 * expr2 || expr2 || expr2 logical OR
3888 *
3889 * "arg" must point to the first non-white of the expression.
3890 * "arg" is advanced to the next non-white after the recognized expression.
3891 *
3892 * Return OK or FAIL.
3893 */
3894 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003895eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 int evaluate;
3899{
Bram Moolenaar33570922005-01-25 22:26:29 +00003900 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 long result;
3902 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003903 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905 /*
3906 * Get the first variable.
3907 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003908 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return FAIL;
3910
3911 /*
3912 * Repeat until there is no following "||".
3913 */
3914 first = TRUE;
3915 result = FALSE;
3916 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3917 {
3918 if (evaluate && first)
3919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003920 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003922 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003923 if (error)
3924 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 first = FALSE;
3926 }
3927
3928 /*
3929 * Get the second variable.
3930 */
3931 *arg = skipwhite(*arg + 2);
3932 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3933 return FAIL;
3934
3935 /*
3936 * Compute the result.
3937 */
3938 if (evaluate && !result)
3939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003940 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (error)
3944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 if (evaluate)
3947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003948 rettv->v_type = VAR_NUMBER;
3949 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 }
3952
3953 return OK;
3954}
3955
3956/*
3957 * Handle second level expression:
3958 * expr3 && expr3 && expr3 logical AND
3959 *
3960 * "arg" must point to the first non-white of the expression.
3961 * "arg" is advanced to the next non-white after the recognized expression.
3962 *
3963 * Return OK or FAIL.
3964 */
3965 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003966eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 int evaluate;
3970{
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 long result;
3973 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003974 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976 /*
3977 * Get the first variable.
3978 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003979 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 return FAIL;
3981
3982 /*
3983 * Repeat until there is no following "&&".
3984 */
3985 first = TRUE;
3986 result = TRUE;
3987 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3988 {
3989 if (evaluate && first)
3990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003993 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003994 if (error)
3995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 first = FALSE;
3997 }
3998
3999 /*
4000 * Get the second variable.
4001 */
4002 *arg = skipwhite(*arg + 2);
4003 if (eval4(arg, &var2, evaluate && result) == FAIL)
4004 return FAIL;
4005
4006 /*
4007 * Compute the result.
4008 */
4009 if (evaluate && result)
4010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004011 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 if (error)
4015 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 if (evaluate)
4018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 rettv->v_type = VAR_NUMBER;
4020 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022 }
4023
4024 return OK;
4025}
4026
4027/*
4028 * Handle third level expression:
4029 * var1 == var2
4030 * var1 =~ var2
4031 * var1 != var2
4032 * var1 !~ var2
4033 * var1 > var2
4034 * var1 >= var2
4035 * var1 < var2
4036 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004037 * var1 is var2
4038 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 *
4040 * "arg" must point to the first non-white of the expression.
4041 * "arg" is advanced to the next non-white after the recognized expression.
4042 *
4043 * Return OK or FAIL.
4044 */
4045 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 int evaluate;
4050{
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 char_u *p;
4053 int i;
4054 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004055 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 int len = 2;
4057 long n1, n2;
4058 char_u *s1, *s2;
4059 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4060 regmatch_T regmatch;
4061 int ic;
4062 char_u *save_cpo;
4063
4064 /*
4065 * Get the first variable.
4066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 return FAIL;
4069
4070 p = *arg;
4071 switch (p[0])
4072 {
4073 case '=': if (p[1] == '=')
4074 type = TYPE_EQUAL;
4075 else if (p[1] == '~')
4076 type = TYPE_MATCH;
4077 break;
4078 case '!': if (p[1] == '=')
4079 type = TYPE_NEQUAL;
4080 else if (p[1] == '~')
4081 type = TYPE_NOMATCH;
4082 break;
4083 case '>': if (p[1] != '=')
4084 {
4085 type = TYPE_GREATER;
4086 len = 1;
4087 }
4088 else
4089 type = TYPE_GEQUAL;
4090 break;
4091 case '<': if (p[1] != '=')
4092 {
4093 type = TYPE_SMALLER;
4094 len = 1;
4095 }
4096 else
4097 type = TYPE_SEQUAL;
4098 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004099 case 'i': if (p[1] == 's')
4100 {
4101 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4102 len = 5;
4103 if (!vim_isIDc(p[len]))
4104 {
4105 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4106 type_is = TRUE;
4107 }
4108 }
4109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111
4112 /*
4113 * If there is a comparitive operator, use it.
4114 */
4115 if (type != TYPE_UNKNOWN)
4116 {
4117 /* extra question mark appended: ignore case */
4118 if (p[len] == '?')
4119 {
4120 ic = TRUE;
4121 ++len;
4122 }
4123 /* extra '#' appended: match case */
4124 else if (p[len] == '#')
4125 {
4126 ic = FALSE;
4127 ++len;
4128 }
4129 /* nothing appened: use 'ignorecase' */
4130 else
4131 ic = p_ic;
4132
4133 /*
4134 * Get the second variable.
4135 */
4136 *arg = skipwhite(p + len);
4137 if (eval5(arg, &var2, evaluate) == FAIL)
4138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 return FAIL;
4141 }
4142
4143 if (evaluate)
4144 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004145 if (type_is && rettv->v_type != var2.v_type)
4146 {
4147 /* For "is" a different type always means FALSE, for "notis"
4148 * it means TRUE. */
4149 n1 = (type == TYPE_NEQUAL);
4150 }
4151 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4152 {
4153 if (type_is)
4154 {
4155 n1 = (rettv->v_type == var2.v_type
4156 && rettv->vval.v_list == var2.vval.v_list);
4157 if (type == TYPE_NEQUAL)
4158 n1 = !n1;
4159 }
4160 else if (rettv->v_type != var2.v_type
4161 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4162 {
4163 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004164 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004165 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004166 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 clear_tv(rettv);
4168 clear_tv(&var2);
4169 return FAIL;
4170 }
4171 else
4172 {
4173 /* Compare two Lists for being equal or unequal. */
4174 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4175 if (type == TYPE_NEQUAL)
4176 n1 = !n1;
4177 }
4178 }
4179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004180 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4181 {
4182 if (type_is)
4183 {
4184 n1 = (rettv->v_type == var2.v_type
4185 && rettv->vval.v_dict == var2.vval.v_dict);
4186 if (type == TYPE_NEQUAL)
4187 n1 = !n1;
4188 }
4189 else if (rettv->v_type != var2.v_type
4190 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4191 {
4192 if (rettv->v_type != var2.v_type)
4193 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4194 else
4195 EMSG(_("E736: Invalid operation for Dictionary"));
4196 clear_tv(rettv);
4197 clear_tv(&var2);
4198 return FAIL;
4199 }
4200 else
4201 {
4202 /* Compare two Dictionaries for being equal or unequal. */
4203 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4204 if (type == TYPE_NEQUAL)
4205 n1 = !n1;
4206 }
4207 }
4208
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004209 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4210 {
4211 if (rettv->v_type != var2.v_type
4212 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4213 {
4214 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004215 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004217 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004218 clear_tv(rettv);
4219 clear_tv(&var2);
4220 return FAIL;
4221 }
4222 else
4223 {
4224 /* Compare two Funcrefs for being equal or unequal. */
4225 if (rettv->vval.v_string == NULL
4226 || var2.vval.v_string == NULL)
4227 n1 = FALSE;
4228 else
4229 n1 = STRCMP(rettv->vval.v_string,
4230 var2.vval.v_string) == 0;
4231 if (type == TYPE_NEQUAL)
4232 n1 = !n1;
4233 }
4234 }
4235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 /*
4237 * If one of the two variables is a number, compare as a number.
4238 * When using "=~" or "!~", always compare as string.
4239 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004240 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 n1 = get_tv_number(rettv);
4244 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 switch (type)
4246 {
4247 case TYPE_EQUAL: n1 = (n1 == n2); break;
4248 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4249 case TYPE_GREATER: n1 = (n1 > n2); break;
4250 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4251 case TYPE_SMALLER: n1 = (n1 < n2); break;
4252 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4253 case TYPE_UNKNOWN:
4254 case TYPE_MATCH:
4255 case TYPE_NOMATCH: break; /* avoid gcc warning */
4256 }
4257 }
4258 else
4259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 s1 = get_tv_string_buf(rettv, buf1);
4261 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4263 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4264 else
4265 i = 0;
4266 n1 = FALSE;
4267 switch (type)
4268 {
4269 case TYPE_EQUAL: n1 = (i == 0); break;
4270 case TYPE_NEQUAL: n1 = (i != 0); break;
4271 case TYPE_GREATER: n1 = (i > 0); break;
4272 case TYPE_GEQUAL: n1 = (i >= 0); break;
4273 case TYPE_SMALLER: n1 = (i < 0); break;
4274 case TYPE_SEQUAL: n1 = (i <= 0); break;
4275
4276 case TYPE_MATCH:
4277 case TYPE_NOMATCH:
4278 /* avoid 'l' flag in 'cpoptions' */
4279 save_cpo = p_cpo;
4280 p_cpo = (char_u *)"";
4281 regmatch.regprog = vim_regcomp(s2,
4282 RE_MAGIC + RE_STRING);
4283 regmatch.rm_ic = ic;
4284 if (regmatch.regprog != NULL)
4285 {
4286 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4287 vim_free(regmatch.regprog);
4288 if (type == TYPE_NOMATCH)
4289 n1 = !n1;
4290 }
4291 p_cpo = save_cpo;
4292 break;
4293
4294 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4295 }
4296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 clear_tv(rettv);
4298 clear_tv(&var2);
4299 rettv->v_type = VAR_NUMBER;
4300 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
4303
4304 return OK;
4305}
4306
4307/*
4308 * Handle fourth level expression:
4309 * + number addition
4310 * - number subtraction
4311 * . string concatenation
4312 *
4313 * "arg" must point to the first non-white of the expression.
4314 * "arg" is advanced to the next non-white after the recognized expression.
4315 *
4316 * Return OK or FAIL.
4317 */
4318 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 int evaluate;
4323{
Bram Moolenaar33570922005-01-25 22:26:29 +00004324 typval_T var2;
4325 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int op;
4327 long n1, n2;
4328 char_u *s1, *s2;
4329 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4330 char_u *p;
4331
4332 /*
4333 * Get the first variable.
4334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return FAIL;
4337
4338 /*
4339 * Repeat computing, until no '+', '-' or '.' is following.
4340 */
4341 for (;;)
4342 {
4343 op = **arg;
4344 if (op != '+' && op != '-' && op != '.')
4345 break;
4346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 if (op != '+' || rettv->v_type != VAR_LIST)
4348 {
4349 /* For "list + ...", an illegal use of the first operand as
4350 * a number cannot be determined before evaluating the 2nd
4351 * operand: if this is also a list, all is ok.
4352 * For "something . ...", "something - ..." or "non-list + ...",
4353 * we know that the first operand needs to be a string or number
4354 * without evaluating the 2nd operand. So check before to avoid
4355 * side effects after an error. */
4356 if (evaluate && get_tv_string_chk(rettv) == NULL)
4357 {
4358 clear_tv(rettv);
4359 return FAIL;
4360 }
4361 }
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 /*
4364 * Get the second variable.
4365 */
4366 *arg = skipwhite(*arg + 1);
4367 if (eval6(arg, &var2, evaluate) == FAIL)
4368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 return FAIL;
4371 }
4372
4373 if (evaluate)
4374 {
4375 /*
4376 * Compute the result.
4377 */
4378 if (op == '.')
4379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4381 s2 = get_tv_string_buf_chk(&var2, buf2);
4382 if (s2 == NULL) /* type error ? */
4383 {
4384 clear_tv(rettv);
4385 clear_tv(&var2);
4386 return FAIL;
4387 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004388 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389 clear_tv(rettv);
4390 rettv->v_type = VAR_STRING;
4391 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004393 else if (op == '+' && rettv->v_type == VAR_LIST
4394 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 {
4396 /* concatenate Lists */
4397 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4398 &var3) == FAIL)
4399 {
4400 clear_tv(rettv);
4401 clear_tv(&var2);
4402 return FAIL;
4403 }
4404 clear_tv(rettv);
4405 *rettv = var3;
4406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 else
4408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 int error = FALSE;
4410
4411 n1 = get_tv_number_chk(rettv, &error);
4412 if (error)
4413 {
4414 /* This can only happen for "list + non-list".
4415 * For "non-list + ..." or "something - ...", we returned
4416 * before evaluating the 2nd operand. */
4417 clear_tv(rettv);
4418 return FAIL;
4419 }
4420 n2 = get_tv_number_chk(&var2, &error);
4421 if (error)
4422 {
4423 clear_tv(rettv);
4424 clear_tv(&var2);
4425 return FAIL;
4426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004427 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 if (op == '+')
4429 n1 = n1 + n2;
4430 else
4431 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 rettv->v_type = VAR_NUMBER;
4433 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437 }
4438 return OK;
4439}
4440
4441/*
4442 * Handle fifth level expression:
4443 * * number multiplication
4444 * / number division
4445 * % number modulo
4446 *
4447 * "arg" must point to the first non-white of the expression.
4448 * "arg" is advanced to the next non-white after the recognized expression.
4449 *
4450 * Return OK or FAIL.
4451 */
4452 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004453eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 int evaluate;
4457{
Bram Moolenaar33570922005-01-25 22:26:29 +00004458 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 int op;
4460 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004461 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 /*
4464 * Get the first variable.
4465 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004466 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 return FAIL;
4468
4469 /*
4470 * Repeat computing, until no '*', '/' or '%' is following.
4471 */
4472 for (;;)
4473 {
4474 op = **arg;
4475 if (op != '*' && op != '/' && op != '%')
4476 break;
4477
4478 if (evaluate)
4479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004480 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 if (error)
4483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 else
4486 n1 = 0;
4487
4488 /*
4489 * Get the second variable.
4490 */
4491 *arg = skipwhite(*arg + 1);
4492 if (eval7(arg, &var2, evaluate) == FAIL)
4493 return FAIL;
4494
4495 if (evaluate)
4496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004497 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 if (error)
4500 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 /*
4503 * Compute the result.
4504 */
4505 if (op == '*')
4506 n1 = n1 * n2;
4507 else if (op == '/')
4508 {
4509 if (n2 == 0) /* give an error message? */
4510 n1 = 0x7fffffffL;
4511 else
4512 n1 = n1 / n2;
4513 }
4514 else
4515 {
4516 if (n2 == 0) /* give an error message? */
4517 n1 = 0;
4518 else
4519 n1 = n1 % n2;
4520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 rettv->v_type = VAR_NUMBER;
4522 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 }
4525
4526 return OK;
4527}
4528
4529/*
4530 * Handle sixth level expression:
4531 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004532 * "string" string constant
4533 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 * &option-name option value
4535 * @r register contents
4536 * identifier variable value
4537 * function() function call
4538 * $VAR environment variable
4539 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004540 * [expr, expr] List
4541 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 *
4543 * Also handle:
4544 * ! in front logical NOT
4545 * - in front unary minus
4546 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 * trailing [] subscript in String or List
4548 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 *
4550 * "arg" must point to the first non-white of the expression.
4551 * "arg" is advanced to the next non-white after the recognized expression.
4552 *
4553 * Return OK or FAIL.
4554 */
4555 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004556eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 int evaluate;
4560{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 long n;
4562 int len;
4563 char_u *s;
4564 int val;
4565 char_u *start_leader, *end_leader;
4566 int ret = OK;
4567 char_u *alias;
4568
4569 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004570 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004571 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 /*
4576 * Skip '!' and '-' characters. They are handled later.
4577 */
4578 start_leader = *arg;
4579 while (**arg == '!' || **arg == '-' || **arg == '+')
4580 *arg = skipwhite(*arg + 1);
4581 end_leader = *arg;
4582
4583 switch (**arg)
4584 {
4585 /*
4586 * Number constant.
4587 */
4588 case '0':
4589 case '1':
4590 case '2':
4591 case '3':
4592 case '4':
4593 case '5':
4594 case '6':
4595 case '7':
4596 case '8':
4597 case '9':
4598 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4599 *arg += len;
4600 if (evaluate)
4601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 rettv->v_type = VAR_NUMBER;
4603 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605 break;
4606
4607 /*
4608 * String constant: "string".
4609 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004610 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612
4613 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 break;
4618
4619 /*
4620 * List: [expr, expr]
4621 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
4624
4625 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 * Dictionary: {key: val, key: val}
4627 */
4628 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4629 break;
4630
4631 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004632 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004634 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 break;
4636
4637 /*
4638 * Environment variable: $VAR.
4639 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 break;
4642
4643 /*
4644 * Register contents: @r.
4645 */
4646 case '@': ++*arg;
4647 if (evaluate)
4648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004649 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004650 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 if (**arg != NUL)
4653 ++*arg;
4654 break;
4655
4656 /*
4657 * nested expression: (expression).
4658 */
4659 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (**arg == ')')
4662 ++*arg;
4663 else if (ret == OK)
4664 {
4665 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 ret = FAIL;
4668 }
4669 break;
4670
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 break;
4673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674
4675 if (ret == NOTDONE)
4676 {
4677 /*
4678 * Must be a variable or function name.
4679 * Can also be a curly-braces kind of name: {expr}.
4680 */
4681 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004682 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 if (alias != NULL)
4684 s = alias;
4685
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004686 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 ret = FAIL;
4688 else
4689 {
4690 if (**arg == '(') /* recursive! */
4691 {
4692 /* If "s" is the name of a variable of type VAR_FUNC
4693 * use its contents. */
4694 s = deref_func_name(s, &len);
4695
4696 /* Invoke the function. */
4697 ret = get_func_tv(s, len, rettv, arg,
4698 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004699 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 /* Stop the expression evaluation when immediately
4701 * aborting on error, or when an interrupt occurred or
4702 * an exception was thrown but not caught. */
4703 if (aborting())
4704 {
4705 if (ret == OK)
4706 clear_tv(rettv);
4707 ret = FAIL;
4708 }
4709 }
4710 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004711 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004712 else
4713 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 }
4715
4716 if (alias != NULL)
4717 vim_free(alias);
4718 }
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 *arg = skipwhite(*arg);
4721
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004722 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4723 * expr(expr). */
4724 if (ret == OK)
4725 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
4727 /*
4728 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4729 */
4730 if (ret == OK && evaluate && end_leader > start_leader)
4731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 int error = FALSE;
4733
4734 val = get_tv_number_chk(rettv, &error);
4735 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737 clear_tv(rettv);
4738 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 else
4741 {
4742 while (end_leader > start_leader)
4743 {
4744 --end_leader;
4745 if (*end_leader == '!')
4746 val = !val;
4747 else if (*end_leader == '-')
4748 val = -val;
4749 }
4750 clear_tv(rettv);
4751 rettv->v_type = VAR_NUMBER;
4752 rettv->vval.v_number = val;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
4755
4756 return ret;
4757}
4758
4759/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004760 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4761 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4763 */
4764 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004765eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004769 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770{
4771 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004772 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 long len = -1;
4775 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004781 if (verbose)
4782 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 return FAIL;
4784 }
4785
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 /*
4789 * dict.name
4790 */
4791 key = *arg + 1;
4792 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4793 ;
4794 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004795 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 }
4798 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 /*
4801 * something[idx]
4802 *
4803 * Get the (first) variable from inside the [].
4804 */
4805 *arg = skipwhite(*arg + 1);
4806 if (**arg == ':')
4807 empty1 = TRUE;
4808 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4809 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4811 {
4812 /* not a number or string */
4813 clear_tv(&var1);
4814 return FAIL;
4815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816
4817 /*
4818 * Get the second variable from inside the [:].
4819 */
4820 if (**arg == ':')
4821 {
4822 range = TRUE;
4823 *arg = skipwhite(*arg + 1);
4824 if (**arg == ']')
4825 empty2 = TRUE;
4826 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 if (!empty1)
4829 clear_tv(&var1);
4830 return FAIL;
4831 }
4832 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4833 {
4834 /* not a number or string */
4835 if (!empty1)
4836 clear_tv(&var1);
4837 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004838 return FAIL;
4839 }
4840 }
4841
4842 /* Check for the ']'. */
4843 if (**arg != ']')
4844 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004845 if (verbose)
4846 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 clear_tv(&var1);
4848 if (range)
4849 clear_tv(&var2);
4850 return FAIL;
4851 }
4852 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004853 }
4854
4855 if (evaluate)
4856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004857 n1 = 0;
4858 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 n1 = get_tv_number(&var1);
4861 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 }
4863 if (range)
4864 {
4865 if (empty2)
4866 n2 = -1;
4867 else
4868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 n2 = get_tv_number(&var2);
4870 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 }
4872 }
4873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 {
4876 case VAR_NUMBER:
4877 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 len = (long)STRLEN(s);
4880 if (range)
4881 {
4882 /* The resulting variable is a substring. If the indexes
4883 * are out of range the result is empty. */
4884 if (n1 < 0)
4885 {
4886 n1 = len + n1;
4887 if (n1 < 0)
4888 n1 = 0;
4889 }
4890 if (n2 < 0)
4891 n2 = len + n2;
4892 else if (n2 >= len)
4893 n2 = len;
4894 if (n1 >= len || n2 < 0 || n1 > n2)
4895 s = NULL;
4896 else
4897 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4898 }
4899 else
4900 {
4901 /* The resulting variable is a string of a single
4902 * character. If the index is too big or negative the
4903 * result is empty. */
4904 if (n1 >= len || n1 < 0)
4905 s = NULL;
4906 else
4907 s = vim_strnsave(s + n1, 1);
4908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 clear_tv(rettv);
4910 rettv->v_type = VAR_STRING;
4911 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004912 break;
4913
4914 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916 if (n1 < 0)
4917 n1 = len + n1;
4918 if (!empty1 && (n1 < 0 || n1 >= len))
4919 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004920 /* For a range we allow invalid values and return an empty
4921 * list. A list index out of range is an error. */
4922 if (!range)
4923 {
4924 if (verbose)
4925 EMSGN(_(e_listidx), n1);
4926 return FAIL;
4927 }
4928 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004929 }
4930 if (range)
4931 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004932 list_T *l;
4933 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934
4935 if (n2 < 0)
4936 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004937 else if (n2 >= len)
4938 n2 = len - 1;
4939 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004940 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004941 l = list_alloc();
4942 if (l == NULL)
4943 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004944 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 n1 <= n2; ++n1)
4946 {
4947 if (list_append_tv(l, &item->li_tv) == FAIL)
4948 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004949 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004950 return FAIL;
4951 }
4952 item = item->li_next;
4953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004954 clear_tv(rettv);
4955 rettv->v_type = VAR_LIST;
4956 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004957 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958 }
4959 else
4960 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004961 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004962 clear_tv(rettv);
4963 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004964 }
4965 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966
4967 case VAR_DICT:
4968 if (range)
4969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004970 if (verbose)
4971 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 if (len == -1)
4973 clear_tv(&var1);
4974 return FAIL;
4975 }
4976 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004977 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978
4979 if (len == -1)
4980 {
4981 key = get_tv_string(&var1);
4982 if (*key == NUL)
4983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004984 if (verbose)
4985 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 clear_tv(&var1);
4987 return FAIL;
4988 }
4989 }
4990
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004991 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004994 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 if (len == -1)
4996 clear_tv(&var1);
4997 if (item == NULL)
4998 return FAIL;
4999
5000 copy_tv(&item->di_tv, &var1);
5001 clear_tv(rettv);
5002 *rettv = var1;
5003 }
5004 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 }
5006 }
5007
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 return OK;
5009}
5010
5011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 * Get an option value.
5013 * "arg" points to the '&' or '+' before the option name.
5014 * "arg" is advanced to character after the option name.
5015 * Return OK or FAIL.
5016 */
5017 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005020 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 int evaluate;
5022{
5023 char_u *option_end;
5024 long numval;
5025 char_u *stringval;
5026 int opt_type;
5027 int c;
5028 int working = (**arg == '+'); /* has("+option") */
5029 int ret = OK;
5030 int opt_flags;
5031
5032 /*
5033 * Isolate the option name and find its value.
5034 */
5035 option_end = find_option_end(arg, &opt_flags);
5036 if (option_end == NULL)
5037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005038 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 EMSG2(_("E112: Option name missing: %s"), *arg);
5040 return FAIL;
5041 }
5042
5043 if (!evaluate)
5044 {
5045 *arg = option_end;
5046 return OK;
5047 }
5048
5049 c = *option_end;
5050 *option_end = NUL;
5051 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
5054 if (opt_type == -3) /* invalid name */
5055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 EMSG2(_("E113: Unknown option: %s"), *arg);
5058 ret = FAIL;
5059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 if (opt_type == -2) /* hidden string option */
5063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 rettv->v_type = VAR_STRING;
5065 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067 else if (opt_type == -1) /* hidden number option */
5068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 rettv->v_type = VAR_NUMBER;
5070 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 else if (opt_type == 1) /* number option */
5073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 rettv->v_type = VAR_NUMBER;
5075 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 else /* string option */
5078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 rettv->v_type = VAR_STRING;
5080 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 }
5083 else if (working && (opt_type == -2 || opt_type == -1))
5084 ret = FAIL;
5085
5086 *option_end = c; /* put back for error messages */
5087 *arg = option_end;
5088
5089 return ret;
5090}
5091
5092/*
5093 * Allocate a variable for a string constant.
5094 * Return OK or FAIL.
5095 */
5096 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 int evaluate;
5101{
5102 char_u *p;
5103 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 int extra = 0;
5105
5106 /*
5107 * Find the end of the string, skipping backslashed characters.
5108 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 if (*p == '\\' && p[1] != NUL)
5112 {
5113 ++p;
5114 /* A "\<x>" form occupies at least 4 characters, and produces up
5115 * to 6 characters: reserve space for 2 extra */
5116 if (*p == '<')
5117 extra += 2;
5118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120
5121 if (*p != '"')
5122 {
5123 EMSG2(_("E114: Missing quote: %s"), *arg);
5124 return FAIL;
5125 }
5126
5127 /* If only parsing, set *arg and return here */
5128 if (!evaluate)
5129 {
5130 *arg = p + 1;
5131 return OK;
5132 }
5133
5134 /*
5135 * Copy the string into allocated memory, handling backslashed
5136 * characters.
5137 */
5138 name = alloc((unsigned)(p - *arg + extra));
5139 if (name == NULL)
5140 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 rettv->v_type = VAR_STRING;
5142 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 if (*p == '\\')
5147 {
5148 switch (*++p)
5149 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 case 'b': *name++ = BS; ++p; break;
5151 case 'e': *name++ = ESC; ++p; break;
5152 case 'f': *name++ = FF; ++p; break;
5153 case 'n': *name++ = NL; ++p; break;
5154 case 'r': *name++ = CAR; ++p; break;
5155 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
5157 case 'X': /* hex: "\x1", "\x12" */
5158 case 'x':
5159 case 'u': /* Unicode: "\u0023" */
5160 case 'U':
5161 if (vim_isxdigit(p[1]))
5162 {
5163 int n, nr;
5164 int c = toupper(*p);
5165
5166 if (c == 'X')
5167 n = 2;
5168 else
5169 n = 4;
5170 nr = 0;
5171 while (--n >= 0 && vim_isxdigit(p[1]))
5172 {
5173 ++p;
5174 nr = (nr << 4) + hex2nr(*p);
5175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177#ifdef FEAT_MBYTE
5178 /* For "\u" store the number according to
5179 * 'encoding'. */
5180 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 else
5183#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187
5188 /* octal: "\1", "\12", "\123" */
5189 case '0':
5190 case '1':
5191 case '2':
5192 case '3':
5193 case '4':
5194 case '5':
5195 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 case '7': *name = *p++ - '0';
5197 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 *name = (*name << 3) + *p++ - '0';
5200 if (*p >= '0' && *p <= '7')
5201 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 break;
5205
5206 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (extra != 0)
5209 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212 }
5213 /* FALLTHROUGH */
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 break;
5217 }
5218 }
5219 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 *arg = p + 1;
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 return OK;
5227}
5228
5229/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 * Return OK or FAIL.
5232 */
5233 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005234get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 int evaluate;
5238{
5239 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005240 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 int reduce = 0;
5242
5243 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005245 */
5246 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5247 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005251 break;
5252 ++reduce;
5253 ++p;
5254 }
5255 }
5256
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005258 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005260 return FAIL;
5261 }
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005264 if (!evaluate)
5265 {
5266 *arg = p + 1;
5267 return OK;
5268 }
5269
5270 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005272 */
5273 str = alloc((unsigned)((p - *arg) - reduce));
5274 if (str == NULL)
5275 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 rettv->v_type = VAR_STRING;
5277 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005278
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005280 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005282 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005284 break;
5285 ++p;
5286 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005290 *arg = p + 1;
5291
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005292 return OK;
5293}
5294
5295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 * Allocate a variable for a List and fill it from "*arg".
5297 * Return OK or FAIL.
5298 */
5299 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005300get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005302 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 int evaluate;
5304{
Bram Moolenaar33570922005-01-25 22:26:29 +00005305 list_T *l = NULL;
5306 typval_T tv;
5307 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308
5309 if (evaluate)
5310 {
5311 l = list_alloc();
5312 if (l == NULL)
5313 return FAIL;
5314 }
5315
5316 *arg = skipwhite(*arg + 1);
5317 while (**arg != ']' && **arg != NUL)
5318 {
5319 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5320 goto failret;
5321 if (evaluate)
5322 {
5323 item = listitem_alloc();
5324 if (item != NULL)
5325 {
5326 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005327 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 list_append(l, item);
5329 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005330 else
5331 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 }
5333
5334 if (**arg == ']')
5335 break;
5336 if (**arg != ',')
5337 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005338 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 goto failret;
5340 }
5341 *arg = skipwhite(*arg + 1);
5342 }
5343
5344 if (**arg != ']')
5345 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347failret:
5348 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005349 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
5351 }
5352
5353 *arg = skipwhite(*arg + 1);
5354 if (evaluate)
5355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 rettv->v_type = VAR_LIST;
5357 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 ++l->lv_refcount;
5359 }
5360
5361 return OK;
5362}
5363
5364/*
5365 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005366 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005368 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369list_alloc()
5370{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005371 list_T *l;
5372
5373 l = (list_T *)alloc_clear(sizeof(list_T));
5374 if (l != NULL)
5375 {
5376 /* Prepend the list to the list of lists for garbage collection. */
5377 if (first_list != NULL)
5378 first_list->lv_used_prev = l;
5379 l->lv_used_prev = NULL;
5380 l->lv_used_next = first_list;
5381 first_list = l;
5382 }
5383 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384}
5385
5386/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005387 * Allocate an empty list for a return value.
5388 * Returns OK or FAIL.
5389 */
5390 static int
5391rettv_list_alloc(rettv)
5392 typval_T *rettv;
5393{
5394 list_T *l = list_alloc();
5395
5396 if (l == NULL)
5397 return FAIL;
5398
5399 rettv->vval.v_list = l;
5400 rettv->v_type = VAR_LIST;
5401 ++l->lv_refcount;
5402 return OK;
5403}
5404
5405/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 * Unreference a list: decrement the reference count and free it when it
5407 * becomes zero.
5408 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005409 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005413 if (l != NULL && --l->lv_refcount <= 0)
5414 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415}
5416
5417/*
5418 * Free a list, including all items it points to.
5419 * Ignores the reference count.
5420 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005421 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005422list_free(l, recurse)
5423 list_T *l;
5424 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425{
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005428 /* Remove the list from the list of lists for garbage collection. */
5429 if (l->lv_used_prev == NULL)
5430 first_list = l->lv_used_next;
5431 else
5432 l->lv_used_prev->lv_used_next = l->lv_used_next;
5433 if (l->lv_used_next != NULL)
5434 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5435
Bram Moolenaard9fba312005-06-26 22:34:35 +00005436 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005438 /* Remove the item before deleting it. */
5439 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005440 if (recurse || (item->li_tv.v_type != VAR_LIST
5441 && item->li_tv.v_type != VAR_DICT))
5442 clear_tv(&item->li_tv);
5443 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 vim_free(l);
5446}
5447
5448/*
5449 * Allocate a list item.
5450 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005451 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452listitem_alloc()
5453{
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455}
5456
5457/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005458 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 */
5460 static void
5461listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 vim_free(item);
5466}
5467
5468/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005469 * Remove a list item from a List and free it. Also clears the value.
5470 */
5471 static void
5472listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 list_T *l;
5474 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005475{
5476 list_remove(l, item, item);
5477 listitem_free(item);
5478}
5479
5480/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 * Get the number of items in a list.
5482 */
5483 static long
5484list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005485 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 if (l == NULL)
5488 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005489 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490}
5491
5492/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005493 * Return TRUE when two lists have exactly the same values.
5494 */
5495 static int
5496list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 list_T *l1;
5498 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005499 int ic; /* ignore case for strings */
5500{
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005502
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005503 if (l1 == l2)
5504 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005505 if (list_len(l1) != list_len(l2))
5506 return FALSE;
5507
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005508 for (item1 = l1->lv_first, item2 = l2->lv_first;
5509 item1 != NULL && item2 != NULL;
5510 item1 = item1->li_next, item2 = item2->li_next)
5511 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5512 return FALSE;
5513 return item1 == NULL && item2 == NULL;
5514}
5515
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005516#if defined(FEAT_PYTHON) || defined(PROTO)
5517/*
5518 * Return the dictitem that an entry in a hashtable points to.
5519 */
5520 dictitem_T *
5521dict_lookup(hi)
5522 hashitem_T *hi;
5523{
5524 return HI2DI(hi);
5525}
5526#endif
5527
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005528/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005529 * Return TRUE when two dictionaries have exactly the same key/values.
5530 */
5531 static int
5532dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005533 dict_T *d1;
5534 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005535 int ic; /* ignore case for strings */
5536{
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 hashitem_T *hi;
5538 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005539 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005540
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005541 if (d1 == d2)
5542 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005543 if (dict_len(d1) != dict_len(d2))
5544 return FALSE;
5545
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005546 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005547 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 if (!HASHITEM_EMPTY(hi))
5550 {
5551 item2 = dict_find(d2, hi->hi_key, -1);
5552 if (item2 == NULL)
5553 return FALSE;
5554 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5555 return FALSE;
5556 --todo;
5557 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005558 }
5559 return TRUE;
5560}
5561
5562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005563 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005564 * Compares the items just like "==" would compare them, but strings and
5565 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005566 */
5567 static int
5568tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005569 typval_T *tv1;
5570 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005571 int ic; /* ignore case */
5572{
5573 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005574 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005575 static int recursive = 0; /* cach recursive loops */
5576 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005577
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005578 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005579 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005580 /* Catch lists and dicts that have an endless loop by limiting
5581 * recursiveness to 1000. We guess they are equal then. */
5582 if (recursive >= 1000)
5583 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005584
5585 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005586 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005587 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005588 ++recursive;
5589 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5590 --recursive;
5591 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005592
5593 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005594 ++recursive;
5595 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5596 --recursive;
5597 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005598
5599 case VAR_FUNC:
5600 return (tv1->vval.v_string != NULL
5601 && tv2->vval.v_string != NULL
5602 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5603
5604 case VAR_NUMBER:
5605 return tv1->vval.v_number == tv2->vval.v_number;
5606
5607 case VAR_STRING:
5608 s1 = get_tv_string_buf(tv1, buf1);
5609 s2 = get_tv_string_buf(tv2, buf2);
5610 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005611 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005612
5613 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005614 return TRUE;
5615}
5616
5617/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 * Locate item with index "n" in list "l" and return it.
5619 * A negative index is counted from the end; -1 is the last item.
5620 * Returns NULL when "n" is out of range.
5621 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625 long n;
5626{
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 long idx;
5629
5630 if (l == NULL)
5631 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005632
5633 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005635 n = l->lv_len + n;
5636
5637 /* Check for index out of range. */
5638 if (n < 0 || n >= l->lv_len)
5639 return NULL;
5640
5641 /* When there is a cached index may start search from there. */
5642 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005643 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005644 if (n < l->lv_idx / 2)
5645 {
5646 /* closest to the start of the list */
5647 item = l->lv_first;
5648 idx = 0;
5649 }
5650 else if (n > (l->lv_idx + l->lv_len) / 2)
5651 {
5652 /* closest to the end of the list */
5653 item = l->lv_last;
5654 idx = l->lv_len - 1;
5655 }
5656 else
5657 {
5658 /* closest to the cached index */
5659 item = l->lv_idx_item;
5660 idx = l->lv_idx;
5661 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 }
5663 else
5664 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005665 if (n < l->lv_len / 2)
5666 {
5667 /* closest to the start of the list */
5668 item = l->lv_first;
5669 idx = 0;
5670 }
5671 else
5672 {
5673 /* closest to the end of the list */
5674 item = l->lv_last;
5675 idx = l->lv_len - 1;
5676 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005678
5679 while (n > idx)
5680 {
5681 /* search forward */
5682 item = item->li_next;
5683 ++idx;
5684 }
5685 while (n < idx)
5686 {
5687 /* search backward */
5688 item = item->li_prev;
5689 --idx;
5690 }
5691
5692 /* cache the used index */
5693 l->lv_idx = idx;
5694 l->lv_idx_item = item;
5695
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 return item;
5697}
5698
5699/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005700 * Get list item "l[idx]" as a number.
5701 */
5702 static long
5703list_find_nr(l, idx, errorp)
5704 list_T *l;
5705 long idx;
5706 int *errorp; /* set to TRUE when something wrong */
5707{
5708 listitem_T *li;
5709
5710 li = list_find(l, idx);
5711 if (li == NULL)
5712 {
5713 if (errorp != NULL)
5714 *errorp = TRUE;
5715 return -1L;
5716 }
5717 return get_tv_number_chk(&li->li_tv, errorp);
5718}
5719
5720/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005721 * Locate "item" list "l" and return its index.
5722 * Returns -1 when "item" is not in the list.
5723 */
5724 static long
5725list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 list_T *l;
5727 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005728{
5729 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005731
5732 if (l == NULL)
5733 return -1;
5734 idx = 0;
5735 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5736 ++idx;
5737 if (li == NULL)
5738 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005739 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005740}
5741
5742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743 * Append item "item" to the end of list "l".
5744 */
5745 static void
5746list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005747 list_T *l;
5748 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749{
5750 if (l->lv_last == NULL)
5751 {
5752 /* empty list */
5753 l->lv_first = item;
5754 l->lv_last = item;
5755 item->li_prev = NULL;
5756 }
5757 else
5758 {
5759 l->lv_last->li_next = item;
5760 item->li_prev = l->lv_last;
5761 l->lv_last = item;
5762 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005763 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 item->li_next = NULL;
5765}
5766
5767/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005769 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 */
5771 static int
5772list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005773 list_T *l;
5774 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005776 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777
Bram Moolenaar05159a02005-02-26 23:04:13 +00005778 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005780 copy_tv(tv, &li->li_tv);
5781 list_append(l, li);
5782 return OK;
5783}
5784
5785/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005786 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005787 * Return FAIL when out of memory.
5788 */
5789 int
5790list_append_dict(list, dict)
5791 list_T *list;
5792 dict_T *dict;
5793{
5794 listitem_T *li = listitem_alloc();
5795
5796 if (li == NULL)
5797 return FAIL;
5798 li->li_tv.v_type = VAR_DICT;
5799 li->li_tv.v_lock = 0;
5800 li->li_tv.vval.v_dict = dict;
5801 list_append(list, li);
5802 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 return OK;
5804}
5805
5806/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005807 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005808 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005809 * Returns FAIL when out of memory.
5810 */
5811 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005812list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005813 list_T *l;
5814 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005815 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005816{
5817 listitem_T *li = listitem_alloc();
5818
5819 if (li == NULL)
5820 return FAIL;
5821 list_append(l, li);
5822 li->li_tv.v_type = VAR_STRING;
5823 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005824 if (str == NULL)
5825 li->li_tv.vval.v_string = NULL;
5826 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005827 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005828 return FAIL;
5829 return OK;
5830}
5831
5832/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005833 * Append "n" to list "l".
5834 * Returns FAIL when out of memory.
5835 */
5836 static int
5837list_append_number(l, n)
5838 list_T *l;
5839 varnumber_T n;
5840{
5841 listitem_T *li;
5842
5843 li = listitem_alloc();
5844 if (li == NULL)
5845 return FAIL;
5846 li->li_tv.v_type = VAR_NUMBER;
5847 li->li_tv.v_lock = 0;
5848 li->li_tv.vval.v_number = n;
5849 list_append(l, li);
5850 return OK;
5851}
5852
5853/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005855 * If "item" is NULL append at the end.
5856 * Return FAIL when out of memory.
5857 */
5858 static int
5859list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 list_T *l;
5861 typval_T *tv;
5862 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863{
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005865
5866 if (ni == NULL)
5867 return FAIL;
5868 copy_tv(tv, &ni->li_tv);
5869 if (item == NULL)
5870 /* Append new item at end of list. */
5871 list_append(l, ni);
5872 else
5873 {
5874 /* Insert new item before existing item. */
5875 ni->li_prev = item->li_prev;
5876 ni->li_next = item;
5877 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005878 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005880 ++l->lv_idx;
5881 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005883 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005884 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005885 l->lv_idx_item = NULL;
5886 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005888 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889 }
5890 return OK;
5891}
5892
5893/*
5894 * Extend "l1" with "l2".
5895 * If "bef" is NULL append at the end, otherwise insert before this item.
5896 * Returns FAIL when out of memory.
5897 */
5898 static int
5899list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 list_T *l1;
5901 list_T *l2;
5902 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005903{
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005905
5906 for (item = l2->lv_first; item != NULL; item = item->li_next)
5907 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5908 return FAIL;
5909 return OK;
5910}
5911
5912/*
5913 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5914 * Return FAIL when out of memory.
5915 */
5916 static int
5917list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 list_T *l1;
5919 list_T *l2;
5920 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005921{
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923
5924 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005925 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926 if (l == NULL)
5927 return FAIL;
5928 tv->v_type = VAR_LIST;
5929 tv->vval.v_list = l;
5930
5931 /* append all items from the second list */
5932 return list_extend(l, l2, NULL);
5933}
5934
5935/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005938 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 * Returns NULL when out of memory.
5940 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005945 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946{
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 list_T *copy;
5948 listitem_T *item;
5949 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950
5951 if (orig == NULL)
5952 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953
5954 copy = list_alloc();
5955 if (copy != NULL)
5956 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005957 if (copyID != 0)
5958 {
5959 /* Do this before adding the items, because one of the items may
5960 * refer back to this list. */
5961 orig->lv_copyID = copyID;
5962 orig->lv_copylist = copy;
5963 }
5964 for (item = orig->lv_first; item != NULL && !got_int;
5965 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 {
5967 ni = listitem_alloc();
5968 if (ni == NULL)
5969 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005970 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005971 {
5972 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5973 {
5974 vim_free(ni);
5975 break;
5976 }
5977 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 list_append(copy, ni);
5981 }
5982 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005983 if (item != NULL)
5984 {
5985 list_unref(copy);
5986 copy = NULL;
5987 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 }
5989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 return copy;
5991}
5992
5993/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005994 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005995 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005998list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 list_T *l;
6000 listitem_T *item;
6001 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002{
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005 /* notify watchers */
6006 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006008 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 list_fix_watch(l, ip);
6010 if (ip == item2)
6011 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013
6014 if (item2->li_next == NULL)
6015 l->lv_last = item->li_prev;
6016 else
6017 item2->li_next->li_prev = item->li_prev;
6018 if (item->li_prev == NULL)
6019 l->lv_first = item2->li_next;
6020 else
6021 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006022 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023}
6024
6025/*
6026 * Return an allocated string with the string representation of a list.
6027 * May return NULL.
6028 */
6029 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006030list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033{
6034 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035
6036 if (tv->vval.v_list == NULL)
6037 return NULL;
6038 ga_init2(&ga, (int)sizeof(char), 80);
6039 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006040 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006041 {
6042 vim_free(ga.ga_data);
6043 return NULL;
6044 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 ga_append(&ga, ']');
6046 ga_append(&ga, NUL);
6047 return (char_u *)ga.ga_data;
6048}
6049
6050/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006051 * Join list "l" into a string in "*gap", using separator "sep".
6052 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006053 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006056list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006059 char_u *sep;
6060 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006061 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006062{
6063 int first = TRUE;
6064 char_u *tofree;
6065 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006067 char_u *s;
6068
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006069 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006070 {
6071 if (first)
6072 first = FALSE;
6073 else
6074 ga_concat(gap, sep);
6075
6076 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006077 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006078 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006079 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006080 if (s != NULL)
6081 ga_concat(gap, s);
6082 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006083 if (s == NULL)
6084 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006085 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006086 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087}
6088
6089/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006090 * Garbage collection for lists and dictionaries.
6091 *
6092 * We use reference counts to be able to free most items right away when they
6093 * are no longer used. But for composite items it's possible that it becomes
6094 * unused while the reference count is > 0: When there is a recursive
6095 * reference. Example:
6096 * :let l = [1, 2, 3]
6097 * :let d = {9: l}
6098 * :let l[1] = d
6099 *
6100 * Since this is quite unusual we handle this with garbage collection: every
6101 * once in a while find out which lists and dicts are not referenced from any
6102 * variable.
6103 *
6104 * Here is a good reference text about garbage collection (refers to Python
6105 * but it applies to all reference-counting mechanisms):
6106 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006107 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006108
6109/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 * Do garbage collection for lists and dicts.
6111 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006112 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 int
6114garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006115{
6116 dict_T *dd;
6117 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006118 int copyID = ++current_copyID;
6119 buf_T *buf;
6120 win_T *wp;
6121 int i;
6122 funccall_T *fc;
6123 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006124#ifdef FEAT_WINDOWS
6125 tabpage_T *tp;
6126#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006127
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006128 /* Only do this once. */
6129 want_garbage_collect = FALSE;
6130 may_garbage_collect = FALSE;
6131
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006132 /*
6133 * 1. Go through all accessible variables and mark all lists and dicts
6134 * with copyID.
6135 */
6136 /* script-local variables */
6137 for (i = 1; i <= ga_scripts.ga_len; ++i)
6138 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6139
6140 /* buffer-local variables */
6141 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6142 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6143
6144 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006145 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006146 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6147
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006148#ifdef FEAT_WINDOWS
6149 /* tabpage-local variables */
6150 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6151 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6152#endif
6153
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006154 /* global variables */
6155 set_ref_in_ht(&globvarht, copyID);
6156
6157 /* function-local variables */
6158 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6159 {
6160 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6161 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6162 }
6163
6164 /*
6165 * 2. Go through the list of dicts and free items without the copyID.
6166 */
6167 for (dd = first_dict; dd != NULL; )
6168 if (dd->dv_copyID != copyID)
6169 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006170 /* Free the Dictionary and ordinary items it contains, but don't
6171 * recurse into Lists and Dictionaries, they will be in the list
6172 * of dicts or list of lists. */
6173 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006174 did_free = TRUE;
6175
6176 /* restart, next dict may also have been freed */
6177 dd = first_dict;
6178 }
6179 else
6180 dd = dd->dv_used_next;
6181
6182 /*
6183 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006184 * But don't free a list that has a watcher (used in a for loop), these
6185 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006186 */
6187 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006188 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006189 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006190 /* Free the List and ordinary items it contains, but don't recurse
6191 * into Lists and Dictionaries, they will be in the list of dicts
6192 * or list of lists. */
6193 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006194 did_free = TRUE;
6195
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006196 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006197 ll = first_list;
6198 }
6199 else
6200 ll = ll->lv_used_next;
6201
6202 return did_free;
6203}
6204
6205/*
6206 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6207 */
6208 static void
6209set_ref_in_ht(ht, copyID)
6210 hashtab_T *ht;
6211 int copyID;
6212{
6213 int todo;
6214 hashitem_T *hi;
6215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006216 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006217 for (hi = ht->ht_array; todo > 0; ++hi)
6218 if (!HASHITEM_EMPTY(hi))
6219 {
6220 --todo;
6221 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6222 }
6223}
6224
6225/*
6226 * Mark all lists and dicts referenced through list "l" with "copyID".
6227 */
6228 static void
6229set_ref_in_list(l, copyID)
6230 list_T *l;
6231 int copyID;
6232{
6233 listitem_T *li;
6234
6235 for (li = l->lv_first; li != NULL; li = li->li_next)
6236 set_ref_in_item(&li->li_tv, copyID);
6237}
6238
6239/*
6240 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6241 */
6242 static void
6243set_ref_in_item(tv, copyID)
6244 typval_T *tv;
6245 int copyID;
6246{
6247 dict_T *dd;
6248 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006249
6250 switch (tv->v_type)
6251 {
6252 case VAR_DICT:
6253 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006254 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006255 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006256 /* Didn't see this dict yet. */
6257 dd->dv_copyID = copyID;
6258 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006259 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006260 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006261
6262 case VAR_LIST:
6263 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006264 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006265 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006266 /* Didn't see this list yet. */
6267 ll->lv_copyID = copyID;
6268 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006269 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006270 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006271 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006272 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006273}
6274
6275/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276 * Allocate an empty header for a dictionary.
6277 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006278 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006279dict_alloc()
6280{
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006282
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006285 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006286 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006287 if (first_dict != NULL)
6288 first_dict->dv_used_prev = d;
6289 d->dv_used_next = first_dict;
6290 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006291 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006292
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006294 d->dv_lock = 0;
6295 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006296 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006297 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006298 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006299}
6300
6301/*
6302 * Unreference a Dictionary: decrement the reference count and free it when it
6303 * becomes zero.
6304 */
6305 static void
6306dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006307 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006308{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006309 if (d != NULL && --d->dv_refcount <= 0)
6310 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006311}
6312
6313/*
6314 * Free a Dictionary, including all items it contains.
6315 * Ignores the reference count.
6316 */
6317 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006318dict_free(d, recurse)
6319 dict_T *d;
6320 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006324 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006325
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006326 /* Remove the dict from the list of dicts for garbage collection. */
6327 if (d->dv_used_prev == NULL)
6328 first_dict = d->dv_used_next;
6329 else
6330 d->dv_used_prev->dv_used_next = d->dv_used_next;
6331 if (d->dv_used_next != NULL)
6332 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6333
6334 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006335 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006336 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006339 if (!HASHITEM_EMPTY(hi))
6340 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006341 /* Remove the item before deleting it, just in case there is
6342 * something recursive causing trouble. */
6343 di = HI2DI(hi);
6344 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006345 if (recurse || (di->di_tv.v_type != VAR_LIST
6346 && di->di_tv.v_type != VAR_DICT))
6347 clear_tv(&di->di_tv);
6348 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006349 --todo;
6350 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 vim_free(d);
6354}
6355
6356/*
6357 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006358 * The "key" is copied to the new item.
6359 * Note that the value of the item "di_tv" still needs to be initialized!
6360 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006361 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006362 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006363dictitem_alloc(key)
6364 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365{
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006368 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006372 di->di_flags = 0;
6373 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375}
6376
6377/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006378 * Make a copy of a Dictionary item.
6379 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006381dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383{
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006386 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6387 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388 if (di != NULL)
6389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006390 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006391 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006392 copy_tv(&org->di_tv, &di->di_tv);
6393 }
6394 return di;
6395}
6396
6397/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006398 * Remove item "item" from Dictionary "dict" and free it.
6399 */
6400 static void
6401dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 dict_T *dict;
6403 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404{
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 if (HASHITEM_EMPTY(hi))
6409 EMSG2(_(e_intern2), "dictitem_remove()");
6410 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412 dictitem_free(item);
6413}
6414
6415/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006416 * Free a dict item. Also clears the value.
6417 */
6418 static void
6419dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006422 clear_tv(&item->di_tv);
6423 vim_free(item);
6424}
6425
6426/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006427 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6428 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006429 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006430 * Returns NULL when out of memory.
6431 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006432 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006433dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006435 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006436 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437{
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 dict_T *copy;
6439 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006440 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006441 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006442
6443 if (orig == NULL)
6444 return NULL;
6445
6446 copy = dict_alloc();
6447 if (copy != NULL)
6448 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006449 if (copyID != 0)
6450 {
6451 orig->dv_copyID = copyID;
6452 orig->dv_copydict = copy;
6453 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006454 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006455 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006456 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006457 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006459 --todo;
6460
6461 di = dictitem_alloc(hi->hi_key);
6462 if (di == NULL)
6463 break;
6464 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006465 {
6466 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6467 copyID) == FAIL)
6468 {
6469 vim_free(di);
6470 break;
6471 }
6472 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006473 else
6474 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6475 if (dict_add(copy, di) == FAIL)
6476 {
6477 dictitem_free(di);
6478 break;
6479 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006480 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006482
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 if (todo > 0)
6485 {
6486 dict_unref(copy);
6487 copy = NULL;
6488 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 }
6490
6491 return copy;
6492}
6493
6494/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006495 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006496 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 dict_T *d;
6501 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502{
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504}
6505
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006507 * Add a number or string entry to dictionary "d".
6508 * When "str" is NULL use number "nr", otherwise use "str".
6509 * Returns FAIL when out of memory and when key already exists.
6510 */
6511 int
6512dict_add_nr_str(d, key, nr, str)
6513 dict_T *d;
6514 char *key;
6515 long nr;
6516 char_u *str;
6517{
6518 dictitem_T *item;
6519
6520 item = dictitem_alloc((char_u *)key);
6521 if (item == NULL)
6522 return FAIL;
6523 item->di_tv.v_lock = 0;
6524 if (str == NULL)
6525 {
6526 item->di_tv.v_type = VAR_NUMBER;
6527 item->di_tv.vval.v_number = nr;
6528 }
6529 else
6530 {
6531 item->di_tv.v_type = VAR_STRING;
6532 item->di_tv.vval.v_string = vim_strsave(str);
6533 }
6534 if (dict_add(d, item) == FAIL)
6535 {
6536 dictitem_free(item);
6537 return FAIL;
6538 }
6539 return OK;
6540}
6541
6542/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006543 * Get the number of items in a Dictionary.
6544 */
6545 static long
6546dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 if (d == NULL)
6550 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006551 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552}
6553
6554/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006555 * Find item "key[len]" in Dictionary "d".
6556 * If "len" is negative use strlen(key).
6557 * Returns NULL when not found.
6558 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006560dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006562 char_u *key;
6563 int len;
6564{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006565#define AKEYLEN 200
6566 char_u buf[AKEYLEN];
6567 char_u *akey;
6568 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006571 if (len < 0)
6572 akey = key;
6573 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006574 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006575 tofree = akey = vim_strnsave(key, len);
6576 if (akey == NULL)
6577 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006578 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006579 else
6580 {
6581 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006582 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006583 akey = buf;
6584 }
6585
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006587 vim_free(tofree);
6588 if (HASHITEM_EMPTY(hi))
6589 return NULL;
6590 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006591}
6592
6593/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006594 * Get a string item from a dictionary.
6595 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006596 * Returns NULL if the entry doesn't exist or out of memory.
6597 */
6598 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006599get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006600 dict_T *d;
6601 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006602 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006603{
6604 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006605 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006606
6607 di = dict_find(d, key, -1);
6608 if (di == NULL)
6609 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006610 s = get_tv_string(&di->di_tv);
6611 if (save && s != NULL)
6612 s = vim_strsave(s);
6613 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006614}
6615
6616/*
6617 * Get a number item from a dictionary.
6618 * Returns 0 if the entry doesn't exist or out of memory.
6619 */
6620 long
6621get_dict_number(d, key)
6622 dict_T *d;
6623 char_u *key;
6624{
6625 dictitem_T *di;
6626
6627 di = dict_find(d, key, -1);
6628 if (di == NULL)
6629 return 0;
6630 return get_tv_number(&di->di_tv);
6631}
6632
6633/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006634 * Return an allocated string with the string representation of a Dictionary.
6635 * May return NULL.
6636 */
6637 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006638dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006639 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006640 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006641{
6642 garray_T ga;
6643 int first = TRUE;
6644 char_u *tofree;
6645 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006648 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006649 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006650
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006651 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006652 return NULL;
6653 ga_init2(&ga, (int)sizeof(char), 80);
6654 ga_append(&ga, '{');
6655
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006656 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006657 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006659 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 --todo;
6662
6663 if (first)
6664 first = FALSE;
6665 else
6666 ga_concat(&ga, (char_u *)", ");
6667
6668 tofree = string_quote(hi->hi_key, FALSE);
6669 if (tofree != NULL)
6670 {
6671 ga_concat(&ga, tofree);
6672 vim_free(tofree);
6673 }
6674 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006675 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006676 if (s != NULL)
6677 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006678 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679 if (s == NULL)
6680 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006682 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006683 if (todo > 0)
6684 {
6685 vim_free(ga.ga_data);
6686 return NULL;
6687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006688
6689 ga_append(&ga, '}');
6690 ga_append(&ga, NUL);
6691 return (char_u *)ga.ga_data;
6692}
6693
6694/*
6695 * Allocate a variable for a Dictionary and fill it from "*arg".
6696 * Return OK or FAIL. Returns NOTDONE for {expr}.
6697 */
6698 static int
6699get_dict_tv(arg, rettv, evaluate)
6700 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006701 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006702 int evaluate;
6703{
Bram Moolenaar33570922005-01-25 22:26:29 +00006704 dict_T *d = NULL;
6705 typval_T tvkey;
6706 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00006707 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006709 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006710 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711
6712 /*
6713 * First check if it's not a curly-braces thing: {expr}.
6714 * Must do this without evaluating, otherwise a function may be called
6715 * twice. Unfortunately this means we need to call eval1() twice for the
6716 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006717 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006718 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006719 if (*start != '}')
6720 {
6721 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6722 return FAIL;
6723 if (*start == '}')
6724 return NOTDONE;
6725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006726
6727 if (evaluate)
6728 {
6729 d = dict_alloc();
6730 if (d == NULL)
6731 return FAIL;
6732 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006733 tvkey.v_type = VAR_UNKNOWN;
6734 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006735
6736 *arg = skipwhite(*arg + 1);
6737 while (**arg != '}' && **arg != NUL)
6738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006739 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006740 goto failret;
6741 if (**arg != ':')
6742 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006743 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006744 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006745 goto failret;
6746 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006747 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006749 key = get_tv_string_buf_chk(&tvkey, buf);
6750 if (key == NULL || *key == NUL)
6751 {
6752 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6753 if (key != NULL)
6754 EMSG(_(e_emptykey));
6755 clear_tv(&tvkey);
6756 goto failret;
6757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006759
6760 *arg = skipwhite(*arg + 1);
6761 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6762 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006763 if (evaluate)
6764 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765 goto failret;
6766 }
6767 if (evaluate)
6768 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006769 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770 if (item != NULL)
6771 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006772 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006773 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774 clear_tv(&tv);
6775 goto failret;
6776 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006777 item = dictitem_alloc(key);
6778 clear_tv(&tvkey);
6779 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006781 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006782 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006783 if (dict_add(d, item) == FAIL)
6784 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785 }
6786 }
6787
6788 if (**arg == '}')
6789 break;
6790 if (**arg != ',')
6791 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006792 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006793 goto failret;
6794 }
6795 *arg = skipwhite(*arg + 1);
6796 }
6797
6798 if (**arg != '}')
6799 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006800 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006801failret:
6802 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006803 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006804 return FAIL;
6805 }
6806
6807 *arg = skipwhite(*arg + 1);
6808 if (evaluate)
6809 {
6810 rettv->v_type = VAR_DICT;
6811 rettv->vval.v_dict = d;
6812 ++d->dv_refcount;
6813 }
6814
6815 return OK;
6816}
6817
Bram Moolenaar8c711452005-01-14 21:53:12 +00006818/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006819 * Return a string with the string representation of a variable.
6820 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006821 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006822 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006823 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006824 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 */
6826 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006827echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006828 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006829 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006830 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006831 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006832{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006833 static int recurse = 0;
6834 char_u *r = NULL;
6835
Bram Moolenaar33570922005-01-25 22:26:29 +00006836 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006838 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006839 *tofree = NULL;
6840 return NULL;
6841 }
6842 ++recurse;
6843
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006844 switch (tv->v_type)
6845 {
6846 case VAR_FUNC:
6847 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006848 r = tv->vval.v_string;
6849 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006850
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006851 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006852 if (tv->vval.v_list == NULL)
6853 {
6854 *tofree = NULL;
6855 r = NULL;
6856 }
6857 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6858 {
6859 *tofree = NULL;
6860 r = (char_u *)"[...]";
6861 }
6862 else
6863 {
6864 tv->vval.v_list->lv_copyID = copyID;
6865 *tofree = list2string(tv, copyID);
6866 r = *tofree;
6867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006868 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006869
Bram Moolenaar8c711452005-01-14 21:53:12 +00006870 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006871 if (tv->vval.v_dict == NULL)
6872 {
6873 *tofree = NULL;
6874 r = NULL;
6875 }
6876 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6877 {
6878 *tofree = NULL;
6879 r = (char_u *)"{...}";
6880 }
6881 else
6882 {
6883 tv->vval.v_dict->dv_copyID = copyID;
6884 *tofree = dict2string(tv, copyID);
6885 r = *tofree;
6886 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006887 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006889 case VAR_STRING:
6890 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006891 *tofree = NULL;
6892 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006893 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006894
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006895 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006896 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006898 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899
6900 --recurse;
6901 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006902}
6903
6904/*
6905 * Return a string with the string representation of a variable.
6906 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6907 * "numbuf" is used for a number.
6908 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006909 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006910 */
6911 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006912tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006913 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006914 char_u **tofree;
6915 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006916 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006917{
6918 switch (tv->v_type)
6919 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006920 case VAR_FUNC:
6921 *tofree = string_quote(tv->vval.v_string, TRUE);
6922 return *tofree;
6923 case VAR_STRING:
6924 *tofree = string_quote(tv->vval.v_string, FALSE);
6925 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006926 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006927 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006928 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006930 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006931 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006932 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006933 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006934}
6935
6936/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006937 * Return string "str" in ' quotes, doubling ' characters.
6938 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006939 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006940 */
6941 static char_u *
6942string_quote(str, function)
6943 char_u *str;
6944 int function;
6945{
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006947 char_u *p, *r, *s;
6948
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 len = (function ? 13 : 3);
6950 if (str != NULL)
6951 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006952 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 for (p = str; *p != NUL; mb_ptr_adv(p))
6954 if (*p == '\'')
6955 ++len;
6956 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006957 s = r = alloc(len);
6958 if (r != NULL)
6959 {
6960 if (function)
6961 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006962 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006963 r += 10;
6964 }
6965 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006966 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 if (str != NULL)
6968 for (p = str; *p != NUL; )
6969 {
6970 if (*p == '\'')
6971 *r++ = '\'';
6972 MB_COPY_CHAR(p, r);
6973 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006975 if (function)
6976 *r++ = ')';
6977 *r++ = NUL;
6978 }
6979 return s;
6980}
6981
6982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 * Get the value of an environment variable.
6984 * "arg" is pointing to the '$'. It is advanced to after the name.
6985 * If the environment variable was not set, silently assume it is empty.
6986 * Always return OK.
6987 */
6988 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006989get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 int evaluate;
6993{
6994 char_u *string = NULL;
6995 int len;
6996 int cc;
6997 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006998 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000 ++*arg;
7001 name = *arg;
7002 len = get_env_len(arg);
7003 if (evaluate)
7004 {
7005 if (len != 0)
7006 {
7007 cc = name[len];
7008 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007009 /* first try vim_getenv(), fast for normal environment vars */
7010 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007012 {
7013 if (!mustfree)
7014 string = vim_strsave(string);
7015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 else
7017 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007018 if (mustfree)
7019 vim_free(string);
7020
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 /* next try expanding things like $VIM and ${HOME} */
7022 string = expand_env_save(name - 1);
7023 if (string != NULL && *string == '$')
7024 {
7025 vim_free(string);
7026 string = NULL;
7027 }
7028 }
7029 name[len] = cc;
7030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007031 rettv->v_type = VAR_STRING;
7032 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 }
7034
7035 return OK;
7036}
7037
7038/*
7039 * Array with names and number of arguments of all internal functions
7040 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7041 */
7042static struct fst
7043{
7044 char *f_name; /* function name */
7045 char f_min_argc; /* minimal number of arguments */
7046 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007047 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007048 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049} functions[] =
7050{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007051 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {"append", 2, 2, f_append},
7053 {"argc", 0, 0, f_argc},
7054 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007055 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007057 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"bufexists", 1, 1, f_bufexists},
7059 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7060 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7061 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7062 {"buflisted", 1, 1, f_buflisted},
7063 {"bufloaded", 1, 1, f_bufloaded},
7064 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007065 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {"bufwinnr", 1, 1, f_bufwinnr},
7067 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007068 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007069 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007070 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {"char2nr", 1, 1, f_char2nr},
7072 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007073 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007075#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007076 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007077 {"complete_add", 1, 1, f_complete_add},
7078 {"complete_check", 0, 0, f_complete_check},
7079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007081 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007082 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007084 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007085 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"delete", 1, 1, f_delete},
7087 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007088 {"diff_filler", 1, 1, f_diff_filler},
7089 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007090 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007092 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {"eventhandler", 0, 0, f_eventhandler},
7094 {"executable", 1, 1, f_executable},
7095 {"exists", 1, 1, f_exists},
7096 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007097 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007098 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7100 {"filereadable", 1, 1, f_filereadable},
7101 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007102 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007103 {"finddir", 1, 3, f_finddir},
7104 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {"fnamemodify", 2, 2, f_fnamemodify},
7106 {"foldclosed", 1, 1, f_foldclosed},
7107 {"foldclosedend", 1, 1, f_foldclosedend},
7108 {"foldlevel", 1, 1, f_foldlevel},
7109 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007110 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007112 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007114 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007115 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"getbufvar", 2, 2, f_getbufvar},
7117 {"getchar", 0, 1, f_getchar},
7118 {"getcharmod", 0, 0, f_getcharmod},
7119 {"getcmdline", 0, 0, f_getcmdline},
7120 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007121 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007123 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007124 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {"getfsize", 1, 1, f_getfsize},
7126 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007127 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007128 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007129 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007130 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007131 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007132 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007133 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007135 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 {"getwinposx", 0, 0, f_getwinposx},
7137 {"getwinposy", 0, 0, f_getwinposy},
7138 {"getwinvar", 2, 2, f_getwinvar},
7139 {"glob", 1, 1, f_glob},
7140 {"globpath", 2, 2, f_globpath},
7141 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007143 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007144 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7146 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7147 {"histadd", 2, 2, f_histadd},
7148 {"histdel", 1, 2, f_histdel},
7149 {"histget", 1, 2, f_histget},
7150 {"histnr", 1, 1, f_histnr},
7151 {"hlID", 1, 1, f_hlID},
7152 {"hlexists", 1, 1, f_hlexists},
7153 {"hostname", 0, 0, f_hostname},
7154 {"iconv", 3, 3, f_iconv},
7155 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007156 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007157 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007159 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {"inputrestore", 0, 0, f_inputrestore},
7161 {"inputsave", 0, 0, f_inputsave},
7162 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007163 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007165 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007166 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007167 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007170 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {"libcall", 3, 3, f_libcall},
7172 {"libcallnr", 3, 3, f_libcallnr},
7173 {"line", 1, 1, f_line},
7174 {"line2byte", 1, 1, f_line2byte},
7175 {"lispindent", 1, 1, f_lispindent},
7176 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007177 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007178 {"maparg", 1, 3, f_maparg},
7179 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007180 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007181 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007182 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007183 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007184 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007185 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007186 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007187 {"max", 1, 1, f_max},
7188 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007189#ifdef vim_mkdir
7190 {"mkdir", 1, 3, f_mkdir},
7191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {"mode", 0, 0, f_mode},
7193 {"nextnonblank", 1, 1, f_nextnonblank},
7194 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007195 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007197 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007198 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007200 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007201 {"reltime", 0, 2, f_reltime},
7202 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {"remote_expr", 2, 3, f_remote_expr},
7204 {"remote_foreground", 1, 1, f_remote_foreground},
7205 {"remote_peek", 1, 2, f_remote_peek},
7206 {"remote_read", 1, 1, f_remote_read},
7207 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007208 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007210 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007212 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007213 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007214 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007215 {"searchpair", 3, 6, f_searchpair},
7216 {"searchpairpos", 3, 6, f_searchpairpos},
7217 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {"server2client", 2, 2, f_server2client},
7219 {"serverlist", 0, 0, f_serverlist},
7220 {"setbufvar", 3, 3, f_setbufvar},
7221 {"setcmdpos", 1, 1, f_setcmdpos},
7222 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007223 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007224 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007225 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007226 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007228 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007230 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007232 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007233 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007234 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007235 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007236 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007237 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238#ifdef HAVE_STRFTIME
7239 {"strftime", 1, 2, f_strftime},
7240#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007241 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007242 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {"strlen", 1, 1, f_strlen},
7244 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007245 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {"strtrans", 1, 1, f_strtrans},
7247 {"submatch", 1, 1, f_submatch},
7248 {"substitute", 4, 4, f_substitute},
7249 {"synID", 3, 3, f_synID},
7250 {"synIDattr", 2, 3, f_synIDattr},
7251 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007252 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007253 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007254 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007255 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007256 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007257 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007259 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 {"tolower", 1, 1, f_tolower},
7261 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007262 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {"virtcol", 1, 1, f_virtcol},
7266 {"visualmode", 0, 1, f_visualmode},
7267 {"winbufnr", 1, 1, f_winbufnr},
7268 {"wincol", 0, 0, f_wincol},
7269 {"winheight", 1, 1, f_winheight},
7270 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007271 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007273 {"winrestview", 1, 1, f_winrestview},
7274 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007276 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277};
7278
7279#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7280
7281/*
7282 * Function given to ExpandGeneric() to obtain the list of internal
7283 * or user defined function names.
7284 */
7285 char_u *
7286get_function_name(xp, idx)
7287 expand_T *xp;
7288 int idx;
7289{
7290 static int intidx = -1;
7291 char_u *name;
7292
7293 if (idx == 0)
7294 intidx = -1;
7295 if (intidx < 0)
7296 {
7297 name = get_user_func_name(xp, idx);
7298 if (name != NULL)
7299 return name;
7300 }
7301 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7302 {
7303 STRCPY(IObuff, functions[intidx].f_name);
7304 STRCAT(IObuff, "(");
7305 if (functions[intidx].f_max_argc == 0)
7306 STRCAT(IObuff, ")");
7307 return IObuff;
7308 }
7309
7310 return NULL;
7311}
7312
7313/*
7314 * Function given to ExpandGeneric() to obtain the list of internal or
7315 * user defined variable or function names.
7316 */
7317/*ARGSUSED*/
7318 char_u *
7319get_expr_name(xp, idx)
7320 expand_T *xp;
7321 int idx;
7322{
7323 static int intidx = -1;
7324 char_u *name;
7325
7326 if (idx == 0)
7327 intidx = -1;
7328 if (intidx < 0)
7329 {
7330 name = get_function_name(xp, idx);
7331 if (name != NULL)
7332 return name;
7333 }
7334 return get_user_var_name(xp, ++intidx);
7335}
7336
7337#endif /* FEAT_CMDL_COMPL */
7338
7339/*
7340 * Find internal function in table above.
7341 * Return index, or -1 if not found
7342 */
7343 static int
7344find_internal_func(name)
7345 char_u *name; /* name of the function */
7346{
7347 int first = 0;
7348 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7349 int cmp;
7350 int x;
7351
7352 /*
7353 * Find the function name in the table. Binary search.
7354 */
7355 while (first <= last)
7356 {
7357 x = first + ((unsigned)(last - first) >> 1);
7358 cmp = STRCMP(name, functions[x].f_name);
7359 if (cmp < 0)
7360 last = x - 1;
7361 else if (cmp > 0)
7362 first = x + 1;
7363 else
7364 return x;
7365 }
7366 return -1;
7367}
7368
7369/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007370 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7371 * name it contains, otherwise return "name".
7372 */
7373 static char_u *
7374deref_func_name(name, lenp)
7375 char_u *name;
7376 int *lenp;
7377{
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007379 int cc;
7380
7381 cc = name[*lenp];
7382 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007383 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007384 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388 {
7389 *lenp = 0;
7390 return (char_u *)""; /* just in case */
7391 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007392 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007394 }
7395
7396 return name;
7397}
7398
7399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 * Allocate a variable for the result of a function.
7401 * Return OK or FAIL.
7402 */
7403 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7405 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 char_u *name; /* name of the function */
7407 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 char_u **arg; /* argument, pointing to the '(' */
7410 linenr_T firstline; /* first line of range */
7411 linenr_T lastline; /* last line of range */
7412 int *doesrange; /* return: function handled range */
7413 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 char_u *argp;
7417 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007418 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 int argcount = 0; /* number of arguments found */
7420
7421 /*
7422 * Get the arguments.
7423 */
7424 argp = *arg;
7425 while (argcount < MAX_FUNC_ARGS)
7426 {
7427 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7428 if (*argp == ')' || *argp == ',' || *argp == NUL)
7429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7431 {
7432 ret = FAIL;
7433 break;
7434 }
7435 ++argcount;
7436 if (*argp != ',')
7437 break;
7438 }
7439 if (*argp == ')')
7440 ++argp;
7441 else
7442 ret = FAIL;
7443
7444 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007446 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 {
7449 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007450 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007452 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454
7455 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007456 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 *arg = skipwhite(argp);
7459 return ret;
7460}
7461
7462
7463/*
7464 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007465 * Return OK when the function can't be called, FAIL otherwise.
7466 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 */
7468 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 char_u *name; /* name of the function */
7472 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007473 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007475 typval_T *argvars; /* vars for arguments, must have "argcount"
7476 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 linenr_T firstline; /* first line of range */
7478 linenr_T lastline; /* last line of range */
7479 int *doesrange; /* return: function handled range */
7480 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
7483 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484#define ERROR_UNKNOWN 0
7485#define ERROR_TOOMANY 1
7486#define ERROR_TOOFEW 2
7487#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488#define ERROR_DICT 4
7489#define ERROR_NONE 5
7490#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 int error = ERROR_NONE;
7492 int i;
7493 int llen;
7494 ufunc_T *fp;
7495 int cc;
7496#define FLEN_FIXED 40
7497 char_u fname_buf[FLEN_FIXED + 1];
7498 char_u *fname;
7499
7500 /*
7501 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7502 * Change <SNR>123_name() to K_SNR 123_name().
7503 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7504 */
7505 cc = name[len];
7506 name[len] = NUL;
7507 llen = eval_fname_script(name);
7508 if (llen > 0)
7509 {
7510 fname_buf[0] = K_SPECIAL;
7511 fname_buf[1] = KS_EXTRA;
7512 fname_buf[2] = (int)KE_SNR;
7513 i = 3;
7514 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7515 {
7516 if (current_SID <= 0)
7517 error = ERROR_SCRIPT;
7518 else
7519 {
7520 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7521 i = (int)STRLEN(fname_buf);
7522 }
7523 }
7524 if (i + STRLEN(name + llen) < FLEN_FIXED)
7525 {
7526 STRCPY(fname_buf + i, name + llen);
7527 fname = fname_buf;
7528 }
7529 else
7530 {
7531 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7532 if (fname == NULL)
7533 error = ERROR_OTHER;
7534 else
7535 {
7536 mch_memmove(fname, fname_buf, (size_t)i);
7537 STRCPY(fname + i, name + llen);
7538 }
7539 }
7540 }
7541 else
7542 fname = name;
7543
7544 *doesrange = FALSE;
7545
7546
7547 /* execute the function if no errors detected and executing */
7548 if (evaluate && error == ERROR_NONE)
7549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007550 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 error = ERROR_UNKNOWN;
7552
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007553 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 {
7555 /*
7556 * User defined function.
7557 */
7558 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007559
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007561 /* Trigger FuncUndefined event, may load the function. */
7562 if (fp == NULL
7563 && apply_autocmds(EVENT_FUNCUNDEFINED,
7564 fname, fname, TRUE, NULL)
7565 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007567 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 fp = find_func(fname);
7569 }
7570#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007571 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007572 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007573 {
7574 /* loaded a package, search for the function again */
7575 fp = find_func(fname);
7576 }
7577
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 if (fp != NULL)
7579 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007580 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007582 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007584 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007586 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007587 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 else
7589 {
7590 /*
7591 * Call the user function.
7592 * Save and restore search patterns, script variables and
7593 * redo buffer.
7594 */
7595 save_search_patterns();
7596 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007597 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007599 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007600 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7601 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7602 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007603 /* Function was unreferenced while being used, free it
7604 * now. */
7605 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 restoreRedobuff();
7607 restore_search_patterns();
7608 error = ERROR_NONE;
7609 }
7610 }
7611 }
7612 else
7613 {
7614 /*
7615 * Find the function name in the table, call its implementation.
7616 */
7617 i = find_internal_func(fname);
7618 if (i >= 0)
7619 {
7620 if (argcount < functions[i].f_min_argc)
7621 error = ERROR_TOOFEW;
7622 else if (argcount > functions[i].f_max_argc)
7623 error = ERROR_TOOMANY;
7624 else
7625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007626 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 error = ERROR_NONE;
7629 }
7630 }
7631 }
7632 /*
7633 * The function call (or "FuncUndefined" autocommand sequence) might
7634 * have been aborted by an error, an interrupt, or an explicitly thrown
7635 * exception that has not been caught so far. This situation can be
7636 * tested for by calling aborting(). For an error in an internal
7637 * function or for the "E132" error in call_user_func(), however, the
7638 * throw point at which the "force_abort" flag (temporarily reset by
7639 * emsg()) is normally updated has not been reached yet. We need to
7640 * update that flag first to make aborting() reliable.
7641 */
7642 update_force_abort();
7643 }
7644 if (error == ERROR_NONE)
7645 ret = OK;
7646
7647 /*
7648 * Report an error unless the argument evaluation or function call has been
7649 * cancelled due to an aborting error, an interrupt, or an exception.
7650 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 if (!aborting())
7652 {
7653 switch (error)
7654 {
7655 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007656 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 break;
7658 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007659 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 break;
7661 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007662 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 name);
7664 break;
7665 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007666 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 name);
7668 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007670 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 name);
7672 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 }
7674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
7676 name[len] = cc;
7677 if (fname != name && fname != fname_buf)
7678 vim_free(fname);
7679
7680 return ret;
7681}
7682
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007683/*
7684 * Give an error message with a function name. Handle <SNR> things.
7685 */
7686 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007687emsg_funcname(ermsg, name)
7688 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007689 char_u *name;
7690{
7691 char_u *p;
7692
7693 if (*name == K_SPECIAL)
7694 p = concat_str((char_u *)"<SNR>", name + 3);
7695 else
7696 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007697 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007698 if (p != name)
7699 vim_free(p);
7700}
7701
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702/*********************************************
7703 * Implementation of the built-in functions
7704 */
7705
7706/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007707 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 */
7709 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007710f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 typval_T *argvars;
7712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007717 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007719 if ((l = argvars[0].vval.v_list) != NULL
7720 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7721 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007723 }
7724 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007725 EMSG(_(e_listreq));
7726}
7727
7728/*
7729 * "append(lnum, string/list)" function
7730 */
7731 static void
7732f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 typval_T *argvars;
7734 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007735{
7736 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007737 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 list_T *l = NULL;
7739 listitem_T *li = NULL;
7740 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007741 long added = 0;
7742
Bram Moolenaar0d660222005-01-07 21:51:51 +00007743 lnum = get_tv_lnum(argvars);
7744 if (lnum >= 0
7745 && lnum <= curbuf->b_ml.ml_line_count
7746 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007748 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007749 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007750 l = argvars[1].vval.v_list;
7751 if (l == NULL)
7752 return;
7753 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007754 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007755 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007756 for (;;)
7757 {
7758 if (l == NULL)
7759 tv = &argvars[1]; /* append a string */
7760 else if (li == NULL)
7761 break; /* end of list */
7762 else
7763 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007764 line = get_tv_string_chk(tv);
7765 if (line == NULL) /* type error */
7766 {
7767 rettv->vval.v_number = 1; /* Failed */
7768 break;
7769 }
7770 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007771 ++added;
7772 if (l == NULL)
7773 break;
7774 li = li->li_next;
7775 }
7776
7777 appended_lines_mark(lnum, added);
7778 if (curwin->w_cursor.lnum > lnum)
7779 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007781 else
7782 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783}
7784
7785/*
7786 * "argc()" function
7787 */
7788/* ARGSUSED */
7789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007790f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007791 typval_T *argvars;
7792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795}
7796
7797/*
7798 * "argidx()" function
7799 */
7800/* ARGSUSED */
7801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *argvars;
7804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807}
7808
7809/*
7810 * "argv(nr)" function
7811 */
7812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007813f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007814 typval_T *argvars;
7815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
7817 int idx;
7818
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007819 if (argvars[0].v_type != VAR_UNKNOWN)
7820 {
7821 idx = get_tv_number_chk(&argvars[0], NULL);
7822 if (idx >= 0 && idx < ARGCOUNT)
7823 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7824 else
7825 rettv->vval.v_string = NULL;
7826 rettv->v_type = VAR_STRING;
7827 }
7828 else if (rettv_list_alloc(rettv) == OK)
7829 for (idx = 0; idx < ARGCOUNT; ++idx)
7830 list_append_string(rettv->vval.v_list,
7831 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832}
7833
7834/*
7835 * "browse(save, title, initdir, default)" function
7836 */
7837/* ARGSUSED */
7838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 typval_T *argvars;
7841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843#ifdef FEAT_BROWSE
7844 int save;
7845 char_u *title;
7846 char_u *initdir;
7847 char_u *defname;
7848 char_u buf[NUMBUFLEN];
7849 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852 save = get_tv_number_chk(&argvars[0], &error);
7853 title = get_tv_string_chk(&argvars[1]);
7854 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7855 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007857 if (error || title == NULL || initdir == NULL || defname == NULL)
7858 rettv->vval.v_string = NULL;
7859 else
7860 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007861 do_browse(save ? BROWSE_SAVE : 0,
7862 title, defname, NULL, initdir, NULL, curbuf);
7863#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007865#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007867}
7868
7869/*
7870 * "browsedir(title, initdir)" function
7871 */
7872/* ARGSUSED */
7873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007874f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 typval_T *argvars;
7876 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007877{
7878#ifdef FEAT_BROWSE
7879 char_u *title;
7880 char_u *initdir;
7881 char_u buf[NUMBUFLEN];
7882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007883 title = get_tv_string_chk(&argvars[0]);
7884 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007886 if (title == NULL || initdir == NULL)
7887 rettv->vval.v_string = NULL;
7888 else
7889 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007890 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895}
7896
Bram Moolenaar33570922005-01-25 22:26:29 +00007897static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007898
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899/*
7900 * Find a buffer by number or exact name.
7901 */
7902 static buf_T *
7903find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908 if (avar->v_type == VAR_NUMBER)
7909 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007910 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007912 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007913 if (buf == NULL)
7914 {
7915 /* No full path name match, try a match with a URL or a "nofile"
7916 * buffer, these don't use the full path. */
7917 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7918 if (buf->b_fname != NULL
7919 && (path_with_url(buf->b_fname)
7920#ifdef FEAT_QUICKFIX
7921 || bt_nofile(buf)
7922#endif
7923 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007924 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007925 break;
7926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928 return buf;
7929}
7930
7931/*
7932 * "bufexists(expr)" function
7933 */
7934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 typval_T *argvars;
7937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940}
7941
7942/*
7943 * "buflisted(expr)" function
7944 */
7945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007946f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 typval_T *argvars;
7948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
7950 buf_T *buf;
7951
7952 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954}
7955
7956/*
7957 * "bufloaded(expr)" function
7958 */
7959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 typval_T *argvars;
7962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
7964 buf_T *buf;
7965
7966 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007967 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968}
7969
Bram Moolenaar33570922005-01-25 22:26:29 +00007970static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007971
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972/*
7973 * Get buffer by number or pattern.
7974 */
7975 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007979 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 int save_magic;
7981 char_u *save_cpo;
7982 buf_T *buf;
7983
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 if (tv->v_type == VAR_NUMBER)
7985 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007986 if (tv->v_type != VAR_STRING)
7987 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 if (name == NULL || *name == NUL)
7989 return curbuf;
7990 if (name[0] == '$' && name[1] == NUL)
7991 return lastbuf;
7992
7993 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7994 save_magic = p_magic;
7995 p_magic = TRUE;
7996 save_cpo = p_cpo;
7997 p_cpo = (char_u *)"";
7998
7999 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8000 TRUE, FALSE));
8001
8002 p_magic = save_magic;
8003 p_cpo = save_cpo;
8004
8005 /* If not found, try expanding the name, like done for bufexists(). */
8006 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008007 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008
8009 return buf;
8010}
8011
8012/*
8013 * "bufname(expr)" function
8014 */
8015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 typval_T *argvars;
8018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
8020 buf_T *buf;
8021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008022 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 buf = get_buf_tv(&argvars[0]);
8025 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 --emsg_off;
8031}
8032
8033/*
8034 * "bufnr(expr)" function
8035 */
8036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008037f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 typval_T *argvars;
8039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008042 int error = FALSE;
8043 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008045 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008048 --emsg_off;
8049
8050 /* If the buffer isn't found and the second argument is not zero create a
8051 * new buffer. */
8052 if (buf == NULL
8053 && argvars[1].v_type != VAR_UNKNOWN
8054 && get_tv_number_chk(&argvars[1], &error) != 0
8055 && !error
8056 && (name = get_tv_string_chk(&argvars[0])) != NULL
8057 && !error)
8058 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8059
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008061 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064}
8065
8066/*
8067 * "bufwinnr(nr)" function
8068 */
8069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 typval_T *argvars;
8072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074#ifdef FEAT_WINDOWS
8075 win_T *wp;
8076 int winnr = 0;
8077#endif
8078 buf_T *buf;
8079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008080 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008082 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083#ifdef FEAT_WINDOWS
8084 for (wp = firstwin; wp; wp = wp->w_next)
8085 {
8086 ++winnr;
8087 if (wp->w_buffer == buf)
8088 break;
8089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#endif
8094 --emsg_off;
8095}
8096
8097/*
8098 * "byte2line(byte)" function
8099 */
8100/*ARGSUSED*/
8101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008102f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008103 typval_T *argvars;
8104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108#else
8109 long boff = 0;
8110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008111 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008115 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 (linenr_T)0, &boff);
8117#endif
8118}
8119
8120/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008121 * "byteidx()" function
8122 */
8123/*ARGSUSED*/
8124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008125f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008126 typval_T *argvars;
8127 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008128{
8129#ifdef FEAT_MBYTE
8130 char_u *t;
8131#endif
8132 char_u *str;
8133 long idx;
8134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008135 str = get_tv_string_chk(&argvars[0]);
8136 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008138 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008139 return;
8140
8141#ifdef FEAT_MBYTE
8142 t = str;
8143 for ( ; idx > 0; idx--)
8144 {
8145 if (*t == NUL) /* EOL reached */
8146 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008147 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008148 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008149 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008150#else
8151 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008153#endif
8154}
8155
8156/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008157 * "call(func, arglist)" function
8158 */
8159 static void
8160f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 typval_T *argvars;
8162 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008163{
8164 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008165 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008166 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008167 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008168 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170
8171 rettv->vval.v_number = 0;
8172 if (argvars[1].v_type != VAR_LIST)
8173 {
8174 EMSG(_(e_listreq));
8175 return;
8176 }
8177 if (argvars[1].vval.v_list == NULL)
8178 return;
8179
8180 if (argvars[0].v_type == VAR_FUNC)
8181 func = argvars[0].vval.v_string;
8182 else
8183 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008184 if (*func == NUL)
8185 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008186
Bram Moolenaare9a41262005-01-15 22:18:47 +00008187 if (argvars[2].v_type != VAR_UNKNOWN)
8188 {
8189 if (argvars[2].v_type != VAR_DICT)
8190 {
8191 EMSG(_(e_dictreq));
8192 return;
8193 }
8194 selfdict = argvars[2].vval.v_dict;
8195 }
8196
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008197 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8198 item = item->li_next)
8199 {
8200 if (argc == MAX_FUNC_ARGS)
8201 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008202 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008203 break;
8204 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008205 /* Make a copy of each argument. This is needed to be able to set
8206 * v_lock to VAR_FIXED in the copy without changing the original list.
8207 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008208 copy_tv(&item->li_tv, &argv[argc++]);
8209 }
8210
8211 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008212 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8214 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008215
8216 /* Free the arguments. */
8217 while (argc > 0)
8218 clear_tv(&argv[--argc]);
8219}
8220
8221/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008222 * "changenr()" function
8223 */
8224/*ARGSUSED*/
8225 static void
8226f_changenr(argvars, rettv)
8227 typval_T *argvars;
8228 typval_T *rettv;
8229{
8230 rettv->vval.v_number = curbuf->b_u_seq_cur;
8231}
8232
8233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 * "char2nr(string)" function
8235 */
8236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008238 typval_T *argvars;
8239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240{
8241#ifdef FEAT_MBYTE
8242 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008243 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 else
8245#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247}
8248
8249/*
8250 * "cindent(lnum)" function
8251 */
8252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 typval_T *argvars;
8255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
8257#ifdef FEAT_CINDENT
8258 pos_T pos;
8259 linenr_T lnum;
8260
8261 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008262 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8264 {
8265 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008266 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 curwin->w_cursor = pos;
8268 }
8269 else
8270#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272}
8273
8274/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008275 * "clearmatches()" function
8276 */
8277/*ARGSUSED*/
8278 static void
8279f_clearmatches(argvars, rettv)
8280 typval_T *argvars;
8281 typval_T *rettv;
8282{
8283#ifdef FEAT_SEARCH_EXTRA
8284 clear_matches(curwin);
8285#endif
8286}
8287
8288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 * "col(string)" function
8290 */
8291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008293 typval_T *argvars;
8294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 colnr_T col = 0;
8297 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008298 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008300 fp = var2fpos(&argvars[0], FALSE, &fnum);
8301 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {
8303 if (fp->col == MAXCOL)
8304 {
8305 /* '> can be MAXCOL, get the length of the line then */
8306 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008307 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 else
8309 col = MAXCOL;
8310 }
8311 else
8312 {
8313 col = fp->col + 1;
8314#ifdef FEAT_VIRTUALEDIT
8315 /* col(".") when the cursor is on the NUL at the end of the line
8316 * because of "coladd" can be seen as an extra column. */
8317 if (virtual_active() && fp == &curwin->w_cursor)
8318 {
8319 char_u *p = ml_get_cursor();
8320
8321 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8322 curwin->w_virtcol - curwin->w_cursor.coladd))
8323 {
8324# ifdef FEAT_MBYTE
8325 int l;
8326
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008327 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 col += l;
8329# else
8330 if (*p != NUL && p[1] == NUL)
8331 ++col;
8332# endif
8333 }
8334 }
8335#endif
8336 }
8337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008338 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339}
8340
Bram Moolenaar572cb562005-08-05 21:35:02 +00008341#if defined(FEAT_INS_EXPAND)
8342/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008343 * "complete()" function
8344 */
8345/*ARGSUSED*/
8346 static void
8347f_complete(argvars, rettv)
8348 typval_T *argvars;
8349 typval_T *rettv;
8350{
8351 int startcol;
8352
8353 if ((State & INSERT) == 0)
8354 {
8355 EMSG(_("E785: complete() can only be used in Insert mode"));
8356 return;
8357 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008358
8359 /* Check for undo allowed here, because if something was already inserted
8360 * the line was already saved for undo and this check isn't done. */
8361 if (!undo_allowed())
8362 return;
8363
Bram Moolenaarade00832006-03-10 21:46:58 +00008364 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8365 {
8366 EMSG(_(e_invarg));
8367 return;
8368 }
8369
8370 startcol = get_tv_number_chk(&argvars[0], NULL);
8371 if (startcol <= 0)
8372 return;
8373
8374 set_completion(startcol - 1, argvars[1].vval.v_list);
8375}
8376
8377/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008378 * "complete_add()" function
8379 */
8380/*ARGSUSED*/
8381 static void
8382f_complete_add(argvars, rettv)
8383 typval_T *argvars;
8384 typval_T *rettv;
8385{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008386 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008387}
8388
8389/*
8390 * "complete_check()" function
8391 */
8392/*ARGSUSED*/
8393 static void
8394f_complete_check(argvars, rettv)
8395 typval_T *argvars;
8396 typval_T *rettv;
8397{
8398 int saved = RedrawingDisabled;
8399
8400 RedrawingDisabled = 0;
8401 ins_compl_check_keys(0);
8402 rettv->vval.v_number = compl_interrupted;
8403 RedrawingDisabled = saved;
8404}
8405#endif
8406
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407/*
8408 * "confirm(message, buttons[, default [, type]])" function
8409 */
8410/*ARGSUSED*/
8411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 typval_T *argvars;
8414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415{
8416#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8417 char_u *message;
8418 char_u *buttons = NULL;
8419 char_u buf[NUMBUFLEN];
8420 char_u buf2[NUMBUFLEN];
8421 int def = 1;
8422 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008423 char_u *typestr;
8424 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008426 message = get_tv_string_chk(&argvars[0]);
8427 if (message == NULL)
8428 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008429 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8432 if (buttons == NULL)
8433 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008436 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008437 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008439 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8440 if (typestr == NULL)
8441 error = TRUE;
8442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008444 switch (TOUPPER_ASC(*typestr))
8445 {
8446 case 'E': type = VIM_ERROR; break;
8447 case 'Q': type = VIM_QUESTION; break;
8448 case 'I': type = VIM_INFO; break;
8449 case 'W': type = VIM_WARNING; break;
8450 case 'G': type = VIM_GENERIC; break;
8451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 }
8453 }
8454 }
8455 }
8456
8457 if (buttons == NULL || *buttons == NUL)
8458 buttons = (char_u *)_("&Ok");
8459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008460 if (error)
8461 rettv->vval.v_number = 0;
8462 else
8463 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 def, NULL);
8465#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008466 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#endif
8468}
8469
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008470/*
8471 * "copy()" function
8472 */
8473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008474f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 typval_T *argvars;
8476 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008478 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008479}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480
8481/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008482 * "count()" function
8483 */
8484 static void
8485f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008486 typval_T *argvars;
8487 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008488{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008489 long n = 0;
8490 int ic = FALSE;
8491
Bram Moolenaare9a41262005-01-15 22:18:47 +00008492 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008493 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008494 listitem_T *li;
8495 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008496 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008497
Bram Moolenaare9a41262005-01-15 22:18:47 +00008498 if ((l = argvars[0].vval.v_list) != NULL)
8499 {
8500 li = l->lv_first;
8501 if (argvars[2].v_type != VAR_UNKNOWN)
8502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008503 int error = FALSE;
8504
8505 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 if (argvars[3].v_type != VAR_UNKNOWN)
8507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008508 idx = get_tv_number_chk(&argvars[3], &error);
8509 if (!error)
8510 {
8511 li = list_find(l, idx);
8512 if (li == NULL)
8513 EMSGN(_(e_listidx), idx);
8514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008515 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008516 if (error)
8517 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008518 }
8519
8520 for ( ; li != NULL; li = li->li_next)
8521 if (tv_equal(&li->li_tv, &argvars[1], ic))
8522 ++n;
8523 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008524 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008525 else if (argvars[0].v_type == VAR_DICT)
8526 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008527 int todo;
8528 dict_T *d;
8529 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008530
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008531 if ((d = argvars[0].vval.v_dict) != NULL)
8532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008533 int error = FALSE;
8534
Bram Moolenaare9a41262005-01-15 22:18:47 +00008535 if (argvars[2].v_type != VAR_UNKNOWN)
8536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008537 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008538 if (argvars[3].v_type != VAR_UNKNOWN)
8539 EMSG(_(e_invarg));
8540 }
8541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008542 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008544 {
8545 if (!HASHITEM_EMPTY(hi))
8546 {
8547 --todo;
8548 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8549 ++n;
8550 }
8551 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008552 }
8553 }
8554 else
8555 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008556 rettv->vval.v_number = n;
8557}
8558
8559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8561 *
8562 * Checks the existence of a cscope connection.
8563 */
8564/*ARGSUSED*/
8565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008567 typval_T *argvars;
8568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569{
8570#ifdef FEAT_CSCOPE
8571 int num = 0;
8572 char_u *dbpath = NULL;
8573 char_u *prepend = NULL;
8574 char_u buf[NUMBUFLEN];
8575
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008576 if (argvars[0].v_type != VAR_UNKNOWN
8577 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 num = (int)get_tv_number(&argvars[0]);
8580 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 }
8584
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#endif
8589}
8590
8591/*
8592 * "cursor(lnum, col)" function
8593 *
8594 * Moves the cursor to the specified line and column
8595 */
8596/*ARGSUSED*/
8597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008599 typval_T *argvars;
8600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
8602 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008603#ifdef FEAT_VIRTUALEDIT
8604 long coladd = 0;
8605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaara5525202006-03-02 22:52:09 +00008607 if (argvars[1].v_type == VAR_UNKNOWN)
8608 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008609 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008610
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008611 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008612 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008613 line = pos.lnum;
8614 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008615#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008616 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008617#endif
8618 }
8619 else
8620 {
8621 line = get_tv_lnum(argvars);
8622 col = get_tv_number_chk(&argvars[1], NULL);
8623#ifdef FEAT_VIRTUALEDIT
8624 if (argvars[2].v_type != VAR_UNKNOWN)
8625 coladd = get_tv_number_chk(&argvars[2], NULL);
8626#endif
8627 }
8628 if (line < 0 || col < 0
8629#ifdef FEAT_VIRTUALEDIT
8630 || coladd < 0
8631#endif
8632 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008633 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 if (line > 0)
8635 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 if (col > 0)
8637 curwin->w_cursor.col = col - 1;
8638#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008639 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#endif
8641
8642 /* Make sure the cursor is in a valid position. */
8643 check_cursor();
8644#ifdef FEAT_MBYTE
8645 /* Correct cursor for multi-byte character. */
8646 if (has_mbyte)
8647 mb_adjust_cursor();
8648#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008649
8650 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651}
8652
8653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 */
8656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008661 int noref = 0;
8662
8663 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008664 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008665 if (noref < 0 || noref > 1)
8666 EMSG(_(e_invarg));
8667 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008668 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669}
8670
8671/*
8672 * "delete()" function
8673 */
8674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008676 typval_T *argvars;
8677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
8679 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683}
8684
8685/*
8686 * "did_filetype()" function
8687 */
8688/*ARGSUSED*/
8689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008690f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008691 typval_T *argvars;
8692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693{
8694#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698#endif
8699}
8700
8701/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008702 * "diff_filler()" function
8703 */
8704/*ARGSUSED*/
8705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008707 typval_T *argvars;
8708 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008709{
8710#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008712#endif
8713}
8714
8715/*
8716 * "diff_hlID()" function
8717 */
8718/*ARGSUSED*/
8719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008721 typval_T *argvars;
8722 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008723{
8724#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008726 static linenr_T prev_lnum = 0;
8727 static int changedtick = 0;
8728 static int fnum = 0;
8729 static int change_start = 0;
8730 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008731 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008732 int filler_lines;
8733 int col;
8734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008735 if (lnum < 0) /* ignore type error in {lnum} arg */
8736 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008737 if (lnum != prev_lnum
8738 || changedtick != curbuf->b_changedtick
8739 || fnum != curbuf->b_fnum)
8740 {
8741 /* New line, buffer, change: need to get the values. */
8742 filler_lines = diff_check(curwin, lnum);
8743 if (filler_lines < 0)
8744 {
8745 if (filler_lines == -1)
8746 {
8747 change_start = MAXCOL;
8748 change_end = -1;
8749 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8750 hlID = HLF_ADD; /* added line */
8751 else
8752 hlID = HLF_CHD; /* changed line */
8753 }
8754 else
8755 hlID = HLF_ADD; /* added line */
8756 }
8757 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008758 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008759 prev_lnum = lnum;
8760 changedtick = curbuf->b_changedtick;
8761 fnum = curbuf->b_fnum;
8762 }
8763
8764 if (hlID == HLF_CHD || hlID == HLF_TXD)
8765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008766 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008767 if (col >= change_start && col <= change_end)
8768 hlID = HLF_TXD; /* changed text */
8769 else
8770 hlID = HLF_CHD; /* changed line */
8771 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008772 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008773#endif
8774}
8775
8776/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008777 * "empty({expr})" function
8778 */
8779 static void
8780f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 typval_T *argvars;
8782 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008783{
8784 int n;
8785
8786 switch (argvars[0].v_type)
8787 {
8788 case VAR_STRING:
8789 case VAR_FUNC:
8790 n = argvars[0].vval.v_string == NULL
8791 || *argvars[0].vval.v_string == NUL;
8792 break;
8793 case VAR_NUMBER:
8794 n = argvars[0].vval.v_number == 0;
8795 break;
8796 case VAR_LIST:
8797 n = argvars[0].vval.v_list == NULL
8798 || argvars[0].vval.v_list->lv_first == NULL;
8799 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008800 case VAR_DICT:
8801 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008803 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008804 default:
8805 EMSG2(_(e_intern2), "f_empty()");
8806 n = 0;
8807 }
8808
8809 rettv->vval.v_number = n;
8810}
8811
8812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 * "escape({string}, {chars})" function
8814 */
8815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008817 typval_T *argvars;
8818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819{
8820 char_u buf[NUMBUFLEN];
8821
Bram Moolenaar758711c2005-02-02 23:11:38 +00008822 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8823 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825}
8826
8827/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008828 * "eval()" function
8829 */
8830/*ARGSUSED*/
8831 static void
8832f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008833 typval_T *argvars;
8834 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008835{
8836 char_u *s;
8837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008838 s = get_tv_string_chk(&argvars[0]);
8839 if (s != NULL)
8840 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008842 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8843 {
8844 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008845 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008846 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008847 else if (*s != NUL)
8848 EMSG(_(e_trailing));
8849}
8850
8851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 * "eventhandler()" function
8853 */
8854/*ARGSUSED*/
8855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 typval_T *argvars;
8858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008860 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861}
8862
8863/*
8864 * "executable()" function
8865 */
8866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008868 typval_T *argvars;
8869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872}
8873
8874/*
8875 * "exists()" function
8876 */
8877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008879 typval_T *argvars;
8880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882 char_u *p;
8883 char_u *name;
8884 int n = FALSE;
8885 int len = 0;
8886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008887 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 if (*p == '$') /* environment variable */
8889 {
8890 /* first try "normal" environment variables (fast) */
8891 if (mch_getenv(p + 1) != NULL)
8892 n = TRUE;
8893 else
8894 {
8895 /* try expanding things like $VIM and ${HOME} */
8896 p = expand_env_save(p);
8897 if (p != NULL && *p != '$')
8898 n = TRUE;
8899 vim_free(p);
8900 }
8901 }
8902 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008905 if (*skipwhite(p) != NUL)
8906 n = FALSE; /* trailing garbage */
8907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 else if (*p == '*') /* internal or user defined function */
8909 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008910 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 }
8912 else if (*p == ':')
8913 {
8914 n = cmd_exists(p + 1);
8915 }
8916 else if (*p == '#')
8917 {
8918#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008919 if (p[1] == '#')
8920 n = autocmd_supported(p + 2);
8921 else
8922 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923#endif
8924 }
8925 else /* internal variable */
8926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008927 char_u *tofree;
8928 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008930 /* get_name_len() takes care of expanding curly braces */
8931 name = p;
8932 len = get_name_len(&p, &tofree, TRUE, FALSE);
8933 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008935 if (tofree != NULL)
8936 name = tofree;
8937 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8938 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008940 /* handle d.key, l[idx], f(expr) */
8941 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8942 if (n)
8943 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 }
8945 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008946 if (*p != NUL)
8947 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008949 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953}
8954
8955/*
8956 * "expand()" function
8957 */
8958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963 char_u *s;
8964 int len;
8965 char_u *errormsg;
8966 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8967 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 rettv->v_type = VAR_STRING;
8971 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 if (*s == '%' || *s == '#' || *s == '<')
8973 {
8974 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008975 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 --emsg_off;
8977 }
8978 else
8979 {
8980 /* When the optional second argument is non-zero, don't remove matches
8981 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008982 if (argvars[1].v_type != VAR_UNKNOWN
8983 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 if (!error)
8986 {
8987 ExpandInit(&xpc);
8988 xpc.xp_context = EXPAND_FILES;
8989 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 }
8991 else
8992 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 }
8994}
8995
8996/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008997 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008998 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008999 */
9000 static void
9001f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 typval_T *argvars;
9003 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009004{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009005 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009006 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009008 list_T *l1, *l2;
9009 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009010 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009012
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 l1 = argvars[0].vval.v_list;
9014 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009015 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9016 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009017 {
9018 if (argvars[2].v_type != VAR_UNKNOWN)
9019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 before = get_tv_number_chk(&argvars[2], &error);
9021 if (error)
9022 return; /* type error; errmsg already given */
9023
Bram Moolenaar758711c2005-02-02 23:11:38 +00009024 if (before == l1->lv_len)
9025 item = NULL;
9026 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009027 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009028 item = list_find(l1, before);
9029 if (item == NULL)
9030 {
9031 EMSGN(_(e_listidx), before);
9032 return;
9033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009034 }
9035 }
9036 else
9037 item = NULL;
9038 list_extend(l1, l2, item);
9039
Bram Moolenaare9a41262005-01-15 22:18:47 +00009040 copy_tv(&argvars[0], rettv);
9041 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009042 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009043 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9044 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009045 dict_T *d1, *d2;
9046 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009047 char_u *action;
9048 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009050 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009051
9052 d1 = argvars[0].vval.v_dict;
9053 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009054 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9055 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056 {
9057 /* Check the third argument. */
9058 if (argvars[2].v_type != VAR_UNKNOWN)
9059 {
9060 static char *(av[]) = {"keep", "force", "error"};
9061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009062 action = get_tv_string_chk(&argvars[2]);
9063 if (action == NULL)
9064 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009065 for (i = 0; i < 3; ++i)
9066 if (STRCMP(action, av[i]) == 0)
9067 break;
9068 if (i == 3)
9069 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009070 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009071 return;
9072 }
9073 }
9074 else
9075 action = (char_u *)"force";
9076
9077 /* Go over all entries in the second dict and add them to the
9078 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009079 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009082 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009083 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009084 --todo;
9085 di1 = dict_find(d1, hi2->hi_key, -1);
9086 if (di1 == NULL)
9087 {
9088 di1 = dictitem_copy(HI2DI(hi2));
9089 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9090 dictitem_free(di1);
9091 }
9092 else if (*action == 'e')
9093 {
9094 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9095 break;
9096 }
9097 else if (*action == 'f')
9098 {
9099 clear_tv(&di1->di_tv);
9100 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9101 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009102 }
9103 }
9104
Bram Moolenaare9a41262005-01-15 22:18:47 +00009105 copy_tv(&argvars[0], rettv);
9106 }
9107 }
9108 else
9109 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009110}
9111
9112/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009113 * "feedkeys()" function
9114 */
9115/*ARGSUSED*/
9116 static void
9117f_feedkeys(argvars, rettv)
9118 typval_T *argvars;
9119 typval_T *rettv;
9120{
9121 int remap = TRUE;
9122 char_u *keys, *flags;
9123 char_u nbuf[NUMBUFLEN];
9124 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009125 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009126
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009127 /* This is not allowed in the sandbox. If the commands would still be
9128 * executed in the sandbox it would be OK, but it probably happens later,
9129 * when "sandbox" is no longer set. */
9130 if (check_secure())
9131 return;
9132
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009133 rettv->vval.v_number = 0;
9134 keys = get_tv_string(&argvars[0]);
9135 if (*keys != NUL)
9136 {
9137 if (argvars[1].v_type != VAR_UNKNOWN)
9138 {
9139 flags = get_tv_string_buf(&argvars[1], nbuf);
9140 for ( ; *flags != NUL; ++flags)
9141 {
9142 switch (*flags)
9143 {
9144 case 'n': remap = FALSE; break;
9145 case 'm': remap = TRUE; break;
9146 case 't': typed = TRUE; break;
9147 }
9148 }
9149 }
9150
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009151 /* Need to escape K_SPECIAL and CSI before putting the string in the
9152 * typeahead buffer. */
9153 keys_esc = vim_strsave_escape_csi(keys);
9154 if (keys_esc != NULL)
9155 {
9156 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009157 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009158 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009159 if (vgetc_busy)
9160 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009161 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009162 }
9163}
9164
9165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 * "filereadable()" function
9167 */
9168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009170 typval_T *argvars;
9171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172{
9173 FILE *fd;
9174 char_u *p;
9175 int n;
9176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9179 {
9180 n = TRUE;
9181 fclose(fd);
9182 }
9183 else
9184 n = FALSE;
9185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187}
9188
9189/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009190 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 * rights to write into.
9192 */
9193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009195 typval_T *argvars;
9196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009198 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199}
9200
Bram Moolenaar33570922005-01-25 22:26:29 +00009201static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009202
9203 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009204findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009205 typval_T *argvars;
9206 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009207 int dir;
9208{
9209#ifdef FEAT_SEARCHPATH
9210 char_u *fname;
9211 char_u *fresult = NULL;
9212 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9213 char_u *p;
9214 char_u pathbuf[NUMBUFLEN];
9215 int count = 1;
9216 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009217 int error = FALSE;
9218#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009219
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009220 rettv->vval.v_string = NULL;
9221 rettv->v_type = VAR_STRING;
9222
9223#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009225
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009226 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9229 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009230 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 else
9232 {
9233 if (*p != NUL)
9234 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009237 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009239 }
9240
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009241 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9242 error = TRUE;
9243
9244 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 do
9247 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009248 if (rettv->v_type == VAR_STRING)
9249 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 fresult = find_file_in_path_option(first ? fname : NULL,
9251 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009252 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009253 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009254 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009255
9256 if (fresult != NULL && rettv->v_type == VAR_LIST)
9257 list_append_string(rettv->vval.v_list, fresult, -1);
9258
9259 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009261
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009262 if (rettv->v_type == VAR_STRING)
9263 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009264#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009265}
9266
Bram Moolenaar33570922005-01-25 22:26:29 +00009267static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9268static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009269
9270/*
9271 * Implementation of map() and filter().
9272 */
9273 static void
9274filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009275 typval_T *argvars;
9276 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009277 int map;
9278{
9279 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 listitem_T *li, *nli;
9282 list_T *l = NULL;
9283 dictitem_T *di;
9284 hashtab_T *ht;
9285 hashitem_T *hi;
9286 dict_T *d = NULL;
9287 typval_T save_val;
9288 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009289 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009290 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009291 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009292 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009293
9294 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009295 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009296 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009297 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009298 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009299 return;
9300 }
9301 else if (argvars[0].v_type == VAR_DICT)
9302 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009303 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009304 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009305 return;
9306 }
9307 else
9308 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009309 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009310 return;
9311 }
9312
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009313 expr = get_tv_string_buf_chk(&argvars[1], buf);
9314 /* On type errors, the preceding call has already displayed an error
9315 * message. Avoid a misleading error message for an empty string that
9316 * was not passed as argument. */
9317 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009319 prepare_vimvar(VV_VAL, &save_val);
9320 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009321
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009322 /* We reset "did_emsg" to be able to detect whether an error
9323 * occurred during evaluation of the expression. */
9324 save_did_emsg = did_emsg;
9325 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 prepare_vimvar(VV_KEY, &save_key);
9330 vimvars[VV_KEY].vv_type = VAR_STRING;
9331
9332 ht = &d->dv_hashtab;
9333 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009334 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 if (!HASHITEM_EMPTY(hi))
9338 {
9339 --todo;
9340 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009341 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 break;
9343 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009344 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009345 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 break;
9347 if (!map && rem)
9348 dictitem_remove(d, di);
9349 clear_tv(&vimvars[VV_KEY].vv_tv);
9350 }
9351 }
9352 hash_unlock(ht);
9353
9354 restore_vimvar(VV_KEY, &save_key);
9355 }
9356 else
9357 {
9358 for (li = l->lv_first; li != NULL; li = nli)
9359 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009360 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009361 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009362 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009363 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009364 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009365 break;
9366 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009367 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009368 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009369 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009371 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009372
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009373 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009374 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009375
9376 copy_tv(&argvars[0], rettv);
9377}
9378
9379 static int
9380filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009381 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009382 char_u *expr;
9383 int map;
9384 int *remp;
9385{
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009388 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009389
Bram Moolenaar33570922005-01-25 22:26:29 +00009390 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009391 s = expr;
9392 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009393 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009394 if (*s != NUL) /* check for trailing chars after expr */
9395 {
9396 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009397 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009398 }
9399 if (map)
9400 {
9401 /* map(): replace the list item value */
9402 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009403 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009404 *tv = rettv;
9405 }
9406 else
9407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 int error = FALSE;
9409
Bram Moolenaare9a41262005-01-15 22:18:47 +00009410 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009412 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009413 /* On type error, nothing has been removed; return FAIL to stop the
9414 * loop. The error message was given by get_tv_number_chk(). */
9415 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009416 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009417 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009418 retval = OK;
9419theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009421 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009422}
9423
9424/*
9425 * "filter()" function
9426 */
9427 static void
9428f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009429 typval_T *argvars;
9430 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009431{
9432 filter_map(argvars, rettv, FALSE);
9433}
9434
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009436 * "finddir({fname}[, {path}[, {count}]])" function
9437 */
9438 static void
9439f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009440 typval_T *argvars;
9441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009442{
9443 findfilendir(argvars, rettv, TRUE);
9444}
9445
9446/*
9447 * "findfile({fname}[, {path}[, {count}]])" function
9448 */
9449 static void
9450f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 typval_T *argvars;
9452 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009453{
9454 findfilendir(argvars, rettv, FALSE);
9455}
9456
9457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 * "fnamemodify({fname}, {mods})" function
9459 */
9460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009462 typval_T *argvars;
9463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
9465 char_u *fname;
9466 char_u *mods;
9467 int usedlen = 0;
9468 int len;
9469 char_u *fbuf = NULL;
9470 char_u buf[NUMBUFLEN];
9471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009472 fname = get_tv_string_chk(&argvars[0]);
9473 mods = get_tv_string_buf_chk(&argvars[1], buf);
9474 if (fname == NULL || mods == NULL)
9475 fname = NULL;
9476 else
9477 {
9478 len = (int)STRLEN(fname);
9479 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 vim_free(fbuf);
9488}
9489
Bram Moolenaar33570922005-01-25 22:26:29 +00009490static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491
9492/*
9493 * "foldclosed()" function
9494 */
9495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009497 typval_T *argvars;
9498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 int end;
9500{
9501#ifdef FEAT_FOLDING
9502 linenr_T lnum;
9503 linenr_T first, last;
9504
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009505 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9507 {
9508 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9509 {
9510 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 return;
9515 }
9516 }
9517#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519}
9520
9521/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009522 * "foldclosed()" function
9523 */
9524 static void
9525f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009528{
9529 foldclosed_both(argvars, rettv, FALSE);
9530}
9531
9532/*
9533 * "foldclosedend()" function
9534 */
9535 static void
9536f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009537 typval_T *argvars;
9538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009539{
9540 foldclosed_both(argvars, rettv, TRUE);
9541}
9542
9543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 * "foldlevel()" function
9545 */
9546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 typval_T *argvars;
9549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551#ifdef FEAT_FOLDING
9552 linenr_T lnum;
9553
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 else
9558#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560}
9561
9562/*
9563 * "foldtext()" function
9564 */
9565/*ARGSUSED*/
9566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 typval_T *argvars;
9569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
9571#ifdef FEAT_FOLDING
9572 linenr_T lnum;
9573 char_u *s;
9574 char_u *r;
9575 int len;
9576 char *txt;
9577#endif
9578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->v_type = VAR_STRING;
9580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009582 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9583 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9584 <= curbuf->b_ml.ml_line_count
9585 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 {
9587 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009588 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9589 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 {
9591 if (!linewhite(lnum))
9592 break;
9593 ++lnum;
9594 }
9595
9596 /* Find interesting text in this line. */
9597 s = skipwhite(ml_get(lnum));
9598 /* skip C comment-start */
9599 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009602 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009603 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009604 {
9605 s = skipwhite(ml_get(lnum + 1));
9606 if (*s == '*')
9607 s = skipwhite(s + 1);
9608 }
9609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 txt = _("+-%s%3ld lines: ");
9611 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 + 20 /* for %3ld */
9614 + STRLEN(s))); /* concatenated */
9615 if (r != NULL)
9616 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009617 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9618 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9619 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 len = (int)STRLEN(r);
9621 STRCAT(r, s);
9622 /* remove 'foldmarker' and 'commentstring' */
9623 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 }
9626 }
9627#endif
9628}
9629
9630/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009631 * "foldtextresult(lnum)" function
9632 */
9633/*ARGSUSED*/
9634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009636 typval_T *argvars;
9637 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009638{
9639#ifdef FEAT_FOLDING
9640 linenr_T lnum;
9641 char_u *text;
9642 char_u buf[51];
9643 foldinfo_T foldinfo;
9644 int fold_count;
9645#endif
9646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 rettv->v_type = VAR_STRING;
9648 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009649#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 /* treat illegal types and illegal string values for {lnum} the same */
9652 if (lnum < 0)
9653 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009654 fold_count = foldedCount(curwin, lnum, &foldinfo);
9655 if (fold_count > 0)
9656 {
9657 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9658 &foldinfo, buf);
9659 if (text == buf)
9660 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009662 }
9663#endif
9664}
9665
9666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 * "foreground()" function
9668 */
9669/*ARGSUSED*/
9670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009672 typval_T *argvars;
9673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676#ifdef FEAT_GUI
9677 if (gui.in_use)
9678 gui_mch_set_foreground();
9679#else
9680# ifdef WIN32
9681 win32_set_foreground();
9682# endif
9683#endif
9684}
9685
9686/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009687 * "function()" function
9688 */
9689/*ARGSUSED*/
9690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009692 typval_T *argvars;
9693 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009694{
9695 char_u *s;
9696
Bram Moolenaara7043832005-01-21 11:56:39 +00009697 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009699 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009700 EMSG2(_(e_invarg2), s);
9701 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009702 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009703 else
9704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->vval.v_string = vim_strsave(s);
9706 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009707 }
9708}
9709
9710/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009711 * "garbagecollect()" function
9712 */
9713/*ARGSUSED*/
9714 static void
9715f_garbagecollect(argvars, rettv)
9716 typval_T *argvars;
9717 typval_T *rettv;
9718{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009719 /* This is postponed until we are back at the toplevel, because we may be
9720 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9721 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009722}
9723
9724/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009725 * "get()" function
9726 */
9727 static void
9728f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009729 typval_T *argvars;
9730 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009731{
Bram Moolenaar33570922005-01-25 22:26:29 +00009732 listitem_T *li;
9733 list_T *l;
9734 dictitem_T *di;
9735 dict_T *d;
9736 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009737
Bram Moolenaare9a41262005-01-15 22:18:47 +00009738 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009739 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009740 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 int error = FALSE;
9743
9744 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9745 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009747 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009749 else if (argvars[0].v_type == VAR_DICT)
9750 {
9751 if ((d = argvars[0].vval.v_dict) != NULL)
9752 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009753 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009754 if (di != NULL)
9755 tv = &di->di_tv;
9756 }
9757 }
9758 else
9759 EMSG2(_(e_listdictarg), "get()");
9760
9761 if (tv == NULL)
9762 {
9763 if (argvars[2].v_type == VAR_UNKNOWN)
9764 rettv->vval.v_number = 0;
9765 else
9766 copy_tv(&argvars[2], rettv);
9767 }
9768 else
9769 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009770}
9771
Bram Moolenaar342337a2005-07-21 21:11:17 +00009772static 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 +00009773
9774/*
9775 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009776 * Return a range (from start to end) of lines in rettv from the specified
9777 * buffer.
9778 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009779 */
9780 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009781get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009782 buf_T *buf;
9783 linenr_T start;
9784 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009785 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009786 typval_T *rettv;
9787{
9788 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009789
Bram Moolenaar342337a2005-07-21 21:11:17 +00009790 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009791 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009792 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009793 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009794 }
9795 else
9796 rettv->vval.v_number = 0;
9797
9798 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9799 return;
9800
9801 if (!retlist)
9802 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009803 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9804 p = ml_get_buf(buf, start, FALSE);
9805 else
9806 p = (char_u *)"";
9807
9808 rettv->v_type = VAR_STRING;
9809 rettv->vval.v_string = vim_strsave(p);
9810 }
9811 else
9812 {
9813 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009814 return;
9815
9816 if (start < 1)
9817 start = 1;
9818 if (end > buf->b_ml.ml_line_count)
9819 end = buf->b_ml.ml_line_count;
9820 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009821 if (list_append_string(rettv->vval.v_list,
9822 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009823 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009824 }
9825}
9826
9827/*
9828 * "getbufline()" function
9829 */
9830 static void
9831f_getbufline(argvars, rettv)
9832 typval_T *argvars;
9833 typval_T *rettv;
9834{
9835 linenr_T lnum;
9836 linenr_T end;
9837 buf_T *buf;
9838
9839 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9840 ++emsg_off;
9841 buf = get_buf_tv(&argvars[0]);
9842 --emsg_off;
9843
Bram Moolenaar661b1822005-07-28 22:36:45 +00009844 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009845 if (argvars[2].v_type == VAR_UNKNOWN)
9846 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009847 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009848 end = get_tv_lnum_buf(&argvars[2], buf);
9849
Bram Moolenaar342337a2005-07-21 21:11:17 +00009850 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009851}
9852
Bram Moolenaar0d660222005-01-07 21:51:51 +00009853/*
9854 * "getbufvar()" function
9855 */
9856 static void
9857f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009858 typval_T *argvars;
9859 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009860{
9861 buf_T *buf;
9862 buf_T *save_curbuf;
9863 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009866 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9867 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009868 ++emsg_off;
9869 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009870
9871 rettv->v_type = VAR_STRING;
9872 rettv->vval.v_string = NULL;
9873
9874 if (buf != NULL && varname != NULL)
9875 {
9876 if (*varname == '&') /* buffer-local-option */
9877 {
9878 /* set curbuf to be our buf, temporarily */
9879 save_curbuf = curbuf;
9880 curbuf = buf;
9881
9882 get_option_tv(&varname, rettv, TRUE);
9883
9884 /* restore previous notion of curbuf */
9885 curbuf = save_curbuf;
9886 }
9887 else
9888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009889 if (*varname == NUL)
9890 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9891 * scope prefix before the NUL byte is required by
9892 * find_var_in_ht(). */
9893 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009894 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009895 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009896 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009898 }
9899 }
9900
9901 --emsg_off;
9902}
9903
9904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 * "getchar()" function
9906 */
9907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 typval_T *argvars;
9910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009913 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009915 /* Position the cursor. Needed after a message that ends in a space. */
9916 windgoto(msg_row, msg_col);
9917
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 ++no_mapping;
9919 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009920 for (;;)
9921 {
9922 if (argvars[0].v_type == VAR_UNKNOWN)
9923 /* getchar(): blocking wait. */
9924 n = safe_vgetc();
9925 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9926 /* getchar(1): only check if char avail */
9927 n = vpeekc();
9928 else if (error || vpeekc() == NUL)
9929 /* illegal argument or getchar(0) and no char avail: return zero */
9930 n = 0;
9931 else
9932 /* getchar(0) and char avail: return char */
9933 n = safe_vgetc();
9934 if (n == K_IGNORE)
9935 continue;
9936 break;
9937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 --no_mapping;
9939 --allow_keys;
9940
Bram Moolenaar219b8702006-11-01 14:32:36 +00009941 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9942 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9943 vimvars[VV_MOUSE_COL].vv_nr = 0;
9944
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 if (IS_SPECIAL(n) || mod_mask != 0)
9947 {
9948 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9949 int i = 0;
9950
9951 /* Turn a special key into three bytes, plus modifier. */
9952 if (mod_mask != 0)
9953 {
9954 temp[i++] = K_SPECIAL;
9955 temp[i++] = KS_MODIFIER;
9956 temp[i++] = mod_mask;
9957 }
9958 if (IS_SPECIAL(n))
9959 {
9960 temp[i++] = K_SPECIAL;
9961 temp[i++] = K_SECOND(n);
9962 temp[i++] = K_THIRD(n);
9963 }
9964#ifdef FEAT_MBYTE
9965 else if (has_mbyte)
9966 i += (*mb_char2bytes)(n, temp + i);
9967#endif
9968 else
9969 temp[i++] = n;
9970 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->v_type = VAR_STRING;
9972 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009973
9974#ifdef FEAT_MOUSE
9975 if (n == K_LEFTMOUSE
9976 || n == K_LEFTMOUSE_NM
9977 || n == K_LEFTDRAG
9978 || n == K_LEFTRELEASE
9979 || n == K_LEFTRELEASE_NM
9980 || n == K_MIDDLEMOUSE
9981 || n == K_MIDDLEDRAG
9982 || n == K_MIDDLERELEASE
9983 || n == K_RIGHTMOUSE
9984 || n == K_RIGHTDRAG
9985 || n == K_RIGHTRELEASE
9986 || n == K_X1MOUSE
9987 || n == K_X1DRAG
9988 || n == K_X1RELEASE
9989 || n == K_X2MOUSE
9990 || n == K_X2DRAG
9991 || n == K_X2RELEASE
9992 || n == K_MOUSEDOWN
9993 || n == K_MOUSEUP)
9994 {
9995 int row = mouse_row;
9996 int col = mouse_col;
9997 win_T *win;
9998 linenr_T lnum;
9999# ifdef FEAT_WINDOWS
10000 win_T *wp;
10001# endif
10002 int n = 1;
10003
10004 if (row >= 0 && col >= 0)
10005 {
10006 /* Find the window at the mouse coordinates and compute the
10007 * text position. */
10008 win = mouse_find_win(&row, &col);
10009 (void)mouse_comp_pos(win, &row, &col, &lnum);
10010# ifdef FEAT_WINDOWS
10011 for (wp = firstwin; wp != win; wp = wp->w_next)
10012 ++n;
10013# endif
10014 vimvars[VV_MOUSE_WIN].vv_nr = n;
10015 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10016 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10017 }
10018 }
10019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 }
10021}
10022
10023/*
10024 * "getcharmod()" function
10025 */
10026/*ARGSUSED*/
10027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010028f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010029 typval_T *argvars;
10030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033}
10034
10035/*
10036 * "getcmdline()" function
10037 */
10038/*ARGSUSED*/
10039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010041 typval_T *argvars;
10042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 rettv->v_type = VAR_STRING;
10045 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * "getcmdpos()" function
10050 */
10051/*ARGSUSED*/
10052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053f_getcmdpos(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->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058}
10059
10060/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010061 * "getcmdtype()" function
10062 */
10063/*ARGSUSED*/
10064 static void
10065f_getcmdtype(argvars, rettv)
10066 typval_T *argvars;
10067 typval_T *rettv;
10068{
10069 rettv->v_type = VAR_STRING;
10070 rettv->vval.v_string = alloc(2);
10071 if (rettv->vval.v_string != NULL)
10072 {
10073 rettv->vval.v_string[0] = get_cmdline_type();
10074 rettv->vval.v_string[1] = NUL;
10075 }
10076}
10077
10078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 * "getcwd()" function
10080 */
10081/*ARGSUSED*/
10082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010084 typval_T *argvars;
10085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086{
10087 char_u cwd[MAXPATHL];
10088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 else
10093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010096 if (rettv->vval.v_string != NULL)
10097 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098#endif
10099 }
10100}
10101
10102/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010103 * "getfontname()" function
10104 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010105/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010108 typval_T *argvars;
10109 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010110{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 rettv->v_type = VAR_STRING;
10112 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010113#ifdef FEAT_GUI
10114 if (gui.in_use)
10115 {
10116 GuiFont font;
10117 char_u *name = NULL;
10118
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010119 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010120 {
10121 /* Get the "Normal" font. Either the name saved by
10122 * hl_set_font_name() or from the font ID. */
10123 font = gui.norm_font;
10124 name = hl_get_font_name();
10125 }
10126 else
10127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010128 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010129 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10130 return;
10131 font = gui_mch_get_font(name, FALSE);
10132 if (font == NOFONT)
10133 return; /* Invalid font name, return empty string. */
10134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010136 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010137 gui_mch_free_font(font);
10138 }
10139#endif
10140}
10141
10142/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010143 * "getfperm({fname})" function
10144 */
10145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010147 typval_T *argvars;
10148 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010149{
10150 char_u *fname;
10151 struct stat st;
10152 char_u *perm = NULL;
10153 char_u flags[] = "rwx";
10154 int i;
10155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010159 if (mch_stat((char *)fname, &st) >= 0)
10160 {
10161 perm = vim_strsave((char_u *)"---------");
10162 if (perm != NULL)
10163 {
10164 for (i = 0; i < 9; i++)
10165 {
10166 if (st.st_mode & (1 << (8 - i)))
10167 perm[i] = flags[i % 3];
10168 }
10169 }
10170 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010172}
10173
10174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 * "getfsize({fname})" function
10176 */
10177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010179 typval_T *argvars;
10180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 char_u *fname;
10183 struct stat st;
10184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188
10189 if (mch_stat((char *)fname, &st) >= 0)
10190 {
10191 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010192 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010195 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010196
10197 /* non-perfect check for overflow */
10198 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10199 rettv->vval.v_number = -2;
10200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 }
10202 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010203 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204}
10205
10206/*
10207 * "getftime({fname})" function
10208 */
10209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010210f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010211 typval_T *argvars;
10212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 char_u *fname;
10215 struct stat st;
10216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218
10219 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010222 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223}
10224
10225/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010226 * "getftype({fname})" function
10227 */
10228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010229f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010230 typval_T *argvars;
10231 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010232{
10233 char_u *fname;
10234 struct stat st;
10235 char_u *type = NULL;
10236 char *t;
10237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010240 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010241 if (mch_lstat((char *)fname, &st) >= 0)
10242 {
10243#ifdef S_ISREG
10244 if (S_ISREG(st.st_mode))
10245 t = "file";
10246 else if (S_ISDIR(st.st_mode))
10247 t = "dir";
10248# ifdef S_ISLNK
10249 else if (S_ISLNK(st.st_mode))
10250 t = "link";
10251# endif
10252# ifdef S_ISBLK
10253 else if (S_ISBLK(st.st_mode))
10254 t = "bdev";
10255# endif
10256# ifdef S_ISCHR
10257 else if (S_ISCHR(st.st_mode))
10258 t = "cdev";
10259# endif
10260# ifdef S_ISFIFO
10261 else if (S_ISFIFO(st.st_mode))
10262 t = "fifo";
10263# endif
10264# ifdef S_ISSOCK
10265 else if (S_ISSOCK(st.st_mode))
10266 t = "fifo";
10267# endif
10268 else
10269 t = "other";
10270#else
10271# ifdef S_IFMT
10272 switch (st.st_mode & S_IFMT)
10273 {
10274 case S_IFREG: t = "file"; break;
10275 case S_IFDIR: t = "dir"; break;
10276# ifdef S_IFLNK
10277 case S_IFLNK: t = "link"; break;
10278# endif
10279# ifdef S_IFBLK
10280 case S_IFBLK: t = "bdev"; break;
10281# endif
10282# ifdef S_IFCHR
10283 case S_IFCHR: t = "cdev"; break;
10284# endif
10285# ifdef S_IFIFO
10286 case S_IFIFO: t = "fifo"; break;
10287# endif
10288# ifdef S_IFSOCK
10289 case S_IFSOCK: t = "socket"; break;
10290# endif
10291 default: t = "other";
10292 }
10293# else
10294 if (mch_isdir(fname))
10295 t = "dir";
10296 else
10297 t = "file";
10298# endif
10299#endif
10300 type = vim_strsave((char_u *)t);
10301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010303}
10304
10305/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010306 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010307 */
10308 static void
10309f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010310 typval_T *argvars;
10311 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010312{
10313 linenr_T lnum;
10314 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010315 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010316
10317 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010318 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010319 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010320 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010321 retlist = FALSE;
10322 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010323 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010324 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010325 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010326 retlist = TRUE;
10327 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010328
Bram Moolenaar342337a2005-07-21 21:11:17 +000010329 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010330}
10331
10332/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010333 * "getmatches()" function
10334 */
10335/*ARGSUSED*/
10336 static void
10337f_getmatches(argvars, rettv)
10338 typval_T *argvars;
10339 typval_T *rettv;
10340{
10341#ifdef FEAT_SEARCH_EXTRA
10342 dict_T *dict;
10343 matchitem_T *cur = curwin->w_match_head;
10344
10345 rettv->vval.v_number = 0;
10346
10347 if (rettv_list_alloc(rettv) == OK)
10348 {
10349 while (cur != NULL)
10350 {
10351 dict = dict_alloc();
10352 if (dict == NULL)
10353 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010354 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10355 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10356 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10357 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10358 list_append_dict(rettv->vval.v_list, dict);
10359 cur = cur->next;
10360 }
10361 }
10362#endif
10363}
10364
10365/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010366 * "getpos(string)" function
10367 */
10368 static void
10369f_getpos(argvars, rettv)
10370 typval_T *argvars;
10371 typval_T *rettv;
10372{
10373 pos_T *fp;
10374 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010375 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010376
10377 if (rettv_list_alloc(rettv) == OK)
10378 {
10379 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010380 fp = var2fpos(&argvars[0], TRUE, &fnum);
10381 if (fnum != -1)
10382 list_append_number(l, (varnumber_T)fnum);
10383 else
10384 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010385 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10386 : (varnumber_T)0);
10387 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10388 : (varnumber_T)0);
10389 list_append_number(l,
10390#ifdef FEAT_VIRTUALEDIT
10391 (fp != NULL) ? (varnumber_T)fp->coladd :
10392#endif
10393 (varnumber_T)0);
10394 }
10395 else
10396 rettv->vval.v_number = FALSE;
10397}
10398
10399/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010400 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010401 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010402/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010403 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010404f_getqflist(argvars, rettv)
10405 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010406 typval_T *rettv;
10407{
10408#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010409 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010410#endif
10411
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010412 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010413#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010414 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010415 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010416 wp = NULL;
10417 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10418 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010419 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010420 if (wp == NULL)
10421 return;
10422 }
10423
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010424 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010425 }
10426#endif
10427}
10428
10429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 * "getreg()" function
10431 */
10432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010433f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010434 typval_T *argvars;
10435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436{
10437 char_u *strregname;
10438 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010439 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010442 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 strregname = get_tv_string_chk(&argvars[0]);
10445 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010446 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010450 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 regname = (strregname == NULL ? '"' : *strregname);
10452 if (regname == 0)
10453 regname = '"';
10454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010456 rettv->vval.v_string = error ? NULL :
10457 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458}
10459
10460/*
10461 * "getregtype()" function
10462 */
10463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010465 typval_T *argvars;
10466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467{
10468 char_u *strregname;
10469 int regname;
10470 char_u buf[NUMBUFLEN + 2];
10471 long reglen = 0;
10472
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010473 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 {
10475 strregname = get_tv_string_chk(&argvars[0]);
10476 if (strregname == NULL) /* type error; errmsg already given */
10477 {
10478 rettv->v_type = VAR_STRING;
10479 rettv->vval.v_string = NULL;
10480 return;
10481 }
10482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 else
10484 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010485 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486
10487 regname = (strregname == NULL ? '"' : *strregname);
10488 if (regname == 0)
10489 regname = '"';
10490
10491 buf[0] = NUL;
10492 buf[1] = NUL;
10493 switch (get_reg_type(regname, &reglen))
10494 {
10495 case MLINE: buf[0] = 'V'; break;
10496 case MCHAR: buf[0] = 'v'; break;
10497#ifdef FEAT_VISUAL
10498 case MBLOCK:
10499 buf[0] = Ctrl_V;
10500 sprintf((char *)buf + 1, "%ld", reglen + 1);
10501 break;
10502#endif
10503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504 rettv->v_type = VAR_STRING;
10505 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506}
10507
10508/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010509 * "gettabwinvar()" function
10510 */
10511 static void
10512f_gettabwinvar(argvars, rettv)
10513 typval_T *argvars;
10514 typval_T *rettv;
10515{
10516 getwinvar(argvars, rettv, 1);
10517}
10518
10519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 * "getwinposx()" function
10521 */
10522/*ARGSUSED*/
10523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010525 typval_T *argvars;
10526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529#ifdef FEAT_GUI
10530 if (gui.in_use)
10531 {
10532 int x, y;
10533
10534 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 }
10537#endif
10538}
10539
10540/*
10541 * "getwinposy()" function
10542 */
10543/*ARGSUSED*/
10544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010546 typval_T *argvars;
10547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550#ifdef FEAT_GUI
10551 if (gui.in_use)
10552 {
10553 int x, y;
10554
10555 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 }
10558#endif
10559}
10560
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010561/*
10562 * Find window specifed by "vp" in tabpage "tp".
10563 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010564 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010565find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010566 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010567 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010568{
10569#ifdef FEAT_WINDOWS
10570 win_T *wp;
10571#endif
10572 int nr;
10573
10574 nr = get_tv_number_chk(vp, NULL);
10575
10576#ifdef FEAT_WINDOWS
10577 if (nr < 0)
10578 return NULL;
10579 if (nr == 0)
10580 return curwin;
10581
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010582 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10583 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010584 if (--nr <= 0)
10585 break;
10586 return wp;
10587#else
10588 if (nr == 0 || nr == 1)
10589 return curwin;
10590 return NULL;
10591#endif
10592}
10593
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594/*
10595 * "getwinvar()" function
10596 */
10597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010599 typval_T *argvars;
10600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010602 getwinvar(argvars, rettv, 0);
10603}
10604
10605/*
10606 * getwinvar() and gettabwinvar()
10607 */
10608 static void
10609getwinvar(argvars, rettv, off)
10610 typval_T *argvars;
10611 typval_T *rettv;
10612 int off; /* 1 for gettabwinvar() */
10613{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 win_T *win, *oldcurwin;
10615 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010616 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010617 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010619#ifdef FEAT_WINDOWS
10620 if (off == 1)
10621 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10622 else
10623 tp = curtab;
10624#endif
10625 win = find_win_by_nr(&argvars[off], tp);
10626 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 rettv->v_type = VAR_STRING;
10630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631
10632 if (win != NULL && varname != NULL)
10633 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010634 /* Set curwin to be our win, temporarily. Also set curbuf, so
10635 * that we can get buffer-local options. */
10636 oldcurwin = curwin;
10637 curwin = win;
10638 curbuf = win->w_buffer;
10639
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010641 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 else
10643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 if (*varname == NUL)
10645 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10646 * scope prefix before the NUL byte is required by
10647 * find_var_in_ht(). */
10648 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010650 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010654
10655 /* restore previous notion of curwin */
10656 curwin = oldcurwin;
10657 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 }
10659
10660 --emsg_off;
10661}
10662
10663/*
10664 * "glob()" function
10665 */
10666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010668 typval_T *argvars;
10669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670{
10671 expand_T xpc;
10672
10673 ExpandInit(&xpc);
10674 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675 rettv->v_type = VAR_STRING;
10676 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678}
10679
10680/*
10681 * "globpath()" function
10682 */
10683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010685 typval_T *argvars;
10686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
10688 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 if (file == NULL)
10693 rettv->vval.v_string = NULL;
10694 else
10695 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696}
10697
10698/*
10699 * "has()" function
10700 */
10701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010702f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010703 typval_T *argvars;
10704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705{
10706 int i;
10707 char_u *name;
10708 int n = FALSE;
10709 static char *(has_list[]) =
10710 {
10711#ifdef AMIGA
10712 "amiga",
10713# ifdef FEAT_ARP
10714 "arp",
10715# endif
10716#endif
10717#ifdef __BEOS__
10718 "beos",
10719#endif
10720#ifdef MSDOS
10721# ifdef DJGPP
10722 "dos32",
10723# else
10724 "dos16",
10725# endif
10726#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010727#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 "mac",
10729#endif
10730#if defined(MACOS_X_UNIX)
10731 "macunix",
10732#endif
10733#ifdef OS2
10734 "os2",
10735#endif
10736#ifdef __QNX__
10737 "qnx",
10738#endif
10739#ifdef RISCOS
10740 "riscos",
10741#endif
10742#ifdef UNIX
10743 "unix",
10744#endif
10745#ifdef VMS
10746 "vms",
10747#endif
10748#ifdef WIN16
10749 "win16",
10750#endif
10751#ifdef WIN32
10752 "win32",
10753#endif
10754#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10755 "win32unix",
10756#endif
10757#ifdef WIN64
10758 "win64",
10759#endif
10760#ifdef EBCDIC
10761 "ebcdic",
10762#endif
10763#ifndef CASE_INSENSITIVE_FILENAME
10764 "fname_case",
10765#endif
10766#ifdef FEAT_ARABIC
10767 "arabic",
10768#endif
10769#ifdef FEAT_AUTOCMD
10770 "autocmd",
10771#endif
10772#ifdef FEAT_BEVAL
10773 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010774# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10775 "balloon_multiline",
10776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777#endif
10778#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10779 "builtin_terms",
10780# ifdef ALL_BUILTIN_TCAPS
10781 "all_builtin_terms",
10782# endif
10783#endif
10784#ifdef FEAT_BYTEOFF
10785 "byte_offset",
10786#endif
10787#ifdef FEAT_CINDENT
10788 "cindent",
10789#endif
10790#ifdef FEAT_CLIENTSERVER
10791 "clientserver",
10792#endif
10793#ifdef FEAT_CLIPBOARD
10794 "clipboard",
10795#endif
10796#ifdef FEAT_CMDL_COMPL
10797 "cmdline_compl",
10798#endif
10799#ifdef FEAT_CMDHIST
10800 "cmdline_hist",
10801#endif
10802#ifdef FEAT_COMMENTS
10803 "comments",
10804#endif
10805#ifdef FEAT_CRYPT
10806 "cryptv",
10807#endif
10808#ifdef FEAT_CSCOPE
10809 "cscope",
10810#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010811#ifdef CURSOR_SHAPE
10812 "cursorshape",
10813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814#ifdef DEBUG
10815 "debug",
10816#endif
10817#ifdef FEAT_CON_DIALOG
10818 "dialog_con",
10819#endif
10820#ifdef FEAT_GUI_DIALOG
10821 "dialog_gui",
10822#endif
10823#ifdef FEAT_DIFF
10824 "diff",
10825#endif
10826#ifdef FEAT_DIGRAPHS
10827 "digraphs",
10828#endif
10829#ifdef FEAT_DND
10830 "dnd",
10831#endif
10832#ifdef FEAT_EMACS_TAGS
10833 "emacs_tags",
10834#endif
10835 "eval", /* always present, of course! */
10836#ifdef FEAT_EX_EXTRA
10837 "ex_extra",
10838#endif
10839#ifdef FEAT_SEARCH_EXTRA
10840 "extra_search",
10841#endif
10842#ifdef FEAT_FKMAP
10843 "farsi",
10844#endif
10845#ifdef FEAT_SEARCHPATH
10846 "file_in_path",
10847#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010848#if defined(UNIX) && !defined(USE_SYSTEM)
10849 "filterpipe",
10850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851#ifdef FEAT_FIND_ID
10852 "find_in_path",
10853#endif
10854#ifdef FEAT_FOLDING
10855 "folding",
10856#endif
10857#ifdef FEAT_FOOTER
10858 "footer",
10859#endif
10860#if !defined(USE_SYSTEM) && defined(UNIX)
10861 "fork",
10862#endif
10863#ifdef FEAT_GETTEXT
10864 "gettext",
10865#endif
10866#ifdef FEAT_GUI
10867 "gui",
10868#endif
10869#ifdef FEAT_GUI_ATHENA
10870# ifdef FEAT_GUI_NEXTAW
10871 "gui_neXtaw",
10872# else
10873 "gui_athena",
10874# endif
10875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876#ifdef FEAT_GUI_GTK
10877 "gui_gtk",
10878# ifdef HAVE_GTK2
10879 "gui_gtk2",
10880# endif
10881#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000010882#ifdef FEAT_GUI_GNOME
10883 "gui_gnome",
10884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885#ifdef FEAT_GUI_MAC
10886 "gui_mac",
10887#endif
10888#ifdef FEAT_GUI_MOTIF
10889 "gui_motif",
10890#endif
10891#ifdef FEAT_GUI_PHOTON
10892 "gui_photon",
10893#endif
10894#ifdef FEAT_GUI_W16
10895 "gui_win16",
10896#endif
10897#ifdef FEAT_GUI_W32
10898 "gui_win32",
10899#endif
10900#ifdef FEAT_HANGULIN
10901 "hangul_input",
10902#endif
10903#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10904 "iconv",
10905#endif
10906#ifdef FEAT_INS_EXPAND
10907 "insert_expand",
10908#endif
10909#ifdef FEAT_JUMPLIST
10910 "jumplist",
10911#endif
10912#ifdef FEAT_KEYMAP
10913 "keymap",
10914#endif
10915#ifdef FEAT_LANGMAP
10916 "langmap",
10917#endif
10918#ifdef FEAT_LIBCALL
10919 "libcall",
10920#endif
10921#ifdef FEAT_LINEBREAK
10922 "linebreak",
10923#endif
10924#ifdef FEAT_LISP
10925 "lispindent",
10926#endif
10927#ifdef FEAT_LISTCMDS
10928 "listcmds",
10929#endif
10930#ifdef FEAT_LOCALMAP
10931 "localmap",
10932#endif
10933#ifdef FEAT_MENU
10934 "menu",
10935#endif
10936#ifdef FEAT_SESSION
10937 "mksession",
10938#endif
10939#ifdef FEAT_MODIFY_FNAME
10940 "modify_fname",
10941#endif
10942#ifdef FEAT_MOUSE
10943 "mouse",
10944#endif
10945#ifdef FEAT_MOUSESHAPE
10946 "mouseshape",
10947#endif
10948#if defined(UNIX) || defined(VMS)
10949# ifdef FEAT_MOUSE_DEC
10950 "mouse_dec",
10951# endif
10952# ifdef FEAT_MOUSE_GPM
10953 "mouse_gpm",
10954# endif
10955# ifdef FEAT_MOUSE_JSB
10956 "mouse_jsbterm",
10957# endif
10958# ifdef FEAT_MOUSE_NET
10959 "mouse_netterm",
10960# endif
10961# ifdef FEAT_MOUSE_PTERM
10962 "mouse_pterm",
10963# endif
10964# ifdef FEAT_MOUSE_XTERM
10965 "mouse_xterm",
10966# endif
10967#endif
10968#ifdef FEAT_MBYTE
10969 "multi_byte",
10970#endif
10971#ifdef FEAT_MBYTE_IME
10972 "multi_byte_ime",
10973#endif
10974#ifdef FEAT_MULTI_LANG
10975 "multi_lang",
10976#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010977#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010978#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010979 "mzscheme",
10980#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982#ifdef FEAT_OLE
10983 "ole",
10984#endif
10985#ifdef FEAT_OSFILETYPE
10986 "osfiletype",
10987#endif
10988#ifdef FEAT_PATH_EXTRA
10989 "path_extra",
10990#endif
10991#ifdef FEAT_PERL
10992#ifndef DYNAMIC_PERL
10993 "perl",
10994#endif
10995#endif
10996#ifdef FEAT_PYTHON
10997#ifndef DYNAMIC_PYTHON
10998 "python",
10999#endif
11000#endif
11001#ifdef FEAT_POSTSCRIPT
11002 "postscript",
11003#endif
11004#ifdef FEAT_PRINTER
11005 "printer",
11006#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011007#ifdef FEAT_PROFILE
11008 "profile",
11009#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011010#ifdef FEAT_RELTIME
11011 "reltime",
11012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013#ifdef FEAT_QUICKFIX
11014 "quickfix",
11015#endif
11016#ifdef FEAT_RIGHTLEFT
11017 "rightleft",
11018#endif
11019#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11020 "ruby",
11021#endif
11022#ifdef FEAT_SCROLLBIND
11023 "scrollbind",
11024#endif
11025#ifdef FEAT_CMDL_INFO
11026 "showcmd",
11027 "cmdline_info",
11028#endif
11029#ifdef FEAT_SIGNS
11030 "signs",
11031#endif
11032#ifdef FEAT_SMARTINDENT
11033 "smartindent",
11034#endif
11035#ifdef FEAT_SNIFF
11036 "sniff",
11037#endif
11038#ifdef FEAT_STL_OPT
11039 "statusline",
11040#endif
11041#ifdef FEAT_SUN_WORKSHOP
11042 "sun_workshop",
11043#endif
11044#ifdef FEAT_NETBEANS_INTG
11045 "netbeans_intg",
11046#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011047#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011048 "spell",
11049#endif
11050#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 "syntax",
11052#endif
11053#if defined(USE_SYSTEM) || !defined(UNIX)
11054 "system",
11055#endif
11056#ifdef FEAT_TAG_BINS
11057 "tag_binary",
11058#endif
11059#ifdef FEAT_TAG_OLDSTATIC
11060 "tag_old_static",
11061#endif
11062#ifdef FEAT_TAG_ANYWHITE
11063 "tag_any_white",
11064#endif
11065#ifdef FEAT_TCL
11066# ifndef DYNAMIC_TCL
11067 "tcl",
11068# endif
11069#endif
11070#ifdef TERMINFO
11071 "terminfo",
11072#endif
11073#ifdef FEAT_TERMRESPONSE
11074 "termresponse",
11075#endif
11076#ifdef FEAT_TEXTOBJ
11077 "textobjects",
11078#endif
11079#ifdef HAVE_TGETENT
11080 "tgetent",
11081#endif
11082#ifdef FEAT_TITLE
11083 "title",
11084#endif
11085#ifdef FEAT_TOOLBAR
11086 "toolbar",
11087#endif
11088#ifdef FEAT_USR_CMDS
11089 "user-commands", /* was accidentally included in 5.4 */
11090 "user_commands",
11091#endif
11092#ifdef FEAT_VIMINFO
11093 "viminfo",
11094#endif
11095#ifdef FEAT_VERTSPLIT
11096 "vertsplit",
11097#endif
11098#ifdef FEAT_VIRTUALEDIT
11099 "virtualedit",
11100#endif
11101#ifdef FEAT_VISUAL
11102 "visual",
11103#endif
11104#ifdef FEAT_VISUALEXTRA
11105 "visualextra",
11106#endif
11107#ifdef FEAT_VREPLACE
11108 "vreplace",
11109#endif
11110#ifdef FEAT_WILDIGN
11111 "wildignore",
11112#endif
11113#ifdef FEAT_WILDMENU
11114 "wildmenu",
11115#endif
11116#ifdef FEAT_WINDOWS
11117 "windows",
11118#endif
11119#ifdef FEAT_WAK
11120 "winaltkeys",
11121#endif
11122#ifdef FEAT_WRITEBACKUP
11123 "writebackup",
11124#endif
11125#ifdef FEAT_XIM
11126 "xim",
11127#endif
11128#ifdef FEAT_XFONTSET
11129 "xfontset",
11130#endif
11131#ifdef USE_XSMP
11132 "xsmp",
11133#endif
11134#ifdef USE_XSMP_INTERACT
11135 "xsmp_interact",
11136#endif
11137#ifdef FEAT_XCLIPBOARD
11138 "xterm_clipboard",
11139#endif
11140#ifdef FEAT_XTERM_SAVE
11141 "xterm_save",
11142#endif
11143#if defined(UNIX) && defined(FEAT_X11)
11144 "X11",
11145#endif
11146 NULL
11147 };
11148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150 for (i = 0; has_list[i] != NULL; ++i)
11151 if (STRICMP(name, has_list[i]) == 0)
11152 {
11153 n = TRUE;
11154 break;
11155 }
11156
11157 if (n == FALSE)
11158 {
11159 if (STRNICMP(name, "patch", 5) == 0)
11160 n = has_patch(atoi((char *)name + 5));
11161 else if (STRICMP(name, "vim_starting") == 0)
11162 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011163#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11164 else if (STRICMP(name, "balloon_multiline") == 0)
11165 n = multiline_balloon_available();
11166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167#ifdef DYNAMIC_TCL
11168 else if (STRICMP(name, "tcl") == 0)
11169 n = tcl_enabled(FALSE);
11170#endif
11171#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11172 else if (STRICMP(name, "iconv") == 0)
11173 n = iconv_enabled(FALSE);
11174#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011175#ifdef DYNAMIC_MZSCHEME
11176 else if (STRICMP(name, "mzscheme") == 0)
11177 n = mzscheme_enabled(FALSE);
11178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179#ifdef DYNAMIC_RUBY
11180 else if (STRICMP(name, "ruby") == 0)
11181 n = ruby_enabled(FALSE);
11182#endif
11183#ifdef DYNAMIC_PYTHON
11184 else if (STRICMP(name, "python") == 0)
11185 n = python_enabled(FALSE);
11186#endif
11187#ifdef DYNAMIC_PERL
11188 else if (STRICMP(name, "perl") == 0)
11189 n = perl_enabled(FALSE);
11190#endif
11191#ifdef FEAT_GUI
11192 else if (STRICMP(name, "gui_running") == 0)
11193 n = (gui.in_use || gui.starting);
11194# ifdef FEAT_GUI_W32
11195 else if (STRICMP(name, "gui_win32s") == 0)
11196 n = gui_is_win32s();
11197# endif
11198# ifdef FEAT_BROWSE
11199 else if (STRICMP(name, "browse") == 0)
11200 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11201# endif
11202#endif
11203#ifdef FEAT_SYN_HL
11204 else if (STRICMP(name, "syntax_items") == 0)
11205 n = syntax_present(curbuf);
11206#endif
11207#if defined(WIN3264)
11208 else if (STRICMP(name, "win95") == 0)
11209 n = mch_windows95();
11210#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011211#ifdef FEAT_NETBEANS_INTG
11212 else if (STRICMP(name, "netbeans_enabled") == 0)
11213 n = usingNetbeans;
11214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 }
11216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218}
11219
11220/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011221 * "has_key()" function
11222 */
11223 static void
11224f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011225 typval_T *argvars;
11226 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227{
11228 rettv->vval.v_number = 0;
11229 if (argvars[0].v_type != VAR_DICT)
11230 {
11231 EMSG(_(e_dictreq));
11232 return;
11233 }
11234 if (argvars[0].vval.v_dict == NULL)
11235 return;
11236
11237 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011238 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239}
11240
11241/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011242 * "haslocaldir()" function
11243 */
11244/*ARGSUSED*/
11245 static void
11246f_haslocaldir(argvars, rettv)
11247 typval_T *argvars;
11248 typval_T *rettv;
11249{
11250 rettv->vval.v_number = (curwin->w_localdir != NULL);
11251}
11252
11253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 * "hasmapto()" function
11255 */
11256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011258 typval_T *argvars;
11259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260{
11261 char_u *name;
11262 char_u *mode;
11263 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011264 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011267 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 mode = (char_u *)"nvo";
11269 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011272 if (argvars[2].v_type != VAR_UNKNOWN)
11273 abbr = get_tv_number(&argvars[2]);
11274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275
Bram Moolenaar2c932302006-03-18 21:42:09 +000011276 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280}
11281
11282/*
11283 * "histadd()" function
11284 */
11285/*ARGSUSED*/
11286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 typval_T *argvars;
11289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
11291#ifdef FEAT_CMDHIST
11292 int histype;
11293 char_u *str;
11294 char_u buf[NUMBUFLEN];
11295#endif
11296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 if (check_restricted() || check_secure())
11299 return;
11300#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11302 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 if (histype >= 0)
11304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 if (*str != NUL)
11307 {
11308 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 return;
11311 }
11312 }
11313#endif
11314}
11315
11316/*
11317 * "histdel()" function
11318 */
11319/*ARGSUSED*/
11320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *argvars;
11323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324{
11325#ifdef FEAT_CMDHIST
11326 int n;
11327 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011330 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11331 if (str == NULL)
11332 n = 0;
11333 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011336 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011338 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 else
11341 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 get_tv_string_buf(&argvars[1], buf));
11344 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347#endif
11348}
11349
11350/*
11351 * "histget()" function
11352 */
11353/*ARGSUSED*/
11354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011356 typval_T *argvars;
11357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358{
11359#ifdef FEAT_CMDHIST
11360 int type;
11361 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11365 if (str == NULL)
11366 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 {
11369 type = get_histtype(str);
11370 if (argvars[1].v_type == VAR_UNKNOWN)
11371 idx = get_history_idx(type);
11372 else
11373 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11374 /* -1 on type error */
11375 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381}
11382
11383/*
11384 * "histnr()" function
11385 */
11386/*ARGSUSED*/
11387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391{
11392 int i;
11393
11394#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 char_u *history = get_tv_string_chk(&argvars[0]);
11396
11397 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 if (i >= HIST_CMD && i < HIST_COUNT)
11399 i = get_history_idx(i);
11400 else
11401#endif
11402 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404}
11405
11406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 * "highlightID(name)" function
11408 */
11409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 typval_T *argvars;
11412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415}
11416
11417/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011418 * "highlight_exists()" function
11419 */
11420 static void
11421f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011422 typval_T *argvars;
11423 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011424{
11425 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11426}
11427
11428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 * "hostname()" function
11430 */
11431/*ARGSUSED*/
11432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011434 typval_T *argvars;
11435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436{
11437 char_u hostname[256];
11438
11439 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->v_type = VAR_STRING;
11441 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442}
11443
11444/*
11445 * iconv() function
11446 */
11447/*ARGSUSED*/
11448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011450 typval_T *argvars;
11451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452{
11453#ifdef FEAT_MBYTE
11454 char_u buf1[NUMBUFLEN];
11455 char_u buf2[NUMBUFLEN];
11456 char_u *from, *to, *str;
11457 vimconv_T vimconv;
11458#endif
11459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->v_type = VAR_STRING;
11461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462
11463#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 str = get_tv_string(&argvars[0]);
11465 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11466 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 vimconv.vc_type = CONV_NONE;
11468 convert_setup(&vimconv, from, to);
11469
11470 /* If the encodings are equal, no conversion needed. */
11471 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475
11476 convert_setup(&vimconv, NULL, NULL);
11477 vim_free(from);
11478 vim_free(to);
11479#endif
11480}
11481
11482/*
11483 * "indent()" function
11484 */
11485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011487 typval_T *argvars;
11488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489{
11490 linenr_T lnum;
11491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497}
11498
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011499/*
11500 * "index()" function
11501 */
11502 static void
11503f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011504 typval_T *argvars;
11505 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011506{
Bram Moolenaar33570922005-01-25 22:26:29 +000011507 list_T *l;
11508 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011509 long idx = 0;
11510 int ic = FALSE;
11511
11512 rettv->vval.v_number = -1;
11513 if (argvars[0].v_type != VAR_LIST)
11514 {
11515 EMSG(_(e_listreq));
11516 return;
11517 }
11518 l = argvars[0].vval.v_list;
11519 if (l != NULL)
11520 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011521 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011522 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011524 int error = FALSE;
11525
Bram Moolenaar758711c2005-02-02 23:11:38 +000011526 /* Start at specified item. Use the cached index that list_find()
11527 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011529 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011530 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 ic = get_tv_number_chk(&argvars[3], &error);
11532 if (error)
11533 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011534 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011535
Bram Moolenaar758711c2005-02-02 23:11:38 +000011536 for ( ; item != NULL; item = item->li_next, ++idx)
11537 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011538 {
11539 rettv->vval.v_number = idx;
11540 break;
11541 }
11542 }
11543}
11544
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545static int inputsecret_flag = 0;
11546
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011547static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11548
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011550 * This function is used by f_input() and f_inputdialog() functions. The third
11551 * argument to f_input() specifies the type of completion to use at the
11552 * prompt. The third argument to f_inputdialog() specifies the value to return
11553 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 */
11555 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011556get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011557 typval_T *argvars;
11558 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011559 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011561 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 char_u *p = NULL;
11563 int c;
11564 char_u buf[NUMBUFLEN];
11565 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011566 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011567 int xp_type = EXPAND_NOTHING;
11568 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011571 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572
11573#ifdef NO_CONSOLE_INPUT
11574 /* While starting up, there is no place to enter text. */
11575 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577#endif
11578
11579 cmd_silent = FALSE; /* Want to see the prompt. */
11580 if (prompt != NULL)
11581 {
11582 /* Only the part of the message after the last NL is considered as
11583 * prompt for the command line */
11584 p = vim_strrchr(prompt, '\n');
11585 if (p == NULL)
11586 p = prompt;
11587 else
11588 {
11589 ++p;
11590 c = *p;
11591 *p = NUL;
11592 msg_start();
11593 msg_clr_eos();
11594 msg_puts_attr(prompt, echo_attr);
11595 msg_didout = FALSE;
11596 msg_starthere();
11597 *p = c;
11598 }
11599 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011601 if (argvars[1].v_type != VAR_UNKNOWN)
11602 {
11603 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11604 if (defstr != NULL)
11605 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011607 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011608 {
11609 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011610 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011611 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011612
Bram Moolenaar4463f292005-09-25 22:20:24 +000011613 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011614
Bram Moolenaar4463f292005-09-25 22:20:24 +000011615 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11616 if (xp_name == NULL)
11617 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011618
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011619 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011620
Bram Moolenaar4463f292005-09-25 22:20:24 +000011621 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11622 &xp_arg) == FAIL)
11623 return;
11624 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011625 }
11626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011627 if (defstr != NULL)
11628 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011629 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11630 xp_type, xp_arg);
11631
11632 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011634 /* since the user typed this, no need to wait for return */
11635 need_wait_return = FALSE;
11636 msg_didout = FALSE;
11637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 cmd_silent = cmd_silent_save;
11639}
11640
11641/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011642 * "input()" function
11643 * Also handles inputsecret() when inputsecret is set.
11644 */
11645 static void
11646f_input(argvars, rettv)
11647 typval_T *argvars;
11648 typval_T *rettv;
11649{
11650 get_user_input(argvars, rettv, FALSE);
11651}
11652
11653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 * "inputdialog()" function
11655 */
11656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011658 typval_T *argvars;
11659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660{
11661#if defined(FEAT_GUI_TEXTDIALOG)
11662 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11663 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11664 {
11665 char_u *message;
11666 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011669 message = get_tv_string_chk(&argvars[0]);
11670 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011671 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011672 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 else
11674 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 if (message != NULL && defstr != NULL
11676 && do_dialog(VIM_QUESTION, NULL, message,
11677 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011678 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 else
11680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011681 if (message != NULL && defstr != NULL
11682 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011683 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684 rettv->vval.v_string = vim_strsave(
11685 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 }
11691 else
11692#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011693 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694}
11695
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011696/*
11697 * "inputlist()" function
11698 */
11699 static void
11700f_inputlist(argvars, rettv)
11701 typval_T *argvars;
11702 typval_T *rettv;
11703{
11704 listitem_T *li;
11705 int selected;
11706 int mouse_used;
11707
11708 rettv->vval.v_number = 0;
11709#ifdef NO_CONSOLE_INPUT
11710 /* While starting up, there is no place to enter text. */
11711 if (no_console_input())
11712 return;
11713#endif
11714 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11715 {
11716 EMSG2(_(e_listarg), "inputlist()");
11717 return;
11718 }
11719
11720 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011721 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011722 lines_left = Rows; /* avoid more prompt */
11723 msg_scroll = TRUE;
11724 msg_clr_eos();
11725
11726 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11727 {
11728 msg_puts(get_tv_string(&li->li_tv));
11729 msg_putchar('\n');
11730 }
11731
11732 /* Ask for choice. */
11733 selected = prompt_for_number(&mouse_used);
11734 if (mouse_used)
11735 selected -= lines_left;
11736
11737 rettv->vval.v_number = selected;
11738}
11739
11740
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11742
11743/*
11744 * "inputrestore()" function
11745 */
11746/*ARGSUSED*/
11747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011749 typval_T *argvars;
11750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751{
11752 if (ga_userinput.ga_len > 0)
11753 {
11754 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11756 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 }
11759 else if (p_verbose > 1)
11760 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011761 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 }
11764}
11765
11766/*
11767 * "inputsave()" function
11768 */
11769/*ARGSUSED*/
11770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011772 typval_T *argvars;
11773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774{
11775 /* Add an entry to the stack of typehead storage. */
11776 if (ga_grow(&ga_userinput, 1) == OK)
11777 {
11778 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11779 + ga_userinput.ga_len);
11780 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782 }
11783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785}
11786
11787/*
11788 * "inputsecret()" function
11789 */
11790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011791f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011792 typval_T *argvars;
11793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794{
11795 ++cmdline_star;
11796 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 --cmdline_star;
11799 --inputsecret_flag;
11800}
11801
11802/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011803 * "insert()" function
11804 */
11805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011807 typval_T *argvars;
11808 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011809{
11810 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011811 listitem_T *item;
11812 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011814
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011815 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011816 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011817 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011818 else if ((l = argvars[0].vval.v_list) != NULL
11819 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011820 {
11821 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 before = get_tv_number_chk(&argvars[2], &error);
11823 if (error)
11824 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011825
Bram Moolenaar758711c2005-02-02 23:11:38 +000011826 if (before == l->lv_len)
11827 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011828 else
11829 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011830 item = list_find(l, before);
11831 if (item == NULL)
11832 {
11833 EMSGN(_(e_listidx), before);
11834 l = NULL;
11835 }
11836 }
11837 if (l != NULL)
11838 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011839 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011840 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011841 }
11842 }
11843}
11844
11845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 * "isdirectory()" function
11847 */
11848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011850 typval_T *argvars;
11851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854}
11855
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011856/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011857 * "islocked()" function
11858 */
11859 static void
11860f_islocked(argvars, rettv)
11861 typval_T *argvars;
11862 typval_T *rettv;
11863{
11864 lval_T lv;
11865 char_u *end;
11866 dictitem_T *di;
11867
11868 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011869 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11870 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011871 if (end != NULL && lv.ll_name != NULL)
11872 {
11873 if (*end != NUL)
11874 EMSG(_(e_trailing));
11875 else
11876 {
11877 if (lv.ll_tv == NULL)
11878 {
11879 if (check_changedtick(lv.ll_name))
11880 rettv->vval.v_number = 1; /* always locked */
11881 else
11882 {
11883 di = find_var(lv.ll_name, NULL);
11884 if (di != NULL)
11885 {
11886 /* Consider a variable locked when:
11887 * 1. the variable itself is locked
11888 * 2. the value of the variable is locked.
11889 * 3. the List or Dict value is locked.
11890 */
11891 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11892 || tv_islocked(&di->di_tv));
11893 }
11894 }
11895 }
11896 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011897 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011898 else if (lv.ll_newkey != NULL)
11899 EMSG2(_(e_dictkey), lv.ll_newkey);
11900 else if (lv.ll_list != NULL)
11901 /* List item. */
11902 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11903 else
11904 /* Dictionary item. */
11905 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11906 }
11907 }
11908
11909 clear_lval(&lv);
11910}
11911
Bram Moolenaar33570922005-01-25 22:26:29 +000011912static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011913
11914/*
11915 * Turn a dict into a list:
11916 * "what" == 0: list of keys
11917 * "what" == 1: list of values
11918 * "what" == 2: list of items
11919 */
11920 static void
11921dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011922 typval_T *argvars;
11923 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011924 int what;
11925{
Bram Moolenaar33570922005-01-25 22:26:29 +000011926 list_T *l2;
11927 dictitem_T *di;
11928 hashitem_T *hi;
11929 listitem_T *li;
11930 listitem_T *li2;
11931 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011932 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011933
11934 rettv->vval.v_number = 0;
11935 if (argvars[0].v_type != VAR_DICT)
11936 {
11937 EMSG(_(e_dictreq));
11938 return;
11939 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011940 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011941 return;
11942
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011943 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011944 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011945
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011946 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011947 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011949 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011950 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011951 --todo;
11952 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011953
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011954 li = listitem_alloc();
11955 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011956 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011957 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011958
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011959 if (what == 0)
11960 {
11961 /* keys() */
11962 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011963 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011964 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11965 }
11966 else if (what == 1)
11967 {
11968 /* values() */
11969 copy_tv(&di->di_tv, &li->li_tv);
11970 }
11971 else
11972 {
11973 /* items() */
11974 l2 = list_alloc();
11975 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011976 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011977 li->li_tv.vval.v_list = l2;
11978 if (l2 == NULL)
11979 break;
11980 ++l2->lv_refcount;
11981
11982 li2 = listitem_alloc();
11983 if (li2 == NULL)
11984 break;
11985 list_append(l2, li2);
11986 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011987 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011988 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11989
11990 li2 = listitem_alloc();
11991 if (li2 == NULL)
11992 break;
11993 list_append(l2, li2);
11994 copy_tv(&di->di_tv, &li2->li_tv);
11995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011996 }
11997 }
11998}
11999
12000/*
12001 * "items(dict)" function
12002 */
12003 static void
12004f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012005 typval_T *argvars;
12006 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012007{
12008 dict_list(argvars, rettv, 2);
12009}
12010
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012012 * "join()" function
12013 */
12014 static void
12015f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012016 typval_T *argvars;
12017 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012018{
12019 garray_T ga;
12020 char_u *sep;
12021
12022 rettv->vval.v_number = 0;
12023 if (argvars[0].v_type != VAR_LIST)
12024 {
12025 EMSG(_(e_listreq));
12026 return;
12027 }
12028 if (argvars[0].vval.v_list == NULL)
12029 return;
12030 if (argvars[1].v_type == VAR_UNKNOWN)
12031 sep = (char_u *)" ";
12032 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012033 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012034
12035 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012036
12037 if (sep != NULL)
12038 {
12039 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012040 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012041 ga_append(&ga, NUL);
12042 rettv->vval.v_string = (char_u *)ga.ga_data;
12043 }
12044 else
12045 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012046}
12047
12048/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012049 * "keys()" function
12050 */
12051 static void
12052f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012053 typval_T *argvars;
12054 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012055{
12056 dict_list(argvars, rettv, 0);
12057}
12058
12059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060 * "last_buffer_nr()" function.
12061 */
12062/*ARGSUSED*/
12063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012065 typval_T *argvars;
12066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067{
12068 int n = 0;
12069 buf_T *buf;
12070
12071 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12072 if (n < buf->b_fnum)
12073 n = buf->b_fnum;
12074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012075 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012076}
12077
12078/*
12079 * "len()" function
12080 */
12081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012083 typval_T *argvars;
12084 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012085{
12086 switch (argvars[0].v_type)
12087 {
12088 case VAR_STRING:
12089 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_number = (varnumber_T)STRLEN(
12091 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012092 break;
12093 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012095 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012096 case VAR_DICT:
12097 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12098 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012099 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012100 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012101 break;
12102 }
12103}
12104
Bram Moolenaar33570922005-01-25 22:26:29 +000012105static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012106
12107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012109 typval_T *argvars;
12110 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012111 int type;
12112{
12113#ifdef FEAT_LIBCALL
12114 char_u *string_in;
12115 char_u **string_result;
12116 int nr_result;
12117#endif
12118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012120 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012124
12125 if (check_restricted() || check_secure())
12126 return;
12127
12128#ifdef FEAT_LIBCALL
12129 /* The first two args must be strings, otherwise its meaningless */
12130 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12131 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012132 string_in = NULL;
12133 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012134 string_in = argvars[2].vval.v_string;
12135 if (type == VAR_NUMBER)
12136 string_result = NULL;
12137 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012139 if (mch_libcall(argvars[0].vval.v_string,
12140 argvars[1].vval.v_string,
12141 string_in,
12142 argvars[2].vval.v_number,
12143 string_result,
12144 &nr_result) == OK
12145 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012147 }
12148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149}
12150
12151/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012152 * "libcall()" function
12153 */
12154 static void
12155f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012156 typval_T *argvars;
12157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012158{
12159 libcall_common(argvars, rettv, VAR_STRING);
12160}
12161
12162/*
12163 * "libcallnr()" function
12164 */
12165 static void
12166f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012167 typval_T *argvars;
12168 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012169{
12170 libcall_common(argvars, rettv, VAR_NUMBER);
12171}
12172
12173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 * "line(string)" function
12175 */
12176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012177f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012178 typval_T *argvars;
12179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180{
12181 linenr_T lnum = 0;
12182 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012183 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012185 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 if (fp != NULL)
12187 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012188 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189}
12190
12191/*
12192 * "line2byte(lnum)" function
12193 */
12194/*ARGSUSED*/
12195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012197 typval_T *argvars;
12198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199{
12200#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202#else
12203 linenr_T lnum;
12204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012209 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12210 if (rettv->vval.v_number >= 0)
12211 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212#endif
12213}
12214
12215/*
12216 * "lispindent(lnum)" function
12217 */
12218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012220 typval_T *argvars;
12221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222{
12223#ifdef FEAT_LISP
12224 pos_T pos;
12225 linenr_T lnum;
12226
12227 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012228 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12230 {
12231 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012232 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 curwin->w_cursor = pos;
12234 }
12235 else
12236#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238}
12239
12240/*
12241 * "localtime()" function
12242 */
12243/*ARGSUSED*/
12244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245f_localtime(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250}
12251
Bram Moolenaar33570922005-01-25 22:26:29 +000012252static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253
12254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012256 typval_T *argvars;
12257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 int exact;
12259{
12260 char_u *keys;
12261 char_u *which;
12262 char_u buf[NUMBUFLEN];
12263 char_u *keys_buf = NULL;
12264 char_u *rhs;
12265 int mode;
12266 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012267 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268
12269 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 rettv->v_type = VAR_STRING;
12271 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 if (*keys == NUL)
12275 return;
12276
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012277 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012280 if (argvars[2].v_type != VAR_UNKNOWN)
12281 abbr = get_tv_number(&argvars[2]);
12282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 else
12284 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012285 if (which == NULL)
12286 return;
12287
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 mode = get_map_mode(&which, 0);
12289
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012290 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012291 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 vim_free(keys_buf);
12293 if (rhs != NULL)
12294 {
12295 ga_init(&ga);
12296 ga.ga_itemsize = 1;
12297 ga.ga_growsize = 40;
12298
12299 while (*rhs != NUL)
12300 ga_concat(&ga, str2special(&rhs, FALSE));
12301
12302 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012303 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 }
12305}
12306
12307/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012308 * "map()" function
12309 */
12310 static void
12311f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012312 typval_T *argvars;
12313 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012314{
12315 filter_map(argvars, rettv, TRUE);
12316}
12317
12318/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012319 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 */
12321 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012322f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012323 typval_T *argvars;
12324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012326 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327}
12328
12329/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 */
12332 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012333f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012334 typval_T *argvars;
12335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012337 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338}
12339
Bram Moolenaar33570922005-01-25 22:26:29 +000012340static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341
12342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012344 typval_T *argvars;
12345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 int type;
12347{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012348 char_u *str = NULL;
12349 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 char_u *pat;
12351 regmatch_T regmatch;
12352 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012353 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 char_u *save_cpo;
12355 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012356 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012357 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012358 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012359 list_T *l = NULL;
12360 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012361 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012362 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363
12364 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12365 save_cpo = p_cpo;
12366 p_cpo = (char_u *)"";
12367
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012368 rettv->vval.v_number = -1;
12369 if (type == 3)
12370 {
12371 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012372 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012373 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012374 }
12375 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377 rettv->v_type = VAR_STRING;
12378 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012381 if (argvars[0].v_type == VAR_LIST)
12382 {
12383 if ((l = argvars[0].vval.v_list) == NULL)
12384 goto theend;
12385 li = l->lv_first;
12386 }
12387 else
12388 expr = str = get_tv_string(&argvars[0]);
12389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12391 if (pat == NULL)
12392 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012393
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012394 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012396 int error = FALSE;
12397
12398 start = get_tv_number_chk(&argvars[2], &error);
12399 if (error)
12400 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012401 if (l != NULL)
12402 {
12403 li = list_find(l, start);
12404 if (li == NULL)
12405 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012406 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012407 }
12408 else
12409 {
12410 if (start < 0)
12411 start = 0;
12412 if (start > (long)STRLEN(str))
12413 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012414 /* When "count" argument is there ignore matches before "start",
12415 * otherwise skip part of the string. Differs when pattern is "^"
12416 * or "\<". */
12417 if (argvars[3].v_type != VAR_UNKNOWN)
12418 startcol = start;
12419 else
12420 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012421 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012422
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012423 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012424 nth = get_tv_number_chk(&argvars[3], &error);
12425 if (error)
12426 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 }
12428
12429 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12430 if (regmatch.regprog != NULL)
12431 {
12432 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012433
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012434 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012435 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012436 if (l != NULL)
12437 {
12438 if (li == NULL)
12439 {
12440 match = FALSE;
12441 break;
12442 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012443 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012444 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012445 if (str == NULL)
12446 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012447 }
12448
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012449 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012450
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012451 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012452 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012453 if (l == NULL && !match)
12454 break;
12455
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012456 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012457 if (l != NULL)
12458 {
12459 li = li->li_next;
12460 ++idx;
12461 }
12462 else
12463 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012464#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012465 startcol = (colnr_T)(regmatch.startp[0]
12466 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012467#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012468 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012469#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012470 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012471 }
12472
12473 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012475 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012476 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012477 int i;
12478
12479 /* return list with matched string and submatches */
12480 for (i = 0; i < NSUBEXP; ++i)
12481 {
12482 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012483 {
12484 if (list_append_string(rettv->vval.v_list,
12485 (char_u *)"", 0) == FAIL)
12486 break;
12487 }
12488 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012489 regmatch.startp[i],
12490 (int)(regmatch.endp[i] - regmatch.startp[i]))
12491 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012492 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012493 }
12494 }
12495 else if (type == 2)
12496 {
12497 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012498 if (l != NULL)
12499 copy_tv(&li->li_tv, rettv);
12500 else
12501 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012503 }
12504 else if (l != NULL)
12505 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 else
12507 {
12508 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 (varnumber_T)(regmatch.startp[0] - str);
12511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012514 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515 }
12516 }
12517 vim_free(regmatch.regprog);
12518 }
12519
12520theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012521 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 p_cpo = save_cpo;
12523}
12524
12525/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012526 * "match()" function
12527 */
12528 static void
12529f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012530 typval_T *argvars;
12531 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532{
12533 find_some_match(argvars, rettv, 1);
12534}
12535
12536/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012537 * "matchadd()" function
12538 */
12539 static void
12540f_matchadd(argvars, rettv)
12541 typval_T *argvars;
12542 typval_T *rettv;
12543{
12544#ifdef FEAT_SEARCH_EXTRA
12545 char_u buf[NUMBUFLEN];
12546 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12547 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12548 int prio = 10; /* default priority */
12549 int id = -1;
12550 int error = FALSE;
12551
12552 rettv->vval.v_number = -1;
12553
12554 if (grp == NULL || pat == NULL)
12555 return;
12556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012557 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012558 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012559 if (argvars[3].v_type != VAR_UNKNOWN)
12560 id = get_tv_number_chk(&argvars[3], &error);
12561 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012562 if (error == TRUE)
12563 return;
12564 if (id >= 1 && id <= 3)
12565 {
12566 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12567 return;
12568 }
12569
12570 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12571#endif
12572}
12573
12574/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012575 * "matcharg()" function
12576 */
12577 static void
12578f_matcharg(argvars, rettv)
12579 typval_T *argvars;
12580 typval_T *rettv;
12581{
12582 if (rettv_list_alloc(rettv) == OK)
12583 {
12584#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012585 int id = get_tv_number(&argvars[0]);
12586 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012587
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012588 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012589 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012590 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12591 {
12592 list_append_string(rettv->vval.v_list,
12593 syn_id2name(m->hlg_id), -1);
12594 list_append_string(rettv->vval.v_list, m->pattern, -1);
12595 }
12596 else
12597 {
12598 list_append_string(rettv->vval.v_list, NUL, -1);
12599 list_append_string(rettv->vval.v_list, NUL, -1);
12600 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012601 }
12602#endif
12603 }
12604}
12605
12606/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012607 * "matchdelete()" function
12608 */
12609 static void
12610f_matchdelete(argvars, rettv)
12611 typval_T *argvars;
12612 typval_T *rettv;
12613{
12614#ifdef FEAT_SEARCH_EXTRA
12615 rettv->vval.v_number = match_delete(curwin,
12616 (int)get_tv_number(&argvars[0]), TRUE);
12617#endif
12618}
12619
12620/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012621 * "matchend()" function
12622 */
12623 static void
12624f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012625 typval_T *argvars;
12626 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012627{
12628 find_some_match(argvars, rettv, 0);
12629}
12630
12631/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012632 * "matchlist()" function
12633 */
12634 static void
12635f_matchlist(argvars, rettv)
12636 typval_T *argvars;
12637 typval_T *rettv;
12638{
12639 find_some_match(argvars, rettv, 3);
12640}
12641
12642/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012643 * "matchstr()" function
12644 */
12645 static void
12646f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012647 typval_T *argvars;
12648 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012649{
12650 find_some_match(argvars, rettv, 2);
12651}
12652
Bram Moolenaar33570922005-01-25 22:26:29 +000012653static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012654
12655 static void
12656max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012657 typval_T *argvars;
12658 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012659 int domax;
12660{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012661 long n = 0;
12662 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012663 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012664
12665 if (argvars[0].v_type == VAR_LIST)
12666 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012667 list_T *l;
12668 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012669
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012670 l = argvars[0].vval.v_list;
12671 if (l != NULL)
12672 {
12673 li = l->lv_first;
12674 if (li != NULL)
12675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012676 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012677 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012678 {
12679 li = li->li_next;
12680 if (li == NULL)
12681 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012682 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012683 if (domax ? i > n : i < n)
12684 n = i;
12685 }
12686 }
12687 }
12688 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012689 else if (argvars[0].v_type == VAR_DICT)
12690 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012691 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012692 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012694 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012695
12696 d = argvars[0].vval.v_dict;
12697 if (d != NULL)
12698 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012699 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012700 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012701 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012702 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012703 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012704 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012705 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012706 if (first)
12707 {
12708 n = i;
12709 first = FALSE;
12710 }
12711 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012712 n = i;
12713 }
12714 }
12715 }
12716 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012717 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012718 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012720}
12721
12722/*
12723 * "max()" function
12724 */
12725 static void
12726f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012727 typval_T *argvars;
12728 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012729{
12730 max_min(argvars, rettv, TRUE);
12731}
12732
12733/*
12734 * "min()" function
12735 */
12736 static void
12737f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012738 typval_T *argvars;
12739 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012740{
12741 max_min(argvars, rettv, FALSE);
12742}
12743
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012744static int mkdir_recurse __ARGS((char_u *dir, int prot));
12745
12746/*
12747 * Create the directory in which "dir" is located, and higher levels when
12748 * needed.
12749 */
12750 static int
12751mkdir_recurse(dir, prot)
12752 char_u *dir;
12753 int prot;
12754{
12755 char_u *p;
12756 char_u *updir;
12757 int r = FAIL;
12758
12759 /* Get end of directory name in "dir".
12760 * We're done when it's "/" or "c:/". */
12761 p = gettail_sep(dir);
12762 if (p <= get_past_head(dir))
12763 return OK;
12764
12765 /* If the directory exists we're done. Otherwise: create it.*/
12766 updir = vim_strnsave(dir, (int)(p - dir));
12767 if (updir == NULL)
12768 return FAIL;
12769 if (mch_isdir(updir))
12770 r = OK;
12771 else if (mkdir_recurse(updir, prot) == OK)
12772 r = vim_mkdir_emsg(updir, prot);
12773 vim_free(updir);
12774 return r;
12775}
12776
12777#ifdef vim_mkdir
12778/*
12779 * "mkdir()" function
12780 */
12781 static void
12782f_mkdir(argvars, rettv)
12783 typval_T *argvars;
12784 typval_T *rettv;
12785{
12786 char_u *dir;
12787 char_u buf[NUMBUFLEN];
12788 int prot = 0755;
12789
12790 rettv->vval.v_number = FAIL;
12791 if (check_restricted() || check_secure())
12792 return;
12793
12794 dir = get_tv_string_buf(&argvars[0], buf);
12795 if (argvars[1].v_type != VAR_UNKNOWN)
12796 {
12797 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012798 prot = get_tv_number_chk(&argvars[2], NULL);
12799 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012800 mkdir_recurse(dir, prot);
12801 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012802 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012803}
12804#endif
12805
Bram Moolenaar0d660222005-01-07 21:51:51 +000012806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 * "mode()" function
12808 */
12809/*ARGSUSED*/
12810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012812 typval_T *argvars;
12813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814{
12815 char_u buf[2];
12816
12817#ifdef FEAT_VISUAL
12818 if (VIsual_active)
12819 {
12820 if (VIsual_select)
12821 buf[0] = VIsual_mode + 's' - 'v';
12822 else
12823 buf[0] = VIsual_mode;
12824 }
12825 else
12826#endif
12827 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12828 buf[0] = 'r';
12829 else if (State & INSERT)
12830 {
12831 if (State & REPLACE_FLAG)
12832 buf[0] = 'R';
12833 else
12834 buf[0] = 'i';
12835 }
12836 else if (State & CMDLINE)
12837 buf[0] = 'c';
12838 else
12839 buf[0] = 'n';
12840
12841 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->vval.v_string = vim_strsave(buf);
12843 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844}
12845
12846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012847 * "nextnonblank()" function
12848 */
12849 static void
12850f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012851 typval_T *argvars;
12852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853{
12854 linenr_T lnum;
12855
12856 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012858 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012859 {
12860 lnum = 0;
12861 break;
12862 }
12863 if (*skipwhite(ml_get(lnum)) != NUL)
12864 break;
12865 }
12866 rettv->vval.v_number = lnum;
12867}
12868
12869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 * "nr2char()" function
12871 */
12872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012874 typval_T *argvars;
12875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876{
12877 char_u buf[NUMBUFLEN];
12878
12879#ifdef FEAT_MBYTE
12880 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 else
12883#endif
12884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 buf[1] = NUL;
12887 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->v_type = VAR_STRING;
12889 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012890}
12891
12892/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012893 * "pathshorten()" function
12894 */
12895 static void
12896f_pathshorten(argvars, rettv)
12897 typval_T *argvars;
12898 typval_T *rettv;
12899{
12900 char_u *p;
12901
12902 rettv->v_type = VAR_STRING;
12903 p = get_tv_string_chk(&argvars[0]);
12904 if (p == NULL)
12905 rettv->vval.v_string = NULL;
12906 else
12907 {
12908 p = vim_strsave(p);
12909 rettv->vval.v_string = p;
12910 if (p != NULL)
12911 shorten_dir(p);
12912 }
12913}
12914
12915/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012916 * "prevnonblank()" function
12917 */
12918 static void
12919f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012920 typval_T *argvars;
12921 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012922{
12923 linenr_T lnum;
12924
12925 lnum = get_tv_lnum(argvars);
12926 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12927 lnum = 0;
12928 else
12929 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12930 --lnum;
12931 rettv->vval.v_number = lnum;
12932}
12933
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012934#ifdef HAVE_STDARG_H
12935/* This dummy va_list is here because:
12936 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12937 * - locally in the function results in a "used before set" warning
12938 * - using va_start() to initialize it gives "function with fixed args" error */
12939static va_list ap;
12940#endif
12941
Bram Moolenaar8c711452005-01-14 21:53:12 +000012942/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012943 * "printf()" function
12944 */
12945 static void
12946f_printf(argvars, rettv)
12947 typval_T *argvars;
12948 typval_T *rettv;
12949{
12950 rettv->v_type = VAR_STRING;
12951 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012952#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012953 {
12954 char_u buf[NUMBUFLEN];
12955 int len;
12956 char_u *s;
12957 int saved_did_emsg = did_emsg;
12958 char *fmt;
12959
12960 /* Get the required length, allocate the buffer and do it for real. */
12961 did_emsg = FALSE;
12962 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012963 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012964 if (!did_emsg)
12965 {
12966 s = alloc(len + 1);
12967 if (s != NULL)
12968 {
12969 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012970 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012971 }
12972 }
12973 did_emsg |= saved_did_emsg;
12974 }
12975#endif
12976}
12977
12978/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012979 * "pumvisible()" function
12980 */
12981/*ARGSUSED*/
12982 static void
12983f_pumvisible(argvars, rettv)
12984 typval_T *argvars;
12985 typval_T *rettv;
12986{
12987 rettv->vval.v_number = 0;
12988#ifdef FEAT_INS_EXPAND
12989 if (pum_visible())
12990 rettv->vval.v_number = 1;
12991#endif
12992}
12993
12994/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012995 * "range()" function
12996 */
12997 static void
12998f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012999 typval_T *argvars;
13000 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013001{
13002 long start;
13003 long end;
13004 long stride = 1;
13005 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013006 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013009 if (argvars[1].v_type == VAR_UNKNOWN)
13010 {
13011 end = start - 1;
13012 start = 0;
13013 }
13014 else
13015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013016 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013017 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013019 }
13020
13021 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013022 if (error)
13023 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013024 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013025 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013026 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013027 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013028 else
13029 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013030 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013031 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013032 if (list_append_number(rettv->vval.v_list,
13033 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013034 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013035 }
13036}
13037
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013038/*
13039 * "readfile()" function
13040 */
13041 static void
13042f_readfile(argvars, rettv)
13043 typval_T *argvars;
13044 typval_T *rettv;
13045{
13046 int binary = FALSE;
13047 char_u *fname;
13048 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013049 listitem_T *li;
13050#define FREAD_SIZE 200 /* optimized for text lines */
13051 char_u buf[FREAD_SIZE];
13052 int readlen; /* size of last fread() */
13053 int buflen; /* nr of valid chars in buf[] */
13054 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13055 int tolist; /* first byte in buf[] still to be put in list */
13056 int chop; /* how many CR to chop off */
13057 char_u *prev = NULL; /* previously read bytes, if any */
13058 int prevlen = 0; /* length of "prev" if not NULL */
13059 char_u *s;
13060 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013061 long maxline = MAXLNUM;
13062 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013063
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013064 if (argvars[1].v_type != VAR_UNKNOWN)
13065 {
13066 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13067 binary = TRUE;
13068 if (argvars[2].v_type != VAR_UNKNOWN)
13069 maxline = get_tv_number(&argvars[2]);
13070 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013071
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013072 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013073 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013074
13075 /* Always open the file in binary mode, library functions have a mind of
13076 * their own about CR-LF conversion. */
13077 fname = get_tv_string(&argvars[0]);
13078 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13079 {
13080 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13081 return;
13082 }
13083
13084 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013085 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013086 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013087 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013088 buflen = filtd + readlen;
13089 tolist = 0;
13090 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13091 {
13092 if (buf[filtd] == '\n' || readlen <= 0)
13093 {
13094 /* Only when in binary mode add an empty list item when the
13095 * last line ends in a '\n'. */
13096 if (!binary && readlen == 0 && filtd == 0)
13097 break;
13098
13099 /* Found end-of-line or end-of-file: add a text line to the
13100 * list. */
13101 chop = 0;
13102 if (!binary)
13103 while (filtd - chop - 1 >= tolist
13104 && buf[filtd - chop - 1] == '\r')
13105 ++chop;
13106 len = filtd - tolist - chop;
13107 if (prev == NULL)
13108 s = vim_strnsave(buf + tolist, len);
13109 else
13110 {
13111 s = alloc((unsigned)(prevlen + len + 1));
13112 if (s != NULL)
13113 {
13114 mch_memmove(s, prev, prevlen);
13115 vim_free(prev);
13116 prev = NULL;
13117 mch_memmove(s + prevlen, buf + tolist, len);
13118 s[prevlen + len] = NUL;
13119 }
13120 }
13121 tolist = filtd + 1;
13122
13123 li = listitem_alloc();
13124 if (li == NULL)
13125 {
13126 vim_free(s);
13127 break;
13128 }
13129 li->li_tv.v_type = VAR_STRING;
13130 li->li_tv.v_lock = 0;
13131 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013132 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013133
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013134 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013135 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013136 if (readlen <= 0)
13137 break;
13138 }
13139 else if (buf[filtd] == NUL)
13140 buf[filtd] = '\n';
13141 }
13142 if (readlen <= 0)
13143 break;
13144
13145 if (tolist == 0)
13146 {
13147 /* "buf" is full, need to move text to an allocated buffer */
13148 if (prev == NULL)
13149 {
13150 prev = vim_strnsave(buf, buflen);
13151 prevlen = buflen;
13152 }
13153 else
13154 {
13155 s = alloc((unsigned)(prevlen + buflen));
13156 if (s != NULL)
13157 {
13158 mch_memmove(s, prev, prevlen);
13159 mch_memmove(s + prevlen, buf, buflen);
13160 vim_free(prev);
13161 prev = s;
13162 prevlen += buflen;
13163 }
13164 }
13165 filtd = 0;
13166 }
13167 else
13168 {
13169 mch_memmove(buf, buf + tolist, buflen - tolist);
13170 filtd -= tolist;
13171 }
13172 }
13173
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013174 /*
13175 * For a negative line count use only the lines at the end of the file,
13176 * free the rest.
13177 */
13178 if (maxline < 0)
13179 while (cnt > -maxline)
13180 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013181 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013182 --cnt;
13183 }
13184
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013185 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013186 fclose(fd);
13187}
13188
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013189#if defined(FEAT_RELTIME)
13190static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13191
13192/*
13193 * Convert a List to proftime_T.
13194 * Return FAIL when there is something wrong.
13195 */
13196 static int
13197list2proftime(arg, tm)
13198 typval_T *arg;
13199 proftime_T *tm;
13200{
13201 long n1, n2;
13202 int error = FALSE;
13203
13204 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13205 || arg->vval.v_list->lv_len != 2)
13206 return FAIL;
13207 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13208 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13209# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013210 tm->HighPart = n1;
13211 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013212# else
13213 tm->tv_sec = n1;
13214 tm->tv_usec = n2;
13215# endif
13216 return error ? FAIL : OK;
13217}
13218#endif /* FEAT_RELTIME */
13219
13220/*
13221 * "reltime()" function
13222 */
13223 static void
13224f_reltime(argvars, rettv)
13225 typval_T *argvars;
13226 typval_T *rettv;
13227{
13228#ifdef FEAT_RELTIME
13229 proftime_T res;
13230 proftime_T start;
13231
13232 if (argvars[0].v_type == VAR_UNKNOWN)
13233 {
13234 /* No arguments: get current time. */
13235 profile_start(&res);
13236 }
13237 else if (argvars[1].v_type == VAR_UNKNOWN)
13238 {
13239 if (list2proftime(&argvars[0], &res) == FAIL)
13240 return;
13241 profile_end(&res);
13242 }
13243 else
13244 {
13245 /* Two arguments: compute the difference. */
13246 if (list2proftime(&argvars[0], &start) == FAIL
13247 || list2proftime(&argvars[1], &res) == FAIL)
13248 return;
13249 profile_sub(&res, &start);
13250 }
13251
13252 if (rettv_list_alloc(rettv) == OK)
13253 {
13254 long n1, n2;
13255
13256# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013257 n1 = res.HighPart;
13258 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013259# else
13260 n1 = res.tv_sec;
13261 n2 = res.tv_usec;
13262# endif
13263 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13264 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13265 }
13266#endif
13267}
13268
13269/*
13270 * "reltimestr()" function
13271 */
13272 static void
13273f_reltimestr(argvars, rettv)
13274 typval_T *argvars;
13275 typval_T *rettv;
13276{
13277#ifdef FEAT_RELTIME
13278 proftime_T tm;
13279#endif
13280
13281 rettv->v_type = VAR_STRING;
13282 rettv->vval.v_string = NULL;
13283#ifdef FEAT_RELTIME
13284 if (list2proftime(&argvars[0], &tm) == OK)
13285 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13286#endif
13287}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013288
Bram Moolenaar0d660222005-01-07 21:51:51 +000013289#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13290static void make_connection __ARGS((void));
13291static int check_connection __ARGS((void));
13292
13293 static void
13294make_connection()
13295{
13296 if (X_DISPLAY == NULL
13297# ifdef FEAT_GUI
13298 && !gui.in_use
13299# endif
13300 )
13301 {
13302 x_force_connect = TRUE;
13303 setup_term_clip();
13304 x_force_connect = FALSE;
13305 }
13306}
13307
13308 static int
13309check_connection()
13310{
13311 make_connection();
13312 if (X_DISPLAY == NULL)
13313 {
13314 EMSG(_("E240: No connection to Vim server"));
13315 return FAIL;
13316 }
13317 return OK;
13318}
13319#endif
13320
13321#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013322static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013323
13324 static void
13325remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013326 typval_T *argvars;
13327 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013328 int expr;
13329{
13330 char_u *server_name;
13331 char_u *keys;
13332 char_u *r = NULL;
13333 char_u buf[NUMBUFLEN];
13334# ifdef WIN32
13335 HWND w;
13336# else
13337 Window w;
13338# endif
13339
13340 if (check_restricted() || check_secure())
13341 return;
13342
13343# ifdef FEAT_X11
13344 if (check_connection() == FAIL)
13345 return;
13346# endif
13347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013348 server_name = get_tv_string_chk(&argvars[0]);
13349 if (server_name == NULL)
13350 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013351 keys = get_tv_string_buf(&argvars[1], buf);
13352# ifdef WIN32
13353 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13354# else
13355 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13356 < 0)
13357# endif
13358 {
13359 if (r != NULL)
13360 EMSG(r); /* sending worked but evaluation failed */
13361 else
13362 EMSG2(_("E241: Unable to send to %s"), server_name);
13363 return;
13364 }
13365
13366 rettv->vval.v_string = r;
13367
13368 if (argvars[2].v_type != VAR_UNKNOWN)
13369 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013370 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013371 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013372 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013373
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013374 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013375 v.di_tv.v_type = VAR_STRING;
13376 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013377 idvar = get_tv_string_chk(&argvars[2]);
13378 if (idvar != NULL)
13379 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013380 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013381 }
13382}
13383#endif
13384
13385/*
13386 * "remote_expr()" function
13387 */
13388/*ARGSUSED*/
13389 static void
13390f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013391 typval_T *argvars;
13392 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013393{
13394 rettv->v_type = VAR_STRING;
13395 rettv->vval.v_string = NULL;
13396#ifdef FEAT_CLIENTSERVER
13397 remote_common(argvars, rettv, TRUE);
13398#endif
13399}
13400
13401/*
13402 * "remote_foreground()" function
13403 */
13404/*ARGSUSED*/
13405 static void
13406f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013407 typval_T *argvars;
13408 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013409{
13410 rettv->vval.v_number = 0;
13411#ifdef FEAT_CLIENTSERVER
13412# ifdef WIN32
13413 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 {
13415 char_u *server_name = get_tv_string_chk(&argvars[0]);
13416
13417 if (server_name != NULL)
13418 serverForeground(server_name);
13419 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013420# else
13421 /* Send a foreground() expression to the server. */
13422 argvars[1].v_type = VAR_STRING;
13423 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13424 argvars[2].v_type = VAR_UNKNOWN;
13425 remote_common(argvars, rettv, TRUE);
13426 vim_free(argvars[1].vval.v_string);
13427# endif
13428#endif
13429}
13430
13431/*ARGSUSED*/
13432 static void
13433f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013434 typval_T *argvars;
13435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436{
13437#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013438 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013439 char_u *s = NULL;
13440# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013441 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013442# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013443 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013444
13445 if (check_restricted() || check_secure())
13446 {
13447 rettv->vval.v_number = -1;
13448 return;
13449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013450 serverid = get_tv_string_chk(&argvars[0]);
13451 if (serverid == NULL)
13452 {
13453 rettv->vval.v_number = -1;
13454 return; /* type error; errmsg already given */
13455 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013456# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013457 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013458 if (n == 0)
13459 rettv->vval.v_number = -1;
13460 else
13461 {
13462 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13463 rettv->vval.v_number = (s != NULL);
13464 }
13465# else
13466 rettv->vval.v_number = 0;
13467 if (check_connection() == FAIL)
13468 return;
13469
13470 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013472# endif
13473
13474 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013476 char_u *retvar;
13477
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 v.di_tv.v_type = VAR_STRING;
13479 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013480 retvar = get_tv_string_chk(&argvars[1]);
13481 if (retvar != NULL)
13482 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013483 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013484 }
13485#else
13486 rettv->vval.v_number = -1;
13487#endif
13488}
13489
13490/*ARGSUSED*/
13491 static void
13492f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013493 typval_T *argvars;
13494 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013495{
13496 char_u *r = NULL;
13497
13498#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013499 char_u *serverid = get_tv_string_chk(&argvars[0]);
13500
13501 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013502 {
13503# ifdef WIN32
13504 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013505 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013506
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013507 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013508 if (n != 0)
13509 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13510 if (r == NULL)
13511# else
13512 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013514# endif
13515 EMSG(_("E277: Unable to read a server reply"));
13516 }
13517#endif
13518 rettv->v_type = VAR_STRING;
13519 rettv->vval.v_string = r;
13520}
13521
13522/*
13523 * "remote_send()" function
13524 */
13525/*ARGSUSED*/
13526 static void
13527f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013528 typval_T *argvars;
13529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013530{
13531 rettv->v_type = VAR_STRING;
13532 rettv->vval.v_string = NULL;
13533#ifdef FEAT_CLIENTSERVER
13534 remote_common(argvars, rettv, FALSE);
13535#endif
13536}
13537
13538/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013539 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013540 */
13541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013543 typval_T *argvars;
13544 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013545{
Bram Moolenaar33570922005-01-25 22:26:29 +000013546 list_T *l;
13547 listitem_T *item, *item2;
13548 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013549 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013550 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013551 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013552 dict_T *d;
13553 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013554
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013555 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013556 if (argvars[0].v_type == VAR_DICT)
13557 {
13558 if (argvars[2].v_type != VAR_UNKNOWN)
13559 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013560 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013561 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013563 key = get_tv_string_chk(&argvars[1]);
13564 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566 di = dict_find(d, key, -1);
13567 if (di == NULL)
13568 EMSG2(_(e_dictkey), key);
13569 else
13570 {
13571 *rettv = di->di_tv;
13572 init_tv(&di->di_tv);
13573 dictitem_remove(d, di);
13574 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013576 }
13577 }
13578 else if (argvars[0].v_type != VAR_LIST)
13579 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013580 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013581 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 int error = FALSE;
13584
13585 idx = get_tv_number_chk(&argvars[1], &error);
13586 if (error)
13587 ; /* type error: do nothing, errmsg already given */
13588 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589 EMSGN(_(e_listidx), idx);
13590 else
13591 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013592 if (argvars[2].v_type == VAR_UNKNOWN)
13593 {
13594 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013595 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013596 *rettv = item->li_tv;
13597 vim_free(item);
13598 }
13599 else
13600 {
13601 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013602 end = get_tv_number_chk(&argvars[2], &error);
13603 if (error)
13604 ; /* type error: do nothing */
13605 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013606 EMSGN(_(e_listidx), end);
13607 else
13608 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013609 int cnt = 0;
13610
13611 for (li = item; li != NULL; li = li->li_next)
13612 {
13613 ++cnt;
13614 if (li == item2)
13615 break;
13616 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013617 if (li == NULL) /* didn't find "item2" after "item" */
13618 EMSG(_(e_invrange));
13619 else
13620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013621 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013622 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013623 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013624 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013625 l->lv_first = item;
13626 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013627 item->li_prev = NULL;
13628 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013629 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013630 }
13631 }
13632 }
13633 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013634 }
13635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636}
13637
13638/*
13639 * "rename({from}, {to})" function
13640 */
13641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 typval_T *argvars;
13644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645{
13646 char_u buf[NUMBUFLEN];
13647
13648 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13652 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653}
13654
13655/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013656 * "repeat()" function
13657 */
13658/*ARGSUSED*/
13659 static void
13660f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013661 typval_T *argvars;
13662 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013663{
13664 char_u *p;
13665 int n;
13666 int slen;
13667 int len;
13668 char_u *r;
13669 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013670
13671 n = get_tv_number(&argvars[1]);
13672 if (argvars[0].v_type == VAR_LIST)
13673 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013674 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013675 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013676 if (list_extend(rettv->vval.v_list,
13677 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013678 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013679 }
13680 else
13681 {
13682 p = get_tv_string(&argvars[0]);
13683 rettv->v_type = VAR_STRING;
13684 rettv->vval.v_string = NULL;
13685
13686 slen = (int)STRLEN(p);
13687 len = slen * n;
13688 if (len <= 0)
13689 return;
13690
13691 r = alloc(len + 1);
13692 if (r != NULL)
13693 {
13694 for (i = 0; i < n; i++)
13695 mch_memmove(r + i * slen, p, (size_t)slen);
13696 r[len] = NUL;
13697 }
13698
13699 rettv->vval.v_string = r;
13700 }
13701}
13702
13703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 * "resolve()" function
13705 */
13706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013707f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013708 typval_T *argvars;
13709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710{
13711 char_u *p;
13712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714#ifdef FEAT_SHORTCUT
13715 {
13716 char_u *v = NULL;
13717
13718 v = mch_resolve_shortcut(p);
13719 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 }
13724#else
13725# ifdef HAVE_READLINK
13726 {
13727 char_u buf[MAXPATHL + 1];
13728 char_u *cpy;
13729 int len;
13730 char_u *remain = NULL;
13731 char_u *q;
13732 int is_relative_to_current = FALSE;
13733 int has_trailing_pathsep = FALSE;
13734 int limit = 100;
13735
13736 p = vim_strsave(p);
13737
13738 if (p[0] == '.' && (vim_ispathsep(p[1])
13739 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13740 is_relative_to_current = TRUE;
13741
13742 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013743 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 has_trailing_pathsep = TRUE;
13745
13746 q = getnextcomp(p);
13747 if (*q != NUL)
13748 {
13749 /* Separate the first path component in "p", and keep the
13750 * remainder (beginning with the path separator). */
13751 remain = vim_strsave(q - 1);
13752 q[-1] = NUL;
13753 }
13754
13755 for (;;)
13756 {
13757 for (;;)
13758 {
13759 len = readlink((char *)p, (char *)buf, MAXPATHL);
13760 if (len <= 0)
13761 break;
13762 buf[len] = NUL;
13763
13764 if (limit-- == 0)
13765 {
13766 vim_free(p);
13767 vim_free(remain);
13768 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013769 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 goto fail;
13771 }
13772
13773 /* Ensure that the result will have a trailing path separator
13774 * if the argument has one. */
13775 if (remain == NULL && has_trailing_pathsep)
13776 add_pathsep(buf);
13777
13778 /* Separate the first path component in the link value and
13779 * concatenate the remainders. */
13780 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13781 if (*q != NUL)
13782 {
13783 if (remain == NULL)
13784 remain = vim_strsave(q - 1);
13785 else
13786 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013787 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 if (cpy != NULL)
13789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790 vim_free(remain);
13791 remain = cpy;
13792 }
13793 }
13794 q[-1] = NUL;
13795 }
13796
13797 q = gettail(p);
13798 if (q > p && *q == NUL)
13799 {
13800 /* Ignore trailing path separator. */
13801 q[-1] = NUL;
13802 q = gettail(p);
13803 }
13804 if (q > p && !mch_isFullName(buf))
13805 {
13806 /* symlink is relative to directory of argument */
13807 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13808 if (cpy != NULL)
13809 {
13810 STRCPY(cpy, p);
13811 STRCPY(gettail(cpy), buf);
13812 vim_free(p);
13813 p = cpy;
13814 }
13815 }
13816 else
13817 {
13818 vim_free(p);
13819 p = vim_strsave(buf);
13820 }
13821 }
13822
13823 if (remain == NULL)
13824 break;
13825
13826 /* Append the first path component of "remain" to "p". */
13827 q = getnextcomp(remain + 1);
13828 len = q - remain - (*q != NUL);
13829 cpy = vim_strnsave(p, STRLEN(p) + len);
13830 if (cpy != NULL)
13831 {
13832 STRNCAT(cpy, remain, len);
13833 vim_free(p);
13834 p = cpy;
13835 }
13836 /* Shorten "remain". */
13837 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013838 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 else
13840 {
13841 vim_free(remain);
13842 remain = NULL;
13843 }
13844 }
13845
13846 /* If the result is a relative path name, make it explicitly relative to
13847 * the current directory if and only if the argument had this form. */
13848 if (!vim_ispathsep(*p))
13849 {
13850 if (is_relative_to_current
13851 && *p != NUL
13852 && !(p[0] == '.'
13853 && (p[1] == NUL
13854 || vim_ispathsep(p[1])
13855 || (p[1] == '.'
13856 && (p[2] == NUL
13857 || vim_ispathsep(p[2]))))))
13858 {
13859 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013860 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 if (cpy != NULL)
13862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 vim_free(p);
13864 p = cpy;
13865 }
13866 }
13867 else if (!is_relative_to_current)
13868 {
13869 /* Strip leading "./". */
13870 q = p;
13871 while (q[0] == '.' && vim_ispathsep(q[1]))
13872 q += 2;
13873 if (q > p)
13874 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13875 }
13876 }
13877
13878 /* Ensure that the result will have no trailing path separator
13879 * if the argument had none. But keep "/" or "//". */
13880 if (!has_trailing_pathsep)
13881 {
13882 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013883 if (after_pathsep(p, q))
13884 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 }
13886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888 }
13889# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891# endif
13892#endif
13893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895
13896#ifdef HAVE_READLINK
13897fail:
13898#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013899 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900}
13901
13902/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013903 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 */
13905 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013906f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013907 typval_T *argvars;
13908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909{
Bram Moolenaar33570922005-01-25 22:26:29 +000013910 list_T *l;
13911 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912
Bram Moolenaar0d660222005-01-07 21:51:51 +000013913 rettv->vval.v_number = 0;
13914 if (argvars[0].v_type != VAR_LIST)
13915 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013916 else if ((l = argvars[0].vval.v_list) != NULL
13917 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013918 {
13919 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013920 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013921 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013922 while (li != NULL)
13923 {
13924 ni = li->li_prev;
13925 list_append(l, li);
13926 li = ni;
13927 }
13928 rettv->vval.v_list = l;
13929 rettv->v_type = VAR_LIST;
13930 ++l->lv_refcount;
13931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932}
13933
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013934#define SP_NOMOVE 0x01 /* don't move cursor */
13935#define SP_REPEAT 0x02 /* repeat to find outer pair */
13936#define SP_RETCOUNT 0x04 /* return matchcount */
13937#define SP_SETPCMARK 0x08 /* set previous context mark */
13938#define SP_START 0x10 /* accept match at start position */
13939#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13940#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013941
Bram Moolenaar33570922005-01-25 22:26:29 +000013942static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013943
13944/*
13945 * Get flags for a search function.
13946 * Possibly sets "p_ws".
13947 * Returns BACKWARD, FORWARD or zero (for an error).
13948 */
13949 static int
13950get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013951 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013952 int *flagsp;
13953{
13954 int dir = FORWARD;
13955 char_u *flags;
13956 char_u nbuf[NUMBUFLEN];
13957 int mask;
13958
13959 if (varp->v_type != VAR_UNKNOWN)
13960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013961 flags = get_tv_string_buf_chk(varp, nbuf);
13962 if (flags == NULL)
13963 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013964 while (*flags != NUL)
13965 {
13966 switch (*flags)
13967 {
13968 case 'b': dir = BACKWARD; break;
13969 case 'w': p_ws = TRUE; break;
13970 case 'W': p_ws = FALSE; break;
13971 default: mask = 0;
13972 if (flagsp != NULL)
13973 switch (*flags)
13974 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013975 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013976 case 'e': mask = SP_END; break;
13977 case 'm': mask = SP_RETCOUNT; break;
13978 case 'n': mask = SP_NOMOVE; break;
13979 case 'p': mask = SP_SUBPAT; break;
13980 case 'r': mask = SP_REPEAT; break;
13981 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013982 }
13983 if (mask == 0)
13984 {
13985 EMSG2(_(e_invarg2), flags);
13986 dir = 0;
13987 }
13988 else
13989 *flagsp |= mask;
13990 }
13991 if (dir == 0)
13992 break;
13993 ++flags;
13994 }
13995 }
13996 return dir;
13997}
13998
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014000 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014002 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014003search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014004 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014005 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014006 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014008 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 char_u *pat;
14010 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014011 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 int save_p_ws = p_ws;
14013 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014014 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014015 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014016 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014017 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014020 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014021 if (dir == 0)
14022 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014023 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014024 if (flags & SP_START)
14025 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014026 if (flags & SP_END)
14027 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014028
14029 /* Optional extra argument: line number to stop searching. */
14030 if (argvars[1].v_type != VAR_UNKNOWN
14031 && argvars[2].v_type != VAR_UNKNOWN)
14032 {
14033 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14034 if (lnum_stop < 0)
14035 goto theend;
14036 }
14037
Bram Moolenaar231334e2005-07-25 20:46:57 +000014038 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014039 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014040 * Check to make sure only those flags are set.
14041 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14042 * flags cannot be set. Check for that condition also.
14043 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014044 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014045 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014048 goto theend;
14049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014051 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014052 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
14053 options, RE_SEARCH, (linenr_T)lnum_stop);
14054 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014056 if (flags & SP_SUBPAT)
14057 retval = subpatnum;
14058 else
14059 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014060 if (flags & SP_SETPCMARK)
14061 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014063 if (match_pos != NULL)
14064 {
14065 /* Store the match cursor position */
14066 match_pos->lnum = pos.lnum;
14067 match_pos->col = pos.col + 1;
14068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 /* "/$" will put the cursor after the end of the line, may need to
14070 * correct that here */
14071 check_cursor();
14072 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014073
14074 /* If 'n' flag is used: restore cursor position. */
14075 if (flags & SP_NOMOVE)
14076 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014077 else
14078 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014079theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014081
14082 return retval;
14083}
14084
14085/*
14086 * "search()" function
14087 */
14088 static void
14089f_search(argvars, rettv)
14090 typval_T *argvars;
14091 typval_T *rettv;
14092{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014093 int flags = 0;
14094
14095 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096}
14097
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014099 * "searchdecl()" function
14100 */
14101 static void
14102f_searchdecl(argvars, rettv)
14103 typval_T *argvars;
14104 typval_T *rettv;
14105{
14106 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014107 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014108 int error = FALSE;
14109 char_u *name;
14110
14111 rettv->vval.v_number = 1; /* default: FAIL */
14112
14113 name = get_tv_string_chk(&argvars[0]);
14114 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014115 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014116 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014117 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14118 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14119 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014120 if (!error && name != NULL)
14121 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014122 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014123}
14124
14125/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014126 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014128 static int
14129searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014131 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132{
14133 char_u *spat, *mpat, *epat;
14134 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 int dir;
14137 int flags = 0;
14138 char_u nbuf1[NUMBUFLEN];
14139 char_u nbuf2[NUMBUFLEN];
14140 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014141 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014142 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 spat = get_tv_string_chk(&argvars[0]);
14146 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14147 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14148 if (spat == NULL || mpat == NULL || epat == NULL)
14149 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 /* Handle the optional fourth argument: flags */
14152 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014153 if (dir == 0)
14154 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014155
14156 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014157 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14158 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014159 if ((flags & (SP_END | SP_SUBPAT)) != 0
14160 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014161 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014162 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014163 goto theend;
14164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014166 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014167 if (argvars[3].v_type == VAR_UNKNOWN
14168 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 skip = (char_u *)"";
14170 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014173 if (argvars[5].v_type != VAR_UNKNOWN)
14174 {
14175 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14176 if (lnum_stop < 0)
14177 goto theend;
14178 }
14179 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 if (skip == NULL)
14181 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014183 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14184 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014185
14186theend:
14187 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014188
14189 return retval;
14190}
14191
14192/*
14193 * "searchpair()" function
14194 */
14195 static void
14196f_searchpair(argvars, rettv)
14197 typval_T *argvars;
14198 typval_T *rettv;
14199{
14200 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14201}
14202
14203/*
14204 * "searchpairpos()" function
14205 */
14206 static void
14207f_searchpairpos(argvars, rettv)
14208 typval_T *argvars;
14209 typval_T *rettv;
14210{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014211 pos_T match_pos;
14212 int lnum = 0;
14213 int col = 0;
14214
14215 rettv->vval.v_number = 0;
14216
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014217 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014218 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014219
14220 if (searchpair_cmn(argvars, &match_pos) > 0)
14221 {
14222 lnum = match_pos.lnum;
14223 col = match_pos.col;
14224 }
14225
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014226 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14227 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014228}
14229
14230/*
14231 * Search for a start/middle/end thing.
14232 * Used by searchpair(), see its documentation for the details.
14233 * Returns 0 or -1 for no match,
14234 */
14235 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014236do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014237 char_u *spat; /* start pattern */
14238 char_u *mpat; /* middle pattern */
14239 char_u *epat; /* end pattern */
14240 int dir; /* BACKWARD or FORWARD */
14241 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014242 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014243 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014244 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014245{
14246 char_u *save_cpo;
14247 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14248 long retval = 0;
14249 pos_T pos;
14250 pos_T firstpos;
14251 pos_T foundpos;
14252 pos_T save_cursor;
14253 pos_T save_pos;
14254 int n;
14255 int r;
14256 int nest = 1;
14257 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014258 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014259
14260 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14261 save_cpo = p_cpo;
14262 p_cpo = (char_u *)"";
14263
14264 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14265 * start/middle/end (pat3, for the top pair). */
14266 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14267 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14268 if (pat2 == NULL || pat3 == NULL)
14269 goto theend;
14270 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14271 if (*mpat == NUL)
14272 STRCPY(pat3, pat2);
14273 else
14274 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14275 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014276 if (flags & SP_START)
14277 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014278
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 save_cursor = curwin->w_cursor;
14280 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014281 clearpos(&firstpos);
14282 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 pat = pat3;
14284 for (;;)
14285 {
14286 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014287 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14289 /* didn't find it or found the first match again: FAIL */
14290 break;
14291
14292 if (firstpos.lnum == 0)
14293 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014294 if (equalpos(pos, foundpos))
14295 {
14296 /* Found the same position again. Can happen with a pattern that
14297 * has "\zs" at the end and searching backwards. Advance one
14298 * character and try again. */
14299 if (dir == BACKWARD)
14300 decl(&pos);
14301 else
14302 incl(&pos);
14303 }
14304 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305
14306 /* If the skip pattern matches, ignore this match. */
14307 if (*skip != NUL)
14308 {
14309 save_pos = curwin->w_cursor;
14310 curwin->w_cursor = pos;
14311 r = eval_to_bool(skip, &err, NULL, FALSE);
14312 curwin->w_cursor = save_pos;
14313 if (err)
14314 {
14315 /* Evaluating {skip} caused an error, break here. */
14316 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014317 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 break;
14319 }
14320 if (r)
14321 continue;
14322 }
14323
14324 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14325 {
14326 /* Found end when searching backwards or start when searching
14327 * forward: nested pair. */
14328 ++nest;
14329 pat = pat2; /* nested, don't search for middle */
14330 }
14331 else
14332 {
14333 /* Found end when searching forward or start when searching
14334 * backward: end of (nested) pair; or found middle in outer pair. */
14335 if (--nest == 1)
14336 pat = pat3; /* outer level, search for middle */
14337 }
14338
14339 if (nest == 0)
14340 {
14341 /* Found the match: return matchcount or line number. */
14342 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014343 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014345 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014346 if (flags & SP_SETPCMARK)
14347 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 curwin->w_cursor = pos;
14349 if (!(flags & SP_REPEAT))
14350 break;
14351 nest = 1; /* search for next unmatched */
14352 }
14353 }
14354
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014355 if (match_pos != NULL)
14356 {
14357 /* Store the match cursor position */
14358 match_pos->lnum = curwin->w_cursor.lnum;
14359 match_pos->col = curwin->w_cursor.col + 1;
14360 }
14361
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014363 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 curwin->w_cursor = save_cursor;
14365
14366theend:
14367 vim_free(pat2);
14368 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014370
14371 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372}
14373
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014374/*
14375 * "searchpos()" function
14376 */
14377 static void
14378f_searchpos(argvars, rettv)
14379 typval_T *argvars;
14380 typval_T *rettv;
14381{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014382 pos_T match_pos;
14383 int lnum = 0;
14384 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014385 int n;
14386 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014387
14388 rettv->vval.v_number = 0;
14389
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014390 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014391 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014392
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014393 n = search_cmn(argvars, &match_pos, &flags);
14394 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014395 {
14396 lnum = match_pos.lnum;
14397 col = match_pos.col;
14398 }
14399
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014400 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14401 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014402 if (flags & SP_SUBPAT)
14403 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014404}
14405
14406
Bram Moolenaar0d660222005-01-07 21:51:51 +000014407/*ARGSUSED*/
14408 static void
14409f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014410 typval_T *argvars;
14411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014413#ifdef FEAT_CLIENTSERVER
14414 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 char_u *server = get_tv_string_chk(&argvars[0]);
14416 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417
Bram Moolenaar0d660222005-01-07 21:51:51 +000014418 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 if (server == NULL || reply == NULL)
14420 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014421 if (check_restricted() || check_secure())
14422 return;
14423# ifdef FEAT_X11
14424 if (check_connection() == FAIL)
14425 return;
14426# endif
14427
14428 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430 EMSG(_("E258: Unable to send to client"));
14431 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014433 rettv->vval.v_number = 0;
14434#else
14435 rettv->vval.v_number = -1;
14436#endif
14437}
14438
14439/*ARGSUSED*/
14440 static void
14441f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014442 typval_T *argvars;
14443 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014444{
14445 char_u *r = NULL;
14446
14447#ifdef FEAT_CLIENTSERVER
14448# ifdef WIN32
14449 r = serverGetVimNames();
14450# else
14451 make_connection();
14452 if (X_DISPLAY != NULL)
14453 r = serverGetVimNames(X_DISPLAY);
14454# endif
14455#endif
14456 rettv->v_type = VAR_STRING;
14457 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458}
14459
14460/*
14461 * "setbufvar()" function
14462 */
14463/*ARGSUSED*/
14464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014465f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014466 typval_T *argvars;
14467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468{
14469 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014472 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 char_u nbuf[NUMBUFLEN];
14474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014475 rettv->vval.v_number = 0;
14476
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 if (check_restricted() || check_secure())
14478 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014479 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14480 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 varp = &argvars[2];
14483
14484 if (buf != NULL && varname != NULL && varp != NULL)
14485 {
14486 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488
14489 if (*varname == '&')
14490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491 long numval;
14492 char_u *strval;
14493 int error = FALSE;
14494
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 numval = get_tv_number_chk(varp, &error);
14497 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014498 if (!error && strval != NULL)
14499 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 }
14501 else
14502 {
14503 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14504 if (bufvarname != NULL)
14505 {
14506 STRCPY(bufvarname, "b:");
14507 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014508 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 vim_free(bufvarname);
14510 }
14511 }
14512
14513 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516}
14517
14518/*
14519 * "setcmdpos()" function
14520 */
14521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014523 typval_T *argvars;
14524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 int pos = (int)get_tv_number(&argvars[0]) - 1;
14527
14528 if (pos >= 0)
14529 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530}
14531
14532/*
14533 * "setline()" function
14534 */
14535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014537 typval_T *argvars;
14538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539{
14540 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014541 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014542 list_T *l = NULL;
14543 listitem_T *li = NULL;
14544 long added = 0;
14545 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014547 lnum = get_tv_lnum(&argvars[0]);
14548 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014550 l = argvars[1].vval.v_list;
14551 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014553 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014554 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014555
14556 rettv->vval.v_number = 0; /* OK */
14557 for (;;)
14558 {
14559 if (l != NULL)
14560 {
14561 /* list argument, get next string */
14562 if (li == NULL)
14563 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014564 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014565 li = li->li_next;
14566 }
14567
14568 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014569 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014570 break;
14571 if (lnum <= curbuf->b_ml.ml_line_count)
14572 {
14573 /* existing line, replace it */
14574 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14575 {
14576 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014577 if (lnum == curwin->w_cursor.lnum)
14578 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014579 rettv->vval.v_number = 0; /* OK */
14580 }
14581 }
14582 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14583 {
14584 /* lnum is one past the last line, append the line */
14585 ++added;
14586 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14587 rettv->vval.v_number = 0; /* OK */
14588 }
14589
14590 if (l == NULL) /* only one string argument */
14591 break;
14592 ++lnum;
14593 }
14594
14595 if (added > 0)
14596 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597}
14598
14599/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014600 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014601 */
14602/*ARGSUSED*/
14603 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014604set_qf_ll_list(wp, list_arg, action_arg, rettv)
14605 win_T *wp;
14606 typval_T *list_arg;
14607 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014608 typval_T *rettv;
14609{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014610#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014611 char_u *act;
14612 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014613#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014614
Bram Moolenaar2641f772005-03-25 21:58:17 +000014615 rettv->vval.v_number = -1;
14616
14617#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014618 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014619 EMSG(_(e_listreq));
14620 else
14621 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014622 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014623
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014624 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014625 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014626 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 if (act == NULL)
14628 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014629 if (*act == 'a' || *act == 'r')
14630 action = *act;
14631 }
14632
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014633 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014634 rettv->vval.v_number = 0;
14635 }
14636#endif
14637}
14638
14639/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014640 * "setloclist()" function
14641 */
14642/*ARGSUSED*/
14643 static void
14644f_setloclist(argvars, rettv)
14645 typval_T *argvars;
14646 typval_T *rettv;
14647{
14648 win_T *win;
14649
14650 rettv->vval.v_number = -1;
14651
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014652 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014653 if (win != NULL)
14654 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14655}
14656
14657/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014658 * "setmatches()" function
14659 */
14660 static void
14661f_setmatches(argvars, rettv)
14662 typval_T *argvars;
14663 typval_T *rettv;
14664{
14665#ifdef FEAT_SEARCH_EXTRA
14666 list_T *l;
14667 listitem_T *li;
14668 dict_T *d;
14669
14670 rettv->vval.v_number = -1;
14671 if (argvars[0].v_type != VAR_LIST)
14672 {
14673 EMSG(_(e_listreq));
14674 return;
14675 }
14676 if ((l = argvars[0].vval.v_list) != NULL)
14677 {
14678
14679 /* To some extent make sure that we are dealing with a list from
14680 * "getmatches()". */
14681 li = l->lv_first;
14682 while (li != NULL)
14683 {
14684 if (li->li_tv.v_type != VAR_DICT
14685 || (d = li->li_tv.vval.v_dict) == NULL)
14686 {
14687 EMSG(_(e_invarg));
14688 return;
14689 }
14690 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14691 && dict_find(d, (char_u *)"pattern", -1) != NULL
14692 && dict_find(d, (char_u *)"priority", -1) != NULL
14693 && dict_find(d, (char_u *)"id", -1) != NULL))
14694 {
14695 EMSG(_(e_invarg));
14696 return;
14697 }
14698 li = li->li_next;
14699 }
14700
14701 clear_matches(curwin);
14702 li = l->lv_first;
14703 while (li != NULL)
14704 {
14705 d = li->li_tv.vval.v_dict;
14706 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14707 get_dict_string(d, (char_u *)"pattern", FALSE),
14708 (int)get_dict_number(d, (char_u *)"priority"),
14709 (int)get_dict_number(d, (char_u *)"id"));
14710 li = li->li_next;
14711 }
14712 rettv->vval.v_number = 0;
14713 }
14714#endif
14715}
14716
14717/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014718 * "setpos()" function
14719 */
14720/*ARGSUSED*/
14721 static void
14722f_setpos(argvars, rettv)
14723 typval_T *argvars;
14724 typval_T *rettv;
14725{
14726 pos_T pos;
14727 int fnum;
14728 char_u *name;
14729
14730 name = get_tv_string_chk(argvars);
14731 if (name != NULL)
14732 {
14733 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14734 {
14735 --pos.col;
14736 if (name[0] == '.') /* cursor */
14737 {
14738 if (fnum == curbuf->b_fnum)
14739 {
14740 curwin->w_cursor = pos;
14741 check_cursor();
14742 }
14743 else
14744 EMSG(_(e_invarg));
14745 }
14746 else if (name[0] == '\'') /* mark */
14747 (void)setmark_pos(name[1], &pos, fnum);
14748 else
14749 EMSG(_(e_invarg));
14750 }
14751 }
14752}
14753
14754/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014755 * "setqflist()" function
14756 */
14757/*ARGSUSED*/
14758 static void
14759f_setqflist(argvars, rettv)
14760 typval_T *argvars;
14761 typval_T *rettv;
14762{
14763 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14764}
14765
14766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 * "setreg()" function
14768 */
14769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014770f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014771 typval_T *argvars;
14772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773{
14774 int regname;
14775 char_u *strregname;
14776 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014777 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778 int append;
14779 char_u yank_type;
14780 long block_len;
14781
14782 block_len = -1;
14783 yank_type = MAUTO;
14784 append = FALSE;
14785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014786 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014787 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 if (strregname == NULL)
14790 return; /* type error; errmsg already given */
14791 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792 if (regname == 0 || regname == '@')
14793 regname = '"';
14794 else if (regname == '=')
14795 return;
14796
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014797 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 stropt = get_tv_string_chk(&argvars[2]);
14800 if (stropt == NULL)
14801 return; /* type error */
14802 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 switch (*stropt)
14804 {
14805 case 'a': case 'A': /* append */
14806 append = TRUE;
14807 break;
14808 case 'v': case 'c': /* character-wise selection */
14809 yank_type = MCHAR;
14810 break;
14811 case 'V': case 'l': /* line-wise selection */
14812 yank_type = MLINE;
14813 break;
14814#ifdef FEAT_VISUAL
14815 case 'b': case Ctrl_V: /* block-wise selection */
14816 yank_type = MBLOCK;
14817 if (VIM_ISDIGIT(stropt[1]))
14818 {
14819 ++stropt;
14820 block_len = getdigits(&stropt) - 1;
14821 --stropt;
14822 }
14823 break;
14824#endif
14825 }
14826 }
14827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014828 strval = get_tv_string_chk(&argvars[1]);
14829 if (strval != NULL)
14830 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833}
14834
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014835/*
14836 * "settabwinvar()" function
14837 */
14838 static void
14839f_settabwinvar(argvars, rettv)
14840 typval_T *argvars;
14841 typval_T *rettv;
14842{
14843 setwinvar(argvars, rettv, 1);
14844}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845
14846/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014847 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014851 typval_T *argvars;
14852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014854 setwinvar(argvars, rettv, 0);
14855}
14856
14857/*
14858 * "setwinvar()" and "settabwinvar()" functions
14859 */
14860 static void
14861setwinvar(argvars, rettv, off)
14862 typval_T *argvars;
14863 typval_T *rettv;
14864 int off;
14865{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866 win_T *win;
14867#ifdef FEAT_WINDOWS
14868 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014869 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870#endif
14871 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014872 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014874 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014876 rettv->vval.v_number = 0;
14877
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 if (check_restricted() || check_secure())
14879 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014880
14881#ifdef FEAT_WINDOWS
14882 if (off == 1)
14883 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14884 else
14885 tp = curtab;
14886#endif
14887 win = find_win_by_nr(&argvars[off], tp);
14888 varname = get_tv_string_chk(&argvars[off + 1]);
14889 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890
14891 if (win != NULL && varname != NULL && varp != NULL)
14892 {
14893#ifdef FEAT_WINDOWS
14894 /* set curwin to be our win, temporarily */
14895 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014896 save_curtab = curtab;
14897 goto_tabpage_tp(tp);
14898 if (!win_valid(win))
14899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900 curwin = win;
14901 curbuf = curwin->w_buffer;
14902#endif
14903
14904 if (*varname == '&')
14905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014906 long numval;
14907 char_u *strval;
14908 int error = FALSE;
14909
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014911 numval = get_tv_number_chk(varp, &error);
14912 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014913 if (!error && strval != NULL)
14914 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 }
14916 else
14917 {
14918 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14919 if (winvarname != NULL)
14920 {
14921 STRCPY(winvarname, "w:");
14922 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014923 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924 vim_free(winvarname);
14925 }
14926 }
14927
14928#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014929 /* Restore current tabpage and window, if still valid (autocomands can
14930 * make them invalid). */
14931 if (valid_tabpage(save_curtab))
14932 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 if (win_valid(save_curwin))
14934 {
14935 curwin = save_curwin;
14936 curbuf = curwin->w_buffer;
14937 }
14938#endif
14939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940}
14941
14942/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014943 * "shellescape({string})" function
14944 */
14945 static void
14946f_shellescape(argvars, rettv)
14947 typval_T *argvars;
14948 typval_T *rettv;
14949{
14950 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14951 rettv->v_type = VAR_STRING;
14952}
14953
14954/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 */
14957 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014958f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014959 typval_T *argvars;
14960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963
Bram Moolenaar0d660222005-01-07 21:51:51 +000014964 p = get_tv_string(&argvars[0]);
14965 rettv->vval.v_string = vim_strsave(p);
14966 simplify_filename(rettv->vval.v_string); /* simplify in place */
14967 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968}
14969
Bram Moolenaar0d660222005-01-07 21:51:51 +000014970static int
14971#ifdef __BORLANDC__
14972 _RTLENTRYF
14973#endif
14974 item_compare __ARGS((const void *s1, const void *s2));
14975static int
14976#ifdef __BORLANDC__
14977 _RTLENTRYF
14978#endif
14979 item_compare2 __ARGS((const void *s1, const void *s2));
14980
14981static int item_compare_ic;
14982static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014983static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984#define ITEM_COMPARE_FAIL 999
14985
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014987 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014989 static int
14990#ifdef __BORLANDC__
14991_RTLENTRYF
14992#endif
14993item_compare(s1, s2)
14994 const void *s1;
14995 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997 char_u *p1, *p2;
14998 char_u *tofree1, *tofree2;
14999 int res;
15000 char_u numbuf1[NUMBUFLEN];
15001 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015003 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15004 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015005 if (p1 == NULL)
15006 p1 = (char_u *)"";
15007 if (p2 == NULL)
15008 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015009 if (item_compare_ic)
15010 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015012 res = STRCMP(p1, p2);
15013 vim_free(tofree1);
15014 vim_free(tofree2);
15015 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016}
15017
15018 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019#ifdef __BORLANDC__
15020_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015022item_compare2(s1, s2)
15023 const void *s1;
15024 const void *s2;
15025{
15026 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015028 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015029 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015031 /* shortcut after failure in previous call; compare all items equal */
15032 if (item_compare_func_err)
15033 return 0;
15034
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015035 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15036 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015037 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15038 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015039
15040 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015041 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015042 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043 clear_tv(&argv[0]);
15044 clear_tv(&argv[1]);
15045
15046 if (res == FAIL)
15047 res = ITEM_COMPARE_FAIL;
15048 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015049 /* return value has wrong type */
15050 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15051 if (item_compare_func_err)
15052 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015053 clear_tv(&rettv);
15054 return res;
15055}
15056
15057/*
15058 * "sort({list})" function
15059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015062 typval_T *argvars;
15063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064{
Bram Moolenaar33570922005-01-25 22:26:29 +000015065 list_T *l;
15066 listitem_T *li;
15067 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 long len;
15069 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071 rettv->vval.v_number = 0;
15072 if (argvars[0].v_type != VAR_LIST)
15073 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 else
15075 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015077 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078 return;
15079 rettv->vval.v_list = l;
15080 rettv->v_type = VAR_LIST;
15081 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083 len = list_len(l);
15084 if (len <= 1)
15085 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087 item_compare_ic = FALSE;
15088 item_compare_func = NULL;
15089 if (argvars[1].v_type != VAR_UNKNOWN)
15090 {
15091 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 else
15094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015095 int error = FALSE;
15096
15097 i = get_tv_number_chk(&argvars[1], &error);
15098 if (error)
15099 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015100 if (i == 1)
15101 item_compare_ic = TRUE;
15102 else
15103 item_compare_func = get_tv_string(&argvars[1]);
15104 }
15105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015108 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109 if (ptrs == NULL)
15110 return;
15111 i = 0;
15112 for (li = l->lv_first; li != NULL; li = li->li_next)
15113 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015115 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015116 /* test the compare function */
15117 if (item_compare_func != NULL
15118 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15119 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015120 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 {
15123 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015124 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015125 item_compare_func == NULL ? item_compare : item_compare2);
15126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015127 if (!item_compare_func_err)
15128 {
15129 /* Clear the List and append the items in the sorted order. */
15130 l->lv_first = l->lv_last = NULL;
15131 l->lv_len = 0;
15132 for (i = 0; i < len; ++i)
15133 list_append(l, ptrs[i]);
15134 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135 }
15136
15137 vim_free(ptrs);
15138 }
15139}
15140
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015141/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015142 * "soundfold({word})" function
15143 */
15144 static void
15145f_soundfold(argvars, rettv)
15146 typval_T *argvars;
15147 typval_T *rettv;
15148{
15149 char_u *s;
15150
15151 rettv->v_type = VAR_STRING;
15152 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015153#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015154 rettv->vval.v_string = eval_soundfold(s);
15155#else
15156 rettv->vval.v_string = vim_strsave(s);
15157#endif
15158}
15159
15160/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015161 * "spellbadword()" function
15162 */
15163/* ARGSUSED */
15164 static void
15165f_spellbadword(argvars, rettv)
15166 typval_T *argvars;
15167 typval_T *rettv;
15168{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015169 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015170 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015171 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015172
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015173 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015174 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015175
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015176#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015177 if (argvars[0].v_type == VAR_UNKNOWN)
15178 {
15179 /* Find the start and length of the badly spelled word. */
15180 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15181 if (len != 0)
15182 word = ml_get_cursor();
15183 }
15184 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15185 {
15186 char_u *str = get_tv_string_chk(&argvars[0]);
15187 int capcol = -1;
15188
15189 if (str != NULL)
15190 {
15191 /* Check the argument for spelling. */
15192 while (*str != NUL)
15193 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015194 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015195 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015196 {
15197 word = str;
15198 break;
15199 }
15200 str += len;
15201 }
15202 }
15203 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015204#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015205
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015206 list_append_string(rettv->vval.v_list, word, len);
15207 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015208 attr == HLF_SPB ? "bad" :
15209 attr == HLF_SPR ? "rare" :
15210 attr == HLF_SPL ? "local" :
15211 attr == HLF_SPC ? "caps" :
15212 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015213}
15214
15215/*
15216 * "spellsuggest()" function
15217 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015218/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015219 static void
15220f_spellsuggest(argvars, rettv)
15221 typval_T *argvars;
15222 typval_T *rettv;
15223{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015224#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015225 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015226 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015227 int maxcount;
15228 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015229 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015230 listitem_T *li;
15231 int need_capital = FALSE;
15232#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015233
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015234 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015235 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015236
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015237#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015238 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15239 {
15240 str = get_tv_string(&argvars[0]);
15241 if (argvars[1].v_type != VAR_UNKNOWN)
15242 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015243 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015244 if (maxcount <= 0)
15245 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015246 if (argvars[2].v_type != VAR_UNKNOWN)
15247 {
15248 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15249 if (typeerr)
15250 return;
15251 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015252 }
15253 else
15254 maxcount = 25;
15255
Bram Moolenaar4770d092006-01-12 23:22:24 +000015256 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015257
15258 for (i = 0; i < ga.ga_len; ++i)
15259 {
15260 str = ((char_u **)ga.ga_data)[i];
15261
15262 li = listitem_alloc();
15263 if (li == NULL)
15264 vim_free(str);
15265 else
15266 {
15267 li->li_tv.v_type = VAR_STRING;
15268 li->li_tv.v_lock = 0;
15269 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015270 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015271 }
15272 }
15273 ga_clear(&ga);
15274 }
15275#endif
15276}
15277
Bram Moolenaar0d660222005-01-07 21:51:51 +000015278 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015279f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015280 typval_T *argvars;
15281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015282{
15283 char_u *str;
15284 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015285 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286 regmatch_T regmatch;
15287 char_u patbuf[NUMBUFLEN];
15288 char_u *save_cpo;
15289 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015291 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015292 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015293
15294 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15295 save_cpo = p_cpo;
15296 p_cpo = (char_u *)"";
15297
15298 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015299 if (argvars[1].v_type != VAR_UNKNOWN)
15300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015301 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15302 if (pat == NULL)
15303 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015304 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015305 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015306 }
15307 if (pat == NULL || *pat == NUL)
15308 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015310 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015312 if (typeerr)
15313 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314
Bram Moolenaar0d660222005-01-07 21:51:51 +000015315 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15316 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015318 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015319 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015320 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015321 if (*str == NUL)
15322 match = FALSE; /* empty item at the end */
15323 else
15324 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015325 if (match)
15326 end = regmatch.startp[0];
15327 else
15328 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015329 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15330 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015331 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015332 if (list_append_string(rettv->vval.v_list, str,
15333 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015334 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015335 }
15336 if (!match)
15337 break;
15338 /* Advance to just after the match. */
15339 if (regmatch.endp[0] > str)
15340 col = 0;
15341 else
15342 {
15343 /* Don't get stuck at the same match. */
15344#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015345 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015346#else
15347 col = 1;
15348#endif
15349 }
15350 str = regmatch.endp[0];
15351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352
Bram Moolenaar0d660222005-01-07 21:51:51 +000015353 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355
Bram Moolenaar0d660222005-01-07 21:51:51 +000015356 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357}
15358
Bram Moolenaar2c932302006-03-18 21:42:09 +000015359/*
15360 * "str2nr()" function
15361 */
15362 static void
15363f_str2nr(argvars, rettv)
15364 typval_T *argvars;
15365 typval_T *rettv;
15366{
15367 int base = 10;
15368 char_u *p;
15369 long n;
15370
15371 if (argvars[1].v_type != VAR_UNKNOWN)
15372 {
15373 base = get_tv_number(&argvars[1]);
15374 if (base != 8 && base != 10 && base != 16)
15375 {
15376 EMSG(_(e_invarg));
15377 return;
15378 }
15379 }
15380
15381 p = skipwhite(get_tv_string(&argvars[0]));
15382 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15383 rettv->vval.v_number = n;
15384}
15385
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386#ifdef HAVE_STRFTIME
15387/*
15388 * "strftime({format}[, {time}])" function
15389 */
15390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015392 typval_T *argvars;
15393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394{
15395 char_u result_buf[256];
15396 struct tm *curtime;
15397 time_t seconds;
15398 char_u *p;
15399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015402 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015403 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404 seconds = time(NULL);
15405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 curtime = localtime(&seconds);
15408 /* MSVC returns NULL for an invalid value of seconds. */
15409 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015410 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 else
15412 {
15413# ifdef FEAT_MBYTE
15414 vimconv_T conv;
15415 char_u *enc;
15416
15417 conv.vc_type = CONV_NONE;
15418 enc = enc_locale();
15419 convert_setup(&conv, p_enc, enc);
15420 if (conv.vc_type != CONV_NONE)
15421 p = string_convert(&conv, p, NULL);
15422# endif
15423 if (p != NULL)
15424 (void)strftime((char *)result_buf, sizeof(result_buf),
15425 (char *)p, curtime);
15426 else
15427 result_buf[0] = NUL;
15428
15429# ifdef FEAT_MBYTE
15430 if (conv.vc_type != CONV_NONE)
15431 vim_free(p);
15432 convert_setup(&conv, enc, p_enc);
15433 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015434 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 else
15436# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015437 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438
15439# ifdef FEAT_MBYTE
15440 /* Release conversion descriptors */
15441 convert_setup(&conv, NULL, NULL);
15442 vim_free(enc);
15443# endif
15444 }
15445}
15446#endif
15447
15448/*
15449 * "stridx()" function
15450 */
15451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015452f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015453 typval_T *argvars;
15454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455{
15456 char_u buf[NUMBUFLEN];
15457 char_u *needle;
15458 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015461 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015463 needle = get_tv_string_chk(&argvars[1]);
15464 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015466 if (needle == NULL || haystack == NULL)
15467 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468
Bram Moolenaar33570922005-01-25 22:26:29 +000015469 if (argvars[2].v_type != VAR_UNKNOWN)
15470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015471 int error = FALSE;
15472
15473 start_idx = get_tv_number_chk(&argvars[2], &error);
15474 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015475 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015476 if (start_idx >= 0)
15477 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015478 }
15479
15480 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15481 if (pos != NULL)
15482 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483}
15484
15485/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015486 * "string()" function
15487 */
15488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015489f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015490 typval_T *argvars;
15491 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015492{
15493 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015494 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015497 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015498 /* Make a copy if we have a value but it's not in allocate memory. */
15499 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015500 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501}
15502
15503/*
15504 * "strlen()" function
15505 */
15506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015507f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015508 typval_T *argvars;
15509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511 rettv->vval.v_number = (varnumber_T)(STRLEN(
15512 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513}
15514
15515/*
15516 * "strpart()" function
15517 */
15518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015519f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015520 typval_T *argvars;
15521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522{
15523 char_u *p;
15524 int n;
15525 int len;
15526 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015527 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 slen = (int)STRLEN(p);
15531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015532 n = get_tv_number_chk(&argvars[1], &error);
15533 if (error)
15534 len = 0;
15535 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 else
15538 len = slen - n; /* default len: all bytes that are available. */
15539
15540 /*
15541 * Only return the overlap between the specified part and the actual
15542 * string.
15543 */
15544 if (n < 0)
15545 {
15546 len += n;
15547 n = 0;
15548 }
15549 else if (n > slen)
15550 n = slen;
15551 if (len < 0)
15552 len = 0;
15553 else if (n + len > slen)
15554 len = slen - n;
15555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->v_type = VAR_STRING;
15557 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558}
15559
15560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015561 * "strridx()" function
15562 */
15563 static void
15564f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015565 typval_T *argvars;
15566 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567{
15568 char_u buf[NUMBUFLEN];
15569 char_u *needle;
15570 char_u *haystack;
15571 char_u *rest;
15572 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015573 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015575 needle = get_tv_string_chk(&argvars[1]);
15576 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015577
15578 rettv->vval.v_number = -1;
15579 if (needle == NULL || haystack == NULL)
15580 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015581
15582 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015583 if (argvars[2].v_type != VAR_UNKNOWN)
15584 {
15585 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015586 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015587 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015589 }
15590 else
15591 end_idx = haystack_len;
15592
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015594 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015595 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015596 lastmatch = haystack + end_idx;
15597 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015598 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015599 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600 for (rest = haystack; *rest != '\0'; ++rest)
15601 {
15602 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015603 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604 break;
15605 lastmatch = rest;
15606 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015607 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015608
15609 if (lastmatch == NULL)
15610 rettv->vval.v_number = -1;
15611 else
15612 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15613}
15614
15615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 * "strtrans()" function
15617 */
15618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015619f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015620 typval_T *argvars;
15621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015623 rettv->v_type = VAR_STRING;
15624 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625}
15626
15627/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015628 * "submatch()" function
15629 */
15630 static void
15631f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015632 typval_T *argvars;
15633 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015634{
15635 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015636 rettv->vval.v_string =
15637 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015638}
15639
15640/*
15641 * "substitute()" function
15642 */
15643 static void
15644f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015645 typval_T *argvars;
15646 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015647{
15648 char_u patbuf[NUMBUFLEN];
15649 char_u subbuf[NUMBUFLEN];
15650 char_u flagsbuf[NUMBUFLEN];
15651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015652 char_u *str = get_tv_string_chk(&argvars[0]);
15653 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15654 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15655 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15656
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015658 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15659 rettv->vval.v_string = NULL;
15660 else
15661 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015662}
15663
15664/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015665 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 */
15667/*ARGSUSED*/
15668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015669f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015670 typval_T *argvars;
15671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672{
15673 int id = 0;
15674#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015675 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 long col;
15677 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015678 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015680 lnum = get_tv_lnum(argvars); /* -1 on type error */
15681 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15682 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015684 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015685 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015686 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687#endif
15688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015689 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690}
15691
15692/*
15693 * "synIDattr(id, what [, mode])" function
15694 */
15695/*ARGSUSED*/
15696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015697f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015698 typval_T *argvars;
15699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700{
15701 char_u *p = NULL;
15702#ifdef FEAT_SYN_HL
15703 int id;
15704 char_u *what;
15705 char_u *mode;
15706 char_u modebuf[NUMBUFLEN];
15707 int modec;
15708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015709 id = get_tv_number(&argvars[0]);
15710 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015711 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015713 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 modec = TOLOWER_ASC(mode[0]);
15715 if (modec != 't' && modec != 'c'
15716#ifdef FEAT_GUI
15717 && modec != 'g'
15718#endif
15719 )
15720 modec = 0; /* replace invalid with current */
15721 }
15722 else
15723 {
15724#ifdef FEAT_GUI
15725 if (gui.in_use)
15726 modec = 'g';
15727 else
15728#endif
15729 if (t_colors > 1)
15730 modec = 'c';
15731 else
15732 modec = 't';
15733 }
15734
15735
15736 switch (TOLOWER_ASC(what[0]))
15737 {
15738 case 'b':
15739 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15740 p = highlight_color(id, what, modec);
15741 else /* bold */
15742 p = highlight_has_attr(id, HL_BOLD, modec);
15743 break;
15744
15745 case 'f': /* fg[#] */
15746 p = highlight_color(id, what, modec);
15747 break;
15748
15749 case 'i':
15750 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15751 p = highlight_has_attr(id, HL_INVERSE, modec);
15752 else /* italic */
15753 p = highlight_has_attr(id, HL_ITALIC, modec);
15754 break;
15755
15756 case 'n': /* name */
15757 p = get_highlight_name(NULL, id - 1);
15758 break;
15759
15760 case 'r': /* reverse */
15761 p = highlight_has_attr(id, HL_INVERSE, modec);
15762 break;
15763
15764 case 's': /* standout */
15765 p = highlight_has_attr(id, HL_STANDOUT, modec);
15766 break;
15767
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015768 case 'u':
15769 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15770 /* underline */
15771 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15772 else
15773 /* undercurl */
15774 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 break;
15776 }
15777
15778 if (p != NULL)
15779 p = vim_strsave(p);
15780#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015781 rettv->v_type = VAR_STRING;
15782 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783}
15784
15785/*
15786 * "synIDtrans(id)" function
15787 */
15788/*ARGSUSED*/
15789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015790f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015791 typval_T *argvars;
15792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793{
15794 int id;
15795
15796#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015797 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798
15799 if (id > 0)
15800 id = syn_get_final_id(id);
15801 else
15802#endif
15803 id = 0;
15804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015805 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806}
15807
15808/*
15809 * "system()" function
15810 */
15811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015812f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015813 typval_T *argvars;
15814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015816 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015818 char_u *infile = NULL;
15819 char_u buf[NUMBUFLEN];
15820 int err = FALSE;
15821 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015823 if (check_restricted() || check_secure())
15824 return;
15825
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015826 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015827 {
15828 /*
15829 * Write the string to a temp file, to be used for input of the shell
15830 * command.
15831 */
15832 if ((infile = vim_tempname('i')) == NULL)
15833 {
15834 EMSG(_(e_notmp));
15835 return;
15836 }
15837
15838 fd = mch_fopen((char *)infile, WRITEBIN);
15839 if (fd == NULL)
15840 {
15841 EMSG2(_(e_notopen), infile);
15842 goto done;
15843 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015844 p = get_tv_string_buf_chk(&argvars[1], buf);
15845 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015846 {
15847 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015848 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015849 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015850 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15851 err = TRUE;
15852 if (fclose(fd) != 0)
15853 err = TRUE;
15854 if (err)
15855 {
15856 EMSG(_("E677: Error writing temp file"));
15857 goto done;
15858 }
15859 }
15860
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015861 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15862 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015863
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864#ifdef USE_CR
15865 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015866 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 {
15868 char_u *s;
15869
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015870 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 {
15872 if (*s == CAR)
15873 *s = NL;
15874 }
15875 }
15876#else
15877# ifdef USE_CRNL
15878 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015879 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 {
15881 char_u *s, *d;
15882
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015883 d = res;
15884 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 {
15886 if (s[0] == CAR && s[1] == NL)
15887 ++s;
15888 *d++ = *s;
15889 }
15890 *d = NUL;
15891 }
15892# endif
15893#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015894
15895done:
15896 if (infile != NULL)
15897 {
15898 mch_remove(infile);
15899 vim_free(infile);
15900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015901 rettv->v_type = VAR_STRING;
15902 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903}
15904
15905/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015906 * "tabpagebuflist()" function
15907 */
15908/* ARGSUSED */
15909 static void
15910f_tabpagebuflist(argvars, rettv)
15911 typval_T *argvars;
15912 typval_T *rettv;
15913{
15914#ifndef FEAT_WINDOWS
15915 rettv->vval.v_number = 0;
15916#else
15917 tabpage_T *tp;
15918 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015919
15920 if (argvars[0].v_type == VAR_UNKNOWN)
15921 wp = firstwin;
15922 else
15923 {
15924 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15925 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015926 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015927 }
15928 if (wp == NULL)
15929 rettv->vval.v_number = 0;
15930 else
15931 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015932 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015933 rettv->vval.v_number = 0;
15934 else
15935 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015936 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015937 if (list_append_number(rettv->vval.v_list,
15938 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015939 break;
15940 }
15941 }
15942#endif
15943}
15944
15945
15946/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015947 * "tabpagenr()" function
15948 */
15949/* ARGSUSED */
15950 static void
15951f_tabpagenr(argvars, rettv)
15952 typval_T *argvars;
15953 typval_T *rettv;
15954{
15955 int nr = 1;
15956#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015957 char_u *arg;
15958
15959 if (argvars[0].v_type != VAR_UNKNOWN)
15960 {
15961 arg = get_tv_string_chk(&argvars[0]);
15962 nr = 0;
15963 if (arg != NULL)
15964 {
15965 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015966 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015967 else
15968 EMSG2(_(e_invexpr2), arg);
15969 }
15970 }
15971 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015972 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015973#endif
15974 rettv->vval.v_number = nr;
15975}
15976
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015977
15978#ifdef FEAT_WINDOWS
15979static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15980
15981/*
15982 * Common code for tabpagewinnr() and winnr().
15983 */
15984 static int
15985get_winnr(tp, argvar)
15986 tabpage_T *tp;
15987 typval_T *argvar;
15988{
15989 win_T *twin;
15990 int nr = 1;
15991 win_T *wp;
15992 char_u *arg;
15993
15994 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15995 if (argvar->v_type != VAR_UNKNOWN)
15996 {
15997 arg = get_tv_string_chk(argvar);
15998 if (arg == NULL)
15999 nr = 0; /* type error; errmsg already given */
16000 else if (STRCMP(arg, "$") == 0)
16001 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16002 else if (STRCMP(arg, "#") == 0)
16003 {
16004 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16005 if (twin == NULL)
16006 nr = 0;
16007 }
16008 else
16009 {
16010 EMSG2(_(e_invexpr2), arg);
16011 nr = 0;
16012 }
16013 }
16014
16015 if (nr > 0)
16016 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16017 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016018 {
16019 if (wp == NULL)
16020 {
16021 /* didn't find it in this tabpage */
16022 nr = 0;
16023 break;
16024 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016025 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016026 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016027 return nr;
16028}
16029#endif
16030
16031/*
16032 * "tabpagewinnr()" function
16033 */
16034/* ARGSUSED */
16035 static void
16036f_tabpagewinnr(argvars, rettv)
16037 typval_T *argvars;
16038 typval_T *rettv;
16039{
16040 int nr = 1;
16041#ifdef FEAT_WINDOWS
16042 tabpage_T *tp;
16043
16044 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16045 if (tp == NULL)
16046 nr = 0;
16047 else
16048 nr = get_winnr(tp, &argvars[1]);
16049#endif
16050 rettv->vval.v_number = nr;
16051}
16052
16053
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016054/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016055 * "tagfiles()" function
16056 */
16057/*ARGSUSED*/
16058 static void
16059f_tagfiles(argvars, rettv)
16060 typval_T *argvars;
16061 typval_T *rettv;
16062{
16063 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016064 tagname_T tn;
16065 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016066
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016067 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016068 {
16069 rettv->vval.v_number = 0;
16070 return;
16071 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016072
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016073 for (first = TRUE; ; first = FALSE)
16074 if (get_tagfname(&tn, first, fname) == FAIL
16075 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016076 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016077 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016078}
16079
16080/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016081 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016082 */
16083 static void
16084f_taglist(argvars, rettv)
16085 typval_T *argvars;
16086 typval_T *rettv;
16087{
16088 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016089
16090 tag_pattern = get_tv_string(&argvars[0]);
16091
16092 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016093 if (*tag_pattern == NUL)
16094 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016095
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016096 if (rettv_list_alloc(rettv) == OK)
16097 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016098}
16099
16100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 * "tempname()" function
16102 */
16103/*ARGSUSED*/
16104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016105f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016106 typval_T *argvars;
16107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108{
16109 static int x = 'A';
16110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016111 rettv->v_type = VAR_STRING;
16112 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113
16114 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16115 * names. Skip 'I' and 'O', they are used for shell redirection. */
16116 do
16117 {
16118 if (x == 'Z')
16119 x = '0';
16120 else if (x == '9')
16121 x = 'A';
16122 else
16123 {
16124#ifdef EBCDIC
16125 if (x == 'I')
16126 x = 'J';
16127 else if (x == 'R')
16128 x = 'S';
16129 else
16130#endif
16131 ++x;
16132 }
16133 } while (x == 'I' || x == 'O');
16134}
16135
16136/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016137 * "test(list)" function: Just checking the walls...
16138 */
16139/*ARGSUSED*/
16140 static void
16141f_test(argvars, rettv)
16142 typval_T *argvars;
16143 typval_T *rettv;
16144{
16145 /* Used for unit testing. Change the code below to your liking. */
16146#if 0
16147 listitem_T *li;
16148 list_T *l;
16149 char_u *bad, *good;
16150
16151 if (argvars[0].v_type != VAR_LIST)
16152 return;
16153 l = argvars[0].vval.v_list;
16154 if (l == NULL)
16155 return;
16156 li = l->lv_first;
16157 if (li == NULL)
16158 return;
16159 bad = get_tv_string(&li->li_tv);
16160 li = li->li_next;
16161 if (li == NULL)
16162 return;
16163 good = get_tv_string(&li->li_tv);
16164 rettv->vval.v_number = test_edit_score(bad, good);
16165#endif
16166}
16167
16168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169 * "tolower(string)" function
16170 */
16171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016172f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016173 typval_T *argvars;
16174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175{
16176 char_u *p;
16177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016178 p = vim_strsave(get_tv_string(&argvars[0]));
16179 rettv->v_type = VAR_STRING;
16180 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181
16182 if (p != NULL)
16183 while (*p != NUL)
16184 {
16185#ifdef FEAT_MBYTE
16186 int l;
16187
16188 if (enc_utf8)
16189 {
16190 int c, lc;
16191
16192 c = utf_ptr2char(p);
16193 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016194 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 /* TODO: reallocate string when byte count changes. */
16196 if (utf_char2len(lc) == l)
16197 utf_char2bytes(lc, p);
16198 p += l;
16199 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016200 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 p += l; /* skip multi-byte character */
16202 else
16203#endif
16204 {
16205 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16206 ++p;
16207 }
16208 }
16209}
16210
16211/*
16212 * "toupper(string)" function
16213 */
16214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016215f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016216 typval_T *argvars;
16217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016219 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016220 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221}
16222
16223/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016224 * "tr(string, fromstr, tostr)" function
16225 */
16226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016227f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016228 typval_T *argvars;
16229 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016230{
16231 char_u *instr;
16232 char_u *fromstr;
16233 char_u *tostr;
16234 char_u *p;
16235#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016236 int inlen;
16237 int fromlen;
16238 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016239 int idx;
16240 char_u *cpstr;
16241 int cplen;
16242 int first = TRUE;
16243#endif
16244 char_u buf[NUMBUFLEN];
16245 char_u buf2[NUMBUFLEN];
16246 garray_T ga;
16247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016248 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016249 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16250 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016251
16252 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016253 rettv->v_type = VAR_STRING;
16254 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016255 if (fromstr == NULL || tostr == NULL)
16256 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016257 ga_init2(&ga, (int)sizeof(char), 80);
16258
16259#ifdef FEAT_MBYTE
16260 if (!has_mbyte)
16261#endif
16262 /* not multi-byte: fromstr and tostr must be the same length */
16263 if (STRLEN(fromstr) != STRLEN(tostr))
16264 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016265#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016266error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016267#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016268 EMSG2(_(e_invarg2), fromstr);
16269 ga_clear(&ga);
16270 return;
16271 }
16272
16273 /* fromstr and tostr have to contain the same number of chars */
16274 while (*instr != NUL)
16275 {
16276#ifdef FEAT_MBYTE
16277 if (has_mbyte)
16278 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016279 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016280 cpstr = instr;
16281 cplen = inlen;
16282 idx = 0;
16283 for (p = fromstr; *p != NUL; p += fromlen)
16284 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016285 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016286 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16287 {
16288 for (p = tostr; *p != NUL; p += tolen)
16289 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016290 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016291 if (idx-- == 0)
16292 {
16293 cplen = tolen;
16294 cpstr = p;
16295 break;
16296 }
16297 }
16298 if (*p == NUL) /* tostr is shorter than fromstr */
16299 goto error;
16300 break;
16301 }
16302 ++idx;
16303 }
16304
16305 if (first && cpstr == instr)
16306 {
16307 /* Check that fromstr and tostr have the same number of
16308 * (multi-byte) characters. Done only once when a character
16309 * of instr doesn't appear in fromstr. */
16310 first = FALSE;
16311 for (p = tostr; *p != NUL; p += tolen)
16312 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016313 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016314 --idx;
16315 }
16316 if (idx != 0)
16317 goto error;
16318 }
16319
16320 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016321 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016322 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016323
16324 instr += inlen;
16325 }
16326 else
16327#endif
16328 {
16329 /* When not using multi-byte chars we can do it faster. */
16330 p = vim_strchr(fromstr, *instr);
16331 if (p != NULL)
16332 ga_append(&ga, tostr[p - fromstr]);
16333 else
16334 ga_append(&ga, *instr);
16335 ++instr;
16336 }
16337 }
16338
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016339 /* add a terminating NUL */
16340 ga_grow(&ga, 1);
16341 ga_append(&ga, NUL);
16342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016343 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016344}
16345
16346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 * "type(expr)" function
16348 */
16349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016351 typval_T *argvars;
16352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016354 int n;
16355
16356 switch (argvars[0].v_type)
16357 {
16358 case VAR_NUMBER: n = 0; break;
16359 case VAR_STRING: n = 1; break;
16360 case VAR_FUNC: n = 2; break;
16361 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016362 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016363 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16364 }
16365 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366}
16367
16368/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016369 * "values(dict)" function
16370 */
16371 static void
16372f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016373 typval_T *argvars;
16374 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016375{
16376 dict_list(argvars, rettv, 1);
16377}
16378
16379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 * "virtcol(string)" function
16381 */
16382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016383f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016384 typval_T *argvars;
16385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386{
16387 colnr_T vcol = 0;
16388 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016389 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016391 fp = var2fpos(&argvars[0], FALSE, &fnum);
16392 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16393 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 {
16395 getvvcol(curwin, fp, NULL, NULL, &vcol);
16396 ++vcol;
16397 }
16398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016399 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400}
16401
16402/*
16403 * "visualmode()" function
16404 */
16405/*ARGSUSED*/
16406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016407f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016408 typval_T *argvars;
16409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410{
16411#ifdef FEAT_VISUAL
16412 char_u str[2];
16413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016414 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 str[0] = curbuf->b_visual_mode_eval;
16416 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016417 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418
16419 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016420 if ((argvars[0].v_type == VAR_NUMBER
16421 && argvars[0].vval.v_number != 0)
16422 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016423 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 curbuf->b_visual_mode_eval = NUL;
16425#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016426 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427#endif
16428}
16429
16430/*
16431 * "winbufnr(nr)" function
16432 */
16433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016434f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016435 typval_T *argvars;
16436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437{
16438 win_T *wp;
16439
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016440 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016442 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016444 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445}
16446
16447/*
16448 * "wincol()" function
16449 */
16450/*ARGSUSED*/
16451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016452f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016453 typval_T *argvars;
16454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455{
16456 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016457 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458}
16459
16460/*
16461 * "winheight(nr)" function
16462 */
16463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016464f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016465 typval_T *argvars;
16466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467{
16468 win_T *wp;
16469
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016470 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016472 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016474 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475}
16476
16477/*
16478 * "winline()" function
16479 */
16480/*ARGSUSED*/
16481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016482f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 typval_T *argvars;
16484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485{
16486 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016487 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488}
16489
16490/*
16491 * "winnr()" function
16492 */
16493/* ARGSUSED */
16494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016495f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016496 typval_T *argvars;
16497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498{
16499 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016500
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016502 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016504 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505}
16506
16507/*
16508 * "winrestcmd()" function
16509 */
16510/* ARGSUSED */
16511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016512f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 typval_T *argvars;
16514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515{
16516#ifdef FEAT_WINDOWS
16517 win_T *wp;
16518 int winnr = 1;
16519 garray_T ga;
16520 char_u buf[50];
16521
16522 ga_init2(&ga, (int)sizeof(char), 70);
16523 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16524 {
16525 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16526 ga_concat(&ga, buf);
16527# ifdef FEAT_VERTSPLIT
16528 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16529 ga_concat(&ga, buf);
16530# endif
16531 ++winnr;
16532 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016533 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016535 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540}
16541
16542/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016543 * "winrestview()" function
16544 */
16545/* ARGSUSED */
16546 static void
16547f_winrestview(argvars, rettv)
16548 typval_T *argvars;
16549 typval_T *rettv;
16550{
16551 dict_T *dict;
16552
16553 if (argvars[0].v_type != VAR_DICT
16554 || (dict = argvars[0].vval.v_dict) == NULL)
16555 EMSG(_(e_invarg));
16556 else
16557 {
16558 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16559 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16560#ifdef FEAT_VIRTUALEDIT
16561 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16562#endif
16563 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016564 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016565
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016566 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016567#ifdef FEAT_DIFF
16568 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16569#endif
16570 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16571 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16572
16573 check_cursor();
16574 changed_cline_bef_curs();
16575 invalidate_botline();
16576 redraw_later(VALID);
16577
16578 if (curwin->w_topline == 0)
16579 curwin->w_topline = 1;
16580 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16581 curwin->w_topline = curbuf->b_ml.ml_line_count;
16582#ifdef FEAT_DIFF
16583 check_topfill(curwin, TRUE);
16584#endif
16585 }
16586}
16587
16588/*
16589 * "winsaveview()" function
16590 */
16591/* ARGSUSED */
16592 static void
16593f_winsaveview(argvars, rettv)
16594 typval_T *argvars;
16595 typval_T *rettv;
16596{
16597 dict_T *dict;
16598
16599 dict = dict_alloc();
16600 if (dict == NULL)
16601 return;
16602 rettv->v_type = VAR_DICT;
16603 rettv->vval.v_dict = dict;
16604 ++dict->dv_refcount;
16605
16606 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16607 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16608#ifdef FEAT_VIRTUALEDIT
16609 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16610#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016611 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016612 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16613
16614 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16615#ifdef FEAT_DIFF
16616 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16617#endif
16618 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16619 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16620}
16621
16622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 * "winwidth(nr)" function
16624 */
16625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016627 typval_T *argvars;
16628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629{
16630 win_T *wp;
16631
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016632 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 else
16636#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016639 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640#endif
16641}
16642
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016644 * "writefile()" function
16645 */
16646 static void
16647f_writefile(argvars, rettv)
16648 typval_T *argvars;
16649 typval_T *rettv;
16650{
16651 int binary = FALSE;
16652 char_u *fname;
16653 FILE *fd;
16654 listitem_T *li;
16655 char_u *s;
16656 int ret = 0;
16657 int c;
16658
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016659 if (check_restricted() || check_secure())
16660 return;
16661
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016662 if (argvars[0].v_type != VAR_LIST)
16663 {
16664 EMSG2(_(e_listarg), "writefile()");
16665 return;
16666 }
16667 if (argvars[0].vval.v_list == NULL)
16668 return;
16669
16670 if (argvars[2].v_type != VAR_UNKNOWN
16671 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16672 binary = TRUE;
16673
16674 /* Always open the file in binary mode, library functions have a mind of
16675 * their own about CR-LF conversion. */
16676 fname = get_tv_string(&argvars[1]);
16677 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16678 {
16679 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16680 ret = -1;
16681 }
16682 else
16683 {
16684 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16685 li = li->li_next)
16686 {
16687 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16688 {
16689 if (*s == '\n')
16690 c = putc(NUL, fd);
16691 else
16692 c = putc(*s, fd);
16693 if (c == EOF)
16694 {
16695 ret = -1;
16696 break;
16697 }
16698 }
16699 if (!binary || li->li_next != NULL)
16700 if (putc('\n', fd) == EOF)
16701 {
16702 ret = -1;
16703 break;
16704 }
16705 if (ret < 0)
16706 {
16707 EMSG(_(e_write));
16708 break;
16709 }
16710 }
16711 fclose(fd);
16712 }
16713
16714 rettv->vval.v_number = ret;
16715}
16716
16717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016719 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 */
16721 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016722var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016723 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016724 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016725 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016727 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016729 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730
Bram Moolenaara5525202006-03-02 22:52:09 +000016731 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016732 if (varp->v_type == VAR_LIST)
16733 {
16734 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016735 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016736 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016737 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016738
16739 l = varp->vval.v_list;
16740 if (l == NULL)
16741 return NULL;
16742
16743 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016744 pos.lnum = list_find_nr(l, 0L, &error);
16745 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016746 return NULL; /* invalid line number */
16747
16748 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016749 pos.col = list_find_nr(l, 1L, &error);
16750 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016751 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016752 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016753
16754 /* We accept "$" for the column number: last column. */
16755 li = list_find(l, 1L);
16756 if (li != NULL && li->li_tv.v_type == VAR_STRING
16757 && li->li_tv.vval.v_string != NULL
16758 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16759 pos.col = len + 1;
16760
Bram Moolenaara5525202006-03-02 22:52:09 +000016761 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016762 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016763 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016764 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016765
Bram Moolenaara5525202006-03-02 22:52:09 +000016766#ifdef FEAT_VIRTUALEDIT
16767 /* Get the virtual offset. Defaults to zero. */
16768 pos.coladd = list_find_nr(l, 2L, &error);
16769 if (error)
16770 pos.coladd = 0;
16771#endif
16772
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016773 return &pos;
16774 }
16775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016776 name = get_tv_string_chk(varp);
16777 if (name == NULL)
16778 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779 if (name[0] == '.') /* cursor */
16780 return &curwin->w_cursor;
16781 if (name[0] == '\'') /* mark */
16782 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016783 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16785 return NULL;
16786 return pp;
16787 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016788
16789#ifdef FEAT_VIRTUALEDIT
16790 pos.coladd = 0;
16791#endif
16792
Bram Moolenaar477933c2007-07-17 14:32:23 +000016793 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016794 {
16795 pos.col = 0;
16796 if (name[1] == '0') /* "w0": first visible line */
16797 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016798 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016799 pos.lnum = curwin->w_topline;
16800 return &pos;
16801 }
16802 else if (name[1] == '$') /* "w$": last visible line */
16803 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016804 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016805 pos.lnum = curwin->w_botline - 1;
16806 return &pos;
16807 }
16808 }
16809 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016811 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 {
16813 pos.lnum = curbuf->b_ml.ml_line_count;
16814 pos.col = 0;
16815 }
16816 else
16817 {
16818 pos.lnum = curwin->w_cursor.lnum;
16819 pos.col = (colnr_T)STRLEN(ml_get_curline());
16820 }
16821 return &pos;
16822 }
16823 return NULL;
16824}
16825
16826/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016827 * Convert list in "arg" into a position and optional file number.
16828 * When "fnump" is NULL there is no file number, only 3 items.
16829 * Note that the column is passed on as-is, the caller may want to decrement
16830 * it to use 1 for the first column.
16831 * Return FAIL when conversion is not possible, doesn't check the position for
16832 * validity.
16833 */
16834 static int
16835list2fpos(arg, posp, fnump)
16836 typval_T *arg;
16837 pos_T *posp;
16838 int *fnump;
16839{
16840 list_T *l = arg->vval.v_list;
16841 long i = 0;
16842 long n;
16843
Bram Moolenaarbde35262006-07-23 20:12:24 +000016844 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16845 * when "fnump" isn't NULL and "coladd" is optional. */
16846 if (arg->v_type != VAR_LIST
16847 || l == NULL
16848 || l->lv_len < (fnump == NULL ? 2 : 3)
16849 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016850 return FAIL;
16851
16852 if (fnump != NULL)
16853 {
16854 n = list_find_nr(l, i++, NULL); /* fnum */
16855 if (n < 0)
16856 return FAIL;
16857 if (n == 0)
16858 n = curbuf->b_fnum; /* current buffer */
16859 *fnump = n;
16860 }
16861
16862 n = list_find_nr(l, i++, NULL); /* lnum */
16863 if (n < 0)
16864 return FAIL;
16865 posp->lnum = n;
16866
16867 n = list_find_nr(l, i++, NULL); /* col */
16868 if (n < 0)
16869 return FAIL;
16870 posp->col = n;
16871
16872#ifdef FEAT_VIRTUALEDIT
16873 n = list_find_nr(l, i, NULL);
16874 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016875 posp->coladd = 0;
16876 else
16877 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016878#endif
16879
16880 return OK;
16881}
16882
16883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 * Get the length of an environment variable name.
16885 * Advance "arg" to the first character after the name.
16886 * Return 0 for error.
16887 */
16888 static int
16889get_env_len(arg)
16890 char_u **arg;
16891{
16892 char_u *p;
16893 int len;
16894
16895 for (p = *arg; vim_isIDc(*p); ++p)
16896 ;
16897 if (p == *arg) /* no name found */
16898 return 0;
16899
16900 len = (int)(p - *arg);
16901 *arg = p;
16902 return len;
16903}
16904
16905/*
16906 * Get the length of the name of a function or internal variable.
16907 * "arg" is advanced to the first non-white character after the name.
16908 * Return 0 if something is wrong.
16909 */
16910 static int
16911get_id_len(arg)
16912 char_u **arg;
16913{
16914 char_u *p;
16915 int len;
16916
16917 /* Find the end of the name. */
16918 for (p = *arg; eval_isnamec(*p); ++p)
16919 ;
16920 if (p == *arg) /* no name found */
16921 return 0;
16922
16923 len = (int)(p - *arg);
16924 *arg = skipwhite(p);
16925
16926 return len;
16927}
16928
16929/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016930 * Get the length of the name of a variable or function.
16931 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016933 * Return -1 if curly braces expansion failed.
16934 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 * If the name contains 'magic' {}'s, expand them and return the
16936 * expanded name in an allocated string via 'alias' - caller must free.
16937 */
16938 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016939get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 char_u **arg;
16941 char_u **alias;
16942 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016943 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944{
16945 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 char_u *p;
16947 char_u *expr_start;
16948 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949
16950 *alias = NULL; /* default to no alias */
16951
16952 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16953 && (*arg)[2] == (int)KE_SNR)
16954 {
16955 /* hard coded <SNR>, already translated */
16956 *arg += 3;
16957 return get_id_len(arg) + 3;
16958 }
16959 len = eval_fname_script(*arg);
16960 if (len > 0)
16961 {
16962 /* literal "<SID>", "s:" or "<SNR>" */
16963 *arg += len;
16964 }
16965
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016967 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016969 p = find_name_end(*arg, &expr_start, &expr_end,
16970 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971 if (expr_start != NULL)
16972 {
16973 char_u *temp_string;
16974
16975 if (!evaluate)
16976 {
16977 len += (int)(p - *arg);
16978 *arg = skipwhite(p);
16979 return len;
16980 }
16981
16982 /*
16983 * Include any <SID> etc in the expanded string:
16984 * Thus the -len here.
16985 */
16986 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16987 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016988 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 *alias = temp_string;
16990 *arg = skipwhite(p);
16991 return (int)STRLEN(temp_string);
16992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993
16994 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016995 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 EMSG2(_(e_invexpr2), *arg);
16997
16998 return len;
16999}
17000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017001/*
17002 * Find the end of a variable or function name, taking care of magic braces.
17003 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17004 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017005 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017006 * Return a pointer to just after the name. Equal to "arg" if there is no
17007 * valid name.
17008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017010find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 char_u *arg;
17012 char_u **expr_start;
17013 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017014 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017016 int mb_nest = 0;
17017 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 char_u *p;
17019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017020 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 *expr_start = NULL;
17023 *expr_end = NULL;
17024 }
17025
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017026 /* Quick check for valid starting character. */
17027 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17028 return arg;
17029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017030 for (p = arg; *p != NUL
17031 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017032 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017033 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017035 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017036 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017037 if (*p == '\'')
17038 {
17039 /* skip over 'string' to avoid counting [ and ] inside it. */
17040 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17041 ;
17042 if (*p == NUL)
17043 break;
17044 }
17045 else if (*p == '"')
17046 {
17047 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17048 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17049 if (*p == '\\' && p[1] != NUL)
17050 ++p;
17051 if (*p == NUL)
17052 break;
17053 }
17054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017055 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017057 if (*p == '[')
17058 ++br_nest;
17059 else if (*p == ']')
17060 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017063 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017065 if (*p == '{')
17066 {
17067 mb_nest++;
17068 if (expr_start != NULL && *expr_start == NULL)
17069 *expr_start = p;
17070 }
17071 else if (*p == '}')
17072 {
17073 mb_nest--;
17074 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17075 *expr_end = p;
17076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 }
17079
17080 return p;
17081}
17082
17083/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017084 * Expands out the 'magic' {}'s in a variable/function name.
17085 * Note that this can call itself recursively, to deal with
17086 * constructs like foo{bar}{baz}{bam}
17087 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17088 * "in_start" ^
17089 * "expr_start" ^
17090 * "expr_end" ^
17091 * "in_end" ^
17092 *
17093 * Returns a new allocated string, which the caller must free.
17094 * Returns NULL for failure.
17095 */
17096 static char_u *
17097make_expanded_name(in_start, expr_start, expr_end, in_end)
17098 char_u *in_start;
17099 char_u *expr_start;
17100 char_u *expr_end;
17101 char_u *in_end;
17102{
17103 char_u c1;
17104 char_u *retval = NULL;
17105 char_u *temp_result;
17106 char_u *nextcmd = NULL;
17107
17108 if (expr_end == NULL || in_end == NULL)
17109 return NULL;
17110 *expr_start = NUL;
17111 *expr_end = NUL;
17112 c1 = *in_end;
17113 *in_end = NUL;
17114
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017115 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017116 if (temp_result != NULL && nextcmd == NULL)
17117 {
17118 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17119 + (in_end - expr_end) + 1));
17120 if (retval != NULL)
17121 {
17122 STRCPY(retval, in_start);
17123 STRCAT(retval, temp_result);
17124 STRCAT(retval, expr_end + 1);
17125 }
17126 }
17127 vim_free(temp_result);
17128
17129 *in_end = c1; /* put char back for error messages */
17130 *expr_start = '{';
17131 *expr_end = '}';
17132
17133 if (retval != NULL)
17134 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017135 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017136 if (expr_start != NULL)
17137 {
17138 /* Further expansion! */
17139 temp_result = make_expanded_name(retval, expr_start,
17140 expr_end, temp_result);
17141 vim_free(retval);
17142 retval = temp_result;
17143 }
17144 }
17145
17146 return retval;
17147}
17148
17149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017151 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 */
17153 static int
17154eval_isnamec(c)
17155 int c;
17156{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017157 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17158}
17159
17160/*
17161 * Return TRUE if character "c" can be used as the first character in a
17162 * variable or function name (excluding '{' and '}').
17163 */
17164 static int
17165eval_isnamec1(c)
17166 int c;
17167{
17168 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169}
17170
17171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 * Set number v: variable to "val".
17173 */
17174 void
17175set_vim_var_nr(idx, val)
17176 int idx;
17177 long val;
17178{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017179 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180}
17181
17182/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017183 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 */
17185 long
17186get_vim_var_nr(idx)
17187 int idx;
17188{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017189 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190}
17191
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017192#if defined(FEAT_AUTOCMD) || defined(PROTO)
17193/*
17194 * Get string v: variable value. Uses a static buffer, can only be used once.
17195 */
17196 char_u *
17197get_vim_var_str(idx)
17198 int idx;
17199{
17200 return get_tv_string(&vimvars[idx].vv_tv);
17201}
17202#endif
17203
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204/*
17205 * Set v:count, v:count1 and v:prevcount.
17206 */
17207 void
17208set_vcount(count, count1)
17209 long count;
17210 long count1;
17211{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017212 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17213 vimvars[VV_COUNT].vv_nr = count;
17214 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215}
17216
17217/*
17218 * Set string v: variable to a copy of "val".
17219 */
17220 void
17221set_vim_var_string(idx, val, len)
17222 int idx;
17223 char_u *val;
17224 int len; /* length of "val" to use or -1 (whole string) */
17225{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017226 /* Need to do this (at least) once, since we can't initialize a union.
17227 * Will always be invoked when "v:progname" is set. */
17228 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17229
Bram Moolenaare9a41262005-01-15 22:18:47 +000017230 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017232 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017234 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017236 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237}
17238
17239/*
17240 * Set v:register if needed.
17241 */
17242 void
17243set_reg_var(c)
17244 int c;
17245{
17246 char_u regname;
17247
17248 if (c == 0 || c == ' ')
17249 regname = '"';
17250 else
17251 regname = c;
17252 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017253 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254 set_vim_var_string(VV_REG, &regname, 1);
17255}
17256
17257/*
17258 * Get or set v:exception. If "oldval" == NULL, return the current value.
17259 * Otherwise, restore the value to "oldval" and return NULL.
17260 * Must always be called in pairs to save and restore v:exception! Does not
17261 * take care of memory allocations.
17262 */
17263 char_u *
17264v_exception(oldval)
17265 char_u *oldval;
17266{
17267 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017268 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269
Bram Moolenaare9a41262005-01-15 22:18:47 +000017270 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 return NULL;
17272}
17273
17274/*
17275 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17276 * Otherwise, restore the value to "oldval" and return NULL.
17277 * Must always be called in pairs to save and restore v:throwpoint! Does not
17278 * take care of memory allocations.
17279 */
17280 char_u *
17281v_throwpoint(oldval)
17282 char_u *oldval;
17283{
17284 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017285 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286
Bram Moolenaare9a41262005-01-15 22:18:47 +000017287 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 return NULL;
17289}
17290
17291#if defined(FEAT_AUTOCMD) || defined(PROTO)
17292/*
17293 * Set v:cmdarg.
17294 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17295 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17296 * Must always be called in pairs!
17297 */
17298 char_u *
17299set_cmdarg(eap, oldarg)
17300 exarg_T *eap;
17301 char_u *oldarg;
17302{
17303 char_u *oldval;
17304 char_u *newval;
17305 unsigned len;
17306
Bram Moolenaare9a41262005-01-15 22:18:47 +000017307 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017308 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017310 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017311 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017312 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 }
17314
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017315 if (eap->force_bin == FORCE_BIN)
17316 len = 6;
17317 else if (eap->force_bin == FORCE_NOBIN)
17318 len = 8;
17319 else
17320 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017321
17322 if (eap->read_edit)
17323 len += 7;
17324
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017325 if (eap->force_ff != 0)
17326 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17327# ifdef FEAT_MBYTE
17328 if (eap->force_enc != 0)
17329 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017330 if (eap->bad_char != 0)
17331 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017332# endif
17333
17334 newval = alloc(len + 1);
17335 if (newval == NULL)
17336 return NULL;
17337
17338 if (eap->force_bin == FORCE_BIN)
17339 sprintf((char *)newval, " ++bin");
17340 else if (eap->force_bin == FORCE_NOBIN)
17341 sprintf((char *)newval, " ++nobin");
17342 else
17343 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017344
17345 if (eap->read_edit)
17346 STRCAT(newval, " ++edit");
17347
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017348 if (eap->force_ff != 0)
17349 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17350 eap->cmd + eap->force_ff);
17351# ifdef FEAT_MBYTE
17352 if (eap->force_enc != 0)
17353 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17354 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017355 if (eap->bad_char != 0)
17356 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17357 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017358# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017359 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017360 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361}
17362#endif
17363
17364/*
17365 * Get the value of internal variable "name".
17366 * Return OK or FAIL.
17367 */
17368 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017369get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 char_u *name;
17371 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017372 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017373 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374{
17375 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017376 typval_T *tv = NULL;
17377 typval_T atv;
17378 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380
17381 /* truncate the name, so that we can use strcmp() */
17382 cc = name[len];
17383 name[len] = NUL;
17384
17385 /*
17386 * Check for "b:changedtick".
17387 */
17388 if (STRCMP(name, "b:changedtick") == 0)
17389 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017390 atv.v_type = VAR_NUMBER;
17391 atv.vval.v_number = curbuf->b_changedtick;
17392 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 }
17394
17395 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 * Check for user-defined variables.
17397 */
17398 else
17399 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017400 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 }
17404
Bram Moolenaare9a41262005-01-15 22:18:47 +000017405 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017407 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017408 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 ret = FAIL;
17410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017411 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017412 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413
17414 name[len] = cc;
17415
17416 return ret;
17417}
17418
17419/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017420 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17421 * Also handle function call with Funcref variable: func(expr)
17422 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17423 */
17424 static int
17425handle_subscript(arg, rettv, evaluate, verbose)
17426 char_u **arg;
17427 typval_T *rettv;
17428 int evaluate; /* do more than finding the end */
17429 int verbose; /* give error messages */
17430{
17431 int ret = OK;
17432 dict_T *selfdict = NULL;
17433 char_u *s;
17434 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017435 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017436
17437 while (ret == OK
17438 && (**arg == '['
17439 || (**arg == '.' && rettv->v_type == VAR_DICT)
17440 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17441 && !vim_iswhite(*(*arg - 1)))
17442 {
17443 if (**arg == '(')
17444 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017445 /* need to copy the funcref so that we can clear rettv */
17446 functv = *rettv;
17447 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017448
17449 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017450 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017451 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017452 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17453 &len, evaluate, selfdict);
17454
17455 /* Clear the funcref afterwards, so that deleting it while
17456 * evaluating the arguments is possible (see test55). */
17457 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017458
17459 /* Stop the expression evaluation when immediately aborting on
17460 * error, or when an interrupt occurred or an exception was thrown
17461 * but not caught. */
17462 if (aborting())
17463 {
17464 if (ret == OK)
17465 clear_tv(rettv);
17466 ret = FAIL;
17467 }
17468 dict_unref(selfdict);
17469 selfdict = NULL;
17470 }
17471 else /* **arg == '[' || **arg == '.' */
17472 {
17473 dict_unref(selfdict);
17474 if (rettv->v_type == VAR_DICT)
17475 {
17476 selfdict = rettv->vval.v_dict;
17477 if (selfdict != NULL)
17478 ++selfdict->dv_refcount;
17479 }
17480 else
17481 selfdict = NULL;
17482 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17483 {
17484 clear_tv(rettv);
17485 ret = FAIL;
17486 }
17487 }
17488 }
17489 dict_unref(selfdict);
17490 return ret;
17491}
17492
17493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017494 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17495 * value).
17496 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017497 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017498alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017499{
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017501}
17502
17503/*
17504 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 * The string "s" must have been allocated, it is consumed.
17506 * Return NULL for out of memory, the variable otherwise.
17507 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017508 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017509alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 char_u *s;
17511{
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017514 rettv = alloc_tv();
17515 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017517 rettv->v_type = VAR_STRING;
17518 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 }
17520 else
17521 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017522 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523}
17524
17525/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017526 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017528 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017529free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017530 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531{
17532 if (varp != NULL)
17533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017534 switch (varp->v_type)
17535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017536 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017537 func_unref(varp->vval.v_string);
17538 /*FALLTHROUGH*/
17539 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017540 vim_free(varp->vval.v_string);
17541 break;
17542 case VAR_LIST:
17543 list_unref(varp->vval.v_list);
17544 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017545 case VAR_DICT:
17546 dict_unref(varp->vval.v_dict);
17547 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017548 case VAR_NUMBER:
17549 case VAR_UNKNOWN:
17550 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017551 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017552 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017553 break;
17554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 vim_free(varp);
17556 }
17557}
17558
17559/*
17560 * Free the memory for a variable value and set the value to NULL or 0.
17561 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017562 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017563clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565{
17566 if (varp != NULL)
17567 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017568 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017570 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017571 func_unref(varp->vval.v_string);
17572 /*FALLTHROUGH*/
17573 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017574 vim_free(varp->vval.v_string);
17575 varp->vval.v_string = NULL;
17576 break;
17577 case VAR_LIST:
17578 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017579 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017580 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017581 case VAR_DICT:
17582 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017583 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017584 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017585 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017586 varp->vval.v_number = 0;
17587 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017588 case VAR_UNKNOWN:
17589 break;
17590 default:
17591 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017593 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 }
17595}
17596
17597/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017598 * Set the value of a variable to NULL without freeing items.
17599 */
17600 static void
17601init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017602 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017603{
17604 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017605 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017606}
17607
17608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 * Get the number value of a variable.
17610 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017611 * For incompatible types, return 0.
17612 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17613 * caller of incompatible types: it sets *denote to TRUE if "denote"
17614 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 */
17616 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017617get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017618 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017620 int error = FALSE;
17621
17622 return get_tv_number_chk(varp, &error); /* return 0L on error */
17623}
17624
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017625 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017626get_tv_number_chk(varp, denote)
17627 typval_T *varp;
17628 int *denote;
17629{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017630 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017632 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017634 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017635 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017636 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017637 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017638 break;
17639 case VAR_STRING:
17640 if (varp->vval.v_string != NULL)
17641 vim_str2nr(varp->vval.v_string, NULL, NULL,
17642 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017643 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017644 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017645 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017646 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017647 case VAR_DICT:
17648 EMSG(_("E728: Using a Dictionary as a number"));
17649 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017650 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017651 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017652 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017654 if (denote == NULL) /* useful for values that must be unsigned */
17655 n = -1;
17656 else
17657 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017658 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659}
17660
17661/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017662 * Get the lnum from the first argument.
17663 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017664 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 */
17666 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017667get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017668 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669{
Bram Moolenaar33570922005-01-25 22:26:29 +000017670 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 linenr_T lnum;
17672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017673 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 if (lnum == 0) /* no valid number, try using line() */
17675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676 rettv.v_type = VAR_NUMBER;
17677 f_line(argvars, &rettv);
17678 lnum = rettv.vval.v_number;
17679 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 }
17681 return lnum;
17682}
17683
17684/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017685 * Get the lnum from the first argument.
17686 * Also accepts "$", then "buf" is used.
17687 * Returns 0 on error.
17688 */
17689 static linenr_T
17690get_tv_lnum_buf(argvars, buf)
17691 typval_T *argvars;
17692 buf_T *buf;
17693{
17694 if (argvars[0].v_type == VAR_STRING
17695 && argvars[0].vval.v_string != NULL
17696 && argvars[0].vval.v_string[0] == '$'
17697 && buf != NULL)
17698 return buf->b_ml.ml_line_count;
17699 return get_tv_number_chk(&argvars[0], NULL);
17700}
17701
17702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 * Get the string value of a variable.
17704 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017705 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17706 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 * If the String variable has never been set, return an empty string.
17708 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017709 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17710 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 */
17712 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017714 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715{
17716 static char_u mybuf[NUMBUFLEN];
17717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017718 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719}
17720
17721 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017722get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017723 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017724 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017726 char_u *res = get_tv_string_buf_chk(varp, buf);
17727
17728 return res != NULL ? res : (char_u *)"";
17729}
17730
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017731 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017732get_tv_string_chk(varp)
17733 typval_T *varp;
17734{
17735 static char_u mybuf[NUMBUFLEN];
17736
17737 return get_tv_string_buf_chk(varp, mybuf);
17738}
17739
17740 static char_u *
17741get_tv_string_buf_chk(varp, buf)
17742 typval_T *varp;
17743 char_u *buf;
17744{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017745 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017747 case VAR_NUMBER:
17748 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17749 return buf;
17750 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017751 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017752 break;
17753 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017754 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017755 break;
17756 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017757 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017758 break;
17759 case VAR_STRING:
17760 if (varp->vval.v_string != NULL)
17761 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017762 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017763 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017764 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017767 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768}
17769
17770/*
17771 * Find variable "name" in the list of variables.
17772 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017773 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017774 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017777 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017778find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017780 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017783 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784
Bram Moolenaara7043832005-01-21 11:56:39 +000017785 ht = find_var_ht(name, &varname);
17786 if (htp != NULL)
17787 *htp = ht;
17788 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017790 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791}
17792
17793/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017794 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017795 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017797 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017798find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017799 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017800 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017801 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017802{
Bram Moolenaar33570922005-01-25 22:26:29 +000017803 hashitem_T *hi;
17804
17805 if (*varname == NUL)
17806 {
17807 /* Must be something like "s:", otherwise "ht" would be NULL. */
17808 switch (varname[-2])
17809 {
17810 case 's': return &SCRIPT_SV(current_SID).sv_var;
17811 case 'g': return &globvars_var;
17812 case 'v': return &vimvars_var;
17813 case 'b': return &curbuf->b_bufvar;
17814 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017815#ifdef FEAT_WINDOWS
17816 case 't': return &curtab->tp_winvar;
17817#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017818 case 'l': return current_funccal == NULL
17819 ? NULL : &current_funccal->l_vars_var;
17820 case 'a': return current_funccal == NULL
17821 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017822 }
17823 return NULL;
17824 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017825
17826 hi = hash_find(ht, varname);
17827 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017828 {
17829 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017830 * worked find the variable again. Don't auto-load a script if it was
17831 * loaded already, otherwise it would be loaded every time when
17832 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017833 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017834 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017835 hi = hash_find(ht, varname);
17836 if (HASHITEM_EMPTY(hi))
17837 return NULL;
17838 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017839 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017840}
17841
17842/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017843 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017844 * Set "varname" to the start of name without ':'.
17845 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017846 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017847find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 char_u *name;
17849 char_u **varname;
17850{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017851 hashitem_T *hi;
17852
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853 if (name[1] != ':')
17854 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017855 /* The name must not start with a colon or #. */
17856 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 return NULL;
17858 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017859
17860 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017861 hi = hash_find(&compat_hashtab, name);
17862 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017863 return &compat_hashtab;
17864
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017866 return &globvarht; /* global variable */
17867 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 }
17869 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017870 if (*name == 'g') /* global variable */
17871 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017872 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17873 */
17874 if (vim_strchr(name + 2, ':') != NULL
17875 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017876 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017878 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017880 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017881#ifdef FEAT_WINDOWS
17882 if (*name == 't') /* tab page variable */
17883 return &curtab->tp_vars.dv_hashtab;
17884#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017885 if (*name == 'v') /* v: variable */
17886 return &vimvarht;
17887 if (*name == 'a' && current_funccal != NULL) /* function argument */
17888 return &current_funccal->l_avars.dv_hashtab;
17889 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17890 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 if (*name == 's' /* script variable */
17892 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17893 return &SCRIPT_VARS(current_SID);
17894 return NULL;
17895}
17896
17897/*
17898 * Get the string value of a (global/local) variable.
17899 * Returns NULL when it doesn't exist.
17900 */
17901 char_u *
17902get_var_value(name)
17903 char_u *name;
17904{
Bram Moolenaar33570922005-01-25 22:26:29 +000017905 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906
Bram Moolenaara7043832005-01-21 11:56:39 +000017907 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 if (v == NULL)
17909 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017910 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911}
17912
17913/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017914 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 * sourcing this script and when executing functions defined in the script.
17916 */
17917 void
17918new_script_vars(id)
17919 scid_T id;
17920{
Bram Moolenaara7043832005-01-21 11:56:39 +000017921 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017922 hashtab_T *ht;
17923 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017924
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17926 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017927 /* Re-allocating ga_data means that an ht_array pointing to
17928 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017929 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017930 for (i = 1; i <= ga_scripts.ga_len; ++i)
17931 {
17932 ht = &SCRIPT_VARS(i);
17933 if (ht->ht_mask == HT_INIT_SIZE - 1)
17934 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017935 sv = &SCRIPT_SV(i);
17936 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017937 }
17938
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 while (ga_scripts.ga_len < id)
17940 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017941 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17942 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 }
17945 }
17946}
17947
17948/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017949 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17950 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 */
17952 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017953init_var_dict(dict, dict_var)
17954 dict_T *dict;
17955 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956{
Bram Moolenaar33570922005-01-25 22:26:29 +000017957 hash_init(&dict->dv_hashtab);
17958 dict->dv_refcount = 99999;
17959 dict_var->di_tv.vval.v_dict = dict;
17960 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017961 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017962 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17963 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964}
17965
17966/*
17967 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017968 * Frees all allocated variables and the value they contain.
17969 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 */
17971 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017972vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017973 hashtab_T *ht;
17974{
17975 vars_clear_ext(ht, TRUE);
17976}
17977
17978/*
17979 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17980 */
17981 static void
17982vars_clear_ext(ht, free_val)
17983 hashtab_T *ht;
17984 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985{
Bram Moolenaara7043832005-01-21 11:56:39 +000017986 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017987 hashitem_T *hi;
17988 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989
Bram Moolenaar33570922005-01-25 22:26:29 +000017990 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017991 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017992 for (hi = ht->ht_array; todo > 0; ++hi)
17993 {
17994 if (!HASHITEM_EMPTY(hi))
17995 {
17996 --todo;
17997
Bram Moolenaar33570922005-01-25 22:26:29 +000017998 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017999 * ht_array might change then. hash_clear() takes care of it
18000 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018001 v = HI2DI(hi);
18002 if (free_val)
18003 clear_tv(&v->di_tv);
18004 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18005 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018006 }
18007 }
18008 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018009 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010}
18011
Bram Moolenaara7043832005-01-21 11:56:39 +000018012/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018013 * Delete a variable from hashtab "ht" at item "hi".
18014 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018017delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 hashtab_T *ht;
18019 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020{
Bram Moolenaar33570922005-01-25 22:26:29 +000018021 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018022
18023 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018024 clear_tv(&di->di_tv);
18025 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026}
18027
18028/*
18029 * List the value of one internal variable.
18030 */
18031 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018032list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018033 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018035 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018037 char_u *tofree;
18038 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018039 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018040
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018041 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018042 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018043 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018044 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045}
18046
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018048list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 char_u *prefix;
18050 char_u *name;
18051 int type;
18052 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018053 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054{
Bram Moolenaar31859182007-08-14 20:41:13 +000018055 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18056 msg_start();
18057 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 if (name != NULL) /* "a:" vars don't have a name stored */
18059 msg_puts(name);
18060 msg_putchar(' ');
18061 msg_advance(22);
18062 if (type == VAR_NUMBER)
18063 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018064 else if (type == VAR_FUNC)
18065 msg_putchar('*');
18066 else if (type == VAR_LIST)
18067 {
18068 msg_putchar('[');
18069 if (*string == '[')
18070 ++string;
18071 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018072 else if (type == VAR_DICT)
18073 {
18074 msg_putchar('{');
18075 if (*string == '{')
18076 ++string;
18077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 else
18079 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018080
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018082
18083 if (type == VAR_FUNC)
18084 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018085 if (*first)
18086 {
18087 msg_clr_eos();
18088 *first = FALSE;
18089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090}
18091
18092/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018093 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 * If the variable already exists, the value is updated.
18095 * Otherwise the variable is created.
18096 */
18097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018098set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018101 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102{
Bram Moolenaar33570922005-01-25 22:26:29 +000018103 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018105 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018106 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018108 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018109 {
18110 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18111 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18112 ? name[2] : name[0]))
18113 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018114 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018115 return;
18116 }
18117 if (function_exists(name))
18118 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018119 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018120 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018121 return;
18122 }
18123 }
18124
Bram Moolenaara7043832005-01-21 11:56:39 +000018125 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018126 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018127 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018128 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018129 return;
18130 }
18131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018132 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018133 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018135 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018136 if (var_check_ro(v->di_flags, name)
18137 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018138 return;
18139 if (v->di_tv.v_type != tv->v_type
18140 && !((v->di_tv.v_type == VAR_STRING
18141 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018142 && (tv->v_type == VAR_STRING
18143 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018144 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018145 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018146 return;
18147 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018148
18149 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018150 * Handle setting internal v: variables separately: we don't change
18151 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018152 */
18153 if (ht == &vimvarht)
18154 {
18155 if (v->di_tv.v_type == VAR_STRING)
18156 {
18157 vim_free(v->di_tv.vval.v_string);
18158 if (copy || tv->v_type != VAR_STRING)
18159 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18160 else
18161 {
18162 /* Take over the string to avoid an extra alloc/free. */
18163 v->di_tv.vval.v_string = tv->vval.v_string;
18164 tv->vval.v_string = NULL;
18165 }
18166 }
18167 else if (v->di_tv.v_type != VAR_NUMBER)
18168 EMSG2(_(e_intern2), "set_var()");
18169 else
18170 v->di_tv.vval.v_number = get_tv_number(tv);
18171 return;
18172 }
18173
18174 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 }
18176 else /* add a new variable */
18177 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018178 /* Can't add "v:" variable. */
18179 if (ht == &vimvarht)
18180 {
18181 EMSG2(_(e_illvar), name);
18182 return;
18183 }
18184
Bram Moolenaar92124a32005-06-17 22:03:40 +000018185 /* Make sure the variable name is valid. */
18186 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018187 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18188 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018189 {
18190 EMSG2(_(e_illvar), varname);
18191 return;
18192 }
18193
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018194 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18195 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018196 if (v == NULL)
18197 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018198 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018199 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018201 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 return;
18203 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018204 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018207 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018208 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018209 else
18210 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018211 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018212 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018213 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215}
18216
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018217/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018218 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018219 * Also give an error message.
18220 */
18221 static int
18222var_check_ro(flags, name)
18223 int flags;
18224 char_u *name;
18225{
18226 if (flags & DI_FLAGS_RO)
18227 {
18228 EMSG2(_(e_readonlyvar), name);
18229 return TRUE;
18230 }
18231 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18232 {
18233 EMSG2(_(e_readonlysbx), name);
18234 return TRUE;
18235 }
18236 return FALSE;
18237}
18238
18239/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018240 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18241 * Also give an error message.
18242 */
18243 static int
18244var_check_fixed(flags, name)
18245 int flags;
18246 char_u *name;
18247{
18248 if (flags & DI_FLAGS_FIX)
18249 {
18250 EMSG2(_("E795: Cannot delete variable %s"), name);
18251 return TRUE;
18252 }
18253 return FALSE;
18254}
18255
18256/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018257 * Return TRUE if typeval "tv" is set to be locked (immutable).
18258 * Also give an error message, using "name".
18259 */
18260 static int
18261tv_check_lock(lock, name)
18262 int lock;
18263 char_u *name;
18264{
18265 if (lock & VAR_LOCKED)
18266 {
18267 EMSG2(_("E741: Value is locked: %s"),
18268 name == NULL ? (char_u *)_("Unknown") : name);
18269 return TRUE;
18270 }
18271 if (lock & VAR_FIXED)
18272 {
18273 EMSG2(_("E742: Cannot change value of %s"),
18274 name == NULL ? (char_u *)_("Unknown") : name);
18275 return TRUE;
18276 }
18277 return FALSE;
18278}
18279
18280/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018281 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018282 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018283 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018286copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018287 typval_T *from;
18288 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018290 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018291 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018292 switch (from->v_type)
18293 {
18294 case VAR_NUMBER:
18295 to->vval.v_number = from->vval.v_number;
18296 break;
18297 case VAR_STRING:
18298 case VAR_FUNC:
18299 if (from->vval.v_string == NULL)
18300 to->vval.v_string = NULL;
18301 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018302 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018303 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018304 if (from->v_type == VAR_FUNC)
18305 func_ref(to->vval.v_string);
18306 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018307 break;
18308 case VAR_LIST:
18309 if (from->vval.v_list == NULL)
18310 to->vval.v_list = NULL;
18311 else
18312 {
18313 to->vval.v_list = from->vval.v_list;
18314 ++to->vval.v_list->lv_refcount;
18315 }
18316 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018317 case VAR_DICT:
18318 if (from->vval.v_dict == NULL)
18319 to->vval.v_dict = NULL;
18320 else
18321 {
18322 to->vval.v_dict = from->vval.v_dict;
18323 ++to->vval.v_dict->dv_refcount;
18324 }
18325 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018326 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018327 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018328 break;
18329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330}
18331
18332/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018333 * Make a copy of an item.
18334 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018335 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18336 * reference to an already copied list/dict can be used.
18337 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018338 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018339 static int
18340item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018341 typval_T *from;
18342 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018343 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018344 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018345{
18346 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018347 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018348
Bram Moolenaar33570922005-01-25 22:26:29 +000018349 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018350 {
18351 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018352 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018353 }
18354 ++recurse;
18355
18356 switch (from->v_type)
18357 {
18358 case VAR_NUMBER:
18359 case VAR_STRING:
18360 case VAR_FUNC:
18361 copy_tv(from, to);
18362 break;
18363 case VAR_LIST:
18364 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018365 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018366 if (from->vval.v_list == NULL)
18367 to->vval.v_list = NULL;
18368 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18369 {
18370 /* use the copy made earlier */
18371 to->vval.v_list = from->vval.v_list->lv_copylist;
18372 ++to->vval.v_list->lv_refcount;
18373 }
18374 else
18375 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18376 if (to->vval.v_list == NULL)
18377 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018378 break;
18379 case VAR_DICT:
18380 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018381 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018382 if (from->vval.v_dict == NULL)
18383 to->vval.v_dict = NULL;
18384 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18385 {
18386 /* use the copy made earlier */
18387 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18388 ++to->vval.v_dict->dv_refcount;
18389 }
18390 else
18391 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18392 if (to->vval.v_dict == NULL)
18393 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018394 break;
18395 default:
18396 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018397 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018398 }
18399 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018400 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018401}
18402
18403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 * ":echo expr1 ..." print each argument separated with a space, add a
18405 * newline at the end.
18406 * ":echon expr1 ..." print each argument plain.
18407 */
18408 void
18409ex_echo(eap)
18410 exarg_T *eap;
18411{
18412 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018413 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018414 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 char_u *p;
18416 int needclr = TRUE;
18417 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419
18420 if (eap->skip)
18421 ++emsg_skip;
18422 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18423 {
18424 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018425 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426 {
18427 /*
18428 * Report the invalid expression unless the expression evaluation
18429 * has been cancelled due to an aborting error, an interrupt, or an
18430 * exception.
18431 */
18432 if (!aborting())
18433 EMSG2(_(e_invexpr2), p);
18434 break;
18435 }
18436 if (!eap->skip)
18437 {
18438 if (atstart)
18439 {
18440 atstart = FALSE;
18441 /* Call msg_start() after eval1(), evaluating the expression
18442 * may cause a message to appear. */
18443 if (eap->cmdidx == CMD_echo)
18444 msg_start();
18445 }
18446 else if (eap->cmdidx == CMD_echo)
18447 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018448 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018449 if (p != NULL)
18450 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018452 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018454 if (*p != TAB && needclr)
18455 {
18456 /* remove any text still there from the command */
18457 msg_clr_eos();
18458 needclr = FALSE;
18459 }
18460 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 }
18462 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018463 {
18464#ifdef FEAT_MBYTE
18465 if (has_mbyte)
18466 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018467 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018468
18469 (void)msg_outtrans_len_attr(p, i, echo_attr);
18470 p += i - 1;
18471 }
18472 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018474 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018477 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 arg = skipwhite(arg);
18481 }
18482 eap->nextcmd = check_nextcmd(arg);
18483
18484 if (eap->skip)
18485 --emsg_skip;
18486 else
18487 {
18488 /* remove text that may still be there from the command */
18489 if (needclr)
18490 msg_clr_eos();
18491 if (eap->cmdidx == CMD_echo)
18492 msg_end();
18493 }
18494}
18495
18496/*
18497 * ":echohl {name}".
18498 */
18499 void
18500ex_echohl(eap)
18501 exarg_T *eap;
18502{
18503 int id;
18504
18505 id = syn_name2id(eap->arg);
18506 if (id == 0)
18507 echo_attr = 0;
18508 else
18509 echo_attr = syn_id2attr(id);
18510}
18511
18512/*
18513 * ":execute expr1 ..." execute the result of an expression.
18514 * ":echomsg expr1 ..." Print a message
18515 * ":echoerr expr1 ..." Print an error
18516 * Each gets spaces around each argument and a newline at the end for
18517 * echo commands
18518 */
18519 void
18520ex_execute(eap)
18521 exarg_T *eap;
18522{
18523 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018524 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 int ret = OK;
18526 char_u *p;
18527 garray_T ga;
18528 int len;
18529 int save_did_emsg;
18530
18531 ga_init2(&ga, 1, 80);
18532
18533 if (eap->skip)
18534 ++emsg_skip;
18535 while (*arg != NUL && *arg != '|' && *arg != '\n')
18536 {
18537 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018538 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 {
18540 /*
18541 * Report the invalid expression unless the expression evaluation
18542 * has been cancelled due to an aborting error, an interrupt, or an
18543 * exception.
18544 */
18545 if (!aborting())
18546 EMSG2(_(e_invexpr2), p);
18547 ret = FAIL;
18548 break;
18549 }
18550
18551 if (!eap->skip)
18552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018553 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 len = (int)STRLEN(p);
18555 if (ga_grow(&ga, len + 2) == FAIL)
18556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018557 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 ret = FAIL;
18559 break;
18560 }
18561 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 ga.ga_len += len;
18565 }
18566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018567 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 arg = skipwhite(arg);
18569 }
18570
18571 if (ret != FAIL && ga.ga_data != NULL)
18572 {
18573 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018576 out_flush();
18577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 else if (eap->cmdidx == CMD_echoerr)
18579 {
18580 /* We don't want to abort following commands, restore did_emsg. */
18581 save_did_emsg = did_emsg;
18582 EMSG((char_u *)ga.ga_data);
18583 if (!force_abort)
18584 did_emsg = save_did_emsg;
18585 }
18586 else if (eap->cmdidx == CMD_execute)
18587 do_cmdline((char_u *)ga.ga_data,
18588 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18589 }
18590
18591 ga_clear(&ga);
18592
18593 if (eap->skip)
18594 --emsg_skip;
18595
18596 eap->nextcmd = check_nextcmd(arg);
18597}
18598
18599/*
18600 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18601 * "arg" points to the "&" or '+' when called, to "option" when returning.
18602 * Returns NULL when no option name found. Otherwise pointer to the char
18603 * after the option name.
18604 */
18605 static char_u *
18606find_option_end(arg, opt_flags)
18607 char_u **arg;
18608 int *opt_flags;
18609{
18610 char_u *p = *arg;
18611
18612 ++p;
18613 if (*p == 'g' && p[1] == ':')
18614 {
18615 *opt_flags = OPT_GLOBAL;
18616 p += 2;
18617 }
18618 else if (*p == 'l' && p[1] == ':')
18619 {
18620 *opt_flags = OPT_LOCAL;
18621 p += 2;
18622 }
18623 else
18624 *opt_flags = 0;
18625
18626 if (!ASCII_ISALPHA(*p))
18627 return NULL;
18628 *arg = p;
18629
18630 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18631 p += 4; /* termcap option */
18632 else
18633 while (ASCII_ISALPHA(*p))
18634 ++p;
18635 return p;
18636}
18637
18638/*
18639 * ":function"
18640 */
18641 void
18642ex_function(eap)
18643 exarg_T *eap;
18644{
18645 char_u *theline;
18646 int j;
18647 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 char_u *name = NULL;
18650 char_u *p;
18651 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018652 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 garray_T newargs;
18654 garray_T newlines;
18655 int varargs = FALSE;
18656 int mustend = FALSE;
18657 int flags = 0;
18658 ufunc_T *fp;
18659 int indent;
18660 int nesting;
18661 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018662 dictitem_T *v;
18663 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018664 static int func_nr = 0; /* number for nameless function */
18665 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018666 hashtab_T *ht;
18667 int todo;
18668 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018669 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670
18671 /*
18672 * ":function" without argument: list functions.
18673 */
18674 if (ends_excmd(*eap->arg))
18675 {
18676 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018677 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018678 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018679 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018680 {
18681 if (!HASHITEM_EMPTY(hi))
18682 {
18683 --todo;
18684 fp = HI2UF(hi);
18685 if (!isdigit(*fp->uf_name))
18686 list_func_head(fp, FALSE);
18687 }
18688 }
18689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 eap->nextcmd = check_nextcmd(eap->arg);
18691 return;
18692 }
18693
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018694 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018695 * ":function /pat": list functions matching pattern.
18696 */
18697 if (*eap->arg == '/')
18698 {
18699 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18700 if (!eap->skip)
18701 {
18702 regmatch_T regmatch;
18703
18704 c = *p;
18705 *p = NUL;
18706 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18707 *p = c;
18708 if (regmatch.regprog != NULL)
18709 {
18710 regmatch.rm_ic = p_ic;
18711
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018712 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018713 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18714 {
18715 if (!HASHITEM_EMPTY(hi))
18716 {
18717 --todo;
18718 fp = HI2UF(hi);
18719 if (!isdigit(*fp->uf_name)
18720 && vim_regexec(&regmatch, fp->uf_name, 0))
18721 list_func_head(fp, FALSE);
18722 }
18723 }
18724 }
18725 }
18726 if (*p == '/')
18727 ++p;
18728 eap->nextcmd = check_nextcmd(p);
18729 return;
18730 }
18731
18732 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018733 * Get the function name. There are these situations:
18734 * func normal function name
18735 * "name" == func, "fudi.fd_dict" == NULL
18736 * dict.func new dictionary entry
18737 * "name" == NULL, "fudi.fd_dict" set,
18738 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18739 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018740 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018741 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18742 * dict.func existing dict entry that's not a Funcref
18743 * "name" == NULL, "fudi.fd_dict" set,
18744 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018747 name = trans_function_name(&p, eap->skip, 0, &fudi);
18748 paren = (vim_strchr(p, '(') != NULL);
18749 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 {
18751 /*
18752 * Return on an invalid expression in braces, unless the expression
18753 * evaluation has been cancelled due to an aborting error, an
18754 * interrupt, or an exception.
18755 */
18756 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018757 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018758 if (!eap->skip && fudi.fd_newkey != NULL)
18759 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018760 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 else
18764 eap->skip = TRUE;
18765 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018766
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 /* An error in a function call during evaluation of an expression in magic
18768 * braces should not cause the function not to be defined. */
18769 saved_did_emsg = did_emsg;
18770 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771
18772 /*
18773 * ":function func" with only function name: list function.
18774 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018775 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 {
18777 if (!ends_excmd(*skipwhite(p)))
18778 {
18779 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018780 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 }
18782 eap->nextcmd = check_nextcmd(p);
18783 if (eap->nextcmd != NULL)
18784 *p = NUL;
18785 if (!eap->skip && !got_int)
18786 {
18787 fp = find_func(name);
18788 if (fp != NULL)
18789 {
18790 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018791 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018793 if (FUNCLINE(fp, j) == NULL)
18794 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 msg_putchar('\n');
18796 msg_outnum((long)(j + 1));
18797 if (j < 9)
18798 msg_putchar(' ');
18799 if (j < 99)
18800 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018801 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802 out_flush(); /* show a line at a time */
18803 ui_breakcheck();
18804 }
18805 if (!got_int)
18806 {
18807 msg_putchar('\n');
18808 msg_puts((char_u *)" endfunction");
18809 }
18810 }
18811 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018812 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018814 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815 }
18816
18817 /*
18818 * ":function name(arg1, arg2)" Define function.
18819 */
18820 p = skipwhite(p);
18821 if (*p != '(')
18822 {
18823 if (!eap->skip)
18824 {
18825 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018826 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 }
18828 /* attempt to continue by skipping some text */
18829 if (vim_strchr(p, '(') != NULL)
18830 p = vim_strchr(p, '(');
18831 }
18832 p = skipwhite(p + 1);
18833
18834 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18835 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18836
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018837 if (!eap->skip)
18838 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018839 /* Check the name of the function. Unless it's a dictionary function
18840 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018841 if (name != NULL)
18842 arg = name;
18843 else
18844 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018845 if (arg != NULL && (fudi.fd_di == NULL
18846 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018847 {
18848 if (*arg == K_SPECIAL)
18849 j = 3;
18850 else
18851 j = 0;
18852 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18853 : eval_isnamec(arg[j])))
18854 ++j;
18855 if (arg[j] != NUL)
18856 emsg_funcname(_(e_invarg2), arg);
18857 }
18858 }
18859
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 /*
18861 * Isolate the arguments: "arg1, arg2, ...)"
18862 */
18863 while (*p != ')')
18864 {
18865 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18866 {
18867 varargs = TRUE;
18868 p += 3;
18869 mustend = TRUE;
18870 }
18871 else
18872 {
18873 arg = p;
18874 while (ASCII_ISALNUM(*p) || *p == '_')
18875 ++p;
18876 if (arg == p || isdigit(*arg)
18877 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18878 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18879 {
18880 if (!eap->skip)
18881 EMSG2(_("E125: Illegal argument: %s"), arg);
18882 break;
18883 }
18884 if (ga_grow(&newargs, 1) == FAIL)
18885 goto erret;
18886 c = *p;
18887 *p = NUL;
18888 arg = vim_strsave(arg);
18889 if (arg == NULL)
18890 goto erret;
18891 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18892 *p = c;
18893 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894 if (*p == ',')
18895 ++p;
18896 else
18897 mustend = TRUE;
18898 }
18899 p = skipwhite(p);
18900 if (mustend && *p != ')')
18901 {
18902 if (!eap->skip)
18903 EMSG2(_(e_invarg2), eap->arg);
18904 break;
18905 }
18906 }
18907 ++p; /* skip the ')' */
18908
Bram Moolenaare9a41262005-01-15 22:18:47 +000018909 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 for (;;)
18911 {
18912 p = skipwhite(p);
18913 if (STRNCMP(p, "range", 5) == 0)
18914 {
18915 flags |= FC_RANGE;
18916 p += 5;
18917 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018918 else if (STRNCMP(p, "dict", 4) == 0)
18919 {
18920 flags |= FC_DICT;
18921 p += 4;
18922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 else if (STRNCMP(p, "abort", 5) == 0)
18924 {
18925 flags |= FC_ABORT;
18926 p += 5;
18927 }
18928 else
18929 break;
18930 }
18931
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018932 /* When there is a line break use what follows for the function body.
18933 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18934 if (*p == '\n')
18935 line_arg = p + 1;
18936 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 EMSG(_(e_trailing));
18938
18939 /*
18940 * Read the body of the function, until ":endfunction" is found.
18941 */
18942 if (KeyTyped)
18943 {
18944 /* Check if the function already exists, don't let the user type the
18945 * whole function before telling him it doesn't work! For a script we
18946 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018947 if (!eap->skip && !eap->forceit)
18948 {
18949 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18950 EMSG(_(e_funcdict));
18951 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018952 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018955 if (!eap->skip && did_emsg)
18956 goto erret;
18957
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 msg_putchar('\n'); /* don't overwrite the function name */
18959 cmdline_row = msg_row;
18960 }
18961
18962 indent = 2;
18963 nesting = 0;
18964 for (;;)
18965 {
18966 msg_scroll = TRUE;
18967 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018968 sourcing_lnum_off = sourcing_lnum;
18969
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018970 if (line_arg != NULL)
18971 {
18972 /* Use eap->arg, split up in parts by line breaks. */
18973 theline = line_arg;
18974 p = vim_strchr(theline, '\n');
18975 if (p == NULL)
18976 line_arg += STRLEN(line_arg);
18977 else
18978 {
18979 *p = NUL;
18980 line_arg = p + 1;
18981 }
18982 }
18983 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 theline = getcmdline(':', 0L, indent);
18985 else
18986 theline = eap->getline(':', eap->cookie, indent);
18987 if (KeyTyped)
18988 lines_left = Rows - 1;
18989 if (theline == NULL)
18990 {
18991 EMSG(_("E126: Missing :endfunction"));
18992 goto erret;
18993 }
18994
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018995 /* Detect line continuation: sourcing_lnum increased more than one. */
18996 if (sourcing_lnum > sourcing_lnum_off + 1)
18997 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18998 else
18999 sourcing_lnum_off = 0;
19000
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 if (skip_until != NULL)
19002 {
19003 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19004 * don't check for ":endfunc". */
19005 if (STRCMP(theline, skip_until) == 0)
19006 {
19007 vim_free(skip_until);
19008 skip_until = NULL;
19009 }
19010 }
19011 else
19012 {
19013 /* skip ':' and blanks*/
19014 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19015 ;
19016
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019017 /* Check for "endfunction". */
19018 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019020 if (line_arg == NULL)
19021 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 break;
19023 }
19024
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019025 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 * at "end". */
19027 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19028 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019029 else if (STRNCMP(p, "if", 2) == 0
19030 || STRNCMP(p, "wh", 2) == 0
19031 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 || STRNCMP(p, "try", 3) == 0)
19033 indent += 2;
19034
19035 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019036 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019038 if (*p == '!')
19039 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 p += eval_fname_script(p);
19041 if (ASCII_ISALPHA(*p))
19042 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019043 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 if (*skipwhite(p) == '(')
19045 {
19046 ++nesting;
19047 indent += 2;
19048 }
19049 }
19050 }
19051
19052 /* Check for ":append" or ":insert". */
19053 p = skip_range(p, NULL);
19054 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19055 || (p[0] == 'i'
19056 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19057 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19058 skip_until = vim_strsave((char_u *)".");
19059
19060 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19061 arg = skipwhite(skiptowhite(p));
19062 if (arg[0] == '<' && arg[1] =='<'
19063 && ((p[0] == 'p' && p[1] == 'y'
19064 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19065 || (p[0] == 'p' && p[1] == 'e'
19066 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19067 || (p[0] == 't' && p[1] == 'c'
19068 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19069 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19070 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019071 || (p[0] == 'm' && p[1] == 'z'
19072 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 ))
19074 {
19075 /* ":python <<" continues until a dot, like ":append" */
19076 p = skipwhite(arg + 2);
19077 if (*p == NUL)
19078 skip_until = vim_strsave((char_u *)".");
19079 else
19080 skip_until = vim_strsave(p);
19081 }
19082 }
19083
19084 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019085 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019086 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019087 if (line_arg == NULL)
19088 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019090 }
19091
19092 /* Copy the line to newly allocated memory. get_one_sourceline()
19093 * allocates 250 bytes per line, this saves 80% on average. The cost
19094 * is an extra alloc/free. */
19095 p = vim_strsave(theline);
19096 if (p != NULL)
19097 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019098 if (line_arg == NULL)
19099 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019100 theline = p;
19101 }
19102
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019103 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19104
19105 /* Add NULL lines for continuation lines, so that the line count is
19106 * equal to the index in the growarray. */
19107 while (sourcing_lnum_off-- > 0)
19108 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019109
19110 /* Check for end of eap->arg. */
19111 if (line_arg != NULL && *line_arg == NUL)
19112 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 }
19114
19115 /* Don't define the function when skipping commands or when an error was
19116 * detected. */
19117 if (eap->skip || did_emsg)
19118 goto erret;
19119
19120 /*
19121 * If there are no errors, add the function
19122 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019123 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019124 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019125 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019126 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019127 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019128 emsg_funcname("E707: Function name conflicts with variable: %s",
19129 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019130 goto erret;
19131 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019132
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019133 fp = find_func(name);
19134 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019136 if (!eap->forceit)
19137 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019138 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019139 goto erret;
19140 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019141 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019142 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019143 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019144 name);
19145 goto erret;
19146 }
19147 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019148 ga_clear_strings(&(fp->uf_args));
19149 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019150 vim_free(name);
19151 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 }
19154 else
19155 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019156 char numbuf[20];
19157
19158 fp = NULL;
19159 if (fudi.fd_newkey == NULL && !eap->forceit)
19160 {
19161 EMSG(_(e_funcdict));
19162 goto erret;
19163 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019164 if (fudi.fd_di == NULL)
19165 {
19166 /* Can't add a function to a locked dictionary */
19167 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19168 goto erret;
19169 }
19170 /* Can't change an existing function if it is locked */
19171 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19172 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019173
19174 /* Give the function a sequential number. Can only be used with a
19175 * Funcref! */
19176 vim_free(name);
19177 sprintf(numbuf, "%d", ++func_nr);
19178 name = vim_strsave((char_u *)numbuf);
19179 if (name == NULL)
19180 goto erret;
19181 }
19182
19183 if (fp == NULL)
19184 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019185 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019186 {
19187 int slen, plen;
19188 char_u *scriptname;
19189
19190 /* Check that the autoload name matches the script name. */
19191 j = FAIL;
19192 if (sourcing_name != NULL)
19193 {
19194 scriptname = autoload_name(name);
19195 if (scriptname != NULL)
19196 {
19197 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019198 plen = (int)STRLEN(p);
19199 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019200 if (slen > plen && fnamecmp(p,
19201 sourcing_name + slen - plen) == 0)
19202 j = OK;
19203 vim_free(scriptname);
19204 }
19205 }
19206 if (j == FAIL)
19207 {
19208 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19209 goto erret;
19210 }
19211 }
19212
19213 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214 if (fp == NULL)
19215 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019216
19217 if (fudi.fd_dict != NULL)
19218 {
19219 if (fudi.fd_di == NULL)
19220 {
19221 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019222 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019223 if (fudi.fd_di == NULL)
19224 {
19225 vim_free(fp);
19226 goto erret;
19227 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019228 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19229 {
19230 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019231 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019232 goto erret;
19233 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019234 }
19235 else
19236 /* overwrite existing dict entry */
19237 clear_tv(&fudi.fd_di->di_tv);
19238 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019239 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019240 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019241 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019242
19243 /* behave like "dict" was used */
19244 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019245 }
19246
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019248 STRCPY(fp->uf_name, name);
19249 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019251 fp->uf_args = newargs;
19252 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019253#ifdef FEAT_PROFILE
19254 fp->uf_tml_count = NULL;
19255 fp->uf_tml_total = NULL;
19256 fp->uf_tml_self = NULL;
19257 fp->uf_profiling = FALSE;
19258 if (prof_def_func())
19259 func_do_profile(fp);
19260#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019261 fp->uf_varargs = varargs;
19262 fp->uf_flags = flags;
19263 fp->uf_calls = 0;
19264 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019265 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266
19267erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 ga_clear_strings(&newargs);
19269 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019270ret_free:
19271 vim_free(skip_until);
19272 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275}
19276
19277/*
19278 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019279 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019281 * flags:
19282 * TFN_INT: internal function name OK
19283 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284 * Advances "pp" to just after the function name (if no error).
19285 */
19286 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019287trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 char_u **pp;
19289 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019290 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019291 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019293 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 char_u *start;
19295 char_u *end;
19296 int lead;
19297 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019299 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019300
19301 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019302 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019304
19305 /* Check for hard coded <SNR>: already translated function ID (from a user
19306 * command). */
19307 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19308 && (*pp)[2] == (int)KE_SNR)
19309 {
19310 *pp += 3;
19311 len = get_id_len(pp) + 3;
19312 return vim_strnsave(start, len);
19313 }
19314
19315 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19316 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019318 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019320
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019321 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19322 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019323 if (end == start)
19324 {
19325 if (!skip)
19326 EMSG(_("E129: Function name required"));
19327 goto theend;
19328 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019329 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019330 {
19331 /*
19332 * Report an invalid expression in braces, unless the expression
19333 * evaluation has been cancelled due to an aborting error, an
19334 * interrupt, or an exception.
19335 */
19336 if (!aborting())
19337 {
19338 if (end != NULL)
19339 EMSG2(_(e_invarg2), start);
19340 }
19341 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019342 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019343 goto theend;
19344 }
19345
19346 if (lv.ll_tv != NULL)
19347 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019348 if (fdp != NULL)
19349 {
19350 fdp->fd_dict = lv.ll_dict;
19351 fdp->fd_newkey = lv.ll_newkey;
19352 lv.ll_newkey = NULL;
19353 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019354 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019355 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19356 {
19357 name = vim_strsave(lv.ll_tv->vval.v_string);
19358 *pp = end;
19359 }
19360 else
19361 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019362 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19363 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019364 EMSG(_(e_funcref));
19365 else
19366 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019367 name = NULL;
19368 }
19369 goto theend;
19370 }
19371
19372 if (lv.ll_name == NULL)
19373 {
19374 /* Error found, but continue after the function name. */
19375 *pp = end;
19376 goto theend;
19377 }
19378
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019379 /* Check if the name is a Funcref. If so, use the value. */
19380 if (lv.ll_exp_name != NULL)
19381 {
19382 len = (int)STRLEN(lv.ll_exp_name);
19383 name = deref_func_name(lv.ll_exp_name, &len);
19384 if (name == lv.ll_exp_name)
19385 name = NULL;
19386 }
19387 else
19388 {
19389 len = (int)(end - *pp);
19390 name = deref_func_name(*pp, &len);
19391 if (name == *pp)
19392 name = NULL;
19393 }
19394 if (name != NULL)
19395 {
19396 name = vim_strsave(name);
19397 *pp = end;
19398 goto theend;
19399 }
19400
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019401 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019402 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019403 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019404 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19405 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19406 {
19407 /* When there was "s:" already or the name expanded to get a
19408 * leading "s:" then remove it. */
19409 lv.ll_name += 2;
19410 len -= 2;
19411 lead = 2;
19412 }
19413 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019414 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019415 {
19416 if (lead == 2) /* skip over "s:" */
19417 lv.ll_name += 2;
19418 len = (int)(end - lv.ll_name);
19419 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019420
19421 /*
19422 * Copy the function name to allocated memory.
19423 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19424 * Accept <SNR>123_name() outside a script.
19425 */
19426 if (skip)
19427 lead = 0; /* do nothing */
19428 else if (lead > 0)
19429 {
19430 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019431 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19432 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019433 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019434 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019435 if (current_SID <= 0)
19436 {
19437 EMSG(_(e_usingsid));
19438 goto theend;
19439 }
19440 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19441 lead += (int)STRLEN(sid_buf);
19442 }
19443 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019444 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019445 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019446 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019447 goto theend;
19448 }
19449 name = alloc((unsigned)(len + lead + 1));
19450 if (name != NULL)
19451 {
19452 if (lead > 0)
19453 {
19454 name[0] = K_SPECIAL;
19455 name[1] = KS_EXTRA;
19456 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019457 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019458 STRCPY(name + 3, sid_buf);
19459 }
19460 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19461 name[len + lead] = NUL;
19462 }
19463 *pp = end;
19464
19465theend:
19466 clear_lval(&lv);
19467 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468}
19469
19470/*
19471 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19472 * Return 2 if "p" starts with "s:".
19473 * Return 0 otherwise.
19474 */
19475 static int
19476eval_fname_script(p)
19477 char_u *p;
19478{
19479 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19480 || STRNICMP(p + 1, "SNR>", 4) == 0))
19481 return 5;
19482 if (p[0] == 's' && p[1] == ':')
19483 return 2;
19484 return 0;
19485}
19486
19487/*
19488 * Return TRUE if "p" starts with "<SID>" or "s:".
19489 * Only works if eval_fname_script() returned non-zero for "p"!
19490 */
19491 static int
19492eval_fname_sid(p)
19493 char_u *p;
19494{
19495 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19496}
19497
19498/*
19499 * List the head of the function: "name(arg1, arg2)".
19500 */
19501 static void
19502list_func_head(fp, indent)
19503 ufunc_T *fp;
19504 int indent;
19505{
19506 int j;
19507
19508 msg_start();
19509 if (indent)
19510 MSG_PUTS(" ");
19511 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019512 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 {
19514 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019515 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 }
19517 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019518 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019520 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 {
19522 if (j)
19523 MSG_PUTS(", ");
19524 msg_puts(FUNCARG(fp, j));
19525 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019526 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 {
19528 if (j)
19529 MSG_PUTS(", ");
19530 MSG_PUTS("...");
19531 }
19532 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019533 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019534 if (p_verbose > 0)
19535 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536}
19537
19538/*
19539 * Find a function by name, return pointer to it in ufuncs.
19540 * Return NULL for unknown function.
19541 */
19542 static ufunc_T *
19543find_func(name)
19544 char_u *name;
19545{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019546 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019548 hi = hash_find(&func_hashtab, name);
19549 if (!HASHITEM_EMPTY(hi))
19550 return HI2UF(hi);
19551 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552}
19553
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019554#if defined(EXITFREE) || defined(PROTO)
19555 void
19556free_all_functions()
19557{
19558 hashitem_T *hi;
19559
19560 /* Need to start all over every time, because func_free() may change the
19561 * hash table. */
19562 while (func_hashtab.ht_used > 0)
19563 for (hi = func_hashtab.ht_array; ; ++hi)
19564 if (!HASHITEM_EMPTY(hi))
19565 {
19566 func_free(HI2UF(hi));
19567 break;
19568 }
19569}
19570#endif
19571
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019572/*
19573 * Return TRUE if a function "name" exists.
19574 */
19575 static int
19576function_exists(name)
19577 char_u *name;
19578{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019579 char_u *nm = name;
19580 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019581 int n = FALSE;
19582
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019583 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019584 nm = skipwhite(nm);
19585
19586 /* Only accept "funcname", "funcname ", "funcname (..." and
19587 * "funcname(...", not "funcname!...". */
19588 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019589 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019590 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019591 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019592 else
19593 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019594 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019595 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019596 return n;
19597}
19598
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019599/*
19600 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019601 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019602 */
19603 static int
19604builtin_function(name)
19605 char_u *name;
19606{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019607 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19608 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019609}
19610
Bram Moolenaar05159a02005-02-26 23:04:13 +000019611#if defined(FEAT_PROFILE) || defined(PROTO)
19612/*
19613 * Start profiling function "fp".
19614 */
19615 static void
19616func_do_profile(fp)
19617 ufunc_T *fp;
19618{
19619 fp->uf_tm_count = 0;
19620 profile_zero(&fp->uf_tm_self);
19621 profile_zero(&fp->uf_tm_total);
19622 if (fp->uf_tml_count == NULL)
19623 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19624 (sizeof(int) * fp->uf_lines.ga_len));
19625 if (fp->uf_tml_total == NULL)
19626 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19627 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19628 if (fp->uf_tml_self == NULL)
19629 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19630 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19631 fp->uf_tml_idx = -1;
19632 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19633 || fp->uf_tml_self == NULL)
19634 return; /* out of memory */
19635
19636 fp->uf_profiling = TRUE;
19637}
19638
19639/*
19640 * Dump the profiling results for all functions in file "fd".
19641 */
19642 void
19643func_dump_profile(fd)
19644 FILE *fd;
19645{
19646 hashitem_T *hi;
19647 int todo;
19648 ufunc_T *fp;
19649 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019650 ufunc_T **sorttab;
19651 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019652
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019653 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019654 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19655
Bram Moolenaar05159a02005-02-26 23:04:13 +000019656 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19657 {
19658 if (!HASHITEM_EMPTY(hi))
19659 {
19660 --todo;
19661 fp = HI2UF(hi);
19662 if (fp->uf_profiling)
19663 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019664 if (sorttab != NULL)
19665 sorttab[st_len++] = fp;
19666
Bram Moolenaar05159a02005-02-26 23:04:13 +000019667 if (fp->uf_name[0] == K_SPECIAL)
19668 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19669 else
19670 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19671 if (fp->uf_tm_count == 1)
19672 fprintf(fd, "Called 1 time\n");
19673 else
19674 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19675 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19676 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19677 fprintf(fd, "\n");
19678 fprintf(fd, "count total (s) self (s)\n");
19679
19680 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19681 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019682 if (FUNCLINE(fp, i) == NULL)
19683 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019684 prof_func_line(fd, fp->uf_tml_count[i],
19685 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019686 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19687 }
19688 fprintf(fd, "\n");
19689 }
19690 }
19691 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019692
19693 if (sorttab != NULL && st_len > 0)
19694 {
19695 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19696 prof_total_cmp);
19697 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19698 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19699 prof_self_cmp);
19700 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19701 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019702}
Bram Moolenaar73830342005-02-28 22:48:19 +000019703
19704 static void
19705prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19706 FILE *fd;
19707 ufunc_T **sorttab;
19708 int st_len;
19709 char *title;
19710 int prefer_self; /* when equal print only self time */
19711{
19712 int i;
19713 ufunc_T *fp;
19714
19715 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19716 fprintf(fd, "count total (s) self (s) function\n");
19717 for (i = 0; i < 20 && i < st_len; ++i)
19718 {
19719 fp = sorttab[i];
19720 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19721 prefer_self);
19722 if (fp->uf_name[0] == K_SPECIAL)
19723 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19724 else
19725 fprintf(fd, " %s()\n", fp->uf_name);
19726 }
19727 fprintf(fd, "\n");
19728}
19729
19730/*
19731 * Print the count and times for one function or function line.
19732 */
19733 static void
19734prof_func_line(fd, count, total, self, prefer_self)
19735 FILE *fd;
19736 int count;
19737 proftime_T *total;
19738 proftime_T *self;
19739 int prefer_self; /* when equal print only self time */
19740{
19741 if (count > 0)
19742 {
19743 fprintf(fd, "%5d ", count);
19744 if (prefer_self && profile_equal(total, self))
19745 fprintf(fd, " ");
19746 else
19747 fprintf(fd, "%s ", profile_msg(total));
19748 if (!prefer_self && profile_equal(total, self))
19749 fprintf(fd, " ");
19750 else
19751 fprintf(fd, "%s ", profile_msg(self));
19752 }
19753 else
19754 fprintf(fd, " ");
19755}
19756
19757/*
19758 * Compare function for total time sorting.
19759 */
19760 static int
19761#ifdef __BORLANDC__
19762_RTLENTRYF
19763#endif
19764prof_total_cmp(s1, s2)
19765 const void *s1;
19766 const void *s2;
19767{
19768 ufunc_T *p1, *p2;
19769
19770 p1 = *(ufunc_T **)s1;
19771 p2 = *(ufunc_T **)s2;
19772 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19773}
19774
19775/*
19776 * Compare function for self time sorting.
19777 */
19778 static int
19779#ifdef __BORLANDC__
19780_RTLENTRYF
19781#endif
19782prof_self_cmp(s1, s2)
19783 const void *s1;
19784 const void *s2;
19785{
19786 ufunc_T *p1, *p2;
19787
19788 p1 = *(ufunc_T **)s1;
19789 p2 = *(ufunc_T **)s2;
19790 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19791}
19792
Bram Moolenaar05159a02005-02-26 23:04:13 +000019793#endif
19794
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019795/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019796 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019797 * Return TRUE if a package was loaded.
19798 */
19799 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019800script_autoload(name, reload)
19801 char_u *name;
19802 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019803{
19804 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019805 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019806 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019807 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019808
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019809 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019810 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019811 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019812 return FALSE;
19813
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019814 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019815
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019816 /* Find the name in the list of previously loaded package names. Skip
19817 * "autoload/", it's always the same. */
19818 for (i = 0; i < ga_loaded.ga_len; ++i)
19819 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19820 break;
19821 if (!reload && i < ga_loaded.ga_len)
19822 ret = FALSE; /* was loaded already */
19823 else
19824 {
19825 /* Remember the name if it wasn't loaded already. */
19826 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19827 {
19828 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19829 tofree = NULL;
19830 }
19831
19832 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019833 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019834 ret = TRUE;
19835 }
19836
19837 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019838 return ret;
19839}
19840
19841/*
19842 * Return the autoload script name for a function or variable name.
19843 * Returns NULL when out of memory.
19844 */
19845 static char_u *
19846autoload_name(name)
19847 char_u *name;
19848{
19849 char_u *p;
19850 char_u *scriptname;
19851
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019852 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019853 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19854 if (scriptname == NULL)
19855 return FALSE;
19856 STRCPY(scriptname, "autoload/");
19857 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019858 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019859 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019860 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019861 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019862 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019863}
19864
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19866
19867/*
19868 * Function given to ExpandGeneric() to obtain the list of user defined
19869 * function names.
19870 */
19871 char_u *
19872get_user_func_name(xp, idx)
19873 expand_T *xp;
19874 int idx;
19875{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019876 static long_u done;
19877 static hashitem_T *hi;
19878 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879
19880 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019882 done = 0;
19883 hi = func_hashtab.ht_array;
19884 }
19885 if (done < func_hashtab.ht_used)
19886 {
19887 if (done++ > 0)
19888 ++hi;
19889 while (HASHITEM_EMPTY(hi))
19890 ++hi;
19891 fp = HI2UF(hi);
19892
19893 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19894 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895
19896 cat_func_name(IObuff, fp);
19897 if (xp->xp_context != EXPAND_USER_FUNC)
19898 {
19899 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019900 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 STRCAT(IObuff, ")");
19902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 return IObuff;
19904 }
19905 return NULL;
19906}
19907
19908#endif /* FEAT_CMDL_COMPL */
19909
19910/*
19911 * Copy the function name of "fp" to buffer "buf".
19912 * "buf" must be able to hold the function name plus three bytes.
19913 * Takes care of script-local function names.
19914 */
19915 static void
19916cat_func_name(buf, fp)
19917 char_u *buf;
19918 ufunc_T *fp;
19919{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019920 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 {
19922 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019923 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 }
19925 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019926 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927}
19928
19929/*
19930 * ":delfunction {name}"
19931 */
19932 void
19933ex_delfunction(eap)
19934 exarg_T *eap;
19935{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019936 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 char_u *p;
19938 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019939 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940
19941 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019942 name = trans_function_name(&p, eap->skip, 0, &fudi);
19943 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019945 {
19946 if (fudi.fd_dict != NULL && !eap->skip)
19947 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 if (!ends_excmd(*skipwhite(p)))
19951 {
19952 vim_free(name);
19953 EMSG(_(e_trailing));
19954 return;
19955 }
19956 eap->nextcmd = check_nextcmd(p);
19957 if (eap->nextcmd != NULL)
19958 *p = NUL;
19959
19960 if (!eap->skip)
19961 fp = find_func(name);
19962 vim_free(name);
19963
19964 if (!eap->skip)
19965 {
19966 if (fp == NULL)
19967 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019968 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 return;
19970 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019971 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 {
19973 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19974 return;
19975 }
19976
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019977 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019979 /* Delete the dict item that refers to the function, it will
19980 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019981 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019983 else
19984 func_free(fp);
19985 }
19986}
19987
19988/*
19989 * Free a function and remove it from the list of functions.
19990 */
19991 static void
19992func_free(fp)
19993 ufunc_T *fp;
19994{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019995 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019996
19997 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019998 ga_clear_strings(&(fp->uf_args));
19999 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020000#ifdef FEAT_PROFILE
20001 vim_free(fp->uf_tml_count);
20002 vim_free(fp->uf_tml_total);
20003 vim_free(fp->uf_tml_self);
20004#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020005
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020006 /* remove the function from the function hashtable */
20007 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20008 if (HASHITEM_EMPTY(hi))
20009 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020010 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020011 hash_remove(&func_hashtab, hi);
20012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020013 vim_free(fp);
20014}
20015
20016/*
20017 * Unreference a Function: decrement the reference count and free it when it
20018 * becomes zero. Only for numbered functions.
20019 */
20020 static void
20021func_unref(name)
20022 char_u *name;
20023{
20024 ufunc_T *fp;
20025
20026 if (name != NULL && isdigit(*name))
20027 {
20028 fp = find_func(name);
20029 if (fp == NULL)
20030 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020031 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020032 {
20033 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020034 * when "uf_calls" becomes zero. */
20035 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020036 func_free(fp);
20037 }
20038 }
20039}
20040
20041/*
20042 * Count a reference to a Function.
20043 */
20044 static void
20045func_ref(name)
20046 char_u *name;
20047{
20048 ufunc_T *fp;
20049
20050 if (name != NULL && isdigit(*name))
20051 {
20052 fp = find_func(name);
20053 if (fp == NULL)
20054 EMSG2(_(e_intern2), "func_ref()");
20055 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020056 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 }
20058}
20059
20060/*
20061 * Call a user function.
20062 */
20063 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020064call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 ufunc_T *fp; /* pointer to function */
20066 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020067 typval_T *argvars; /* arguments */
20068 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 linenr_T firstline; /* first line of range */
20070 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020071 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072{
Bram Moolenaar33570922005-01-25 22:26:29 +000020073 char_u *save_sourcing_name;
20074 linenr_T save_sourcing_lnum;
20075 scid_T save_current_SID;
20076 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 int save_did_emsg;
20078 static int depth = 0;
20079 dictitem_T *v;
20080 int fixvar_idx = 0; /* index in fixvar[] */
20081 int i;
20082 int ai;
20083 char_u numbuf[NUMBUFLEN];
20084 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020085#ifdef FEAT_PROFILE
20086 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020087 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089
20090 /* If depth of calling is getting too high, don't execute the function */
20091 if (depth >= p_mfd)
20092 {
20093 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020094 rettv->v_type = VAR_NUMBER;
20095 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 return;
20097 }
20098 ++depth;
20099
20100 line_breakcheck(); /* check for CTRL-C hit */
20101
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020102 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020103 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020105 fc.rettv = rettv;
20106 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 fc.linenr = 0;
20108 fc.returned = FALSE;
20109 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020111 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 fc.dbg_tick = debug_tick;
20113
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 /*
20115 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20116 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20117 * each argument variable and saves a lot of time.
20118 */
20119 /*
20120 * Init l: variables.
20121 */
20122 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020123 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020124 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020125 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20126 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020128 name = v->di_key;
20129 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020130 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20131 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20132 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020133 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 v->di_tv.vval.v_dict = selfdict;
20135 ++selfdict->dv_refcount;
20136 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020137
Bram Moolenaar33570922005-01-25 22:26:29 +000020138 /*
20139 * Init a: variables.
20140 * Set a:0 to "argcount".
20141 * Set a:000 to a list with room for the "..." arguments.
20142 */
20143 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20144 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020145 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020146 v = &fc.fixvar[fixvar_idx++].var;
20147 STRCPY(v->di_key, "000");
20148 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20149 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20150 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020151 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020152 v->di_tv.vval.v_list = &fc.l_varlist;
20153 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20154 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020155 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020156
20157 /*
20158 * Set a:firstline to "firstline" and a:lastline to "lastline".
20159 * Set a:name to named arguments.
20160 * Set a:N to the "..." arguments.
20161 */
20162 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20163 (varnumber_T)firstline);
20164 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20165 (varnumber_T)lastline);
20166 for (i = 0; i < argcount; ++i)
20167 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020168 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020169 if (ai < 0)
20170 /* named argument a:name */
20171 name = FUNCARG(fp, i);
20172 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020173 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020174 /* "..." argument a:1, a:2, etc. */
20175 sprintf((char *)numbuf, "%d", ai + 1);
20176 name = numbuf;
20177 }
20178 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20179 {
20180 v = &fc.fixvar[fixvar_idx++].var;
20181 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20182 }
20183 else
20184 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020185 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20186 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020187 if (v == NULL)
20188 break;
20189 v->di_flags = DI_FLAGS_RO;
20190 }
20191 STRCPY(v->di_key, name);
20192 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20193
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020194 /* Note: the values are copied directly to avoid alloc/free.
20195 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020196 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020197 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020198
20199 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20200 {
20201 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20202 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020203 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020204 }
20205 }
20206
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 /* Don't redraw while executing the function. */
20208 ++RedrawingDisabled;
20209 save_sourcing_name = sourcing_name;
20210 save_sourcing_lnum = sourcing_lnum;
20211 sourcing_lnum = 1;
20212 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020213 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 if (sourcing_name != NULL)
20215 {
20216 if (save_sourcing_name != NULL
20217 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20218 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20219 else
20220 STRCPY(sourcing_name, "function ");
20221 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20222
20223 if (p_verbose >= 12)
20224 {
20225 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020226 verbose_enter_scroll();
20227
Bram Moolenaar555b2802005-05-19 21:08:39 +000020228 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 if (p_verbose >= 14)
20230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020232 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020233 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020234 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235
20236 msg_puts((char_u *)"(");
20237 for (i = 0; i < argcount; ++i)
20238 {
20239 if (i > 0)
20240 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020241 if (argvars[i].v_type == VAR_NUMBER)
20242 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 else
20244 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020245 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20246 if (s != NULL)
20247 {
20248 trunc_string(s, buf, MSG_BUF_CLEN);
20249 msg_puts(buf);
20250 vim_free(tofree);
20251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 }
20253 }
20254 msg_puts((char_u *)")");
20255 }
20256 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020257
20258 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 --no_wait_return;
20260 }
20261 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020262#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020263 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020264 {
20265 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20266 func_do_profile(fp);
20267 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020268 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020269 {
20270 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020271 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020272 profile_zero(&fp->uf_tm_children);
20273 }
20274 script_prof_save(&wait_start);
20275 }
20276#endif
20277
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020279 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 save_did_emsg = did_emsg;
20281 did_emsg = FALSE;
20282
20283 /* call do_cmdline() to execute the lines */
20284 do_cmdline(NULL, get_func_line, (void *)&fc,
20285 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20286
20287 --RedrawingDisabled;
20288
20289 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020290 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020292 clear_tv(rettv);
20293 rettv->v_type = VAR_NUMBER;
20294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 }
20296
Bram Moolenaar05159a02005-02-26 23:04:13 +000020297#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020298 if (do_profiling == PROF_YES && (fp->uf_profiling
20299 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020300 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020301 profile_end(&call_start);
20302 profile_sub_wait(&wait_start, &call_start);
20303 profile_add(&fp->uf_tm_total, &call_start);
20304 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020305 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020306 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020307 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20308 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020309 }
20310 }
20311#endif
20312
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 /* when being verbose, mention the return value */
20314 if (p_verbose >= 12)
20315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020317 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020320 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020321 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020322 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20323 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020326 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020327 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020328 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020329 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020330
Bram Moolenaar555b2802005-05-19 21:08:39 +000020331 /* The value may be very long. Skip the middle part, so that we
20332 * have some idea how it starts and ends. smsg() would always
20333 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020334 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20335 if (s != NULL)
20336 {
20337 trunc_string(s, buf, MSG_BUF_CLEN);
20338 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20339 vim_free(tofree);
20340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 }
20342 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020343
20344 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 --no_wait_return;
20346 }
20347
20348 vim_free(sourcing_name);
20349 sourcing_name = save_sourcing_name;
20350 sourcing_lnum = save_sourcing_lnum;
20351 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020352#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020353 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020354 script_prof_restore(&wait_start);
20355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356
20357 if (p_verbose >= 12 && sourcing_name != NULL)
20358 {
20359 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020360 verbose_enter_scroll();
20361
Bram Moolenaar555b2802005-05-19 21:08:39 +000020362 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020364
20365 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 --no_wait_return;
20367 }
20368
20369 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020370 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371
Bram Moolenaar33570922005-01-25 22:26:29 +000020372 /* The a: variables typevals were not alloced, only free the allocated
20373 * variables. */
20374 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20375
20376 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 --depth;
20378}
20379
20380/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 * Add a number variable "name" to dict "dp" with value "nr".
20382 */
20383 static void
20384add_nr_var(dp, v, name, nr)
20385 dict_T *dp;
20386 dictitem_T *v;
20387 char *name;
20388 varnumber_T nr;
20389{
20390 STRCPY(v->di_key, name);
20391 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20392 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20393 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020394 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020395 v->di_tv.vval.v_number = nr;
20396}
20397
20398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 * ":return [expr]"
20400 */
20401 void
20402ex_return(eap)
20403 exarg_T *eap;
20404{
20405 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020406 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 int returning = FALSE;
20408
20409 if (current_funccal == NULL)
20410 {
20411 EMSG(_("E133: :return not inside a function"));
20412 return;
20413 }
20414
20415 if (eap->skip)
20416 ++emsg_skip;
20417
20418 eap->nextcmd = NULL;
20419 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020420 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 {
20422 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020423 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020425 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 }
20427 /* It's safer to return also on error. */
20428 else if (!eap->skip)
20429 {
20430 /*
20431 * Return unless the expression evaluation has been cancelled due to an
20432 * aborting error, an interrupt, or an exception.
20433 */
20434 if (!aborting())
20435 returning = do_return(eap, FALSE, TRUE, NULL);
20436 }
20437
20438 /* When skipping or the return gets pending, advance to the next command
20439 * in this line (!returning). Otherwise, ignore the rest of the line.
20440 * Following lines will be ignored by get_func_line(). */
20441 if (returning)
20442 eap->nextcmd = NULL;
20443 else if (eap->nextcmd == NULL) /* no argument */
20444 eap->nextcmd = check_nextcmd(arg);
20445
20446 if (eap->skip)
20447 --emsg_skip;
20448}
20449
20450/*
20451 * Return from a function. Possibly makes the return pending. Also called
20452 * for a pending return at the ":endtry" or after returning from an extra
20453 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020454 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020455 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 * FALSE when the return gets pending.
20457 */
20458 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020459do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 exarg_T *eap;
20461 int reanimate;
20462 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020463 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464{
20465 int idx;
20466 struct condstack *cstack = eap->cstack;
20467
20468 if (reanimate)
20469 /* Undo the return. */
20470 current_funccal->returned = FALSE;
20471
20472 /*
20473 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20474 * not in its finally clause (which then is to be executed next) is found.
20475 * In this case, make the ":return" pending for execution at the ":endtry".
20476 * Otherwise, return normally.
20477 */
20478 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20479 if (idx >= 0)
20480 {
20481 cstack->cs_pending[idx] = CSTP_RETURN;
20482
20483 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020484 /* A pending return again gets pending. "rettv" points to an
20485 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020487 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 else
20489 {
20490 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020491 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020493 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 {
20497 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020498 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020499 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 else
20501 EMSG(_(e_outofmem));
20502 }
20503 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020504 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505
20506 if (reanimate)
20507 {
20508 /* The pending return value could be overwritten by a ":return"
20509 * without argument in a finally clause; reset the default
20510 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020511 current_funccal->rettv->v_type = VAR_NUMBER;
20512 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 }
20514 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020515 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516 }
20517 else
20518 {
20519 current_funccal->returned = TRUE;
20520
20521 /* If the return is carried out now, store the return value. For
20522 * a return immediately after reanimation, the value is already
20523 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020524 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020526 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020527 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020529 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 }
20531 }
20532
20533 return idx < 0;
20534}
20535
20536/*
20537 * Free the variable with a pending return value.
20538 */
20539 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020540discard_pending_return(rettv)
20541 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542{
Bram Moolenaar33570922005-01-25 22:26:29 +000020543 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544}
20545
20546/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020547 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 * is an allocated string. Used by report_pending() for verbose messages.
20549 */
20550 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551get_return_cmd(rettv)
20552 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020554 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020555 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020556 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020558 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020559 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020560 if (s == NULL)
20561 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020562
20563 STRCPY(IObuff, ":return ");
20564 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20565 if (STRLEN(s) + 8 >= IOSIZE)
20566 STRCPY(IObuff + IOSIZE - 4, "...");
20567 vim_free(tofree);
20568 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569}
20570
20571/*
20572 * Get next function line.
20573 * Called by do_cmdline() to get the next line.
20574 * Returns allocated string, or NULL for end of function.
20575 */
20576/* ARGSUSED */
20577 char_u *
20578get_func_line(c, cookie, indent)
20579 int c; /* not used */
20580 void *cookie;
20581 int indent; /* not used */
20582{
Bram Moolenaar33570922005-01-25 22:26:29 +000020583 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020584 ufunc_T *fp = fcp->func;
20585 char_u *retval;
20586 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587
20588 /* If breakpoints have been added/deleted need to check for it. */
20589 if (fcp->dbg_tick != debug_tick)
20590 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020591 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 sourcing_lnum);
20593 fcp->dbg_tick = debug_tick;
20594 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020595#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020596 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020597 func_line_end(cookie);
20598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599
Bram Moolenaar05159a02005-02-26 23:04:13 +000020600 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020601 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20602 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 retval = NULL;
20604 else
20605 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020606 /* Skip NULL lines (continuation lines). */
20607 while (fcp->linenr < gap->ga_len
20608 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20609 ++fcp->linenr;
20610 if (fcp->linenr >= gap->ga_len)
20611 retval = NULL;
20612 else
20613 {
20614 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20615 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020616#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020617 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020618 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020619#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 }
20622
20623 /* Did we encounter a breakpoint? */
20624 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20625 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020626 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020628 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 sourcing_lnum);
20630 fcp->dbg_tick = debug_tick;
20631 }
20632
20633 return retval;
20634}
20635
Bram Moolenaar05159a02005-02-26 23:04:13 +000020636#if defined(FEAT_PROFILE) || defined(PROTO)
20637/*
20638 * Called when starting to read a function line.
20639 * "sourcing_lnum" must be correct!
20640 * When skipping lines it may not actually be executed, but we won't find out
20641 * until later and we need to store the time now.
20642 */
20643 void
20644func_line_start(cookie)
20645 void *cookie;
20646{
20647 funccall_T *fcp = (funccall_T *)cookie;
20648 ufunc_T *fp = fcp->func;
20649
20650 if (fp->uf_profiling && sourcing_lnum >= 1
20651 && sourcing_lnum <= fp->uf_lines.ga_len)
20652 {
20653 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020654 /* Skip continuation lines. */
20655 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20656 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020657 fp->uf_tml_execed = FALSE;
20658 profile_start(&fp->uf_tml_start);
20659 profile_zero(&fp->uf_tml_children);
20660 profile_get_wait(&fp->uf_tml_wait);
20661 }
20662}
20663
20664/*
20665 * Called when actually executing a function line.
20666 */
20667 void
20668func_line_exec(cookie)
20669 void *cookie;
20670{
20671 funccall_T *fcp = (funccall_T *)cookie;
20672 ufunc_T *fp = fcp->func;
20673
20674 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20675 fp->uf_tml_execed = TRUE;
20676}
20677
20678/*
20679 * Called when done with a function line.
20680 */
20681 void
20682func_line_end(cookie)
20683 void *cookie;
20684{
20685 funccall_T *fcp = (funccall_T *)cookie;
20686 ufunc_T *fp = fcp->func;
20687
20688 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20689 {
20690 if (fp->uf_tml_execed)
20691 {
20692 ++fp->uf_tml_count[fp->uf_tml_idx];
20693 profile_end(&fp->uf_tml_start);
20694 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020695 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020696 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20697 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020698 }
20699 fp->uf_tml_idx = -1;
20700 }
20701}
20702#endif
20703
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704/*
20705 * Return TRUE if the currently active function should be ended, because a
20706 * return was encountered or an error occured. Used inside a ":while".
20707 */
20708 int
20709func_has_ended(cookie)
20710 void *cookie;
20711{
Bram Moolenaar33570922005-01-25 22:26:29 +000020712 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713
20714 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20715 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020716 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 || fcp->returned);
20718}
20719
20720/*
20721 * return TRUE if cookie indicates a function which "abort"s on errors.
20722 */
20723 int
20724func_has_abort(cookie)
20725 void *cookie;
20726{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020727 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728}
20729
20730#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20731typedef enum
20732{
20733 VAR_FLAVOUR_DEFAULT,
20734 VAR_FLAVOUR_SESSION,
20735 VAR_FLAVOUR_VIMINFO
20736} var_flavour_T;
20737
20738static var_flavour_T var_flavour __ARGS((char_u *varname));
20739
20740 static var_flavour_T
20741var_flavour(varname)
20742 char_u *varname;
20743{
20744 char_u *p = varname;
20745
20746 if (ASCII_ISUPPER(*p))
20747 {
20748 while (*(++p))
20749 if (ASCII_ISLOWER(*p))
20750 return VAR_FLAVOUR_SESSION;
20751 return VAR_FLAVOUR_VIMINFO;
20752 }
20753 else
20754 return VAR_FLAVOUR_DEFAULT;
20755}
20756#endif
20757
20758#if defined(FEAT_VIMINFO) || defined(PROTO)
20759/*
20760 * Restore global vars that start with a capital from the viminfo file
20761 */
20762 int
20763read_viminfo_varlist(virp, writing)
20764 vir_T *virp;
20765 int writing;
20766{
20767 char_u *tab;
20768 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020769 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770
20771 if (!writing && (find_viminfo_parameter('!') != NULL))
20772 {
20773 tab = vim_strchr(virp->vir_line + 1, '\t');
20774 if (tab != NULL)
20775 {
20776 *tab++ = '\0'; /* isolate the variable name */
20777 if (*tab == 'S') /* string var */
20778 is_string = TRUE;
20779
20780 tab = vim_strchr(tab, '\t');
20781 if (tab != NULL)
20782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 if (is_string)
20784 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020785 tv.v_type = VAR_STRING;
20786 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 }
20789 else
20790 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020791 tv.v_type = VAR_NUMBER;
20792 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020794 set_var(virp->vir_line + 1, &tv, FALSE);
20795 if (is_string)
20796 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 }
20798 }
20799 }
20800
20801 return viminfo_readline(virp);
20802}
20803
20804/*
20805 * Write global vars that start with a capital to the viminfo file
20806 */
20807 void
20808write_viminfo_varlist(fp)
20809 FILE *fp;
20810{
Bram Moolenaar33570922005-01-25 22:26:29 +000020811 hashitem_T *hi;
20812 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020813 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020814 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020815 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020816 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020817 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818
20819 if (find_viminfo_parameter('!') == NULL)
20820 return;
20821
20822 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020823
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020824 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020825 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020827 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020829 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020830 this_var = HI2DI(hi);
20831 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020832 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020833 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020834 {
20835 case VAR_STRING: s = "STR"; break;
20836 case VAR_NUMBER: s = "NUM"; break;
20837 default: continue;
20838 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020839 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020840 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020841 if (p != NULL)
20842 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020843 vim_free(tofree);
20844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 }
20846 }
20847}
20848#endif
20849
20850#if defined(FEAT_SESSION) || defined(PROTO)
20851 int
20852store_session_globals(fd)
20853 FILE *fd;
20854{
Bram Moolenaar33570922005-01-25 22:26:29 +000020855 hashitem_T *hi;
20856 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020857 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 char_u *p, *t;
20859
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020860 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020861 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020863 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020865 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020866 this_var = HI2DI(hi);
20867 if ((this_var->di_tv.v_type == VAR_NUMBER
20868 || this_var->di_tv.v_type == VAR_STRING)
20869 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020870 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020871 /* Escape special characters with a backslash. Turn a LF and
20872 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020873 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020874 (char_u *)"\\\"\n\r");
20875 if (p == NULL) /* out of memory */
20876 break;
20877 for (t = p; *t != NUL; ++t)
20878 if (*t == '\n')
20879 *t = 'n';
20880 else if (*t == '\r')
20881 *t = 'r';
20882 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020883 this_var->di_key,
20884 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20885 : ' ',
20886 p,
20887 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20888 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020889 || put_eol(fd) == FAIL)
20890 {
20891 vim_free(p);
20892 return FAIL;
20893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 vim_free(p);
20895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 }
20897 }
20898 return OK;
20899}
20900#endif
20901
Bram Moolenaar661b1822005-07-28 22:36:45 +000020902/*
20903 * Display script name where an item was last set.
20904 * Should only be invoked when 'verbose' is non-zero.
20905 */
20906 void
20907last_set_msg(scriptID)
20908 scid_T scriptID;
20909{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020910 char_u *p;
20911
Bram Moolenaar661b1822005-07-28 22:36:45 +000020912 if (scriptID != 0)
20913 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020914 p = home_replace_save(NULL, get_scriptname(scriptID));
20915 if (p != NULL)
20916 {
20917 verbose_enter();
20918 MSG_PUTS(_("\n\tLast set from "));
20919 MSG_PUTS(p);
20920 vim_free(p);
20921 verbose_leave();
20922 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020923 }
20924}
20925
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926#endif /* FEAT_EVAL */
20927
20928#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20929
20930
20931#ifdef WIN3264
20932/*
20933 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20934 */
20935static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20936static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20937static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20938
20939/*
20940 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020941 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 */
20943 static int
20944get_short_pathname(fnamep, bufp, fnamelen)
20945 char_u **fnamep;
20946 char_u **bufp;
20947 int *fnamelen;
20948{
20949 int l,len;
20950 char_u *newbuf;
20951
20952 len = *fnamelen;
20953
20954 l = GetShortPathName(*fnamep, *fnamep, len);
20955 if (l > len - 1)
20956 {
20957 /* If that doesn't work (not enough space), then save the string
20958 * and try again with a new buffer big enough
20959 */
20960 newbuf = vim_strnsave(*fnamep, l);
20961 if (newbuf == NULL)
20962 return 0;
20963
20964 vim_free(*bufp);
20965 *fnamep = *bufp = newbuf;
20966
20967 l = GetShortPathName(*fnamep,*fnamep,l+1);
20968
20969 /* Really should always succeed, as the buffer is big enough */
20970 }
20971
20972 *fnamelen = l;
20973 return 1;
20974}
20975
20976/*
20977 * Create a short path name. Returns the length of the buffer it needs.
20978 * Doesn't copy over the end of the buffer passed in.
20979 */
20980 static int
20981shortpath_for_invalid_fname(fname, bufp, fnamelen)
20982 char_u **fname;
20983 char_u **bufp;
20984 int *fnamelen;
20985{
20986 char_u *s, *p, *pbuf2, *pbuf3;
20987 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020988 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989
20990 /* Make a copy */
20991 len2 = *fnamelen;
20992 pbuf2 = vim_strnsave(*fname, len2);
20993 pbuf3 = NULL;
20994
20995 s = pbuf2 + len2 - 1; /* Find the end */
20996 slen = 1;
20997 plen = len2;
20998
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020999 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 {
21001 --s;
21002 ++slen;
21003 --plen;
21004 }
21005
21006 do
21007 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021008 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021009 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 {
21011 --s;
21012 ++slen;
21013 --plen;
21014 }
21015 if (s <= pbuf2)
21016 break;
21017
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021018 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 ch = *s;
21020 *s = 0; /* get_short_pathname requires a null-terminated string */
21021
21022 /* Try it in situ */
21023 p = pbuf2;
21024 if (!get_short_pathname(&p, &pbuf3, &plen))
21025 {
21026 vim_free(pbuf2);
21027 return -1;
21028 }
21029 *s = ch; /* Preserve the string */
21030 } while (plen == 0);
21031
21032 if (plen > 0)
21033 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021034 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 *fnamelen = len = plen + slen;
21036 vim_free(*bufp);
21037 if (len > len2)
21038 {
21039 /* If there's not enough space in the currently allocated string,
21040 * then copy it to a buffer big enough.
21041 */
21042 *fname= *bufp = vim_strnsave(p, len);
21043 if (*fname == NULL)
21044 return -1;
21045 }
21046 else
21047 {
21048 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21049 *fname = *bufp = pbuf2;
21050 if (p != pbuf2)
21051 strncpy(*fname, p, plen);
21052 pbuf2 = NULL;
21053 }
21054 /* Concat the next bit */
21055 strncpy(*fname + plen, s, slen);
21056 (*fname)[len] = '\0';
21057 }
21058 vim_free(pbuf3);
21059 vim_free(pbuf2);
21060 return 0;
21061}
21062
21063/*
21064 * Get a pathname for a partial path.
21065 */
21066 static int
21067shortpath_for_partial(fnamep, bufp, fnamelen)
21068 char_u **fnamep;
21069 char_u **bufp;
21070 int *fnamelen;
21071{
21072 int sepcount, len, tflen;
21073 char_u *p;
21074 char_u *pbuf, *tfname;
21075 int hasTilde;
21076
21077 /* Count up the path seperators from the RHS.. so we know which part
21078 * of the path to return.
21079 */
21080 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021081 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 if (vim_ispathsep(*p))
21083 ++sepcount;
21084
21085 /* Need full path first (use expand_env() to remove a "~/") */
21086 hasTilde = (**fnamep == '~');
21087 if (hasTilde)
21088 pbuf = tfname = expand_env_save(*fnamep);
21089 else
21090 pbuf = tfname = FullName_save(*fnamep, FALSE);
21091
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021092 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093
21094 if (!get_short_pathname(&tfname, &pbuf, &len))
21095 return -1;
21096
21097 if (len == 0)
21098 {
21099 /* Don't have a valid filename, so shorten the rest of the
21100 * path if we can. This CAN give us invalid 8.3 filenames, but
21101 * there's not a lot of point in guessing what it might be.
21102 */
21103 len = tflen;
21104 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21105 return -1;
21106 }
21107
21108 /* Count the paths backward to find the beginning of the desired string. */
21109 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021110 {
21111#ifdef FEAT_MBYTE
21112 if (has_mbyte)
21113 p -= mb_head_off(tfname, p);
21114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 if (vim_ispathsep(*p))
21116 {
21117 if (sepcount == 0 || (hasTilde && sepcount == 1))
21118 break;
21119 else
21120 sepcount --;
21121 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 if (hasTilde)
21124 {
21125 --p;
21126 if (p >= tfname)
21127 *p = '~';
21128 else
21129 return -1;
21130 }
21131 else
21132 ++p;
21133
21134 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21135 vim_free(*bufp);
21136 *fnamelen = (int)STRLEN(p);
21137 *bufp = pbuf;
21138 *fnamep = p;
21139
21140 return 0;
21141}
21142#endif /* WIN3264 */
21143
21144/*
21145 * Adjust a filename, according to a string of modifiers.
21146 * *fnamep must be NUL terminated when called. When returning, the length is
21147 * determined by *fnamelen.
21148 * Returns valid flags.
21149 * When there is an error, *fnamep is set to NULL.
21150 */
21151 int
21152modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21153 char_u *src; /* string with modifiers */
21154 int *usedlen; /* characters after src that are used */
21155 char_u **fnamep; /* file name so far */
21156 char_u **bufp; /* buffer for allocated file name or NULL */
21157 int *fnamelen; /* length of fnamep */
21158{
21159 int valid = 0;
21160 char_u *tail;
21161 char_u *s, *p, *pbuf;
21162 char_u dirname[MAXPATHL];
21163 int c;
21164 int has_fullname = 0;
21165#ifdef WIN3264
21166 int has_shortname = 0;
21167#endif
21168
21169repeat:
21170 /* ":p" - full path/file_name */
21171 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21172 {
21173 has_fullname = 1;
21174
21175 valid |= VALID_PATH;
21176 *usedlen += 2;
21177
21178 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21179 if ((*fnamep)[0] == '~'
21180#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21181 && ((*fnamep)[1] == '/'
21182# ifdef BACKSLASH_IN_FILENAME
21183 || (*fnamep)[1] == '\\'
21184# endif
21185 || (*fnamep)[1] == NUL)
21186
21187#endif
21188 )
21189 {
21190 *fnamep = expand_env_save(*fnamep);
21191 vim_free(*bufp); /* free any allocated file name */
21192 *bufp = *fnamep;
21193 if (*fnamep == NULL)
21194 return -1;
21195 }
21196
21197 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021198 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 {
21200 if (vim_ispathsep(*p)
21201 && p[1] == '.'
21202 && (p[2] == NUL
21203 || vim_ispathsep(p[2])
21204 || (p[2] == '.'
21205 && (p[3] == NUL || vim_ispathsep(p[3])))))
21206 break;
21207 }
21208
21209 /* FullName_save() is slow, don't use it when not needed. */
21210 if (*p != NUL || !vim_isAbsName(*fnamep))
21211 {
21212 *fnamep = FullName_save(*fnamep, *p != NUL);
21213 vim_free(*bufp); /* free any allocated file name */
21214 *bufp = *fnamep;
21215 if (*fnamep == NULL)
21216 return -1;
21217 }
21218
21219 /* Append a path separator to a directory. */
21220 if (mch_isdir(*fnamep))
21221 {
21222 /* Make room for one or two extra characters. */
21223 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21224 vim_free(*bufp); /* free any allocated file name */
21225 *bufp = *fnamep;
21226 if (*fnamep == NULL)
21227 return -1;
21228 add_pathsep(*fnamep);
21229 }
21230 }
21231
21232 /* ":." - path relative to the current directory */
21233 /* ":~" - path relative to the home directory */
21234 /* ":8" - shortname path - postponed till after */
21235 while (src[*usedlen] == ':'
21236 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21237 {
21238 *usedlen += 2;
21239 if (c == '8')
21240 {
21241#ifdef WIN3264
21242 has_shortname = 1; /* Postpone this. */
21243#endif
21244 continue;
21245 }
21246 pbuf = NULL;
21247 /* Need full path first (use expand_env() to remove a "~/") */
21248 if (!has_fullname)
21249 {
21250 if (c == '.' && **fnamep == '~')
21251 p = pbuf = expand_env_save(*fnamep);
21252 else
21253 p = pbuf = FullName_save(*fnamep, FALSE);
21254 }
21255 else
21256 p = *fnamep;
21257
21258 has_fullname = 0;
21259
21260 if (p != NULL)
21261 {
21262 if (c == '.')
21263 {
21264 mch_dirname(dirname, MAXPATHL);
21265 s = shorten_fname(p, dirname);
21266 if (s != NULL)
21267 {
21268 *fnamep = s;
21269 if (pbuf != NULL)
21270 {
21271 vim_free(*bufp); /* free any allocated file name */
21272 *bufp = pbuf;
21273 pbuf = NULL;
21274 }
21275 }
21276 }
21277 else
21278 {
21279 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21280 /* Only replace it when it starts with '~' */
21281 if (*dirname == '~')
21282 {
21283 s = vim_strsave(dirname);
21284 if (s != NULL)
21285 {
21286 *fnamep = s;
21287 vim_free(*bufp);
21288 *bufp = s;
21289 }
21290 }
21291 }
21292 vim_free(pbuf);
21293 }
21294 }
21295
21296 tail = gettail(*fnamep);
21297 *fnamelen = (int)STRLEN(*fnamep);
21298
21299 /* ":h" - head, remove "/file_name", can be repeated */
21300 /* Don't remove the first "/" or "c:\" */
21301 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21302 {
21303 valid |= VALID_HEAD;
21304 *usedlen += 2;
21305 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021306 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 --tail;
21308 *fnamelen = (int)(tail - *fnamep);
21309#ifdef VMS
21310 if (*fnamelen > 0)
21311 *fnamelen += 1; /* the path separator is part of the path */
21312#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021313 while (tail > s && !after_pathsep(s, tail))
21314 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 }
21316
21317 /* ":8" - shortname */
21318 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21319 {
21320 *usedlen += 2;
21321#ifdef WIN3264
21322 has_shortname = 1;
21323#endif
21324 }
21325
21326#ifdef WIN3264
21327 /* Check shortname after we have done 'heads' and before we do 'tails'
21328 */
21329 if (has_shortname)
21330 {
21331 pbuf = NULL;
21332 /* Copy the string if it is shortened by :h */
21333 if (*fnamelen < (int)STRLEN(*fnamep))
21334 {
21335 p = vim_strnsave(*fnamep, *fnamelen);
21336 if (p == 0)
21337 return -1;
21338 vim_free(*bufp);
21339 *bufp = *fnamep = p;
21340 }
21341
21342 /* Split into two implementations - makes it easier. First is where
21343 * there isn't a full name already, second is where there is.
21344 */
21345 if (!has_fullname && !vim_isAbsName(*fnamep))
21346 {
21347 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21348 return -1;
21349 }
21350 else
21351 {
21352 int l;
21353
21354 /* Simple case, already have the full-name
21355 * Nearly always shorter, so try first time. */
21356 l = *fnamelen;
21357 if (!get_short_pathname(fnamep, bufp, &l))
21358 return -1;
21359
21360 if (l == 0)
21361 {
21362 /* Couldn't find the filename.. search the paths.
21363 */
21364 l = *fnamelen;
21365 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21366 return -1;
21367 }
21368 *fnamelen = l;
21369 }
21370 }
21371#endif /* WIN3264 */
21372
21373 /* ":t" - tail, just the basename */
21374 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21375 {
21376 *usedlen += 2;
21377 *fnamelen -= (int)(tail - *fnamep);
21378 *fnamep = tail;
21379 }
21380
21381 /* ":e" - extension, can be repeated */
21382 /* ":r" - root, without extension, can be repeated */
21383 while (src[*usedlen] == ':'
21384 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21385 {
21386 /* find a '.' in the tail:
21387 * - for second :e: before the current fname
21388 * - otherwise: The last '.'
21389 */
21390 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21391 s = *fnamep - 2;
21392 else
21393 s = *fnamep + *fnamelen - 1;
21394 for ( ; s > tail; --s)
21395 if (s[0] == '.')
21396 break;
21397 if (src[*usedlen + 1] == 'e') /* :e */
21398 {
21399 if (s > tail)
21400 {
21401 *fnamelen += (int)(*fnamep - (s + 1));
21402 *fnamep = s + 1;
21403#ifdef VMS
21404 /* cut version from the extension */
21405 s = *fnamep + *fnamelen - 1;
21406 for ( ; s > *fnamep; --s)
21407 if (s[0] == ';')
21408 break;
21409 if (s > *fnamep)
21410 *fnamelen = s - *fnamep;
21411#endif
21412 }
21413 else if (*fnamep <= tail)
21414 *fnamelen = 0;
21415 }
21416 else /* :r */
21417 {
21418 if (s > tail) /* remove one extension */
21419 *fnamelen = (int)(s - *fnamep);
21420 }
21421 *usedlen += 2;
21422 }
21423
21424 /* ":s?pat?foo?" - substitute */
21425 /* ":gs?pat?foo?" - global substitute */
21426 if (src[*usedlen] == ':'
21427 && (src[*usedlen + 1] == 's'
21428 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21429 {
21430 char_u *str;
21431 char_u *pat;
21432 char_u *sub;
21433 int sep;
21434 char_u *flags;
21435 int didit = FALSE;
21436
21437 flags = (char_u *)"";
21438 s = src + *usedlen + 2;
21439 if (src[*usedlen + 1] == 'g')
21440 {
21441 flags = (char_u *)"g";
21442 ++s;
21443 }
21444
21445 sep = *s++;
21446 if (sep)
21447 {
21448 /* find end of pattern */
21449 p = vim_strchr(s, sep);
21450 if (p != NULL)
21451 {
21452 pat = vim_strnsave(s, (int)(p - s));
21453 if (pat != NULL)
21454 {
21455 s = p + 1;
21456 /* find end of substitution */
21457 p = vim_strchr(s, sep);
21458 if (p != NULL)
21459 {
21460 sub = vim_strnsave(s, (int)(p - s));
21461 str = vim_strnsave(*fnamep, *fnamelen);
21462 if (sub != NULL && str != NULL)
21463 {
21464 *usedlen = (int)(p + 1 - src);
21465 s = do_string_sub(str, pat, sub, flags);
21466 if (s != NULL)
21467 {
21468 *fnamep = s;
21469 *fnamelen = (int)STRLEN(s);
21470 vim_free(*bufp);
21471 *bufp = s;
21472 didit = TRUE;
21473 }
21474 }
21475 vim_free(sub);
21476 vim_free(str);
21477 }
21478 vim_free(pat);
21479 }
21480 }
21481 /* after using ":s", repeat all the modifiers */
21482 if (didit)
21483 goto repeat;
21484 }
21485 }
21486
21487 return valid;
21488}
21489
21490/*
21491 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21492 * "flags" can be "g" to do a global substitute.
21493 * Returns an allocated string, NULL for error.
21494 */
21495 char_u *
21496do_string_sub(str, pat, sub, flags)
21497 char_u *str;
21498 char_u *pat;
21499 char_u *sub;
21500 char_u *flags;
21501{
21502 int sublen;
21503 regmatch_T regmatch;
21504 int i;
21505 int do_all;
21506 char_u *tail;
21507 garray_T ga;
21508 char_u *ret;
21509 char_u *save_cpo;
21510
21511 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21512 save_cpo = p_cpo;
21513 p_cpo = (char_u *)"";
21514
21515 ga_init2(&ga, 1, 200);
21516
21517 do_all = (flags[0] == 'g');
21518
21519 regmatch.rm_ic = p_ic;
21520 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21521 if (regmatch.regprog != NULL)
21522 {
21523 tail = str;
21524 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21525 {
21526 /*
21527 * Get some space for a temporary buffer to do the substitution
21528 * into. It will contain:
21529 * - The text up to where the match is.
21530 * - The substituted text.
21531 * - The text after the match.
21532 */
21533 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21534 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21535 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21536 {
21537 ga_clear(&ga);
21538 break;
21539 }
21540
21541 /* copy the text up to where the match is */
21542 i = (int)(regmatch.startp[0] - tail);
21543 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21544 /* add the substituted text */
21545 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21546 + ga.ga_len + i, TRUE, TRUE, FALSE);
21547 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 /* avoid getting stuck on a match with an empty string */
21549 if (tail == regmatch.endp[0])
21550 {
21551 if (*tail == NUL)
21552 break;
21553 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21554 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 }
21556 else
21557 {
21558 tail = regmatch.endp[0];
21559 if (*tail == NUL)
21560 break;
21561 }
21562 if (!do_all)
21563 break;
21564 }
21565
21566 if (ga.ga_data != NULL)
21567 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21568
21569 vim_free(regmatch.regprog);
21570 }
21571
21572 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21573 ga_clear(&ga);
21574 p_cpo = save_cpo;
21575
21576 return ret;
21577}
21578
21579#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */