blob: 30bae9c2efb0163528d19f0c19f8a547ba306e3b [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));
372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
373static void list_glob_vars __ARGS((void));
374static void list_buf_vars __ARGS((void));
375static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
377static void list_tab_vars __ARGS((void));
378#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000380static void list_script_vars __ARGS((void));
381static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
383static 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));
707static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
708static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
709static 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
1321 clear_tv(&vimvars[idx].vv_tv);
1322 vimvars[idx].vv_tv = *save_tv;
1323 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1324 {
1325 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1326 if (HASHITEM_EMPTY(hi))
1327 EMSG2(_(e_intern2), "restore_vimvar()");
1328 else
1329 hash_remove(&vimvarht, hi);
1330 }
1331}
1332
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001333#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001334/*
1335 * Evaluate an expression to a list with suggestions.
1336 * For the "expr:" part of 'spellsuggest'.
1337 */
1338 list_T *
1339eval_spell_expr(badword, expr)
1340 char_u *badword;
1341 char_u *expr;
1342{
1343 typval_T save_val;
1344 typval_T rettv;
1345 list_T *list = NULL;
1346 char_u *p = skipwhite(expr);
1347
1348 /* Set "v:val" to the bad word. */
1349 prepare_vimvar(VV_VAL, &save_val);
1350 vimvars[VV_VAL].vv_type = VAR_STRING;
1351 vimvars[VV_VAL].vv_str = badword;
1352 if (p_verbose == 0)
1353 ++emsg_off;
1354
1355 if (eval1(&p, &rettv, TRUE) == OK)
1356 {
1357 if (rettv.v_type != VAR_LIST)
1358 clear_tv(&rettv);
1359 else
1360 list = rettv.vval.v_list;
1361 }
1362
1363 if (p_verbose == 0)
1364 --emsg_off;
1365 vimvars[VV_VAL].vv_str = NULL;
1366 restore_vimvar(VV_VAL, &save_val);
1367
1368 return list;
1369}
1370
1371/*
1372 * "list" is supposed to contain two items: a word and a number. Return the
1373 * word in "pp" and the number as the return value.
1374 * Return -1 if anything isn't right.
1375 * Used to get the good word and score from the eval_spell_expr() result.
1376 */
1377 int
1378get_spellword(list, pp)
1379 list_T *list;
1380 char_u **pp;
1381{
1382 listitem_T *li;
1383
1384 li = list->lv_first;
1385 if (li == NULL)
1386 return -1;
1387 *pp = get_tv_string(&li->li_tv);
1388
1389 li = li->li_next;
1390 if (li == NULL)
1391 return -1;
1392 return get_tv_number(&li->li_tv);
1393}
1394#endif
1395
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001396/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001397 * Top level evaluation function.
1398 * Returns an allocated typval_T with the result.
1399 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001400 */
1401 typval_T *
1402eval_expr(arg, nextcmd)
1403 char_u *arg;
1404 char_u **nextcmd;
1405{
1406 typval_T *tv;
1407
1408 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001409 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001410 {
1411 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001412 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001413 }
1414
1415 return tv;
1416}
1417
1418
Bram Moolenaar4f688582007-07-24 12:34:30 +00001419#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1420 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 static int
1427call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 char_u *func;
1429 int argc;
1430 char_u **argv;
1431 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
Bram Moolenaar33570922005-01-25 22:26:29 +00001434 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 long n;
1436 int len;
1437 int i;
1438 int doesrange;
1439 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001440 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001442 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 for (i = 0; i < argc; i++)
1447 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001448 /* Pass a NULL or empty argument as an empty string */
1449 if (argv[i] == NULL || *argv[i] == NUL)
1450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001451 argvars[i].v_type = VAR_STRING;
1452 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001453 continue;
1454 }
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 /* Recognize a number argument, the others must be strings. */
1457 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1458 if (len != 0 && len == (int)STRLEN(argv[i]))
1459 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001460 argvars[i].v_type = VAR_NUMBER;
1461 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 else
1464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001465 argvars[i].v_type = VAR_STRING;
1466 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468 }
1469
1470 if (safe)
1471 {
1472 save_funccalp = save_funccal();
1473 ++sandbox;
1474 }
1475
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001476 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1477 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (safe)
1481 {
1482 --sandbox;
1483 restore_funccal(save_funccalp);
1484 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485 vim_free(argvars);
1486
1487 if (ret == FAIL)
1488 clear_tv(rettv);
1489
1490 return ret;
1491}
1492
Bram Moolenaar4f688582007-07-24 12:34:30 +00001493# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001494/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001495 * Call vimL function "func" and return the result as a string.
1496 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497 * Uses argv[argc] for the function arguments.
1498 */
1499 void *
1500call_func_retstr(func, argc, argv, safe)
1501 char_u *func;
1502 int argc;
1503 char_u **argv;
1504 int safe; /* use the sandbox */
1505{
1506 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001507 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508
1509 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1510 return NULL;
1511
1512 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001513 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 return retval;
1515}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001516# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001517
Bram Moolenaar4f688582007-07-24 12:34:30 +00001518# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001519/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001520 * Call vimL function "func" and return the result as a number.
1521 * Returns -1 when calling the function fails.
1522 * Uses argv[argc] for the function arguments.
1523 */
1524 long
1525call_func_retnr(func, argc, argv, safe)
1526 char_u *func;
1527 int argc;
1528 char_u **argv;
1529 int safe; /* use the sandbox */
1530{
1531 typval_T rettv;
1532 long retval;
1533
1534 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1535 return -1;
1536
1537 retval = get_tv_number_chk(&rettv, NULL);
1538 clear_tv(&rettv);
1539 return retval;
1540}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001541# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001542
1543/*
1544 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001545 * Uses argv[argc] for the function arguments.
1546 */
1547 void *
1548call_func_retlist(func, argc, argv, safe)
1549 char_u *func;
1550 int argc;
1551 char_u **argv;
1552 int safe; /* use the sandbox */
1553{
1554 typval_T rettv;
1555
1556 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1557 return NULL;
1558
1559 if (rettv.v_type != VAR_LIST)
1560 {
1561 clear_tv(&rettv);
1562 return NULL;
1563 }
1564
1565 return rettv.vval.v_list;
1566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567#endif
1568
Bram Moolenaar4f688582007-07-24 12:34:30 +00001569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570/*
1571 * Save the current function call pointer, and set it to NULL.
1572 * Used when executing autocommands and for ":source".
1573 */
1574 void *
1575save_funccal()
1576{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001577 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 current_funccal = NULL;
1580 return (void *)fc;
1581}
1582
1583 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001584restore_funccal(vfc)
1585 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001587 funccall_T *fc = (funccall_T *)vfc;
1588
1589 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590}
1591
Bram Moolenaar05159a02005-02-26 23:04:13 +00001592#if defined(FEAT_PROFILE) || defined(PROTO)
1593/*
1594 * Prepare profiling for entering a child or something else that is not
1595 * counted for the script/function itself.
1596 * Should always be called in pair with prof_child_exit().
1597 */
1598 void
1599prof_child_enter(tm)
1600 proftime_T *tm; /* place to store waittime */
1601{
1602 funccall_T *fc = current_funccal;
1603
1604 if (fc != NULL && fc->func->uf_profiling)
1605 profile_start(&fc->prof_child);
1606 script_prof_save(tm);
1607}
1608
1609/*
1610 * Take care of time spent in a child.
1611 * Should always be called after prof_child_enter().
1612 */
1613 void
1614prof_child_exit(tm)
1615 proftime_T *tm; /* where waittime was stored */
1616{
1617 funccall_T *fc = current_funccal;
1618
1619 if (fc != NULL && fc->func->uf_profiling)
1620 {
1621 profile_end(&fc->prof_child);
1622 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1623 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1624 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1625 }
1626 script_prof_restore(tm);
1627}
1628#endif
1629
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#ifdef FEAT_FOLDING
1632/*
1633 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1634 * it in "*cp". Doesn't give error messages.
1635 */
1636 int
1637eval_foldexpr(arg, cp)
1638 char_u *arg;
1639 int *cp;
1640{
Bram Moolenaar33570922005-01-25 22:26:29 +00001641 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 int retval;
1643 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001644 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1645 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001648 if (use_sandbox)
1649 ++sandbox;
1650 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 retval = 0;
1654 else
1655 {
1656 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 if (tv.v_type == VAR_NUMBER)
1658 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001659 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 retval = 0;
1661 else
1662 {
1663 /* If the result is a string, check if there is a non-digit before
1664 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001665 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (!VIM_ISDIGIT(*s) && *s != '-')
1667 *cp = *s++;
1668 retval = atol((char *)s);
1669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
1672 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001673 if (use_sandbox)
1674 --sandbox;
1675 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677 return retval;
1678}
1679#endif
1680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 * ":let" list all variable values
1683 * ":let var1 var2" list variable values
1684 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 * ":let var += expr" assignment command.
1686 * ":let var -= expr" assignment command.
1687 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001688 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 */
1690 void
1691ex_let(eap)
1692 exarg_T *eap;
1693{
1694 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001695 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698 int var_count = 0;
1699 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001701 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaardb552d602006-03-23 22:59:57 +00001703 argend = skip_var_list(arg, &var_count, &semicolon);
1704 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001706 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1707 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001708 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001711 /*
1712 * ":let" without "=": list variables
1713 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 if (*arg == '[')
1715 EMSG(_(e_invarg));
1716 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001717 /* ":let var1 var2" */
1718 arg = list_arg_vars(eap, arg);
1719 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001722 list_glob_vars();
1723 list_buf_vars();
1724 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001725#ifdef FEAT_WINDOWS
1726 list_tab_vars();
1727#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001728 list_script_vars();
1729 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001730 list_vim_vars();
1731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 eap->nextcmd = check_nextcmd(arg);
1733 }
1734 else
1735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001736 op[0] = '=';
1737 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001738 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 {
1740 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1741 op[0] = expr[-1]; /* +=, -= or .= */
1742 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 if (eap->skip)
1746 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (eap->skip)
1749 {
1750 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001751 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 --emsg_skip;
1753 }
1754 else if (i != FAIL)
1755 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760 }
1761}
1762
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763/*
1764 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1765 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 * When "nextchars" is not NULL it points to a string with characters that
1767 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1768 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 * Returns OK or FAIL;
1770 */
1771 static int
1772ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1773 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001774 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 int copy; /* copy values from "tv", don't move */
1776 int semicolon; /* from skip_var_list() */
1777 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779{
1780 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 listitem_T *item;
1784 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785
1786 if (*arg != '[')
1787 {
1788 /*
1789 * ":let var = expr" or ":for var in list"
1790 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001791 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 return FAIL;
1793 return OK;
1794 }
1795
1796 /*
1797 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1798 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001799 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800 {
1801 EMSG(_(e_listreq));
1802 return FAIL;
1803 }
1804
1805 i = list_len(l);
1806 if (semicolon == 0 && var_count < i)
1807 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001808 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 return FAIL;
1810 }
1811 if (var_count - semicolon > i)
1812 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001813 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 return FAIL;
1815 }
1816
1817 item = l->lv_first;
1818 while (*arg != ']')
1819 {
1820 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 item = item->li_next;
1823 if (arg == NULL)
1824 return FAIL;
1825
1826 arg = skipwhite(arg);
1827 if (*arg == ';')
1828 {
1829 /* Put the rest of the list (may be empty) in the var after ';'.
1830 * Create a new list for this. */
1831 l = list_alloc();
1832 if (l == NULL)
1833 return FAIL;
1834 while (item != NULL)
1835 {
1836 list_append_tv(l, &item->li_tv);
1837 item = item->li_next;
1838 }
1839
1840 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001841 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 ltv.vval.v_list = l;
1843 l->lv_refcount = 1;
1844
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1846 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 clear_tv(&ltv);
1848 if (arg == NULL)
1849 return FAIL;
1850 break;
1851 }
1852 else if (*arg != ',' && *arg != ']')
1853 {
1854 EMSG2(_(e_intern2), "ex_let_vars()");
1855 return FAIL;
1856 }
1857 }
1858
1859 return OK;
1860}
1861
1862/*
1863 * Skip over assignable variable "var" or list of variables "[var, var]".
1864 * Used for ":let varvar = expr" and ":for varvar in expr".
1865 * For "[var, var]" increment "*var_count" for each variable.
1866 * for "[var, var; var]" set "semicolon".
1867 * Return NULL for an error.
1868 */
1869 static char_u *
1870skip_var_list(arg, var_count, semicolon)
1871 char_u *arg;
1872 int *var_count;
1873 int *semicolon;
1874{
1875 char_u *p, *s;
1876
1877 if (*arg == '[')
1878 {
1879 /* "[var, var]": find the matching ']'. */
1880 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001881 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 {
1883 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1884 s = skip_var_one(p);
1885 if (s == p)
1886 {
1887 EMSG2(_(e_invarg2), p);
1888 return NULL;
1889 }
1890 ++*var_count;
1891
1892 p = skipwhite(s);
1893 if (*p == ']')
1894 break;
1895 else if (*p == ';')
1896 {
1897 if (*semicolon == 1)
1898 {
1899 EMSG(_("Double ; in list of variables"));
1900 return NULL;
1901 }
1902 *semicolon = 1;
1903 }
1904 else if (*p != ',')
1905 {
1906 EMSG2(_(e_invarg2), p);
1907 return NULL;
1908 }
1909 }
1910 return p + 1;
1911 }
1912 else
1913 return skip_var_one(arg);
1914}
1915
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001916/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001917 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001918 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001919 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 static char_u *
1921skip_var_one(arg)
1922 char_u *arg;
1923{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001924 if (*arg == '@' && arg[1] != NUL)
1925 return arg + 2;
1926 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1927 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928}
1929
Bram Moolenaara7043832005-01-21 11:56:39 +00001930/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 * List variables for hashtab "ht" with prefix "prefix".
1932 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001935list_hashtable_vars(ht, prefix, empty)
1936 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 int empty;
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)
1953 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001954 }
1955 }
1956}
1957
1958/*
1959 * List global variables.
1960 */
1961 static void
1962list_glob_vars()
1963{
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001965}
1966
1967/*
1968 * List buffer variables.
1969 */
1970 static void
1971list_buf_vars()
1972{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001973 char_u numbuf[NUMBUFLEN];
1974
Bram Moolenaar33570922005-01-25 22:26:29 +00001975 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001976
1977 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1978 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001979}
1980
1981/*
1982 * List window variables.
1983 */
1984 static void
1985list_win_vars()
1986{
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001988}
1989
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001990#ifdef FEAT_WINDOWS
1991/*
1992 * List tab page variables.
1993 */
1994 static void
1995list_tab_vars()
1996{
1997 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1998}
1999#endif
2000
Bram Moolenaara7043832005-01-21 11:56:39 +00002001/*
2002 * List Vim variables.
2003 */
2004 static void
2005list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006{
Bram Moolenaar33570922005-01-25 22:26:29 +00002007 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002008}
2009
2010/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002011 * List script-local variables, if there is a script.
2012 */
2013 static void
2014list_script_vars()
2015{
2016 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
2017 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
2018}
2019
2020/*
2021 * List function variables, if there is a function.
2022 */
2023 static void
2024list_func_vars()
2025{
2026 if (current_funccal != NULL)
2027 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2028 (char_u *)"l:", FALSE);
2029}
2030
2031/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 * List variables in "arg".
2033 */
2034 static char_u *
2035list_arg_vars(eap, arg)
2036 exarg_T *eap;
2037 char_u *arg;
2038{
2039 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002040 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002042 char_u *name_start;
2043 char_u *arg_subsc;
2044 char_u *tofree;
2045 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046
2047 while (!ends_excmd(*arg) && !got_int)
2048 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002049 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002051 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2053 {
2054 emsg_severe = TRUE;
2055 EMSG(_(e_trailing));
2056 break;
2057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002061 /* get_name_len() takes care of expanding curly braces */
2062 name_start = name = arg;
2063 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2064 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 /* This is mainly to keep test 49 working: when expanding
2067 * curly braces fails overrule the exception error message. */
2068 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002070 emsg_severe = TRUE;
2071 EMSG2(_(e_invarg2), arg);
2072 break;
2073 }
2074 error = TRUE;
2075 }
2076 else
2077 {
2078 if (tofree != NULL)
2079 name = tofree;
2080 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002082 else
2083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 /* handle d.key, l[idx], f(expr) */
2085 arg_subsc = arg;
2086 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002088 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002090 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002091 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002092 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002094 case 'g': list_glob_vars(); break;
2095 case 'b': list_buf_vars(); break;
2096 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002097#ifdef FEAT_WINDOWS
2098 case 't': list_tab_vars(); break;
2099#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002101 case 's': list_script_vars(); break;
2102 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103 default:
2104 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 }
2107 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002108 {
2109 char_u numbuf[NUMBUFLEN];
2110 char_u *tf;
2111 int c;
2112 char_u *s;
2113
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002114 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002115 c = *arg;
2116 *arg = NUL;
2117 list_one_var_a((char_u *)"",
2118 arg == arg_subsc ? name : name_start,
2119 tv.v_type, s == NULL ? (char_u *)"" : s);
2120 *arg = c;
2121 vim_free(tf);
2122 }
2123 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 }
2126 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002127
2128 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130
2131 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132 }
2133
2134 return arg;
2135}
2136
2137/*
2138 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2139 * Returns a pointer to the char just after the var name.
2140 * Returns NULL if there is an error.
2141 */
2142 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002143ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002144 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002145 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002147 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002148 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149{
2150 int c1;
2151 char_u *name;
2152 char_u *p;
2153 char_u *arg_end = NULL;
2154 int len;
2155 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002156 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157
2158 /*
2159 * ":let $VAR = expr": Set environment variable.
2160 */
2161 if (*arg == '$')
2162 {
2163 /* Find the end of the name. */
2164 ++arg;
2165 name = arg;
2166 len = get_env_len(&arg);
2167 if (len == 0)
2168 EMSG2(_(e_invarg2), name - 1);
2169 else
2170 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 if (op != NULL && (*op == '+' || *op == '-'))
2172 EMSG2(_(e_letwrong), op);
2173 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 EMSG(_(e_letunexp));
2176 else
2177 {
2178 c1 = name[len];
2179 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002180 p = get_tv_string_chk(tv);
2181 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002182 {
2183 int mustfree = FALSE;
2184 char_u *s = vim_getenv(name, &mustfree);
2185
2186 if (s != NULL)
2187 {
2188 p = tofree = concat_str(s, p);
2189 if (mustfree)
2190 vim_free(s);
2191 }
2192 }
2193 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002194 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002195 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 if (STRICMP(name, "HOME") == 0)
2197 init_homedir();
2198 else if (didset_vim && STRICMP(name, "VIM") == 0)
2199 didset_vim = FALSE;
2200 else if (didset_vimruntime
2201 && STRICMP(name, "VIMRUNTIME") == 0)
2202 didset_vimruntime = FALSE;
2203 arg_end = arg;
2204 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002206 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 }
2208 }
2209 }
2210
2211 /*
2212 * ":let &option = expr": Set option value.
2213 * ":let &l:option = expr": Set local option value.
2214 * ":let &g:option = expr": Set global option value.
2215 */
2216 else if (*arg == '&')
2217 {
2218 /* Find the end of the name. */
2219 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002220 if (p == NULL || (endchars != NULL
2221 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 EMSG(_(e_letunexp));
2223 else
2224 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002225 long n;
2226 int opt_type;
2227 long numval;
2228 char_u *stringval = NULL;
2229 char_u *s;
2230
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 c1 = *p;
2232 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002233
2234 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002235 s = get_tv_string_chk(tv); /* != NULL if number or string */
2236 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002237 {
2238 opt_type = get_option_value(arg, &numval,
2239 &stringval, opt_flags);
2240 if ((opt_type == 1 && *op == '.')
2241 || (opt_type == 0 && *op != '.'))
2242 EMSG2(_(e_letwrong), op);
2243 else
2244 {
2245 if (opt_type == 1) /* number */
2246 {
2247 if (*op == '+')
2248 n = numval + n;
2249 else
2250 n = numval - n;
2251 }
2252 else if (opt_type == 0 && stringval != NULL) /* string */
2253 {
2254 s = concat_str(stringval, s);
2255 vim_free(stringval);
2256 stringval = s;
2257 }
2258 }
2259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002260 if (s != NULL)
2261 {
2262 set_option_value(arg, n, s, opt_flags);
2263 arg_end = p;
2264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002266 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 }
2268 }
2269
2270 /*
2271 * ":let @r = expr": Set register contents.
2272 */
2273 else if (*arg == '@')
2274 {
2275 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 if (op != NULL && (*op == '+' || *op == '-'))
2277 EMSG2(_(e_letwrong), op);
2278 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002279 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 EMSG(_(e_letunexp));
2281 else
2282 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002283 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002284 char_u *s;
2285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002286 p = get_tv_string_chk(tv);
2287 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002288 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002289 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002290 if (s != NULL)
2291 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002292 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 vim_free(s);
2294 }
2295 }
2296 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002297 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002298 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002299 arg_end = arg + 1;
2300 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002301 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 }
2303 }
2304
2305 /*
2306 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002309 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002313 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2317 EMSG(_(e_letunexp));
2318 else
2319 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 arg_end = p;
2322 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 }
2326
2327 else
2328 EMSG2(_(e_invarg2), arg);
2329
2330 return arg_end;
2331}
2332
2333/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002334 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2335 */
2336 static int
2337check_changedtick(arg)
2338 char_u *arg;
2339{
2340 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2341 {
2342 EMSG2(_(e_readonlyvar), arg);
2343 return TRUE;
2344 }
2345 return FALSE;
2346}
2347
2348/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 * Get an lval: variable, Dict item or List item that can be assigned a value
2350 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2351 * "name.key", "name.key[expr]" etc.
2352 * Indexing only works if "name" is an existing List or Dictionary.
2353 * "name" points to the start of the name.
2354 * If "rettv" is not NULL it points to the value to be assigned.
2355 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2356 * wrong; must end in space or cmd separator.
2357 *
2358 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002359 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 */
2362 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002363get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002365 typval_T *rettv;
2366 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 int unlet;
2368 int skip;
2369 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002370 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 char_u *expr_start, *expr_end;
2374 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002375 dictitem_T *v;
2376 typval_T var1;
2377 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002379 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002382 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002385 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386
2387 if (skip)
2388 {
2389 /* When skipping just find the end of the name. */
2390 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002391 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 }
2393
2394 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002395 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 if (expr_start != NULL)
2397 {
2398 /* Don't expand the name when we already know there is an error. */
2399 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2400 && *p != '[' && *p != '.')
2401 {
2402 EMSG(_(e_trailing));
2403 return NULL;
2404 }
2405
2406 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2407 if (lp->ll_exp_name == NULL)
2408 {
2409 /* Report an invalid expression in braces, unless the
2410 * expression evaluation has been cancelled due to an
2411 * aborting error, an interrupt, or an exception. */
2412 if (!aborting() && !quiet)
2413 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002414 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 EMSG2(_(e_invarg2), name);
2416 return NULL;
2417 }
2418 }
2419 lp->ll_name = lp->ll_exp_name;
2420 }
2421 else
2422 lp->ll_name = name;
2423
2424 /* Without [idx] or .key we are done. */
2425 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2426 return p;
2427
2428 cc = *p;
2429 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002430 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 if (v == NULL && !quiet)
2432 EMSG2(_(e_undefvar), lp->ll_name);
2433 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 if (v == NULL)
2435 return NULL;
2436
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 /*
2438 * Loop until no more [idx] or .key is following.
2439 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002440 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2444 && !(lp->ll_tv->v_type == VAR_DICT
2445 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (!quiet)
2448 EMSG(_("E689: Can only index a List or Dictionary"));
2449 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (!quiet)
2454 EMSG(_("E708: [:] must come last"));
2455 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002457
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 len = -1;
2459 if (*p == '.')
2460 {
2461 key = p + 1;
2462 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2463 ;
2464 if (len == 0)
2465 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (!quiet)
2467 EMSG(_(e_emptykey));
2468 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 }
2470 p = key + len;
2471 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002472 else
2473 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002475 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 if (*p == ':')
2477 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002478 else
2479 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 empty1 = FALSE;
2481 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002483 if (get_tv_string_chk(&var1) == NULL)
2484 {
2485 /* not a number or string */
2486 clear_tv(&var1);
2487 return NULL;
2488 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 }
2490
2491 /* Optionally get the second index [ :expr]. */
2492 if (*p == ':')
2493 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002498 if (!empty1)
2499 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002501 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (rettv != NULL && (rettv->v_type != VAR_LIST
2503 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 if (!quiet)
2506 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 if (!empty1)
2508 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 }
2511 p = skipwhite(p + 1);
2512 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 else
2515 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2518 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 if (!empty1)
2520 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002523 if (get_tv_string_chk(&var2) == NULL)
2524 {
2525 /* not a number or string */
2526 if (!empty1)
2527 clear_tv(&var1);
2528 clear_tv(&var2);
2529 return NULL;
2530 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002536
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002538 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 if (!quiet)
2540 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 if (!empty1)
2542 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 }
2547
2548 /* Skip to past ']'. */
2549 ++p;
2550 }
2551
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 {
2554 if (len == -1)
2555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002557 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 if (*key == NUL)
2559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (!quiet)
2561 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 }
2565 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566 lp->ll_list = NULL;
2567 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002568 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002571 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002575 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 if (len == -1)
2577 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 }
2580 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 if (len == -1)
2585 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 p = NULL;
2588 break;
2589 }
2590 if (len == -1)
2591 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 }
2594 else
2595 {
2596 /*
2597 * Get the number and item for the only or first index of the List.
2598 */
2599 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 else
2602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002603 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 clear_tv(&var1);
2605 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002606 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 lp->ll_list = lp->ll_tv->vval.v_list;
2608 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2609 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002611 if (lp->ll_n1 < 0)
2612 {
2613 lp->ll_n1 = 0;
2614 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2615 }
2616 }
2617 if (lp->ll_li == NULL)
2618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623
2624 /*
2625 * May need to find the item or absolute index for the second
2626 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 * When no index given: "lp->ll_empty2" is TRUE.
2628 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002632 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2643 if (lp->ll_n1 < 0)
2644 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2645 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 }
2648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 }
2652
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return p;
2654}
2655
2656/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002657 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 */
2659 static void
2660clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002661 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662{
2663 vim_free(lp->ll_exp_name);
2664 vim_free(lp->ll_newkey);
2665}
2666
2667/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002668 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 */
2672 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002676 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002678 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679{
2680 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002681 listitem_T *ri;
2682 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683
2684 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 cc = *endp;
2689 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690 if (op != NULL && *op != '=')
2691 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002692 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002694 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002695 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002696 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002697 {
2698 if (tv_op(&tv, rettv, op) == OK)
2699 set_var(lp->ll_name, &tv, FALSE);
2700 clear_tv(&tv);
2701 }
2702 }
2703 else
2704 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002706 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002708 else if (tv_check_lock(lp->ll_newkey == NULL
2709 ? lp->ll_tv->v_lock
2710 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2711 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 else if (lp->ll_range)
2713 {
2714 /*
2715 * Assign the List values to the list items.
2716 */
2717 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 if (op != NULL && *op != '=')
2720 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2721 else
2722 {
2723 clear_tv(&lp->ll_li->li_tv);
2724 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2725 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 ri = ri->li_next;
2727 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2728 break;
2729 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002732 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 ri = NULL;
2735 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_li = lp->ll_li->li_next;
2739 ++lp->ll_n1;
2740 }
2741 if (ri != NULL)
2742 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002743 else if (lp->ll_empty2
2744 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 : lp->ll_n1 != lp->ll_n2)
2746 EMSG(_("E711: List value has not enough items"));
2747 }
2748 else
2749 {
2750 /*
2751 * Assign to a List or Dictionary item.
2752 */
2753 if (lp->ll_newkey != NULL)
2754 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755 if (op != NULL && *op != '=')
2756 {
2757 EMSG2(_(e_letwrong), op);
2758 return;
2759 }
2760
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002762 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (di == NULL)
2764 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002765 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2766 {
2767 vim_free(di);
2768 return;
2769 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 else if (op != NULL && *op != '=')
2773 {
2774 tv_op(lp->ll_tv, rettv, op);
2775 return;
2776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002777 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 /*
2781 * Assign the value to the variable or list item.
2782 */
2783 if (copy)
2784 copy_tv(rettv, lp->ll_tv);
2785 else
2786 {
2787 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002788 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002790 }
2791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002792}
2793
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002794/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2796 * Returns OK or FAIL.
2797 */
2798 static int
2799tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 typval_T *tv1;
2801 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 char_u *op;
2803{
2804 long n;
2805 char_u numbuf[NUMBUFLEN];
2806 char_u *s;
2807
2808 /* Can't do anything with a Funcref or a Dict on the right. */
2809 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2810 {
2811 switch (tv1->v_type)
2812 {
2813 case VAR_DICT:
2814 case VAR_FUNC:
2815 break;
2816
2817 case VAR_LIST:
2818 if (*op != '+' || tv2->v_type != VAR_LIST)
2819 break;
2820 /* List += List */
2821 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2822 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2823 return OK;
2824
2825 case VAR_NUMBER:
2826 case VAR_STRING:
2827 if (tv2->v_type == VAR_LIST)
2828 break;
2829 if (*op == '+' || *op == '-')
2830 {
2831 /* nr += nr or nr -= nr*/
2832 n = get_tv_number(tv1);
2833 if (*op == '+')
2834 n += get_tv_number(tv2);
2835 else
2836 n -= get_tv_number(tv2);
2837 clear_tv(tv1);
2838 tv1->v_type = VAR_NUMBER;
2839 tv1->vval.v_number = n;
2840 }
2841 else
2842 {
2843 /* str .= str */
2844 s = get_tv_string(tv1);
2845 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2846 clear_tv(tv1);
2847 tv1->v_type = VAR_STRING;
2848 tv1->vval.v_string = s;
2849 }
2850 return OK;
2851 }
2852 }
2853
2854 EMSG2(_(e_letwrong), op);
2855 return FAIL;
2856}
2857
2858/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002859 * Add a watcher to a list.
2860 */
2861 static void
2862list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 list_T *l;
2864 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002865{
2866 lw->lw_next = l->lv_watch;
2867 l->lv_watch = lw;
2868}
2869
2870/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002871 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872 * No warning when it isn't found...
2873 */
2874 static void
2875list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002876 list_T *l;
2877 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002878{
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002880
2881 lwp = &l->lv_watch;
2882 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2883 {
2884 if (lw == lwrem)
2885 {
2886 *lwp = lw->lw_next;
2887 break;
2888 }
2889 lwp = &lw->lw_next;
2890 }
2891}
2892
2893/*
2894 * Just before removing an item from a list: advance watchers to the next
2895 * item.
2896 */
2897 static void
2898list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 list_T *l;
2900 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002901{
Bram Moolenaar33570922005-01-25 22:26:29 +00002902 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903
2904 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2905 if (lw->lw_item == item)
2906 lw->lw_item = item->li_next;
2907}
2908
2909/*
2910 * Evaluate the expression used in a ":for var in expr" command.
2911 * "arg" points to "var".
2912 * Set "*errp" to TRUE for an error, FALSE otherwise;
2913 * Return a pointer that holds the info. Null when there is an error.
2914 */
2915 void *
2916eval_for_line(arg, errp, nextcmdp, skip)
2917 char_u *arg;
2918 int *errp;
2919 char_u **nextcmdp;
2920 int skip;
2921{
Bram Moolenaar33570922005-01-25 22:26:29 +00002922 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T tv;
2925 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002926
2927 *errp = TRUE; /* default: there is an error */
2928
Bram Moolenaar33570922005-01-25 22:26:29 +00002929 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002930 if (fi == NULL)
2931 return NULL;
2932
2933 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2934 if (expr == NULL)
2935 return fi;
2936
2937 expr = skipwhite(expr);
2938 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2939 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002940 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941 return fi;
2942 }
2943
2944 if (skip)
2945 ++emsg_skip;
2946 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2947 {
2948 *errp = FALSE;
2949 if (!skip)
2950 {
2951 l = tv.vval.v_list;
2952 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002953 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002954 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002955 clear_tv(&tv);
2956 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002957 else
2958 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002959 /* No need to increment the refcount, it's already set for the
2960 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002961 fi->fi_list = l;
2962 list_add_watch(l, &fi->fi_lw);
2963 fi->fi_lw.lw_item = l->lv_first;
2964 }
2965 }
2966 }
2967 if (skip)
2968 --emsg_skip;
2969
2970 return fi;
2971}
2972
2973/*
2974 * Use the first item in a ":for" list. Advance to the next.
2975 * Assign the values to the variable (list). "arg" points to the first one.
2976 * Return TRUE when a valid item was found, FALSE when at end of list or
2977 * something wrong.
2978 */
2979 int
2980next_for_item(fi_void, arg)
2981 void *fi_void;
2982 char_u *arg;
2983{
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002985 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002986 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002987
2988 item = fi->fi_lw.lw_item;
2989 if (item == NULL)
2990 result = FALSE;
2991 else
2992 {
2993 fi->fi_lw.lw_item = item->li_next;
2994 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2995 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2996 }
2997 return result;
2998}
2999
3000/*
3001 * Free the structure used to store info used by ":for".
3002 */
3003 void
3004free_for_info(fi_void)
3005 void *fi_void;
3006{
Bram Moolenaar33570922005-01-25 22:26:29 +00003007 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003008
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003009 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003010 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003012 list_unref(fi->fi_list);
3013 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003014 vim_free(fi);
3015}
3016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3018
3019 void
3020set_context_for_expression(xp, arg, cmdidx)
3021 expand_T *xp;
3022 char_u *arg;
3023 cmdidx_T cmdidx;
3024{
3025 int got_eq = FALSE;
3026 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003027 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003029 if (cmdidx == CMD_let)
3030 {
3031 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003032 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003033 {
3034 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003035 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003036 {
3037 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003038 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003039 if (vim_iswhite(*p))
3040 break;
3041 }
3042 return;
3043 }
3044 }
3045 else
3046 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3047 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 while ((xp->xp_pattern = vim_strpbrk(arg,
3049 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3050 {
3051 c = *xp->xp_pattern;
3052 if (c == '&')
3053 {
3054 c = xp->xp_pattern[1];
3055 if (c == '&')
3056 {
3057 ++xp->xp_pattern;
3058 xp->xp_context = cmdidx != CMD_let || got_eq
3059 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3060 }
3061 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003064 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3065 xp->xp_pattern += 2;
3066
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 }
3069 else if (c == '$')
3070 {
3071 /* environment variable */
3072 xp->xp_context = EXPAND_ENV_VARS;
3073 }
3074 else if (c == '=')
3075 {
3076 got_eq = TRUE;
3077 xp->xp_context = EXPAND_EXPRESSION;
3078 }
3079 else if (c == '<'
3080 && xp->xp_context == EXPAND_FUNCTIONS
3081 && vim_strchr(xp->xp_pattern, '(') == NULL)
3082 {
3083 /* Function name can start with "<SNR>" */
3084 break;
3085 }
3086 else if (cmdidx != CMD_let || got_eq)
3087 {
3088 if (c == '"') /* string */
3089 {
3090 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3091 if (c == '\\' && xp->xp_pattern[1] != NUL)
3092 ++xp->xp_pattern;
3093 xp->xp_context = EXPAND_NOTHING;
3094 }
3095 else if (c == '\'') /* literal string */
3096 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003097 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3099 /* skip */ ;
3100 xp->xp_context = EXPAND_NOTHING;
3101 }
3102 else if (c == '|')
3103 {
3104 if (xp->xp_pattern[1] == '|')
3105 {
3106 ++xp->xp_pattern;
3107 xp->xp_context = EXPAND_EXPRESSION;
3108 }
3109 else
3110 xp->xp_context = EXPAND_COMMANDS;
3111 }
3112 else
3113 xp->xp_context = EXPAND_EXPRESSION;
3114 }
3115 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003116 /* Doesn't look like something valid, expand as an expression
3117 * anyway. */
3118 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 arg = xp->xp_pattern;
3120 if (*arg != NUL)
3121 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3122 /* skip */ ;
3123 }
3124 xp->xp_pattern = arg;
3125}
3126
3127#endif /* FEAT_CMDL_COMPL */
3128
3129/*
3130 * ":1,25call func(arg1, arg2)" function call.
3131 */
3132 void
3133ex_call(eap)
3134 exarg_T *eap;
3135{
3136 char_u *arg = eap->arg;
3137 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 linenr_T lnum;
3143 int doesrange;
3144 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003145 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003147 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003148 if (fudi.fd_newkey != NULL)
3149 {
3150 /* Still need to give an error message for missing key. */
3151 EMSG2(_(e_dictkey), fudi.fd_newkey);
3152 vim_free(fudi.fd_newkey);
3153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 if (tofree == NULL)
3155 return;
3156
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003157 /* Increase refcount on dictionary, it could get deleted when evaluating
3158 * the arguments. */
3159 if (fudi.fd_dict != NULL)
3160 ++fudi.fd_dict->dv_refcount;
3161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003162 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003163 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003164 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaar532c7802005-01-27 14:44:31 +00003166 /* Skip white space to allow ":call func ()". Not good, but required for
3167 * backward compatibility. */
3168 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003169 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 if (*startarg != '(')
3172 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003173 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 goto end;
3175 }
3176
3177 /*
3178 * When skipping, evaluate the function once, to find the end of the
3179 * arguments.
3180 * When the function takes a range, this is discovered after the first
3181 * call, and the loop is broken.
3182 */
3183 if (eap->skip)
3184 {
3185 ++emsg_skip;
3186 lnum = eap->line2; /* do it once, also with an invalid range */
3187 }
3188 else
3189 lnum = eap->line1;
3190 for ( ; lnum <= eap->line2; ++lnum)
3191 {
3192 if (!eap->skip && eap->addr_count > 0)
3193 {
3194 curwin->w_cursor.lnum = lnum;
3195 curwin->w_cursor.col = 0;
3196 }
3197 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003198 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003199 eap->line1, eap->line2, &doesrange,
3200 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
3202 failed = TRUE;
3203 break;
3204 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003205
3206 /* Handle a function returning a Funcref, Dictionary or List. */
3207 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3208 {
3209 failed = TRUE;
3210 break;
3211 }
3212
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003213 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 if (doesrange || eap->skip)
3215 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003218 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003219 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003220 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (aborting())
3222 break;
3223 }
3224 if (eap->skip)
3225 --emsg_skip;
3226
3227 if (!failed)
3228 {
3229 /* Check for trailing illegal characters and a following command. */
3230 if (!ends_excmd(*arg))
3231 {
3232 emsg_severe = TRUE;
3233 EMSG(_(e_trailing));
3234 }
3235 else
3236 eap->nextcmd = check_nextcmd(arg);
3237 }
3238
3239end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003240 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003241 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242}
3243
3244/*
3245 * ":unlet[!] var1 ... " command.
3246 */
3247 void
3248ex_unlet(eap)
3249 exarg_T *eap;
3250{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003251 ex_unletlock(eap, eap->arg, 0);
3252}
3253
3254/*
3255 * ":lockvar" and ":unlockvar" commands
3256 */
3257 void
3258ex_lockvar(eap)
3259 exarg_T *eap;
3260{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003262 int deep = 2;
3263
3264 if (eap->forceit)
3265 deep = -1;
3266 else if (vim_isdigit(*arg))
3267 {
3268 deep = getdigits(&arg);
3269 arg = skipwhite(arg);
3270 }
3271
3272 ex_unletlock(eap, arg, deep);
3273}
3274
3275/*
3276 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3277 */
3278 static void
3279ex_unletlock(eap, argstart, deep)
3280 exarg_T *eap;
3281 char_u *argstart;
3282 int deep;
3283{
3284 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003287 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288
3289 do
3290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003291 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003292 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3293 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003294 if (lv.ll_name == NULL)
3295 error = TRUE; /* error but continue parsing */
3296 if (name_end == NULL || (!vim_iswhite(*name_end)
3297 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003299 if (name_end != NULL)
3300 {
3301 emsg_severe = TRUE;
3302 EMSG(_(e_trailing));
3303 }
3304 if (!(eap->skip || error))
3305 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 break;
3307 }
3308
3309 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003310 {
3311 if (eap->cmdidx == CMD_unlet)
3312 {
3313 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3314 error = TRUE;
3315 }
3316 else
3317 {
3318 if (do_lock_var(&lv, name_end, deep,
3319 eap->cmdidx == CMD_lockvar) == FAIL)
3320 error = TRUE;
3321 }
3322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003324 if (!eap->skip)
3325 clear_lval(&lv);
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 arg = skipwhite(name_end);
3328 } while (!ends_excmd(*arg));
3329
3330 eap->nextcmd = check_nextcmd(arg);
3331}
3332
Bram Moolenaar8c711452005-01-14 21:53:12 +00003333 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003334do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003335 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003336 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003337 int forceit;
3338{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003339 int ret = OK;
3340 int cc;
3341
3342 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003343 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003344 cc = *name_end;
3345 *name_end = NUL;
3346
3347 /* Normal name or expanded name. */
3348 if (check_changedtick(lp->ll_name))
3349 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003350 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003352 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003353 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003354 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3355 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003356 else if (lp->ll_range)
3357 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003358 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003359
3360 /* Delete a range of List items. */
3361 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3362 {
3363 li = lp->ll_li->li_next;
3364 listitem_remove(lp->ll_list, lp->ll_li);
3365 lp->ll_li = li;
3366 ++lp->ll_n1;
3367 }
3368 }
3369 else
3370 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003371 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003372 /* unlet a List item. */
3373 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003374 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003375 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003376 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003377 }
3378
3379 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003380}
3381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382/*
3383 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003384 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 */
3386 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003387do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003389 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
Bram Moolenaar33570922005-01-25 22:26:29 +00003391 hashtab_T *ht;
3392 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003393 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaar33570922005-01-25 22:26:29 +00003395 ht = find_var_ht(name, &varname);
3396 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003398 hi = hash_find(ht, varname);
3399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003400 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003401 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3402 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003403 if (var_check_ro(HI2DI(hi)->di_flags, name))
3404 return FAIL;
3405 delete_var(ht, hi);
3406 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003409 if (forceit)
3410 return OK;
3411 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 return FAIL;
3413}
3414
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003415/*
3416 * Lock or unlock variable indicated by "lp".
3417 * "deep" is the levels to go (-1 for unlimited);
3418 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3419 */
3420 static int
3421do_lock_var(lp, name_end, deep, lock)
3422 lval_T *lp;
3423 char_u *name_end;
3424 int deep;
3425 int lock;
3426{
3427 int ret = OK;
3428 int cc;
3429 dictitem_T *di;
3430
3431 if (deep == 0) /* nothing to do */
3432 return OK;
3433
3434 if (lp->ll_tv == NULL)
3435 {
3436 cc = *name_end;
3437 *name_end = NUL;
3438
3439 /* Normal name or expanded name. */
3440 if (check_changedtick(lp->ll_name))
3441 ret = FAIL;
3442 else
3443 {
3444 di = find_var(lp->ll_name, NULL);
3445 if (di == NULL)
3446 ret = FAIL;
3447 else
3448 {
3449 if (lock)
3450 di->di_flags |= DI_FLAGS_LOCK;
3451 else
3452 di->di_flags &= ~DI_FLAGS_LOCK;
3453 item_lock(&di->di_tv, deep, lock);
3454 }
3455 }
3456 *name_end = cc;
3457 }
3458 else if (lp->ll_range)
3459 {
3460 listitem_T *li = lp->ll_li;
3461
3462 /* (un)lock a range of List items. */
3463 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3464 {
3465 item_lock(&li->li_tv, deep, lock);
3466 li = li->li_next;
3467 ++lp->ll_n1;
3468 }
3469 }
3470 else if (lp->ll_list != NULL)
3471 /* (un)lock a List item. */
3472 item_lock(&lp->ll_li->li_tv, deep, lock);
3473 else
3474 /* un(lock) a Dictionary item. */
3475 item_lock(&lp->ll_di->di_tv, deep, lock);
3476
3477 return ret;
3478}
3479
3480/*
3481 * Lock or unlock an item. "deep" is nr of levels to go.
3482 */
3483 static void
3484item_lock(tv, deep, lock)
3485 typval_T *tv;
3486 int deep;
3487 int lock;
3488{
3489 static int recurse = 0;
3490 list_T *l;
3491 listitem_T *li;
3492 dict_T *d;
3493 hashitem_T *hi;
3494 int todo;
3495
3496 if (recurse >= DICT_MAXNEST)
3497 {
3498 EMSG(_("E743: variable nested too deep for (un)lock"));
3499 return;
3500 }
3501 if (deep == 0)
3502 return;
3503 ++recurse;
3504
3505 /* lock/unlock the item itself */
3506 if (lock)
3507 tv->v_lock |= VAR_LOCKED;
3508 else
3509 tv->v_lock &= ~VAR_LOCKED;
3510
3511 switch (tv->v_type)
3512 {
3513 case VAR_LIST:
3514 if ((l = tv->vval.v_list) != NULL)
3515 {
3516 if (lock)
3517 l->lv_lock |= VAR_LOCKED;
3518 else
3519 l->lv_lock &= ~VAR_LOCKED;
3520 if (deep < 0 || deep > 1)
3521 /* recursive: lock/unlock the items the List contains */
3522 for (li = l->lv_first; li != NULL; li = li->li_next)
3523 item_lock(&li->li_tv, deep - 1, lock);
3524 }
3525 break;
3526 case VAR_DICT:
3527 if ((d = tv->vval.v_dict) != NULL)
3528 {
3529 if (lock)
3530 d->dv_lock |= VAR_LOCKED;
3531 else
3532 d->dv_lock &= ~VAR_LOCKED;
3533 if (deep < 0 || deep > 1)
3534 {
3535 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003536 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003537 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3538 {
3539 if (!HASHITEM_EMPTY(hi))
3540 {
3541 --todo;
3542 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3543 }
3544 }
3545 }
3546 }
3547 }
3548 --recurse;
3549}
3550
Bram Moolenaara40058a2005-07-11 22:42:07 +00003551/*
3552 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3553 * it refers to a List or Dictionary that is locked.
3554 */
3555 static int
3556tv_islocked(tv)
3557 typval_T *tv;
3558{
3559 return (tv->v_lock & VAR_LOCKED)
3560 || (tv->v_type == VAR_LIST
3561 && tv->vval.v_list != NULL
3562 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3563 || (tv->v_type == VAR_DICT
3564 && tv->vval.v_dict != NULL
3565 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3566}
3567
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3569/*
3570 * Delete all "menutrans_" variables.
3571 */
3572 void
3573del_menutrans_vars()
3574{
Bram Moolenaar33570922005-01-25 22:26:29 +00003575 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003576 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
Bram Moolenaar33570922005-01-25 22:26:29 +00003578 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003579 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003580 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003581 {
3582 if (!HASHITEM_EMPTY(hi))
3583 {
3584 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3586 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003587 }
3588 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003589 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590}
3591#endif
3592
3593#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3594
3595/*
3596 * Local string buffer for the next two functions to store a variable name
3597 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3598 * get_user_var_name().
3599 */
3600
3601static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3602
3603static char_u *varnamebuf = NULL;
3604static int varnamebuflen = 0;
3605
3606/*
3607 * Function to concatenate a prefix and a variable name.
3608 */
3609 static char_u *
3610cat_prefix_varname(prefix, name)
3611 int prefix;
3612 char_u *name;
3613{
3614 int len;
3615
3616 len = (int)STRLEN(name) + 3;
3617 if (len > varnamebuflen)
3618 {
3619 vim_free(varnamebuf);
3620 len += 10; /* some additional space */
3621 varnamebuf = alloc(len);
3622 if (varnamebuf == NULL)
3623 {
3624 varnamebuflen = 0;
3625 return NULL;
3626 }
3627 varnamebuflen = len;
3628 }
3629 *varnamebuf = prefix;
3630 varnamebuf[1] = ':';
3631 STRCPY(varnamebuf + 2, name);
3632 return varnamebuf;
3633}
3634
3635/*
3636 * Function given to ExpandGeneric() to obtain the list of user defined
3637 * (global/buffer/window/built-in) variable names.
3638 */
3639/*ARGSUSED*/
3640 char_u *
3641get_user_var_name(xp, idx)
3642 expand_T *xp;
3643 int idx;
3644{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003645 static long_u gdone;
3646 static long_u bdone;
3647 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003648#ifdef FEAT_WINDOWS
3649 static long_u tdone;
3650#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003651 static int vidx;
3652 static hashitem_T *hi;
3653 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003656 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003657 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003658#ifdef FEAT_WINDOWS
3659 tdone = 0;
3660#endif
3661 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003662
3663 /* Global variables */
3664 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003666 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003667 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003668 else
3669 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003670 while (HASHITEM_EMPTY(hi))
3671 ++hi;
3672 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3673 return cat_prefix_varname('g', hi->hi_key);
3674 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003676
3677 /* b: variables */
3678 ht = &curbuf->b_vars.dv_hashtab;
3679 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = ht->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 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 return (char_u *)"b:changedtick";
3693 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003694
3695 /* w: variables */
3696 ht = &curwin->w_vars.dv_hashtab;
3697 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003699 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003700 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003701 else
3702 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003703 while (HASHITEM_EMPTY(hi))
3704 ++hi;
3705 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003707
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003708#ifdef FEAT_WINDOWS
3709 /* t: variables */
3710 ht = &curtab->tp_vars.dv_hashtab;
3711 if (tdone < ht->ht_used)
3712 {
3713 if (tdone++ == 0)
3714 hi = ht->ht_array;
3715 else
3716 ++hi;
3717 while (HASHITEM_EMPTY(hi))
3718 ++hi;
3719 return cat_prefix_varname('t', hi->hi_key);
3720 }
3721#endif
3722
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 /* v: variables */
3724 if (vidx < VV_LEN)
3725 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 vim_free(varnamebuf);
3728 varnamebuf = NULL;
3729 varnamebuflen = 0;
3730 return NULL;
3731}
3732
3733#endif /* FEAT_CMDL_COMPL */
3734
3735/*
3736 * types for expressions.
3737 */
3738typedef enum
3739{
3740 TYPE_UNKNOWN = 0
3741 , TYPE_EQUAL /* == */
3742 , TYPE_NEQUAL /* != */
3743 , TYPE_GREATER /* > */
3744 , TYPE_GEQUAL /* >= */
3745 , TYPE_SMALLER /* < */
3746 , TYPE_SEQUAL /* <= */
3747 , TYPE_MATCH /* =~ */
3748 , TYPE_NOMATCH /* !~ */
3749} exptype_T;
3750
3751/*
3752 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003753 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3755 */
3756
3757/*
3758 * Handle zero level expression.
3759 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003760 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003761 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 * Return OK or FAIL.
3763 */
3764 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003765eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 char_u **nextcmd;
3769 int evaluate;
3770{
3771 int ret;
3772 char_u *p;
3773
3774 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (ret == FAIL || !ends_excmd(*p))
3777 {
3778 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003779 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 /*
3781 * Report the invalid expression unless the expression evaluation has
3782 * been cancelled due to an aborting error, an interrupt, or an
3783 * exception.
3784 */
3785 if (!aborting())
3786 EMSG2(_(e_invexpr2), arg);
3787 ret = FAIL;
3788 }
3789 if (nextcmd != NULL)
3790 *nextcmd = check_nextcmd(p);
3791
3792 return ret;
3793}
3794
3795/*
3796 * Handle top level expression:
3797 * expr1 ? expr0 : expr0
3798 *
3799 * "arg" must point to the first non-white of the expression.
3800 * "arg" is advanced to the next non-white after the recognized expression.
3801 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003802 * Note: "rettv.v_lock" is not set.
3803 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 * Return OK or FAIL.
3805 */
3806 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 int evaluate;
3811{
3812 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003813 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 /*
3816 * Get the first variable.
3817 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003818 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 return FAIL;
3820
3821 if ((*arg)[0] == '?')
3822 {
3823 result = FALSE;
3824 if (evaluate)
3825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003826 int error = FALSE;
3827
3828 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003831 if (error)
3832 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834
3835 /*
3836 * Get the second variable.
3837 */
3838 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003839 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 return FAIL;
3841
3842 /*
3843 * Check for the ":".
3844 */
3845 if ((*arg)[0] != ':')
3846 {
3847 EMSG(_("E109: Missing ':' after '?'"));
3848 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003849 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 return FAIL;
3851 }
3852
3853 /*
3854 * Get the third variable.
3855 */
3856 *arg = skipwhite(*arg + 1);
3857 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3858 {
3859 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003860 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return FAIL;
3862 }
3863 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866
3867 return OK;
3868}
3869
3870/*
3871 * Handle first level expression:
3872 * expr2 || expr2 || expr2 logical OR
3873 *
3874 * "arg" must point to the first non-white of the expression.
3875 * "arg" is advanced to the next non-white after the recognized expression.
3876 *
3877 * Return OK or FAIL.
3878 */
3879 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003880eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 int evaluate;
3884{
Bram Moolenaar33570922005-01-25 22:26:29 +00003885 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 long result;
3887 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003888 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 /*
3891 * Get the first variable.
3892 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003893 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 return FAIL;
3895
3896 /*
3897 * Repeat until there is no following "||".
3898 */
3899 first = TRUE;
3900 result = FALSE;
3901 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3902 {
3903 if (evaluate && first)
3904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003905 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003907 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 if (error)
3909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 first = FALSE;
3911 }
3912
3913 /*
3914 * Get the second variable.
3915 */
3916 *arg = skipwhite(*arg + 2);
3917 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3918 return FAIL;
3919
3920 /*
3921 * Compute the result.
3922 */
3923 if (evaluate && !result)
3924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003927 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003928 if (error)
3929 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 if (evaluate)
3932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 rettv->v_type = VAR_NUMBER;
3934 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936 }
3937
3938 return OK;
3939}
3940
3941/*
3942 * Handle second level expression:
3943 * expr3 && expr3 && expr3 logical AND
3944 *
3945 * "arg" must point to the first non-white of the expression.
3946 * "arg" is advanced to the next non-white after the recognized expression.
3947 *
3948 * Return OK or FAIL.
3949 */
3950 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003951eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 int evaluate;
3955{
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 long result;
3958 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003959 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960
3961 /*
3962 * Get the first variable.
3963 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003964 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return FAIL;
3966
3967 /*
3968 * Repeat until there is no following "&&".
3969 */
3970 first = TRUE;
3971 result = TRUE;
3972 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3973 {
3974 if (evaluate && first)
3975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003976 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003978 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003979 if (error)
3980 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 first = FALSE;
3982 }
3983
3984 /*
3985 * Get the second variable.
3986 */
3987 *arg = skipwhite(*arg + 2);
3988 if (eval4(arg, &var2, evaluate && result) == FAIL)
3989 return FAIL;
3990
3991 /*
3992 * Compute the result.
3993 */
3994 if (evaluate && result)
3995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003999 if (error)
4000 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 }
4002 if (evaluate)
4003 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004 rettv->v_type = VAR_NUMBER;
4005 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 }
4008
4009 return OK;
4010}
4011
4012/*
4013 * Handle third level expression:
4014 * var1 == var2
4015 * var1 =~ var2
4016 * var1 != var2
4017 * var1 !~ var2
4018 * var1 > var2
4019 * var1 >= var2
4020 * var1 < var2
4021 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004022 * var1 is var2
4023 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 *
4025 * "arg" must point to the first non-white of the expression.
4026 * "arg" is advanced to the next non-white after the recognized expression.
4027 *
4028 * Return OK or FAIL.
4029 */
4030 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004031eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 int evaluate;
4035{
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 char_u *p;
4038 int i;
4039 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004040 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 int len = 2;
4042 long n1, n2;
4043 char_u *s1, *s2;
4044 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4045 regmatch_T regmatch;
4046 int ic;
4047 char_u *save_cpo;
4048
4049 /*
4050 * Get the first variable.
4051 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004052 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return FAIL;
4054
4055 p = *arg;
4056 switch (p[0])
4057 {
4058 case '=': if (p[1] == '=')
4059 type = TYPE_EQUAL;
4060 else if (p[1] == '~')
4061 type = TYPE_MATCH;
4062 break;
4063 case '!': if (p[1] == '=')
4064 type = TYPE_NEQUAL;
4065 else if (p[1] == '~')
4066 type = TYPE_NOMATCH;
4067 break;
4068 case '>': if (p[1] != '=')
4069 {
4070 type = TYPE_GREATER;
4071 len = 1;
4072 }
4073 else
4074 type = TYPE_GEQUAL;
4075 break;
4076 case '<': if (p[1] != '=')
4077 {
4078 type = TYPE_SMALLER;
4079 len = 1;
4080 }
4081 else
4082 type = TYPE_SEQUAL;
4083 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004084 case 'i': if (p[1] == 's')
4085 {
4086 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4087 len = 5;
4088 if (!vim_isIDc(p[len]))
4089 {
4090 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4091 type_is = TRUE;
4092 }
4093 }
4094 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096
4097 /*
4098 * If there is a comparitive operator, use it.
4099 */
4100 if (type != TYPE_UNKNOWN)
4101 {
4102 /* extra question mark appended: ignore case */
4103 if (p[len] == '?')
4104 {
4105 ic = TRUE;
4106 ++len;
4107 }
4108 /* extra '#' appended: match case */
4109 else if (p[len] == '#')
4110 {
4111 ic = FALSE;
4112 ++len;
4113 }
4114 /* nothing appened: use 'ignorecase' */
4115 else
4116 ic = p_ic;
4117
4118 /*
4119 * Get the second variable.
4120 */
4121 *arg = skipwhite(p + len);
4122 if (eval5(arg, &var2, evaluate) == FAIL)
4123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 return FAIL;
4126 }
4127
4128 if (evaluate)
4129 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004130 if (type_is && rettv->v_type != var2.v_type)
4131 {
4132 /* For "is" a different type always means FALSE, for "notis"
4133 * it means TRUE. */
4134 n1 = (type == TYPE_NEQUAL);
4135 }
4136 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4137 {
4138 if (type_is)
4139 {
4140 n1 = (rettv->v_type == var2.v_type
4141 && rettv->vval.v_list == var2.vval.v_list);
4142 if (type == TYPE_NEQUAL)
4143 n1 = !n1;
4144 }
4145 else if (rettv->v_type != var2.v_type
4146 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4147 {
4148 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004149 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004150 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004151 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004152 clear_tv(rettv);
4153 clear_tv(&var2);
4154 return FAIL;
4155 }
4156 else
4157 {
4158 /* Compare two Lists for being equal or unequal. */
4159 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4160 if (type == TYPE_NEQUAL)
4161 n1 = !n1;
4162 }
4163 }
4164
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004165 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4166 {
4167 if (type_is)
4168 {
4169 n1 = (rettv->v_type == var2.v_type
4170 && rettv->vval.v_dict == var2.vval.v_dict);
4171 if (type == TYPE_NEQUAL)
4172 n1 = !n1;
4173 }
4174 else if (rettv->v_type != var2.v_type
4175 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4176 {
4177 if (rettv->v_type != var2.v_type)
4178 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4179 else
4180 EMSG(_("E736: Invalid operation for Dictionary"));
4181 clear_tv(rettv);
4182 clear_tv(&var2);
4183 return FAIL;
4184 }
4185 else
4186 {
4187 /* Compare two Dictionaries for being equal or unequal. */
4188 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4189 if (type == TYPE_NEQUAL)
4190 n1 = !n1;
4191 }
4192 }
4193
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004194 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4195 {
4196 if (rettv->v_type != var2.v_type
4197 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4198 {
4199 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004200 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004201 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004202 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004203 clear_tv(rettv);
4204 clear_tv(&var2);
4205 return FAIL;
4206 }
4207 else
4208 {
4209 /* Compare two Funcrefs for being equal or unequal. */
4210 if (rettv->vval.v_string == NULL
4211 || var2.vval.v_string == NULL)
4212 n1 = FALSE;
4213 else
4214 n1 = STRCMP(rettv->vval.v_string,
4215 var2.vval.v_string) == 0;
4216 if (type == TYPE_NEQUAL)
4217 n1 = !n1;
4218 }
4219 }
4220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 /*
4222 * If one of the two variables is a number, compare as a number.
4223 * When using "=~" or "!~", always compare as string.
4224 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004225 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 n1 = get_tv_number(rettv);
4229 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 switch (type)
4231 {
4232 case TYPE_EQUAL: n1 = (n1 == n2); break;
4233 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4234 case TYPE_GREATER: n1 = (n1 > n2); break;
4235 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4236 case TYPE_SMALLER: n1 = (n1 < n2); break;
4237 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4238 case TYPE_UNKNOWN:
4239 case TYPE_MATCH:
4240 case TYPE_NOMATCH: break; /* avoid gcc warning */
4241 }
4242 }
4243 else
4244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 s1 = get_tv_string_buf(rettv, buf1);
4246 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4248 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4249 else
4250 i = 0;
4251 n1 = FALSE;
4252 switch (type)
4253 {
4254 case TYPE_EQUAL: n1 = (i == 0); break;
4255 case TYPE_NEQUAL: n1 = (i != 0); break;
4256 case TYPE_GREATER: n1 = (i > 0); break;
4257 case TYPE_GEQUAL: n1 = (i >= 0); break;
4258 case TYPE_SMALLER: n1 = (i < 0); break;
4259 case TYPE_SEQUAL: n1 = (i <= 0); break;
4260
4261 case TYPE_MATCH:
4262 case TYPE_NOMATCH:
4263 /* avoid 'l' flag in 'cpoptions' */
4264 save_cpo = p_cpo;
4265 p_cpo = (char_u *)"";
4266 regmatch.regprog = vim_regcomp(s2,
4267 RE_MAGIC + RE_STRING);
4268 regmatch.rm_ic = ic;
4269 if (regmatch.regprog != NULL)
4270 {
4271 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4272 vim_free(regmatch.regprog);
4273 if (type == TYPE_NOMATCH)
4274 n1 = !n1;
4275 }
4276 p_cpo = save_cpo;
4277 break;
4278
4279 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4280 }
4281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
4283 clear_tv(&var2);
4284 rettv->v_type = VAR_NUMBER;
4285 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 }
4288
4289 return OK;
4290}
4291
4292/*
4293 * Handle fourth level expression:
4294 * + number addition
4295 * - number subtraction
4296 * . string concatenation
4297 *
4298 * "arg" must point to the first non-white of the expression.
4299 * "arg" is advanced to the next non-white after the recognized expression.
4300 *
4301 * Return OK or FAIL.
4302 */
4303 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 int evaluate;
4308{
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 typval_T var2;
4310 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 int op;
4312 long n1, n2;
4313 char_u *s1, *s2;
4314 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4315 char_u *p;
4316
4317 /*
4318 * Get the first variable.
4319 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 return FAIL;
4322
4323 /*
4324 * Repeat computing, until no '+', '-' or '.' is following.
4325 */
4326 for (;;)
4327 {
4328 op = **arg;
4329 if (op != '+' && op != '-' && op != '.')
4330 break;
4331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (op != '+' || rettv->v_type != VAR_LIST)
4333 {
4334 /* For "list + ...", an illegal use of the first operand as
4335 * a number cannot be determined before evaluating the 2nd
4336 * operand: if this is also a list, all is ok.
4337 * For "something . ...", "something - ..." or "non-list + ...",
4338 * we know that the first operand needs to be a string or number
4339 * without evaluating the 2nd operand. So check before to avoid
4340 * side effects after an error. */
4341 if (evaluate && get_tv_string_chk(rettv) == NULL)
4342 {
4343 clear_tv(rettv);
4344 return FAIL;
4345 }
4346 }
4347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 /*
4349 * Get the second variable.
4350 */
4351 *arg = skipwhite(*arg + 1);
4352 if (eval6(arg, &var2, evaluate) == FAIL)
4353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 return FAIL;
4356 }
4357
4358 if (evaluate)
4359 {
4360 /*
4361 * Compute the result.
4362 */
4363 if (op == '.')
4364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004365 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4366 s2 = get_tv_string_buf_chk(&var2, buf2);
4367 if (s2 == NULL) /* type error ? */
4368 {
4369 clear_tv(rettv);
4370 clear_tv(&var2);
4371 return FAIL;
4372 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004373 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 clear_tv(rettv);
4375 rettv->v_type = VAR_STRING;
4376 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004378 else if (op == '+' && rettv->v_type == VAR_LIST
4379 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004380 {
4381 /* concatenate Lists */
4382 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4383 &var3) == FAIL)
4384 {
4385 clear_tv(rettv);
4386 clear_tv(&var2);
4387 return FAIL;
4388 }
4389 clear_tv(rettv);
4390 *rettv = var3;
4391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 else
4393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004394 int error = FALSE;
4395
4396 n1 = get_tv_number_chk(rettv, &error);
4397 if (error)
4398 {
4399 /* This can only happen for "list + non-list".
4400 * For "non-list + ..." or "something - ...", we returned
4401 * before evaluating the 2nd operand. */
4402 clear_tv(rettv);
4403 return FAIL;
4404 }
4405 n2 = get_tv_number_chk(&var2, &error);
4406 if (error)
4407 {
4408 clear_tv(rettv);
4409 clear_tv(&var2);
4410 return FAIL;
4411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 if (op == '+')
4414 n1 = n1 + n2;
4415 else
4416 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004417 rettv->v_type = VAR_NUMBER;
4418 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
4422 }
4423 return OK;
4424}
4425
4426/*
4427 * Handle fifth level expression:
4428 * * number multiplication
4429 * / number division
4430 * % number modulo
4431 *
4432 * "arg" must point to the first non-white of the expression.
4433 * "arg" is advanced to the next non-white after the recognized expression.
4434 *
4435 * Return OK or FAIL.
4436 */
4437 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 int evaluate;
4442{
Bram Moolenaar33570922005-01-25 22:26:29 +00004443 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 int op;
4445 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004446 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 /*
4449 * Get the first variable.
4450 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004451 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 return FAIL;
4453
4454 /*
4455 * Repeat computing, until no '*', '/' or '%' is following.
4456 */
4457 for (;;)
4458 {
4459 op = **arg;
4460 if (op != '*' && op != '/' && op != '%')
4461 break;
4462
4463 if (evaluate)
4464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004465 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004466 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004467 if (error)
4468 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 else
4471 n1 = 0;
4472
4473 /*
4474 * Get the second variable.
4475 */
4476 *arg = skipwhite(*arg + 1);
4477 if (eval7(arg, &var2, evaluate) == FAIL)
4478 return FAIL;
4479
4480 if (evaluate)
4481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004484 if (error)
4485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 /*
4488 * Compute the result.
4489 */
4490 if (op == '*')
4491 n1 = n1 * n2;
4492 else if (op == '/')
4493 {
4494 if (n2 == 0) /* give an error message? */
4495 n1 = 0x7fffffffL;
4496 else
4497 n1 = n1 / n2;
4498 }
4499 else
4500 {
4501 if (n2 == 0) /* give an error message? */
4502 n1 = 0;
4503 else
4504 n1 = n1 % n2;
4505 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004506 rettv->v_type = VAR_NUMBER;
4507 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 }
4510
4511 return OK;
4512}
4513
4514/*
4515 * Handle sixth level expression:
4516 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004517 * "string" string constant
4518 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 * &option-name option value
4520 * @r register contents
4521 * identifier variable value
4522 * function() function call
4523 * $VAR environment variable
4524 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004525 * [expr, expr] List
4526 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 *
4528 * Also handle:
4529 * ! in front logical NOT
4530 * - in front unary minus
4531 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004532 * trailing [] subscript in String or List
4533 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 *
4535 * "arg" must point to the first non-white of the expression.
4536 * "arg" is advanced to the next non-white after the recognized expression.
4537 *
4538 * Return OK or FAIL.
4539 */
4540 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004541eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 int evaluate;
4545{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 long n;
4547 int len;
4548 char_u *s;
4549 int val;
4550 char_u *start_leader, *end_leader;
4551 int ret = OK;
4552 char_u *alias;
4553
4554 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560 /*
4561 * Skip '!' and '-' characters. They are handled later.
4562 */
4563 start_leader = *arg;
4564 while (**arg == '!' || **arg == '-' || **arg == '+')
4565 *arg = skipwhite(*arg + 1);
4566 end_leader = *arg;
4567
4568 switch (**arg)
4569 {
4570 /*
4571 * Number constant.
4572 */
4573 case '0':
4574 case '1':
4575 case '2':
4576 case '3':
4577 case '4':
4578 case '5':
4579 case '6':
4580 case '7':
4581 case '8':
4582 case '9':
4583 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4584 *arg += len;
4585 if (evaluate)
4586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587 rettv->v_type = VAR_NUMBER;
4588 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 break;
4591
4592 /*
4593 * String constant: "string".
4594 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 break;
4597
4598 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004599 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602 break;
4603
4604 /*
4605 * List: [expr, expr]
4606 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 break;
4609
4610 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004611 * Dictionary: {key: val, key: val}
4612 */
4613 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4614 break;
4615
4616 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004617 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004619 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 break;
4621
4622 /*
4623 * Environment variable: $VAR.
4624 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 break;
4627
4628 /*
4629 * Register contents: @r.
4630 */
4631 case '@': ++*arg;
4632 if (evaluate)
4633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004634 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004635 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
4637 if (**arg != NUL)
4638 ++*arg;
4639 break;
4640
4641 /*
4642 * nested expression: (expression).
4643 */
4644 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004645 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (**arg == ')')
4647 ++*arg;
4648 else if (ret == OK)
4649 {
4650 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 ret = FAIL;
4653 }
4654 break;
4655
Bram Moolenaar8c711452005-01-14 21:53:12 +00004656 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 break;
4658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659
4660 if (ret == NOTDONE)
4661 {
4662 /*
4663 * Must be a variable or function name.
4664 * Can also be a curly-braces kind of name: {expr}.
4665 */
4666 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004667 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 if (alias != NULL)
4669 s = alias;
4670
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004671 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 ret = FAIL;
4673 else
4674 {
4675 if (**arg == '(') /* recursive! */
4676 {
4677 /* If "s" is the name of a variable of type VAR_FUNC
4678 * use its contents. */
4679 s = deref_func_name(s, &len);
4680
4681 /* Invoke the function. */
4682 ret = get_func_tv(s, len, rettv, arg,
4683 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004684 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 /* Stop the expression evaluation when immediately
4686 * aborting on error, or when an interrupt occurred or
4687 * an exception was thrown but not caught. */
4688 if (aborting())
4689 {
4690 if (ret == OK)
4691 clear_tv(rettv);
4692 ret = FAIL;
4693 }
4694 }
4695 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004696 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004697 else
4698 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 }
4700
4701 if (alias != NULL)
4702 vim_free(alias);
4703 }
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 *arg = skipwhite(*arg);
4706
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004707 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4708 * expr(expr). */
4709 if (ret == OK)
4710 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
4712 /*
4713 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4714 */
4715 if (ret == OK && evaluate && end_leader > start_leader)
4716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 int error = FALSE;
4718
4719 val = get_tv_number_chk(rettv, &error);
4720 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 clear_tv(rettv);
4723 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 else
4726 {
4727 while (end_leader > start_leader)
4728 {
4729 --end_leader;
4730 if (*end_leader == '!')
4731 val = !val;
4732 else if (*end_leader == '-')
4733 val = -val;
4734 }
4735 clear_tv(rettv);
4736 rettv->v_type = VAR_NUMBER;
4737 rettv->vval.v_number = val;
4738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
4740
4741 return ret;
4742}
4743
4744/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004745 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4746 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4748 */
4749 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004750eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004752 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004754 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755{
4756 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004757 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004758 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759 long len = -1;
4760 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004762 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004766 if (verbose)
4767 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 return FAIL;
4769 }
4770
Bram Moolenaar8c711452005-01-14 21:53:12 +00004771 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004773 /*
4774 * dict.name
4775 */
4776 key = *arg + 1;
4777 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4778 ;
4779 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 }
4783 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785 /*
4786 * something[idx]
4787 *
4788 * Get the (first) variable from inside the [].
4789 */
4790 *arg = skipwhite(*arg + 1);
4791 if (**arg == ':')
4792 empty1 = TRUE;
4793 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4794 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4796 {
4797 /* not a number or string */
4798 clear_tv(&var1);
4799 return FAIL;
4800 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801
4802 /*
4803 * Get the second variable from inside the [:].
4804 */
4805 if (**arg == ':')
4806 {
4807 range = TRUE;
4808 *arg = skipwhite(*arg + 1);
4809 if (**arg == ']')
4810 empty2 = TRUE;
4811 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 if (!empty1)
4814 clear_tv(&var1);
4815 return FAIL;
4816 }
4817 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4818 {
4819 /* not a number or string */
4820 if (!empty1)
4821 clear_tv(&var1);
4822 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004823 return FAIL;
4824 }
4825 }
4826
4827 /* Check for the ']'. */
4828 if (**arg != ']')
4829 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004830 if (verbose)
4831 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004832 clear_tv(&var1);
4833 if (range)
4834 clear_tv(&var2);
4835 return FAIL;
4836 }
4837 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 }
4839
4840 if (evaluate)
4841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004842 n1 = 0;
4843 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004845 n1 = get_tv_number(&var1);
4846 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847 }
4848 if (range)
4849 {
4850 if (empty2)
4851 n2 = -1;
4852 else
4853 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 n2 = get_tv_number(&var2);
4855 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 }
4857 }
4858
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 {
4861 case VAR_NUMBER:
4862 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 len = (long)STRLEN(s);
4865 if (range)
4866 {
4867 /* The resulting variable is a substring. If the indexes
4868 * are out of range the result is empty. */
4869 if (n1 < 0)
4870 {
4871 n1 = len + n1;
4872 if (n1 < 0)
4873 n1 = 0;
4874 }
4875 if (n2 < 0)
4876 n2 = len + n2;
4877 else if (n2 >= len)
4878 n2 = len;
4879 if (n1 >= len || n2 < 0 || n1 > n2)
4880 s = NULL;
4881 else
4882 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4883 }
4884 else
4885 {
4886 /* The resulting variable is a string of a single
4887 * character. If the index is too big or negative the
4888 * result is empty. */
4889 if (n1 >= len || n1 < 0)
4890 s = NULL;
4891 else
4892 s = vim_strnsave(s + n1, 1);
4893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004894 clear_tv(rettv);
4895 rettv->v_type = VAR_STRING;
4896 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004897 break;
4898
4899 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 if (n1 < 0)
4902 n1 = len + n1;
4903 if (!empty1 && (n1 < 0 || n1 >= len))
4904 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004905 /* For a range we allow invalid values and return an empty
4906 * list. A list index out of range is an error. */
4907 if (!range)
4908 {
4909 if (verbose)
4910 EMSGN(_(e_listidx), n1);
4911 return FAIL;
4912 }
4913 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 }
4915 if (range)
4916 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004917 list_T *l;
4918 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919
4920 if (n2 < 0)
4921 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004922 else if (n2 >= len)
4923 n2 = len - 1;
4924 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004925 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004926 l = list_alloc();
4927 if (l == NULL)
4928 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004930 n1 <= n2; ++n1)
4931 {
4932 if (list_append_tv(l, &item->li_tv) == FAIL)
4933 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004934 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935 return FAIL;
4936 }
4937 item = item->li_next;
4938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 clear_tv(rettv);
4940 rettv->v_type = VAR_LIST;
4941 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004942 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 }
4944 else
4945 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004946 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004947 clear_tv(rettv);
4948 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004949 }
4950 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004951
4952 case VAR_DICT:
4953 if (range)
4954 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004955 if (verbose)
4956 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 if (len == -1)
4958 clear_tv(&var1);
4959 return FAIL;
4960 }
4961 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004962 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963
4964 if (len == -1)
4965 {
4966 key = get_tv_string(&var1);
4967 if (*key == NUL)
4968 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004969 if (verbose)
4970 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 clear_tv(&var1);
4972 return FAIL;
4973 }
4974 }
4975
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004976 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004978 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004979 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 if (len == -1)
4981 clear_tv(&var1);
4982 if (item == NULL)
4983 return FAIL;
4984
4985 copy_tv(&item->di_tv, &var1);
4986 clear_tv(rettv);
4987 *rettv = var1;
4988 }
4989 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004990 }
4991 }
4992
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993 return OK;
4994}
4995
4996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 * Get an option value.
4998 * "arg" points to the '&' or '+' before the option name.
4999 * "arg" is advanced to character after the option name.
5000 * Return OK or FAIL.
5001 */
5002 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005003get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005005 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 int evaluate;
5007{
5008 char_u *option_end;
5009 long numval;
5010 char_u *stringval;
5011 int opt_type;
5012 int c;
5013 int working = (**arg == '+'); /* has("+option") */
5014 int ret = OK;
5015 int opt_flags;
5016
5017 /*
5018 * Isolate the option name and find its value.
5019 */
5020 option_end = find_option_end(arg, &opt_flags);
5021 if (option_end == NULL)
5022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 EMSG2(_("E112: Option name missing: %s"), *arg);
5025 return FAIL;
5026 }
5027
5028 if (!evaluate)
5029 {
5030 *arg = option_end;
5031 return OK;
5032 }
5033
5034 c = *option_end;
5035 *option_end = NUL;
5036 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005037 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
5039 if (opt_type == -3) /* invalid name */
5040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 EMSG2(_("E113: Unknown option: %s"), *arg);
5043 ret = FAIL;
5044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005045 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 if (opt_type == -2) /* hidden string option */
5048 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005049 rettv->v_type = VAR_STRING;
5050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052 else if (opt_type == -1) /* hidden number option */
5053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 rettv->v_type = VAR_NUMBER;
5055 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 else if (opt_type == 1) /* number option */
5058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 rettv->v_type = VAR_NUMBER;
5060 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062 else /* string option */
5063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 rettv->v_type = VAR_STRING;
5065 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067 }
5068 else if (working && (opt_type == -2 || opt_type == -1))
5069 ret = FAIL;
5070
5071 *option_end = c; /* put back for error messages */
5072 *arg = option_end;
5073
5074 return ret;
5075}
5076
5077/*
5078 * Allocate a variable for a string constant.
5079 * Return OK or FAIL.
5080 */
5081 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 int evaluate;
5086{
5087 char_u *p;
5088 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 int extra = 0;
5090
5091 /*
5092 * Find the end of the string, skipping backslashed characters.
5093 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005094 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
5096 if (*p == '\\' && p[1] != NUL)
5097 {
5098 ++p;
5099 /* A "\<x>" form occupies at least 4 characters, and produces up
5100 * to 6 characters: reserve space for 2 extra */
5101 if (*p == '<')
5102 extra += 2;
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105
5106 if (*p != '"')
5107 {
5108 EMSG2(_("E114: Missing quote: %s"), *arg);
5109 return FAIL;
5110 }
5111
5112 /* If only parsing, set *arg and return here */
5113 if (!evaluate)
5114 {
5115 *arg = p + 1;
5116 return OK;
5117 }
5118
5119 /*
5120 * Copy the string into allocated memory, handling backslashed
5121 * characters.
5122 */
5123 name = alloc((unsigned)(p - *arg + extra));
5124 if (name == NULL)
5125 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126 rettv->v_type = VAR_STRING;
5127 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131 if (*p == '\\')
5132 {
5133 switch (*++p)
5134 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 case 'b': *name++ = BS; ++p; break;
5136 case 'e': *name++ = ESC; ++p; break;
5137 case 'f': *name++ = FF; ++p; break;
5138 case 'n': *name++ = NL; ++p; break;
5139 case 'r': *name++ = CAR; ++p; break;
5140 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 case 'X': /* hex: "\x1", "\x12" */
5143 case 'x':
5144 case 'u': /* Unicode: "\u0023" */
5145 case 'U':
5146 if (vim_isxdigit(p[1]))
5147 {
5148 int n, nr;
5149 int c = toupper(*p);
5150
5151 if (c == 'X')
5152 n = 2;
5153 else
5154 n = 4;
5155 nr = 0;
5156 while (--n >= 0 && vim_isxdigit(p[1]))
5157 {
5158 ++p;
5159 nr = (nr << 4) + hex2nr(*p);
5160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162#ifdef FEAT_MBYTE
5163 /* For "\u" store the number according to
5164 * 'encoding'. */
5165 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 else
5168#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 break;
5172
5173 /* octal: "\1", "\12", "\123" */
5174 case '0':
5175 case '1':
5176 case '2':
5177 case '3':
5178 case '4':
5179 case '5':
5180 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 case '7': *name = *p++ - '0';
5182 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 *name = (*name << 3) + *p++ - '0';
5185 if (*p >= '0' && *p <= '7')
5186 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 break;
5190
5191 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (extra != 0)
5194 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 break;
5197 }
5198 /* FALLTHROUGH */
5199
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 break;
5202 }
5203 }
5204 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 *arg = p + 1;
5210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 return OK;
5212}
5213
5214/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 * Return OK or FAIL.
5217 */
5218 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005219get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 int evaluate;
5223{
5224 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005225 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005226 int reduce = 0;
5227
5228 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005230 */
5231 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5232 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005234 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005236 break;
5237 ++reduce;
5238 ++p;
5239 }
5240 }
5241
Bram Moolenaar8c711452005-01-14 21:53:12 +00005242 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005243 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005245 return FAIL;
5246 }
5247
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 if (!evaluate)
5250 {
5251 *arg = p + 1;
5252 return OK;
5253 }
5254
5255 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005257 */
5258 str = alloc((unsigned)((p - *arg) - reduce));
5259 if (str == NULL)
5260 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 rettv->v_type = VAR_STRING;
5262 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005263
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005267 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005269 break;
5270 ++p;
5271 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005273 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005275 *arg = p + 1;
5276
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005277 return OK;
5278}
5279
5280/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 * Allocate a variable for a List and fill it from "*arg".
5282 * Return OK or FAIL.
5283 */
5284 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005285get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005287 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 int evaluate;
5289{
Bram Moolenaar33570922005-01-25 22:26:29 +00005290 list_T *l = NULL;
5291 typval_T tv;
5292 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293
5294 if (evaluate)
5295 {
5296 l = list_alloc();
5297 if (l == NULL)
5298 return FAIL;
5299 }
5300
5301 *arg = skipwhite(*arg + 1);
5302 while (**arg != ']' && **arg != NUL)
5303 {
5304 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5305 goto failret;
5306 if (evaluate)
5307 {
5308 item = listitem_alloc();
5309 if (item != NULL)
5310 {
5311 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005312 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313 list_append(l, item);
5314 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005315 else
5316 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 }
5318
5319 if (**arg == ']')
5320 break;
5321 if (**arg != ',')
5322 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 goto failret;
5325 }
5326 *arg = skipwhite(*arg + 1);
5327 }
5328
5329 if (**arg != ']')
5330 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332failret:
5333 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005334 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 return FAIL;
5336 }
5337
5338 *arg = skipwhite(*arg + 1);
5339 if (evaluate)
5340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005341 rettv->v_type = VAR_LIST;
5342 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 ++l->lv_refcount;
5344 }
5345
5346 return OK;
5347}
5348
5349/*
5350 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005351 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005353 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354list_alloc()
5355{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005356 list_T *l;
5357
5358 l = (list_T *)alloc_clear(sizeof(list_T));
5359 if (l != NULL)
5360 {
5361 /* Prepend the list to the list of lists for garbage collection. */
5362 if (first_list != NULL)
5363 first_list->lv_used_prev = l;
5364 l->lv_used_prev = NULL;
5365 l->lv_used_next = first_list;
5366 first_list = l;
5367 }
5368 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369}
5370
5371/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005372 * Allocate an empty list for a return value.
5373 * Returns OK or FAIL.
5374 */
5375 static int
5376rettv_list_alloc(rettv)
5377 typval_T *rettv;
5378{
5379 list_T *l = list_alloc();
5380
5381 if (l == NULL)
5382 return FAIL;
5383
5384 rettv->vval.v_list = l;
5385 rettv->v_type = VAR_LIST;
5386 ++l->lv_refcount;
5387 return OK;
5388}
5389
5390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 * Unreference a list: decrement the reference count and free it when it
5392 * becomes zero.
5393 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005394 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005398 if (l != NULL && --l->lv_refcount <= 0)
5399 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400}
5401
5402/*
5403 * Free a list, including all items it points to.
5404 * Ignores the reference count.
5405 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005406 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005407list_free(l, recurse)
5408 list_T *l;
5409 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410{
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005413 /* Remove the list from the list of lists for garbage collection. */
5414 if (l->lv_used_prev == NULL)
5415 first_list = l->lv_used_next;
5416 else
5417 l->lv_used_prev->lv_used_next = l->lv_used_next;
5418 if (l->lv_used_next != NULL)
5419 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5420
Bram Moolenaard9fba312005-06-26 22:34:35 +00005421 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005423 /* Remove the item before deleting it. */
5424 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005425 if (recurse || (item->li_tv.v_type != VAR_LIST
5426 && item->li_tv.v_type != VAR_DICT))
5427 clear_tv(&item->li_tv);
5428 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 }
5430 vim_free(l);
5431}
5432
5433/*
5434 * Allocate a list item.
5435 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437listitem_alloc()
5438{
Bram Moolenaar33570922005-01-25 22:26:29 +00005439 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440}
5441
5442/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005443 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 */
5445 static void
5446listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 vim_free(item);
5451}
5452
5453/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005454 * Remove a list item from a List and free it. Also clears the value.
5455 */
5456 static void
5457listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005458 list_T *l;
5459 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005460{
5461 list_remove(l, item, item);
5462 listitem_free(item);
5463}
5464
5465/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 * Get the number of items in a list.
5467 */
5468 static long
5469list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005470 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 if (l == NULL)
5473 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005474 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475}
5476
5477/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005478 * Return TRUE when two lists have exactly the same values.
5479 */
5480 static int
5481list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005482 list_T *l1;
5483 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005484 int ic; /* ignore case for strings */
5485{
Bram Moolenaar33570922005-01-25 22:26:29 +00005486 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005487
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005488 if (l1 == l2)
5489 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005490 if (list_len(l1) != list_len(l2))
5491 return FALSE;
5492
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005493 for (item1 = l1->lv_first, item2 = l2->lv_first;
5494 item1 != NULL && item2 != NULL;
5495 item1 = item1->li_next, item2 = item2->li_next)
5496 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5497 return FALSE;
5498 return item1 == NULL && item2 == NULL;
5499}
5500
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005501#if defined(FEAT_PYTHON) || defined(PROTO)
5502/*
5503 * Return the dictitem that an entry in a hashtable points to.
5504 */
5505 dictitem_T *
5506dict_lookup(hi)
5507 hashitem_T *hi;
5508{
5509 return HI2DI(hi);
5510}
5511#endif
5512
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005513/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005514 * Return TRUE when two dictionaries have exactly the same key/values.
5515 */
5516 static int
5517dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005518 dict_T *d1;
5519 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005520 int ic; /* ignore case for strings */
5521{
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 hashitem_T *hi;
5523 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005524 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005525
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005526 if (d1 == d2)
5527 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005528 if (dict_len(d1) != dict_len(d2))
5529 return FALSE;
5530
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005531 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005533 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005534 if (!HASHITEM_EMPTY(hi))
5535 {
5536 item2 = dict_find(d2, hi->hi_key, -1);
5537 if (item2 == NULL)
5538 return FALSE;
5539 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5540 return FALSE;
5541 --todo;
5542 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005543 }
5544 return TRUE;
5545}
5546
5547/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005548 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005549 * Compares the items just like "==" would compare them, but strings and
5550 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005551 */
5552 static int
5553tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005554 typval_T *tv1;
5555 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005556 int ic; /* ignore case */
5557{
5558 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005559 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005560 static int recursive = 0; /* cach recursive loops */
5561 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005562
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005563 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005564 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005565 /* Catch lists and dicts that have an endless loop by limiting
5566 * recursiveness to 1000. We guess they are equal then. */
5567 if (recursive >= 1000)
5568 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005569
5570 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005571 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005572 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005573 ++recursive;
5574 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5575 --recursive;
5576 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005577
5578 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005579 ++recursive;
5580 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5581 --recursive;
5582 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005583
5584 case VAR_FUNC:
5585 return (tv1->vval.v_string != NULL
5586 && tv2->vval.v_string != NULL
5587 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5588
5589 case VAR_NUMBER:
5590 return tv1->vval.v_number == tv2->vval.v_number;
5591
5592 case VAR_STRING:
5593 s1 = get_tv_string_buf(tv1, buf1);
5594 s2 = get_tv_string_buf(tv2, buf2);
5595 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005596 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005597
5598 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005599 return TRUE;
5600}
5601
5602/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603 * Locate item with index "n" in list "l" and return it.
5604 * A negative index is counted from the end; -1 is the last item.
5605 * Returns NULL when "n" is out of range.
5606 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005607 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 long n;
5611{
Bram Moolenaar33570922005-01-25 22:26:29 +00005612 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 long idx;
5614
5615 if (l == NULL)
5616 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005617
5618 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005620 n = l->lv_len + n;
5621
5622 /* Check for index out of range. */
5623 if (n < 0 || n >= l->lv_len)
5624 return NULL;
5625
5626 /* When there is a cached index may start search from there. */
5627 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005629 if (n < l->lv_idx / 2)
5630 {
5631 /* closest to the start of the list */
5632 item = l->lv_first;
5633 idx = 0;
5634 }
5635 else if (n > (l->lv_idx + l->lv_len) / 2)
5636 {
5637 /* closest to the end of the list */
5638 item = l->lv_last;
5639 idx = l->lv_len - 1;
5640 }
5641 else
5642 {
5643 /* closest to the cached index */
5644 item = l->lv_idx_item;
5645 idx = l->lv_idx;
5646 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647 }
5648 else
5649 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005650 if (n < l->lv_len / 2)
5651 {
5652 /* closest to the start of the list */
5653 item = l->lv_first;
5654 idx = 0;
5655 }
5656 else
5657 {
5658 /* closest to the end of the list */
5659 item = l->lv_last;
5660 idx = l->lv_len - 1;
5661 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005663
5664 while (n > idx)
5665 {
5666 /* search forward */
5667 item = item->li_next;
5668 ++idx;
5669 }
5670 while (n < idx)
5671 {
5672 /* search backward */
5673 item = item->li_prev;
5674 --idx;
5675 }
5676
5677 /* cache the used index */
5678 l->lv_idx = idx;
5679 l->lv_idx_item = item;
5680
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681 return item;
5682}
5683
5684/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005685 * Get list item "l[idx]" as a number.
5686 */
5687 static long
5688list_find_nr(l, idx, errorp)
5689 list_T *l;
5690 long idx;
5691 int *errorp; /* set to TRUE when something wrong */
5692{
5693 listitem_T *li;
5694
5695 li = list_find(l, idx);
5696 if (li == NULL)
5697 {
5698 if (errorp != NULL)
5699 *errorp = TRUE;
5700 return -1L;
5701 }
5702 return get_tv_number_chk(&li->li_tv, errorp);
5703}
5704
5705/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005706 * Locate "item" list "l" and return its index.
5707 * Returns -1 when "item" is not in the list.
5708 */
5709 static long
5710list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005711 list_T *l;
5712 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005713{
5714 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005715 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005716
5717 if (l == NULL)
5718 return -1;
5719 idx = 0;
5720 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5721 ++idx;
5722 if (li == NULL)
5723 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005724 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005725}
5726
5727/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 * Append item "item" to the end of list "l".
5729 */
5730 static void
5731list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 list_T *l;
5733 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005734{
5735 if (l->lv_last == NULL)
5736 {
5737 /* empty list */
5738 l->lv_first = item;
5739 l->lv_last = item;
5740 item->li_prev = NULL;
5741 }
5742 else
5743 {
5744 l->lv_last->li_next = item;
5745 item->li_prev = l->lv_last;
5746 l->lv_last = item;
5747 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005748 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 item->li_next = NULL;
5750}
5751
5752/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005753 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005754 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 */
5756 static int
5757list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005758 list_T *l;
5759 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005761 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005762
Bram Moolenaar05159a02005-02-26 23:04:13 +00005763 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005765 copy_tv(tv, &li->li_tv);
5766 list_append(l, li);
5767 return OK;
5768}
5769
5770/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005771 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005772 * Return FAIL when out of memory.
5773 */
5774 int
5775list_append_dict(list, dict)
5776 list_T *list;
5777 dict_T *dict;
5778{
5779 listitem_T *li = listitem_alloc();
5780
5781 if (li == NULL)
5782 return FAIL;
5783 li->li_tv.v_type = VAR_DICT;
5784 li->li_tv.v_lock = 0;
5785 li->li_tv.vval.v_dict = dict;
5786 list_append(list, li);
5787 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005788 return OK;
5789}
5790
5791/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005792 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005793 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005794 * Returns FAIL when out of memory.
5795 */
5796 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005797list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005798 list_T *l;
5799 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005800 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005801{
5802 listitem_T *li = listitem_alloc();
5803
5804 if (li == NULL)
5805 return FAIL;
5806 list_append(l, li);
5807 li->li_tv.v_type = VAR_STRING;
5808 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005809 if (str == NULL)
5810 li->li_tv.vval.v_string = NULL;
5811 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005812 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005813 return FAIL;
5814 return OK;
5815}
5816
5817/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005818 * Append "n" to list "l".
5819 * Returns FAIL when out of memory.
5820 */
5821 static int
5822list_append_number(l, n)
5823 list_T *l;
5824 varnumber_T n;
5825{
5826 listitem_T *li;
5827
5828 li = listitem_alloc();
5829 if (li == NULL)
5830 return FAIL;
5831 li->li_tv.v_type = VAR_NUMBER;
5832 li->li_tv.v_lock = 0;
5833 li->li_tv.vval.v_number = n;
5834 list_append(l, li);
5835 return OK;
5836}
5837
5838/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005839 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840 * If "item" is NULL append at the end.
5841 * Return FAIL when out of memory.
5842 */
5843 static int
5844list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 list_T *l;
5846 typval_T *tv;
5847 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005848{
Bram Moolenaar33570922005-01-25 22:26:29 +00005849 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005850
5851 if (ni == NULL)
5852 return FAIL;
5853 copy_tv(tv, &ni->li_tv);
5854 if (item == NULL)
5855 /* Append new item at end of list. */
5856 list_append(l, ni);
5857 else
5858 {
5859 /* Insert new item before existing item. */
5860 ni->li_prev = item->li_prev;
5861 ni->li_next = item;
5862 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005863 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005864 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005865 ++l->lv_idx;
5866 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005867 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005868 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005869 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005870 l->lv_idx_item = NULL;
5871 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005872 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005873 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005874 }
5875 return OK;
5876}
5877
5878/*
5879 * Extend "l1" with "l2".
5880 * If "bef" is NULL append at the end, otherwise insert before this item.
5881 * Returns FAIL when out of memory.
5882 */
5883 static int
5884list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 list_T *l1;
5886 list_T *l2;
5887 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005888{
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005890
5891 for (item = l2->lv_first; item != NULL; item = item->li_next)
5892 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5893 return FAIL;
5894 return OK;
5895}
5896
5897/*
5898 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5899 * Return FAIL when out of memory.
5900 */
5901 static int
5902list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 list_T *l1;
5904 list_T *l2;
5905 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005906{
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005908
5909 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005910 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005911 if (l == NULL)
5912 return FAIL;
5913 tv->v_type = VAR_LIST;
5914 tv->vval.v_list = l;
5915
5916 /* append all items from the second list */
5917 return list_extend(l, l2, NULL);
5918}
5919
5920/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005921 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005923 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 * Returns NULL when out of memory.
5925 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005930 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931{
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 list_T *copy;
5933 listitem_T *item;
5934 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935
5936 if (orig == NULL)
5937 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938
5939 copy = list_alloc();
5940 if (copy != NULL)
5941 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942 if (copyID != 0)
5943 {
5944 /* Do this before adding the items, because one of the items may
5945 * refer back to this list. */
5946 orig->lv_copyID = copyID;
5947 orig->lv_copylist = copy;
5948 }
5949 for (item = orig->lv_first; item != NULL && !got_int;
5950 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 {
5952 ni = listitem_alloc();
5953 if (ni == NULL)
5954 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005955 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005956 {
5957 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5958 {
5959 vim_free(ni);
5960 break;
5961 }
5962 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 list_append(copy, ni);
5966 }
5967 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005968 if (item != NULL)
5969 {
5970 list_unref(copy);
5971 copy = NULL;
5972 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 }
5974
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 return copy;
5976}
5977
5978/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005979 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005980 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005983list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 list_T *l;
5985 listitem_T *item;
5986 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005987{
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005990 /* notify watchers */
5991 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005993 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005994 list_fix_watch(l, ip);
5995 if (ip == item2)
5996 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998
5999 if (item2->li_next == NULL)
6000 l->lv_last = item->li_prev;
6001 else
6002 item2->li_next->li_prev = item->li_prev;
6003 if (item->li_prev == NULL)
6004 l->lv_first = item2->li_next;
6005 else
6006 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006007 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008}
6009
6010/*
6011 * Return an allocated string with the string representation of a list.
6012 * May return NULL.
6013 */
6014 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006015list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
6019 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020
6021 if (tv->vval.v_list == NULL)
6022 return NULL;
6023 ga_init2(&ga, (int)sizeof(char), 80);
6024 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006025 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006026 {
6027 vim_free(ga.ga_data);
6028 return NULL;
6029 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 ga_append(&ga, ']');
6031 ga_append(&ga, NUL);
6032 return (char_u *)ga.ga_data;
6033}
6034
6035/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006036 * Join list "l" into a string in "*gap", using separator "sep".
6037 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006038 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006039 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006040 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006041list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006042 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006044 char_u *sep;
6045 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006046 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006047{
6048 int first = TRUE;
6049 char_u *tofree;
6050 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006052 char_u *s;
6053
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006054 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006055 {
6056 if (first)
6057 first = FALSE;
6058 else
6059 ga_concat(gap, sep);
6060
6061 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006062 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006063 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006064 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006065 if (s != NULL)
6066 ga_concat(gap, s);
6067 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006068 if (s == NULL)
6069 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006070 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006071 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006072}
6073
6074/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006075 * Garbage collection for lists and dictionaries.
6076 *
6077 * We use reference counts to be able to free most items right away when they
6078 * are no longer used. But for composite items it's possible that it becomes
6079 * unused while the reference count is > 0: When there is a recursive
6080 * reference. Example:
6081 * :let l = [1, 2, 3]
6082 * :let d = {9: l}
6083 * :let l[1] = d
6084 *
6085 * Since this is quite unusual we handle this with garbage collection: every
6086 * once in a while find out which lists and dicts are not referenced from any
6087 * variable.
6088 *
6089 * Here is a good reference text about garbage collection (refers to Python
6090 * but it applies to all reference-counting mechanisms):
6091 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006092 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006093
6094/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006095 * Do garbage collection for lists and dicts.
6096 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006097 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006098 int
6099garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006100{
6101 dict_T *dd;
6102 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006103 int copyID = ++current_copyID;
6104 buf_T *buf;
6105 win_T *wp;
6106 int i;
6107 funccall_T *fc;
6108 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006109#ifdef FEAT_WINDOWS
6110 tabpage_T *tp;
6111#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006112
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006113 /* Only do this once. */
6114 want_garbage_collect = FALSE;
6115 may_garbage_collect = FALSE;
6116
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006117 /*
6118 * 1. Go through all accessible variables and mark all lists and dicts
6119 * with copyID.
6120 */
6121 /* script-local variables */
6122 for (i = 1; i <= ga_scripts.ga_len; ++i)
6123 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6124
6125 /* buffer-local variables */
6126 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6127 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6128
6129 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006130 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006131 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6132
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006133#ifdef FEAT_WINDOWS
6134 /* tabpage-local variables */
6135 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6136 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6137#endif
6138
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006139 /* global variables */
6140 set_ref_in_ht(&globvarht, copyID);
6141
6142 /* function-local variables */
6143 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6144 {
6145 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6146 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6147 }
6148
6149 /*
6150 * 2. Go through the list of dicts and free items without the copyID.
6151 */
6152 for (dd = first_dict; dd != NULL; )
6153 if (dd->dv_copyID != copyID)
6154 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006155 /* Free the Dictionary and ordinary items it contains, but don't
6156 * recurse into Lists and Dictionaries, they will be in the list
6157 * of dicts or list of lists. */
6158 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006159 did_free = TRUE;
6160
6161 /* restart, next dict may also have been freed */
6162 dd = first_dict;
6163 }
6164 else
6165 dd = dd->dv_used_next;
6166
6167 /*
6168 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006169 * But don't free a list that has a watcher (used in a for loop), these
6170 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006171 */
6172 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006173 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006174 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006175 /* Free the List and ordinary items it contains, but don't recurse
6176 * into Lists and Dictionaries, they will be in the list of dicts
6177 * or list of lists. */
6178 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006179 did_free = TRUE;
6180
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006181 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006182 ll = first_list;
6183 }
6184 else
6185 ll = ll->lv_used_next;
6186
6187 return did_free;
6188}
6189
6190/*
6191 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6192 */
6193 static void
6194set_ref_in_ht(ht, copyID)
6195 hashtab_T *ht;
6196 int copyID;
6197{
6198 int todo;
6199 hashitem_T *hi;
6200
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006201 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006202 for (hi = ht->ht_array; todo > 0; ++hi)
6203 if (!HASHITEM_EMPTY(hi))
6204 {
6205 --todo;
6206 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6207 }
6208}
6209
6210/*
6211 * Mark all lists and dicts referenced through list "l" with "copyID".
6212 */
6213 static void
6214set_ref_in_list(l, copyID)
6215 list_T *l;
6216 int copyID;
6217{
6218 listitem_T *li;
6219
6220 for (li = l->lv_first; li != NULL; li = li->li_next)
6221 set_ref_in_item(&li->li_tv, copyID);
6222}
6223
6224/*
6225 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6226 */
6227 static void
6228set_ref_in_item(tv, copyID)
6229 typval_T *tv;
6230 int copyID;
6231{
6232 dict_T *dd;
6233 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006234
6235 switch (tv->v_type)
6236 {
6237 case VAR_DICT:
6238 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006239 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006240 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006241 /* Didn't see this dict yet. */
6242 dd->dv_copyID = copyID;
6243 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006244 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006245 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006246
6247 case VAR_LIST:
6248 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006249 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006250 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006251 /* Didn't see this list yet. */
6252 ll->lv_copyID = copyID;
6253 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006254 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006255 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006256 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006257 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006258}
6259
6260/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006261 * Allocate an empty header for a dictionary.
6262 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006263 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006264dict_alloc()
6265{
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006267
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006269 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006270 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006271 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006272 if (first_dict != NULL)
6273 first_dict->dv_used_prev = d;
6274 d->dv_used_next = first_dict;
6275 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006276 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006277
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006279 d->dv_lock = 0;
6280 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006281 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006282 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006283 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006284}
6285
6286/*
6287 * Unreference a Dictionary: decrement the reference count and free it when it
6288 * becomes zero.
6289 */
6290 static void
6291dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006293{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006294 if (d != NULL && --d->dv_refcount <= 0)
6295 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006296}
6297
6298/*
6299 * Free a Dictionary, including all items it contains.
6300 * Ignores the reference count.
6301 */
6302 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006303dict_free(d, recurse)
6304 dict_T *d;
6305 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006306{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006307 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006309 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006310
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006311 /* Remove the dict from the list of dicts for garbage collection. */
6312 if (d->dv_used_prev == NULL)
6313 first_dict = d->dv_used_next;
6314 else
6315 d->dv_used_prev->dv_used_next = d->dv_used_next;
6316 if (d->dv_used_next != NULL)
6317 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6318
6319 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006320 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006321 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006324 if (!HASHITEM_EMPTY(hi))
6325 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006326 /* Remove the item before deleting it, just in case there is
6327 * something recursive causing trouble. */
6328 di = HI2DI(hi);
6329 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006330 if (recurse || (di->di_tv.v_type != VAR_LIST
6331 && di->di_tv.v_type != VAR_DICT))
6332 clear_tv(&di->di_tv);
6333 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334 --todo;
6335 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006336 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338 vim_free(d);
6339}
6340
6341/*
6342 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006343 * The "key" is copied to the new item.
6344 * Note that the value of the item "di_tv" still needs to be initialized!
6345 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006347 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006348dictitem_alloc(key)
6349 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006350{
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006352
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006353 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006354 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006355 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006356 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006357 di->di_flags = 0;
6358 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006359 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006360}
6361
6362/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006363 * Make a copy of a Dictionary item.
6364 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006368{
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006370
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006371 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6372 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006373 if (di != NULL)
6374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006376 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006377 copy_tv(&org->di_tv, &di->di_tv);
6378 }
6379 return di;
6380}
6381
6382/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006383 * Remove item "item" from Dictionary "dict" and free it.
6384 */
6385 static void
6386dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 dict_T *dict;
6388 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006389{
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006391
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006393 if (HASHITEM_EMPTY(hi))
6394 EMSG2(_(e_intern2), "dictitem_remove()");
6395 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006397 dictitem_free(item);
6398}
6399
6400/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401 * Free a dict item. Also clears the value.
6402 */
6403 static void
6404dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006407 clear_tv(&item->di_tv);
6408 vim_free(item);
6409}
6410
6411/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006412 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6413 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006414 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006415 * Returns NULL when out of memory.
6416 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006418dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006420 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006421 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006422{
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 dict_T *copy;
6424 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006425 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006427
6428 if (orig == NULL)
6429 return NULL;
6430
6431 copy = dict_alloc();
6432 if (copy != NULL)
6433 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434 if (copyID != 0)
6435 {
6436 orig->dv_copyID = copyID;
6437 orig->dv_copydict = copy;
6438 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006439 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006440 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006441 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006442 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006443 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006444 --todo;
6445
6446 di = dictitem_alloc(hi->hi_key);
6447 if (di == NULL)
6448 break;
6449 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006450 {
6451 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6452 copyID) == FAIL)
6453 {
6454 vim_free(di);
6455 break;
6456 }
6457 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006458 else
6459 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6460 if (dict_add(copy, di) == FAIL)
6461 {
6462 dictitem_free(di);
6463 break;
6464 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006465 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006466 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006467
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 if (todo > 0)
6470 {
6471 dict_unref(copy);
6472 copy = NULL;
6473 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006474 }
6475
6476 return copy;
6477}
6478
6479/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006480 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006481 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006482 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006483 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 dict_T *d;
6486 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487{
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006489}
6490
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006492 * Add a number or string entry to dictionary "d".
6493 * When "str" is NULL use number "nr", otherwise use "str".
6494 * Returns FAIL when out of memory and when key already exists.
6495 */
6496 int
6497dict_add_nr_str(d, key, nr, str)
6498 dict_T *d;
6499 char *key;
6500 long nr;
6501 char_u *str;
6502{
6503 dictitem_T *item;
6504
6505 item = dictitem_alloc((char_u *)key);
6506 if (item == NULL)
6507 return FAIL;
6508 item->di_tv.v_lock = 0;
6509 if (str == NULL)
6510 {
6511 item->di_tv.v_type = VAR_NUMBER;
6512 item->di_tv.vval.v_number = nr;
6513 }
6514 else
6515 {
6516 item->di_tv.v_type = VAR_STRING;
6517 item->di_tv.vval.v_string = vim_strsave(str);
6518 }
6519 if (dict_add(d, item) == FAIL)
6520 {
6521 dictitem_free(item);
6522 return FAIL;
6523 }
6524 return OK;
6525}
6526
6527/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006528 * Get the number of items in a Dictionary.
6529 */
6530 static long
6531dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006532 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006533{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006534 if (d == NULL)
6535 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006536 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537}
6538
6539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006540 * Find item "key[len]" in Dictionary "d".
6541 * If "len" is negative use strlen(key).
6542 * Returns NULL when not found.
6543 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006545dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006546 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006547 char_u *key;
6548 int len;
6549{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006550#define AKEYLEN 200
6551 char_u buf[AKEYLEN];
6552 char_u *akey;
6553 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006555
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006556 if (len < 0)
6557 akey = key;
6558 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006559 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006560 tofree = akey = vim_strnsave(key, len);
6561 if (akey == NULL)
6562 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006563 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006564 else
6565 {
6566 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006567 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006568 akey = buf;
6569 }
6570
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006572 vim_free(tofree);
6573 if (HASHITEM_EMPTY(hi))
6574 return NULL;
6575 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006576}
6577
6578/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006579 * Get a string item from a dictionary.
6580 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006581 * Returns NULL if the entry doesn't exist or out of memory.
6582 */
6583 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006584get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006585 dict_T *d;
6586 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006587 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006588{
6589 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006590 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006591
6592 di = dict_find(d, key, -1);
6593 if (di == NULL)
6594 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006595 s = get_tv_string(&di->di_tv);
6596 if (save && s != NULL)
6597 s = vim_strsave(s);
6598 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006599}
6600
6601/*
6602 * Get a number item from a dictionary.
6603 * Returns 0 if the entry doesn't exist or out of memory.
6604 */
6605 long
6606get_dict_number(d, key)
6607 dict_T *d;
6608 char_u *key;
6609{
6610 dictitem_T *di;
6611
6612 di = dict_find(d, key, -1);
6613 if (di == NULL)
6614 return 0;
6615 return get_tv_number(&di->di_tv);
6616}
6617
6618/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006619 * Return an allocated string with the string representation of a Dictionary.
6620 * May return NULL.
6621 */
6622 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006623dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006625 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006626{
6627 garray_T ga;
6628 int first = TRUE;
6629 char_u *tofree;
6630 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006631 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006632 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006633 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006634 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006635
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006636 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006637 return NULL;
6638 ga_init2(&ga, (int)sizeof(char), 80);
6639 ga_append(&ga, '{');
6640
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006641 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006642 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006644 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006645 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006646 --todo;
6647
6648 if (first)
6649 first = FALSE;
6650 else
6651 ga_concat(&ga, (char_u *)", ");
6652
6653 tofree = string_quote(hi->hi_key, FALSE);
6654 if (tofree != NULL)
6655 {
6656 ga_concat(&ga, tofree);
6657 vim_free(tofree);
6658 }
6659 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006660 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 if (s != NULL)
6662 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006663 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006664 if (s == NULL)
6665 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006666 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006667 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006668 if (todo > 0)
6669 {
6670 vim_free(ga.ga_data);
6671 return NULL;
6672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006673
6674 ga_append(&ga, '}');
6675 ga_append(&ga, NUL);
6676 return (char_u *)ga.ga_data;
6677}
6678
6679/*
6680 * Allocate a variable for a Dictionary and fill it from "*arg".
6681 * Return OK or FAIL. Returns NOTDONE for {expr}.
6682 */
6683 static int
6684get_dict_tv(arg, rettv, evaluate)
6685 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006686 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006687 int evaluate;
6688{
Bram Moolenaar33570922005-01-25 22:26:29 +00006689 dict_T *d = NULL;
6690 typval_T tvkey;
6691 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006692 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006693 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006694 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006695 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006696
6697 /*
6698 * First check if it's not a curly-braces thing: {expr}.
6699 * Must do this without evaluating, otherwise a function may be called
6700 * twice. Unfortunately this means we need to call eval1() twice for the
6701 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006702 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006703 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006704 if (*start != '}')
6705 {
6706 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6707 return FAIL;
6708 if (*start == '}')
6709 return NOTDONE;
6710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711
6712 if (evaluate)
6713 {
6714 d = dict_alloc();
6715 if (d == NULL)
6716 return FAIL;
6717 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006718 tvkey.v_type = VAR_UNKNOWN;
6719 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006720
6721 *arg = skipwhite(*arg + 1);
6722 while (**arg != '}' && **arg != NUL)
6723 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006724 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006725 goto failret;
6726 if (**arg != ':')
6727 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006728 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006729 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006730 goto failret;
6731 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006732 key = get_tv_string_buf_chk(&tvkey, buf);
6733 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006735 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6736 if (key != NULL)
6737 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006738 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739 goto failret;
6740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006741
6742 *arg = skipwhite(*arg + 1);
6743 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006745 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006746 goto failret;
6747 }
6748 if (evaluate)
6749 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006750 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751 if (item != NULL)
6752 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006753 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006754 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006755 clear_tv(&tv);
6756 goto failret;
6757 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006758 item = dictitem_alloc(key);
6759 clear_tv(&tvkey);
6760 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006762 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006763 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006764 if (dict_add(d, item) == FAIL)
6765 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006766 }
6767 }
6768
6769 if (**arg == '}')
6770 break;
6771 if (**arg != ',')
6772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006773 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774 goto failret;
6775 }
6776 *arg = skipwhite(*arg + 1);
6777 }
6778
6779 if (**arg != '}')
6780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006781 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006782failret:
6783 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006784 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785 return FAIL;
6786 }
6787
6788 *arg = skipwhite(*arg + 1);
6789 if (evaluate)
6790 {
6791 rettv->v_type = VAR_DICT;
6792 rettv->vval.v_dict = d;
6793 ++d->dv_refcount;
6794 }
6795
6796 return OK;
6797}
6798
Bram Moolenaar8c711452005-01-14 21:53:12 +00006799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006800 * Return a string with the string representation of a variable.
6801 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006802 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006804 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805 * May return NULL;
6806 */
6807 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006808echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006810 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006811 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006812 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006813{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006814 static int recurse = 0;
6815 char_u *r = NULL;
6816
Bram Moolenaar33570922005-01-25 22:26:29 +00006817 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006818 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006819 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006820 *tofree = NULL;
6821 return NULL;
6822 }
6823 ++recurse;
6824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 switch (tv->v_type)
6826 {
6827 case VAR_FUNC:
6828 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006829 r = tv->vval.v_string;
6830 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006832 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006833 if (tv->vval.v_list == NULL)
6834 {
6835 *tofree = NULL;
6836 r = NULL;
6837 }
6838 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6839 {
6840 *tofree = NULL;
6841 r = (char_u *)"[...]";
6842 }
6843 else
6844 {
6845 tv->vval.v_list->lv_copyID = copyID;
6846 *tofree = list2string(tv, copyID);
6847 r = *tofree;
6848 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006850
Bram Moolenaar8c711452005-01-14 21:53:12 +00006851 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006852 if (tv->vval.v_dict == NULL)
6853 {
6854 *tofree = NULL;
6855 r = NULL;
6856 }
6857 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6858 {
6859 *tofree = NULL;
6860 r = (char_u *)"{...}";
6861 }
6862 else
6863 {
6864 tv->vval.v_dict->dv_copyID = copyID;
6865 *tofree = dict2string(tv, copyID);
6866 r = *tofree;
6867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006868 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006870 case VAR_STRING:
6871 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872 *tofree = NULL;
6873 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006874 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006876 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006877 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006878 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006879 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006880
6881 --recurse;
6882 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006883}
6884
6885/*
6886 * Return a string with the string representation of a variable.
6887 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6888 * "numbuf" is used for a number.
6889 * Puts quotes around strings, so that they can be parsed back by eval().
6890 * May return NULL;
6891 */
6892 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006893tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006894 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006895 char_u **tofree;
6896 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006897 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006898{
6899 switch (tv->v_type)
6900 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 case VAR_FUNC:
6902 *tofree = string_quote(tv->vval.v_string, TRUE);
6903 return *tofree;
6904 case VAR_STRING:
6905 *tofree = string_quote(tv->vval.v_string, FALSE);
6906 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006907 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006908 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006909 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006910 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006911 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006912 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006913 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006914 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006915}
6916
6917/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 * Return string "str" in ' quotes, doubling ' characters.
6919 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006920 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006921 */
6922 static char_u *
6923string_quote(str, function)
6924 char_u *str;
6925 int function;
6926{
Bram Moolenaar33570922005-01-25 22:26:29 +00006927 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006928 char_u *p, *r, *s;
6929
Bram Moolenaar33570922005-01-25 22:26:29 +00006930 len = (function ? 13 : 3);
6931 if (str != NULL)
6932 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006933 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006934 for (p = str; *p != NUL; mb_ptr_adv(p))
6935 if (*p == '\'')
6936 ++len;
6937 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006938 s = r = alloc(len);
6939 if (r != NULL)
6940 {
6941 if (function)
6942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006943 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006944 r += 10;
6945 }
6946 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006947 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006948 if (str != NULL)
6949 for (p = str; *p != NUL; )
6950 {
6951 if (*p == '\'')
6952 *r++ = '\'';
6953 MB_COPY_CHAR(p, r);
6954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006955 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006956 if (function)
6957 *r++ = ')';
6958 *r++ = NUL;
6959 }
6960 return s;
6961}
6962
6963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 * Get the value of an environment variable.
6965 * "arg" is pointing to the '$'. It is advanced to after the name.
6966 * If the environment variable was not set, silently assume it is empty.
6967 * Always return OK.
6968 */
6969 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006970get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 int evaluate;
6974{
6975 char_u *string = NULL;
6976 int len;
6977 int cc;
6978 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006979 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
6981 ++*arg;
6982 name = *arg;
6983 len = get_env_len(arg);
6984 if (evaluate)
6985 {
6986 if (len != 0)
6987 {
6988 cc = name[len];
6989 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006990 /* first try vim_getenv(), fast for normal environment vars */
6991 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006993 {
6994 if (!mustfree)
6995 string = vim_strsave(string);
6996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 else
6998 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006999 if (mustfree)
7000 vim_free(string);
7001
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 /* next try expanding things like $VIM and ${HOME} */
7003 string = expand_env_save(name - 1);
7004 if (string != NULL && *string == '$')
7005 {
7006 vim_free(string);
7007 string = NULL;
7008 }
7009 }
7010 name[len] = cc;
7011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007012 rettv->v_type = VAR_STRING;
7013 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015
7016 return OK;
7017}
7018
7019/*
7020 * Array with names and number of arguments of all internal functions
7021 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7022 */
7023static struct fst
7024{
7025 char *f_name; /* function name */
7026 char f_min_argc; /* minimal number of arguments */
7027 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007028 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007029 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030} functions[] =
7031{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007032 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {"append", 2, 2, f_append},
7034 {"argc", 0, 0, f_argc},
7035 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007036 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007038 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {"bufexists", 1, 1, f_bufexists},
7040 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7041 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7042 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7043 {"buflisted", 1, 1, f_buflisted},
7044 {"bufloaded", 1, 1, f_bufloaded},
7045 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007046 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {"bufwinnr", 1, 1, f_bufwinnr},
7048 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007049 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007050 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007051 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {"char2nr", 1, 1, f_char2nr},
7053 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007054 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007056#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007057 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007058 {"complete_add", 1, 1, f_complete_add},
7059 {"complete_check", 0, 0, f_complete_check},
7060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007062 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007063 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007065 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007066 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {"delete", 1, 1, f_delete},
7068 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007069 {"diff_filler", 1, 1, f_diff_filler},
7070 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007071 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007073 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {"eventhandler", 0, 0, f_eventhandler},
7075 {"executable", 1, 1, f_executable},
7076 {"exists", 1, 1, f_exists},
7077 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007078 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007079 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7081 {"filereadable", 1, 1, f_filereadable},
7082 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007083 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007084 {"finddir", 1, 3, f_finddir},
7085 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"fnamemodify", 2, 2, f_fnamemodify},
7087 {"foldclosed", 1, 1, f_foldclosed},
7088 {"foldclosedend", 1, 1, f_foldclosedend},
7089 {"foldlevel", 1, 1, f_foldlevel},
7090 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007091 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007093 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007095 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007096 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"getbufvar", 2, 2, f_getbufvar},
7098 {"getchar", 0, 1, f_getchar},
7099 {"getcharmod", 0, 0, f_getcharmod},
7100 {"getcmdline", 0, 0, f_getcmdline},
7101 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007102 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007104 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007105 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 {"getfsize", 1, 1, f_getfsize},
7107 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007108 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007109 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007110 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007111 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007112 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007113 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007114 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007116 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {"getwinposx", 0, 0, f_getwinposx},
7118 {"getwinposy", 0, 0, f_getwinposy},
7119 {"getwinvar", 2, 2, f_getwinvar},
7120 {"glob", 1, 1, f_glob},
7121 {"globpath", 2, 2, f_globpath},
7122 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007123 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007124 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007125 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7127 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7128 {"histadd", 2, 2, f_histadd},
7129 {"histdel", 1, 2, f_histdel},
7130 {"histget", 1, 2, f_histget},
7131 {"histnr", 1, 1, f_histnr},
7132 {"hlID", 1, 1, f_hlID},
7133 {"hlexists", 1, 1, f_hlexists},
7134 {"hostname", 0, 0, f_hostname},
7135 {"iconv", 3, 3, f_iconv},
7136 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007137 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007138 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007140 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {"inputrestore", 0, 0, f_inputrestore},
7142 {"inputsave", 0, 0, f_inputsave},
7143 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007144 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007146 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007147 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007148 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007149 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007151 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {"libcall", 3, 3, f_libcall},
7153 {"libcallnr", 3, 3, f_libcallnr},
7154 {"line", 1, 1, f_line},
7155 {"line2byte", 1, 1, f_line2byte},
7156 {"lispindent", 1, 1, f_lispindent},
7157 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007158 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007159 {"maparg", 1, 3, f_maparg},
7160 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007161 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007162 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007163 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007164 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007165 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007166 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007167 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007168 {"max", 1, 1, f_max},
7169 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007170#ifdef vim_mkdir
7171 {"mkdir", 1, 3, f_mkdir},
7172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {"mode", 0, 0, f_mode},
7174 {"nextnonblank", 1, 1, f_nextnonblank},
7175 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007176 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007178 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007179 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007181 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007182 {"reltime", 0, 2, f_reltime},
7183 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {"remote_expr", 2, 3, f_remote_expr},
7185 {"remote_foreground", 1, 1, f_remote_foreground},
7186 {"remote_peek", 1, 2, f_remote_peek},
7187 {"remote_read", 1, 1, f_remote_read},
7188 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007189 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007191 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007193 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007194 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007195 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007196 {"searchpair", 3, 6, f_searchpair},
7197 {"searchpairpos", 3, 6, f_searchpairpos},
7198 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {"server2client", 2, 2, f_server2client},
7200 {"serverlist", 0, 0, f_serverlist},
7201 {"setbufvar", 3, 3, f_setbufvar},
7202 {"setcmdpos", 1, 1, f_setcmdpos},
7203 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007204 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007205 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007206 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007207 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007209 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007211 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007213 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007214 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007215 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007216 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007217 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007218 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219#ifdef HAVE_STRFTIME
7220 {"strftime", 1, 2, f_strftime},
7221#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007223 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {"strlen", 1, 1, f_strlen},
7225 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007226 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 {"strtrans", 1, 1, f_strtrans},
7228 {"submatch", 1, 1, f_submatch},
7229 {"substitute", 4, 4, f_substitute},
7230 {"synID", 3, 3, f_synID},
7231 {"synIDattr", 2, 3, f_synIDattr},
7232 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007233 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007234 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007235 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007236 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007237 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007238 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007240 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {"tolower", 1, 1, f_tolower},
7242 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007243 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {"virtcol", 1, 1, f_virtcol},
7247 {"visualmode", 0, 1, f_visualmode},
7248 {"winbufnr", 1, 1, f_winbufnr},
7249 {"wincol", 0, 0, f_wincol},
7250 {"winheight", 1, 1, f_winheight},
7251 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007252 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007254 {"winrestview", 1, 1, f_winrestview},
7255 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007257 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258};
7259
7260#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7261
7262/*
7263 * Function given to ExpandGeneric() to obtain the list of internal
7264 * or user defined function names.
7265 */
7266 char_u *
7267get_function_name(xp, idx)
7268 expand_T *xp;
7269 int idx;
7270{
7271 static int intidx = -1;
7272 char_u *name;
7273
7274 if (idx == 0)
7275 intidx = -1;
7276 if (intidx < 0)
7277 {
7278 name = get_user_func_name(xp, idx);
7279 if (name != NULL)
7280 return name;
7281 }
7282 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7283 {
7284 STRCPY(IObuff, functions[intidx].f_name);
7285 STRCAT(IObuff, "(");
7286 if (functions[intidx].f_max_argc == 0)
7287 STRCAT(IObuff, ")");
7288 return IObuff;
7289 }
7290
7291 return NULL;
7292}
7293
7294/*
7295 * Function given to ExpandGeneric() to obtain the list of internal or
7296 * user defined variable or function names.
7297 */
7298/*ARGSUSED*/
7299 char_u *
7300get_expr_name(xp, idx)
7301 expand_T *xp;
7302 int idx;
7303{
7304 static int intidx = -1;
7305 char_u *name;
7306
7307 if (idx == 0)
7308 intidx = -1;
7309 if (intidx < 0)
7310 {
7311 name = get_function_name(xp, idx);
7312 if (name != NULL)
7313 return name;
7314 }
7315 return get_user_var_name(xp, ++intidx);
7316}
7317
7318#endif /* FEAT_CMDL_COMPL */
7319
7320/*
7321 * Find internal function in table above.
7322 * Return index, or -1 if not found
7323 */
7324 static int
7325find_internal_func(name)
7326 char_u *name; /* name of the function */
7327{
7328 int first = 0;
7329 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7330 int cmp;
7331 int x;
7332
7333 /*
7334 * Find the function name in the table. Binary search.
7335 */
7336 while (first <= last)
7337 {
7338 x = first + ((unsigned)(last - first) >> 1);
7339 cmp = STRCMP(name, functions[x].f_name);
7340 if (cmp < 0)
7341 last = x - 1;
7342 else if (cmp > 0)
7343 first = x + 1;
7344 else
7345 return x;
7346 }
7347 return -1;
7348}
7349
7350/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007351 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7352 * name it contains, otherwise return "name".
7353 */
7354 static char_u *
7355deref_func_name(name, lenp)
7356 char_u *name;
7357 int *lenp;
7358{
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007360 int cc;
7361
7362 cc = name[*lenp];
7363 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007364 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007365 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007367 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007369 {
7370 *lenp = 0;
7371 return (char_u *)""; /* just in case */
7372 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007373 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007375 }
7376
7377 return name;
7378}
7379
7380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 * Allocate a variable for the result of a function.
7382 * Return OK or FAIL.
7383 */
7384 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7386 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 char_u *name; /* name of the function */
7388 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 char_u **arg; /* argument, pointing to the '(' */
7391 linenr_T firstline; /* first line of range */
7392 linenr_T lastline; /* last line of range */
7393 int *doesrange; /* return: function handled range */
7394 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 char_u *argp;
7398 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007399 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 int argcount = 0; /* number of arguments found */
7401
7402 /*
7403 * Get the arguments.
7404 */
7405 argp = *arg;
7406 while (argcount < MAX_FUNC_ARGS)
7407 {
7408 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7409 if (*argp == ')' || *argp == ',' || *argp == NUL)
7410 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7412 {
7413 ret = FAIL;
7414 break;
7415 }
7416 ++argcount;
7417 if (*argp != ',')
7418 break;
7419 }
7420 if (*argp == ')')
7421 ++argp;
7422 else
7423 ret = FAIL;
7424
7425 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 {
7430 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007431 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
7436 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
7439 *arg = skipwhite(argp);
7440 return ret;
7441}
7442
7443
7444/*
7445 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007446 * Return OK when the function can't be called, FAIL otherwise.
7447 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 */
7449 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007451 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 char_u *name; /* name of the function */
7453 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007456 typval_T *argvars; /* vars for arguments, must have "argcount"
7457 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 linenr_T firstline; /* first line of range */
7459 linenr_T lastline; /* last line of range */
7460 int *doesrange; /* return: function handled range */
7461 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463{
7464 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465#define ERROR_UNKNOWN 0
7466#define ERROR_TOOMANY 1
7467#define ERROR_TOOFEW 2
7468#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469#define ERROR_DICT 4
7470#define ERROR_NONE 5
7471#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 int error = ERROR_NONE;
7473 int i;
7474 int llen;
7475 ufunc_T *fp;
7476 int cc;
7477#define FLEN_FIXED 40
7478 char_u fname_buf[FLEN_FIXED + 1];
7479 char_u *fname;
7480
7481 /*
7482 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7483 * Change <SNR>123_name() to K_SNR 123_name().
7484 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7485 */
7486 cc = name[len];
7487 name[len] = NUL;
7488 llen = eval_fname_script(name);
7489 if (llen > 0)
7490 {
7491 fname_buf[0] = K_SPECIAL;
7492 fname_buf[1] = KS_EXTRA;
7493 fname_buf[2] = (int)KE_SNR;
7494 i = 3;
7495 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7496 {
7497 if (current_SID <= 0)
7498 error = ERROR_SCRIPT;
7499 else
7500 {
7501 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7502 i = (int)STRLEN(fname_buf);
7503 }
7504 }
7505 if (i + STRLEN(name + llen) < FLEN_FIXED)
7506 {
7507 STRCPY(fname_buf + i, name + llen);
7508 fname = fname_buf;
7509 }
7510 else
7511 {
7512 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7513 if (fname == NULL)
7514 error = ERROR_OTHER;
7515 else
7516 {
7517 mch_memmove(fname, fname_buf, (size_t)i);
7518 STRCPY(fname + i, name + llen);
7519 }
7520 }
7521 }
7522 else
7523 fname = name;
7524
7525 *doesrange = FALSE;
7526
7527
7528 /* execute the function if no errors detected and executing */
7529 if (evaluate && error == ERROR_NONE)
7530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007531 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 error = ERROR_UNKNOWN;
7533
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007534 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {
7536 /*
7537 * User defined function.
7538 */
7539 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007540
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007542 /* Trigger FuncUndefined event, may load the function. */
7543 if (fp == NULL
7544 && apply_autocmds(EVENT_FUNCUNDEFINED,
7545 fname, fname, TRUE, NULL)
7546 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007548 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 fp = find_func(fname);
7550 }
7551#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007552 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007553 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007554 {
7555 /* loaded a package, search for the function again */
7556 fp = find_func(fname);
7557 }
7558
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 if (fp != NULL)
7560 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007561 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007563 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007565 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007567 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007568 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 else
7570 {
7571 /*
7572 * Call the user function.
7573 * Save and restore search patterns, script variables and
7574 * redo buffer.
7575 */
7576 save_search_patterns();
7577 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007578 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007579 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007580 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007581 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7582 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7583 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007584 /* Function was unreferenced while being used, free it
7585 * now. */
7586 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 restoreRedobuff();
7588 restore_search_patterns();
7589 error = ERROR_NONE;
7590 }
7591 }
7592 }
7593 else
7594 {
7595 /*
7596 * Find the function name in the table, call its implementation.
7597 */
7598 i = find_internal_func(fname);
7599 if (i >= 0)
7600 {
7601 if (argcount < functions[i].f_min_argc)
7602 error = ERROR_TOOFEW;
7603 else if (argcount > functions[i].f_max_argc)
7604 error = ERROR_TOOMANY;
7605 else
7606 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 error = ERROR_NONE;
7610 }
7611 }
7612 }
7613 /*
7614 * The function call (or "FuncUndefined" autocommand sequence) might
7615 * have been aborted by an error, an interrupt, or an explicitly thrown
7616 * exception that has not been caught so far. This situation can be
7617 * tested for by calling aborting(). For an error in an internal
7618 * function or for the "E132" error in call_user_func(), however, the
7619 * throw point at which the "force_abort" flag (temporarily reset by
7620 * emsg()) is normally updated has not been reached yet. We need to
7621 * update that flag first to make aborting() reliable.
7622 */
7623 update_force_abort();
7624 }
7625 if (error == ERROR_NONE)
7626 ret = OK;
7627
7628 /*
7629 * Report an error unless the argument evaluation or function call has been
7630 * cancelled due to an aborting error, an interrupt, or an exception.
7631 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632 if (!aborting())
7633 {
7634 switch (error)
7635 {
7636 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007637 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 break;
7639 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007640 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 break;
7642 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007643 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007644 name);
7645 break;
7646 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007647 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 name);
7649 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007651 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007652 name);
7653 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654 }
7655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656
7657 name[len] = cc;
7658 if (fname != name && fname != fname_buf)
7659 vim_free(fname);
7660
7661 return ret;
7662}
7663
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664/*
7665 * Give an error message with a function name. Handle <SNR> things.
7666 */
7667 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007668emsg_funcname(ermsg, name)
7669 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007670 char_u *name;
7671{
7672 char_u *p;
7673
7674 if (*name == K_SPECIAL)
7675 p = concat_str((char_u *)"<SNR>", name + 3);
7676 else
7677 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007678 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007679 if (p != name)
7680 vim_free(p);
7681}
7682
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683/*********************************************
7684 * Implementation of the built-in functions
7685 */
7686
7687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007688 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 */
7690 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007691f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 typval_T *argvars;
7693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007700 if ((l = argvars[0].vval.v_list) != NULL
7701 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7702 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007703 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007704 }
7705 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007706 EMSG(_(e_listreq));
7707}
7708
7709/*
7710 * "append(lnum, string/list)" function
7711 */
7712 static void
7713f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 typval_T *argvars;
7715 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007716{
7717 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007718 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 list_T *l = NULL;
7720 listitem_T *li = NULL;
7721 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007722 long added = 0;
7723
Bram Moolenaar0d660222005-01-07 21:51:51 +00007724 lnum = get_tv_lnum(argvars);
7725 if (lnum >= 0
7726 && lnum <= curbuf->b_ml.ml_line_count
7727 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007729 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007731 l = argvars[1].vval.v_list;
7732 if (l == NULL)
7733 return;
7734 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007735 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007736 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007737 for (;;)
7738 {
7739 if (l == NULL)
7740 tv = &argvars[1]; /* append a string */
7741 else if (li == NULL)
7742 break; /* end of list */
7743 else
7744 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007745 line = get_tv_string_chk(tv);
7746 if (line == NULL) /* type error */
7747 {
7748 rettv->vval.v_number = 1; /* Failed */
7749 break;
7750 }
7751 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007752 ++added;
7753 if (l == NULL)
7754 break;
7755 li = li->li_next;
7756 }
7757
7758 appended_lines_mark(lnum, added);
7759 if (curwin->w_cursor.lnum > lnum)
7760 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 else
7763 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764}
7765
7766/*
7767 * "argc()" function
7768 */
7769/* ARGSUSED */
7770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007772 typval_T *argvars;
7773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007775 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776}
7777
7778/*
7779 * "argidx()" function
7780 */
7781/* ARGSUSED */
7782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 typval_T *argvars;
7785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007787 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788}
7789
7790/*
7791 * "argv(nr)" function
7792 */
7793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007795 typval_T *argvars;
7796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 int idx;
7799
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007800 if (argvars[0].v_type != VAR_UNKNOWN)
7801 {
7802 idx = get_tv_number_chk(&argvars[0], NULL);
7803 if (idx >= 0 && idx < ARGCOUNT)
7804 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7805 else
7806 rettv->vval.v_string = NULL;
7807 rettv->v_type = VAR_STRING;
7808 }
7809 else if (rettv_list_alloc(rettv) == OK)
7810 for (idx = 0; idx < ARGCOUNT; ++idx)
7811 list_append_string(rettv->vval.v_list,
7812 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813}
7814
7815/*
7816 * "browse(save, title, initdir, default)" function
7817 */
7818/* ARGSUSED */
7819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007821 typval_T *argvars;
7822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824#ifdef FEAT_BROWSE
7825 int save;
7826 char_u *title;
7827 char_u *initdir;
7828 char_u *defname;
7829 char_u buf[NUMBUFLEN];
7830 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007831 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007833 save = get_tv_number_chk(&argvars[0], &error);
7834 title = get_tv_string_chk(&argvars[1]);
7835 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7836 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007838 if (error || title == NULL || initdir == NULL || defname == NULL)
7839 rettv->vval.v_string = NULL;
7840 else
7841 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007842 do_browse(save ? BROWSE_SAVE : 0,
7843 title, defname, NULL, initdir, NULL, curbuf);
7844#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007845 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007846#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007848}
7849
7850/*
7851 * "browsedir(title, initdir)" function
7852 */
7853/* ARGSUSED */
7854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *argvars;
7857 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007858{
7859#ifdef FEAT_BROWSE
7860 char_u *title;
7861 char_u *initdir;
7862 char_u buf[NUMBUFLEN];
7863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007864 title = get_tv_string_chk(&argvars[0]);
7865 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007867 if (title == NULL || initdir == NULL)
7868 rettv->vval.v_string = NULL;
7869 else
7870 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007871 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876}
7877
Bram Moolenaar33570922005-01-25 22:26:29 +00007878static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007879
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880/*
7881 * Find a buffer by number or exact name.
7882 */
7883 static buf_T *
7884find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007889 if (avar->v_type == VAR_NUMBER)
7890 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007891 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007893 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007894 if (buf == NULL)
7895 {
7896 /* No full path name match, try a match with a URL or a "nofile"
7897 * buffer, these don't use the full path. */
7898 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7899 if (buf->b_fname != NULL
7900 && (path_with_url(buf->b_fname)
7901#ifdef FEAT_QUICKFIX
7902 || bt_nofile(buf)
7903#endif
7904 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007905 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007906 break;
7907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 }
7909 return buf;
7910}
7911
7912/*
7913 * "bufexists(expr)" function
7914 */
7915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007917 typval_T *argvars;
7918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921}
7922
7923/*
7924 * "buflisted(expr)" function
7925 */
7926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 typval_T *argvars;
7929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 buf_T *buf;
7932
7933 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935}
7936
7937/*
7938 * "bufloaded(expr)" function
7939 */
7940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 typval_T *argvars;
7943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 buf_T *buf;
7946
7947 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949}
7950
Bram Moolenaar33570922005-01-25 22:26:29 +00007951static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007952
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953/*
7954 * Get buffer by number or pattern.
7955 */
7956 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 int save_magic;
7962 char_u *save_cpo;
7963 buf_T *buf;
7964
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 if (tv->v_type == VAR_NUMBER)
7966 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007967 if (tv->v_type != VAR_STRING)
7968 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (name == NULL || *name == NUL)
7970 return curbuf;
7971 if (name[0] == '$' && name[1] == NUL)
7972 return lastbuf;
7973
7974 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7975 save_magic = p_magic;
7976 p_magic = TRUE;
7977 save_cpo = p_cpo;
7978 p_cpo = (char_u *)"";
7979
7980 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7981 TRUE, FALSE));
7982
7983 p_magic = save_magic;
7984 p_cpo = save_cpo;
7985
7986 /* If not found, try expanding the name, like done for bufexists(). */
7987 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 return buf;
7991}
7992
7993/*
7994 * "bufname(expr)" function
7995 */
7996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007998 typval_T *argvars;
7999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
8001 buf_T *buf;
8002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008003 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008005 buf = get_buf_tv(&argvars[0]);
8006 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 --emsg_off;
8012}
8013
8014/*
8015 * "bufnr(expr)" function
8016 */
8017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008019 typval_T *argvars;
8020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021{
8022 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008023 int error = FALSE;
8024 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008026 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008029 --emsg_off;
8030
8031 /* If the buffer isn't found and the second argument is not zero create a
8032 * new buffer. */
8033 if (buf == NULL
8034 && argvars[1].v_type != VAR_UNKNOWN
8035 && get_tv_number_chk(&argvars[1], &error) != 0
8036 && !error
8037 && (name = get_tv_string_chk(&argvars[0])) != NULL
8038 && !error)
8039 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8040
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008042 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045}
8046
8047/*
8048 * "bufwinnr(nr)" function
8049 */
8050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008051f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008052 typval_T *argvars;
8053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055#ifdef FEAT_WINDOWS
8056 win_T *wp;
8057 int winnr = 0;
8058#endif
8059 buf_T *buf;
8060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008061 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064#ifdef FEAT_WINDOWS
8065 for (wp = firstwin; wp; wp = wp->w_next)
8066 {
8067 ++winnr;
8068 if (wp->w_buffer == buf)
8069 break;
8070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008071 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008073 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074#endif
8075 --emsg_off;
8076}
8077
8078/*
8079 * "byte2line(byte)" function
8080 */
8081/*ARGSUSED*/
8082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008084 typval_T *argvars;
8085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
8087#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008088 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089#else
8090 long boff = 0;
8091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008092 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008094 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008096 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 (linenr_T)0, &boff);
8098#endif
8099}
8100
8101/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008102 * "byteidx()" function
8103 */
8104/*ARGSUSED*/
8105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008106f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008107 typval_T *argvars;
8108 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008109{
8110#ifdef FEAT_MBYTE
8111 char_u *t;
8112#endif
8113 char_u *str;
8114 long idx;
8115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008116 str = get_tv_string_chk(&argvars[0]);
8117 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008119 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008120 return;
8121
8122#ifdef FEAT_MBYTE
8123 t = str;
8124 for ( ; idx > 0; idx--)
8125 {
8126 if (*t == NUL) /* EOL reached */
8127 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008128 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008129 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008130 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008131#else
8132 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008133 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008134#endif
8135}
8136
8137/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008138 * "call(func, arglist)" function
8139 */
8140 static void
8141f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 typval_T *argvars;
8143 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008144{
8145 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008146 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008147 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008148 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008149 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008150 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008151
8152 rettv->vval.v_number = 0;
8153 if (argvars[1].v_type != VAR_LIST)
8154 {
8155 EMSG(_(e_listreq));
8156 return;
8157 }
8158 if (argvars[1].vval.v_list == NULL)
8159 return;
8160
8161 if (argvars[0].v_type == VAR_FUNC)
8162 func = argvars[0].vval.v_string;
8163 else
8164 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008165 if (*func == NUL)
8166 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008167
Bram Moolenaare9a41262005-01-15 22:18:47 +00008168 if (argvars[2].v_type != VAR_UNKNOWN)
8169 {
8170 if (argvars[2].v_type != VAR_DICT)
8171 {
8172 EMSG(_(e_dictreq));
8173 return;
8174 }
8175 selfdict = argvars[2].vval.v_dict;
8176 }
8177
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008178 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8179 item = item->li_next)
8180 {
8181 if (argc == MAX_FUNC_ARGS)
8182 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008183 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008184 break;
8185 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008186 /* Make a copy of each argument. This is needed to be able to set
8187 * v_lock to VAR_FIXED in the copy without changing the original list.
8188 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008189 copy_tv(&item->li_tv, &argv[argc++]);
8190 }
8191
8192 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008193 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8195 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008196
8197 /* Free the arguments. */
8198 while (argc > 0)
8199 clear_tv(&argv[--argc]);
8200}
8201
8202/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008203 * "changenr()" function
8204 */
8205/*ARGSUSED*/
8206 static void
8207f_changenr(argvars, rettv)
8208 typval_T *argvars;
8209 typval_T *rettv;
8210{
8211 rettv->vval.v_number = curbuf->b_u_seq_cur;
8212}
8213
8214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 * "char2nr(string)" function
8216 */
8217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008218f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 typval_T *argvars;
8220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222#ifdef FEAT_MBYTE
8223 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008224 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 else
8226#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
8230/*
8231 * "cindent(lnum)" function
8232 */
8233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 typval_T *argvars;
8236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238#ifdef FEAT_CINDENT
8239 pos_T pos;
8240 linenr_T lnum;
8241
8242 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008243 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8245 {
8246 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008247 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 curwin->w_cursor = pos;
8249 }
8250 else
8251#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008252 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253}
8254
8255/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008256 * "clearmatches()" function
8257 */
8258/*ARGSUSED*/
8259 static void
8260f_clearmatches(argvars, rettv)
8261 typval_T *argvars;
8262 typval_T *rettv;
8263{
8264#ifdef FEAT_SEARCH_EXTRA
8265 clear_matches(curwin);
8266#endif
8267}
8268
8269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 * "col(string)" function
8271 */
8272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 typval_T *argvars;
8275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276{
8277 colnr_T col = 0;
8278 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008279 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008281 fp = var2fpos(&argvars[0], FALSE, &fnum);
8282 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
8284 if (fp->col == MAXCOL)
8285 {
8286 /* '> can be MAXCOL, get the length of the line then */
8287 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008288 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 else
8290 col = MAXCOL;
8291 }
8292 else
8293 {
8294 col = fp->col + 1;
8295#ifdef FEAT_VIRTUALEDIT
8296 /* col(".") when the cursor is on the NUL at the end of the line
8297 * because of "coladd" can be seen as an extra column. */
8298 if (virtual_active() && fp == &curwin->w_cursor)
8299 {
8300 char_u *p = ml_get_cursor();
8301
8302 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8303 curwin->w_virtcol - curwin->w_cursor.coladd))
8304 {
8305# ifdef FEAT_MBYTE
8306 int l;
8307
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008308 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 col += l;
8310# else
8311 if (*p != NUL && p[1] == NUL)
8312 ++col;
8313# endif
8314 }
8315 }
8316#endif
8317 }
8318 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320}
8321
Bram Moolenaar572cb562005-08-05 21:35:02 +00008322#if defined(FEAT_INS_EXPAND)
8323/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008324 * "complete()" function
8325 */
8326/*ARGSUSED*/
8327 static void
8328f_complete(argvars, rettv)
8329 typval_T *argvars;
8330 typval_T *rettv;
8331{
8332 int startcol;
8333
8334 if ((State & INSERT) == 0)
8335 {
8336 EMSG(_("E785: complete() can only be used in Insert mode"));
8337 return;
8338 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008339
8340 /* Check for undo allowed here, because if something was already inserted
8341 * the line was already saved for undo and this check isn't done. */
8342 if (!undo_allowed())
8343 return;
8344
Bram Moolenaarade00832006-03-10 21:46:58 +00008345 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8346 {
8347 EMSG(_(e_invarg));
8348 return;
8349 }
8350
8351 startcol = get_tv_number_chk(&argvars[0], NULL);
8352 if (startcol <= 0)
8353 return;
8354
8355 set_completion(startcol - 1, argvars[1].vval.v_list);
8356}
8357
8358/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008359 * "complete_add()" function
8360 */
8361/*ARGSUSED*/
8362 static void
8363f_complete_add(argvars, rettv)
8364 typval_T *argvars;
8365 typval_T *rettv;
8366{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008367 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008368}
8369
8370/*
8371 * "complete_check()" function
8372 */
8373/*ARGSUSED*/
8374 static void
8375f_complete_check(argvars, rettv)
8376 typval_T *argvars;
8377 typval_T *rettv;
8378{
8379 int saved = RedrawingDisabled;
8380
8381 RedrawingDisabled = 0;
8382 ins_compl_check_keys(0);
8383 rettv->vval.v_number = compl_interrupted;
8384 RedrawingDisabled = saved;
8385}
8386#endif
8387
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388/*
8389 * "confirm(message, buttons[, default [, type]])" function
8390 */
8391/*ARGSUSED*/
8392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008394 typval_T *argvars;
8395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8398 char_u *message;
8399 char_u *buttons = NULL;
8400 char_u buf[NUMBUFLEN];
8401 char_u buf2[NUMBUFLEN];
8402 int def = 1;
8403 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008404 char_u *typestr;
8405 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 message = get_tv_string_chk(&argvars[0]);
8408 if (message == NULL)
8409 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008410 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008412 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8413 if (buttons == NULL)
8414 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008415 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008417 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008418 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008420 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8421 if (typestr == NULL)
8422 error = TRUE;
8423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008425 switch (TOUPPER_ASC(*typestr))
8426 {
8427 case 'E': type = VIM_ERROR; break;
8428 case 'Q': type = VIM_QUESTION; break;
8429 case 'I': type = VIM_INFO; break;
8430 case 'W': type = VIM_WARNING; break;
8431 case 'G': type = VIM_GENERIC; break;
8432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 }
8434 }
8435 }
8436 }
8437
8438 if (buttons == NULL || *buttons == NUL)
8439 buttons = (char_u *)_("&Ok");
8440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441 if (error)
8442 rettv->vval.v_number = 0;
8443 else
8444 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 def, NULL);
8446#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008447 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448#endif
8449}
8450
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008451/*
8452 * "copy()" function
8453 */
8454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008455f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008456 typval_T *argvars;
8457 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008458{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008459 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008460}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461
8462/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008463 * "count()" function
8464 */
8465 static void
8466f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008467 typval_T *argvars;
8468 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008469{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008470 long n = 0;
8471 int ic = FALSE;
8472
Bram Moolenaare9a41262005-01-15 22:18:47 +00008473 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008474 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 listitem_T *li;
8476 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008477 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008478
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 if ((l = argvars[0].vval.v_list) != NULL)
8480 {
8481 li = l->lv_first;
8482 if (argvars[2].v_type != VAR_UNKNOWN)
8483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008484 int error = FALSE;
8485
8486 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008487 if (argvars[3].v_type != VAR_UNKNOWN)
8488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008489 idx = get_tv_number_chk(&argvars[3], &error);
8490 if (!error)
8491 {
8492 li = list_find(l, idx);
8493 if (li == NULL)
8494 EMSGN(_(e_listidx), idx);
8495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008496 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008497 if (error)
8498 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499 }
8500
8501 for ( ; li != NULL; li = li->li_next)
8502 if (tv_equal(&li->li_tv, &argvars[1], ic))
8503 ++n;
8504 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008505 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 else if (argvars[0].v_type == VAR_DICT)
8507 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008508 int todo;
8509 dict_T *d;
8510 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008511
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008512 if ((d = argvars[0].vval.v_dict) != NULL)
8513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008514 int error = FALSE;
8515
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 if (argvars[2].v_type != VAR_UNKNOWN)
8517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008518 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 if (argvars[3].v_type != VAR_UNKNOWN)
8520 EMSG(_(e_invarg));
8521 }
8522
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008523 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008524 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008525 {
8526 if (!HASHITEM_EMPTY(hi))
8527 {
8528 --todo;
8529 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8530 ++n;
8531 }
8532 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008533 }
8534 }
8535 else
8536 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008537 rettv->vval.v_number = n;
8538}
8539
8540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8542 *
8543 * Checks the existence of a cscope connection.
8544 */
8545/*ARGSUSED*/
8546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008547f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 typval_T *argvars;
8549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551#ifdef FEAT_CSCOPE
8552 int num = 0;
8553 char_u *dbpath = NULL;
8554 char_u *prepend = NULL;
8555 char_u buf[NUMBUFLEN];
8556
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008557 if (argvars[0].v_type != VAR_UNKNOWN
8558 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560 num = (int)get_tv_number(&argvars[0]);
8561 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008562 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 }
8565
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569#endif
8570}
8571
8572/*
8573 * "cursor(lnum, col)" function
8574 *
8575 * Moves the cursor to the specified line and column
8576 */
8577/*ARGSUSED*/
8578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 typval_T *argvars;
8581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
8583 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008584#ifdef FEAT_VIRTUALEDIT
8585 long coladd = 0;
8586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
Bram Moolenaara5525202006-03-02 22:52:09 +00008588 if (argvars[1].v_type == VAR_UNKNOWN)
8589 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008590 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008591
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008592 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008593 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008594 line = pos.lnum;
8595 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008596#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008597 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008598#endif
8599 }
8600 else
8601 {
8602 line = get_tv_lnum(argvars);
8603 col = get_tv_number_chk(&argvars[1], NULL);
8604#ifdef FEAT_VIRTUALEDIT
8605 if (argvars[2].v_type != VAR_UNKNOWN)
8606 coladd = get_tv_number_chk(&argvars[2], NULL);
8607#endif
8608 }
8609 if (line < 0 || col < 0
8610#ifdef FEAT_VIRTUALEDIT
8611 || coladd < 0
8612#endif
8613 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008614 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if (line > 0)
8616 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 if (col > 0)
8618 curwin->w_cursor.col = col - 1;
8619#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008620 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621#endif
8622
8623 /* Make sure the cursor is in a valid position. */
8624 check_cursor();
8625#ifdef FEAT_MBYTE
8626 /* Correct cursor for multi-byte character. */
8627 if (has_mbyte)
8628 mb_adjust_cursor();
8629#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008630
8631 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632}
8633
8634/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008635 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 */
8637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 typval_T *argvars;
8640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008642 int noref = 0;
8643
8644 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008645 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008646 if (noref < 0 || noref > 1)
8647 EMSG(_(e_invarg));
8648 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008649 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650}
8651
8652/*
8653 * "delete()" function
8654 */
8655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008656f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008657 typval_T *argvars;
8658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664}
8665
8666/*
8667 * "did_filetype()" function
8668 */
8669/*ARGSUSED*/
8670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008671f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 typval_T *argvars;
8673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
8675#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008676 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008678 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679#endif
8680}
8681
8682/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008683 * "diff_filler()" function
8684 */
8685/*ARGSUSED*/
8686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008688 typval_T *argvars;
8689 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008690{
8691#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008693#endif
8694}
8695
8696/*
8697 * "diff_hlID()" function
8698 */
8699/*ARGSUSED*/
8700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 typval_T *argvars;
8703 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008704{
8705#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008707 static linenr_T prev_lnum = 0;
8708 static int changedtick = 0;
8709 static int fnum = 0;
8710 static int change_start = 0;
8711 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008712 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008713 int filler_lines;
8714 int col;
8715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008716 if (lnum < 0) /* ignore type error in {lnum} arg */
8717 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008718 if (lnum != prev_lnum
8719 || changedtick != curbuf->b_changedtick
8720 || fnum != curbuf->b_fnum)
8721 {
8722 /* New line, buffer, change: need to get the values. */
8723 filler_lines = diff_check(curwin, lnum);
8724 if (filler_lines < 0)
8725 {
8726 if (filler_lines == -1)
8727 {
8728 change_start = MAXCOL;
8729 change_end = -1;
8730 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8731 hlID = HLF_ADD; /* added line */
8732 else
8733 hlID = HLF_CHD; /* changed line */
8734 }
8735 else
8736 hlID = HLF_ADD; /* added line */
8737 }
8738 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008739 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008740 prev_lnum = lnum;
8741 changedtick = curbuf->b_changedtick;
8742 fnum = curbuf->b_fnum;
8743 }
8744
8745 if (hlID == HLF_CHD || hlID == HLF_TXD)
8746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008747 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008748 if (col >= change_start && col <= change_end)
8749 hlID = HLF_TXD; /* changed text */
8750 else
8751 hlID = HLF_CHD; /* changed line */
8752 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008753 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008754#endif
8755}
8756
8757/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008758 * "empty({expr})" function
8759 */
8760 static void
8761f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 typval_T *argvars;
8763 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008764{
8765 int n;
8766
8767 switch (argvars[0].v_type)
8768 {
8769 case VAR_STRING:
8770 case VAR_FUNC:
8771 n = argvars[0].vval.v_string == NULL
8772 || *argvars[0].vval.v_string == NUL;
8773 break;
8774 case VAR_NUMBER:
8775 n = argvars[0].vval.v_number == 0;
8776 break;
8777 case VAR_LIST:
8778 n = argvars[0].vval.v_list == NULL
8779 || argvars[0].vval.v_list->lv_first == NULL;
8780 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008781 case VAR_DICT:
8782 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008785 default:
8786 EMSG2(_(e_intern2), "f_empty()");
8787 n = 0;
8788 }
8789
8790 rettv->vval.v_number = n;
8791}
8792
8793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 * "escape({string}, {chars})" function
8795 */
8796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008798 typval_T *argvars;
8799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
8801 char_u buf[NUMBUFLEN];
8802
Bram Moolenaar758711c2005-02-02 23:11:38 +00008803 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8804 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806}
8807
8808/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008809 * "eval()" function
8810 */
8811/*ARGSUSED*/
8812 static void
8813f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008814 typval_T *argvars;
8815 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008816{
8817 char_u *s;
8818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008819 s = get_tv_string_chk(&argvars[0]);
8820 if (s != NULL)
8821 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8824 {
8825 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008826 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008827 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008828 else if (*s != NUL)
8829 EMSG(_(e_trailing));
8830}
8831
8832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 * "eventhandler()" function
8834 */
8835/*ARGSUSED*/
8836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008837f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008838 typval_T *argvars;
8839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842}
8843
8844/*
8845 * "executable()" function
8846 */
8847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008849 typval_T *argvars;
8850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853}
8854
8855/*
8856 * "exists()" function
8857 */
8858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008860 typval_T *argvars;
8861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863 char_u *p;
8864 char_u *name;
8865 int n = FALSE;
8866 int len = 0;
8867
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (*p == '$') /* environment variable */
8870 {
8871 /* first try "normal" environment variables (fast) */
8872 if (mch_getenv(p + 1) != NULL)
8873 n = TRUE;
8874 else
8875 {
8876 /* try expanding things like $VIM and ${HOME} */
8877 p = expand_env_save(p);
8878 if (p != NULL && *p != '$')
8879 n = TRUE;
8880 vim_free(p);
8881 }
8882 }
8883 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008885 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008886 if (*skipwhite(p) != NUL)
8887 n = FALSE; /* trailing garbage */
8888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 else if (*p == '*') /* internal or user defined function */
8890 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008891 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
8893 else if (*p == ':')
8894 {
8895 n = cmd_exists(p + 1);
8896 }
8897 else if (*p == '#')
8898 {
8899#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008900 if (p[1] == '#')
8901 n = autocmd_supported(p + 2);
8902 else
8903 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904#endif
8905 }
8906 else /* internal variable */
8907 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008908 char_u *tofree;
8909 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008911 /* get_name_len() takes care of expanding curly braces */
8912 name = p;
8913 len = get_name_len(&p, &tofree, TRUE, FALSE);
8914 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008916 if (tofree != NULL)
8917 name = tofree;
8918 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8919 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008921 /* handle d.key, l[idx], f(expr) */
8922 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8923 if (n)
8924 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 }
8926 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008927 if (*p != NUL)
8928 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008930 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 }
8932
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934}
8935
8936/*
8937 * "expand()" function
8938 */
8939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008940f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 typval_T *argvars;
8942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
8944 char_u *s;
8945 int len;
8946 char_u *errormsg;
8947 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8948 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008949 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951 rettv->v_type = VAR_STRING;
8952 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 if (*s == '%' || *s == '#' || *s == '<')
8954 {
8955 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008956 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 --emsg_off;
8958 }
8959 else
8960 {
8961 /* When the optional second argument is non-zero, don't remove matches
8962 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008963 if (argvars[1].v_type != VAR_UNKNOWN
8964 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966 if (!error)
8967 {
8968 ExpandInit(&xpc);
8969 xpc.xp_context = EXPAND_FILES;
8970 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008971 }
8972 else
8973 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 }
8975}
8976
8977/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008978 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008979 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008980 */
8981 static void
8982f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008983 typval_T *argvars;
8984 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008985{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008986 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008987 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008988 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 list_T *l1, *l2;
8990 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008991 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008993
Bram Moolenaare9a41262005-01-15 22:18:47 +00008994 l1 = argvars[0].vval.v_list;
8995 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008996 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8997 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008998 {
8999 if (argvars[2].v_type != VAR_UNKNOWN)
9000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 before = get_tv_number_chk(&argvars[2], &error);
9002 if (error)
9003 return; /* type error; errmsg already given */
9004
Bram Moolenaar758711c2005-02-02 23:11:38 +00009005 if (before == l1->lv_len)
9006 item = NULL;
9007 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009009 item = list_find(l1, before);
9010 if (item == NULL)
9011 {
9012 EMSGN(_(e_listidx), before);
9013 return;
9014 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 }
9016 }
9017 else
9018 item = NULL;
9019 list_extend(l1, l2, item);
9020
Bram Moolenaare9a41262005-01-15 22:18:47 +00009021 copy_tv(&argvars[0], rettv);
9022 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009023 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009024 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9025 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 dict_T *d1, *d2;
9027 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009028 char_u *action;
9029 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009030 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009031 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032
9033 d1 = argvars[0].vval.v_dict;
9034 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009035 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9036 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 {
9038 /* Check the third argument. */
9039 if (argvars[2].v_type != VAR_UNKNOWN)
9040 {
9041 static char *(av[]) = {"keep", "force", "error"};
9042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 action = get_tv_string_chk(&argvars[2]);
9044 if (action == NULL)
9045 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009046 for (i = 0; i < 3; ++i)
9047 if (STRCMP(action, av[i]) == 0)
9048 break;
9049 if (i == 3)
9050 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009051 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009052 return;
9053 }
9054 }
9055 else
9056 action = (char_u *)"force";
9057
9058 /* Go over all entries in the second dict and add them to the
9059 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009060 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009061 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009062 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009063 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009064 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009065 --todo;
9066 di1 = dict_find(d1, hi2->hi_key, -1);
9067 if (di1 == NULL)
9068 {
9069 di1 = dictitem_copy(HI2DI(hi2));
9070 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9071 dictitem_free(di1);
9072 }
9073 else if (*action == 'e')
9074 {
9075 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9076 break;
9077 }
9078 else if (*action == 'f')
9079 {
9080 clear_tv(&di1->di_tv);
9081 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9082 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009083 }
9084 }
9085
Bram Moolenaare9a41262005-01-15 22:18:47 +00009086 copy_tv(&argvars[0], rettv);
9087 }
9088 }
9089 else
9090 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009091}
9092
9093/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009094 * "feedkeys()" function
9095 */
9096/*ARGSUSED*/
9097 static void
9098f_feedkeys(argvars, rettv)
9099 typval_T *argvars;
9100 typval_T *rettv;
9101{
9102 int remap = TRUE;
9103 char_u *keys, *flags;
9104 char_u nbuf[NUMBUFLEN];
9105 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009106 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009107
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009108 /* This is not allowed in the sandbox. If the commands would still be
9109 * executed in the sandbox it would be OK, but it probably happens later,
9110 * when "sandbox" is no longer set. */
9111 if (check_secure())
9112 return;
9113
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009114 rettv->vval.v_number = 0;
9115 keys = get_tv_string(&argvars[0]);
9116 if (*keys != NUL)
9117 {
9118 if (argvars[1].v_type != VAR_UNKNOWN)
9119 {
9120 flags = get_tv_string_buf(&argvars[1], nbuf);
9121 for ( ; *flags != NUL; ++flags)
9122 {
9123 switch (*flags)
9124 {
9125 case 'n': remap = FALSE; break;
9126 case 'm': remap = TRUE; break;
9127 case 't': typed = TRUE; break;
9128 }
9129 }
9130 }
9131
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009132 /* Need to escape K_SPECIAL and CSI before putting the string in the
9133 * typeahead buffer. */
9134 keys_esc = vim_strsave_escape_csi(keys);
9135 if (keys_esc != NULL)
9136 {
9137 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009138 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009139 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009140 if (vgetc_busy)
9141 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009142 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009143 }
9144}
9145
9146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 * "filereadable()" function
9148 */
9149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009151 typval_T *argvars;
9152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153{
9154 FILE *fd;
9155 char_u *p;
9156 int n;
9157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9160 {
9161 n = TRUE;
9162 fclose(fd);
9163 }
9164 else
9165 n = FALSE;
9166
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168}
9169
9170/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009171 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 * rights to write into.
9173 */
9174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009179 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180}
9181
Bram Moolenaar33570922005-01-25 22:26:29 +00009182static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009183
9184 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009185findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009186 typval_T *argvars;
9187 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009188 int dir;
9189{
9190#ifdef FEAT_SEARCHPATH
9191 char_u *fname;
9192 char_u *fresult = NULL;
9193 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9194 char_u *p;
9195 char_u pathbuf[NUMBUFLEN];
9196 int count = 1;
9197 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009198 int error = FALSE;
9199#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009200
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009201 rettv->vval.v_string = NULL;
9202 rettv->v_type = VAR_STRING;
9203
9204#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009206
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009207 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009209 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9210 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009211 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009212 else
9213 {
9214 if (*p != NUL)
9215 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009217 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009218 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009219 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009220 }
9221
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009222 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9223 error = TRUE;
9224
9225 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009227 do
9228 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009229 if (rettv->v_type == VAR_STRING)
9230 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 fresult = find_file_in_path_option(first ? fname : NULL,
9232 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009233 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009234 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009235 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009236
9237 if (fresult != NULL && rettv->v_type == VAR_LIST)
9238 list_append_string(rettv->vval.v_list, fresult, -1);
9239
9240 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009242
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009243 if (rettv->v_type == VAR_STRING)
9244 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009245#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009246}
9247
Bram Moolenaar33570922005-01-25 22:26:29 +00009248static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9249static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009250
9251/*
9252 * Implementation of map() and filter().
9253 */
9254 static void
9255filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009256 typval_T *argvars;
9257 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009258 int map;
9259{
9260 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009261 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009262 listitem_T *li, *nli;
9263 list_T *l = NULL;
9264 dictitem_T *di;
9265 hashtab_T *ht;
9266 hashitem_T *hi;
9267 dict_T *d = NULL;
9268 typval_T save_val;
9269 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009270 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009271 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009272 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009273 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009274
9275 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009276 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009277 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009278 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009279 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 return;
9281 }
9282 else if (argvars[0].v_type == VAR_DICT)
9283 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009284 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009285 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009286 return;
9287 }
9288 else
9289 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009290 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009291 return;
9292 }
9293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009294 expr = get_tv_string_buf_chk(&argvars[1], buf);
9295 /* On type errors, the preceding call has already displayed an error
9296 * message. Avoid a misleading error message for an empty string that
9297 * was not passed as argument. */
9298 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 prepare_vimvar(VV_VAL, &save_val);
9301 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009302
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009303 /* We reset "did_emsg" to be able to detect whether an error
9304 * occurred during evaluation of the expression. */
9305 save_did_emsg = did_emsg;
9306 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310 prepare_vimvar(VV_KEY, &save_key);
9311 vimvars[VV_KEY].vv_type = VAR_STRING;
9312
9313 ht = &d->dv_hashtab;
9314 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009315 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009316 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009318 if (!HASHITEM_EMPTY(hi))
9319 {
9320 --todo;
9321 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009322 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 break;
9324 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009325 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009326 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 break;
9328 if (!map && rem)
9329 dictitem_remove(d, di);
9330 clear_tv(&vimvars[VV_KEY].vv_tv);
9331 }
9332 }
9333 hash_unlock(ht);
9334
9335 restore_vimvar(VV_KEY, &save_key);
9336 }
9337 else
9338 {
9339 for (li = l->lv_first; li != NULL; li = nli)
9340 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009341 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009342 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009343 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009344 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009345 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009346 break;
9347 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009348 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009349 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009350 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009352 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009353
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009354 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009355 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009356
9357 copy_tv(&argvars[0], rettv);
9358}
9359
9360 static int
9361filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009362 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009363 char_u *expr;
9364 int map;
9365 int *remp;
9366{
Bram Moolenaar33570922005-01-25 22:26:29 +00009367 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009368 char_u *s;
9369
Bram Moolenaar33570922005-01-25 22:26:29 +00009370 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009371 s = expr;
9372 if (eval1(&s, &rettv, TRUE) == FAIL)
9373 return FAIL;
9374 if (*s != NUL) /* check for trailing chars after expr */
9375 {
9376 EMSG2(_(e_invexpr2), s);
9377 return FAIL;
9378 }
9379 if (map)
9380 {
9381 /* map(): replace the list item value */
9382 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009383 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009384 *tv = rettv;
9385 }
9386 else
9387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 int error = FALSE;
9389
Bram Moolenaare9a41262005-01-15 22:18:47 +00009390 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009391 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009393 /* On type error, nothing has been removed; return FAIL to stop the
9394 * loop. The error message was given by get_tv_number_chk(). */
9395 if (error)
9396 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009397 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009398 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009399 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009400}
9401
9402/*
9403 * "filter()" function
9404 */
9405 static void
9406f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009407 typval_T *argvars;
9408 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009409{
9410 filter_map(argvars, rettv, FALSE);
9411}
9412
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009413/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009414 * "finddir({fname}[, {path}[, {count}]])" function
9415 */
9416 static void
9417f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009418 typval_T *argvars;
9419 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009420{
9421 findfilendir(argvars, rettv, TRUE);
9422}
9423
9424/*
9425 * "findfile({fname}[, {path}[, {count}]])" function
9426 */
9427 static void
9428f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009429 typval_T *argvars;
9430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009431{
9432 findfilendir(argvars, rettv, FALSE);
9433}
9434
9435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 * "fnamemodify({fname}, {mods})" function
9437 */
9438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009440 typval_T *argvars;
9441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
9443 char_u *fname;
9444 char_u *mods;
9445 int usedlen = 0;
9446 int len;
9447 char_u *fbuf = NULL;
9448 char_u buf[NUMBUFLEN];
9449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009450 fname = get_tv_string_chk(&argvars[0]);
9451 mods = get_tv_string_buf_chk(&argvars[1], buf);
9452 if (fname == NULL || mods == NULL)
9453 fname = NULL;
9454 else
9455 {
9456 len = (int)STRLEN(fname);
9457 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 vim_free(fbuf);
9466}
9467
Bram Moolenaar33570922005-01-25 22:26:29 +00009468static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
9470/*
9471 * "foldclosed()" function
9472 */
9473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009475 typval_T *argvars;
9476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 int end;
9478{
9479#ifdef FEAT_FOLDING
9480 linenr_T lnum;
9481 linenr_T first, last;
9482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9485 {
9486 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9487 {
9488 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 return;
9493 }
9494 }
9495#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497}
9498
9499/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009500 * "foldclosed()" function
9501 */
9502 static void
9503f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009504 typval_T *argvars;
9505 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009506{
9507 foldclosed_both(argvars, rettv, FALSE);
9508}
9509
9510/*
9511 * "foldclosedend()" function
9512 */
9513 static void
9514f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009515 typval_T *argvars;
9516 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009517{
9518 foldclosed_both(argvars, rettv, TRUE);
9519}
9520
9521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 * "foldlevel()" function
9523 */
9524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529#ifdef FEAT_FOLDING
9530 linenr_T lnum;
9531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 else
9536#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538}
9539
9540/*
9541 * "foldtext()" function
9542 */
9543/*ARGSUSED*/
9544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009546 typval_T *argvars;
9547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549#ifdef FEAT_FOLDING
9550 linenr_T lnum;
9551 char_u *s;
9552 char_u *r;
9553 int len;
9554 char *txt;
9555#endif
9556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->v_type = VAR_STRING;
9558 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009560 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9561 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9562 <= curbuf->b_ml.ml_line_count
9563 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 {
9565 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009566 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9567 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 {
9569 if (!linewhite(lnum))
9570 break;
9571 ++lnum;
9572 }
9573
9574 /* Find interesting text in this line. */
9575 s = skipwhite(ml_get(lnum));
9576 /* skip C comment-start */
9577 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009580 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009581 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009582 {
9583 s = skipwhite(ml_get(lnum + 1));
9584 if (*s == '*')
9585 s = skipwhite(s + 1);
9586 }
9587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 txt = _("+-%s%3ld lines: ");
9589 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009590 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 + 20 /* for %3ld */
9592 + STRLEN(s))); /* concatenated */
9593 if (r != NULL)
9594 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009595 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9596 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9597 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 len = (int)STRLEN(r);
9599 STRCAT(r, s);
9600 /* remove 'foldmarker' and 'commentstring' */
9601 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 }
9604 }
9605#endif
9606}
9607
9608/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009609 * "foldtextresult(lnum)" function
9610 */
9611/*ARGSUSED*/
9612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009614 typval_T *argvars;
9615 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009616{
9617#ifdef FEAT_FOLDING
9618 linenr_T lnum;
9619 char_u *text;
9620 char_u buf[51];
9621 foldinfo_T foldinfo;
9622 int fold_count;
9623#endif
9624
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->v_type = VAR_STRING;
9626 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009627#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 /* treat illegal types and illegal string values for {lnum} the same */
9630 if (lnum < 0)
9631 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009632 fold_count = foldedCount(curwin, lnum, &foldinfo);
9633 if (fold_count > 0)
9634 {
9635 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9636 &foldinfo, buf);
9637 if (text == buf)
9638 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009640 }
9641#endif
9642}
9643
9644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 * "foreground()" function
9646 */
9647/*ARGSUSED*/
9648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 typval_T *argvars;
9651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654#ifdef FEAT_GUI
9655 if (gui.in_use)
9656 gui_mch_set_foreground();
9657#else
9658# ifdef WIN32
9659 win32_set_foreground();
9660# endif
9661#endif
9662}
9663
9664/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009665 * "function()" function
9666 */
9667/*ARGSUSED*/
9668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 typval_T *argvars;
9671 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009672{
9673 char_u *s;
9674
Bram Moolenaara7043832005-01-21 11:56:39 +00009675 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009677 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009678 EMSG2(_(e_invarg2), s);
9679 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009680 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009681 else
9682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 rettv->vval.v_string = vim_strsave(s);
9684 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009685 }
9686}
9687
9688/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009689 * "garbagecollect()" function
9690 */
9691/*ARGSUSED*/
9692 static void
9693f_garbagecollect(argvars, rettv)
9694 typval_T *argvars;
9695 typval_T *rettv;
9696{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009697 /* This is postponed until we are back at the toplevel, because we may be
9698 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9699 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009700}
9701
9702/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009703 * "get()" function
9704 */
9705 static void
9706f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009707 typval_T *argvars;
9708 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009709{
Bram Moolenaar33570922005-01-25 22:26:29 +00009710 listitem_T *li;
9711 list_T *l;
9712 dictitem_T *di;
9713 dict_T *d;
9714 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009715
Bram Moolenaare9a41262005-01-15 22:18:47 +00009716 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009717 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009718 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009720 int error = FALSE;
9721
9722 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9723 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009724 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009725 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009726 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009727 else if (argvars[0].v_type == VAR_DICT)
9728 {
9729 if ((d = argvars[0].vval.v_dict) != NULL)
9730 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009731 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009732 if (di != NULL)
9733 tv = &di->di_tv;
9734 }
9735 }
9736 else
9737 EMSG2(_(e_listdictarg), "get()");
9738
9739 if (tv == NULL)
9740 {
9741 if (argvars[2].v_type == VAR_UNKNOWN)
9742 rettv->vval.v_number = 0;
9743 else
9744 copy_tv(&argvars[2], rettv);
9745 }
9746 else
9747 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009748}
9749
Bram Moolenaar342337a2005-07-21 21:11:17 +00009750static 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 +00009751
9752/*
9753 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009754 * Return a range (from start to end) of lines in rettv from the specified
9755 * buffer.
9756 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009757 */
9758 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009759get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009760 buf_T *buf;
9761 linenr_T start;
9762 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009763 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009764 typval_T *rettv;
9765{
9766 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009767
Bram Moolenaar342337a2005-07-21 21:11:17 +00009768 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009769 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009770 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009771 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009772 }
9773 else
9774 rettv->vval.v_number = 0;
9775
9776 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9777 return;
9778
9779 if (!retlist)
9780 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009781 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9782 p = ml_get_buf(buf, start, FALSE);
9783 else
9784 p = (char_u *)"";
9785
9786 rettv->v_type = VAR_STRING;
9787 rettv->vval.v_string = vim_strsave(p);
9788 }
9789 else
9790 {
9791 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009792 return;
9793
9794 if (start < 1)
9795 start = 1;
9796 if (end > buf->b_ml.ml_line_count)
9797 end = buf->b_ml.ml_line_count;
9798 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009799 if (list_append_string(rettv->vval.v_list,
9800 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009801 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009802 }
9803}
9804
9805/*
9806 * "getbufline()" function
9807 */
9808 static void
9809f_getbufline(argvars, rettv)
9810 typval_T *argvars;
9811 typval_T *rettv;
9812{
9813 linenr_T lnum;
9814 linenr_T end;
9815 buf_T *buf;
9816
9817 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9818 ++emsg_off;
9819 buf = get_buf_tv(&argvars[0]);
9820 --emsg_off;
9821
Bram Moolenaar661b1822005-07-28 22:36:45 +00009822 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009823 if (argvars[2].v_type == VAR_UNKNOWN)
9824 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009825 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009826 end = get_tv_lnum_buf(&argvars[2], buf);
9827
Bram Moolenaar342337a2005-07-21 21:11:17 +00009828 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009829}
9830
Bram Moolenaar0d660222005-01-07 21:51:51 +00009831/*
9832 * "getbufvar()" function
9833 */
9834 static void
9835f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009836 typval_T *argvars;
9837 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009838{
9839 buf_T *buf;
9840 buf_T *save_curbuf;
9841 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009842 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9845 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009846 ++emsg_off;
9847 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009848
9849 rettv->v_type = VAR_STRING;
9850 rettv->vval.v_string = NULL;
9851
9852 if (buf != NULL && varname != NULL)
9853 {
9854 if (*varname == '&') /* buffer-local-option */
9855 {
9856 /* set curbuf to be our buf, temporarily */
9857 save_curbuf = curbuf;
9858 curbuf = buf;
9859
9860 get_option_tv(&varname, rettv, TRUE);
9861
9862 /* restore previous notion of curbuf */
9863 curbuf = save_curbuf;
9864 }
9865 else
9866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009867 if (*varname == NUL)
9868 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9869 * scope prefix before the NUL byte is required by
9870 * find_var_in_ht(). */
9871 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009872 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009873 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009874 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009875 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009876 }
9877 }
9878
9879 --emsg_off;
9880}
9881
9882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 * "getchar()" function
9884 */
9885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009887 typval_T *argvars;
9888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
9890 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009891 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009893 /* Position the cursor. Needed after a message that ends in a space. */
9894 windgoto(msg_row, msg_col);
9895
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 ++no_mapping;
9897 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009898 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 /* getchar(): blocking wait. */
9900 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 /* getchar(1): only check if char avail */
9903 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 else if (error || vpeekc() == NUL)
9905 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 n = 0;
9907 else
9908 /* getchar(0) and char avail: return char */
9909 n = safe_vgetc();
9910 --no_mapping;
9911 --allow_keys;
9912
Bram Moolenaar219b8702006-11-01 14:32:36 +00009913 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9914 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9915 vimvars[VV_MOUSE_COL].vv_nr = 0;
9916
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 if (IS_SPECIAL(n) || mod_mask != 0)
9919 {
9920 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9921 int i = 0;
9922
9923 /* Turn a special key into three bytes, plus modifier. */
9924 if (mod_mask != 0)
9925 {
9926 temp[i++] = K_SPECIAL;
9927 temp[i++] = KS_MODIFIER;
9928 temp[i++] = mod_mask;
9929 }
9930 if (IS_SPECIAL(n))
9931 {
9932 temp[i++] = K_SPECIAL;
9933 temp[i++] = K_SECOND(n);
9934 temp[i++] = K_THIRD(n);
9935 }
9936#ifdef FEAT_MBYTE
9937 else if (has_mbyte)
9938 i += (*mb_char2bytes)(n, temp + i);
9939#endif
9940 else
9941 temp[i++] = n;
9942 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009943 rettv->v_type = VAR_STRING;
9944 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009945
9946#ifdef FEAT_MOUSE
9947 if (n == K_LEFTMOUSE
9948 || n == K_LEFTMOUSE_NM
9949 || n == K_LEFTDRAG
9950 || n == K_LEFTRELEASE
9951 || n == K_LEFTRELEASE_NM
9952 || n == K_MIDDLEMOUSE
9953 || n == K_MIDDLEDRAG
9954 || n == K_MIDDLERELEASE
9955 || n == K_RIGHTMOUSE
9956 || n == K_RIGHTDRAG
9957 || n == K_RIGHTRELEASE
9958 || n == K_X1MOUSE
9959 || n == K_X1DRAG
9960 || n == K_X1RELEASE
9961 || n == K_X2MOUSE
9962 || n == K_X2DRAG
9963 || n == K_X2RELEASE
9964 || n == K_MOUSEDOWN
9965 || n == K_MOUSEUP)
9966 {
9967 int row = mouse_row;
9968 int col = mouse_col;
9969 win_T *win;
9970 linenr_T lnum;
9971# ifdef FEAT_WINDOWS
9972 win_T *wp;
9973# endif
9974 int n = 1;
9975
9976 if (row >= 0 && col >= 0)
9977 {
9978 /* Find the window at the mouse coordinates and compute the
9979 * text position. */
9980 win = mouse_find_win(&row, &col);
9981 (void)mouse_comp_pos(win, &row, &col, &lnum);
9982# ifdef FEAT_WINDOWS
9983 for (wp = firstwin; wp != win; wp = wp->w_next)
9984 ++n;
9985# endif
9986 vimvars[VV_MOUSE_WIN].vv_nr = n;
9987 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
9988 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
9989 }
9990 }
9991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 }
9993}
9994
9995/*
9996 * "getcharmod()" function
9997 */
9998/*ARGSUSED*/
9999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 typval_T *argvars;
10002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005}
10006
10007/*
10008 * "getcmdline()" function
10009 */
10010/*ARGSUSED*/
10011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010013 typval_T *argvars;
10014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 rettv->v_type = VAR_STRING;
10017 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018}
10019
10020/*
10021 * "getcmdpos()" function
10022 */
10023/*ARGSUSED*/
10024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010026 typval_T *argvars;
10027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030}
10031
10032/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010033 * "getcmdtype()" function
10034 */
10035/*ARGSUSED*/
10036 static void
10037f_getcmdtype(argvars, rettv)
10038 typval_T *argvars;
10039 typval_T *rettv;
10040{
10041 rettv->v_type = VAR_STRING;
10042 rettv->vval.v_string = alloc(2);
10043 if (rettv->vval.v_string != NULL)
10044 {
10045 rettv->vval.v_string[0] = get_cmdline_type();
10046 rettv->vval.v_string[1] = NUL;
10047 }
10048}
10049
10050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 * "getcwd()" function
10052 */
10053/*ARGSUSED*/
10054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010056 typval_T *argvars;
10057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
10059 char_u cwd[MAXPATHL];
10060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 else
10065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010068 if (rettv->vval.v_string != NULL)
10069 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070#endif
10071 }
10072}
10073
10074/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010075 * "getfontname()" function
10076 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010077/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010082{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->v_type = VAR_STRING;
10084 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010085#ifdef FEAT_GUI
10086 if (gui.in_use)
10087 {
10088 GuiFont font;
10089 char_u *name = NULL;
10090
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010091 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010092 {
10093 /* Get the "Normal" font. Either the name saved by
10094 * hl_set_font_name() or from the font ID. */
10095 font = gui.norm_font;
10096 name = hl_get_font_name();
10097 }
10098 else
10099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010101 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10102 return;
10103 font = gui_mch_get_font(name, FALSE);
10104 if (font == NOFONT)
10105 return; /* Invalid font name, return empty string. */
10106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010108 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010109 gui_mch_free_font(font);
10110 }
10111#endif
10112}
10113
10114/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010115 * "getfperm({fname})" function
10116 */
10117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010118f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010119 typval_T *argvars;
10120 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010121{
10122 char_u *fname;
10123 struct stat st;
10124 char_u *perm = NULL;
10125 char_u flags[] = "rwx";
10126 int i;
10127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010128 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010131 if (mch_stat((char *)fname, &st) >= 0)
10132 {
10133 perm = vim_strsave((char_u *)"---------");
10134 if (perm != NULL)
10135 {
10136 for (i = 0; i < 9; i++)
10137 {
10138 if (st.st_mode & (1 << (8 - i)))
10139 perm[i] = flags[i % 3];
10140 }
10141 }
10142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010144}
10145
10146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 * "getfsize({fname})" function
10148 */
10149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 typval_T *argvars;
10152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153{
10154 char_u *fname;
10155 struct stat st;
10156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160
10161 if (mch_stat((char *)fname, &st) >= 0)
10162 {
10163 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010167 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010168
10169 /* non-perfect check for overflow */
10170 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10171 rettv->vval.v_number = -2;
10172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 }
10174 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010175 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176}
10177
10178/*
10179 * "getftime({fname})" function
10180 */
10181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010183 typval_T *argvars;
10184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185{
10186 char_u *fname;
10187 struct stat st;
10188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190
10191 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010192 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010194 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195}
10196
10197/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010198 * "getftype({fname})" function
10199 */
10200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010201f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010202 typval_T *argvars;
10203 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010204{
10205 char_u *fname;
10206 struct stat st;
10207 char_u *type = NULL;
10208 char *t;
10209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010210 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010212 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010213 if (mch_lstat((char *)fname, &st) >= 0)
10214 {
10215#ifdef S_ISREG
10216 if (S_ISREG(st.st_mode))
10217 t = "file";
10218 else if (S_ISDIR(st.st_mode))
10219 t = "dir";
10220# ifdef S_ISLNK
10221 else if (S_ISLNK(st.st_mode))
10222 t = "link";
10223# endif
10224# ifdef S_ISBLK
10225 else if (S_ISBLK(st.st_mode))
10226 t = "bdev";
10227# endif
10228# ifdef S_ISCHR
10229 else if (S_ISCHR(st.st_mode))
10230 t = "cdev";
10231# endif
10232# ifdef S_ISFIFO
10233 else if (S_ISFIFO(st.st_mode))
10234 t = "fifo";
10235# endif
10236# ifdef S_ISSOCK
10237 else if (S_ISSOCK(st.st_mode))
10238 t = "fifo";
10239# endif
10240 else
10241 t = "other";
10242#else
10243# ifdef S_IFMT
10244 switch (st.st_mode & S_IFMT)
10245 {
10246 case S_IFREG: t = "file"; break;
10247 case S_IFDIR: t = "dir"; break;
10248# ifdef S_IFLNK
10249 case S_IFLNK: t = "link"; break;
10250# endif
10251# ifdef S_IFBLK
10252 case S_IFBLK: t = "bdev"; break;
10253# endif
10254# ifdef S_IFCHR
10255 case S_IFCHR: t = "cdev"; break;
10256# endif
10257# ifdef S_IFIFO
10258 case S_IFIFO: t = "fifo"; break;
10259# endif
10260# ifdef S_IFSOCK
10261 case S_IFSOCK: t = "socket"; break;
10262# endif
10263 default: t = "other";
10264 }
10265# else
10266 if (mch_isdir(fname))
10267 t = "dir";
10268 else
10269 t = "file";
10270# endif
10271#endif
10272 type = vim_strsave((char_u *)t);
10273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010274 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010275}
10276
10277/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010278 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010279 */
10280 static void
10281f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010282 typval_T *argvars;
10283 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010284{
10285 linenr_T lnum;
10286 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010287 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010288
10289 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010290 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010291 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010292 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010293 retlist = FALSE;
10294 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010295 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010296 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010297 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010298 retlist = TRUE;
10299 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010300
Bram Moolenaar342337a2005-07-21 21:11:17 +000010301 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010302}
10303
10304/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010305 * "getmatches()" function
10306 */
10307/*ARGSUSED*/
10308 static void
10309f_getmatches(argvars, rettv)
10310 typval_T *argvars;
10311 typval_T *rettv;
10312{
10313#ifdef FEAT_SEARCH_EXTRA
10314 dict_T *dict;
10315 matchitem_T *cur = curwin->w_match_head;
10316
10317 rettv->vval.v_number = 0;
10318
10319 if (rettv_list_alloc(rettv) == OK)
10320 {
10321 while (cur != NULL)
10322 {
10323 dict = dict_alloc();
10324 if (dict == NULL)
10325 return;
10326 ++dict->dv_refcount;
10327 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10328 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10329 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10330 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10331 list_append_dict(rettv->vval.v_list, dict);
10332 cur = cur->next;
10333 }
10334 }
10335#endif
10336}
10337
10338/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010339 * "getpos(string)" function
10340 */
10341 static void
10342f_getpos(argvars, rettv)
10343 typval_T *argvars;
10344 typval_T *rettv;
10345{
10346 pos_T *fp;
10347 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010348 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010349
10350 if (rettv_list_alloc(rettv) == OK)
10351 {
10352 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010353 fp = var2fpos(&argvars[0], TRUE, &fnum);
10354 if (fnum != -1)
10355 list_append_number(l, (varnumber_T)fnum);
10356 else
10357 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010358 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10359 : (varnumber_T)0);
10360 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10361 : (varnumber_T)0);
10362 list_append_number(l,
10363#ifdef FEAT_VIRTUALEDIT
10364 (fp != NULL) ? (varnumber_T)fp->coladd :
10365#endif
10366 (varnumber_T)0);
10367 }
10368 else
10369 rettv->vval.v_number = FALSE;
10370}
10371
10372/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010373 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010374 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010375/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010376 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010377f_getqflist(argvars, rettv)
10378 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010379 typval_T *rettv;
10380{
10381#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010382 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010383#endif
10384
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010385 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010386#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010387 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010388 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010389 wp = NULL;
10390 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10391 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010392 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010393 if (wp == NULL)
10394 return;
10395 }
10396
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010397 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010398 }
10399#endif
10400}
10401
10402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 * "getreg()" function
10404 */
10405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010407 typval_T *argvars;
10408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
10410 char_u *strregname;
10411 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010412 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010413 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010415 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 strregname = get_tv_string_chk(&argvars[0]);
10418 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010419 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010423 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 regname = (strregname == NULL ? '"' : *strregname);
10425 if (regname == 0)
10426 regname = '"';
10427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010429 rettv->vval.v_string = error ? NULL :
10430 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431}
10432
10433/*
10434 * "getregtype()" function
10435 */
10436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010437f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010438 typval_T *argvars;
10439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
10441 char_u *strregname;
10442 int regname;
10443 char_u buf[NUMBUFLEN + 2];
10444 long reglen = 0;
10445
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010446 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 {
10448 strregname = get_tv_string_chk(&argvars[0]);
10449 if (strregname == NULL) /* type error; errmsg already given */
10450 {
10451 rettv->v_type = VAR_STRING;
10452 rettv->vval.v_string = NULL;
10453 return;
10454 }
10455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 else
10457 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010458 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459
10460 regname = (strregname == NULL ? '"' : *strregname);
10461 if (regname == 0)
10462 regname = '"';
10463
10464 buf[0] = NUL;
10465 buf[1] = NUL;
10466 switch (get_reg_type(regname, &reglen))
10467 {
10468 case MLINE: buf[0] = 'V'; break;
10469 case MCHAR: buf[0] = 'v'; break;
10470#ifdef FEAT_VISUAL
10471 case MBLOCK:
10472 buf[0] = Ctrl_V;
10473 sprintf((char *)buf + 1, "%ld", reglen + 1);
10474 break;
10475#endif
10476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 rettv->v_type = VAR_STRING;
10478 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479}
10480
10481/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010482 * "gettabwinvar()" function
10483 */
10484 static void
10485f_gettabwinvar(argvars, rettv)
10486 typval_T *argvars;
10487 typval_T *rettv;
10488{
10489 getwinvar(argvars, rettv, 1);
10490}
10491
10492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 * "getwinposx()" function
10494 */
10495/*ARGSUSED*/
10496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010498 typval_T *argvars;
10499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502#ifdef FEAT_GUI
10503 if (gui.in_use)
10504 {
10505 int x, y;
10506
10507 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 }
10510#endif
10511}
10512
10513/*
10514 * "getwinposy()" function
10515 */
10516/*ARGSUSED*/
10517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010518f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010519 typval_T *argvars;
10520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523#ifdef FEAT_GUI
10524 if (gui.in_use)
10525 {
10526 int x, y;
10527
10528 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 }
10531#endif
10532}
10533
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010534/*
10535 * Find window specifed by "vp" in tabpage "tp".
10536 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010537 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010538find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010539 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010540 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010541{
10542#ifdef FEAT_WINDOWS
10543 win_T *wp;
10544#endif
10545 int nr;
10546
10547 nr = get_tv_number_chk(vp, NULL);
10548
10549#ifdef FEAT_WINDOWS
10550 if (nr < 0)
10551 return NULL;
10552 if (nr == 0)
10553 return curwin;
10554
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010555 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10556 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010557 if (--nr <= 0)
10558 break;
10559 return wp;
10560#else
10561 if (nr == 0 || nr == 1)
10562 return curwin;
10563 return NULL;
10564#endif
10565}
10566
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567/*
10568 * "getwinvar()" function
10569 */
10570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010571f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010572 typval_T *argvars;
10573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010575 getwinvar(argvars, rettv, 0);
10576}
10577
10578/*
10579 * getwinvar() and gettabwinvar()
10580 */
10581 static void
10582getwinvar(argvars, rettv, off)
10583 typval_T *argvars;
10584 typval_T *rettv;
10585 int off; /* 1 for gettabwinvar() */
10586{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 win_T *win, *oldcurwin;
10588 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010589 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010590 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010592#ifdef FEAT_WINDOWS
10593 if (off == 1)
10594 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10595 else
10596 tp = curtab;
10597#endif
10598 win = find_win_by_nr(&argvars[off], tp);
10599 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010602 rettv->v_type = VAR_STRING;
10603 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604
10605 if (win != NULL && varname != NULL)
10606 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010607 /* Set curwin to be our win, temporarily. Also set curbuf, so
10608 * that we can get buffer-local options. */
10609 oldcurwin = curwin;
10610 curwin = win;
10611 curbuf = win->w_buffer;
10612
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 else
10616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 if (*varname == NUL)
10618 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10619 * scope prefix before the NUL byte is required by
10620 * find_var_in_ht(). */
10621 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010623 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010627
10628 /* restore previous notion of curwin */
10629 curwin = oldcurwin;
10630 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 }
10632
10633 --emsg_off;
10634}
10635
10636/*
10637 * "glob()" function
10638 */
10639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 typval_T *argvars;
10642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643{
10644 expand_T xpc;
10645
10646 ExpandInit(&xpc);
10647 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648 rettv->v_type = VAR_STRING;
10649 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651}
10652
10653/*
10654 * "globpath()" function
10655 */
10656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010657f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010658 typval_T *argvars;
10659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660{
10661 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010662 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 if (file == NULL)
10666 rettv->vval.v_string = NULL;
10667 else
10668 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669}
10670
10671/*
10672 * "has()" function
10673 */
10674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T *argvars;
10677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
10679 int i;
10680 char_u *name;
10681 int n = FALSE;
10682 static char *(has_list[]) =
10683 {
10684#ifdef AMIGA
10685 "amiga",
10686# ifdef FEAT_ARP
10687 "arp",
10688# endif
10689#endif
10690#ifdef __BEOS__
10691 "beos",
10692#endif
10693#ifdef MSDOS
10694# ifdef DJGPP
10695 "dos32",
10696# else
10697 "dos16",
10698# endif
10699#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010700#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 "mac",
10702#endif
10703#if defined(MACOS_X_UNIX)
10704 "macunix",
10705#endif
10706#ifdef OS2
10707 "os2",
10708#endif
10709#ifdef __QNX__
10710 "qnx",
10711#endif
10712#ifdef RISCOS
10713 "riscos",
10714#endif
10715#ifdef UNIX
10716 "unix",
10717#endif
10718#ifdef VMS
10719 "vms",
10720#endif
10721#ifdef WIN16
10722 "win16",
10723#endif
10724#ifdef WIN32
10725 "win32",
10726#endif
10727#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10728 "win32unix",
10729#endif
10730#ifdef WIN64
10731 "win64",
10732#endif
10733#ifdef EBCDIC
10734 "ebcdic",
10735#endif
10736#ifndef CASE_INSENSITIVE_FILENAME
10737 "fname_case",
10738#endif
10739#ifdef FEAT_ARABIC
10740 "arabic",
10741#endif
10742#ifdef FEAT_AUTOCMD
10743 "autocmd",
10744#endif
10745#ifdef FEAT_BEVAL
10746 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010747# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10748 "balloon_multiline",
10749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750#endif
10751#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10752 "builtin_terms",
10753# ifdef ALL_BUILTIN_TCAPS
10754 "all_builtin_terms",
10755# endif
10756#endif
10757#ifdef FEAT_BYTEOFF
10758 "byte_offset",
10759#endif
10760#ifdef FEAT_CINDENT
10761 "cindent",
10762#endif
10763#ifdef FEAT_CLIENTSERVER
10764 "clientserver",
10765#endif
10766#ifdef FEAT_CLIPBOARD
10767 "clipboard",
10768#endif
10769#ifdef FEAT_CMDL_COMPL
10770 "cmdline_compl",
10771#endif
10772#ifdef FEAT_CMDHIST
10773 "cmdline_hist",
10774#endif
10775#ifdef FEAT_COMMENTS
10776 "comments",
10777#endif
10778#ifdef FEAT_CRYPT
10779 "cryptv",
10780#endif
10781#ifdef FEAT_CSCOPE
10782 "cscope",
10783#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010784#ifdef CURSOR_SHAPE
10785 "cursorshape",
10786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787#ifdef DEBUG
10788 "debug",
10789#endif
10790#ifdef FEAT_CON_DIALOG
10791 "dialog_con",
10792#endif
10793#ifdef FEAT_GUI_DIALOG
10794 "dialog_gui",
10795#endif
10796#ifdef FEAT_DIFF
10797 "diff",
10798#endif
10799#ifdef FEAT_DIGRAPHS
10800 "digraphs",
10801#endif
10802#ifdef FEAT_DND
10803 "dnd",
10804#endif
10805#ifdef FEAT_EMACS_TAGS
10806 "emacs_tags",
10807#endif
10808 "eval", /* always present, of course! */
10809#ifdef FEAT_EX_EXTRA
10810 "ex_extra",
10811#endif
10812#ifdef FEAT_SEARCH_EXTRA
10813 "extra_search",
10814#endif
10815#ifdef FEAT_FKMAP
10816 "farsi",
10817#endif
10818#ifdef FEAT_SEARCHPATH
10819 "file_in_path",
10820#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010821#if defined(UNIX) && !defined(USE_SYSTEM)
10822 "filterpipe",
10823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824#ifdef FEAT_FIND_ID
10825 "find_in_path",
10826#endif
10827#ifdef FEAT_FOLDING
10828 "folding",
10829#endif
10830#ifdef FEAT_FOOTER
10831 "footer",
10832#endif
10833#if !defined(USE_SYSTEM) && defined(UNIX)
10834 "fork",
10835#endif
10836#ifdef FEAT_GETTEXT
10837 "gettext",
10838#endif
10839#ifdef FEAT_GUI
10840 "gui",
10841#endif
10842#ifdef FEAT_GUI_ATHENA
10843# ifdef FEAT_GUI_NEXTAW
10844 "gui_neXtaw",
10845# else
10846 "gui_athena",
10847# endif
10848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849#ifdef FEAT_GUI_GTK
10850 "gui_gtk",
10851# ifdef HAVE_GTK2
10852 "gui_gtk2",
10853# endif
10854#endif
10855#ifdef FEAT_GUI_MAC
10856 "gui_mac",
10857#endif
10858#ifdef FEAT_GUI_MOTIF
10859 "gui_motif",
10860#endif
10861#ifdef FEAT_GUI_PHOTON
10862 "gui_photon",
10863#endif
10864#ifdef FEAT_GUI_W16
10865 "gui_win16",
10866#endif
10867#ifdef FEAT_GUI_W32
10868 "gui_win32",
10869#endif
10870#ifdef FEAT_HANGULIN
10871 "hangul_input",
10872#endif
10873#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10874 "iconv",
10875#endif
10876#ifdef FEAT_INS_EXPAND
10877 "insert_expand",
10878#endif
10879#ifdef FEAT_JUMPLIST
10880 "jumplist",
10881#endif
10882#ifdef FEAT_KEYMAP
10883 "keymap",
10884#endif
10885#ifdef FEAT_LANGMAP
10886 "langmap",
10887#endif
10888#ifdef FEAT_LIBCALL
10889 "libcall",
10890#endif
10891#ifdef FEAT_LINEBREAK
10892 "linebreak",
10893#endif
10894#ifdef FEAT_LISP
10895 "lispindent",
10896#endif
10897#ifdef FEAT_LISTCMDS
10898 "listcmds",
10899#endif
10900#ifdef FEAT_LOCALMAP
10901 "localmap",
10902#endif
10903#ifdef FEAT_MENU
10904 "menu",
10905#endif
10906#ifdef FEAT_SESSION
10907 "mksession",
10908#endif
10909#ifdef FEAT_MODIFY_FNAME
10910 "modify_fname",
10911#endif
10912#ifdef FEAT_MOUSE
10913 "mouse",
10914#endif
10915#ifdef FEAT_MOUSESHAPE
10916 "mouseshape",
10917#endif
10918#if defined(UNIX) || defined(VMS)
10919# ifdef FEAT_MOUSE_DEC
10920 "mouse_dec",
10921# endif
10922# ifdef FEAT_MOUSE_GPM
10923 "mouse_gpm",
10924# endif
10925# ifdef FEAT_MOUSE_JSB
10926 "mouse_jsbterm",
10927# endif
10928# ifdef FEAT_MOUSE_NET
10929 "mouse_netterm",
10930# endif
10931# ifdef FEAT_MOUSE_PTERM
10932 "mouse_pterm",
10933# endif
10934# ifdef FEAT_MOUSE_XTERM
10935 "mouse_xterm",
10936# endif
10937#endif
10938#ifdef FEAT_MBYTE
10939 "multi_byte",
10940#endif
10941#ifdef FEAT_MBYTE_IME
10942 "multi_byte_ime",
10943#endif
10944#ifdef FEAT_MULTI_LANG
10945 "multi_lang",
10946#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010947#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010948#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010949 "mzscheme",
10950#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952#ifdef FEAT_OLE
10953 "ole",
10954#endif
10955#ifdef FEAT_OSFILETYPE
10956 "osfiletype",
10957#endif
10958#ifdef FEAT_PATH_EXTRA
10959 "path_extra",
10960#endif
10961#ifdef FEAT_PERL
10962#ifndef DYNAMIC_PERL
10963 "perl",
10964#endif
10965#endif
10966#ifdef FEAT_PYTHON
10967#ifndef DYNAMIC_PYTHON
10968 "python",
10969#endif
10970#endif
10971#ifdef FEAT_POSTSCRIPT
10972 "postscript",
10973#endif
10974#ifdef FEAT_PRINTER
10975 "printer",
10976#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010977#ifdef FEAT_PROFILE
10978 "profile",
10979#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010980#ifdef FEAT_RELTIME
10981 "reltime",
10982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983#ifdef FEAT_QUICKFIX
10984 "quickfix",
10985#endif
10986#ifdef FEAT_RIGHTLEFT
10987 "rightleft",
10988#endif
10989#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10990 "ruby",
10991#endif
10992#ifdef FEAT_SCROLLBIND
10993 "scrollbind",
10994#endif
10995#ifdef FEAT_CMDL_INFO
10996 "showcmd",
10997 "cmdline_info",
10998#endif
10999#ifdef FEAT_SIGNS
11000 "signs",
11001#endif
11002#ifdef FEAT_SMARTINDENT
11003 "smartindent",
11004#endif
11005#ifdef FEAT_SNIFF
11006 "sniff",
11007#endif
11008#ifdef FEAT_STL_OPT
11009 "statusline",
11010#endif
11011#ifdef FEAT_SUN_WORKSHOP
11012 "sun_workshop",
11013#endif
11014#ifdef FEAT_NETBEANS_INTG
11015 "netbeans_intg",
11016#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011017#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011018 "spell",
11019#endif
11020#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 "syntax",
11022#endif
11023#if defined(USE_SYSTEM) || !defined(UNIX)
11024 "system",
11025#endif
11026#ifdef FEAT_TAG_BINS
11027 "tag_binary",
11028#endif
11029#ifdef FEAT_TAG_OLDSTATIC
11030 "tag_old_static",
11031#endif
11032#ifdef FEAT_TAG_ANYWHITE
11033 "tag_any_white",
11034#endif
11035#ifdef FEAT_TCL
11036# ifndef DYNAMIC_TCL
11037 "tcl",
11038# endif
11039#endif
11040#ifdef TERMINFO
11041 "terminfo",
11042#endif
11043#ifdef FEAT_TERMRESPONSE
11044 "termresponse",
11045#endif
11046#ifdef FEAT_TEXTOBJ
11047 "textobjects",
11048#endif
11049#ifdef HAVE_TGETENT
11050 "tgetent",
11051#endif
11052#ifdef FEAT_TITLE
11053 "title",
11054#endif
11055#ifdef FEAT_TOOLBAR
11056 "toolbar",
11057#endif
11058#ifdef FEAT_USR_CMDS
11059 "user-commands", /* was accidentally included in 5.4 */
11060 "user_commands",
11061#endif
11062#ifdef FEAT_VIMINFO
11063 "viminfo",
11064#endif
11065#ifdef FEAT_VERTSPLIT
11066 "vertsplit",
11067#endif
11068#ifdef FEAT_VIRTUALEDIT
11069 "virtualedit",
11070#endif
11071#ifdef FEAT_VISUAL
11072 "visual",
11073#endif
11074#ifdef FEAT_VISUALEXTRA
11075 "visualextra",
11076#endif
11077#ifdef FEAT_VREPLACE
11078 "vreplace",
11079#endif
11080#ifdef FEAT_WILDIGN
11081 "wildignore",
11082#endif
11083#ifdef FEAT_WILDMENU
11084 "wildmenu",
11085#endif
11086#ifdef FEAT_WINDOWS
11087 "windows",
11088#endif
11089#ifdef FEAT_WAK
11090 "winaltkeys",
11091#endif
11092#ifdef FEAT_WRITEBACKUP
11093 "writebackup",
11094#endif
11095#ifdef FEAT_XIM
11096 "xim",
11097#endif
11098#ifdef FEAT_XFONTSET
11099 "xfontset",
11100#endif
11101#ifdef USE_XSMP
11102 "xsmp",
11103#endif
11104#ifdef USE_XSMP_INTERACT
11105 "xsmp_interact",
11106#endif
11107#ifdef FEAT_XCLIPBOARD
11108 "xterm_clipboard",
11109#endif
11110#ifdef FEAT_XTERM_SAVE
11111 "xterm_save",
11112#endif
11113#if defined(UNIX) && defined(FEAT_X11)
11114 "X11",
11115#endif
11116 NULL
11117 };
11118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 for (i = 0; has_list[i] != NULL; ++i)
11121 if (STRICMP(name, has_list[i]) == 0)
11122 {
11123 n = TRUE;
11124 break;
11125 }
11126
11127 if (n == FALSE)
11128 {
11129 if (STRNICMP(name, "patch", 5) == 0)
11130 n = has_patch(atoi((char *)name + 5));
11131 else if (STRICMP(name, "vim_starting") == 0)
11132 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011133#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11134 else if (STRICMP(name, "balloon_multiline") == 0)
11135 n = multiline_balloon_available();
11136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137#ifdef DYNAMIC_TCL
11138 else if (STRICMP(name, "tcl") == 0)
11139 n = tcl_enabled(FALSE);
11140#endif
11141#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11142 else if (STRICMP(name, "iconv") == 0)
11143 n = iconv_enabled(FALSE);
11144#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011145#ifdef DYNAMIC_MZSCHEME
11146 else if (STRICMP(name, "mzscheme") == 0)
11147 n = mzscheme_enabled(FALSE);
11148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149#ifdef DYNAMIC_RUBY
11150 else if (STRICMP(name, "ruby") == 0)
11151 n = ruby_enabled(FALSE);
11152#endif
11153#ifdef DYNAMIC_PYTHON
11154 else if (STRICMP(name, "python") == 0)
11155 n = python_enabled(FALSE);
11156#endif
11157#ifdef DYNAMIC_PERL
11158 else if (STRICMP(name, "perl") == 0)
11159 n = perl_enabled(FALSE);
11160#endif
11161#ifdef FEAT_GUI
11162 else if (STRICMP(name, "gui_running") == 0)
11163 n = (gui.in_use || gui.starting);
11164# ifdef FEAT_GUI_W32
11165 else if (STRICMP(name, "gui_win32s") == 0)
11166 n = gui_is_win32s();
11167# endif
11168# ifdef FEAT_BROWSE
11169 else if (STRICMP(name, "browse") == 0)
11170 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11171# endif
11172#endif
11173#ifdef FEAT_SYN_HL
11174 else if (STRICMP(name, "syntax_items") == 0)
11175 n = syntax_present(curbuf);
11176#endif
11177#if defined(WIN3264)
11178 else if (STRICMP(name, "win95") == 0)
11179 n = mch_windows95();
11180#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011181#ifdef FEAT_NETBEANS_INTG
11182 else if (STRICMP(name, "netbeans_enabled") == 0)
11183 n = usingNetbeans;
11184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 }
11186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188}
11189
11190/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011191 * "has_key()" function
11192 */
11193 static void
11194f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011195 typval_T *argvars;
11196 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011197{
11198 rettv->vval.v_number = 0;
11199 if (argvars[0].v_type != VAR_DICT)
11200 {
11201 EMSG(_(e_dictreq));
11202 return;
11203 }
11204 if (argvars[0].vval.v_dict == NULL)
11205 return;
11206
11207 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011208 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011209}
11210
11211/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011212 * "haslocaldir()" function
11213 */
11214/*ARGSUSED*/
11215 static void
11216f_haslocaldir(argvars, rettv)
11217 typval_T *argvars;
11218 typval_T *rettv;
11219{
11220 rettv->vval.v_number = (curwin->w_localdir != NULL);
11221}
11222
11223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 * "hasmapto()" function
11225 */
11226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011227f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011228 typval_T *argvars;
11229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230{
11231 char_u *name;
11232 char_u *mode;
11233 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011234 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011237 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 mode = (char_u *)"nvo";
11239 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011242 if (argvars[2].v_type != VAR_UNKNOWN)
11243 abbr = get_tv_number(&argvars[2]);
11244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245
Bram Moolenaar2c932302006-03-18 21:42:09 +000011246 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250}
11251
11252/*
11253 * "histadd()" function
11254 */
11255/*ARGSUSED*/
11256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257f_histadd(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#ifdef FEAT_CMDHIST
11262 int histype;
11263 char_u *str;
11264 char_u buf[NUMBUFLEN];
11265#endif
11266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 if (check_restricted() || check_secure())
11269 return;
11270#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11272 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 if (histype >= 0)
11274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 if (*str != NUL)
11277 {
11278 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 return;
11281 }
11282 }
11283#endif
11284}
11285
11286/*
11287 * "histdel()" function
11288 */
11289/*ARGSUSED*/
11290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *argvars;
11293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294{
11295#ifdef FEAT_CMDHIST
11296 int n;
11297 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11301 if (str == NULL)
11302 n = 0;
11303 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011306 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 else
11311 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313 get_tv_string_buf(&argvars[1], buf));
11314 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317#endif
11318}
11319
11320/*
11321 * "histget()" function
11322 */
11323/*ARGSUSED*/
11324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011325f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011326 typval_T *argvars;
11327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328{
11329#ifdef FEAT_CMDHIST
11330 int type;
11331 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011334 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11335 if (str == NULL)
11336 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011338 {
11339 type = get_histtype(str);
11340 if (argvars[1].v_type == VAR_UNKNOWN)
11341 idx = get_history_idx(type);
11342 else
11343 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11344 /* -1 on type error */
11345 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351}
11352
11353/*
11354 * "histnr()" function
11355 */
11356/*ARGSUSED*/
11357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011359 typval_T *argvars;
11360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361{
11362 int i;
11363
11364#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 char_u *history = get_tv_string_chk(&argvars[0]);
11366
11367 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 if (i >= HIST_CMD && i < HIST_COUNT)
11369 i = get_history_idx(i);
11370 else
11371#endif
11372 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374}
11375
11376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 * "highlightID(name)" function
11378 */
11379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011381 typval_T *argvars;
11382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385}
11386
11387/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388 * "highlight_exists()" function
11389 */
11390 static void
11391f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011392 typval_T *argvars;
11393 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011394{
11395 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11396}
11397
11398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 * "hostname()" function
11400 */
11401/*ARGSUSED*/
11402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011404 typval_T *argvars;
11405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406{
11407 char_u hostname[256];
11408
11409 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 rettv->v_type = VAR_STRING;
11411 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412}
11413
11414/*
11415 * iconv() function
11416 */
11417/*ARGSUSED*/
11418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 typval_T *argvars;
11421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422{
11423#ifdef FEAT_MBYTE
11424 char_u buf1[NUMBUFLEN];
11425 char_u buf2[NUMBUFLEN];
11426 char_u *from, *to, *str;
11427 vimconv_T vimconv;
11428#endif
11429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011430 rettv->v_type = VAR_STRING;
11431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432
11433#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 str = get_tv_string(&argvars[0]);
11435 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11436 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 vimconv.vc_type = CONV_NONE;
11438 convert_setup(&vimconv, from, to);
11439
11440 /* If the encodings are equal, no conversion needed. */
11441 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445
11446 convert_setup(&vimconv, NULL, NULL);
11447 vim_free(from);
11448 vim_free(to);
11449#endif
11450}
11451
11452/*
11453 * "indent()" function
11454 */
11455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011457 typval_T *argvars;
11458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459{
11460 linenr_T lnum;
11461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467}
11468
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011469/*
11470 * "index()" function
11471 */
11472 static void
11473f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 typval_T *argvars;
11475 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011476{
Bram Moolenaar33570922005-01-25 22:26:29 +000011477 list_T *l;
11478 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011479 long idx = 0;
11480 int ic = FALSE;
11481
11482 rettv->vval.v_number = -1;
11483 if (argvars[0].v_type != VAR_LIST)
11484 {
11485 EMSG(_(e_listreq));
11486 return;
11487 }
11488 l = argvars[0].vval.v_list;
11489 if (l != NULL)
11490 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011491 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011492 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494 int error = FALSE;
11495
Bram Moolenaar758711c2005-02-02 23:11:38 +000011496 /* Start at specified item. Use the cached index that list_find()
11497 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011499 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011500 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 ic = get_tv_number_chk(&argvars[3], &error);
11502 if (error)
11503 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011504 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011505
Bram Moolenaar758711c2005-02-02 23:11:38 +000011506 for ( ; item != NULL; item = item->li_next, ++idx)
11507 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011508 {
11509 rettv->vval.v_number = idx;
11510 break;
11511 }
11512 }
11513}
11514
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515static int inputsecret_flag = 0;
11516
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011517static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11518
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011520 * This function is used by f_input() and f_inputdialog() functions. The third
11521 * argument to f_input() specifies the type of completion to use at the
11522 * prompt. The third argument to f_inputdialog() specifies the value to return
11523 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 */
11525 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011526get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011527 typval_T *argvars;
11528 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011529 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 char_u *p = NULL;
11533 int c;
11534 char_u buf[NUMBUFLEN];
11535 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011536 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011537 int xp_type = EXPAND_NOTHING;
11538 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541
11542#ifdef NO_CONSOLE_INPUT
11543 /* While starting up, there is no place to enter text. */
11544 if (no_console_input())
11545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547 return;
11548 }
11549#endif
11550
11551 cmd_silent = FALSE; /* Want to see the prompt. */
11552 if (prompt != NULL)
11553 {
11554 /* Only the part of the message after the last NL is considered as
11555 * prompt for the command line */
11556 p = vim_strrchr(prompt, '\n');
11557 if (p == NULL)
11558 p = prompt;
11559 else
11560 {
11561 ++p;
11562 c = *p;
11563 *p = NUL;
11564 msg_start();
11565 msg_clr_eos();
11566 msg_puts_attr(prompt, echo_attr);
11567 msg_didout = FALSE;
11568 msg_starthere();
11569 *p = c;
11570 }
11571 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 if (argvars[1].v_type != VAR_UNKNOWN)
11574 {
11575 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11576 if (defstr != NULL)
11577 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011579 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011580 {
11581 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011582 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011583 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011584
Bram Moolenaar4463f292005-09-25 22:20:24 +000011585 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011586
Bram Moolenaar4463f292005-09-25 22:20:24 +000011587 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11588 if (xp_name == NULL)
11589 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011590
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011591 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011592
Bram Moolenaar4463f292005-09-25 22:20:24 +000011593 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11594 &xp_arg) == FAIL)
11595 return;
11596 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011597 }
11598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 if (defstr != NULL)
11600 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011601 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11602 xp_type, xp_arg);
11603
11604 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011606 /* since the user typed this, no need to wait for return */
11607 need_wait_return = FALSE;
11608 msg_didout = FALSE;
11609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 cmd_silent = cmd_silent_save;
11611}
11612
11613/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011614 * "input()" function
11615 * Also handles inputsecret() when inputsecret is set.
11616 */
11617 static void
11618f_input(argvars, rettv)
11619 typval_T *argvars;
11620 typval_T *rettv;
11621{
11622 get_user_input(argvars, rettv, FALSE);
11623}
11624
11625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 * "inputdialog()" function
11627 */
11628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011630 typval_T *argvars;
11631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632{
11633#if defined(FEAT_GUI_TEXTDIALOG)
11634 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11635 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11636 {
11637 char_u *message;
11638 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011639 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011641 message = get_tv_string_chk(&argvars[0]);
11642 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011643 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011644 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645 else
11646 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 if (message != NULL && defstr != NULL
11648 && do_dialog(VIM_QUESTION, NULL, message,
11649 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011650 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651 else
11652 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011653 if (message != NULL && defstr != NULL
11654 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011655 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011656 rettv->vval.v_string = vim_strsave(
11657 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 }
11663 else
11664#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011665 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666}
11667
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011668/*
11669 * "inputlist()" function
11670 */
11671 static void
11672f_inputlist(argvars, rettv)
11673 typval_T *argvars;
11674 typval_T *rettv;
11675{
11676 listitem_T *li;
11677 int selected;
11678 int mouse_used;
11679
11680 rettv->vval.v_number = 0;
11681#ifdef NO_CONSOLE_INPUT
11682 /* While starting up, there is no place to enter text. */
11683 if (no_console_input())
11684 return;
11685#endif
11686 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11687 {
11688 EMSG2(_(e_listarg), "inputlist()");
11689 return;
11690 }
11691
11692 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011693 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011694 lines_left = Rows; /* avoid more prompt */
11695 msg_scroll = TRUE;
11696 msg_clr_eos();
11697
11698 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11699 {
11700 msg_puts(get_tv_string(&li->li_tv));
11701 msg_putchar('\n');
11702 }
11703
11704 /* Ask for choice. */
11705 selected = prompt_for_number(&mouse_used);
11706 if (mouse_used)
11707 selected -= lines_left;
11708
11709 rettv->vval.v_number = selected;
11710}
11711
11712
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11714
11715/*
11716 * "inputrestore()" function
11717 */
11718/*ARGSUSED*/
11719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011721 typval_T *argvars;
11722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723{
11724 if (ga_userinput.ga_len > 0)
11725 {
11726 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11728 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 }
11731 else if (p_verbose > 1)
11732 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011733 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 }
11736}
11737
11738/*
11739 * "inputsave()" function
11740 */
11741/*ARGSUSED*/
11742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011744 typval_T *argvars;
11745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746{
11747 /* Add an entry to the stack of typehead storage. */
11748 if (ga_grow(&ga_userinput, 1) == OK)
11749 {
11750 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11751 + ga_userinput.ga_len);
11752 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 }
11755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011756 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757}
11758
11759/*
11760 * "inputsecret()" function
11761 */
11762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011764 typval_T *argvars;
11765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766{
11767 ++cmdline_star;
11768 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 --cmdline_star;
11771 --inputsecret_flag;
11772}
11773
11774/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011775 * "insert()" function
11776 */
11777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011779 typval_T *argvars;
11780 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011781{
11782 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011783 listitem_T *item;
11784 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011786
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011787 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011788 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011789 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011790 else if ((l = argvars[0].vval.v_list) != NULL
11791 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011792 {
11793 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011794 before = get_tv_number_chk(&argvars[2], &error);
11795 if (error)
11796 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011797
Bram Moolenaar758711c2005-02-02 23:11:38 +000011798 if (before == l->lv_len)
11799 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011800 else
11801 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011802 item = list_find(l, before);
11803 if (item == NULL)
11804 {
11805 EMSGN(_(e_listidx), before);
11806 l = NULL;
11807 }
11808 }
11809 if (l != NULL)
11810 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011811 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011812 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011813 }
11814 }
11815}
11816
11817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 * "isdirectory()" function
11819 */
11820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011822 typval_T *argvars;
11823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826}
11827
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011828/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011829 * "islocked()" function
11830 */
11831 static void
11832f_islocked(argvars, rettv)
11833 typval_T *argvars;
11834 typval_T *rettv;
11835{
11836 lval_T lv;
11837 char_u *end;
11838 dictitem_T *di;
11839
11840 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011841 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11842 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011843 if (end != NULL && lv.ll_name != NULL)
11844 {
11845 if (*end != NUL)
11846 EMSG(_(e_trailing));
11847 else
11848 {
11849 if (lv.ll_tv == NULL)
11850 {
11851 if (check_changedtick(lv.ll_name))
11852 rettv->vval.v_number = 1; /* always locked */
11853 else
11854 {
11855 di = find_var(lv.ll_name, NULL);
11856 if (di != NULL)
11857 {
11858 /* Consider a variable locked when:
11859 * 1. the variable itself is locked
11860 * 2. the value of the variable is locked.
11861 * 3. the List or Dict value is locked.
11862 */
11863 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11864 || tv_islocked(&di->di_tv));
11865 }
11866 }
11867 }
11868 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011869 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011870 else if (lv.ll_newkey != NULL)
11871 EMSG2(_(e_dictkey), lv.ll_newkey);
11872 else if (lv.ll_list != NULL)
11873 /* List item. */
11874 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11875 else
11876 /* Dictionary item. */
11877 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11878 }
11879 }
11880
11881 clear_lval(&lv);
11882}
11883
Bram Moolenaar33570922005-01-25 22:26:29 +000011884static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011885
11886/*
11887 * Turn a dict into a list:
11888 * "what" == 0: list of keys
11889 * "what" == 1: list of values
11890 * "what" == 2: list of items
11891 */
11892 static void
11893dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011894 typval_T *argvars;
11895 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011896 int what;
11897{
Bram Moolenaar33570922005-01-25 22:26:29 +000011898 list_T *l2;
11899 dictitem_T *di;
11900 hashitem_T *hi;
11901 listitem_T *li;
11902 listitem_T *li2;
11903 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011904 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011905
11906 rettv->vval.v_number = 0;
11907 if (argvars[0].v_type != VAR_DICT)
11908 {
11909 EMSG(_(e_dictreq));
11910 return;
11911 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011912 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011913 return;
11914
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011915 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011916 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011918 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011920 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011921 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011922 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011923 --todo;
11924 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011925
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011926 li = listitem_alloc();
11927 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011928 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011929 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011930
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011931 if (what == 0)
11932 {
11933 /* keys() */
11934 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011935 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011936 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11937 }
11938 else if (what == 1)
11939 {
11940 /* values() */
11941 copy_tv(&di->di_tv, &li->li_tv);
11942 }
11943 else
11944 {
11945 /* items() */
11946 l2 = list_alloc();
11947 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011948 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011949 li->li_tv.vval.v_list = l2;
11950 if (l2 == NULL)
11951 break;
11952 ++l2->lv_refcount;
11953
11954 li2 = listitem_alloc();
11955 if (li2 == NULL)
11956 break;
11957 list_append(l2, li2);
11958 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011959 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011960 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11961
11962 li2 = listitem_alloc();
11963 if (li2 == NULL)
11964 break;
11965 list_append(l2, li2);
11966 copy_tv(&di->di_tv, &li2->li_tv);
11967 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011968 }
11969 }
11970}
11971
11972/*
11973 * "items(dict)" function
11974 */
11975 static void
11976f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011977 typval_T *argvars;
11978 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011979{
11980 dict_list(argvars, rettv, 2);
11981}
11982
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011984 * "join()" function
11985 */
11986 static void
11987f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011988 typval_T *argvars;
11989 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011990{
11991 garray_T ga;
11992 char_u *sep;
11993
11994 rettv->vval.v_number = 0;
11995 if (argvars[0].v_type != VAR_LIST)
11996 {
11997 EMSG(_(e_listreq));
11998 return;
11999 }
12000 if (argvars[0].vval.v_list == NULL)
12001 return;
12002 if (argvars[1].v_type == VAR_UNKNOWN)
12003 sep = (char_u *)" ";
12004 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012005 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012006
12007 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012008
12009 if (sep != NULL)
12010 {
12011 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012012 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013 ga_append(&ga, NUL);
12014 rettv->vval.v_string = (char_u *)ga.ga_data;
12015 }
12016 else
12017 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012018}
12019
12020/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012021 * "keys()" function
12022 */
12023 static void
12024f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012025 typval_T *argvars;
12026 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012027{
12028 dict_list(argvars, rettv, 0);
12029}
12030
12031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 * "last_buffer_nr()" function.
12033 */
12034/*ARGSUSED*/
12035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012037 typval_T *argvars;
12038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039{
12040 int n = 0;
12041 buf_T *buf;
12042
12043 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12044 if (n < buf->b_fnum)
12045 n = buf->b_fnum;
12046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012047 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012048}
12049
12050/*
12051 * "len()" function
12052 */
12053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012054f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012055 typval_T *argvars;
12056 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012057{
12058 switch (argvars[0].v_type)
12059 {
12060 case VAR_STRING:
12061 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 rettv->vval.v_number = (varnumber_T)STRLEN(
12063 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012064 break;
12065 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012066 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012067 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012068 case VAR_DICT:
12069 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12070 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012071 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012072 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012073 break;
12074 }
12075}
12076
Bram Moolenaar33570922005-01-25 22:26:29 +000012077static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012078
12079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012081 typval_T *argvars;
12082 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012083 int type;
12084{
12085#ifdef FEAT_LIBCALL
12086 char_u *string_in;
12087 char_u **string_result;
12088 int nr_result;
12089#endif
12090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012092 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012094 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012096
12097 if (check_restricted() || check_secure())
12098 return;
12099
12100#ifdef FEAT_LIBCALL
12101 /* The first two args must be strings, otherwise its meaningless */
12102 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12103 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012104 string_in = NULL;
12105 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012106 string_in = argvars[2].vval.v_string;
12107 if (type == VAR_NUMBER)
12108 string_result = NULL;
12109 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012110 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012111 if (mch_libcall(argvars[0].vval.v_string,
12112 argvars[1].vval.v_string,
12113 string_in,
12114 argvars[2].vval.v_number,
12115 string_result,
12116 &nr_result) == OK
12117 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012119 }
12120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121}
12122
12123/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012124 * "libcall()" function
12125 */
12126 static void
12127f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012128 typval_T *argvars;
12129 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130{
12131 libcall_common(argvars, rettv, VAR_STRING);
12132}
12133
12134/*
12135 * "libcallnr()" function
12136 */
12137 static void
12138f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012139 typval_T *argvars;
12140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141{
12142 libcall_common(argvars, rettv, VAR_NUMBER);
12143}
12144
12145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 * "line(string)" function
12147 */
12148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012150 typval_T *argvars;
12151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152{
12153 linenr_T lnum = 0;
12154 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012155 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012157 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 if (fp != NULL)
12159 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161}
12162
12163/*
12164 * "line2byte(lnum)" function
12165 */
12166/*ARGSUSED*/
12167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012169 typval_T *argvars;
12170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171{
12172#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012173 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174#else
12175 linenr_T lnum;
12176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012177 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12182 if (rettv->vval.v_number >= 0)
12183 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184#endif
12185}
12186
12187/*
12188 * "lispindent(lnum)" function
12189 */
12190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012192 typval_T *argvars;
12193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
12195#ifdef FEAT_LISP
12196 pos_T pos;
12197 linenr_T lnum;
12198
12199 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12202 {
12203 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 curwin->w_cursor = pos;
12206 }
12207 else
12208#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012209 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210}
12211
12212/*
12213 * "localtime()" function
12214 */
12215/*ARGSUSED*/
12216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012218 typval_T *argvars;
12219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012221 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222}
12223
Bram Moolenaar33570922005-01-25 22:26:29 +000012224static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225
12226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012228 typval_T *argvars;
12229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 int exact;
12231{
12232 char_u *keys;
12233 char_u *which;
12234 char_u buf[NUMBUFLEN];
12235 char_u *keys_buf = NULL;
12236 char_u *rhs;
12237 int mode;
12238 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012239 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240
12241 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012242 rettv->v_type = VAR_STRING;
12243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 if (*keys == NUL)
12247 return;
12248
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012249 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012251 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012252 if (argvars[2].v_type != VAR_UNKNOWN)
12253 abbr = get_tv_number(&argvars[2]);
12254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 else
12256 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012257 if (which == NULL)
12258 return;
12259
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 mode = get_map_mode(&which, 0);
12261
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012262 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012263 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 vim_free(keys_buf);
12265 if (rhs != NULL)
12266 {
12267 ga_init(&ga);
12268 ga.ga_itemsize = 1;
12269 ga.ga_growsize = 40;
12270
12271 while (*rhs != NUL)
12272 ga_concat(&ga, str2special(&rhs, FALSE));
12273
12274 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 }
12277}
12278
12279/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012280 * "map()" function
12281 */
12282 static void
12283f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012284 typval_T *argvars;
12285 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012286{
12287 filter_map(argvars, rettv, TRUE);
12288}
12289
12290/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012291 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 */
12293 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012294f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012295 typval_T *argvars;
12296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012298 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299}
12300
12301/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012302 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 */
12304 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012305f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012306 typval_T *argvars;
12307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012309 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310}
12311
Bram Moolenaar33570922005-01-25 22:26:29 +000012312static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313
12314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012316 typval_T *argvars;
12317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 int type;
12319{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012320 char_u *str = NULL;
12321 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 char_u *pat;
12323 regmatch_T regmatch;
12324 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012325 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 char_u *save_cpo;
12327 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012328 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012329 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012330 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012331 list_T *l = NULL;
12332 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012333 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012334 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335
12336 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12337 save_cpo = p_cpo;
12338 p_cpo = (char_u *)"";
12339
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012340 rettv->vval.v_number = -1;
12341 if (type == 3)
12342 {
12343 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012344 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012345 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012346 }
12347 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 rettv->v_type = VAR_STRING;
12350 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012353 if (argvars[0].v_type == VAR_LIST)
12354 {
12355 if ((l = argvars[0].vval.v_list) == NULL)
12356 goto theend;
12357 li = l->lv_first;
12358 }
12359 else
12360 expr = str = get_tv_string(&argvars[0]);
12361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012362 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12363 if (pat == NULL)
12364 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012365
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012366 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012368 int error = FALSE;
12369
12370 start = get_tv_number_chk(&argvars[2], &error);
12371 if (error)
12372 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012373 if (l != NULL)
12374 {
12375 li = list_find(l, start);
12376 if (li == NULL)
12377 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012378 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012379 }
12380 else
12381 {
12382 if (start < 0)
12383 start = 0;
12384 if (start > (long)STRLEN(str))
12385 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012386 /* When "count" argument is there ignore matches before "start",
12387 * otherwise skip part of the string. Differs when pattern is "^"
12388 * or "\<". */
12389 if (argvars[3].v_type != VAR_UNKNOWN)
12390 startcol = start;
12391 else
12392 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012393 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012394
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012395 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012396 nth = get_tv_number_chk(&argvars[3], &error);
12397 if (error)
12398 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 }
12400
12401 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12402 if (regmatch.regprog != NULL)
12403 {
12404 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012405
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012406 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012407 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012408 if (l != NULL)
12409 {
12410 if (li == NULL)
12411 {
12412 match = FALSE;
12413 break;
12414 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012415 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012416 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012417 if (str == NULL)
12418 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012419 }
12420
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012421 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012422
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012423 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012424 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012425 if (l == NULL && !match)
12426 break;
12427
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012428 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012429 if (l != NULL)
12430 {
12431 li = li->li_next;
12432 ++idx;
12433 }
12434 else
12435 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012436#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012437 startcol = (colnr_T)(regmatch.startp[0]
12438 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012439#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012440 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012441#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012442 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012443 }
12444
12445 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012447 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012448 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012449 int i;
12450
12451 /* return list with matched string and submatches */
12452 for (i = 0; i < NSUBEXP; ++i)
12453 {
12454 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012455 {
12456 if (list_append_string(rettv->vval.v_list,
12457 (char_u *)"", 0) == FAIL)
12458 break;
12459 }
12460 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012461 regmatch.startp[i],
12462 (int)(regmatch.endp[i] - regmatch.startp[i]))
12463 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012464 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012465 }
12466 }
12467 else if (type == 2)
12468 {
12469 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012470 if (l != NULL)
12471 copy_tv(&li->li_tv, rettv);
12472 else
12473 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012475 }
12476 else if (l != NULL)
12477 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478 else
12479 {
12480 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 (varnumber_T)(regmatch.startp[0] - str);
12483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012484 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012486 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 }
12488 }
12489 vim_free(regmatch.regprog);
12490 }
12491
12492theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012493 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494 p_cpo = save_cpo;
12495}
12496
12497/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012498 * "match()" function
12499 */
12500 static void
12501f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 typval_T *argvars;
12503 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012504{
12505 find_some_match(argvars, rettv, 1);
12506}
12507
12508/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012509 * "matchadd()" function
12510 */
12511 static void
12512f_matchadd(argvars, rettv)
12513 typval_T *argvars;
12514 typval_T *rettv;
12515{
12516#ifdef FEAT_SEARCH_EXTRA
12517 char_u buf[NUMBUFLEN];
12518 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12519 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12520 int prio = 10; /* default priority */
12521 int id = -1;
12522 int error = FALSE;
12523
12524 rettv->vval.v_number = -1;
12525
12526 if (grp == NULL || pat == NULL)
12527 return;
12528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012529 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012530 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012531 if (argvars[3].v_type != VAR_UNKNOWN)
12532 id = get_tv_number_chk(&argvars[3], &error);
12533 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012534 if (error == TRUE)
12535 return;
12536 if (id >= 1 && id <= 3)
12537 {
12538 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12539 return;
12540 }
12541
12542 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12543#endif
12544}
12545
12546/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012547 * "matcharg()" function
12548 */
12549 static void
12550f_matcharg(argvars, rettv)
12551 typval_T *argvars;
12552 typval_T *rettv;
12553{
12554 if (rettv_list_alloc(rettv) == OK)
12555 {
12556#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012557 int id = get_tv_number(&argvars[0]);
12558 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012559
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012560 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012561 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012562 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12563 {
12564 list_append_string(rettv->vval.v_list,
12565 syn_id2name(m->hlg_id), -1);
12566 list_append_string(rettv->vval.v_list, m->pattern, -1);
12567 }
12568 else
12569 {
12570 list_append_string(rettv->vval.v_list, NUL, -1);
12571 list_append_string(rettv->vval.v_list, NUL, -1);
12572 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012573 }
12574#endif
12575 }
12576}
12577
12578/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012579 * "matchdelete()" function
12580 */
12581 static void
12582f_matchdelete(argvars, rettv)
12583 typval_T *argvars;
12584 typval_T *rettv;
12585{
12586#ifdef FEAT_SEARCH_EXTRA
12587 rettv->vval.v_number = match_delete(curwin,
12588 (int)get_tv_number(&argvars[0]), TRUE);
12589#endif
12590}
12591
12592/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012593 * "matchend()" function
12594 */
12595 static void
12596f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012597 typval_T *argvars;
12598 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012599{
12600 find_some_match(argvars, rettv, 0);
12601}
12602
12603/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012604 * "matchlist()" function
12605 */
12606 static void
12607f_matchlist(argvars, rettv)
12608 typval_T *argvars;
12609 typval_T *rettv;
12610{
12611 find_some_match(argvars, rettv, 3);
12612}
12613
12614/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615 * "matchstr()" function
12616 */
12617 static void
12618f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012619 typval_T *argvars;
12620 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012621{
12622 find_some_match(argvars, rettv, 2);
12623}
12624
Bram Moolenaar33570922005-01-25 22:26:29 +000012625static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012626
12627 static void
12628max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012629 typval_T *argvars;
12630 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012631 int domax;
12632{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012633 long n = 0;
12634 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012636
12637 if (argvars[0].v_type == VAR_LIST)
12638 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 list_T *l;
12640 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012641
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012642 l = argvars[0].vval.v_list;
12643 if (l != NULL)
12644 {
12645 li = l->lv_first;
12646 if (li != NULL)
12647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012648 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012649 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012650 {
12651 li = li->li_next;
12652 if (li == NULL)
12653 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012654 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012655 if (domax ? i > n : i < n)
12656 n = i;
12657 }
12658 }
12659 }
12660 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012661 else if (argvars[0].v_type == VAR_DICT)
12662 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012663 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012664 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012665 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012666 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012667
12668 d = argvars[0].vval.v_dict;
12669 if (d != NULL)
12670 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012671 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012672 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012673 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012675 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012676 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012677 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012678 if (first)
12679 {
12680 n = i;
12681 first = FALSE;
12682 }
12683 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012684 n = i;
12685 }
12686 }
12687 }
12688 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012689 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012690 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012691 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012692}
12693
12694/*
12695 * "max()" function
12696 */
12697 static void
12698f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012699 typval_T *argvars;
12700 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012701{
12702 max_min(argvars, rettv, TRUE);
12703}
12704
12705/*
12706 * "min()" function
12707 */
12708 static void
12709f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012710 typval_T *argvars;
12711 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012712{
12713 max_min(argvars, rettv, FALSE);
12714}
12715
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012716static int mkdir_recurse __ARGS((char_u *dir, int prot));
12717
12718/*
12719 * Create the directory in which "dir" is located, and higher levels when
12720 * needed.
12721 */
12722 static int
12723mkdir_recurse(dir, prot)
12724 char_u *dir;
12725 int prot;
12726{
12727 char_u *p;
12728 char_u *updir;
12729 int r = FAIL;
12730
12731 /* Get end of directory name in "dir".
12732 * We're done when it's "/" or "c:/". */
12733 p = gettail_sep(dir);
12734 if (p <= get_past_head(dir))
12735 return OK;
12736
12737 /* If the directory exists we're done. Otherwise: create it.*/
12738 updir = vim_strnsave(dir, (int)(p - dir));
12739 if (updir == NULL)
12740 return FAIL;
12741 if (mch_isdir(updir))
12742 r = OK;
12743 else if (mkdir_recurse(updir, prot) == OK)
12744 r = vim_mkdir_emsg(updir, prot);
12745 vim_free(updir);
12746 return r;
12747}
12748
12749#ifdef vim_mkdir
12750/*
12751 * "mkdir()" function
12752 */
12753 static void
12754f_mkdir(argvars, rettv)
12755 typval_T *argvars;
12756 typval_T *rettv;
12757{
12758 char_u *dir;
12759 char_u buf[NUMBUFLEN];
12760 int prot = 0755;
12761
12762 rettv->vval.v_number = FAIL;
12763 if (check_restricted() || check_secure())
12764 return;
12765
12766 dir = get_tv_string_buf(&argvars[0], buf);
12767 if (argvars[1].v_type != VAR_UNKNOWN)
12768 {
12769 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012770 prot = get_tv_number_chk(&argvars[2], NULL);
12771 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012772 mkdir_recurse(dir, prot);
12773 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012774 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012775}
12776#endif
12777
Bram Moolenaar0d660222005-01-07 21:51:51 +000012778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 * "mode()" function
12780 */
12781/*ARGSUSED*/
12782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012784 typval_T *argvars;
12785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786{
12787 char_u buf[2];
12788
12789#ifdef FEAT_VISUAL
12790 if (VIsual_active)
12791 {
12792 if (VIsual_select)
12793 buf[0] = VIsual_mode + 's' - 'v';
12794 else
12795 buf[0] = VIsual_mode;
12796 }
12797 else
12798#endif
12799 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12800 buf[0] = 'r';
12801 else if (State & INSERT)
12802 {
12803 if (State & REPLACE_FLAG)
12804 buf[0] = 'R';
12805 else
12806 buf[0] = 'i';
12807 }
12808 else if (State & CMDLINE)
12809 buf[0] = 'c';
12810 else
12811 buf[0] = 'n';
12812
12813 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 rettv->vval.v_string = vim_strsave(buf);
12815 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816}
12817
12818/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012819 * "nextnonblank()" function
12820 */
12821 static void
12822f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012823 typval_T *argvars;
12824 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012825{
12826 linenr_T lnum;
12827
12828 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012831 {
12832 lnum = 0;
12833 break;
12834 }
12835 if (*skipwhite(ml_get(lnum)) != NUL)
12836 break;
12837 }
12838 rettv->vval.v_number = lnum;
12839}
12840
12841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 * "nr2char()" function
12843 */
12844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *argvars;
12847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848{
12849 char_u buf[NUMBUFLEN];
12850
12851#ifdef FEAT_MBYTE
12852 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 else
12855#endif
12856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 buf[1] = NUL;
12859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860 rettv->v_type = VAR_STRING;
12861 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012862}
12863
12864/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012865 * "pathshorten()" function
12866 */
12867 static void
12868f_pathshorten(argvars, rettv)
12869 typval_T *argvars;
12870 typval_T *rettv;
12871{
12872 char_u *p;
12873
12874 rettv->v_type = VAR_STRING;
12875 p = get_tv_string_chk(&argvars[0]);
12876 if (p == NULL)
12877 rettv->vval.v_string = NULL;
12878 else
12879 {
12880 p = vim_strsave(p);
12881 rettv->vval.v_string = p;
12882 if (p != NULL)
12883 shorten_dir(p);
12884 }
12885}
12886
12887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012888 * "prevnonblank()" function
12889 */
12890 static void
12891f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012892 typval_T *argvars;
12893 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012894{
12895 linenr_T lnum;
12896
12897 lnum = get_tv_lnum(argvars);
12898 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12899 lnum = 0;
12900 else
12901 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12902 --lnum;
12903 rettv->vval.v_number = lnum;
12904}
12905
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012906#ifdef HAVE_STDARG_H
12907/* This dummy va_list is here because:
12908 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12909 * - locally in the function results in a "used before set" warning
12910 * - using va_start() to initialize it gives "function with fixed args" error */
12911static va_list ap;
12912#endif
12913
Bram Moolenaar8c711452005-01-14 21:53:12 +000012914/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012915 * "printf()" function
12916 */
12917 static void
12918f_printf(argvars, rettv)
12919 typval_T *argvars;
12920 typval_T *rettv;
12921{
12922 rettv->v_type = VAR_STRING;
12923 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012924#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012925 {
12926 char_u buf[NUMBUFLEN];
12927 int len;
12928 char_u *s;
12929 int saved_did_emsg = did_emsg;
12930 char *fmt;
12931
12932 /* Get the required length, allocate the buffer and do it for real. */
12933 did_emsg = FALSE;
12934 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012935 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012936 if (!did_emsg)
12937 {
12938 s = alloc(len + 1);
12939 if (s != NULL)
12940 {
12941 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012942 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012943 }
12944 }
12945 did_emsg |= saved_did_emsg;
12946 }
12947#endif
12948}
12949
12950/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012951 * "pumvisible()" function
12952 */
12953/*ARGSUSED*/
12954 static void
12955f_pumvisible(argvars, rettv)
12956 typval_T *argvars;
12957 typval_T *rettv;
12958{
12959 rettv->vval.v_number = 0;
12960#ifdef FEAT_INS_EXPAND
12961 if (pum_visible())
12962 rettv->vval.v_number = 1;
12963#endif
12964}
12965
12966/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012967 * "range()" function
12968 */
12969 static void
12970f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012971 typval_T *argvars;
12972 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012973{
12974 long start;
12975 long end;
12976 long stride = 1;
12977 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012978 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012980 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012981 if (argvars[1].v_type == VAR_UNKNOWN)
12982 {
12983 end = start - 1;
12984 start = 0;
12985 }
12986 else
12987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012988 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012989 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012990 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012991 }
12992
12993 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012994 if (error)
12995 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012996 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012997 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012998 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012999 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013000 else
13001 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013002 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013003 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013004 if (list_append_number(rettv->vval.v_list,
13005 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013006 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013007 }
13008}
13009
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013010/*
13011 * "readfile()" function
13012 */
13013 static void
13014f_readfile(argvars, rettv)
13015 typval_T *argvars;
13016 typval_T *rettv;
13017{
13018 int binary = FALSE;
13019 char_u *fname;
13020 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013021 listitem_T *li;
13022#define FREAD_SIZE 200 /* optimized for text lines */
13023 char_u buf[FREAD_SIZE];
13024 int readlen; /* size of last fread() */
13025 int buflen; /* nr of valid chars in buf[] */
13026 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13027 int tolist; /* first byte in buf[] still to be put in list */
13028 int chop; /* how many CR to chop off */
13029 char_u *prev = NULL; /* previously read bytes, if any */
13030 int prevlen = 0; /* length of "prev" if not NULL */
13031 char_u *s;
13032 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013033 long maxline = MAXLNUM;
13034 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013036 if (argvars[1].v_type != VAR_UNKNOWN)
13037 {
13038 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13039 binary = TRUE;
13040 if (argvars[2].v_type != VAR_UNKNOWN)
13041 maxline = get_tv_number(&argvars[2]);
13042 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013043
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013044 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013045 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013046
13047 /* Always open the file in binary mode, library functions have a mind of
13048 * their own about CR-LF conversion. */
13049 fname = get_tv_string(&argvars[0]);
13050 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13051 {
13052 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13053 return;
13054 }
13055
13056 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013057 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013058 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013059 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013060 buflen = filtd + readlen;
13061 tolist = 0;
13062 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13063 {
13064 if (buf[filtd] == '\n' || readlen <= 0)
13065 {
13066 /* Only when in binary mode add an empty list item when the
13067 * last line ends in a '\n'. */
13068 if (!binary && readlen == 0 && filtd == 0)
13069 break;
13070
13071 /* Found end-of-line or end-of-file: add a text line to the
13072 * list. */
13073 chop = 0;
13074 if (!binary)
13075 while (filtd - chop - 1 >= tolist
13076 && buf[filtd - chop - 1] == '\r')
13077 ++chop;
13078 len = filtd - tolist - chop;
13079 if (prev == NULL)
13080 s = vim_strnsave(buf + tolist, len);
13081 else
13082 {
13083 s = alloc((unsigned)(prevlen + len + 1));
13084 if (s != NULL)
13085 {
13086 mch_memmove(s, prev, prevlen);
13087 vim_free(prev);
13088 prev = NULL;
13089 mch_memmove(s + prevlen, buf + tolist, len);
13090 s[prevlen + len] = NUL;
13091 }
13092 }
13093 tolist = filtd + 1;
13094
13095 li = listitem_alloc();
13096 if (li == NULL)
13097 {
13098 vim_free(s);
13099 break;
13100 }
13101 li->li_tv.v_type = VAR_STRING;
13102 li->li_tv.v_lock = 0;
13103 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013104 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013105
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013106 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013107 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013108 if (readlen <= 0)
13109 break;
13110 }
13111 else if (buf[filtd] == NUL)
13112 buf[filtd] = '\n';
13113 }
13114 if (readlen <= 0)
13115 break;
13116
13117 if (tolist == 0)
13118 {
13119 /* "buf" is full, need to move text to an allocated buffer */
13120 if (prev == NULL)
13121 {
13122 prev = vim_strnsave(buf, buflen);
13123 prevlen = buflen;
13124 }
13125 else
13126 {
13127 s = alloc((unsigned)(prevlen + buflen));
13128 if (s != NULL)
13129 {
13130 mch_memmove(s, prev, prevlen);
13131 mch_memmove(s + prevlen, buf, buflen);
13132 vim_free(prev);
13133 prev = s;
13134 prevlen += buflen;
13135 }
13136 }
13137 filtd = 0;
13138 }
13139 else
13140 {
13141 mch_memmove(buf, buf + tolist, buflen - tolist);
13142 filtd -= tolist;
13143 }
13144 }
13145
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013146 /*
13147 * For a negative line count use only the lines at the end of the file,
13148 * free the rest.
13149 */
13150 if (maxline < 0)
13151 while (cnt > -maxline)
13152 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013153 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013154 --cnt;
13155 }
13156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013157 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013158 fclose(fd);
13159}
13160
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013161#if defined(FEAT_RELTIME)
13162static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13163
13164/*
13165 * Convert a List to proftime_T.
13166 * Return FAIL when there is something wrong.
13167 */
13168 static int
13169list2proftime(arg, tm)
13170 typval_T *arg;
13171 proftime_T *tm;
13172{
13173 long n1, n2;
13174 int error = FALSE;
13175
13176 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13177 || arg->vval.v_list->lv_len != 2)
13178 return FAIL;
13179 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13180 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13181# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013182 tm->HighPart = n1;
13183 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013184# else
13185 tm->tv_sec = n1;
13186 tm->tv_usec = n2;
13187# endif
13188 return error ? FAIL : OK;
13189}
13190#endif /* FEAT_RELTIME */
13191
13192/*
13193 * "reltime()" function
13194 */
13195 static void
13196f_reltime(argvars, rettv)
13197 typval_T *argvars;
13198 typval_T *rettv;
13199{
13200#ifdef FEAT_RELTIME
13201 proftime_T res;
13202 proftime_T start;
13203
13204 if (argvars[0].v_type == VAR_UNKNOWN)
13205 {
13206 /* No arguments: get current time. */
13207 profile_start(&res);
13208 }
13209 else if (argvars[1].v_type == VAR_UNKNOWN)
13210 {
13211 if (list2proftime(&argvars[0], &res) == FAIL)
13212 return;
13213 profile_end(&res);
13214 }
13215 else
13216 {
13217 /* Two arguments: compute the difference. */
13218 if (list2proftime(&argvars[0], &start) == FAIL
13219 || list2proftime(&argvars[1], &res) == FAIL)
13220 return;
13221 profile_sub(&res, &start);
13222 }
13223
13224 if (rettv_list_alloc(rettv) == OK)
13225 {
13226 long n1, n2;
13227
13228# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013229 n1 = res.HighPart;
13230 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013231# else
13232 n1 = res.tv_sec;
13233 n2 = res.tv_usec;
13234# endif
13235 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13236 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13237 }
13238#endif
13239}
13240
13241/*
13242 * "reltimestr()" function
13243 */
13244 static void
13245f_reltimestr(argvars, rettv)
13246 typval_T *argvars;
13247 typval_T *rettv;
13248{
13249#ifdef FEAT_RELTIME
13250 proftime_T tm;
13251#endif
13252
13253 rettv->v_type = VAR_STRING;
13254 rettv->vval.v_string = NULL;
13255#ifdef FEAT_RELTIME
13256 if (list2proftime(&argvars[0], &tm) == OK)
13257 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13258#endif
13259}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013260
Bram Moolenaar0d660222005-01-07 21:51:51 +000013261#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13262static void make_connection __ARGS((void));
13263static int check_connection __ARGS((void));
13264
13265 static void
13266make_connection()
13267{
13268 if (X_DISPLAY == NULL
13269# ifdef FEAT_GUI
13270 && !gui.in_use
13271# endif
13272 )
13273 {
13274 x_force_connect = TRUE;
13275 setup_term_clip();
13276 x_force_connect = FALSE;
13277 }
13278}
13279
13280 static int
13281check_connection()
13282{
13283 make_connection();
13284 if (X_DISPLAY == NULL)
13285 {
13286 EMSG(_("E240: No connection to Vim server"));
13287 return FAIL;
13288 }
13289 return OK;
13290}
13291#endif
13292
13293#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013294static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013295
13296 static void
13297remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 typval_T *argvars;
13299 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013300 int expr;
13301{
13302 char_u *server_name;
13303 char_u *keys;
13304 char_u *r = NULL;
13305 char_u buf[NUMBUFLEN];
13306# ifdef WIN32
13307 HWND w;
13308# else
13309 Window w;
13310# endif
13311
13312 if (check_restricted() || check_secure())
13313 return;
13314
13315# ifdef FEAT_X11
13316 if (check_connection() == FAIL)
13317 return;
13318# endif
13319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013320 server_name = get_tv_string_chk(&argvars[0]);
13321 if (server_name == NULL)
13322 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013323 keys = get_tv_string_buf(&argvars[1], buf);
13324# ifdef WIN32
13325 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13326# else
13327 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13328 < 0)
13329# endif
13330 {
13331 if (r != NULL)
13332 EMSG(r); /* sending worked but evaluation failed */
13333 else
13334 EMSG2(_("E241: Unable to send to %s"), server_name);
13335 return;
13336 }
13337
13338 rettv->vval.v_string = r;
13339
13340 if (argvars[2].v_type != VAR_UNKNOWN)
13341 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013342 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013343 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013344 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013345
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013346 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 v.di_tv.v_type = VAR_STRING;
13348 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013349 idvar = get_tv_string_chk(&argvars[2]);
13350 if (idvar != NULL)
13351 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013352 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013353 }
13354}
13355#endif
13356
13357/*
13358 * "remote_expr()" function
13359 */
13360/*ARGSUSED*/
13361 static void
13362f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 typval_T *argvars;
13364 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013365{
13366 rettv->v_type = VAR_STRING;
13367 rettv->vval.v_string = NULL;
13368#ifdef FEAT_CLIENTSERVER
13369 remote_common(argvars, rettv, TRUE);
13370#endif
13371}
13372
13373/*
13374 * "remote_foreground()" function
13375 */
13376/*ARGSUSED*/
13377 static void
13378f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 typval_T *argvars;
13380 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013381{
13382 rettv->vval.v_number = 0;
13383#ifdef FEAT_CLIENTSERVER
13384# ifdef WIN32
13385 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 {
13387 char_u *server_name = get_tv_string_chk(&argvars[0]);
13388
13389 if (server_name != NULL)
13390 serverForeground(server_name);
13391 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013392# else
13393 /* Send a foreground() expression to the server. */
13394 argvars[1].v_type = VAR_STRING;
13395 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13396 argvars[2].v_type = VAR_UNKNOWN;
13397 remote_common(argvars, rettv, TRUE);
13398 vim_free(argvars[1].vval.v_string);
13399# endif
13400#endif
13401}
13402
13403/*ARGSUSED*/
13404 static void
13405f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 typval_T *argvars;
13407 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013408{
13409#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013410 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013411 char_u *s = NULL;
13412# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013413 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013414# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013415 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013416
13417 if (check_restricted() || check_secure())
13418 {
13419 rettv->vval.v_number = -1;
13420 return;
13421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422 serverid = get_tv_string_chk(&argvars[0]);
13423 if (serverid == NULL)
13424 {
13425 rettv->vval.v_number = -1;
13426 return; /* type error; errmsg already given */
13427 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013428# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013429 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013430 if (n == 0)
13431 rettv->vval.v_number = -1;
13432 else
13433 {
13434 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13435 rettv->vval.v_number = (s != NULL);
13436 }
13437# else
13438 rettv->vval.v_number = 0;
13439 if (check_connection() == FAIL)
13440 return;
13441
13442 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013443 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013444# endif
13445
13446 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013448 char_u *retvar;
13449
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 v.di_tv.v_type = VAR_STRING;
13451 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013452 retvar = get_tv_string_chk(&argvars[1]);
13453 if (retvar != NULL)
13454 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013455 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013456 }
13457#else
13458 rettv->vval.v_number = -1;
13459#endif
13460}
13461
13462/*ARGSUSED*/
13463 static void
13464f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013465 typval_T *argvars;
13466 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013467{
13468 char_u *r = NULL;
13469
13470#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471 char_u *serverid = get_tv_string_chk(&argvars[0]);
13472
13473 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013474 {
13475# ifdef WIN32
13476 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013477 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013478
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013479 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013480 if (n != 0)
13481 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13482 if (r == NULL)
13483# else
13484 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013485 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013486# endif
13487 EMSG(_("E277: Unable to read a server reply"));
13488 }
13489#endif
13490 rettv->v_type = VAR_STRING;
13491 rettv->vval.v_string = r;
13492}
13493
13494/*
13495 * "remote_send()" function
13496 */
13497/*ARGSUSED*/
13498 static void
13499f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013500 typval_T *argvars;
13501 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013502{
13503 rettv->v_type = VAR_STRING;
13504 rettv->vval.v_string = NULL;
13505#ifdef FEAT_CLIENTSERVER
13506 remote_common(argvars, rettv, FALSE);
13507#endif
13508}
13509
13510/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013511 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013512 */
13513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013515 typval_T *argvars;
13516 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013517{
Bram Moolenaar33570922005-01-25 22:26:29 +000013518 list_T *l;
13519 listitem_T *item, *item2;
13520 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013521 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013522 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013523 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013524 dict_T *d;
13525 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013526
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013527 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013528 if (argvars[0].v_type == VAR_DICT)
13529 {
13530 if (argvars[2].v_type != VAR_UNKNOWN)
13531 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013532 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013533 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013535 key = get_tv_string_chk(&argvars[1]);
13536 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013538 di = dict_find(d, key, -1);
13539 if (di == NULL)
13540 EMSG2(_(e_dictkey), key);
13541 else
13542 {
13543 *rettv = di->di_tv;
13544 init_tv(&di->di_tv);
13545 dictitem_remove(d, di);
13546 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013547 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013548 }
13549 }
13550 else if (argvars[0].v_type != VAR_LIST)
13551 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013552 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013553 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013555 int error = FALSE;
13556
13557 idx = get_tv_number_chk(&argvars[1], &error);
13558 if (error)
13559 ; /* type error: do nothing, errmsg already given */
13560 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013561 EMSGN(_(e_listidx), idx);
13562 else
13563 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013564 if (argvars[2].v_type == VAR_UNKNOWN)
13565 {
13566 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013567 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013568 *rettv = item->li_tv;
13569 vim_free(item);
13570 }
13571 else
13572 {
13573 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 end = get_tv_number_chk(&argvars[2], &error);
13575 if (error)
13576 ; /* type error: do nothing */
13577 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013578 EMSGN(_(e_listidx), end);
13579 else
13580 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013581 int cnt = 0;
13582
13583 for (li = item; li != NULL; li = li->li_next)
13584 {
13585 ++cnt;
13586 if (li == item2)
13587 break;
13588 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013589 if (li == NULL) /* didn't find "item2" after "item" */
13590 EMSG(_(e_invrange));
13591 else
13592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013593 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013594 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013595 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013596 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013597 l->lv_first = item;
13598 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013599 item->li_prev = NULL;
13600 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013601 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013602 }
13603 }
13604 }
13605 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013606 }
13607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608}
13609
13610/*
13611 * "rename({from}, {to})" function
13612 */
13613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013615 typval_T *argvars;
13616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618 char_u buf[NUMBUFLEN];
13619
13620 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13624 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625}
13626
13627/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013628 * "repeat()" function
13629 */
13630/*ARGSUSED*/
13631 static void
13632f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013633 typval_T *argvars;
13634 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013635{
13636 char_u *p;
13637 int n;
13638 int slen;
13639 int len;
13640 char_u *r;
13641 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013642
13643 n = get_tv_number(&argvars[1]);
13644 if (argvars[0].v_type == VAR_LIST)
13645 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013646 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013647 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013648 if (list_extend(rettv->vval.v_list,
13649 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013650 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013651 }
13652 else
13653 {
13654 p = get_tv_string(&argvars[0]);
13655 rettv->v_type = VAR_STRING;
13656 rettv->vval.v_string = NULL;
13657
13658 slen = (int)STRLEN(p);
13659 len = slen * n;
13660 if (len <= 0)
13661 return;
13662
13663 r = alloc(len + 1);
13664 if (r != NULL)
13665 {
13666 for (i = 0; i < n; i++)
13667 mch_memmove(r + i * slen, p, (size_t)slen);
13668 r[len] = NUL;
13669 }
13670
13671 rettv->vval.v_string = r;
13672 }
13673}
13674
13675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 * "resolve()" function
13677 */
13678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *argvars;
13681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682{
13683 char_u *p;
13684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013685 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686#ifdef FEAT_SHORTCUT
13687 {
13688 char_u *v = NULL;
13689
13690 v = mch_resolve_shortcut(p);
13691 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 }
13696#else
13697# ifdef HAVE_READLINK
13698 {
13699 char_u buf[MAXPATHL + 1];
13700 char_u *cpy;
13701 int len;
13702 char_u *remain = NULL;
13703 char_u *q;
13704 int is_relative_to_current = FALSE;
13705 int has_trailing_pathsep = FALSE;
13706 int limit = 100;
13707
13708 p = vim_strsave(p);
13709
13710 if (p[0] == '.' && (vim_ispathsep(p[1])
13711 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13712 is_relative_to_current = TRUE;
13713
13714 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013715 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 has_trailing_pathsep = TRUE;
13717
13718 q = getnextcomp(p);
13719 if (*q != NUL)
13720 {
13721 /* Separate the first path component in "p", and keep the
13722 * remainder (beginning with the path separator). */
13723 remain = vim_strsave(q - 1);
13724 q[-1] = NUL;
13725 }
13726
13727 for (;;)
13728 {
13729 for (;;)
13730 {
13731 len = readlink((char *)p, (char *)buf, MAXPATHL);
13732 if (len <= 0)
13733 break;
13734 buf[len] = NUL;
13735
13736 if (limit-- == 0)
13737 {
13738 vim_free(p);
13739 vim_free(remain);
13740 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 goto fail;
13743 }
13744
13745 /* Ensure that the result will have a trailing path separator
13746 * if the argument has one. */
13747 if (remain == NULL && has_trailing_pathsep)
13748 add_pathsep(buf);
13749
13750 /* Separate the first path component in the link value and
13751 * concatenate the remainders. */
13752 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13753 if (*q != NUL)
13754 {
13755 if (remain == NULL)
13756 remain = vim_strsave(q - 1);
13757 else
13758 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013759 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 if (cpy != NULL)
13761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 vim_free(remain);
13763 remain = cpy;
13764 }
13765 }
13766 q[-1] = NUL;
13767 }
13768
13769 q = gettail(p);
13770 if (q > p && *q == NUL)
13771 {
13772 /* Ignore trailing path separator. */
13773 q[-1] = NUL;
13774 q = gettail(p);
13775 }
13776 if (q > p && !mch_isFullName(buf))
13777 {
13778 /* symlink is relative to directory of argument */
13779 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13780 if (cpy != NULL)
13781 {
13782 STRCPY(cpy, p);
13783 STRCPY(gettail(cpy), buf);
13784 vim_free(p);
13785 p = cpy;
13786 }
13787 }
13788 else
13789 {
13790 vim_free(p);
13791 p = vim_strsave(buf);
13792 }
13793 }
13794
13795 if (remain == NULL)
13796 break;
13797
13798 /* Append the first path component of "remain" to "p". */
13799 q = getnextcomp(remain + 1);
13800 len = q - remain - (*q != NUL);
13801 cpy = vim_strnsave(p, STRLEN(p) + len);
13802 if (cpy != NULL)
13803 {
13804 STRNCAT(cpy, remain, len);
13805 vim_free(p);
13806 p = cpy;
13807 }
13808 /* Shorten "remain". */
13809 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013810 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 else
13812 {
13813 vim_free(remain);
13814 remain = NULL;
13815 }
13816 }
13817
13818 /* If the result is a relative path name, make it explicitly relative to
13819 * the current directory if and only if the argument had this form. */
13820 if (!vim_ispathsep(*p))
13821 {
13822 if (is_relative_to_current
13823 && *p != NUL
13824 && !(p[0] == '.'
13825 && (p[1] == NUL
13826 || vim_ispathsep(p[1])
13827 || (p[1] == '.'
13828 && (p[2] == NUL
13829 || vim_ispathsep(p[2]))))))
13830 {
13831 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013832 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 if (cpy != NULL)
13834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 vim_free(p);
13836 p = cpy;
13837 }
13838 }
13839 else if (!is_relative_to_current)
13840 {
13841 /* Strip leading "./". */
13842 q = p;
13843 while (q[0] == '.' && vim_ispathsep(q[1]))
13844 q += 2;
13845 if (q > p)
13846 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13847 }
13848 }
13849
13850 /* Ensure that the result will have no trailing path separator
13851 * if the argument had none. But keep "/" or "//". */
13852 if (!has_trailing_pathsep)
13853 {
13854 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013855 if (after_pathsep(p, q))
13856 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857 }
13858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013859 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 }
13861# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013862 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863# endif
13864#endif
13865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013866 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867
13868#ifdef HAVE_READLINK
13869fail:
13870#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013871 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872}
13873
13874/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013875 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876 */
13877 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013878f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013879 typval_T *argvars;
13880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881{
Bram Moolenaar33570922005-01-25 22:26:29 +000013882 list_T *l;
13883 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884
Bram Moolenaar0d660222005-01-07 21:51:51 +000013885 rettv->vval.v_number = 0;
13886 if (argvars[0].v_type != VAR_LIST)
13887 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013888 else if ((l = argvars[0].vval.v_list) != NULL
13889 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013890 {
13891 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013892 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013893 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013894 while (li != NULL)
13895 {
13896 ni = li->li_prev;
13897 list_append(l, li);
13898 li = ni;
13899 }
13900 rettv->vval.v_list = l;
13901 rettv->v_type = VAR_LIST;
13902 ++l->lv_refcount;
13903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904}
13905
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013906#define SP_NOMOVE 0x01 /* don't move cursor */
13907#define SP_REPEAT 0x02 /* repeat to find outer pair */
13908#define SP_RETCOUNT 0x04 /* return matchcount */
13909#define SP_SETPCMARK 0x08 /* set previous context mark */
13910#define SP_START 0x10 /* accept match at start position */
13911#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13912#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013913
Bram Moolenaar33570922005-01-25 22:26:29 +000013914static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013915
13916/*
13917 * Get flags for a search function.
13918 * Possibly sets "p_ws".
13919 * Returns BACKWARD, FORWARD or zero (for an error).
13920 */
13921 static int
13922get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013923 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013924 int *flagsp;
13925{
13926 int dir = FORWARD;
13927 char_u *flags;
13928 char_u nbuf[NUMBUFLEN];
13929 int mask;
13930
13931 if (varp->v_type != VAR_UNKNOWN)
13932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013933 flags = get_tv_string_buf_chk(varp, nbuf);
13934 if (flags == NULL)
13935 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013936 while (*flags != NUL)
13937 {
13938 switch (*flags)
13939 {
13940 case 'b': dir = BACKWARD; break;
13941 case 'w': p_ws = TRUE; break;
13942 case 'W': p_ws = FALSE; break;
13943 default: mask = 0;
13944 if (flagsp != NULL)
13945 switch (*flags)
13946 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013947 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013948 case 'e': mask = SP_END; break;
13949 case 'm': mask = SP_RETCOUNT; break;
13950 case 'n': mask = SP_NOMOVE; break;
13951 case 'p': mask = SP_SUBPAT; break;
13952 case 'r': mask = SP_REPEAT; break;
13953 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013954 }
13955 if (mask == 0)
13956 {
13957 EMSG2(_(e_invarg2), flags);
13958 dir = 0;
13959 }
13960 else
13961 *flagsp |= mask;
13962 }
13963 if (dir == 0)
13964 break;
13965 ++flags;
13966 }
13967 }
13968 return dir;
13969}
13970
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013972 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013974 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013975search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013976 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013977 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013978 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013980 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 char_u *pat;
13982 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013983 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 int save_p_ws = p_ws;
13985 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013986 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013987 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013988 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013989 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013992 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013993 if (dir == 0)
13994 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013995 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013996 if (flags & SP_START)
13997 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013998 if (flags & SP_END)
13999 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014000
14001 /* Optional extra argument: line number to stop searching. */
14002 if (argvars[1].v_type != VAR_UNKNOWN
14003 && argvars[2].v_type != VAR_UNKNOWN)
14004 {
14005 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14006 if (lnum_stop < 0)
14007 goto theend;
14008 }
14009
Bram Moolenaar231334e2005-07-25 20:46:57 +000014010 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014011 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014012 * Check to make sure only those flags are set.
14013 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14014 * flags cannot be set. Check for that condition also.
14015 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014016 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014017 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014020 goto theend;
14021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014023 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014024 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
14025 options, RE_SEARCH, (linenr_T)lnum_stop);
14026 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014028 if (flags & SP_SUBPAT)
14029 retval = subpatnum;
14030 else
14031 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014032 if (flags & SP_SETPCMARK)
14033 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014035 if (match_pos != NULL)
14036 {
14037 /* Store the match cursor position */
14038 match_pos->lnum = pos.lnum;
14039 match_pos->col = pos.col + 1;
14040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 /* "/$" will put the cursor after the end of the line, may need to
14042 * correct that here */
14043 check_cursor();
14044 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014045
14046 /* If 'n' flag is used: restore cursor position. */
14047 if (flags & SP_NOMOVE)
14048 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014049 else
14050 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014051theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014053
14054 return retval;
14055}
14056
14057/*
14058 * "search()" function
14059 */
14060 static void
14061f_search(argvars, rettv)
14062 typval_T *argvars;
14063 typval_T *rettv;
14064{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014065 int flags = 0;
14066
14067 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068}
14069
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014071 * "searchdecl()" function
14072 */
14073 static void
14074f_searchdecl(argvars, rettv)
14075 typval_T *argvars;
14076 typval_T *rettv;
14077{
14078 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014079 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014080 int error = FALSE;
14081 char_u *name;
14082
14083 rettv->vval.v_number = 1; /* default: FAIL */
14084
14085 name = get_tv_string_chk(&argvars[0]);
14086 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014087 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014088 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014089 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14090 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14091 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014092 if (!error && name != NULL)
14093 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014094 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014095}
14096
14097/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014098 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014100 static int
14101searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014102 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014103 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104{
14105 char_u *spat, *mpat, *epat;
14106 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108 int dir;
14109 int flags = 0;
14110 char_u nbuf1[NUMBUFLEN];
14111 char_u nbuf2[NUMBUFLEN];
14112 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014113 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014114 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014117 spat = get_tv_string_chk(&argvars[0]);
14118 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14119 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14120 if (spat == NULL || mpat == NULL || epat == NULL)
14121 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 /* Handle the optional fourth argument: flags */
14124 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014125 if (dir == 0)
14126 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014127
14128 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014129 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14130 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014131 if ((flags & (SP_END | SP_SUBPAT)) != 0
14132 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014133 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014134 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014135 goto theend;
14136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014138 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014139 if (argvars[3].v_type == VAR_UNKNOWN
14140 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 skip = (char_u *)"";
14142 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014145 if (argvars[5].v_type != VAR_UNKNOWN)
14146 {
14147 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14148 if (lnum_stop < 0)
14149 goto theend;
14150 }
14151 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 if (skip == NULL)
14153 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014155 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14156 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014157
14158theend:
14159 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014160
14161 return retval;
14162}
14163
14164/*
14165 * "searchpair()" function
14166 */
14167 static void
14168f_searchpair(argvars, rettv)
14169 typval_T *argvars;
14170 typval_T *rettv;
14171{
14172 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14173}
14174
14175/*
14176 * "searchpairpos()" function
14177 */
14178 static void
14179f_searchpairpos(argvars, rettv)
14180 typval_T *argvars;
14181 typval_T *rettv;
14182{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014183 pos_T match_pos;
14184 int lnum = 0;
14185 int col = 0;
14186
14187 rettv->vval.v_number = 0;
14188
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014189 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014190 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014191
14192 if (searchpair_cmn(argvars, &match_pos) > 0)
14193 {
14194 lnum = match_pos.lnum;
14195 col = match_pos.col;
14196 }
14197
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014198 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14199 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014200}
14201
14202/*
14203 * Search for a start/middle/end thing.
14204 * Used by searchpair(), see its documentation for the details.
14205 * Returns 0 or -1 for no match,
14206 */
14207 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014208do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014209 char_u *spat; /* start pattern */
14210 char_u *mpat; /* middle pattern */
14211 char_u *epat; /* end pattern */
14212 int dir; /* BACKWARD or FORWARD */
14213 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014214 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014215 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014216 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014217{
14218 char_u *save_cpo;
14219 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14220 long retval = 0;
14221 pos_T pos;
14222 pos_T firstpos;
14223 pos_T foundpos;
14224 pos_T save_cursor;
14225 pos_T save_pos;
14226 int n;
14227 int r;
14228 int nest = 1;
14229 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014230 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014231
14232 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14233 save_cpo = p_cpo;
14234 p_cpo = (char_u *)"";
14235
14236 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14237 * start/middle/end (pat3, for the top pair). */
14238 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14239 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14240 if (pat2 == NULL || pat3 == NULL)
14241 goto theend;
14242 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14243 if (*mpat == NUL)
14244 STRCPY(pat3, pat2);
14245 else
14246 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14247 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014248 if (flags & SP_START)
14249 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014250
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 save_cursor = curwin->w_cursor;
14252 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014253 clearpos(&firstpos);
14254 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 pat = pat3;
14256 for (;;)
14257 {
14258 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014259 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14261 /* didn't find it or found the first match again: FAIL */
14262 break;
14263
14264 if (firstpos.lnum == 0)
14265 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014266 if (equalpos(pos, foundpos))
14267 {
14268 /* Found the same position again. Can happen with a pattern that
14269 * has "\zs" at the end and searching backwards. Advance one
14270 * character and try again. */
14271 if (dir == BACKWARD)
14272 decl(&pos);
14273 else
14274 incl(&pos);
14275 }
14276 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277
14278 /* If the skip pattern matches, ignore this match. */
14279 if (*skip != NUL)
14280 {
14281 save_pos = curwin->w_cursor;
14282 curwin->w_cursor = pos;
14283 r = eval_to_bool(skip, &err, NULL, FALSE);
14284 curwin->w_cursor = save_pos;
14285 if (err)
14286 {
14287 /* Evaluating {skip} caused an error, break here. */
14288 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014289 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 break;
14291 }
14292 if (r)
14293 continue;
14294 }
14295
14296 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14297 {
14298 /* Found end when searching backwards or start when searching
14299 * forward: nested pair. */
14300 ++nest;
14301 pat = pat2; /* nested, don't search for middle */
14302 }
14303 else
14304 {
14305 /* Found end when searching forward or start when searching
14306 * backward: end of (nested) pair; or found middle in outer pair. */
14307 if (--nest == 1)
14308 pat = pat3; /* outer level, search for middle */
14309 }
14310
14311 if (nest == 0)
14312 {
14313 /* Found the match: return matchcount or line number. */
14314 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014315 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014317 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014318 if (flags & SP_SETPCMARK)
14319 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 curwin->w_cursor = pos;
14321 if (!(flags & SP_REPEAT))
14322 break;
14323 nest = 1; /* search for next unmatched */
14324 }
14325 }
14326
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014327 if (match_pos != NULL)
14328 {
14329 /* Store the match cursor position */
14330 match_pos->lnum = curwin->w_cursor.lnum;
14331 match_pos->col = curwin->w_cursor.col + 1;
14332 }
14333
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014335 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 curwin->w_cursor = save_cursor;
14337
14338theend:
14339 vim_free(pat2);
14340 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014342
14343 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344}
14345
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014346/*
14347 * "searchpos()" function
14348 */
14349 static void
14350f_searchpos(argvars, rettv)
14351 typval_T *argvars;
14352 typval_T *rettv;
14353{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014354 pos_T match_pos;
14355 int lnum = 0;
14356 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014357 int n;
14358 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014359
14360 rettv->vval.v_number = 0;
14361
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014362 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014363 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014364
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014365 n = search_cmn(argvars, &match_pos, &flags);
14366 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014367 {
14368 lnum = match_pos.lnum;
14369 col = match_pos.col;
14370 }
14371
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014372 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14373 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014374 if (flags & SP_SUBPAT)
14375 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014376}
14377
14378
Bram Moolenaar0d660222005-01-07 21:51:51 +000014379/*ARGSUSED*/
14380 static void
14381f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 typval_T *argvars;
14383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385#ifdef FEAT_CLIENTSERVER
14386 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014387 char_u *server = get_tv_string_chk(&argvars[0]);
14388 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389
Bram Moolenaar0d660222005-01-07 21:51:51 +000014390 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014391 if (server == NULL || reply == NULL)
14392 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014393 if (check_restricted() || check_secure())
14394 return;
14395# ifdef FEAT_X11
14396 if (check_connection() == FAIL)
14397 return;
14398# endif
14399
14400 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014402 EMSG(_("E258: Unable to send to client"));
14403 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014405 rettv->vval.v_number = 0;
14406#else
14407 rettv->vval.v_number = -1;
14408#endif
14409}
14410
14411/*ARGSUSED*/
14412 static void
14413f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014414 typval_T *argvars;
14415 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014416{
14417 char_u *r = NULL;
14418
14419#ifdef FEAT_CLIENTSERVER
14420# ifdef WIN32
14421 r = serverGetVimNames();
14422# else
14423 make_connection();
14424 if (X_DISPLAY != NULL)
14425 r = serverGetVimNames(X_DISPLAY);
14426# endif
14427#endif
14428 rettv->v_type = VAR_STRING;
14429 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430}
14431
14432/*
14433 * "setbufvar()" function
14434 */
14435/*ARGSUSED*/
14436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014438 typval_T *argvars;
14439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440{
14441 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014444 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 char_u nbuf[NUMBUFLEN];
14446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014447 rettv->vval.v_number = 0;
14448
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 if (check_restricted() || check_secure())
14450 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14452 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 varp = &argvars[2];
14455
14456 if (buf != NULL && varname != NULL && varp != NULL)
14457 {
14458 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460
14461 if (*varname == '&')
14462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014463 long numval;
14464 char_u *strval;
14465 int error = FALSE;
14466
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 numval = get_tv_number_chk(varp, &error);
14469 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014470 if (!error && strval != NULL)
14471 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 }
14473 else
14474 {
14475 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14476 if (bufvarname != NULL)
14477 {
14478 STRCPY(bufvarname, "b:");
14479 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014480 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 vim_free(bufvarname);
14482 }
14483 }
14484
14485 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488}
14489
14490/*
14491 * "setcmdpos()" function
14492 */
14493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014495 typval_T *argvars;
14496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014498 int pos = (int)get_tv_number(&argvars[0]) - 1;
14499
14500 if (pos >= 0)
14501 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502}
14503
14504/*
14505 * "setline()" function
14506 */
14507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014509 typval_T *argvars;
14510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511{
14512 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014513 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014514 list_T *l = NULL;
14515 listitem_T *li = NULL;
14516 long added = 0;
14517 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014519 lnum = get_tv_lnum(&argvars[0]);
14520 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014522 l = argvars[1].vval.v_list;
14523 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014525 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014527
14528 rettv->vval.v_number = 0; /* OK */
14529 for (;;)
14530 {
14531 if (l != NULL)
14532 {
14533 /* list argument, get next string */
14534 if (li == NULL)
14535 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014536 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014537 li = li->li_next;
14538 }
14539
14540 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014541 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014542 break;
14543 if (lnum <= curbuf->b_ml.ml_line_count)
14544 {
14545 /* existing line, replace it */
14546 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14547 {
14548 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014549 if (lnum == curwin->w_cursor.lnum)
14550 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014551 rettv->vval.v_number = 0; /* OK */
14552 }
14553 }
14554 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14555 {
14556 /* lnum is one past the last line, append the line */
14557 ++added;
14558 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14559 rettv->vval.v_number = 0; /* OK */
14560 }
14561
14562 if (l == NULL) /* only one string argument */
14563 break;
14564 ++lnum;
14565 }
14566
14567 if (added > 0)
14568 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569}
14570
14571/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014572 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014573 */
14574/*ARGSUSED*/
14575 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014576set_qf_ll_list(wp, list_arg, action_arg, rettv)
14577 win_T *wp;
14578 typval_T *list_arg;
14579 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014580 typval_T *rettv;
14581{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014582#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014583 char_u *act;
14584 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014585#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014586
Bram Moolenaar2641f772005-03-25 21:58:17 +000014587 rettv->vval.v_number = -1;
14588
14589#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014590 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014591 EMSG(_(e_listreq));
14592 else
14593 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014594 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014595
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014596 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014597 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014598 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599 if (act == NULL)
14600 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014601 if (*act == 'a' || *act == 'r')
14602 action = *act;
14603 }
14604
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014605 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014606 rettv->vval.v_number = 0;
14607 }
14608#endif
14609}
14610
14611/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014612 * "setloclist()" function
14613 */
14614/*ARGSUSED*/
14615 static void
14616f_setloclist(argvars, rettv)
14617 typval_T *argvars;
14618 typval_T *rettv;
14619{
14620 win_T *win;
14621
14622 rettv->vval.v_number = -1;
14623
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014624 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014625 if (win != NULL)
14626 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14627}
14628
14629/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014630 * "setmatches()" function
14631 */
14632 static void
14633f_setmatches(argvars, rettv)
14634 typval_T *argvars;
14635 typval_T *rettv;
14636{
14637#ifdef FEAT_SEARCH_EXTRA
14638 list_T *l;
14639 listitem_T *li;
14640 dict_T *d;
14641
14642 rettv->vval.v_number = -1;
14643 if (argvars[0].v_type != VAR_LIST)
14644 {
14645 EMSG(_(e_listreq));
14646 return;
14647 }
14648 if ((l = argvars[0].vval.v_list) != NULL)
14649 {
14650
14651 /* To some extent make sure that we are dealing with a list from
14652 * "getmatches()". */
14653 li = l->lv_first;
14654 while (li != NULL)
14655 {
14656 if (li->li_tv.v_type != VAR_DICT
14657 || (d = li->li_tv.vval.v_dict) == NULL)
14658 {
14659 EMSG(_(e_invarg));
14660 return;
14661 }
14662 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14663 && dict_find(d, (char_u *)"pattern", -1) != NULL
14664 && dict_find(d, (char_u *)"priority", -1) != NULL
14665 && dict_find(d, (char_u *)"id", -1) != NULL))
14666 {
14667 EMSG(_(e_invarg));
14668 return;
14669 }
14670 li = li->li_next;
14671 }
14672
14673 clear_matches(curwin);
14674 li = l->lv_first;
14675 while (li != NULL)
14676 {
14677 d = li->li_tv.vval.v_dict;
14678 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14679 get_dict_string(d, (char_u *)"pattern", FALSE),
14680 (int)get_dict_number(d, (char_u *)"priority"),
14681 (int)get_dict_number(d, (char_u *)"id"));
14682 li = li->li_next;
14683 }
14684 rettv->vval.v_number = 0;
14685 }
14686#endif
14687}
14688
14689/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014690 * "setpos()" function
14691 */
14692/*ARGSUSED*/
14693 static void
14694f_setpos(argvars, rettv)
14695 typval_T *argvars;
14696 typval_T *rettv;
14697{
14698 pos_T pos;
14699 int fnum;
14700 char_u *name;
14701
14702 name = get_tv_string_chk(argvars);
14703 if (name != NULL)
14704 {
14705 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14706 {
14707 --pos.col;
14708 if (name[0] == '.') /* cursor */
14709 {
14710 if (fnum == curbuf->b_fnum)
14711 {
14712 curwin->w_cursor = pos;
14713 check_cursor();
14714 }
14715 else
14716 EMSG(_(e_invarg));
14717 }
14718 else if (name[0] == '\'') /* mark */
14719 (void)setmark_pos(name[1], &pos, fnum);
14720 else
14721 EMSG(_(e_invarg));
14722 }
14723 }
14724}
14725
14726/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014727 * "setqflist()" function
14728 */
14729/*ARGSUSED*/
14730 static void
14731f_setqflist(argvars, rettv)
14732 typval_T *argvars;
14733 typval_T *rettv;
14734{
14735 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14736}
14737
14738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739 * "setreg()" function
14740 */
14741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014742f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014743 typval_T *argvars;
14744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745{
14746 int regname;
14747 char_u *strregname;
14748 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014749 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 int append;
14751 char_u yank_type;
14752 long block_len;
14753
14754 block_len = -1;
14755 yank_type = MAUTO;
14756 append = FALSE;
14757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014758 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014759 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014761 if (strregname == NULL)
14762 return; /* type error; errmsg already given */
14763 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 if (regname == 0 || regname == '@')
14765 regname = '"';
14766 else if (regname == '=')
14767 return;
14768
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014769 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014771 stropt = get_tv_string_chk(&argvars[2]);
14772 if (stropt == NULL)
14773 return; /* type error */
14774 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775 switch (*stropt)
14776 {
14777 case 'a': case 'A': /* append */
14778 append = TRUE;
14779 break;
14780 case 'v': case 'c': /* character-wise selection */
14781 yank_type = MCHAR;
14782 break;
14783 case 'V': case 'l': /* line-wise selection */
14784 yank_type = MLINE;
14785 break;
14786#ifdef FEAT_VISUAL
14787 case 'b': case Ctrl_V: /* block-wise selection */
14788 yank_type = MBLOCK;
14789 if (VIM_ISDIGIT(stropt[1]))
14790 {
14791 ++stropt;
14792 block_len = getdigits(&stropt) - 1;
14793 --stropt;
14794 }
14795 break;
14796#endif
14797 }
14798 }
14799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014800 strval = get_tv_string_chk(&argvars[1]);
14801 if (strval != NULL)
14802 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014804 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805}
14806
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014807/*
14808 * "settabwinvar()" function
14809 */
14810 static void
14811f_settabwinvar(argvars, rettv)
14812 typval_T *argvars;
14813 typval_T *rettv;
14814{
14815 setwinvar(argvars, rettv, 1);
14816}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817
14818/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014819 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014823 typval_T *argvars;
14824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014826 setwinvar(argvars, rettv, 0);
14827}
14828
14829/*
14830 * "setwinvar()" and "settabwinvar()" functions
14831 */
14832 static void
14833setwinvar(argvars, rettv, off)
14834 typval_T *argvars;
14835 typval_T *rettv;
14836 int off;
14837{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 win_T *win;
14839#ifdef FEAT_WINDOWS
14840 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014841 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842#endif
14843 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014844 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014846 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014848 rettv->vval.v_number = 0;
14849
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850 if (check_restricted() || check_secure())
14851 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014852
14853#ifdef FEAT_WINDOWS
14854 if (off == 1)
14855 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14856 else
14857 tp = curtab;
14858#endif
14859 win = find_win_by_nr(&argvars[off], tp);
14860 varname = get_tv_string_chk(&argvars[off + 1]);
14861 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862
14863 if (win != NULL && varname != NULL && varp != NULL)
14864 {
14865#ifdef FEAT_WINDOWS
14866 /* set curwin to be our win, temporarily */
14867 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014868 save_curtab = curtab;
14869 goto_tabpage_tp(tp);
14870 if (!win_valid(win))
14871 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 curwin = win;
14873 curbuf = curwin->w_buffer;
14874#endif
14875
14876 if (*varname == '&')
14877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014878 long numval;
14879 char_u *strval;
14880 int error = FALSE;
14881
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014883 numval = get_tv_number_chk(varp, &error);
14884 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014885 if (!error && strval != NULL)
14886 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 }
14888 else
14889 {
14890 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14891 if (winvarname != NULL)
14892 {
14893 STRCPY(winvarname, "w:");
14894 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014895 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896 vim_free(winvarname);
14897 }
14898 }
14899
14900#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014901 /* Restore current tabpage and window, if still valid (autocomands can
14902 * make them invalid). */
14903 if (valid_tabpage(save_curtab))
14904 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 if (win_valid(save_curwin))
14906 {
14907 curwin = save_curwin;
14908 curbuf = curwin->w_buffer;
14909 }
14910#endif
14911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912}
14913
14914/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014915 * "shellescape({string})" function
14916 */
14917 static void
14918f_shellescape(argvars, rettv)
14919 typval_T *argvars;
14920 typval_T *rettv;
14921{
14922 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14923 rettv->v_type = VAR_STRING;
14924}
14925
14926/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014927 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 */
14929 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014930f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014931 typval_T *argvars;
14932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014934 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935
Bram Moolenaar0d660222005-01-07 21:51:51 +000014936 p = get_tv_string(&argvars[0]);
14937 rettv->vval.v_string = vim_strsave(p);
14938 simplify_filename(rettv->vval.v_string); /* simplify in place */
14939 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940}
14941
Bram Moolenaar0d660222005-01-07 21:51:51 +000014942static int
14943#ifdef __BORLANDC__
14944 _RTLENTRYF
14945#endif
14946 item_compare __ARGS((const void *s1, const void *s2));
14947static int
14948#ifdef __BORLANDC__
14949 _RTLENTRYF
14950#endif
14951 item_compare2 __ARGS((const void *s1, const void *s2));
14952
14953static int item_compare_ic;
14954static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014955static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014956#define ITEM_COMPARE_FAIL 999
14957
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014961 static int
14962#ifdef __BORLANDC__
14963_RTLENTRYF
14964#endif
14965item_compare(s1, s2)
14966 const void *s1;
14967 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014969 char_u *p1, *p2;
14970 char_u *tofree1, *tofree2;
14971 int res;
14972 char_u numbuf1[NUMBUFLEN];
14973 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014975 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14976 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977 if (item_compare_ic)
14978 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980 res = STRCMP(p1, p2);
14981 vim_free(tofree1);
14982 vim_free(tofree2);
14983 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984}
14985
14986 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014987#ifdef __BORLANDC__
14988_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014990item_compare2(s1, s2)
14991 const void *s1;
14992 const void *s2;
14993{
14994 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014995 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014996 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014999 /* shortcut after failure in previous call; compare all items equal */
15000 if (item_compare_func_err)
15001 return 0;
15002
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015003 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15004 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015005 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15006 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007
15008 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015009 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015010 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011 clear_tv(&argv[0]);
15012 clear_tv(&argv[1]);
15013
15014 if (res == FAIL)
15015 res = ITEM_COMPARE_FAIL;
15016 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015017 /* return value has wrong type */
15018 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15019 if (item_compare_func_err)
15020 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015021 clear_tv(&rettv);
15022 return res;
15023}
15024
15025/*
15026 * "sort({list})" function
15027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015029f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015030 typval_T *argvars;
15031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032{
Bram Moolenaar33570922005-01-25 22:26:29 +000015033 list_T *l;
15034 listitem_T *li;
15035 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036 long len;
15037 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038
Bram Moolenaar0d660222005-01-07 21:51:51 +000015039 rettv->vval.v_number = 0;
15040 if (argvars[0].v_type != VAR_LIST)
15041 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042 else
15043 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015044 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015045 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046 return;
15047 rettv->vval.v_list = l;
15048 rettv->v_type = VAR_LIST;
15049 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051 len = list_len(l);
15052 if (len <= 1)
15053 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 item_compare_ic = FALSE;
15056 item_compare_func = NULL;
15057 if (argvars[1].v_type != VAR_UNKNOWN)
15058 {
15059 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015060 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061 else
15062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015063 int error = FALSE;
15064
15065 i = get_tv_number_chk(&argvars[1], &error);
15066 if (error)
15067 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 if (i == 1)
15069 item_compare_ic = TRUE;
15070 else
15071 item_compare_func = get_tv_string(&argvars[1]);
15072 }
15073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015076 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 if (ptrs == NULL)
15078 return;
15079 i = 0;
15080 for (li = l->lv_first; li != NULL; li = li->li_next)
15081 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015083 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 /* test the compare function */
15085 if (item_compare_func != NULL
15086 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15087 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015088 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090 {
15091 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015092 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 item_compare_func == NULL ? item_compare : item_compare2);
15094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015095 if (!item_compare_func_err)
15096 {
15097 /* Clear the List and append the items in the sorted order. */
15098 l->lv_first = l->lv_last = NULL;
15099 l->lv_len = 0;
15100 for (i = 0; i < len; ++i)
15101 list_append(l, ptrs[i]);
15102 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015103 }
15104
15105 vim_free(ptrs);
15106 }
15107}
15108
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015109/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015110 * "soundfold({word})" function
15111 */
15112 static void
15113f_soundfold(argvars, rettv)
15114 typval_T *argvars;
15115 typval_T *rettv;
15116{
15117 char_u *s;
15118
15119 rettv->v_type = VAR_STRING;
15120 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015121#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015122 rettv->vval.v_string = eval_soundfold(s);
15123#else
15124 rettv->vval.v_string = vim_strsave(s);
15125#endif
15126}
15127
15128/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015129 * "spellbadword()" function
15130 */
15131/* ARGSUSED */
15132 static void
15133f_spellbadword(argvars, rettv)
15134 typval_T *argvars;
15135 typval_T *rettv;
15136{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015137 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015138 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015139 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015141 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015142 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015143
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015144#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015145 if (argvars[0].v_type == VAR_UNKNOWN)
15146 {
15147 /* Find the start and length of the badly spelled word. */
15148 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15149 if (len != 0)
15150 word = ml_get_cursor();
15151 }
15152 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15153 {
15154 char_u *str = get_tv_string_chk(&argvars[0]);
15155 int capcol = -1;
15156
15157 if (str != NULL)
15158 {
15159 /* Check the argument for spelling. */
15160 while (*str != NUL)
15161 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015162 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015163 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015164 {
15165 word = str;
15166 break;
15167 }
15168 str += len;
15169 }
15170 }
15171 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015172#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015173
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015174 list_append_string(rettv->vval.v_list, word, len);
15175 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015176 attr == HLF_SPB ? "bad" :
15177 attr == HLF_SPR ? "rare" :
15178 attr == HLF_SPL ? "local" :
15179 attr == HLF_SPC ? "caps" :
15180 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015181}
15182
15183/*
15184 * "spellsuggest()" function
15185 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015186/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015187 static void
15188f_spellsuggest(argvars, rettv)
15189 typval_T *argvars;
15190 typval_T *rettv;
15191{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015192#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015193 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015194 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015195 int maxcount;
15196 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015197 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015198 listitem_T *li;
15199 int need_capital = FALSE;
15200#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015201
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015202 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015203 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015204
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015205#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015206 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15207 {
15208 str = get_tv_string(&argvars[0]);
15209 if (argvars[1].v_type != VAR_UNKNOWN)
15210 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015211 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015212 if (maxcount <= 0)
15213 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015214 if (argvars[2].v_type != VAR_UNKNOWN)
15215 {
15216 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15217 if (typeerr)
15218 return;
15219 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015220 }
15221 else
15222 maxcount = 25;
15223
Bram Moolenaar4770d092006-01-12 23:22:24 +000015224 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015225
15226 for (i = 0; i < ga.ga_len; ++i)
15227 {
15228 str = ((char_u **)ga.ga_data)[i];
15229
15230 li = listitem_alloc();
15231 if (li == NULL)
15232 vim_free(str);
15233 else
15234 {
15235 li->li_tv.v_type = VAR_STRING;
15236 li->li_tv.v_lock = 0;
15237 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015238 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015239 }
15240 }
15241 ga_clear(&ga);
15242 }
15243#endif
15244}
15245
Bram Moolenaar0d660222005-01-07 21:51:51 +000015246 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015247f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 typval_T *argvars;
15249 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250{
15251 char_u *str;
15252 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015253 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015254 regmatch_T regmatch;
15255 char_u patbuf[NUMBUFLEN];
15256 char_u *save_cpo;
15257 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015258 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015259 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015260 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015261
15262 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15263 save_cpo = p_cpo;
15264 p_cpo = (char_u *)"";
15265
15266 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015267 if (argvars[1].v_type != VAR_UNKNOWN)
15268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015269 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15270 if (pat == NULL)
15271 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015272 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015273 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015274 }
15275 if (pat == NULL || *pat == NUL)
15276 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015277
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015278 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015280 if (typeerr)
15281 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282
Bram Moolenaar0d660222005-01-07 21:51:51 +000015283 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15284 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015287 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015288 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015289 if (*str == NUL)
15290 match = FALSE; /* empty item at the end */
15291 else
15292 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015293 if (match)
15294 end = regmatch.startp[0];
15295 else
15296 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015297 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15298 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015299 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015300 if (list_append_string(rettv->vval.v_list, str,
15301 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015302 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015303 }
15304 if (!match)
15305 break;
15306 /* Advance to just after the match. */
15307 if (regmatch.endp[0] > str)
15308 col = 0;
15309 else
15310 {
15311 /* Don't get stuck at the same match. */
15312#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015313 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015314#else
15315 col = 1;
15316#endif
15317 }
15318 str = regmatch.endp[0];
15319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320
Bram Moolenaar0d660222005-01-07 21:51:51 +000015321 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323
Bram Moolenaar0d660222005-01-07 21:51:51 +000015324 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325}
15326
Bram Moolenaar2c932302006-03-18 21:42:09 +000015327/*
15328 * "str2nr()" function
15329 */
15330 static void
15331f_str2nr(argvars, rettv)
15332 typval_T *argvars;
15333 typval_T *rettv;
15334{
15335 int base = 10;
15336 char_u *p;
15337 long n;
15338
15339 if (argvars[1].v_type != VAR_UNKNOWN)
15340 {
15341 base = get_tv_number(&argvars[1]);
15342 if (base != 8 && base != 10 && base != 16)
15343 {
15344 EMSG(_(e_invarg));
15345 return;
15346 }
15347 }
15348
15349 p = skipwhite(get_tv_string(&argvars[0]));
15350 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15351 rettv->vval.v_number = n;
15352}
15353
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354#ifdef HAVE_STRFTIME
15355/*
15356 * "strftime({format}[, {time}])" function
15357 */
15358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015359f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015360 typval_T *argvars;
15361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362{
15363 char_u result_buf[256];
15364 struct tm *curtime;
15365 time_t seconds;
15366 char_u *p;
15367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015368 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015371 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372 seconds = time(NULL);
15373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 curtime = localtime(&seconds);
15376 /* MSVC returns NULL for an invalid value of seconds. */
15377 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015378 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 else
15380 {
15381# ifdef FEAT_MBYTE
15382 vimconv_T conv;
15383 char_u *enc;
15384
15385 conv.vc_type = CONV_NONE;
15386 enc = enc_locale();
15387 convert_setup(&conv, p_enc, enc);
15388 if (conv.vc_type != CONV_NONE)
15389 p = string_convert(&conv, p, NULL);
15390# endif
15391 if (p != NULL)
15392 (void)strftime((char *)result_buf, sizeof(result_buf),
15393 (char *)p, curtime);
15394 else
15395 result_buf[0] = NUL;
15396
15397# ifdef FEAT_MBYTE
15398 if (conv.vc_type != CONV_NONE)
15399 vim_free(p);
15400 convert_setup(&conv, enc, p_enc);
15401 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015402 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 else
15404# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406
15407# ifdef FEAT_MBYTE
15408 /* Release conversion descriptors */
15409 convert_setup(&conv, NULL, NULL);
15410 vim_free(enc);
15411# endif
15412 }
15413}
15414#endif
15415
15416/*
15417 * "stridx()" function
15418 */
15419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 typval_T *argvars;
15422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423{
15424 char_u buf[NUMBUFLEN];
15425 char_u *needle;
15426 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015427 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015429 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015431 needle = get_tv_string_chk(&argvars[1]);
15432 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015433 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015434 if (needle == NULL || haystack == NULL)
15435 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436
Bram Moolenaar33570922005-01-25 22:26:29 +000015437 if (argvars[2].v_type != VAR_UNKNOWN)
15438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015439 int error = FALSE;
15440
15441 start_idx = get_tv_number_chk(&argvars[2], &error);
15442 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015443 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015444 if (start_idx >= 0)
15445 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015446 }
15447
15448 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15449 if (pos != NULL)
15450 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451}
15452
15453/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015454 * "string()" function
15455 */
15456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015457f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015458 typval_T *argvars;
15459 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015460{
15461 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015462 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015464 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015465 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015466 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015467 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468}
15469
15470/*
15471 * "strlen()" function
15472 */
15473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015474f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015475 typval_T *argvars;
15476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 rettv->vval.v_number = (varnumber_T)(STRLEN(
15479 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480}
15481
15482/*
15483 * "strpart()" function
15484 */
15485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015487 typval_T *argvars;
15488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489{
15490 char_u *p;
15491 int n;
15492 int len;
15493 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015494 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 slen = (int)STRLEN(p);
15498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015499 n = get_tv_number_chk(&argvars[1], &error);
15500 if (error)
15501 len = 0;
15502 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015503 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 else
15505 len = slen - n; /* default len: all bytes that are available. */
15506
15507 /*
15508 * Only return the overlap between the specified part and the actual
15509 * string.
15510 */
15511 if (n < 0)
15512 {
15513 len += n;
15514 n = 0;
15515 }
15516 else if (n > slen)
15517 n = slen;
15518 if (len < 0)
15519 len = 0;
15520 else if (n + len > slen)
15521 len = slen - n;
15522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015523 rettv->v_type = VAR_STRING;
15524 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525}
15526
15527/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015528 * "strridx()" function
15529 */
15530 static void
15531f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015532 typval_T *argvars;
15533 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015534{
15535 char_u buf[NUMBUFLEN];
15536 char_u *needle;
15537 char_u *haystack;
15538 char_u *rest;
15539 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015540 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015542 needle = get_tv_string_chk(&argvars[1]);
15543 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015544
15545 rettv->vval.v_number = -1;
15546 if (needle == NULL || haystack == NULL)
15547 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015548
15549 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015550 if (argvars[2].v_type != VAR_UNKNOWN)
15551 {
15552 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015553 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015554 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015555 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015556 }
15557 else
15558 end_idx = haystack_len;
15559
Bram Moolenaar0d660222005-01-07 21:51:51 +000015560 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015561 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015563 lastmatch = haystack + end_idx;
15564 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015565 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567 for (rest = haystack; *rest != '\0'; ++rest)
15568 {
15569 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015570 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571 break;
15572 lastmatch = rest;
15573 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015574 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015575
15576 if (lastmatch == NULL)
15577 rettv->vval.v_number = -1;
15578 else
15579 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15580}
15581
15582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 * "strtrans()" function
15584 */
15585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015586f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015587 typval_T *argvars;
15588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015590 rettv->v_type = VAR_STRING;
15591 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592}
15593
15594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015595 * "submatch()" function
15596 */
15597 static void
15598f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015599 typval_T *argvars;
15600 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015601{
15602 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015603 rettv->vval.v_string =
15604 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015605}
15606
15607/*
15608 * "substitute()" function
15609 */
15610 static void
15611f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015612 typval_T *argvars;
15613 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015614{
15615 char_u patbuf[NUMBUFLEN];
15616 char_u subbuf[NUMBUFLEN];
15617 char_u flagsbuf[NUMBUFLEN];
15618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015619 char_u *str = get_tv_string_chk(&argvars[0]);
15620 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15621 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15622 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15623
Bram Moolenaar0d660222005-01-07 21:51:51 +000015624 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015625 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15626 rettv->vval.v_string = NULL;
15627 else
15628 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015629}
15630
15631/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015632 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 */
15634/*ARGSUSED*/
15635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015636f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015637 typval_T *argvars;
15638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639{
15640 int id = 0;
15641#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015642 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 long col;
15644 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015645 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015647 lnum = get_tv_lnum(argvars); /* -1 on type error */
15648 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15649 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015651 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015652 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015653 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654#endif
15655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015656 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657}
15658
15659/*
15660 * "synIDattr(id, what [, mode])" function
15661 */
15662/*ARGSUSED*/
15663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015664f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015665 typval_T *argvars;
15666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667{
15668 char_u *p = NULL;
15669#ifdef FEAT_SYN_HL
15670 int id;
15671 char_u *what;
15672 char_u *mode;
15673 char_u modebuf[NUMBUFLEN];
15674 int modec;
15675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015676 id = get_tv_number(&argvars[0]);
15677 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015678 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015680 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 modec = TOLOWER_ASC(mode[0]);
15682 if (modec != 't' && modec != 'c'
15683#ifdef FEAT_GUI
15684 && modec != 'g'
15685#endif
15686 )
15687 modec = 0; /* replace invalid with current */
15688 }
15689 else
15690 {
15691#ifdef FEAT_GUI
15692 if (gui.in_use)
15693 modec = 'g';
15694 else
15695#endif
15696 if (t_colors > 1)
15697 modec = 'c';
15698 else
15699 modec = 't';
15700 }
15701
15702
15703 switch (TOLOWER_ASC(what[0]))
15704 {
15705 case 'b':
15706 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15707 p = highlight_color(id, what, modec);
15708 else /* bold */
15709 p = highlight_has_attr(id, HL_BOLD, modec);
15710 break;
15711
15712 case 'f': /* fg[#] */
15713 p = highlight_color(id, what, modec);
15714 break;
15715
15716 case 'i':
15717 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15718 p = highlight_has_attr(id, HL_INVERSE, modec);
15719 else /* italic */
15720 p = highlight_has_attr(id, HL_ITALIC, modec);
15721 break;
15722
15723 case 'n': /* name */
15724 p = get_highlight_name(NULL, id - 1);
15725 break;
15726
15727 case 'r': /* reverse */
15728 p = highlight_has_attr(id, HL_INVERSE, modec);
15729 break;
15730
15731 case 's': /* standout */
15732 p = highlight_has_attr(id, HL_STANDOUT, modec);
15733 break;
15734
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015735 case 'u':
15736 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15737 /* underline */
15738 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15739 else
15740 /* undercurl */
15741 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 break;
15743 }
15744
15745 if (p != NULL)
15746 p = vim_strsave(p);
15747#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015748 rettv->v_type = VAR_STRING;
15749 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750}
15751
15752/*
15753 * "synIDtrans(id)" function
15754 */
15755/*ARGSUSED*/
15756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015757f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015758 typval_T *argvars;
15759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760{
15761 int id;
15762
15763#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015764 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765
15766 if (id > 0)
15767 id = syn_get_final_id(id);
15768 else
15769#endif
15770 id = 0;
15771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773}
15774
15775/*
15776 * "system()" function
15777 */
15778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015779f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015780 typval_T *argvars;
15781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015783 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015785 char_u *infile = NULL;
15786 char_u buf[NUMBUFLEN];
15787 int err = FALSE;
15788 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015790 if (check_restricted() || check_secure())
15791 return;
15792
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015793 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015794 {
15795 /*
15796 * Write the string to a temp file, to be used for input of the shell
15797 * command.
15798 */
15799 if ((infile = vim_tempname('i')) == NULL)
15800 {
15801 EMSG(_(e_notmp));
15802 return;
15803 }
15804
15805 fd = mch_fopen((char *)infile, WRITEBIN);
15806 if (fd == NULL)
15807 {
15808 EMSG2(_(e_notopen), infile);
15809 goto done;
15810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 p = get_tv_string_buf_chk(&argvars[1], buf);
15812 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015813 {
15814 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015815 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015816 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015817 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15818 err = TRUE;
15819 if (fclose(fd) != 0)
15820 err = TRUE;
15821 if (err)
15822 {
15823 EMSG(_("E677: Error writing temp file"));
15824 goto done;
15825 }
15826 }
15827
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015828 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15829 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015830
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831#ifdef USE_CR
15832 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015833 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834 {
15835 char_u *s;
15836
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015837 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 {
15839 if (*s == CAR)
15840 *s = NL;
15841 }
15842 }
15843#else
15844# ifdef USE_CRNL
15845 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015846 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847 {
15848 char_u *s, *d;
15849
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015850 d = res;
15851 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 {
15853 if (s[0] == CAR && s[1] == NL)
15854 ++s;
15855 *d++ = *s;
15856 }
15857 *d = NUL;
15858 }
15859# endif
15860#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015861
15862done:
15863 if (infile != NULL)
15864 {
15865 mch_remove(infile);
15866 vim_free(infile);
15867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015868 rettv->v_type = VAR_STRING;
15869 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870}
15871
15872/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015873 * "tabpagebuflist()" function
15874 */
15875/* ARGSUSED */
15876 static void
15877f_tabpagebuflist(argvars, rettv)
15878 typval_T *argvars;
15879 typval_T *rettv;
15880{
15881#ifndef FEAT_WINDOWS
15882 rettv->vval.v_number = 0;
15883#else
15884 tabpage_T *tp;
15885 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015886
15887 if (argvars[0].v_type == VAR_UNKNOWN)
15888 wp = firstwin;
15889 else
15890 {
15891 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15892 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015893 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015894 }
15895 if (wp == NULL)
15896 rettv->vval.v_number = 0;
15897 else
15898 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015899 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015900 rettv->vval.v_number = 0;
15901 else
15902 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015903 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015904 if (list_append_number(rettv->vval.v_list,
15905 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015906 break;
15907 }
15908 }
15909#endif
15910}
15911
15912
15913/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015914 * "tabpagenr()" function
15915 */
15916/* ARGSUSED */
15917 static void
15918f_tabpagenr(argvars, rettv)
15919 typval_T *argvars;
15920 typval_T *rettv;
15921{
15922 int nr = 1;
15923#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015924 char_u *arg;
15925
15926 if (argvars[0].v_type != VAR_UNKNOWN)
15927 {
15928 arg = get_tv_string_chk(&argvars[0]);
15929 nr = 0;
15930 if (arg != NULL)
15931 {
15932 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015933 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015934 else
15935 EMSG2(_(e_invexpr2), arg);
15936 }
15937 }
15938 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015939 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015940#endif
15941 rettv->vval.v_number = nr;
15942}
15943
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015944
15945#ifdef FEAT_WINDOWS
15946static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15947
15948/*
15949 * Common code for tabpagewinnr() and winnr().
15950 */
15951 static int
15952get_winnr(tp, argvar)
15953 tabpage_T *tp;
15954 typval_T *argvar;
15955{
15956 win_T *twin;
15957 int nr = 1;
15958 win_T *wp;
15959 char_u *arg;
15960
15961 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15962 if (argvar->v_type != VAR_UNKNOWN)
15963 {
15964 arg = get_tv_string_chk(argvar);
15965 if (arg == NULL)
15966 nr = 0; /* type error; errmsg already given */
15967 else if (STRCMP(arg, "$") == 0)
15968 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15969 else if (STRCMP(arg, "#") == 0)
15970 {
15971 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15972 if (twin == NULL)
15973 nr = 0;
15974 }
15975 else
15976 {
15977 EMSG2(_(e_invexpr2), arg);
15978 nr = 0;
15979 }
15980 }
15981
15982 if (nr > 0)
15983 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15984 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000015985 {
15986 if (wp == NULL)
15987 {
15988 /* didn't find it in this tabpage */
15989 nr = 0;
15990 break;
15991 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015992 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000015993 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015994 return nr;
15995}
15996#endif
15997
15998/*
15999 * "tabpagewinnr()" function
16000 */
16001/* ARGSUSED */
16002 static void
16003f_tabpagewinnr(argvars, rettv)
16004 typval_T *argvars;
16005 typval_T *rettv;
16006{
16007 int nr = 1;
16008#ifdef FEAT_WINDOWS
16009 tabpage_T *tp;
16010
16011 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16012 if (tp == NULL)
16013 nr = 0;
16014 else
16015 nr = get_winnr(tp, &argvars[1]);
16016#endif
16017 rettv->vval.v_number = nr;
16018}
16019
16020
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016021/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016022 * "tagfiles()" function
16023 */
16024/*ARGSUSED*/
16025 static void
16026f_tagfiles(argvars, rettv)
16027 typval_T *argvars;
16028 typval_T *rettv;
16029{
16030 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016031 tagname_T tn;
16032 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016033
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016034 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016035 {
16036 rettv->vval.v_number = 0;
16037 return;
16038 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016039
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016040 for (first = TRUE; ; first = FALSE)
16041 if (get_tagfname(&tn, first, fname) == FAIL
16042 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016043 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016044 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016045}
16046
16047/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016048 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016049 */
16050 static void
16051f_taglist(argvars, rettv)
16052 typval_T *argvars;
16053 typval_T *rettv;
16054{
16055 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016056
16057 tag_pattern = get_tv_string(&argvars[0]);
16058
16059 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016060 if (*tag_pattern == NUL)
16061 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016062
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016063 if (rettv_list_alloc(rettv) == OK)
16064 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016065}
16066
16067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068 * "tempname()" function
16069 */
16070/*ARGSUSED*/
16071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016072f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016073 typval_T *argvars;
16074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075{
16076 static int x = 'A';
16077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016078 rettv->v_type = VAR_STRING;
16079 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080
16081 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16082 * names. Skip 'I' and 'O', they are used for shell redirection. */
16083 do
16084 {
16085 if (x == 'Z')
16086 x = '0';
16087 else if (x == '9')
16088 x = 'A';
16089 else
16090 {
16091#ifdef EBCDIC
16092 if (x == 'I')
16093 x = 'J';
16094 else if (x == 'R')
16095 x = 'S';
16096 else
16097#endif
16098 ++x;
16099 }
16100 } while (x == 'I' || x == 'O');
16101}
16102
16103/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016104 * "test(list)" function: Just checking the walls...
16105 */
16106/*ARGSUSED*/
16107 static void
16108f_test(argvars, rettv)
16109 typval_T *argvars;
16110 typval_T *rettv;
16111{
16112 /* Used for unit testing. Change the code below to your liking. */
16113#if 0
16114 listitem_T *li;
16115 list_T *l;
16116 char_u *bad, *good;
16117
16118 if (argvars[0].v_type != VAR_LIST)
16119 return;
16120 l = argvars[0].vval.v_list;
16121 if (l == NULL)
16122 return;
16123 li = l->lv_first;
16124 if (li == NULL)
16125 return;
16126 bad = get_tv_string(&li->li_tv);
16127 li = li->li_next;
16128 if (li == NULL)
16129 return;
16130 good = get_tv_string(&li->li_tv);
16131 rettv->vval.v_number = test_edit_score(bad, good);
16132#endif
16133}
16134
16135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 * "tolower(string)" function
16137 */
16138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016139f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016140 typval_T *argvars;
16141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142{
16143 char_u *p;
16144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145 p = vim_strsave(get_tv_string(&argvars[0]));
16146 rettv->v_type = VAR_STRING;
16147 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148
16149 if (p != NULL)
16150 while (*p != NUL)
16151 {
16152#ifdef FEAT_MBYTE
16153 int l;
16154
16155 if (enc_utf8)
16156 {
16157 int c, lc;
16158
16159 c = utf_ptr2char(p);
16160 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016161 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 /* TODO: reallocate string when byte count changes. */
16163 if (utf_char2len(lc) == l)
16164 utf_char2bytes(lc, p);
16165 p += l;
16166 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016167 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 p += l; /* skip multi-byte character */
16169 else
16170#endif
16171 {
16172 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16173 ++p;
16174 }
16175 }
16176}
16177
16178/*
16179 * "toupper(string)" function
16180 */
16181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016183 typval_T *argvars;
16184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016186 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016187 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188}
16189
16190/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016191 * "tr(string, fromstr, tostr)" function
16192 */
16193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016194f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016195 typval_T *argvars;
16196 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016197{
16198 char_u *instr;
16199 char_u *fromstr;
16200 char_u *tostr;
16201 char_u *p;
16202#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016203 int inlen;
16204 int fromlen;
16205 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016206 int idx;
16207 char_u *cpstr;
16208 int cplen;
16209 int first = TRUE;
16210#endif
16211 char_u buf[NUMBUFLEN];
16212 char_u buf2[NUMBUFLEN];
16213 garray_T ga;
16214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016215 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016216 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16217 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016218
16219 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016220 rettv->v_type = VAR_STRING;
16221 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016222 if (fromstr == NULL || tostr == NULL)
16223 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016224 ga_init2(&ga, (int)sizeof(char), 80);
16225
16226#ifdef FEAT_MBYTE
16227 if (!has_mbyte)
16228#endif
16229 /* not multi-byte: fromstr and tostr must be the same length */
16230 if (STRLEN(fromstr) != STRLEN(tostr))
16231 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016232#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016233error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016234#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016235 EMSG2(_(e_invarg2), fromstr);
16236 ga_clear(&ga);
16237 return;
16238 }
16239
16240 /* fromstr and tostr have to contain the same number of chars */
16241 while (*instr != NUL)
16242 {
16243#ifdef FEAT_MBYTE
16244 if (has_mbyte)
16245 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016246 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016247 cpstr = instr;
16248 cplen = inlen;
16249 idx = 0;
16250 for (p = fromstr; *p != NUL; p += fromlen)
16251 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016252 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016253 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16254 {
16255 for (p = tostr; *p != NUL; p += tolen)
16256 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016257 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016258 if (idx-- == 0)
16259 {
16260 cplen = tolen;
16261 cpstr = p;
16262 break;
16263 }
16264 }
16265 if (*p == NUL) /* tostr is shorter than fromstr */
16266 goto error;
16267 break;
16268 }
16269 ++idx;
16270 }
16271
16272 if (first && cpstr == instr)
16273 {
16274 /* Check that fromstr and tostr have the same number of
16275 * (multi-byte) characters. Done only once when a character
16276 * of instr doesn't appear in fromstr. */
16277 first = FALSE;
16278 for (p = tostr; *p != NUL; p += tolen)
16279 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016280 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016281 --idx;
16282 }
16283 if (idx != 0)
16284 goto error;
16285 }
16286
16287 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016288 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016289 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016290
16291 instr += inlen;
16292 }
16293 else
16294#endif
16295 {
16296 /* When not using multi-byte chars we can do it faster. */
16297 p = vim_strchr(fromstr, *instr);
16298 if (p != NULL)
16299 ga_append(&ga, tostr[p - fromstr]);
16300 else
16301 ga_append(&ga, *instr);
16302 ++instr;
16303 }
16304 }
16305
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016306 /* add a terminating NUL */
16307 ga_grow(&ga, 1);
16308 ga_append(&ga, NUL);
16309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016310 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016311}
16312
16313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 * "type(expr)" function
16315 */
16316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016317f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016318 typval_T *argvars;
16319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016321 int n;
16322
16323 switch (argvars[0].v_type)
16324 {
16325 case VAR_NUMBER: n = 0; break;
16326 case VAR_STRING: n = 1; break;
16327 case VAR_FUNC: n = 2; break;
16328 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016329 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016330 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16331 }
16332 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333}
16334
16335/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016336 * "values(dict)" function
16337 */
16338 static void
16339f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016340 typval_T *argvars;
16341 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016342{
16343 dict_list(argvars, rettv, 1);
16344}
16345
16346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 * "virtcol(string)" function
16348 */
16349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350f_virtcol(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{
16354 colnr_T vcol = 0;
16355 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016356 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016358 fp = var2fpos(&argvars[0], FALSE, &fnum);
16359 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16360 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 {
16362 getvvcol(curwin, fp, NULL, NULL, &vcol);
16363 ++vcol;
16364 }
16365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016366 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367}
16368
16369/*
16370 * "visualmode()" function
16371 */
16372/*ARGSUSED*/
16373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016374f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016375 typval_T *argvars;
16376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377{
16378#ifdef FEAT_VISUAL
16379 char_u str[2];
16380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016381 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 str[0] = curbuf->b_visual_mode_eval;
16383 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016384 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385
16386 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016387 if ((argvars[0].v_type == VAR_NUMBER
16388 && argvars[0].vval.v_number != 0)
16389 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016390 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 curbuf->b_visual_mode_eval = NUL;
16392#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016393 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394#endif
16395}
16396
16397/*
16398 * "winbufnr(nr)" function
16399 */
16400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016401f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016402 typval_T *argvars;
16403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404{
16405 win_T *wp;
16406
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016407 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016409 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016411 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412}
16413
16414/*
16415 * "wincol()" function
16416 */
16417/*ARGSUSED*/
16418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016419f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016420 typval_T *argvars;
16421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422{
16423 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016424 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425}
16426
16427/*
16428 * "winheight(nr)" function
16429 */
16430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016431f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016432 typval_T *argvars;
16433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434{
16435 win_T *wp;
16436
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016437 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016441 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442}
16443
16444/*
16445 * "winline()" function
16446 */
16447/*ARGSUSED*/
16448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016449f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016450 typval_T *argvars;
16451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452{
16453 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016454 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455}
16456
16457/*
16458 * "winnr()" function
16459 */
16460/* ARGSUSED */
16461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016462f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016463 typval_T *argvars;
16464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465{
16466 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016467
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016469 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016471 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472}
16473
16474/*
16475 * "winrestcmd()" function
16476 */
16477/* ARGSUSED */
16478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016479f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016480 typval_T *argvars;
16481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482{
16483#ifdef FEAT_WINDOWS
16484 win_T *wp;
16485 int winnr = 1;
16486 garray_T ga;
16487 char_u buf[50];
16488
16489 ga_init2(&ga, (int)sizeof(char), 70);
16490 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16491 {
16492 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16493 ga_concat(&ga, buf);
16494# ifdef FEAT_VERTSPLIT
16495 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16496 ga_concat(&ga, buf);
16497# endif
16498 ++winnr;
16499 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016500 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016506 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507}
16508
16509/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016510 * "winrestview()" function
16511 */
16512/* ARGSUSED */
16513 static void
16514f_winrestview(argvars, rettv)
16515 typval_T *argvars;
16516 typval_T *rettv;
16517{
16518 dict_T *dict;
16519
16520 if (argvars[0].v_type != VAR_DICT
16521 || (dict = argvars[0].vval.v_dict) == NULL)
16522 EMSG(_(e_invarg));
16523 else
16524 {
16525 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16526 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16527#ifdef FEAT_VIRTUALEDIT
16528 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16529#endif
16530 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016531 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016532
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016533 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016534#ifdef FEAT_DIFF
16535 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16536#endif
16537 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16538 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16539
16540 check_cursor();
16541 changed_cline_bef_curs();
16542 invalidate_botline();
16543 redraw_later(VALID);
16544
16545 if (curwin->w_topline == 0)
16546 curwin->w_topline = 1;
16547 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16548 curwin->w_topline = curbuf->b_ml.ml_line_count;
16549#ifdef FEAT_DIFF
16550 check_topfill(curwin, TRUE);
16551#endif
16552 }
16553}
16554
16555/*
16556 * "winsaveview()" function
16557 */
16558/* ARGSUSED */
16559 static void
16560f_winsaveview(argvars, rettv)
16561 typval_T *argvars;
16562 typval_T *rettv;
16563{
16564 dict_T *dict;
16565
16566 dict = dict_alloc();
16567 if (dict == NULL)
16568 return;
16569 rettv->v_type = VAR_DICT;
16570 rettv->vval.v_dict = dict;
16571 ++dict->dv_refcount;
16572
16573 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16574 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16575#ifdef FEAT_VIRTUALEDIT
16576 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16577#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016578 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016579 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16580
16581 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16582#ifdef FEAT_DIFF
16583 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16584#endif
16585 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16586 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16587}
16588
16589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 * "winwidth(nr)" function
16591 */
16592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016593f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016594 typval_T *argvars;
16595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596{
16597 win_T *wp;
16598
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016599 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016601 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 else
16603#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016604 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016606 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607#endif
16608}
16609
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016611 * "writefile()" function
16612 */
16613 static void
16614f_writefile(argvars, rettv)
16615 typval_T *argvars;
16616 typval_T *rettv;
16617{
16618 int binary = FALSE;
16619 char_u *fname;
16620 FILE *fd;
16621 listitem_T *li;
16622 char_u *s;
16623 int ret = 0;
16624 int c;
16625
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016626 if (check_restricted() || check_secure())
16627 return;
16628
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016629 if (argvars[0].v_type != VAR_LIST)
16630 {
16631 EMSG2(_(e_listarg), "writefile()");
16632 return;
16633 }
16634 if (argvars[0].vval.v_list == NULL)
16635 return;
16636
16637 if (argvars[2].v_type != VAR_UNKNOWN
16638 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16639 binary = TRUE;
16640
16641 /* Always open the file in binary mode, library functions have a mind of
16642 * their own about CR-LF conversion. */
16643 fname = get_tv_string(&argvars[1]);
16644 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16645 {
16646 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16647 ret = -1;
16648 }
16649 else
16650 {
16651 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16652 li = li->li_next)
16653 {
16654 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16655 {
16656 if (*s == '\n')
16657 c = putc(NUL, fd);
16658 else
16659 c = putc(*s, fd);
16660 if (c == EOF)
16661 {
16662 ret = -1;
16663 break;
16664 }
16665 }
16666 if (!binary || li->li_next != NULL)
16667 if (putc('\n', fd) == EOF)
16668 {
16669 ret = -1;
16670 break;
16671 }
16672 if (ret < 0)
16673 {
16674 EMSG(_(e_write));
16675 break;
16676 }
16677 }
16678 fclose(fd);
16679 }
16680
16681 rettv->vval.v_number = ret;
16682}
16683
16684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016686 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 */
16688 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016689var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016690 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016691 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016692 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016694 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016696 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697
Bram Moolenaara5525202006-03-02 22:52:09 +000016698 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016699 if (varp->v_type == VAR_LIST)
16700 {
16701 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016702 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016703 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016704 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016705
16706 l = varp->vval.v_list;
16707 if (l == NULL)
16708 return NULL;
16709
16710 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016711 pos.lnum = list_find_nr(l, 0L, &error);
16712 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016713 return NULL; /* invalid line number */
16714
16715 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016716 pos.col = list_find_nr(l, 1L, &error);
16717 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016718 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016719 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016720
16721 /* We accept "$" for the column number: last column. */
16722 li = list_find(l, 1L);
16723 if (li != NULL && li->li_tv.v_type == VAR_STRING
16724 && li->li_tv.vval.v_string != NULL
16725 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16726 pos.col = len + 1;
16727
Bram Moolenaara5525202006-03-02 22:52:09 +000016728 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016729 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016730 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016731 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016732
Bram Moolenaara5525202006-03-02 22:52:09 +000016733#ifdef FEAT_VIRTUALEDIT
16734 /* Get the virtual offset. Defaults to zero. */
16735 pos.coladd = list_find_nr(l, 2L, &error);
16736 if (error)
16737 pos.coladd = 0;
16738#endif
16739
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016740 return &pos;
16741 }
16742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016743 name = get_tv_string_chk(varp);
16744 if (name == NULL)
16745 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 if (name[0] == '.') /* cursor */
16747 return &curwin->w_cursor;
16748 if (name[0] == '\'') /* mark */
16749 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016750 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16752 return NULL;
16753 return pp;
16754 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016755
16756#ifdef FEAT_VIRTUALEDIT
16757 pos.coladd = 0;
16758#endif
16759
Bram Moolenaar477933c2007-07-17 14:32:23 +000016760 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016761 {
16762 pos.col = 0;
16763 if (name[1] == '0') /* "w0": first visible line */
16764 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016765 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016766 pos.lnum = curwin->w_topline;
16767 return &pos;
16768 }
16769 else if (name[1] == '$') /* "w$": last visible line */
16770 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016771 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016772 pos.lnum = curwin->w_botline - 1;
16773 return &pos;
16774 }
16775 }
16776 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016778 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779 {
16780 pos.lnum = curbuf->b_ml.ml_line_count;
16781 pos.col = 0;
16782 }
16783 else
16784 {
16785 pos.lnum = curwin->w_cursor.lnum;
16786 pos.col = (colnr_T)STRLEN(ml_get_curline());
16787 }
16788 return &pos;
16789 }
16790 return NULL;
16791}
16792
16793/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016794 * Convert list in "arg" into a position and optional file number.
16795 * When "fnump" is NULL there is no file number, only 3 items.
16796 * Note that the column is passed on as-is, the caller may want to decrement
16797 * it to use 1 for the first column.
16798 * Return FAIL when conversion is not possible, doesn't check the position for
16799 * validity.
16800 */
16801 static int
16802list2fpos(arg, posp, fnump)
16803 typval_T *arg;
16804 pos_T *posp;
16805 int *fnump;
16806{
16807 list_T *l = arg->vval.v_list;
16808 long i = 0;
16809 long n;
16810
Bram Moolenaarbde35262006-07-23 20:12:24 +000016811 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16812 * when "fnump" isn't NULL and "coladd" is optional. */
16813 if (arg->v_type != VAR_LIST
16814 || l == NULL
16815 || l->lv_len < (fnump == NULL ? 2 : 3)
16816 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016817 return FAIL;
16818
16819 if (fnump != NULL)
16820 {
16821 n = list_find_nr(l, i++, NULL); /* fnum */
16822 if (n < 0)
16823 return FAIL;
16824 if (n == 0)
16825 n = curbuf->b_fnum; /* current buffer */
16826 *fnump = n;
16827 }
16828
16829 n = list_find_nr(l, i++, NULL); /* lnum */
16830 if (n < 0)
16831 return FAIL;
16832 posp->lnum = n;
16833
16834 n = list_find_nr(l, i++, NULL); /* col */
16835 if (n < 0)
16836 return FAIL;
16837 posp->col = n;
16838
16839#ifdef FEAT_VIRTUALEDIT
16840 n = list_find_nr(l, i, NULL);
16841 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016842 posp->coladd = 0;
16843 else
16844 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016845#endif
16846
16847 return OK;
16848}
16849
16850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 * Get the length of an environment variable name.
16852 * Advance "arg" to the first character after the name.
16853 * Return 0 for error.
16854 */
16855 static int
16856get_env_len(arg)
16857 char_u **arg;
16858{
16859 char_u *p;
16860 int len;
16861
16862 for (p = *arg; vim_isIDc(*p); ++p)
16863 ;
16864 if (p == *arg) /* no name found */
16865 return 0;
16866
16867 len = (int)(p - *arg);
16868 *arg = p;
16869 return len;
16870}
16871
16872/*
16873 * Get the length of the name of a function or internal variable.
16874 * "arg" is advanced to the first non-white character after the name.
16875 * Return 0 if something is wrong.
16876 */
16877 static int
16878get_id_len(arg)
16879 char_u **arg;
16880{
16881 char_u *p;
16882 int len;
16883
16884 /* Find the end of the name. */
16885 for (p = *arg; eval_isnamec(*p); ++p)
16886 ;
16887 if (p == *arg) /* no name found */
16888 return 0;
16889
16890 len = (int)(p - *arg);
16891 *arg = skipwhite(p);
16892
16893 return len;
16894}
16895
16896/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016897 * Get the length of the name of a variable or function.
16898 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016900 * Return -1 if curly braces expansion failed.
16901 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 * If the name contains 'magic' {}'s, expand them and return the
16903 * expanded name in an allocated string via 'alias' - caller must free.
16904 */
16905 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016906get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 char_u **arg;
16908 char_u **alias;
16909 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016910 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911{
16912 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 char_u *p;
16914 char_u *expr_start;
16915 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916
16917 *alias = NULL; /* default to no alias */
16918
16919 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16920 && (*arg)[2] == (int)KE_SNR)
16921 {
16922 /* hard coded <SNR>, already translated */
16923 *arg += 3;
16924 return get_id_len(arg) + 3;
16925 }
16926 len = eval_fname_script(*arg);
16927 if (len > 0)
16928 {
16929 /* literal "<SID>", "s:" or "<SNR>" */
16930 *arg += len;
16931 }
16932
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016934 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016936 p = find_name_end(*arg, &expr_start, &expr_end,
16937 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 if (expr_start != NULL)
16939 {
16940 char_u *temp_string;
16941
16942 if (!evaluate)
16943 {
16944 len += (int)(p - *arg);
16945 *arg = skipwhite(p);
16946 return len;
16947 }
16948
16949 /*
16950 * Include any <SID> etc in the expanded string:
16951 * Thus the -len here.
16952 */
16953 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16954 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016955 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 *alias = temp_string;
16957 *arg = skipwhite(p);
16958 return (int)STRLEN(temp_string);
16959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960
16961 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016962 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 EMSG2(_(e_invexpr2), *arg);
16964
16965 return len;
16966}
16967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016968/*
16969 * Find the end of a variable or function name, taking care of magic braces.
16970 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16971 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016972 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016973 * Return a pointer to just after the name. Equal to "arg" if there is no
16974 * valid name.
16975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016977find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 char_u *arg;
16979 char_u **expr_start;
16980 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016981 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016983 int mb_nest = 0;
16984 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 char_u *p;
16986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016987 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016989 *expr_start = NULL;
16990 *expr_end = NULL;
16991 }
16992
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016993 /* Quick check for valid starting character. */
16994 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16995 return arg;
16996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016997 for (p = arg; *p != NUL
16998 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016999 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017000 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017001 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017002 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017003 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017004 if (*p == '\'')
17005 {
17006 /* skip over 'string' to avoid counting [ and ] inside it. */
17007 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17008 ;
17009 if (*p == NUL)
17010 break;
17011 }
17012 else if (*p == '"')
17013 {
17014 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17015 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17016 if (*p == '\\' && p[1] != NUL)
17017 ++p;
17018 if (*p == NUL)
17019 break;
17020 }
17021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017024 if (*p == '[')
17025 ++br_nest;
17026 else if (*p == ']')
17027 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017030 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017032 if (*p == '{')
17033 {
17034 mb_nest++;
17035 if (expr_start != NULL && *expr_start == NULL)
17036 *expr_start = p;
17037 }
17038 else if (*p == '}')
17039 {
17040 mb_nest--;
17041 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17042 *expr_end = p;
17043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 }
17046
17047 return p;
17048}
17049
17050/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017051 * Expands out the 'magic' {}'s in a variable/function name.
17052 * Note that this can call itself recursively, to deal with
17053 * constructs like foo{bar}{baz}{bam}
17054 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17055 * "in_start" ^
17056 * "expr_start" ^
17057 * "expr_end" ^
17058 * "in_end" ^
17059 *
17060 * Returns a new allocated string, which the caller must free.
17061 * Returns NULL for failure.
17062 */
17063 static char_u *
17064make_expanded_name(in_start, expr_start, expr_end, in_end)
17065 char_u *in_start;
17066 char_u *expr_start;
17067 char_u *expr_end;
17068 char_u *in_end;
17069{
17070 char_u c1;
17071 char_u *retval = NULL;
17072 char_u *temp_result;
17073 char_u *nextcmd = NULL;
17074
17075 if (expr_end == NULL || in_end == NULL)
17076 return NULL;
17077 *expr_start = NUL;
17078 *expr_end = NUL;
17079 c1 = *in_end;
17080 *in_end = NUL;
17081
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017082 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017083 if (temp_result != NULL && nextcmd == NULL)
17084 {
17085 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17086 + (in_end - expr_end) + 1));
17087 if (retval != NULL)
17088 {
17089 STRCPY(retval, in_start);
17090 STRCAT(retval, temp_result);
17091 STRCAT(retval, expr_end + 1);
17092 }
17093 }
17094 vim_free(temp_result);
17095
17096 *in_end = c1; /* put char back for error messages */
17097 *expr_start = '{';
17098 *expr_end = '}';
17099
17100 if (retval != NULL)
17101 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017102 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017103 if (expr_start != NULL)
17104 {
17105 /* Further expansion! */
17106 temp_result = make_expanded_name(retval, expr_start,
17107 expr_end, temp_result);
17108 vim_free(retval);
17109 retval = temp_result;
17110 }
17111 }
17112
17113 return retval;
17114}
17115
17116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017118 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 */
17120 static int
17121eval_isnamec(c)
17122 int c;
17123{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017124 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17125}
17126
17127/*
17128 * Return TRUE if character "c" can be used as the first character in a
17129 * variable or function name (excluding '{' and '}').
17130 */
17131 static int
17132eval_isnamec1(c)
17133 int c;
17134{
17135 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136}
17137
17138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 * Set number v: variable to "val".
17140 */
17141 void
17142set_vim_var_nr(idx, val)
17143 int idx;
17144 long val;
17145{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017146 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147}
17148
17149/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017150 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 */
17152 long
17153get_vim_var_nr(idx)
17154 int idx;
17155{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017156 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157}
17158
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017159#if defined(FEAT_AUTOCMD) || defined(PROTO)
17160/*
17161 * Get string v: variable value. Uses a static buffer, can only be used once.
17162 */
17163 char_u *
17164get_vim_var_str(idx)
17165 int idx;
17166{
17167 return get_tv_string(&vimvars[idx].vv_tv);
17168}
17169#endif
17170
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171/*
17172 * Set v:count, v:count1 and v:prevcount.
17173 */
17174 void
17175set_vcount(count, count1)
17176 long count;
17177 long count1;
17178{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017179 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17180 vimvars[VV_COUNT].vv_nr = count;
17181 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182}
17183
17184/*
17185 * Set string v: variable to a copy of "val".
17186 */
17187 void
17188set_vim_var_string(idx, val, len)
17189 int idx;
17190 char_u *val;
17191 int len; /* length of "val" to use or -1 (whole string) */
17192{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017193 /* Need to do this (at least) once, since we can't initialize a union.
17194 * Will always be invoked when "v:progname" is set. */
17195 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17196
Bram Moolenaare9a41262005-01-15 22:18:47 +000017197 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017199 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017201 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017203 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204}
17205
17206/*
17207 * Set v:register if needed.
17208 */
17209 void
17210set_reg_var(c)
17211 int c;
17212{
17213 char_u regname;
17214
17215 if (c == 0 || c == ' ')
17216 regname = '"';
17217 else
17218 regname = c;
17219 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017220 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 set_vim_var_string(VV_REG, &regname, 1);
17222}
17223
17224/*
17225 * Get or set v:exception. If "oldval" == NULL, return the current value.
17226 * Otherwise, restore the value to "oldval" and return NULL.
17227 * Must always be called in pairs to save and restore v:exception! Does not
17228 * take care of memory allocations.
17229 */
17230 char_u *
17231v_exception(oldval)
17232 char_u *oldval;
17233{
17234 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017235 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236
Bram Moolenaare9a41262005-01-15 22:18:47 +000017237 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 return NULL;
17239}
17240
17241/*
17242 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17243 * Otherwise, restore the value to "oldval" and return NULL.
17244 * Must always be called in pairs to save and restore v:throwpoint! Does not
17245 * take care of memory allocations.
17246 */
17247 char_u *
17248v_throwpoint(oldval)
17249 char_u *oldval;
17250{
17251 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017252 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253
Bram Moolenaare9a41262005-01-15 22:18:47 +000017254 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255 return NULL;
17256}
17257
17258#if defined(FEAT_AUTOCMD) || defined(PROTO)
17259/*
17260 * Set v:cmdarg.
17261 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17262 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17263 * Must always be called in pairs!
17264 */
17265 char_u *
17266set_cmdarg(eap, oldarg)
17267 exarg_T *eap;
17268 char_u *oldarg;
17269{
17270 char_u *oldval;
17271 char_u *newval;
17272 unsigned len;
17273
Bram Moolenaare9a41262005-01-15 22:18:47 +000017274 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017275 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017277 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017278 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017279 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 }
17281
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017282 if (eap->force_bin == FORCE_BIN)
17283 len = 6;
17284 else if (eap->force_bin == FORCE_NOBIN)
17285 len = 8;
17286 else
17287 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017288
17289 if (eap->read_edit)
17290 len += 7;
17291
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017292 if (eap->force_ff != 0)
17293 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17294# ifdef FEAT_MBYTE
17295 if (eap->force_enc != 0)
17296 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017297 if (eap->bad_char != 0)
17298 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017299# endif
17300
17301 newval = alloc(len + 1);
17302 if (newval == NULL)
17303 return NULL;
17304
17305 if (eap->force_bin == FORCE_BIN)
17306 sprintf((char *)newval, " ++bin");
17307 else if (eap->force_bin == FORCE_NOBIN)
17308 sprintf((char *)newval, " ++nobin");
17309 else
17310 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017311
17312 if (eap->read_edit)
17313 STRCAT(newval, " ++edit");
17314
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017315 if (eap->force_ff != 0)
17316 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17317 eap->cmd + eap->force_ff);
17318# ifdef FEAT_MBYTE
17319 if (eap->force_enc != 0)
17320 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17321 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017322 if (eap->bad_char != 0)
17323 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17324 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017325# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017326 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017327 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328}
17329#endif
17330
17331/*
17332 * Get the value of internal variable "name".
17333 * Return OK or FAIL.
17334 */
17335 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017336get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 char_u *name;
17338 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017339 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017340 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341{
17342 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017343 typval_T *tv = NULL;
17344 typval_T atv;
17345 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347
17348 /* truncate the name, so that we can use strcmp() */
17349 cc = name[len];
17350 name[len] = NUL;
17351
17352 /*
17353 * Check for "b:changedtick".
17354 */
17355 if (STRCMP(name, "b:changedtick") == 0)
17356 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017357 atv.v_type = VAR_NUMBER;
17358 atv.vval.v_number = curbuf->b_changedtick;
17359 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 }
17361
17362 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 * Check for user-defined variables.
17364 */
17365 else
17366 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017367 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017369 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 }
17371
Bram Moolenaare9a41262005-01-15 22:18:47 +000017372 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017374 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 ret = FAIL;
17377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017379 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380
17381 name[len] = cc;
17382
17383 return ret;
17384}
17385
17386/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017387 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17388 * Also handle function call with Funcref variable: func(expr)
17389 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17390 */
17391 static int
17392handle_subscript(arg, rettv, evaluate, verbose)
17393 char_u **arg;
17394 typval_T *rettv;
17395 int evaluate; /* do more than finding the end */
17396 int verbose; /* give error messages */
17397{
17398 int ret = OK;
17399 dict_T *selfdict = NULL;
17400 char_u *s;
17401 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017402 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017403
17404 while (ret == OK
17405 && (**arg == '['
17406 || (**arg == '.' && rettv->v_type == VAR_DICT)
17407 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17408 && !vim_iswhite(*(*arg - 1)))
17409 {
17410 if (**arg == '(')
17411 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017412 /* need to copy the funcref so that we can clear rettv */
17413 functv = *rettv;
17414 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017415
17416 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017417 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017418 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017419 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17420 &len, evaluate, selfdict);
17421
17422 /* Clear the funcref afterwards, so that deleting it while
17423 * evaluating the arguments is possible (see test55). */
17424 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017425
17426 /* Stop the expression evaluation when immediately aborting on
17427 * error, or when an interrupt occurred or an exception was thrown
17428 * but not caught. */
17429 if (aborting())
17430 {
17431 if (ret == OK)
17432 clear_tv(rettv);
17433 ret = FAIL;
17434 }
17435 dict_unref(selfdict);
17436 selfdict = NULL;
17437 }
17438 else /* **arg == '[' || **arg == '.' */
17439 {
17440 dict_unref(selfdict);
17441 if (rettv->v_type == VAR_DICT)
17442 {
17443 selfdict = rettv->vval.v_dict;
17444 if (selfdict != NULL)
17445 ++selfdict->dv_refcount;
17446 }
17447 else
17448 selfdict = NULL;
17449 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17450 {
17451 clear_tv(rettv);
17452 ret = FAIL;
17453 }
17454 }
17455 }
17456 dict_unref(selfdict);
17457 return ret;
17458}
17459
17460/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017461 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17462 * value).
17463 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017464 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017465alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017466{
Bram Moolenaar33570922005-01-25 22:26:29 +000017467 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017468}
17469
17470/*
17471 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 * The string "s" must have been allocated, it is consumed.
17473 * Return NULL for out of memory, the variable otherwise.
17474 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017475 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017476alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 char_u *s;
17478{
Bram Moolenaar33570922005-01-25 22:26:29 +000017479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017481 rettv = alloc_tv();
17482 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017484 rettv->v_type = VAR_STRING;
17485 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 }
17487 else
17488 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017489 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490}
17491
17492/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017493 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017495 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017496free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017497 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498{
17499 if (varp != NULL)
17500 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017501 switch (varp->v_type)
17502 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017503 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017504 func_unref(varp->vval.v_string);
17505 /*FALLTHROUGH*/
17506 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017507 vim_free(varp->vval.v_string);
17508 break;
17509 case VAR_LIST:
17510 list_unref(varp->vval.v_list);
17511 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017512 case VAR_DICT:
17513 dict_unref(varp->vval.v_dict);
17514 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017515 case VAR_NUMBER:
17516 case VAR_UNKNOWN:
17517 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017518 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017519 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017520 break;
17521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 vim_free(varp);
17523 }
17524}
17525
17526/*
17527 * Free the memory for a variable value and set the value to NULL or 0.
17528 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017529 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017530clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532{
17533 if (varp != NULL)
17534 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017535 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017537 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017538 func_unref(varp->vval.v_string);
17539 /*FALLTHROUGH*/
17540 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017541 vim_free(varp->vval.v_string);
17542 varp->vval.v_string = NULL;
17543 break;
17544 case VAR_LIST:
17545 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017546 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017547 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017548 case VAR_DICT:
17549 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017550 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017551 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017552 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017553 varp->vval.v_number = 0;
17554 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017555 case VAR_UNKNOWN:
17556 break;
17557 default:
17558 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017560 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 }
17562}
17563
17564/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017565 * Set the value of a variable to NULL without freeing items.
17566 */
17567 static void
17568init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017569 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017570{
17571 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017572 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017573}
17574
17575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 * Get the number value of a variable.
17577 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017578 * For incompatible types, return 0.
17579 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17580 * caller of incompatible types: it sets *denote to TRUE if "denote"
17581 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582 */
17583 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017584get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017585 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017587 int error = FALSE;
17588
17589 return get_tv_number_chk(varp, &error); /* return 0L on error */
17590}
17591
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017592 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017593get_tv_number_chk(varp, denote)
17594 typval_T *varp;
17595 int *denote;
17596{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017597 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017599 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017601 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017602 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017603 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017604 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017605 break;
17606 case VAR_STRING:
17607 if (varp->vval.v_string != NULL)
17608 vim_str2nr(varp->vval.v_string, NULL, NULL,
17609 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017610 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017611 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017612 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017613 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017614 case VAR_DICT:
17615 EMSG(_("E728: Using a Dictionary as a number"));
17616 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017617 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017618 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017619 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017621 if (denote == NULL) /* useful for values that must be unsigned */
17622 n = -1;
17623 else
17624 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017625 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626}
17627
17628/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017629 * Get the lnum from the first argument.
17630 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017631 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 */
17633 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017634get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017635 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636{
Bram Moolenaar33570922005-01-25 22:26:29 +000017637 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 linenr_T lnum;
17639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017640 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 if (lnum == 0) /* no valid number, try using line() */
17642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017643 rettv.v_type = VAR_NUMBER;
17644 f_line(argvars, &rettv);
17645 lnum = rettv.vval.v_number;
17646 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 }
17648 return lnum;
17649}
17650
17651/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017652 * Get the lnum from the first argument.
17653 * Also accepts "$", then "buf" is used.
17654 * Returns 0 on error.
17655 */
17656 static linenr_T
17657get_tv_lnum_buf(argvars, buf)
17658 typval_T *argvars;
17659 buf_T *buf;
17660{
17661 if (argvars[0].v_type == VAR_STRING
17662 && argvars[0].vval.v_string != NULL
17663 && argvars[0].vval.v_string[0] == '$'
17664 && buf != NULL)
17665 return buf->b_ml.ml_line_count;
17666 return get_tv_number_chk(&argvars[0], NULL);
17667}
17668
17669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 * Get the string value of a variable.
17671 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017672 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17673 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 * If the String variable has never been set, return an empty string.
17675 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017676 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17677 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 */
17679 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017680get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017681 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682{
17683 static char_u mybuf[NUMBUFLEN];
17684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686}
17687
17688 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017689get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017690 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017691 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017693 char_u *res = get_tv_string_buf_chk(varp, buf);
17694
17695 return res != NULL ? res : (char_u *)"";
17696}
17697
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017698 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017699get_tv_string_chk(varp)
17700 typval_T *varp;
17701{
17702 static char_u mybuf[NUMBUFLEN];
17703
17704 return get_tv_string_buf_chk(varp, mybuf);
17705}
17706
17707 static char_u *
17708get_tv_string_buf_chk(varp, buf)
17709 typval_T *varp;
17710 char_u *buf;
17711{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017712 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017714 case VAR_NUMBER:
17715 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17716 return buf;
17717 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017718 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017719 break;
17720 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017721 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017722 break;
17723 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017724 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017725 break;
17726 case VAR_STRING:
17727 if (varp->vval.v_string != NULL)
17728 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017729 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017730 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017731 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017732 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017734 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735}
17736
17737/*
17738 * Find variable "name" in the list of variables.
17739 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017740 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017741 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017742 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017745find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017747 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017750 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751
Bram Moolenaara7043832005-01-21 11:56:39 +000017752 ht = find_var_ht(name, &varname);
17753 if (htp != NULL)
17754 *htp = ht;
17755 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017757 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758}
17759
17760/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017761 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017762 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017764 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017765find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017766 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017767 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017768 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017769{
Bram Moolenaar33570922005-01-25 22:26:29 +000017770 hashitem_T *hi;
17771
17772 if (*varname == NUL)
17773 {
17774 /* Must be something like "s:", otherwise "ht" would be NULL. */
17775 switch (varname[-2])
17776 {
17777 case 's': return &SCRIPT_SV(current_SID).sv_var;
17778 case 'g': return &globvars_var;
17779 case 'v': return &vimvars_var;
17780 case 'b': return &curbuf->b_bufvar;
17781 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017782#ifdef FEAT_WINDOWS
17783 case 't': return &curtab->tp_winvar;
17784#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017785 case 'l': return current_funccal == NULL
17786 ? NULL : &current_funccal->l_vars_var;
17787 case 'a': return current_funccal == NULL
17788 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017789 }
17790 return NULL;
17791 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017792
17793 hi = hash_find(ht, varname);
17794 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017795 {
17796 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017797 * worked find the variable again. Don't auto-load a script if it was
17798 * loaded already, otherwise it would be loaded every time when
17799 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017800 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017801 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017802 hi = hash_find(ht, varname);
17803 if (HASHITEM_EMPTY(hi))
17804 return NULL;
17805 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017806 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017807}
17808
17809/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017810 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017811 * Set "varname" to the start of name without ':'.
17812 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017813 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017814find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815 char_u *name;
17816 char_u **varname;
17817{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017818 hashitem_T *hi;
17819
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 if (name[1] != ':')
17821 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017822 /* The name must not start with a colon or #. */
17823 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 return NULL;
17825 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017826
17827 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017828 hi = hash_find(&compat_hashtab, name);
17829 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017830 return &compat_hashtab;
17831
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017833 return &globvarht; /* global variable */
17834 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 }
17836 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017837 if (*name == 'g') /* global variable */
17838 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017839 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17840 */
17841 if (vim_strchr(name + 2, ':') != NULL
17842 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017843 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017845 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017847 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017848#ifdef FEAT_WINDOWS
17849 if (*name == 't') /* tab page variable */
17850 return &curtab->tp_vars.dv_hashtab;
17851#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017852 if (*name == 'v') /* v: variable */
17853 return &vimvarht;
17854 if (*name == 'a' && current_funccal != NULL) /* function argument */
17855 return &current_funccal->l_avars.dv_hashtab;
17856 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17857 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 if (*name == 's' /* script variable */
17859 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17860 return &SCRIPT_VARS(current_SID);
17861 return NULL;
17862}
17863
17864/*
17865 * Get the string value of a (global/local) variable.
17866 * Returns NULL when it doesn't exist.
17867 */
17868 char_u *
17869get_var_value(name)
17870 char_u *name;
17871{
Bram Moolenaar33570922005-01-25 22:26:29 +000017872 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873
Bram Moolenaara7043832005-01-21 11:56:39 +000017874 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 if (v == NULL)
17876 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017877 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878}
17879
17880/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017881 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882 * sourcing this script and when executing functions defined in the script.
17883 */
17884 void
17885new_script_vars(id)
17886 scid_T id;
17887{
Bram Moolenaara7043832005-01-21 11:56:39 +000017888 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017889 hashtab_T *ht;
17890 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017891
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17893 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017894 /* Re-allocating ga_data means that an ht_array pointing to
17895 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017896 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017897 for (i = 1; i <= ga_scripts.ga_len; ++i)
17898 {
17899 ht = &SCRIPT_VARS(i);
17900 if (ht->ht_mask == HT_INIT_SIZE - 1)
17901 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017902 sv = &SCRIPT_SV(i);
17903 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017904 }
17905
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 while (ga_scripts.ga_len < id)
17907 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017908 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17909 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 }
17912 }
17913}
17914
17915/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017916 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17917 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 */
17919 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017920init_var_dict(dict, dict_var)
17921 dict_T *dict;
17922 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923{
Bram Moolenaar33570922005-01-25 22:26:29 +000017924 hash_init(&dict->dv_hashtab);
17925 dict->dv_refcount = 99999;
17926 dict_var->di_tv.vval.v_dict = dict;
17927 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017928 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017929 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17930 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931}
17932
17933/*
17934 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017935 * Frees all allocated variables and the value they contain.
17936 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 */
17938 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017939vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017940 hashtab_T *ht;
17941{
17942 vars_clear_ext(ht, TRUE);
17943}
17944
17945/*
17946 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17947 */
17948 static void
17949vars_clear_ext(ht, free_val)
17950 hashtab_T *ht;
17951 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952{
Bram Moolenaara7043832005-01-21 11:56:39 +000017953 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017954 hashitem_T *hi;
17955 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956
Bram Moolenaar33570922005-01-25 22:26:29 +000017957 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017958 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017959 for (hi = ht->ht_array; todo > 0; ++hi)
17960 {
17961 if (!HASHITEM_EMPTY(hi))
17962 {
17963 --todo;
17964
Bram Moolenaar33570922005-01-25 22:26:29 +000017965 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017966 * ht_array might change then. hash_clear() takes care of it
17967 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017968 v = HI2DI(hi);
17969 if (free_val)
17970 clear_tv(&v->di_tv);
17971 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17972 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017973 }
17974 }
17975 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017976 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977}
17978
Bram Moolenaara7043832005-01-21 11:56:39 +000017979/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017980 * Delete a variable from hashtab "ht" at item "hi".
17981 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017984delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017985 hashtab_T *ht;
17986 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987{
Bram Moolenaar33570922005-01-25 22:26:29 +000017988 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017989
17990 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017991 clear_tv(&di->di_tv);
17992 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993}
17994
17995/*
17996 * List the value of one internal variable.
17997 */
17998 static void
17999list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000018000 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001 char_u *prefix;
18002{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018003 char_u *tofree;
18004 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018005 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018006
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018007 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018008 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018009 s == NULL ? (char_u *)"" : s);
18010 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011}
18012
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 static void
18014list_one_var_a(prefix, name, type, string)
18015 char_u *prefix;
18016 char_u *name;
18017 int type;
18018 char_u *string;
18019{
18020 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
18021 if (name != NULL) /* "a:" vars don't have a name stored */
18022 msg_puts(name);
18023 msg_putchar(' ');
18024 msg_advance(22);
18025 if (type == VAR_NUMBER)
18026 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018027 else if (type == VAR_FUNC)
18028 msg_putchar('*');
18029 else if (type == VAR_LIST)
18030 {
18031 msg_putchar('[');
18032 if (*string == '[')
18033 ++string;
18034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018035 else if (type == VAR_DICT)
18036 {
18037 msg_putchar('{');
18038 if (*string == '{')
18039 ++string;
18040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 else
18042 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018043
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018045
18046 if (type == VAR_FUNC)
18047 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048}
18049
18050/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018051 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 * If the variable already exists, the value is updated.
18053 * Otherwise the variable is created.
18054 */
18055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018056set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018058 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060{
Bram Moolenaar33570922005-01-25 22:26:29 +000018061 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018063 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018064 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018066 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018067 {
18068 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18069 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18070 ? name[2] : name[0]))
18071 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018072 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018073 return;
18074 }
18075 if (function_exists(name))
18076 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018077 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018078 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018079 return;
18080 }
18081 }
18082
Bram Moolenaara7043832005-01-21 11:56:39 +000018083 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018084 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018085 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018086 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018087 return;
18088 }
18089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018090 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018091 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018093 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018094 if (var_check_ro(v->di_flags, name)
18095 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018096 return;
18097 if (v->di_tv.v_type != tv->v_type
18098 && !((v->di_tv.v_type == VAR_STRING
18099 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018100 && (tv->v_type == VAR_STRING
18101 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018102 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018103 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018104 return;
18105 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018106
18107 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018108 * Handle setting internal v: variables separately: we don't change
18109 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018110 */
18111 if (ht == &vimvarht)
18112 {
18113 if (v->di_tv.v_type == VAR_STRING)
18114 {
18115 vim_free(v->di_tv.vval.v_string);
18116 if (copy || tv->v_type != VAR_STRING)
18117 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18118 else
18119 {
18120 /* Take over the string to avoid an extra alloc/free. */
18121 v->di_tv.vval.v_string = tv->vval.v_string;
18122 tv->vval.v_string = NULL;
18123 }
18124 }
18125 else if (v->di_tv.v_type != VAR_NUMBER)
18126 EMSG2(_(e_intern2), "set_var()");
18127 else
18128 v->di_tv.vval.v_number = get_tv_number(tv);
18129 return;
18130 }
18131
18132 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 }
18134 else /* add a new variable */
18135 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018136 /* Can't add "v:" variable. */
18137 if (ht == &vimvarht)
18138 {
18139 EMSG2(_(e_illvar), name);
18140 return;
18141 }
18142
Bram Moolenaar92124a32005-06-17 22:03:40 +000018143 /* Make sure the variable name is valid. */
18144 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018145 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18146 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018147 {
18148 EMSG2(_(e_illvar), varname);
18149 return;
18150 }
18151
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018152 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18153 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018154 if (v == NULL)
18155 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018156 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018157 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018159 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 return;
18161 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018162 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018165 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018166 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018167 else
18168 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018169 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018170 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018171 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173}
18174
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018175/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018176 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018177 * Also give an error message.
18178 */
18179 static int
18180var_check_ro(flags, name)
18181 int flags;
18182 char_u *name;
18183{
18184 if (flags & DI_FLAGS_RO)
18185 {
18186 EMSG2(_(e_readonlyvar), name);
18187 return TRUE;
18188 }
18189 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18190 {
18191 EMSG2(_(e_readonlysbx), name);
18192 return TRUE;
18193 }
18194 return FALSE;
18195}
18196
18197/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018198 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18199 * Also give an error message.
18200 */
18201 static int
18202var_check_fixed(flags, name)
18203 int flags;
18204 char_u *name;
18205{
18206 if (flags & DI_FLAGS_FIX)
18207 {
18208 EMSG2(_("E795: Cannot delete variable %s"), name);
18209 return TRUE;
18210 }
18211 return FALSE;
18212}
18213
18214/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018215 * Return TRUE if typeval "tv" is set to be locked (immutable).
18216 * Also give an error message, using "name".
18217 */
18218 static int
18219tv_check_lock(lock, name)
18220 int lock;
18221 char_u *name;
18222{
18223 if (lock & VAR_LOCKED)
18224 {
18225 EMSG2(_("E741: Value is locked: %s"),
18226 name == NULL ? (char_u *)_("Unknown") : name);
18227 return TRUE;
18228 }
18229 if (lock & VAR_FIXED)
18230 {
18231 EMSG2(_("E742: Cannot change value of %s"),
18232 name == NULL ? (char_u *)_("Unknown") : name);
18233 return TRUE;
18234 }
18235 return FALSE;
18236}
18237
18238/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018239 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018240 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018241 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018244copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018245 typval_T *from;
18246 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018248 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018249 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018250 switch (from->v_type)
18251 {
18252 case VAR_NUMBER:
18253 to->vval.v_number = from->vval.v_number;
18254 break;
18255 case VAR_STRING:
18256 case VAR_FUNC:
18257 if (from->vval.v_string == NULL)
18258 to->vval.v_string = NULL;
18259 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018260 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018261 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018262 if (from->v_type == VAR_FUNC)
18263 func_ref(to->vval.v_string);
18264 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018265 break;
18266 case VAR_LIST:
18267 if (from->vval.v_list == NULL)
18268 to->vval.v_list = NULL;
18269 else
18270 {
18271 to->vval.v_list = from->vval.v_list;
18272 ++to->vval.v_list->lv_refcount;
18273 }
18274 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018275 case VAR_DICT:
18276 if (from->vval.v_dict == NULL)
18277 to->vval.v_dict = NULL;
18278 else
18279 {
18280 to->vval.v_dict = from->vval.v_dict;
18281 ++to->vval.v_dict->dv_refcount;
18282 }
18283 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018284 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018285 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018286 break;
18287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288}
18289
18290/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018291 * Make a copy of an item.
18292 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018293 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18294 * reference to an already copied list/dict can be used.
18295 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018296 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018297 static int
18298item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018299 typval_T *from;
18300 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018301 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018302 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018303{
18304 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018305 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018306
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018308 {
18309 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018310 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018311 }
18312 ++recurse;
18313
18314 switch (from->v_type)
18315 {
18316 case VAR_NUMBER:
18317 case VAR_STRING:
18318 case VAR_FUNC:
18319 copy_tv(from, to);
18320 break;
18321 case VAR_LIST:
18322 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018323 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018324 if (from->vval.v_list == NULL)
18325 to->vval.v_list = NULL;
18326 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18327 {
18328 /* use the copy made earlier */
18329 to->vval.v_list = from->vval.v_list->lv_copylist;
18330 ++to->vval.v_list->lv_refcount;
18331 }
18332 else
18333 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18334 if (to->vval.v_list == NULL)
18335 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018336 break;
18337 case VAR_DICT:
18338 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018339 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018340 if (from->vval.v_dict == NULL)
18341 to->vval.v_dict = NULL;
18342 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18343 {
18344 /* use the copy made earlier */
18345 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18346 ++to->vval.v_dict->dv_refcount;
18347 }
18348 else
18349 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18350 if (to->vval.v_dict == NULL)
18351 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018352 break;
18353 default:
18354 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018355 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018356 }
18357 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018358 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018359}
18360
18361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 * ":echo expr1 ..." print each argument separated with a space, add a
18363 * newline at the end.
18364 * ":echon expr1 ..." print each argument plain.
18365 */
18366 void
18367ex_echo(eap)
18368 exarg_T *eap;
18369{
18370 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018371 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018372 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 char_u *p;
18374 int needclr = TRUE;
18375 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018376 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377
18378 if (eap->skip)
18379 ++emsg_skip;
18380 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18381 {
18382 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018383 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 {
18385 /*
18386 * Report the invalid expression unless the expression evaluation
18387 * has been cancelled due to an aborting error, an interrupt, or an
18388 * exception.
18389 */
18390 if (!aborting())
18391 EMSG2(_(e_invexpr2), p);
18392 break;
18393 }
18394 if (!eap->skip)
18395 {
18396 if (atstart)
18397 {
18398 atstart = FALSE;
18399 /* Call msg_start() after eval1(), evaluating the expression
18400 * may cause a message to appear. */
18401 if (eap->cmdidx == CMD_echo)
18402 msg_start();
18403 }
18404 else if (eap->cmdidx == CMD_echo)
18405 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018406 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018407 if (p != NULL)
18408 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018410 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018412 if (*p != TAB && needclr)
18413 {
18414 /* remove any text still there from the command */
18415 msg_clr_eos();
18416 needclr = FALSE;
18417 }
18418 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 }
18420 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018421 {
18422#ifdef FEAT_MBYTE
18423 if (has_mbyte)
18424 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018425 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018426
18427 (void)msg_outtrans_len_attr(p, i, echo_attr);
18428 p += i - 1;
18429 }
18430 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018432 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018435 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 arg = skipwhite(arg);
18439 }
18440 eap->nextcmd = check_nextcmd(arg);
18441
18442 if (eap->skip)
18443 --emsg_skip;
18444 else
18445 {
18446 /* remove text that may still be there from the command */
18447 if (needclr)
18448 msg_clr_eos();
18449 if (eap->cmdidx == CMD_echo)
18450 msg_end();
18451 }
18452}
18453
18454/*
18455 * ":echohl {name}".
18456 */
18457 void
18458ex_echohl(eap)
18459 exarg_T *eap;
18460{
18461 int id;
18462
18463 id = syn_name2id(eap->arg);
18464 if (id == 0)
18465 echo_attr = 0;
18466 else
18467 echo_attr = syn_id2attr(id);
18468}
18469
18470/*
18471 * ":execute expr1 ..." execute the result of an expression.
18472 * ":echomsg expr1 ..." Print a message
18473 * ":echoerr expr1 ..." Print an error
18474 * Each gets spaces around each argument and a newline at the end for
18475 * echo commands
18476 */
18477 void
18478ex_execute(eap)
18479 exarg_T *eap;
18480{
18481 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018482 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 int ret = OK;
18484 char_u *p;
18485 garray_T ga;
18486 int len;
18487 int save_did_emsg;
18488
18489 ga_init2(&ga, 1, 80);
18490
18491 if (eap->skip)
18492 ++emsg_skip;
18493 while (*arg != NUL && *arg != '|' && *arg != '\n')
18494 {
18495 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018496 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 {
18498 /*
18499 * Report the invalid expression unless the expression evaluation
18500 * has been cancelled due to an aborting error, an interrupt, or an
18501 * exception.
18502 */
18503 if (!aborting())
18504 EMSG2(_(e_invexpr2), p);
18505 ret = FAIL;
18506 break;
18507 }
18508
18509 if (!eap->skip)
18510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018511 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 len = (int)STRLEN(p);
18513 if (ga_grow(&ga, len + 2) == FAIL)
18514 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018515 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 ret = FAIL;
18517 break;
18518 }
18519 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 ga.ga_len += len;
18523 }
18524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018525 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 arg = skipwhite(arg);
18527 }
18528
18529 if (ret != FAIL && ga.ga_data != NULL)
18530 {
18531 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018534 out_flush();
18535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 else if (eap->cmdidx == CMD_echoerr)
18537 {
18538 /* We don't want to abort following commands, restore did_emsg. */
18539 save_did_emsg = did_emsg;
18540 EMSG((char_u *)ga.ga_data);
18541 if (!force_abort)
18542 did_emsg = save_did_emsg;
18543 }
18544 else if (eap->cmdidx == CMD_execute)
18545 do_cmdline((char_u *)ga.ga_data,
18546 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18547 }
18548
18549 ga_clear(&ga);
18550
18551 if (eap->skip)
18552 --emsg_skip;
18553
18554 eap->nextcmd = check_nextcmd(arg);
18555}
18556
18557/*
18558 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18559 * "arg" points to the "&" or '+' when called, to "option" when returning.
18560 * Returns NULL when no option name found. Otherwise pointer to the char
18561 * after the option name.
18562 */
18563 static char_u *
18564find_option_end(arg, opt_flags)
18565 char_u **arg;
18566 int *opt_flags;
18567{
18568 char_u *p = *arg;
18569
18570 ++p;
18571 if (*p == 'g' && p[1] == ':')
18572 {
18573 *opt_flags = OPT_GLOBAL;
18574 p += 2;
18575 }
18576 else if (*p == 'l' && p[1] == ':')
18577 {
18578 *opt_flags = OPT_LOCAL;
18579 p += 2;
18580 }
18581 else
18582 *opt_flags = 0;
18583
18584 if (!ASCII_ISALPHA(*p))
18585 return NULL;
18586 *arg = p;
18587
18588 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18589 p += 4; /* termcap option */
18590 else
18591 while (ASCII_ISALPHA(*p))
18592 ++p;
18593 return p;
18594}
18595
18596/*
18597 * ":function"
18598 */
18599 void
18600ex_function(eap)
18601 exarg_T *eap;
18602{
18603 char_u *theline;
18604 int j;
18605 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607 char_u *name = NULL;
18608 char_u *p;
18609 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018610 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 garray_T newargs;
18612 garray_T newlines;
18613 int varargs = FALSE;
18614 int mustend = FALSE;
18615 int flags = 0;
18616 ufunc_T *fp;
18617 int indent;
18618 int nesting;
18619 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018620 dictitem_T *v;
18621 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018622 static int func_nr = 0; /* number for nameless function */
18623 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018624 hashtab_T *ht;
18625 int todo;
18626 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018627 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628
18629 /*
18630 * ":function" without argument: list functions.
18631 */
18632 if (ends_excmd(*eap->arg))
18633 {
18634 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018635 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018636 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018637 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018638 {
18639 if (!HASHITEM_EMPTY(hi))
18640 {
18641 --todo;
18642 fp = HI2UF(hi);
18643 if (!isdigit(*fp->uf_name))
18644 list_func_head(fp, FALSE);
18645 }
18646 }
18647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 eap->nextcmd = check_nextcmd(eap->arg);
18649 return;
18650 }
18651
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018652 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018653 * ":function /pat": list functions matching pattern.
18654 */
18655 if (*eap->arg == '/')
18656 {
18657 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18658 if (!eap->skip)
18659 {
18660 regmatch_T regmatch;
18661
18662 c = *p;
18663 *p = NUL;
18664 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18665 *p = c;
18666 if (regmatch.regprog != NULL)
18667 {
18668 regmatch.rm_ic = p_ic;
18669
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018670 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018671 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18672 {
18673 if (!HASHITEM_EMPTY(hi))
18674 {
18675 --todo;
18676 fp = HI2UF(hi);
18677 if (!isdigit(*fp->uf_name)
18678 && vim_regexec(&regmatch, fp->uf_name, 0))
18679 list_func_head(fp, FALSE);
18680 }
18681 }
18682 }
18683 }
18684 if (*p == '/')
18685 ++p;
18686 eap->nextcmd = check_nextcmd(p);
18687 return;
18688 }
18689
18690 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018691 * Get the function name. There are these situations:
18692 * func normal function name
18693 * "name" == func, "fudi.fd_dict" == NULL
18694 * dict.func new dictionary entry
18695 * "name" == NULL, "fudi.fd_dict" set,
18696 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18697 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018698 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018699 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18700 * dict.func existing dict entry that's not a Funcref
18701 * "name" == NULL, "fudi.fd_dict" set,
18702 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018705 name = trans_function_name(&p, eap->skip, 0, &fudi);
18706 paren = (vim_strchr(p, '(') != NULL);
18707 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 {
18709 /*
18710 * Return on an invalid expression in braces, unless the expression
18711 * evaluation has been cancelled due to an aborting error, an
18712 * interrupt, or an exception.
18713 */
18714 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018716 if (!eap->skip && fudi.fd_newkey != NULL)
18717 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018718 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 else
18722 eap->skip = TRUE;
18723 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018724
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 /* An error in a function call during evaluation of an expression in magic
18726 * braces should not cause the function not to be defined. */
18727 saved_did_emsg = did_emsg;
18728 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729
18730 /*
18731 * ":function func" with only function name: list function.
18732 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018733 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 {
18735 if (!ends_excmd(*skipwhite(p)))
18736 {
18737 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018738 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 }
18740 eap->nextcmd = check_nextcmd(p);
18741 if (eap->nextcmd != NULL)
18742 *p = NUL;
18743 if (!eap->skip && !got_int)
18744 {
18745 fp = find_func(name);
18746 if (fp != NULL)
18747 {
18748 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018749 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018751 if (FUNCLINE(fp, j) == NULL)
18752 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 msg_putchar('\n');
18754 msg_outnum((long)(j + 1));
18755 if (j < 9)
18756 msg_putchar(' ');
18757 if (j < 99)
18758 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018759 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 out_flush(); /* show a line at a time */
18761 ui_breakcheck();
18762 }
18763 if (!got_int)
18764 {
18765 msg_putchar('\n');
18766 msg_puts((char_u *)" endfunction");
18767 }
18768 }
18769 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018770 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018772 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 }
18774
18775 /*
18776 * ":function name(arg1, arg2)" Define function.
18777 */
18778 p = skipwhite(p);
18779 if (*p != '(')
18780 {
18781 if (!eap->skip)
18782 {
18783 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018784 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 }
18786 /* attempt to continue by skipping some text */
18787 if (vim_strchr(p, '(') != NULL)
18788 p = vim_strchr(p, '(');
18789 }
18790 p = skipwhite(p + 1);
18791
18792 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18793 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18794
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018795 if (!eap->skip)
18796 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018797 /* Check the name of the function. Unless it's a dictionary function
18798 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018799 if (name != NULL)
18800 arg = name;
18801 else
18802 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018803 if (arg != NULL && (fudi.fd_di == NULL
18804 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018805 {
18806 if (*arg == K_SPECIAL)
18807 j = 3;
18808 else
18809 j = 0;
18810 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18811 : eval_isnamec(arg[j])))
18812 ++j;
18813 if (arg[j] != NUL)
18814 emsg_funcname(_(e_invarg2), arg);
18815 }
18816 }
18817
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 /*
18819 * Isolate the arguments: "arg1, arg2, ...)"
18820 */
18821 while (*p != ')')
18822 {
18823 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18824 {
18825 varargs = TRUE;
18826 p += 3;
18827 mustend = TRUE;
18828 }
18829 else
18830 {
18831 arg = p;
18832 while (ASCII_ISALNUM(*p) || *p == '_')
18833 ++p;
18834 if (arg == p || isdigit(*arg)
18835 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18836 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18837 {
18838 if (!eap->skip)
18839 EMSG2(_("E125: Illegal argument: %s"), arg);
18840 break;
18841 }
18842 if (ga_grow(&newargs, 1) == FAIL)
18843 goto erret;
18844 c = *p;
18845 *p = NUL;
18846 arg = vim_strsave(arg);
18847 if (arg == NULL)
18848 goto erret;
18849 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18850 *p = c;
18851 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 if (*p == ',')
18853 ++p;
18854 else
18855 mustend = TRUE;
18856 }
18857 p = skipwhite(p);
18858 if (mustend && *p != ')')
18859 {
18860 if (!eap->skip)
18861 EMSG2(_(e_invarg2), eap->arg);
18862 break;
18863 }
18864 }
18865 ++p; /* skip the ')' */
18866
Bram Moolenaare9a41262005-01-15 22:18:47 +000018867 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 for (;;)
18869 {
18870 p = skipwhite(p);
18871 if (STRNCMP(p, "range", 5) == 0)
18872 {
18873 flags |= FC_RANGE;
18874 p += 5;
18875 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018876 else if (STRNCMP(p, "dict", 4) == 0)
18877 {
18878 flags |= FC_DICT;
18879 p += 4;
18880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 else if (STRNCMP(p, "abort", 5) == 0)
18882 {
18883 flags |= FC_ABORT;
18884 p += 5;
18885 }
18886 else
18887 break;
18888 }
18889
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018890 /* When there is a line break use what follows for the function body.
18891 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18892 if (*p == '\n')
18893 line_arg = p + 1;
18894 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 EMSG(_(e_trailing));
18896
18897 /*
18898 * Read the body of the function, until ":endfunction" is found.
18899 */
18900 if (KeyTyped)
18901 {
18902 /* Check if the function already exists, don't let the user type the
18903 * whole function before telling him it doesn't work! For a script we
18904 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018905 if (!eap->skip && !eap->forceit)
18906 {
18907 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18908 EMSG(_(e_funcdict));
18909 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018910 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018913 if (!eap->skip && did_emsg)
18914 goto erret;
18915
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 msg_putchar('\n'); /* don't overwrite the function name */
18917 cmdline_row = msg_row;
18918 }
18919
18920 indent = 2;
18921 nesting = 0;
18922 for (;;)
18923 {
18924 msg_scroll = TRUE;
18925 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018926 sourcing_lnum_off = sourcing_lnum;
18927
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018928 if (line_arg != NULL)
18929 {
18930 /* Use eap->arg, split up in parts by line breaks. */
18931 theline = line_arg;
18932 p = vim_strchr(theline, '\n');
18933 if (p == NULL)
18934 line_arg += STRLEN(line_arg);
18935 else
18936 {
18937 *p = NUL;
18938 line_arg = p + 1;
18939 }
18940 }
18941 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942 theline = getcmdline(':', 0L, indent);
18943 else
18944 theline = eap->getline(':', eap->cookie, indent);
18945 if (KeyTyped)
18946 lines_left = Rows - 1;
18947 if (theline == NULL)
18948 {
18949 EMSG(_("E126: Missing :endfunction"));
18950 goto erret;
18951 }
18952
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018953 /* Detect line continuation: sourcing_lnum increased more than one. */
18954 if (sourcing_lnum > sourcing_lnum_off + 1)
18955 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18956 else
18957 sourcing_lnum_off = 0;
18958
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 if (skip_until != NULL)
18960 {
18961 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18962 * don't check for ":endfunc". */
18963 if (STRCMP(theline, skip_until) == 0)
18964 {
18965 vim_free(skip_until);
18966 skip_until = NULL;
18967 }
18968 }
18969 else
18970 {
18971 /* skip ':' and blanks*/
18972 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18973 ;
18974
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018975 /* Check for "endfunction". */
18976 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018978 if (line_arg == NULL)
18979 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 break;
18981 }
18982
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018983 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 * at "end". */
18985 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18986 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018987 else if (STRNCMP(p, "if", 2) == 0
18988 || STRNCMP(p, "wh", 2) == 0
18989 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 || STRNCMP(p, "try", 3) == 0)
18991 indent += 2;
18992
18993 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018994 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018996 if (*p == '!')
18997 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 p += eval_fname_script(p);
18999 if (ASCII_ISALPHA(*p))
19000 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019001 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002 if (*skipwhite(p) == '(')
19003 {
19004 ++nesting;
19005 indent += 2;
19006 }
19007 }
19008 }
19009
19010 /* Check for ":append" or ":insert". */
19011 p = skip_range(p, NULL);
19012 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19013 || (p[0] == 'i'
19014 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19015 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19016 skip_until = vim_strsave((char_u *)".");
19017
19018 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19019 arg = skipwhite(skiptowhite(p));
19020 if (arg[0] == '<' && arg[1] =='<'
19021 && ((p[0] == 'p' && p[1] == 'y'
19022 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19023 || (p[0] == 'p' && p[1] == 'e'
19024 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19025 || (p[0] == 't' && p[1] == 'c'
19026 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19027 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19028 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019029 || (p[0] == 'm' && p[1] == 'z'
19030 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 ))
19032 {
19033 /* ":python <<" continues until a dot, like ":append" */
19034 p = skipwhite(arg + 2);
19035 if (*p == NUL)
19036 skip_until = vim_strsave((char_u *)".");
19037 else
19038 skip_until = vim_strsave(p);
19039 }
19040 }
19041
19042 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019043 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019044 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019045 if (line_arg == NULL)
19046 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019048 }
19049
19050 /* Copy the line to newly allocated memory. get_one_sourceline()
19051 * allocates 250 bytes per line, this saves 80% on average. The cost
19052 * is an extra alloc/free. */
19053 p = vim_strsave(theline);
19054 if (p != NULL)
19055 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019056 if (line_arg == NULL)
19057 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019058 theline = p;
19059 }
19060
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019061 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19062
19063 /* Add NULL lines for continuation lines, so that the line count is
19064 * equal to the index in the growarray. */
19065 while (sourcing_lnum_off-- > 0)
19066 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019067
19068 /* Check for end of eap->arg. */
19069 if (line_arg != NULL && *line_arg == NUL)
19070 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 }
19072
19073 /* Don't define the function when skipping commands or when an error was
19074 * detected. */
19075 if (eap->skip || did_emsg)
19076 goto erret;
19077
19078 /*
19079 * If there are no errors, add the function
19080 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019081 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019082 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019083 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019084 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019085 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019086 emsg_funcname("E707: Function name conflicts with variable: %s",
19087 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019088 goto erret;
19089 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019090
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019091 fp = find_func(name);
19092 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019094 if (!eap->forceit)
19095 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019096 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019097 goto erret;
19098 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019099 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019100 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019101 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019102 name);
19103 goto erret;
19104 }
19105 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019106 ga_clear_strings(&(fp->uf_args));
19107 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019108 vim_free(name);
19109 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 }
19112 else
19113 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019114 char numbuf[20];
19115
19116 fp = NULL;
19117 if (fudi.fd_newkey == NULL && !eap->forceit)
19118 {
19119 EMSG(_(e_funcdict));
19120 goto erret;
19121 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019122 if (fudi.fd_di == NULL)
19123 {
19124 /* Can't add a function to a locked dictionary */
19125 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19126 goto erret;
19127 }
19128 /* Can't change an existing function if it is locked */
19129 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19130 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019131
19132 /* Give the function a sequential number. Can only be used with a
19133 * Funcref! */
19134 vim_free(name);
19135 sprintf(numbuf, "%d", ++func_nr);
19136 name = vim_strsave((char_u *)numbuf);
19137 if (name == NULL)
19138 goto erret;
19139 }
19140
19141 if (fp == NULL)
19142 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019143 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019144 {
19145 int slen, plen;
19146 char_u *scriptname;
19147
19148 /* Check that the autoload name matches the script name. */
19149 j = FAIL;
19150 if (sourcing_name != NULL)
19151 {
19152 scriptname = autoload_name(name);
19153 if (scriptname != NULL)
19154 {
19155 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019156 plen = (int)STRLEN(p);
19157 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019158 if (slen > plen && fnamecmp(p,
19159 sourcing_name + slen - plen) == 0)
19160 j = OK;
19161 vim_free(scriptname);
19162 }
19163 }
19164 if (j == FAIL)
19165 {
19166 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19167 goto erret;
19168 }
19169 }
19170
19171 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 if (fp == NULL)
19173 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019174
19175 if (fudi.fd_dict != NULL)
19176 {
19177 if (fudi.fd_di == NULL)
19178 {
19179 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019180 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019181 if (fudi.fd_di == NULL)
19182 {
19183 vim_free(fp);
19184 goto erret;
19185 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019186 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19187 {
19188 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019189 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019190 goto erret;
19191 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019192 }
19193 else
19194 /* overwrite existing dict entry */
19195 clear_tv(&fudi.fd_di->di_tv);
19196 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019197 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019198 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019199 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019200
19201 /* behave like "dict" was used */
19202 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019203 }
19204
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019206 STRCPY(fp->uf_name, name);
19207 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019209 fp->uf_args = newargs;
19210 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019211#ifdef FEAT_PROFILE
19212 fp->uf_tml_count = NULL;
19213 fp->uf_tml_total = NULL;
19214 fp->uf_tml_self = NULL;
19215 fp->uf_profiling = FALSE;
19216 if (prof_def_func())
19217 func_do_profile(fp);
19218#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019219 fp->uf_varargs = varargs;
19220 fp->uf_flags = flags;
19221 fp->uf_calls = 0;
19222 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019223 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224
19225erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 ga_clear_strings(&newargs);
19227 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019228ret_free:
19229 vim_free(skip_until);
19230 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233}
19234
19235/*
19236 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019237 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019239 * flags:
19240 * TFN_INT: internal function name OK
19241 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 * Advances "pp" to just after the function name (if no error).
19243 */
19244 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019245trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 char_u **pp;
19247 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019248 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019249 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019251 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 char_u *start;
19253 char_u *end;
19254 int lead;
19255 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019257 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019258
19259 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019260 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019262
19263 /* Check for hard coded <SNR>: already translated function ID (from a user
19264 * command). */
19265 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19266 && (*pp)[2] == (int)KE_SNR)
19267 {
19268 *pp += 3;
19269 len = get_id_len(pp) + 3;
19270 return vim_strnsave(start, len);
19271 }
19272
19273 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19274 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019276 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019278
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019279 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19280 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019281 if (end == start)
19282 {
19283 if (!skip)
19284 EMSG(_("E129: Function name required"));
19285 goto theend;
19286 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019287 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019288 {
19289 /*
19290 * Report an invalid expression in braces, unless the expression
19291 * evaluation has been cancelled due to an aborting error, an
19292 * interrupt, or an exception.
19293 */
19294 if (!aborting())
19295 {
19296 if (end != NULL)
19297 EMSG2(_(e_invarg2), start);
19298 }
19299 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019300 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019301 goto theend;
19302 }
19303
19304 if (lv.ll_tv != NULL)
19305 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019306 if (fdp != NULL)
19307 {
19308 fdp->fd_dict = lv.ll_dict;
19309 fdp->fd_newkey = lv.ll_newkey;
19310 lv.ll_newkey = NULL;
19311 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019312 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019313 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19314 {
19315 name = vim_strsave(lv.ll_tv->vval.v_string);
19316 *pp = end;
19317 }
19318 else
19319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019320 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19321 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019322 EMSG(_(e_funcref));
19323 else
19324 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019325 name = NULL;
19326 }
19327 goto theend;
19328 }
19329
19330 if (lv.ll_name == NULL)
19331 {
19332 /* Error found, but continue after the function name. */
19333 *pp = end;
19334 goto theend;
19335 }
19336
19337 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019338 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019339 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019340 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19341 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19342 {
19343 /* When there was "s:" already or the name expanded to get a
19344 * leading "s:" then remove it. */
19345 lv.ll_name += 2;
19346 len -= 2;
19347 lead = 2;
19348 }
19349 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019350 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019351 {
19352 if (lead == 2) /* skip over "s:" */
19353 lv.ll_name += 2;
19354 len = (int)(end - lv.ll_name);
19355 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019356
19357 /*
19358 * Copy the function name to allocated memory.
19359 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19360 * Accept <SNR>123_name() outside a script.
19361 */
19362 if (skip)
19363 lead = 0; /* do nothing */
19364 else if (lead > 0)
19365 {
19366 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019367 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19368 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019369 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019370 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019371 if (current_SID <= 0)
19372 {
19373 EMSG(_(e_usingsid));
19374 goto theend;
19375 }
19376 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19377 lead += (int)STRLEN(sid_buf);
19378 }
19379 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019380 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019381 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019382 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019383 goto theend;
19384 }
19385 name = alloc((unsigned)(len + lead + 1));
19386 if (name != NULL)
19387 {
19388 if (lead > 0)
19389 {
19390 name[0] = K_SPECIAL;
19391 name[1] = KS_EXTRA;
19392 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019393 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019394 STRCPY(name + 3, sid_buf);
19395 }
19396 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19397 name[len + lead] = NUL;
19398 }
19399 *pp = end;
19400
19401theend:
19402 clear_lval(&lv);
19403 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404}
19405
19406/*
19407 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19408 * Return 2 if "p" starts with "s:".
19409 * Return 0 otherwise.
19410 */
19411 static int
19412eval_fname_script(p)
19413 char_u *p;
19414{
19415 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19416 || STRNICMP(p + 1, "SNR>", 4) == 0))
19417 return 5;
19418 if (p[0] == 's' && p[1] == ':')
19419 return 2;
19420 return 0;
19421}
19422
19423/*
19424 * Return TRUE if "p" starts with "<SID>" or "s:".
19425 * Only works if eval_fname_script() returned non-zero for "p"!
19426 */
19427 static int
19428eval_fname_sid(p)
19429 char_u *p;
19430{
19431 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19432}
19433
19434/*
19435 * List the head of the function: "name(arg1, arg2)".
19436 */
19437 static void
19438list_func_head(fp, indent)
19439 ufunc_T *fp;
19440 int indent;
19441{
19442 int j;
19443
19444 msg_start();
19445 if (indent)
19446 MSG_PUTS(" ");
19447 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019448 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 {
19450 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019451 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 }
19453 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019454 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019456 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 {
19458 if (j)
19459 MSG_PUTS(", ");
19460 msg_puts(FUNCARG(fp, j));
19461 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019462 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 {
19464 if (j)
19465 MSG_PUTS(", ");
19466 MSG_PUTS("...");
19467 }
19468 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019469 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019470 if (p_verbose > 0)
19471 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472}
19473
19474/*
19475 * Find a function by name, return pointer to it in ufuncs.
19476 * Return NULL for unknown function.
19477 */
19478 static ufunc_T *
19479find_func(name)
19480 char_u *name;
19481{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019482 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019484 hi = hash_find(&func_hashtab, name);
19485 if (!HASHITEM_EMPTY(hi))
19486 return HI2UF(hi);
19487 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488}
19489
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019490#if defined(EXITFREE) || defined(PROTO)
19491 void
19492free_all_functions()
19493{
19494 hashitem_T *hi;
19495
19496 /* Need to start all over every time, because func_free() may change the
19497 * hash table. */
19498 while (func_hashtab.ht_used > 0)
19499 for (hi = func_hashtab.ht_array; ; ++hi)
19500 if (!HASHITEM_EMPTY(hi))
19501 {
19502 func_free(HI2UF(hi));
19503 break;
19504 }
19505}
19506#endif
19507
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019508/*
19509 * Return TRUE if a function "name" exists.
19510 */
19511 static int
19512function_exists(name)
19513 char_u *name;
19514{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019515 char_u *nm = name;
19516 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019517 int n = FALSE;
19518
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019519 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019520 nm = skipwhite(nm);
19521
19522 /* Only accept "funcname", "funcname ", "funcname (..." and
19523 * "funcname(...", not "funcname!...". */
19524 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019525 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019526 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019527 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019528 else
19529 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019530 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019531 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019532 return n;
19533}
19534
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019535/*
19536 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019537 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019538 */
19539 static int
19540builtin_function(name)
19541 char_u *name;
19542{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019543 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19544 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019545}
19546
Bram Moolenaar05159a02005-02-26 23:04:13 +000019547#if defined(FEAT_PROFILE) || defined(PROTO)
19548/*
19549 * Start profiling function "fp".
19550 */
19551 static void
19552func_do_profile(fp)
19553 ufunc_T *fp;
19554{
19555 fp->uf_tm_count = 0;
19556 profile_zero(&fp->uf_tm_self);
19557 profile_zero(&fp->uf_tm_total);
19558 if (fp->uf_tml_count == NULL)
19559 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19560 (sizeof(int) * fp->uf_lines.ga_len));
19561 if (fp->uf_tml_total == NULL)
19562 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19563 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19564 if (fp->uf_tml_self == NULL)
19565 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19566 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19567 fp->uf_tml_idx = -1;
19568 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19569 || fp->uf_tml_self == NULL)
19570 return; /* out of memory */
19571
19572 fp->uf_profiling = TRUE;
19573}
19574
19575/*
19576 * Dump the profiling results for all functions in file "fd".
19577 */
19578 void
19579func_dump_profile(fd)
19580 FILE *fd;
19581{
19582 hashitem_T *hi;
19583 int todo;
19584 ufunc_T *fp;
19585 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019586 ufunc_T **sorttab;
19587 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019588
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019589 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019590 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19591
Bram Moolenaar05159a02005-02-26 23:04:13 +000019592 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19593 {
19594 if (!HASHITEM_EMPTY(hi))
19595 {
19596 --todo;
19597 fp = HI2UF(hi);
19598 if (fp->uf_profiling)
19599 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019600 if (sorttab != NULL)
19601 sorttab[st_len++] = fp;
19602
Bram Moolenaar05159a02005-02-26 23:04:13 +000019603 if (fp->uf_name[0] == K_SPECIAL)
19604 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19605 else
19606 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19607 if (fp->uf_tm_count == 1)
19608 fprintf(fd, "Called 1 time\n");
19609 else
19610 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19611 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19612 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19613 fprintf(fd, "\n");
19614 fprintf(fd, "count total (s) self (s)\n");
19615
19616 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19617 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019618 if (FUNCLINE(fp, i) == NULL)
19619 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019620 prof_func_line(fd, fp->uf_tml_count[i],
19621 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019622 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19623 }
19624 fprintf(fd, "\n");
19625 }
19626 }
19627 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019628
19629 if (sorttab != NULL && st_len > 0)
19630 {
19631 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19632 prof_total_cmp);
19633 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19634 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19635 prof_self_cmp);
19636 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19637 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019638}
Bram Moolenaar73830342005-02-28 22:48:19 +000019639
19640 static void
19641prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19642 FILE *fd;
19643 ufunc_T **sorttab;
19644 int st_len;
19645 char *title;
19646 int prefer_self; /* when equal print only self time */
19647{
19648 int i;
19649 ufunc_T *fp;
19650
19651 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19652 fprintf(fd, "count total (s) self (s) function\n");
19653 for (i = 0; i < 20 && i < st_len; ++i)
19654 {
19655 fp = sorttab[i];
19656 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19657 prefer_self);
19658 if (fp->uf_name[0] == K_SPECIAL)
19659 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19660 else
19661 fprintf(fd, " %s()\n", fp->uf_name);
19662 }
19663 fprintf(fd, "\n");
19664}
19665
19666/*
19667 * Print the count and times for one function or function line.
19668 */
19669 static void
19670prof_func_line(fd, count, total, self, prefer_self)
19671 FILE *fd;
19672 int count;
19673 proftime_T *total;
19674 proftime_T *self;
19675 int prefer_self; /* when equal print only self time */
19676{
19677 if (count > 0)
19678 {
19679 fprintf(fd, "%5d ", count);
19680 if (prefer_self && profile_equal(total, self))
19681 fprintf(fd, " ");
19682 else
19683 fprintf(fd, "%s ", profile_msg(total));
19684 if (!prefer_self && profile_equal(total, self))
19685 fprintf(fd, " ");
19686 else
19687 fprintf(fd, "%s ", profile_msg(self));
19688 }
19689 else
19690 fprintf(fd, " ");
19691}
19692
19693/*
19694 * Compare function for total time sorting.
19695 */
19696 static int
19697#ifdef __BORLANDC__
19698_RTLENTRYF
19699#endif
19700prof_total_cmp(s1, s2)
19701 const void *s1;
19702 const void *s2;
19703{
19704 ufunc_T *p1, *p2;
19705
19706 p1 = *(ufunc_T **)s1;
19707 p2 = *(ufunc_T **)s2;
19708 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19709}
19710
19711/*
19712 * Compare function for self time sorting.
19713 */
19714 static int
19715#ifdef __BORLANDC__
19716_RTLENTRYF
19717#endif
19718prof_self_cmp(s1, s2)
19719 const void *s1;
19720 const void *s2;
19721{
19722 ufunc_T *p1, *p2;
19723
19724 p1 = *(ufunc_T **)s1;
19725 p2 = *(ufunc_T **)s2;
19726 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19727}
19728
Bram Moolenaar05159a02005-02-26 23:04:13 +000019729#endif
19730
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019731/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019732 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019733 * Return TRUE if a package was loaded.
19734 */
19735 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019736script_autoload(name, reload)
19737 char_u *name;
19738 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019739{
19740 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019741 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019742 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019743 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019744
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019745 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019746 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019747 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019748 return FALSE;
19749
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019750 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019751
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019752 /* Find the name in the list of previously loaded package names. Skip
19753 * "autoload/", it's always the same. */
19754 for (i = 0; i < ga_loaded.ga_len; ++i)
19755 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19756 break;
19757 if (!reload && i < ga_loaded.ga_len)
19758 ret = FALSE; /* was loaded already */
19759 else
19760 {
19761 /* Remember the name if it wasn't loaded already. */
19762 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19763 {
19764 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19765 tofree = NULL;
19766 }
19767
19768 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019769 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019770 ret = TRUE;
19771 }
19772
19773 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019774 return ret;
19775}
19776
19777/*
19778 * Return the autoload script name for a function or variable name.
19779 * Returns NULL when out of memory.
19780 */
19781 static char_u *
19782autoload_name(name)
19783 char_u *name;
19784{
19785 char_u *p;
19786 char_u *scriptname;
19787
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019788 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019789 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19790 if (scriptname == NULL)
19791 return FALSE;
19792 STRCPY(scriptname, "autoload/");
19793 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019794 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019795 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019796 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019797 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019798 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019799}
19800
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19802
19803/*
19804 * Function given to ExpandGeneric() to obtain the list of user defined
19805 * function names.
19806 */
19807 char_u *
19808get_user_func_name(xp, idx)
19809 expand_T *xp;
19810 int idx;
19811{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019812 static long_u done;
19813 static hashitem_T *hi;
19814 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815
19816 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019818 done = 0;
19819 hi = func_hashtab.ht_array;
19820 }
19821 if (done < func_hashtab.ht_used)
19822 {
19823 if (done++ > 0)
19824 ++hi;
19825 while (HASHITEM_EMPTY(hi))
19826 ++hi;
19827 fp = HI2UF(hi);
19828
19829 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19830 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831
19832 cat_func_name(IObuff, fp);
19833 if (xp->xp_context != EXPAND_USER_FUNC)
19834 {
19835 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019836 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 STRCAT(IObuff, ")");
19838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 return IObuff;
19840 }
19841 return NULL;
19842}
19843
19844#endif /* FEAT_CMDL_COMPL */
19845
19846/*
19847 * Copy the function name of "fp" to buffer "buf".
19848 * "buf" must be able to hold the function name plus three bytes.
19849 * Takes care of script-local function names.
19850 */
19851 static void
19852cat_func_name(buf, fp)
19853 char_u *buf;
19854 ufunc_T *fp;
19855{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019856 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857 {
19858 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019859 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 }
19861 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019862 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863}
19864
19865/*
19866 * ":delfunction {name}"
19867 */
19868 void
19869ex_delfunction(eap)
19870 exarg_T *eap;
19871{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019872 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873 char_u *p;
19874 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019875 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876
19877 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019878 name = trans_function_name(&p, eap->skip, 0, &fudi);
19879 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019881 {
19882 if (fudi.fd_dict != NULL && !eap->skip)
19883 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 if (!ends_excmd(*skipwhite(p)))
19887 {
19888 vim_free(name);
19889 EMSG(_(e_trailing));
19890 return;
19891 }
19892 eap->nextcmd = check_nextcmd(p);
19893 if (eap->nextcmd != NULL)
19894 *p = NUL;
19895
19896 if (!eap->skip)
19897 fp = find_func(name);
19898 vim_free(name);
19899
19900 if (!eap->skip)
19901 {
19902 if (fp == NULL)
19903 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019904 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 return;
19906 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019907 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 {
19909 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19910 return;
19911 }
19912
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019913 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019915 /* Delete the dict item that refers to the function, it will
19916 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019917 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019919 else
19920 func_free(fp);
19921 }
19922}
19923
19924/*
19925 * Free a function and remove it from the list of functions.
19926 */
19927 static void
19928func_free(fp)
19929 ufunc_T *fp;
19930{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019931 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019932
19933 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019934 ga_clear_strings(&(fp->uf_args));
19935 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019936#ifdef FEAT_PROFILE
19937 vim_free(fp->uf_tml_count);
19938 vim_free(fp->uf_tml_total);
19939 vim_free(fp->uf_tml_self);
19940#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019941
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019942 /* remove the function from the function hashtable */
19943 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19944 if (HASHITEM_EMPTY(hi))
19945 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019946 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019947 hash_remove(&func_hashtab, hi);
19948
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019949 vim_free(fp);
19950}
19951
19952/*
19953 * Unreference a Function: decrement the reference count and free it when it
19954 * becomes zero. Only for numbered functions.
19955 */
19956 static void
19957func_unref(name)
19958 char_u *name;
19959{
19960 ufunc_T *fp;
19961
19962 if (name != NULL && isdigit(*name))
19963 {
19964 fp = find_func(name);
19965 if (fp == NULL)
19966 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019967 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019968 {
19969 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019970 * when "uf_calls" becomes zero. */
19971 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019972 func_free(fp);
19973 }
19974 }
19975}
19976
19977/*
19978 * Count a reference to a Function.
19979 */
19980 static void
19981func_ref(name)
19982 char_u *name;
19983{
19984 ufunc_T *fp;
19985
19986 if (name != NULL && isdigit(*name))
19987 {
19988 fp = find_func(name);
19989 if (fp == NULL)
19990 EMSG2(_(e_intern2), "func_ref()");
19991 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019992 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 }
19994}
19995
19996/*
19997 * Call a user function.
19998 */
19999 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020000call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 ufunc_T *fp; /* pointer to function */
20002 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020003 typval_T *argvars; /* arguments */
20004 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 linenr_T firstline; /* first line of range */
20006 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020007 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008{
Bram Moolenaar33570922005-01-25 22:26:29 +000020009 char_u *save_sourcing_name;
20010 linenr_T save_sourcing_lnum;
20011 scid_T save_current_SID;
20012 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020013 int save_did_emsg;
20014 static int depth = 0;
20015 dictitem_T *v;
20016 int fixvar_idx = 0; /* index in fixvar[] */
20017 int i;
20018 int ai;
20019 char_u numbuf[NUMBUFLEN];
20020 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020021#ifdef FEAT_PROFILE
20022 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020023 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025
20026 /* If depth of calling is getting too high, don't execute the function */
20027 if (depth >= p_mfd)
20028 {
20029 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020030 rettv->v_type = VAR_NUMBER;
20031 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 return;
20033 }
20034 ++depth;
20035
20036 line_breakcheck(); /* check for CTRL-C hit */
20037
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020038 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020039 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020041 fc.rettv = rettv;
20042 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 fc.linenr = 0;
20044 fc.returned = FALSE;
20045 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020047 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 fc.dbg_tick = debug_tick;
20049
Bram Moolenaar33570922005-01-25 22:26:29 +000020050 /*
20051 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20052 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20053 * each argument variable and saves a lot of time.
20054 */
20055 /*
20056 * Init l: variables.
20057 */
20058 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020059 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020060 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020061 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20062 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020064 name = v->di_key;
20065 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20067 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20068 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020069 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020070 v->di_tv.vval.v_dict = selfdict;
20071 ++selfdict->dv_refcount;
20072 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020073
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 /*
20075 * Init a: variables.
20076 * Set a:0 to "argcount".
20077 * Set a:000 to a list with room for the "..." arguments.
20078 */
20079 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20080 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020081 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020082 v = &fc.fixvar[fixvar_idx++].var;
20083 STRCPY(v->di_key, "000");
20084 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20085 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20086 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020087 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020088 v->di_tv.vval.v_list = &fc.l_varlist;
20089 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20090 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020091 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020092
20093 /*
20094 * Set a:firstline to "firstline" and a:lastline to "lastline".
20095 * Set a:name to named arguments.
20096 * Set a:N to the "..." arguments.
20097 */
20098 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20099 (varnumber_T)firstline);
20100 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20101 (varnumber_T)lastline);
20102 for (i = 0; i < argcount; ++i)
20103 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020104 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020105 if (ai < 0)
20106 /* named argument a:name */
20107 name = FUNCARG(fp, i);
20108 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020109 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020110 /* "..." argument a:1, a:2, etc. */
20111 sprintf((char *)numbuf, "%d", ai + 1);
20112 name = numbuf;
20113 }
20114 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20115 {
20116 v = &fc.fixvar[fixvar_idx++].var;
20117 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20118 }
20119 else
20120 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020121 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20122 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020123 if (v == NULL)
20124 break;
20125 v->di_flags = DI_FLAGS_RO;
20126 }
20127 STRCPY(v->di_key, name);
20128 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20129
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020130 /* Note: the values are copied directly to avoid alloc/free.
20131 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020132 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020133 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020134
20135 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20136 {
20137 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20138 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020139 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020140 }
20141 }
20142
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 /* Don't redraw while executing the function. */
20144 ++RedrawingDisabled;
20145 save_sourcing_name = sourcing_name;
20146 save_sourcing_lnum = sourcing_lnum;
20147 sourcing_lnum = 1;
20148 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020149 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 if (sourcing_name != NULL)
20151 {
20152 if (save_sourcing_name != NULL
20153 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20154 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20155 else
20156 STRCPY(sourcing_name, "function ");
20157 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20158
20159 if (p_verbose >= 12)
20160 {
20161 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020162 verbose_enter_scroll();
20163
Bram Moolenaar555b2802005-05-19 21:08:39 +000020164 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 if (p_verbose >= 14)
20166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020168 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020169 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170
20171 msg_puts((char_u *)"(");
20172 for (i = 0; i < argcount; ++i)
20173 {
20174 if (i > 0)
20175 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020176 if (argvars[i].v_type == VAR_NUMBER)
20177 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 else
20179 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000020180 trunc_string(tv2string(&argvars[i], &tofree,
20181 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020183 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 }
20185 }
20186 msg_puts((char_u *)")");
20187 }
20188 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020189
20190 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 --no_wait_return;
20192 }
20193 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020194#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020195 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020196 {
20197 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20198 func_do_profile(fp);
20199 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020200 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020201 {
20202 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020203 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020204 profile_zero(&fp->uf_tm_children);
20205 }
20206 script_prof_save(&wait_start);
20207 }
20208#endif
20209
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020211 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 save_did_emsg = did_emsg;
20213 did_emsg = FALSE;
20214
20215 /* call do_cmdline() to execute the lines */
20216 do_cmdline(NULL, get_func_line, (void *)&fc,
20217 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20218
20219 --RedrawingDisabled;
20220
20221 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020222 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020224 clear_tv(rettv);
20225 rettv->v_type = VAR_NUMBER;
20226 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 }
20228
Bram Moolenaar05159a02005-02-26 23:04:13 +000020229#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020230 if (do_profiling == PROF_YES && (fp->uf_profiling
20231 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020232 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020233 profile_end(&call_start);
20234 profile_sub_wait(&wait_start, &call_start);
20235 profile_add(&fp->uf_tm_total, &call_start);
20236 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020237 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020238 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020239 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20240 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020241 }
20242 }
20243#endif
20244
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 /* when being verbose, mention the return value */
20246 if (p_verbose >= 12)
20247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020249 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020252 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020254 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20255 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020258 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020259 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020260 char_u *tofree;
20261
Bram Moolenaar555b2802005-05-19 21:08:39 +000020262 /* The value may be very long. Skip the middle part, so that we
20263 * have some idea how it starts and ends. smsg() would always
20264 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000020265 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020266 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000020267 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020268 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 }
20270 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020271
20272 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 --no_wait_return;
20274 }
20275
20276 vim_free(sourcing_name);
20277 sourcing_name = save_sourcing_name;
20278 sourcing_lnum = save_sourcing_lnum;
20279 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020280#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020281 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020282 script_prof_restore(&wait_start);
20283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284
20285 if (p_verbose >= 12 && sourcing_name != NULL)
20286 {
20287 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020288 verbose_enter_scroll();
20289
Bram Moolenaar555b2802005-05-19 21:08:39 +000020290 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020292
20293 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 --no_wait_return;
20295 }
20296
20297 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020298 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 /* The a: variables typevals were not alloced, only free the allocated
20301 * variables. */
20302 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20303
20304 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 --depth;
20306}
20307
20308/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020309 * Add a number variable "name" to dict "dp" with value "nr".
20310 */
20311 static void
20312add_nr_var(dp, v, name, nr)
20313 dict_T *dp;
20314 dictitem_T *v;
20315 char *name;
20316 varnumber_T nr;
20317{
20318 STRCPY(v->di_key, name);
20319 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20320 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20321 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020322 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 v->di_tv.vval.v_number = nr;
20324}
20325
20326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 * ":return [expr]"
20328 */
20329 void
20330ex_return(eap)
20331 exarg_T *eap;
20332{
20333 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020334 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 int returning = FALSE;
20336
20337 if (current_funccal == NULL)
20338 {
20339 EMSG(_("E133: :return not inside a function"));
20340 return;
20341 }
20342
20343 if (eap->skip)
20344 ++emsg_skip;
20345
20346 eap->nextcmd = NULL;
20347 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020348 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 {
20350 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020351 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020353 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354 }
20355 /* It's safer to return also on error. */
20356 else if (!eap->skip)
20357 {
20358 /*
20359 * Return unless the expression evaluation has been cancelled due to an
20360 * aborting error, an interrupt, or an exception.
20361 */
20362 if (!aborting())
20363 returning = do_return(eap, FALSE, TRUE, NULL);
20364 }
20365
20366 /* When skipping or the return gets pending, advance to the next command
20367 * in this line (!returning). Otherwise, ignore the rest of the line.
20368 * Following lines will be ignored by get_func_line(). */
20369 if (returning)
20370 eap->nextcmd = NULL;
20371 else if (eap->nextcmd == NULL) /* no argument */
20372 eap->nextcmd = check_nextcmd(arg);
20373
20374 if (eap->skip)
20375 --emsg_skip;
20376}
20377
20378/*
20379 * Return from a function. Possibly makes the return pending. Also called
20380 * for a pending return at the ":endtry" or after returning from an extra
20381 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020383 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 * FALSE when the return gets pending.
20385 */
20386 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020387do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 exarg_T *eap;
20389 int reanimate;
20390 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020391 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392{
20393 int idx;
20394 struct condstack *cstack = eap->cstack;
20395
20396 if (reanimate)
20397 /* Undo the return. */
20398 current_funccal->returned = FALSE;
20399
20400 /*
20401 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20402 * not in its finally clause (which then is to be executed next) is found.
20403 * In this case, make the ":return" pending for execution at the ":endtry".
20404 * Otherwise, return normally.
20405 */
20406 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20407 if (idx >= 0)
20408 {
20409 cstack->cs_pending[idx] = CSTP_RETURN;
20410
20411 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020412 /* A pending return again gets pending. "rettv" points to an
20413 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020415 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 else
20417 {
20418 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020419 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020421 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020423 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 {
20425 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020426 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020427 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 else
20429 EMSG(_(e_outofmem));
20430 }
20431 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020432 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433
20434 if (reanimate)
20435 {
20436 /* The pending return value could be overwritten by a ":return"
20437 * without argument in a finally clause; reset the default
20438 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020439 current_funccal->rettv->v_type = VAR_NUMBER;
20440 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 }
20442 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020443 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 }
20445 else
20446 {
20447 current_funccal->returned = TRUE;
20448
20449 /* If the return is carried out now, store the return value. For
20450 * a return immediately after reanimation, the value is already
20451 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020452 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020454 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020455 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020457 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 }
20459 }
20460
20461 return idx < 0;
20462}
20463
20464/*
20465 * Free the variable with a pending return value.
20466 */
20467 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020468discard_pending_return(rettv)
20469 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470{
Bram Moolenaar33570922005-01-25 22:26:29 +000020471 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472}
20473
20474/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020475 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 * is an allocated string. Used by report_pending() for verbose messages.
20477 */
20478 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020479get_return_cmd(rettv)
20480 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020482 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020483 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020484 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020486 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020487 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020488 if (s == NULL)
20489 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020490
20491 STRCPY(IObuff, ":return ");
20492 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20493 if (STRLEN(s) + 8 >= IOSIZE)
20494 STRCPY(IObuff + IOSIZE - 4, "...");
20495 vim_free(tofree);
20496 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497}
20498
20499/*
20500 * Get next function line.
20501 * Called by do_cmdline() to get the next line.
20502 * Returns allocated string, or NULL for end of function.
20503 */
20504/* ARGSUSED */
20505 char_u *
20506get_func_line(c, cookie, indent)
20507 int c; /* not used */
20508 void *cookie;
20509 int indent; /* not used */
20510{
Bram Moolenaar33570922005-01-25 22:26:29 +000020511 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020512 ufunc_T *fp = fcp->func;
20513 char_u *retval;
20514 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515
20516 /* If breakpoints have been added/deleted need to check for it. */
20517 if (fcp->dbg_tick != debug_tick)
20518 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020519 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 sourcing_lnum);
20521 fcp->dbg_tick = debug_tick;
20522 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020523#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020524 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020525 func_line_end(cookie);
20526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527
Bram Moolenaar05159a02005-02-26 23:04:13 +000020528 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020529 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20530 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 retval = NULL;
20532 else
20533 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020534 /* Skip NULL lines (continuation lines). */
20535 while (fcp->linenr < gap->ga_len
20536 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20537 ++fcp->linenr;
20538 if (fcp->linenr >= gap->ga_len)
20539 retval = NULL;
20540 else
20541 {
20542 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20543 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020544#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020545 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020546 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020547#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 }
20550
20551 /* Did we encounter a breakpoint? */
20552 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20553 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020554 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020556 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 sourcing_lnum);
20558 fcp->dbg_tick = debug_tick;
20559 }
20560
20561 return retval;
20562}
20563
Bram Moolenaar05159a02005-02-26 23:04:13 +000020564#if defined(FEAT_PROFILE) || defined(PROTO)
20565/*
20566 * Called when starting to read a function line.
20567 * "sourcing_lnum" must be correct!
20568 * When skipping lines it may not actually be executed, but we won't find out
20569 * until later and we need to store the time now.
20570 */
20571 void
20572func_line_start(cookie)
20573 void *cookie;
20574{
20575 funccall_T *fcp = (funccall_T *)cookie;
20576 ufunc_T *fp = fcp->func;
20577
20578 if (fp->uf_profiling && sourcing_lnum >= 1
20579 && sourcing_lnum <= fp->uf_lines.ga_len)
20580 {
20581 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020582 /* Skip continuation lines. */
20583 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20584 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020585 fp->uf_tml_execed = FALSE;
20586 profile_start(&fp->uf_tml_start);
20587 profile_zero(&fp->uf_tml_children);
20588 profile_get_wait(&fp->uf_tml_wait);
20589 }
20590}
20591
20592/*
20593 * Called when actually executing a function line.
20594 */
20595 void
20596func_line_exec(cookie)
20597 void *cookie;
20598{
20599 funccall_T *fcp = (funccall_T *)cookie;
20600 ufunc_T *fp = fcp->func;
20601
20602 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20603 fp->uf_tml_execed = TRUE;
20604}
20605
20606/*
20607 * Called when done with a function line.
20608 */
20609 void
20610func_line_end(cookie)
20611 void *cookie;
20612{
20613 funccall_T *fcp = (funccall_T *)cookie;
20614 ufunc_T *fp = fcp->func;
20615
20616 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20617 {
20618 if (fp->uf_tml_execed)
20619 {
20620 ++fp->uf_tml_count[fp->uf_tml_idx];
20621 profile_end(&fp->uf_tml_start);
20622 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020623 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020624 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20625 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020626 }
20627 fp->uf_tml_idx = -1;
20628 }
20629}
20630#endif
20631
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632/*
20633 * Return TRUE if the currently active function should be ended, because a
20634 * return was encountered or an error occured. Used inside a ":while".
20635 */
20636 int
20637func_has_ended(cookie)
20638 void *cookie;
20639{
Bram Moolenaar33570922005-01-25 22:26:29 +000020640 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641
20642 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20643 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020644 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 || fcp->returned);
20646}
20647
20648/*
20649 * return TRUE if cookie indicates a function which "abort"s on errors.
20650 */
20651 int
20652func_has_abort(cookie)
20653 void *cookie;
20654{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020655 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656}
20657
20658#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20659typedef enum
20660{
20661 VAR_FLAVOUR_DEFAULT,
20662 VAR_FLAVOUR_SESSION,
20663 VAR_FLAVOUR_VIMINFO
20664} var_flavour_T;
20665
20666static var_flavour_T var_flavour __ARGS((char_u *varname));
20667
20668 static var_flavour_T
20669var_flavour(varname)
20670 char_u *varname;
20671{
20672 char_u *p = varname;
20673
20674 if (ASCII_ISUPPER(*p))
20675 {
20676 while (*(++p))
20677 if (ASCII_ISLOWER(*p))
20678 return VAR_FLAVOUR_SESSION;
20679 return VAR_FLAVOUR_VIMINFO;
20680 }
20681 else
20682 return VAR_FLAVOUR_DEFAULT;
20683}
20684#endif
20685
20686#if defined(FEAT_VIMINFO) || defined(PROTO)
20687/*
20688 * Restore global vars that start with a capital from the viminfo file
20689 */
20690 int
20691read_viminfo_varlist(virp, writing)
20692 vir_T *virp;
20693 int writing;
20694{
20695 char_u *tab;
20696 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020697 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698
20699 if (!writing && (find_viminfo_parameter('!') != NULL))
20700 {
20701 tab = vim_strchr(virp->vir_line + 1, '\t');
20702 if (tab != NULL)
20703 {
20704 *tab++ = '\0'; /* isolate the variable name */
20705 if (*tab == 'S') /* string var */
20706 is_string = TRUE;
20707
20708 tab = vim_strchr(tab, '\t');
20709 if (tab != NULL)
20710 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 if (is_string)
20712 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020713 tv.v_type = VAR_STRING;
20714 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 }
20717 else
20718 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020719 tv.v_type = VAR_NUMBER;
20720 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020722 set_var(virp->vir_line + 1, &tv, FALSE);
20723 if (is_string)
20724 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 }
20726 }
20727 }
20728
20729 return viminfo_readline(virp);
20730}
20731
20732/*
20733 * Write global vars that start with a capital to the viminfo file
20734 */
20735 void
20736write_viminfo_varlist(fp)
20737 FILE *fp;
20738{
Bram Moolenaar33570922005-01-25 22:26:29 +000020739 hashitem_T *hi;
20740 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020741 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020743 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020744 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020745 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746
20747 if (find_viminfo_parameter('!') == NULL)
20748 return;
20749
20750 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020751
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020752 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020753 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020755 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020757 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020758 this_var = HI2DI(hi);
20759 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020760 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020761 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020762 {
20763 case VAR_STRING: s = "STR"; break;
20764 case VAR_NUMBER: s = "NUM"; break;
20765 default: continue;
20766 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020767 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020768 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020769 if (p != NULL)
20770 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020771 vim_free(tofree);
20772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 }
20774 }
20775}
20776#endif
20777
20778#if defined(FEAT_SESSION) || defined(PROTO)
20779 int
20780store_session_globals(fd)
20781 FILE *fd;
20782{
Bram Moolenaar33570922005-01-25 22:26:29 +000020783 hashitem_T *hi;
20784 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020785 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 char_u *p, *t;
20787
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020788 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020789 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020791 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020793 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020794 this_var = HI2DI(hi);
20795 if ((this_var->di_tv.v_type == VAR_NUMBER
20796 || this_var->di_tv.v_type == VAR_STRING)
20797 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020798 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020799 /* Escape special characters with a backslash. Turn a LF and
20800 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020801 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020802 (char_u *)"\\\"\n\r");
20803 if (p == NULL) /* out of memory */
20804 break;
20805 for (t = p; *t != NUL; ++t)
20806 if (*t == '\n')
20807 *t = 'n';
20808 else if (*t == '\r')
20809 *t = 'r';
20810 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020811 this_var->di_key,
20812 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20813 : ' ',
20814 p,
20815 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20816 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020817 || put_eol(fd) == FAIL)
20818 {
20819 vim_free(p);
20820 return FAIL;
20821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 vim_free(p);
20823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 }
20825 }
20826 return OK;
20827}
20828#endif
20829
Bram Moolenaar661b1822005-07-28 22:36:45 +000020830/*
20831 * Display script name where an item was last set.
20832 * Should only be invoked when 'verbose' is non-zero.
20833 */
20834 void
20835last_set_msg(scriptID)
20836 scid_T scriptID;
20837{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020838 char_u *p;
20839
Bram Moolenaar661b1822005-07-28 22:36:45 +000020840 if (scriptID != 0)
20841 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020842 p = home_replace_save(NULL, get_scriptname(scriptID));
20843 if (p != NULL)
20844 {
20845 verbose_enter();
20846 MSG_PUTS(_("\n\tLast set from "));
20847 MSG_PUTS(p);
20848 vim_free(p);
20849 verbose_leave();
20850 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020851 }
20852}
20853
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854#endif /* FEAT_EVAL */
20855
20856#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20857
20858
20859#ifdef WIN3264
20860/*
20861 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20862 */
20863static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20864static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20865static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20866
20867/*
20868 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020869 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 */
20871 static int
20872get_short_pathname(fnamep, bufp, fnamelen)
20873 char_u **fnamep;
20874 char_u **bufp;
20875 int *fnamelen;
20876{
20877 int l,len;
20878 char_u *newbuf;
20879
20880 len = *fnamelen;
20881
20882 l = GetShortPathName(*fnamep, *fnamep, len);
20883 if (l > len - 1)
20884 {
20885 /* If that doesn't work (not enough space), then save the string
20886 * and try again with a new buffer big enough
20887 */
20888 newbuf = vim_strnsave(*fnamep, l);
20889 if (newbuf == NULL)
20890 return 0;
20891
20892 vim_free(*bufp);
20893 *fnamep = *bufp = newbuf;
20894
20895 l = GetShortPathName(*fnamep,*fnamep,l+1);
20896
20897 /* Really should always succeed, as the buffer is big enough */
20898 }
20899
20900 *fnamelen = l;
20901 return 1;
20902}
20903
20904/*
20905 * Create a short path name. Returns the length of the buffer it needs.
20906 * Doesn't copy over the end of the buffer passed in.
20907 */
20908 static int
20909shortpath_for_invalid_fname(fname, bufp, fnamelen)
20910 char_u **fname;
20911 char_u **bufp;
20912 int *fnamelen;
20913{
20914 char_u *s, *p, *pbuf2, *pbuf3;
20915 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020916 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917
20918 /* Make a copy */
20919 len2 = *fnamelen;
20920 pbuf2 = vim_strnsave(*fname, len2);
20921 pbuf3 = NULL;
20922
20923 s = pbuf2 + len2 - 1; /* Find the end */
20924 slen = 1;
20925 plen = len2;
20926
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020927 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 {
20929 --s;
20930 ++slen;
20931 --plen;
20932 }
20933
20934 do
20935 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020936 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020937 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 {
20939 --s;
20940 ++slen;
20941 --plen;
20942 }
20943 if (s <= pbuf2)
20944 break;
20945
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020946 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 ch = *s;
20948 *s = 0; /* get_short_pathname requires a null-terminated string */
20949
20950 /* Try it in situ */
20951 p = pbuf2;
20952 if (!get_short_pathname(&p, &pbuf3, &plen))
20953 {
20954 vim_free(pbuf2);
20955 return -1;
20956 }
20957 *s = ch; /* Preserve the string */
20958 } while (plen == 0);
20959
20960 if (plen > 0)
20961 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020962 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 *fnamelen = len = plen + slen;
20964 vim_free(*bufp);
20965 if (len > len2)
20966 {
20967 /* If there's not enough space in the currently allocated string,
20968 * then copy it to a buffer big enough.
20969 */
20970 *fname= *bufp = vim_strnsave(p, len);
20971 if (*fname == NULL)
20972 return -1;
20973 }
20974 else
20975 {
20976 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20977 *fname = *bufp = pbuf2;
20978 if (p != pbuf2)
20979 strncpy(*fname, p, plen);
20980 pbuf2 = NULL;
20981 }
20982 /* Concat the next bit */
20983 strncpy(*fname + plen, s, slen);
20984 (*fname)[len] = '\0';
20985 }
20986 vim_free(pbuf3);
20987 vim_free(pbuf2);
20988 return 0;
20989}
20990
20991/*
20992 * Get a pathname for a partial path.
20993 */
20994 static int
20995shortpath_for_partial(fnamep, bufp, fnamelen)
20996 char_u **fnamep;
20997 char_u **bufp;
20998 int *fnamelen;
20999{
21000 int sepcount, len, tflen;
21001 char_u *p;
21002 char_u *pbuf, *tfname;
21003 int hasTilde;
21004
21005 /* Count up the path seperators from the RHS.. so we know which part
21006 * of the path to return.
21007 */
21008 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021009 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 if (vim_ispathsep(*p))
21011 ++sepcount;
21012
21013 /* Need full path first (use expand_env() to remove a "~/") */
21014 hasTilde = (**fnamep == '~');
21015 if (hasTilde)
21016 pbuf = tfname = expand_env_save(*fnamep);
21017 else
21018 pbuf = tfname = FullName_save(*fnamep, FALSE);
21019
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021020 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021
21022 if (!get_short_pathname(&tfname, &pbuf, &len))
21023 return -1;
21024
21025 if (len == 0)
21026 {
21027 /* Don't have a valid filename, so shorten the rest of the
21028 * path if we can. This CAN give us invalid 8.3 filenames, but
21029 * there's not a lot of point in guessing what it might be.
21030 */
21031 len = tflen;
21032 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21033 return -1;
21034 }
21035
21036 /* Count the paths backward to find the beginning of the desired string. */
21037 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021038 {
21039#ifdef FEAT_MBYTE
21040 if (has_mbyte)
21041 p -= mb_head_off(tfname, p);
21042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 if (vim_ispathsep(*p))
21044 {
21045 if (sepcount == 0 || (hasTilde && sepcount == 1))
21046 break;
21047 else
21048 sepcount --;
21049 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 if (hasTilde)
21052 {
21053 --p;
21054 if (p >= tfname)
21055 *p = '~';
21056 else
21057 return -1;
21058 }
21059 else
21060 ++p;
21061
21062 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21063 vim_free(*bufp);
21064 *fnamelen = (int)STRLEN(p);
21065 *bufp = pbuf;
21066 *fnamep = p;
21067
21068 return 0;
21069}
21070#endif /* WIN3264 */
21071
21072/*
21073 * Adjust a filename, according to a string of modifiers.
21074 * *fnamep must be NUL terminated when called. When returning, the length is
21075 * determined by *fnamelen.
21076 * Returns valid flags.
21077 * When there is an error, *fnamep is set to NULL.
21078 */
21079 int
21080modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21081 char_u *src; /* string with modifiers */
21082 int *usedlen; /* characters after src that are used */
21083 char_u **fnamep; /* file name so far */
21084 char_u **bufp; /* buffer for allocated file name or NULL */
21085 int *fnamelen; /* length of fnamep */
21086{
21087 int valid = 0;
21088 char_u *tail;
21089 char_u *s, *p, *pbuf;
21090 char_u dirname[MAXPATHL];
21091 int c;
21092 int has_fullname = 0;
21093#ifdef WIN3264
21094 int has_shortname = 0;
21095#endif
21096
21097repeat:
21098 /* ":p" - full path/file_name */
21099 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21100 {
21101 has_fullname = 1;
21102
21103 valid |= VALID_PATH;
21104 *usedlen += 2;
21105
21106 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21107 if ((*fnamep)[0] == '~'
21108#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21109 && ((*fnamep)[1] == '/'
21110# ifdef BACKSLASH_IN_FILENAME
21111 || (*fnamep)[1] == '\\'
21112# endif
21113 || (*fnamep)[1] == NUL)
21114
21115#endif
21116 )
21117 {
21118 *fnamep = expand_env_save(*fnamep);
21119 vim_free(*bufp); /* free any allocated file name */
21120 *bufp = *fnamep;
21121 if (*fnamep == NULL)
21122 return -1;
21123 }
21124
21125 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021126 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 {
21128 if (vim_ispathsep(*p)
21129 && p[1] == '.'
21130 && (p[2] == NUL
21131 || vim_ispathsep(p[2])
21132 || (p[2] == '.'
21133 && (p[3] == NUL || vim_ispathsep(p[3])))))
21134 break;
21135 }
21136
21137 /* FullName_save() is slow, don't use it when not needed. */
21138 if (*p != NUL || !vim_isAbsName(*fnamep))
21139 {
21140 *fnamep = FullName_save(*fnamep, *p != NUL);
21141 vim_free(*bufp); /* free any allocated file name */
21142 *bufp = *fnamep;
21143 if (*fnamep == NULL)
21144 return -1;
21145 }
21146
21147 /* Append a path separator to a directory. */
21148 if (mch_isdir(*fnamep))
21149 {
21150 /* Make room for one or two extra characters. */
21151 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21152 vim_free(*bufp); /* free any allocated file name */
21153 *bufp = *fnamep;
21154 if (*fnamep == NULL)
21155 return -1;
21156 add_pathsep(*fnamep);
21157 }
21158 }
21159
21160 /* ":." - path relative to the current directory */
21161 /* ":~" - path relative to the home directory */
21162 /* ":8" - shortname path - postponed till after */
21163 while (src[*usedlen] == ':'
21164 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21165 {
21166 *usedlen += 2;
21167 if (c == '8')
21168 {
21169#ifdef WIN3264
21170 has_shortname = 1; /* Postpone this. */
21171#endif
21172 continue;
21173 }
21174 pbuf = NULL;
21175 /* Need full path first (use expand_env() to remove a "~/") */
21176 if (!has_fullname)
21177 {
21178 if (c == '.' && **fnamep == '~')
21179 p = pbuf = expand_env_save(*fnamep);
21180 else
21181 p = pbuf = FullName_save(*fnamep, FALSE);
21182 }
21183 else
21184 p = *fnamep;
21185
21186 has_fullname = 0;
21187
21188 if (p != NULL)
21189 {
21190 if (c == '.')
21191 {
21192 mch_dirname(dirname, MAXPATHL);
21193 s = shorten_fname(p, dirname);
21194 if (s != NULL)
21195 {
21196 *fnamep = s;
21197 if (pbuf != NULL)
21198 {
21199 vim_free(*bufp); /* free any allocated file name */
21200 *bufp = pbuf;
21201 pbuf = NULL;
21202 }
21203 }
21204 }
21205 else
21206 {
21207 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21208 /* Only replace it when it starts with '~' */
21209 if (*dirname == '~')
21210 {
21211 s = vim_strsave(dirname);
21212 if (s != NULL)
21213 {
21214 *fnamep = s;
21215 vim_free(*bufp);
21216 *bufp = s;
21217 }
21218 }
21219 }
21220 vim_free(pbuf);
21221 }
21222 }
21223
21224 tail = gettail(*fnamep);
21225 *fnamelen = (int)STRLEN(*fnamep);
21226
21227 /* ":h" - head, remove "/file_name", can be repeated */
21228 /* Don't remove the first "/" or "c:\" */
21229 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21230 {
21231 valid |= VALID_HEAD;
21232 *usedlen += 2;
21233 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021234 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 --tail;
21236 *fnamelen = (int)(tail - *fnamep);
21237#ifdef VMS
21238 if (*fnamelen > 0)
21239 *fnamelen += 1; /* the path separator is part of the path */
21240#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021241 while (tail > s && !after_pathsep(s, tail))
21242 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 }
21244
21245 /* ":8" - shortname */
21246 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21247 {
21248 *usedlen += 2;
21249#ifdef WIN3264
21250 has_shortname = 1;
21251#endif
21252 }
21253
21254#ifdef WIN3264
21255 /* Check shortname after we have done 'heads' and before we do 'tails'
21256 */
21257 if (has_shortname)
21258 {
21259 pbuf = NULL;
21260 /* Copy the string if it is shortened by :h */
21261 if (*fnamelen < (int)STRLEN(*fnamep))
21262 {
21263 p = vim_strnsave(*fnamep, *fnamelen);
21264 if (p == 0)
21265 return -1;
21266 vim_free(*bufp);
21267 *bufp = *fnamep = p;
21268 }
21269
21270 /* Split into two implementations - makes it easier. First is where
21271 * there isn't a full name already, second is where there is.
21272 */
21273 if (!has_fullname && !vim_isAbsName(*fnamep))
21274 {
21275 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21276 return -1;
21277 }
21278 else
21279 {
21280 int l;
21281
21282 /* Simple case, already have the full-name
21283 * Nearly always shorter, so try first time. */
21284 l = *fnamelen;
21285 if (!get_short_pathname(fnamep, bufp, &l))
21286 return -1;
21287
21288 if (l == 0)
21289 {
21290 /* Couldn't find the filename.. search the paths.
21291 */
21292 l = *fnamelen;
21293 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21294 return -1;
21295 }
21296 *fnamelen = l;
21297 }
21298 }
21299#endif /* WIN3264 */
21300
21301 /* ":t" - tail, just the basename */
21302 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21303 {
21304 *usedlen += 2;
21305 *fnamelen -= (int)(tail - *fnamep);
21306 *fnamep = tail;
21307 }
21308
21309 /* ":e" - extension, can be repeated */
21310 /* ":r" - root, without extension, can be repeated */
21311 while (src[*usedlen] == ':'
21312 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21313 {
21314 /* find a '.' in the tail:
21315 * - for second :e: before the current fname
21316 * - otherwise: The last '.'
21317 */
21318 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21319 s = *fnamep - 2;
21320 else
21321 s = *fnamep + *fnamelen - 1;
21322 for ( ; s > tail; --s)
21323 if (s[0] == '.')
21324 break;
21325 if (src[*usedlen + 1] == 'e') /* :e */
21326 {
21327 if (s > tail)
21328 {
21329 *fnamelen += (int)(*fnamep - (s + 1));
21330 *fnamep = s + 1;
21331#ifdef VMS
21332 /* cut version from the extension */
21333 s = *fnamep + *fnamelen - 1;
21334 for ( ; s > *fnamep; --s)
21335 if (s[0] == ';')
21336 break;
21337 if (s > *fnamep)
21338 *fnamelen = s - *fnamep;
21339#endif
21340 }
21341 else if (*fnamep <= tail)
21342 *fnamelen = 0;
21343 }
21344 else /* :r */
21345 {
21346 if (s > tail) /* remove one extension */
21347 *fnamelen = (int)(s - *fnamep);
21348 }
21349 *usedlen += 2;
21350 }
21351
21352 /* ":s?pat?foo?" - substitute */
21353 /* ":gs?pat?foo?" - global substitute */
21354 if (src[*usedlen] == ':'
21355 && (src[*usedlen + 1] == 's'
21356 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21357 {
21358 char_u *str;
21359 char_u *pat;
21360 char_u *sub;
21361 int sep;
21362 char_u *flags;
21363 int didit = FALSE;
21364
21365 flags = (char_u *)"";
21366 s = src + *usedlen + 2;
21367 if (src[*usedlen + 1] == 'g')
21368 {
21369 flags = (char_u *)"g";
21370 ++s;
21371 }
21372
21373 sep = *s++;
21374 if (sep)
21375 {
21376 /* find end of pattern */
21377 p = vim_strchr(s, sep);
21378 if (p != NULL)
21379 {
21380 pat = vim_strnsave(s, (int)(p - s));
21381 if (pat != NULL)
21382 {
21383 s = p + 1;
21384 /* find end of substitution */
21385 p = vim_strchr(s, sep);
21386 if (p != NULL)
21387 {
21388 sub = vim_strnsave(s, (int)(p - s));
21389 str = vim_strnsave(*fnamep, *fnamelen);
21390 if (sub != NULL && str != NULL)
21391 {
21392 *usedlen = (int)(p + 1 - src);
21393 s = do_string_sub(str, pat, sub, flags);
21394 if (s != NULL)
21395 {
21396 *fnamep = s;
21397 *fnamelen = (int)STRLEN(s);
21398 vim_free(*bufp);
21399 *bufp = s;
21400 didit = TRUE;
21401 }
21402 }
21403 vim_free(sub);
21404 vim_free(str);
21405 }
21406 vim_free(pat);
21407 }
21408 }
21409 /* after using ":s", repeat all the modifiers */
21410 if (didit)
21411 goto repeat;
21412 }
21413 }
21414
21415 return valid;
21416}
21417
21418/*
21419 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21420 * "flags" can be "g" to do a global substitute.
21421 * Returns an allocated string, NULL for error.
21422 */
21423 char_u *
21424do_string_sub(str, pat, sub, flags)
21425 char_u *str;
21426 char_u *pat;
21427 char_u *sub;
21428 char_u *flags;
21429{
21430 int sublen;
21431 regmatch_T regmatch;
21432 int i;
21433 int do_all;
21434 char_u *tail;
21435 garray_T ga;
21436 char_u *ret;
21437 char_u *save_cpo;
21438
21439 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21440 save_cpo = p_cpo;
21441 p_cpo = (char_u *)"";
21442
21443 ga_init2(&ga, 1, 200);
21444
21445 do_all = (flags[0] == 'g');
21446
21447 regmatch.rm_ic = p_ic;
21448 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21449 if (regmatch.regprog != NULL)
21450 {
21451 tail = str;
21452 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21453 {
21454 /*
21455 * Get some space for a temporary buffer to do the substitution
21456 * into. It will contain:
21457 * - The text up to where the match is.
21458 * - The substituted text.
21459 * - The text after the match.
21460 */
21461 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21462 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21463 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21464 {
21465 ga_clear(&ga);
21466 break;
21467 }
21468
21469 /* copy the text up to where the match is */
21470 i = (int)(regmatch.startp[0] - tail);
21471 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21472 /* add the substituted text */
21473 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21474 + ga.ga_len + i, TRUE, TRUE, FALSE);
21475 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 /* avoid getting stuck on a match with an empty string */
21477 if (tail == regmatch.endp[0])
21478 {
21479 if (*tail == NUL)
21480 break;
21481 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21482 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 }
21484 else
21485 {
21486 tail = regmatch.endp[0];
21487 if (*tail == NUL)
21488 break;
21489 }
21490 if (!do_all)
21491 break;
21492 }
21493
21494 if (ga.ga_data != NULL)
21495 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21496
21497 vim_free(regmatch.regprog);
21498 }
21499
21500 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21501 ga_clear(&ga);
21502 p_cpo = save_cpo;
21503
21504 return ret;
21505}
21506
21507#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */