blob: 3f231b6478d89479373da4266894def1f1b0c776 [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 */
Bram Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
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
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
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
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000035#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
36 be freed. */
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/*
Bram Moolenaar33570922005-01-25 22:26:29 +000039 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000041 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
44 */
Bram Moolenaar33570922005-01-25 22:26:29 +000045static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000047#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000048#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000049
50/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000051 * Structure returned by get_lval() and used by set_var_lval().
52 * For a plain name:
53 * "name" points to the variable name.
54 * "exp_name" is NULL.
55 * "tv" is NULL
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
59 * "tv" is NULL
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
70 * "newkey" is NULL
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000074 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000075 * "newkey" is the key for the new item.
76 */
77typedef struct lval_S
78{
79 char_u *ll_name; /* start of variable name (can be NULL) */
80 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000082 isn't NULL it's the Dict to which to add
83 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000084 listitem_T *ll_li; /* The list item or NULL. */
85 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 int ll_range; /* TRUE when a [i:j] range was used */
87 long ll_n1; /* First index for list */
88 long ll_n2; /* Second index for list range */
89 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000090 dict_T *ll_dict; /* The Dictionary or NULL */
91 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000092 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000093} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000094
Bram Moolenaar8c711452005-01-14 21:53:12 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000115/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119static dict_T globvardict;
120static dictitem_T globvars_var;
121#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
126 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000127static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128
129/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
132 */
133static int current_copyID = 0;
134
135/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000136 * Array to hold the hashtab with variables local to each sourced script.
137 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000139typedef struct
140{
141 dictitem_T sv_var;
142 dict_T sv_dict;
143} scriptvar_T;
144
145static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
146#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
147#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
149static int echo_attr = 0; /* attributes used for ":echo" */
150
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000151/* Values for trans_function_name() argument: */
152#define TFN_INT 1 /* internal function name OK */
153#define TFN_QUIET 2 /* no error messages */
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155/*
156 * Structure to hold info for a user function.
157 */
158typedef struct ufunc ufunc_T;
159
160struct ufunc
161{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000162 int uf_varargs; /* variable nr of arguments */
163 int uf_flags;
164 int uf_calls; /* nr of active calls */
165 garray_T uf_args; /* arguments */
166 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000167#ifdef FEAT_PROFILE
168 int uf_profiling; /* TRUE when func is being profiled */
169 /* profiling the function as a whole */
170 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000171 proftime_T uf_tm_total; /* time spent in function + children */
172 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000173 proftime_T uf_tm_children; /* time spent in children this call */
174 /* profiling the function per line */
175 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000176 proftime_T *uf_tml_total; /* time spent in a line + children */
177 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000178 proftime_T uf_tml_start; /* start time for current line */
179 proftime_T uf_tml_children; /* time spent in children for this line */
180 proftime_T uf_tml_wait; /* start wait time for current line */
181 int uf_tml_idx; /* index of line being timed; -1 if none */
182 int uf_tml_execed; /* line being timed was executed */
183#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000184 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000186 int uf_refcount; /* for numbered function: reference count */
187 char_u uf_name[1]; /* name of function (actually longer); can
188 start with <SNR>123_ (<SNR> is K_SPECIAL
189 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190};
191
192/* function flags */
193#define FC_ABORT 1 /* abort function on error */
194#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000195#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
197/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000198 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000200static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000202/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000203static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
204
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205/* list heads for garbage collection */
206static dict_T *first_dict = NULL; /* list of all dicts */
207static list_T *first_list = NULL; /* list of all lists */
208
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000209/* From user function to hashitem and back. */
210static ufunc_T dumuf;
211#define UF2HIKEY(fp) ((fp)->uf_name)
212#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
213#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
214
215#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
216#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Bram Moolenaar33570922005-01-25 22:26:29 +0000218#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
219#define VAR_SHORT_LEN 20 /* short variable name length */
220#define FIXVAR_CNT 12 /* number of fixed variables */
221
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000223typedef struct funccall_S funccall_T;
224
225struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226{
227 ufunc_T *func; /* function being called */
228 int linenr; /* next line to be executed */
229 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000230 struct /* fixed variables for arguments */
231 {
232 dictitem_T var; /* variable (without room for name) */
233 char_u room[VAR_SHORT_LEN]; /* room for the name */
234 } fixvar[FIXVAR_CNT];
235 dict_T l_vars; /* l: local function variables */
236 dictitem_T l_vars_var; /* variable for l: scope */
237 dict_T l_avars; /* a: argument variables */
238 dictitem_T l_avars_var; /* variable for a: scope */
239 list_T l_varlist; /* list for a:000 */
240 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
241 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 linenr_T breakpoint; /* next line with breakpoint or zero */
243 int dbg_tick; /* debug_tick when breakpoint was set */
244 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000245#ifdef FEAT_PROFILE
246 proftime_T prof_child; /* time spent in a child */
247#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000248 funccall_T *caller; /* calling function or NULL */
249};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250
251/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000252 * Info used by a ":for" loop.
253 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000255{
256 int fi_semicolon; /* TRUE if ending in '; var]' */
257 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000258 listwatch_T fi_lw; /* keep an eye on the item used. */
259 list_T *fi_list; /* list being used */
260} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000263 * Struct used by trans_function_name()
264 */
265typedef struct
266{
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000268 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000269 dictitem_T *fd_di; /* Dictionary item used */
270} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000271
Bram Moolenaara7043832005-01-21 11:56:39 +0000272
273/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 * Array to hold the value of v: variables.
275 * The value is in a dictitem, so that it can also be used in the v: scope.
276 * The reason to use this table anyway is for very quick access to the
277 * variables with the VV_ defines.
278 */
279#include "version.h"
280
281/* values for vv_flags: */
282#define VV_COMPAT 1 /* compatible, also used without "v:" */
283#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000284#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000286#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000287
288static struct vimvar
289{
290 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000291 dictitem_T vv_di; /* value and name for key */
292 char vv_filler[16]; /* space for LONGEST name below!!! */
293 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
294} vimvars[VV_LEN] =
295{
296 /*
297 * The order here must match the VV_ defines in vim.h!
298 * Initializing a union does not work, leave tv.vval empty to get zero's.
299 */
300 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
301 {VV_NAME("count1", VAR_NUMBER), VV_RO},
302 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
303 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
304 {VV_NAME("warningmsg", VAR_STRING), 0},
305 {VV_NAME("statusmsg", VAR_STRING), 0},
306 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
307 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
308 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
310 {VV_NAME("termresponse", VAR_STRING), VV_RO},
311 {VV_NAME("fname", VAR_STRING), VV_RO},
312 {VV_NAME("lang", VAR_STRING), VV_RO},
313 {VV_NAME("lc_time", VAR_STRING), VV_RO},
314 {VV_NAME("ctype", VAR_STRING), VV_RO},
315 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
316 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
317 {VV_NAME("fname_in", VAR_STRING), VV_RO},
318 {VV_NAME("fname_out", VAR_STRING), VV_RO},
319 {VV_NAME("fname_new", VAR_STRING), VV_RO},
320 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
321 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
322 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
324 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
325 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("progname", VAR_STRING), VV_RO},
327 {VV_NAME("servername", VAR_STRING), VV_RO},
328 {VV_NAME("dying", VAR_NUMBER), VV_RO},
329 {VV_NAME("exception", VAR_STRING), VV_RO},
330 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
331 {VV_NAME("register", VAR_STRING), VV_RO},
332 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
333 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000334 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
335 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000336 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000337 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
338 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000339 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
341 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
342 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000344 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000345 {VV_NAME("swapname", VAR_STRING), VV_RO},
346 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000347 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000348 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000349 {VV_NAME("mouse_win", VAR_NUMBER), 0},
350 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
351 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000352 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000353 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000354 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000355};
356
357/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358#define vv_type vv_di.di_tv.v_type
359#define vv_nr vv_di.di_tv.vval.v_number
360#define vv_float vv_di.di_tv.vval.v_float
361#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000362#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000364
365/*
366 * The v: variables are stored in dictionary "vimvardict".
367 * "vimvars_var" is the variable that is used for the "l:" scope.
368 */
369static dict_T vimvardict;
370static dictitem_T vimvars_var;
371#define vimvarht vimvardict.dv_hashtab
372
Bram Moolenaara40058a2005-07-11 22:42:07 +0000373static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
374static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
375#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
376static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
377#endif
378static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
379static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
380static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000381static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
382static void list_glob_vars __ARGS((int *first));
383static void list_buf_vars __ARGS((int *first));
384static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000385#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_vim_vars __ARGS((int *first));
389static void list_script_vars __ARGS((int *first));
390static void list_func_vars __ARGS((int *first));
391static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000392static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
393static int check_changedtick __ARGS((char_u *arg));
394static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
395static void clear_lval __ARGS((lval_T *lp));
396static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
397static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
398static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
399static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static listitem_T *listitem_alloc __ARGS((void));
423static void listitem_free __ARGS((listitem_T *item));
424static void listitem_remove __ARGS((list_T *l, listitem_T *item));
425static long list_len __ARGS((list_T *l));
426static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
427static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
428static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000430static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static void list_append __ARGS((list_T *l, listitem_T *item));
433static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000434static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
436static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
437static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
441static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000442static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
443static void set_ref_in_list __ARGS((list_T *l, int copyID));
444static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000446static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static dictitem_T *dictitem_alloc __ARGS((char_u *key));
448static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
449static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
450static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000451static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int dict_add __ARGS((dict_T *d, dictitem_T *item));
453static long dict_len __ARGS((dict_T *d));
454static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000455static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
458static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static int string2float __ARGS((char_u *text, float_T *value));
462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
464static int find_internal_func __ARGS((char_u *name));
465static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
466static 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));
467static 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 +0000468static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000469static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
472static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
473#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
480static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
481#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#ifdef FEAT_FLOAT
494static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
495#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000496static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000499static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000501#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000502static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000503static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
509static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
510#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
514static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000527static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#ifdef FEAT_FLOAT
534static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000537static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000546static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000548static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000554static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000562static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000563static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000564static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000565static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000568static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000576static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000590static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
609static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
610#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000615static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000616static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000617static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000619static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000623#ifdef vim_mkdir
624static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000629static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000630#ifdef FEAT_FLOAT
631static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
632#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000634static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000635static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000637static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000638static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000650#ifdef FEAT_FLOAT
651static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000654static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000663static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000664static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000665static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000666static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000668static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000670static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000672#ifdef FEAT_FLOAT
673static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
674#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000676static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000677static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000680#ifdef FEAT_FLOAT
681static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
683#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000684static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685#ifdef HAVE_STRFTIME
686static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
687#endif
688static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000699static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000701static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000702static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000703static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000704static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000705static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000707static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
713#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000724static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000727static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000729static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000730static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000731static int get_env_len __ARGS((char_u **arg));
732static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000733static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000734static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
735#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
736#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
737 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000738static 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 +0000739static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000740static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000741static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
742static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static typval_T *alloc_tv __ARGS((void));
744static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void init_tv __ARGS((typval_T *varp));
746static long get_tv_number __ARGS((typval_T *varp));
747static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000748static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static char_u *get_tv_string __ARGS((typval_T *varp));
750static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000751static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000753static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
755static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
756static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000757static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
758static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
760static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000761static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000762static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000764static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
766static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
767static int eval_fname_script __ARGS((char_u *p));
768static int eval_fname_sid __ARGS((char_u *p));
769static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static ufunc_T *find_func __ARGS((char_u *name));
771static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000772static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773#ifdef FEAT_PROFILE
774static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000775static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
776static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
777static int
778# ifdef __BORLANDC__
779 _RTLENTRYF
780# endif
781 prof_total_cmp __ARGS((const void *s1, const void *s2));
782static int
783# ifdef __BORLANDC__
784 _RTLENTRYF
785# endif
786 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000788static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000789static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000790static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static void func_free __ARGS((ufunc_T *fp));
792static void func_unref __ARGS((char_u *name));
793static void func_ref __ARGS((char_u *name));
794static 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));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000795static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
796static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000798static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
799static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000800static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000801static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000802static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000804/* Character used as separated in autoload function/variable names. */
805#define AUTOLOAD_CHAR '#'
806
Bram Moolenaar33570922005-01-25 22:26:29 +0000807/*
808 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000809 */
810 void
811eval_init()
812{
Bram Moolenaar33570922005-01-25 22:26:29 +0000813 int i;
814 struct vimvar *p;
815
816 init_var_dict(&globvardict, &globvars_var);
817 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000818 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000819 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000820
821 for (i = 0; i < VV_LEN; ++i)
822 {
823 p = &vimvars[i];
824 STRCPY(p->vv_di.di_key, p->vv_name);
825 if (p->vv_flags & VV_RO)
826 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
827 else if (p->vv_flags & VV_RO_SBX)
828 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
829 else
830 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000831
832 /* add to v: scope dict, unless the value is not always available */
833 if (p->vv_type != VAR_UNKNOWN)
834 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000835 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000836 /* add to compat scope dict */
837 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000838 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000839 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000840}
841
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000842#if defined(EXITFREE) || defined(PROTO)
843 void
844eval_clear()
845{
846 int i;
847 struct vimvar *p;
848
849 for (i = 0; i < VV_LEN; ++i)
850 {
851 p = &vimvars[i];
852 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000853 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000854 vim_free(p->vv_str);
855 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000856 }
857 else if (p->vv_di.di_tv.v_type == VAR_LIST)
858 {
859 list_unref(p->vv_list);
860 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000861 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000862 }
863 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000864 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000865 hash_clear(&compat_hashtab);
866
867 /* script-local variables */
868 for (i = 1; i <= ga_scripts.ga_len; ++i)
869 vars_clear(&SCRIPT_VARS(i));
870 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000871 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000872
873 /* global variables */
874 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000875
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000876 /* autoloaded script names */
877 ga_clear_strings(&ga_loaded);
878
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000879 /* unreferenced lists and dicts */
880 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000881
882 /* functions */
883 free_all_functions();
884 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000885}
886#endif
887
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 * Return the name of the executed function.
890 */
891 char_u *
892func_name(cookie)
893 void *cookie;
894{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000895 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896}
897
898/*
899 * Return the address holding the next breakpoint line for a funccall cookie.
900 */
901 linenr_T *
902func_breakpoint(cookie)
903 void *cookie;
904{
Bram Moolenaar33570922005-01-25 22:26:29 +0000905 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906}
907
908/*
909 * Return the address holding the debug tick for a funccall cookie.
910 */
911 int *
912func_dbg_tick(cookie)
913 void *cookie;
914{
Bram Moolenaar33570922005-01-25 22:26:29 +0000915 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916}
917
918/*
919 * Return the nesting level for a funccall cookie.
920 */
921 int
922func_level(cookie)
923 void *cookie;
924{
Bram Moolenaar33570922005-01-25 22:26:29 +0000925 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926}
927
928/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000929funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000931/* pointer to list of previously used funccal, still around because some
932 * item in it is still being used. */
933funccall_T *previous_funccal = NULL;
934
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935/*
936 * Return TRUE when a function was ended by a ":return" command.
937 */
938 int
939current_func_returned()
940{
941 return current_funccal->returned;
942}
943
944
945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 * Set an internal variable to a string value. Creates the variable if it does
947 * not already exist.
948 */
949 void
950set_internal_string_var(name, value)
951 char_u *name;
952 char_u *value;
953{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000954 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 val = vim_strsave(value);
958 if (val != NULL)
959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000960 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000961 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000963 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966 }
967}
968
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000969static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000970static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000971static char_u *redir_endp = NULL;
972static char_u *redir_varname = NULL;
973
974/*
975 * Start recording command output to a variable
976 * Returns OK if successfully completed the setup. FAIL otherwise.
977 */
978 int
979var_redir_start(name, append)
980 char_u *name;
981 int append; /* append to an existing variable */
982{
983 int save_emsg;
984 int err;
985 typval_T tv;
986
987 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000988 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000989 {
990 EMSG(_(e_invarg));
991 return FAIL;
992 }
993
994 redir_varname = vim_strsave(name);
995 if (redir_varname == NULL)
996 return FAIL;
997
998 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
999 if (redir_lval == NULL)
1000 {
1001 var_redir_stop();
1002 return FAIL;
1003 }
1004
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001005 /* The output is stored in growarray "redir_ga" until redirection ends. */
1006 ga_init2(&redir_ga, (int)sizeof(char), 500);
1007
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001008 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001009 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1010 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1012 {
1013 if (redir_endp != NULL && *redir_endp != NUL)
1014 /* Trailing characters are present after the variable name */
1015 EMSG(_(e_trailing));
1016 else
1017 EMSG(_(e_invarg));
1018 var_redir_stop();
1019 return FAIL;
1020 }
1021
1022 /* check if we can write to the variable: set it to or append an empty
1023 * string */
1024 save_emsg = did_emsg;
1025 did_emsg = FALSE;
1026 tv.v_type = VAR_STRING;
1027 tv.vval.v_string = (char_u *)"";
1028 if (append)
1029 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1030 else
1031 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1032 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001033 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 if (err)
1035 {
1036 var_redir_stop();
1037 return FAIL;
1038 }
1039 if (redir_lval->ll_newkey != NULL)
1040 {
1041 /* Dictionary item was created, don't do it again. */
1042 vim_free(redir_lval->ll_newkey);
1043 redir_lval->ll_newkey = NULL;
1044 }
1045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001050 * Append "value[value_len]" to the variable set by var_redir_start().
1051 * The actual appending is postponed until redirection ends, because the value
1052 * appended may in fact be the string we write to, changing it may cause freed
1053 * memory to be used:
1054 * :redir => foo
1055 * :let foo
1056 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 */
1058 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001059var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001061 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001063 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064
1065 if (redir_lval == NULL)
1066 return;
1067
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001068 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001069 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001071 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001073 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001074 {
1075 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001076 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001077 }
1078 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080}
1081
1082/*
1083 * Stop redirecting command output to a variable.
1084 */
1085 void
1086var_redir_stop()
1087{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001088 typval_T tv;
1089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (redir_lval != NULL)
1091 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001092 /* Append the trailing NUL. */
1093 ga_append(&redir_ga, NUL);
1094
1095 /* Assign the text to the variable. */
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = redir_ga.ga_data;
1098 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1099 vim_free(tv.vval.v_string);
1100
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 clear_lval(redir_lval);
1102 vim_free(redir_lval);
1103 redir_lval = NULL;
1104 }
1105 vim_free(redir_varname);
1106 redir_varname = NULL;
1107}
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109# if defined(FEAT_MBYTE) || defined(PROTO)
1110 int
1111eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1112 char_u *enc_from;
1113 char_u *enc_to;
1114 char_u *fname_from;
1115 char_u *fname_to;
1116{
1117 int err = FALSE;
1118
1119 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1120 set_vim_var_string(VV_CC_TO, enc_to, -1);
1121 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1122 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1123 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1124 err = TRUE;
1125 set_vim_var_string(VV_CC_FROM, NULL, -1);
1126 set_vim_var_string(VV_CC_TO, NULL, -1);
1127 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1128 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1129
1130 if (err)
1131 return FAIL;
1132 return OK;
1133}
1134# endif
1135
1136# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1137 int
1138eval_printexpr(fname, args)
1139 char_u *fname;
1140 char_u *args;
1141{
1142 int err = FALSE;
1143
1144 set_vim_var_string(VV_FNAME_IN, fname, -1);
1145 set_vim_var_string(VV_CMDARG, args, -1);
1146 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1147 err = TRUE;
1148 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1149 set_vim_var_string(VV_CMDARG, NULL, -1);
1150
1151 if (err)
1152 {
1153 mch_remove(fname);
1154 return FAIL;
1155 }
1156 return OK;
1157}
1158# endif
1159
1160# if defined(FEAT_DIFF) || defined(PROTO)
1161 void
1162eval_diff(origfile, newfile, outfile)
1163 char_u *origfile;
1164 char_u *newfile;
1165 char_u *outfile;
1166{
1167 int err = FALSE;
1168
1169 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1170 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1171 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1172 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1173 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1174 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1175 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1176}
1177
1178 void
1179eval_patch(origfile, difffile, outfile)
1180 char_u *origfile;
1181 char_u *difffile;
1182 char_u *outfile;
1183{
1184 int err;
1185
1186 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1187 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1188 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1189 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1190 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1191 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1192 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1193}
1194# endif
1195
1196/*
1197 * Top level evaluation function, returning a boolean.
1198 * Sets "error" to TRUE if there was an error.
1199 * Return TRUE or FALSE.
1200 */
1201 int
1202eval_to_bool(arg, error, nextcmd, skip)
1203 char_u *arg;
1204 int *error;
1205 char_u **nextcmd;
1206 int skip; /* only parse, don't execute */
1207{
Bram Moolenaar33570922005-01-25 22:26:29 +00001208 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 int retval = FALSE;
1210
1211 if (skip)
1212 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 else
1216 {
1217 *error = FALSE;
1218 if (!skip)
1219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001220 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001221 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223 }
1224 if (skip)
1225 --emsg_skip;
1226
1227 return retval;
1228}
1229
1230/*
1231 * Top level evaluation function, returning a string. If "skip" is TRUE,
1232 * only parsing to "nextcmd" is done, without reporting errors. Return
1233 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1234 */
1235 char_u *
1236eval_to_string_skip(arg, nextcmd, skip)
1237 char_u *arg;
1238 char_u **nextcmd;
1239 int skip; /* only parse, don't execute */
1240{
Bram Moolenaar33570922005-01-25 22:26:29 +00001241 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 char_u *retval;
1243
1244 if (skip)
1245 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001246 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 retval = NULL;
1248 else
1249 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001250 retval = vim_strsave(get_tv_string(&tv));
1251 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253 if (skip)
1254 --emsg_skip;
1255
1256 return retval;
1257}
1258
1259/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001260 * Skip over an expression at "*pp".
1261 * Return FAIL for an error, OK otherwise.
1262 */
1263 int
1264skip_expr(pp)
1265 char_u **pp;
1266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001268
1269 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001271}
1272
1273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001275 * When "convert" is TRUE convert a List into a sequence of lines and convert
1276 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 * Return pointer to allocated memory, or NULL for failure.
1278 */
1279 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001280eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 char_u *arg;
1282 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001283 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
Bram Moolenaar33570922005-01-25 22:26:29 +00001285 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001287 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001288#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001289 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001292 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 retval = NULL;
1294 else
1295 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001296 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001297 {
1298 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001299 if (tv.vval.v_list != NULL)
1300 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001301 ga_append(&ga, NUL);
1302 retval = (char_u *)ga.ga_data;
1303 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001304#ifdef FEAT_FLOAT
1305 else if (convert && tv.v_type == VAR_FLOAT)
1306 {
1307 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1308 retval = vim_strsave(numbuf);
1309 }
1310#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001311 else
1312 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315
1316 return retval;
1317}
1318
1319/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001320 * Call eval_to_string() without using current local variables and using
1321 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 */
1323 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001324eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 char_u *arg;
1326 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001327 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 char_u *retval;
1330 void *save_funccalp;
1331
1332 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001333 if (use_sandbox)
1334 ++sandbox;
1335 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001336 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001337 if (use_sandbox)
1338 --sandbox;
1339 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 restore_funccal(save_funccalp);
1341 return retval;
1342}
1343
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344/*
1345 * Top level evaluation function, returning a number.
1346 * Evaluates "expr" silently.
1347 * Returns -1 for an error.
1348 */
1349 int
1350eval_to_number(expr)
1351 char_u *expr;
1352{
Bram Moolenaar33570922005-01-25 22:26:29 +00001353 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001355 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
1357 ++emsg_off;
1358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 retval = -1;
1361 else
1362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001363 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001364 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
1366 --emsg_off;
1367
1368 return retval;
1369}
1370
Bram Moolenaara40058a2005-07-11 22:42:07 +00001371/*
1372 * Prepare v: variable "idx" to be used.
1373 * Save the current typeval in "save_tv".
1374 * When not used yet add the variable to the v: hashtable.
1375 */
1376 static void
1377prepare_vimvar(idx, save_tv)
1378 int idx;
1379 typval_T *save_tv;
1380{
1381 *save_tv = vimvars[idx].vv_tv;
1382 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1383 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1384}
1385
1386/*
1387 * Restore v: variable "idx" to typeval "save_tv".
1388 * When no longer defined, remove the variable from the v: hashtable.
1389 */
1390 static void
1391restore_vimvar(idx, save_tv)
1392 int idx;
1393 typval_T *save_tv;
1394{
1395 hashitem_T *hi;
1396
Bram Moolenaara40058a2005-07-11 22:42:07 +00001397 vimvars[idx].vv_tv = *save_tv;
1398 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1399 {
1400 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1401 if (HASHITEM_EMPTY(hi))
1402 EMSG2(_(e_intern2), "restore_vimvar()");
1403 else
1404 hash_remove(&vimvarht, hi);
1405 }
1406}
1407
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001408#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001409/*
1410 * Evaluate an expression to a list with suggestions.
1411 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001412 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001413 */
1414 list_T *
1415eval_spell_expr(badword, expr)
1416 char_u *badword;
1417 char_u *expr;
1418{
1419 typval_T save_val;
1420 typval_T rettv;
1421 list_T *list = NULL;
1422 char_u *p = skipwhite(expr);
1423
1424 /* Set "v:val" to the bad word. */
1425 prepare_vimvar(VV_VAL, &save_val);
1426 vimvars[VV_VAL].vv_type = VAR_STRING;
1427 vimvars[VV_VAL].vv_str = badword;
1428 if (p_verbose == 0)
1429 ++emsg_off;
1430
1431 if (eval1(&p, &rettv, TRUE) == OK)
1432 {
1433 if (rettv.v_type != VAR_LIST)
1434 clear_tv(&rettv);
1435 else
1436 list = rettv.vval.v_list;
1437 }
1438
1439 if (p_verbose == 0)
1440 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001441 restore_vimvar(VV_VAL, &save_val);
1442
1443 return list;
1444}
1445
1446/*
1447 * "list" is supposed to contain two items: a word and a number. Return the
1448 * word in "pp" and the number as the return value.
1449 * Return -1 if anything isn't right.
1450 * Used to get the good word and score from the eval_spell_expr() result.
1451 */
1452 int
1453get_spellword(list, pp)
1454 list_T *list;
1455 char_u **pp;
1456{
1457 listitem_T *li;
1458
1459 li = list->lv_first;
1460 if (li == NULL)
1461 return -1;
1462 *pp = get_tv_string(&li->li_tv);
1463
1464 li = li->li_next;
1465 if (li == NULL)
1466 return -1;
1467 return get_tv_number(&li->li_tv);
1468}
1469#endif
1470
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001471/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001472 * Top level evaluation function.
1473 * Returns an allocated typval_T with the result.
1474 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001475 */
1476 typval_T *
1477eval_expr(arg, nextcmd)
1478 char_u *arg;
1479 char_u **nextcmd;
1480{
1481 typval_T *tv;
1482
1483 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001484 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001485 {
1486 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001487 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001488 }
1489
1490 return tv;
1491}
1492
1493
Bram Moolenaar4f688582007-07-24 12:34:30 +00001494#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1495 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001498 * Uses argv[argc] for the function arguments. Only Number and String
1499 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001500 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001502 static int
1503call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 char_u *func;
1505 int argc;
1506 char_u **argv;
1507 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
Bram Moolenaar33570922005-01-25 22:26:29 +00001510 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 long n;
1512 int len;
1513 int i;
1514 int doesrange;
1515 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001516 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001518 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001520 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
1522 for (i = 0; i < argc; i++)
1523 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001524 /* Pass a NULL or empty argument as an empty string */
1525 if (argv[i] == NULL || *argv[i] == NUL)
1526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001527 argvars[i].v_type = VAR_STRING;
1528 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001529 continue;
1530 }
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 /* Recognize a number argument, the others must be strings. */
1533 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1534 if (len != 0 && len == (int)STRLEN(argv[i]))
1535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001536 argvars[i].v_type = VAR_NUMBER;
1537 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 }
1539 else
1540 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001541 argvars[i].v_type = VAR_STRING;
1542 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
1544 }
1545
1546 if (safe)
1547 {
1548 save_funccalp = save_funccal();
1549 ++sandbox;
1550 }
1551
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001552 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1553 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (safe)
1557 {
1558 --sandbox;
1559 restore_funccal(save_funccalp);
1560 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 vim_free(argvars);
1562
1563 if (ret == FAIL)
1564 clear_tv(rettv);
1565
1566 return ret;
1567}
1568
Bram Moolenaar4f688582007-07-24 12:34:30 +00001569# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001571 * Call vimL function "func" and return the result as a string.
1572 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 * Uses argv[argc] for the function arguments.
1574 */
1575 void *
1576call_func_retstr(func, argc, argv, safe)
1577 char_u *func;
1578 int argc;
1579 char_u **argv;
1580 int safe; /* use the sandbox */
1581{
1582 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001583 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584
1585 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1586 return NULL;
1587
1588 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 return retval;
1591}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001592# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593
Bram Moolenaar4f688582007-07-24 12:34:30 +00001594# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001596 * Call vimL function "func" and return the result as a number.
1597 * Returns -1 when calling the function fails.
1598 * Uses argv[argc] for the function arguments.
1599 */
1600 long
1601call_func_retnr(func, argc, argv, safe)
1602 char_u *func;
1603 int argc;
1604 char_u **argv;
1605 int safe; /* use the sandbox */
1606{
1607 typval_T rettv;
1608 long retval;
1609
1610 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1611 return -1;
1612
1613 retval = get_tv_number_chk(&rettv, NULL);
1614 clear_tv(&rettv);
1615 return retval;
1616}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001617# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001618
1619/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001620 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001622 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 */
1624 void *
1625call_func_retlist(func, argc, argv, safe)
1626 char_u *func;
1627 int argc;
1628 char_u **argv;
1629 int safe; /* use the sandbox */
1630{
1631 typval_T rettv;
1632
1633 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1634 return NULL;
1635
1636 if (rettv.v_type != VAR_LIST)
1637 {
1638 clear_tv(&rettv);
1639 return NULL;
1640 }
1641
1642 return rettv.vval.v_list;
1643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
1645
Bram Moolenaar4f688582007-07-24 12:34:30 +00001646
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647/*
1648 * Save the current function call pointer, and set it to NULL.
1649 * Used when executing autocommands and for ":source".
1650 */
1651 void *
1652save_funccal()
1653{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001654 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 current_funccal = NULL;
1657 return (void *)fc;
1658}
1659
1660 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001661restore_funccal(vfc)
1662 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001664 funccall_T *fc = (funccall_T *)vfc;
1665
1666 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668
Bram Moolenaar05159a02005-02-26 23:04:13 +00001669#if defined(FEAT_PROFILE) || defined(PROTO)
1670/*
1671 * Prepare profiling for entering a child or something else that is not
1672 * counted for the script/function itself.
1673 * Should always be called in pair with prof_child_exit().
1674 */
1675 void
1676prof_child_enter(tm)
1677 proftime_T *tm; /* place to store waittime */
1678{
1679 funccall_T *fc = current_funccal;
1680
1681 if (fc != NULL && fc->func->uf_profiling)
1682 profile_start(&fc->prof_child);
1683 script_prof_save(tm);
1684}
1685
1686/*
1687 * Take care of time spent in a child.
1688 * Should always be called after prof_child_enter().
1689 */
1690 void
1691prof_child_exit(tm)
1692 proftime_T *tm; /* where waittime was stored */
1693{
1694 funccall_T *fc = current_funccal;
1695
1696 if (fc != NULL && fc->func->uf_profiling)
1697 {
1698 profile_end(&fc->prof_child);
1699 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1700 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1701 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1702 }
1703 script_prof_restore(tm);
1704}
1705#endif
1706
1707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#ifdef FEAT_FOLDING
1709/*
1710 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1711 * it in "*cp". Doesn't give error messages.
1712 */
1713 int
1714eval_foldexpr(arg, cp)
1715 char_u *arg;
1716 int *cp;
1717{
Bram Moolenaar33570922005-01-25 22:26:29 +00001718 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 int retval;
1720 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001721 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1722 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
1724 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001725 if (use_sandbox)
1726 ++sandbox;
1727 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 retval = 0;
1731 else
1732 {
1733 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 if (tv.v_type == VAR_NUMBER)
1735 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001736 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 retval = 0;
1738 else
1739 {
1740 /* If the result is a string, check if there is a non-digit before
1741 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (!VIM_ISDIGIT(*s) && *s != '-')
1744 *cp = *s++;
1745 retval = atol((char *)s);
1746 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001750 if (use_sandbox)
1751 --sandbox;
1752 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 return retval;
1755}
1756#endif
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 * ":let" list all variable values
1760 * ":let var1 var2" list variable values
1761 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 * ":let var += expr" assignment command.
1763 * ":let var -= expr" assignment command.
1764 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 */
1767 void
1768ex_let(eap)
1769 exarg_T *eap;
1770{
1771 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001773 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 int var_count = 0;
1776 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001778 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001779 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaardb552d602006-03-23 22:59:57 +00001781 argend = skip_var_list(arg, &var_count, &semicolon);
1782 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001784 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1785 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001786 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001789 /*
1790 * ":let" without "=": list variables
1791 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 if (*arg == '[')
1793 EMSG(_(e_invarg));
1794 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001796 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001800 list_glob_vars(&first);
1801 list_buf_vars(&first);
1802 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001803#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001804 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001805#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001806 list_script_vars(&first);
1807 list_func_vars(&first);
1808 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 eap->nextcmd = check_nextcmd(arg);
1811 }
1812 else
1813 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001814 op[0] = '=';
1815 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001816 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001817 {
1818 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1819 op[0] = expr[-1]; /* +=, -= or .= */
1820 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (eap->skip)
1824 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (eap->skip)
1827 {
1828 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 --emsg_skip;
1831 }
1832 else if (i != FAIL)
1833 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838 }
1839}
1840
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841/*
1842 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1843 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 * When "nextchars" is not NULL it points to a string with characters that
1845 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1846 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 * Returns OK or FAIL;
1848 */
1849 static int
1850ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1851 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001852 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 int copy; /* copy values from "tv", don't move */
1854 int semicolon; /* from skip_var_list() */
1855 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001856 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857{
1858 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001859 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 listitem_T *item;
1862 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863
1864 if (*arg != '[')
1865 {
1866 /*
1867 * ":let var = expr" or ":for var in list"
1868 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001869 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 return FAIL;
1871 return OK;
1872 }
1873
1874 /*
1875 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1876 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001877 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 {
1879 EMSG(_(e_listreq));
1880 return FAIL;
1881 }
1882
1883 i = list_len(l);
1884 if (semicolon == 0 && var_count < i)
1885 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001886 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 return FAIL;
1888 }
1889 if (var_count - semicolon > i)
1890 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001891 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 return FAIL;
1893 }
1894
1895 item = l->lv_first;
1896 while (*arg != ']')
1897 {
1898 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 item = item->li_next;
1901 if (arg == NULL)
1902 return FAIL;
1903
1904 arg = skipwhite(arg);
1905 if (*arg == ';')
1906 {
1907 /* Put the rest of the list (may be empty) in the var after ';'.
1908 * Create a new list for this. */
1909 l = list_alloc();
1910 if (l == NULL)
1911 return FAIL;
1912 while (item != NULL)
1913 {
1914 list_append_tv(l, &item->li_tv);
1915 item = item->li_next;
1916 }
1917
1918 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001919 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 ltv.vval.v_list = l;
1921 l->lv_refcount = 1;
1922
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1924 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 clear_tv(&ltv);
1926 if (arg == NULL)
1927 return FAIL;
1928 break;
1929 }
1930 else if (*arg != ',' && *arg != ']')
1931 {
1932 EMSG2(_(e_intern2), "ex_let_vars()");
1933 return FAIL;
1934 }
1935 }
1936
1937 return OK;
1938}
1939
1940/*
1941 * Skip over assignable variable "var" or list of variables "[var, var]".
1942 * Used for ":let varvar = expr" and ":for varvar in expr".
1943 * For "[var, var]" increment "*var_count" for each variable.
1944 * for "[var, var; var]" set "semicolon".
1945 * Return NULL for an error.
1946 */
1947 static char_u *
1948skip_var_list(arg, var_count, semicolon)
1949 char_u *arg;
1950 int *var_count;
1951 int *semicolon;
1952{
1953 char_u *p, *s;
1954
1955 if (*arg == '[')
1956 {
1957 /* "[var, var]": find the matching ']'. */
1958 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001959 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 {
1961 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1962 s = skip_var_one(p);
1963 if (s == p)
1964 {
1965 EMSG2(_(e_invarg2), p);
1966 return NULL;
1967 }
1968 ++*var_count;
1969
1970 p = skipwhite(s);
1971 if (*p == ']')
1972 break;
1973 else if (*p == ';')
1974 {
1975 if (*semicolon == 1)
1976 {
1977 EMSG(_("Double ; in list of variables"));
1978 return NULL;
1979 }
1980 *semicolon = 1;
1981 }
1982 else if (*p != ',')
1983 {
1984 EMSG2(_(e_invarg2), p);
1985 return NULL;
1986 }
1987 }
1988 return p + 1;
1989 }
1990 else
1991 return skip_var_one(arg);
1992}
1993
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001994/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001995 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001996 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001997 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 static char_u *
1999skip_var_one(arg)
2000 char_u *arg;
2001{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002002 if (*arg == '@' && arg[1] != NUL)
2003 return arg + 2;
2004 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2005 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006}
2007
Bram Moolenaara7043832005-01-21 11:56:39 +00002008/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002009 * List variables for hashtab "ht" with prefix "prefix".
2010 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002011 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002013list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002014 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002015 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002016 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002017 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002018{
Bram Moolenaar33570922005-01-25 22:26:29 +00002019 hashitem_T *hi;
2020 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002021 int todo;
2022
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002023 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002024 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2025 {
2026 if (!HASHITEM_EMPTY(hi))
2027 {
2028 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002029 di = HI2DI(hi);
2030 if (empty || di->di_tv.v_type != VAR_STRING
2031 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002032 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002033 }
2034 }
2035}
2036
2037/*
2038 * List global variables.
2039 */
2040 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002041list_glob_vars(first)
2042 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002043{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002044 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002045}
2046
2047/*
2048 * List buffer variables.
2049 */
2050 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002051list_buf_vars(first)
2052 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002053{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054 char_u numbuf[NUMBUFLEN];
2055
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002056 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2057 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058
2059 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002060 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2061 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002062}
2063
2064/*
2065 * List window variables.
2066 */
2067 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002068list_win_vars(first)
2069 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002070{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2072 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002073}
2074
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002075#ifdef FEAT_WINDOWS
2076/*
2077 * List tab page variables.
2078 */
2079 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002080list_tab_vars(first)
2081 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002082{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002083 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2084 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002085}
2086#endif
2087
Bram Moolenaara7043832005-01-21 11:56:39 +00002088/*
2089 * List Vim variables.
2090 */
2091 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092list_vim_vars(first)
2093 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096}
2097
2098/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002099 * List script-local variables, if there is a script.
2100 */
2101 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002102list_script_vars(first)
2103 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002104{
2105 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2107 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002108}
2109
2110/*
2111 * List function variables, if there is a function.
2112 */
2113 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114list_func_vars(first)
2115 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002116{
2117 if (current_funccal != NULL)
2118 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002120}
2121
2122/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 * List variables in "arg".
2124 */
2125 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127 exarg_T *eap;
2128 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130{
2131 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002134 char_u *name_start;
2135 char_u *arg_subsc;
2136 char_u *tofree;
2137 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138
2139 while (!ends_excmd(*arg) && !got_int)
2140 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002141 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002143 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2145 {
2146 emsg_severe = TRUE;
2147 EMSG(_(e_trailing));
2148 break;
2149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002153 /* get_name_len() takes care of expanding curly braces */
2154 name_start = name = arg;
2155 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2156 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 /* This is mainly to keep test 49 working: when expanding
2159 * curly braces fails overrule the exception error message. */
2160 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162 emsg_severe = TRUE;
2163 EMSG2(_(e_invarg2), arg);
2164 break;
2165 }
2166 error = TRUE;
2167 }
2168 else
2169 {
2170 if (tofree != NULL)
2171 name = tofree;
2172 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 else
2175 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002176 /* handle d.key, l[idx], f(expr) */
2177 arg_subsc = arg;
2178 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002179 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002181 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002183 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002184 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002185 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 case 'g': list_glob_vars(first); break;
2187 case 'b': list_buf_vars(first); break;
2188 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002191#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 case 'v': list_vim_vars(first); break;
2193 case 's': list_script_vars(first); break;
2194 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002195 default:
2196 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002197 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002198 }
2199 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 {
2201 char_u numbuf[NUMBUFLEN];
2202 char_u *tf;
2203 int c;
2204 char_u *s;
2205
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002206 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 c = *arg;
2208 *arg = NUL;
2209 list_one_var_a((char_u *)"",
2210 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 tv.v_type,
2212 s == NULL ? (char_u *)"" : s,
2213 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 *arg = c;
2215 vim_free(tf);
2216 }
2217 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 }
2220 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221
2222 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002224
2225 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 }
2227
2228 return arg;
2229}
2230
2231/*
2232 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2233 * Returns a pointer to the char just after the var name.
2234 * Returns NULL if there is an error.
2235 */
2236 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002237ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002239 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002241 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243{
2244 int c1;
2245 char_u *name;
2246 char_u *p;
2247 char_u *arg_end = NULL;
2248 int len;
2249 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002250 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251
2252 /*
2253 * ":let $VAR = expr": Set environment variable.
2254 */
2255 if (*arg == '$')
2256 {
2257 /* Find the end of the name. */
2258 ++arg;
2259 name = arg;
2260 len = get_env_len(&arg);
2261 if (len == 0)
2262 EMSG2(_(e_invarg2), name - 1);
2263 else
2264 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002265 if (op != NULL && (*op == '+' || *op == '-'))
2266 EMSG2(_(e_letwrong), op);
2267 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002268 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 EMSG(_(e_letunexp));
2270 else
2271 {
2272 c1 = name[len];
2273 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 p = get_tv_string_chk(tv);
2275 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 {
2277 int mustfree = FALSE;
2278 char_u *s = vim_getenv(name, &mustfree);
2279
2280 if (s != NULL)
2281 {
2282 p = tofree = concat_str(s, p);
2283 if (mustfree)
2284 vim_free(s);
2285 }
2286 }
2287 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002288 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002289 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002290 if (STRICMP(name, "HOME") == 0)
2291 init_homedir();
2292 else if (didset_vim && STRICMP(name, "VIM") == 0)
2293 didset_vim = FALSE;
2294 else if (didset_vimruntime
2295 && STRICMP(name, "VIMRUNTIME") == 0)
2296 didset_vimruntime = FALSE;
2297 arg_end = arg;
2298 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
2302 }
2303 }
2304
2305 /*
2306 * ":let &option = expr": Set option value.
2307 * ":let &l:option = expr": Set local option value.
2308 * ":let &g:option = expr": Set global option value.
2309 */
2310 else if (*arg == '&')
2311 {
2312 /* Find the end of the name. */
2313 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002314 if (p == NULL || (endchars != NULL
2315 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 EMSG(_(e_letunexp));
2317 else
2318 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 long n;
2320 int opt_type;
2321 long numval;
2322 char_u *stringval = NULL;
2323 char_u *s;
2324
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 c1 = *p;
2326 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327
2328 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002329 s = get_tv_string_chk(tv); /* != NULL if number or string */
2330 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 {
2332 opt_type = get_option_value(arg, &numval,
2333 &stringval, opt_flags);
2334 if ((opt_type == 1 && *op == '.')
2335 || (opt_type == 0 && *op != '.'))
2336 EMSG2(_(e_letwrong), op);
2337 else
2338 {
2339 if (opt_type == 1) /* number */
2340 {
2341 if (*op == '+')
2342 n = numval + n;
2343 else
2344 n = numval - n;
2345 }
2346 else if (opt_type == 0 && stringval != NULL) /* string */
2347 {
2348 s = concat_str(stringval, s);
2349 vim_free(stringval);
2350 stringval = s;
2351 }
2352 }
2353 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 if (s != NULL)
2355 {
2356 set_option_value(arg, n, s, opt_flags);
2357 arg_end = p;
2358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
2362 }
2363
2364 /*
2365 * ":let @r = expr": Set register contents.
2366 */
2367 else if (*arg == '@')
2368 {
2369 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 if (op != NULL && (*op == '+' || *op == '-'))
2371 EMSG2(_(e_letwrong), op);
2372 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 EMSG(_(e_letunexp));
2375 else
2376 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002377 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 char_u *s;
2379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002380 p = get_tv_string_chk(tv);
2381 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002383 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 if (s != NULL)
2385 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002386 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 vim_free(s);
2388 }
2389 }
2390 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002391 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 arg_end = arg + 1;
2394 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002395 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 }
2397 }
2398
2399 /*
2400 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002403 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002405 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002407 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2411 EMSG(_(e_letunexp));
2412 else
2413 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 arg_end = p;
2416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 }
2420
2421 else
2422 EMSG2(_(e_invarg2), arg);
2423
2424 return arg_end;
2425}
2426
2427/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2429 */
2430 static int
2431check_changedtick(arg)
2432 char_u *arg;
2433{
2434 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2435 {
2436 EMSG2(_(e_readonlyvar), arg);
2437 return TRUE;
2438 }
2439 return FALSE;
2440}
2441
2442/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 * Get an lval: variable, Dict item or List item that can be assigned a value
2444 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2445 * "name.key", "name.key[expr]" etc.
2446 * Indexing only works if "name" is an existing List or Dictionary.
2447 * "name" points to the start of the name.
2448 * If "rettv" is not NULL it points to the value to be assigned.
2449 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2450 * wrong; must end in space or cmd separator.
2451 *
2452 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002453 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 */
2456 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002457get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002459 typval_T *rettv;
2460 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 int unlet;
2462 int skip;
2463 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002464 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 char_u *expr_start, *expr_end;
2468 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002469 dictitem_T *v;
2470 typval_T var1;
2471 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002473 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002476 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002479 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480
2481 if (skip)
2482 {
2483 /* When skipping just find the end of the name. */
2484 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002485 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 }
2487
2488 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002489 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 if (expr_start != NULL)
2491 {
2492 /* Don't expand the name when we already know there is an error. */
2493 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2494 && *p != '[' && *p != '.')
2495 {
2496 EMSG(_(e_trailing));
2497 return NULL;
2498 }
2499
2500 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2501 if (lp->ll_exp_name == NULL)
2502 {
2503 /* Report an invalid expression in braces, unless the
2504 * expression evaluation has been cancelled due to an
2505 * aborting error, an interrupt, or an exception. */
2506 if (!aborting() && !quiet)
2507 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002508 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 EMSG2(_(e_invarg2), name);
2510 return NULL;
2511 }
2512 }
2513 lp->ll_name = lp->ll_exp_name;
2514 }
2515 else
2516 lp->ll_name = name;
2517
2518 /* Without [idx] or .key we are done. */
2519 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2520 return p;
2521
2522 cc = *p;
2523 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002524 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (v == NULL && !quiet)
2526 EMSG2(_(e_undefvar), lp->ll_name);
2527 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 if (v == NULL)
2529 return NULL;
2530
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 /*
2532 * Loop until no more [idx] or .key is following.
2533 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2538 && !(lp->ll_tv->v_type == VAR_DICT
2539 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 if (!quiet)
2542 EMSG(_("E689: Can only index a List or Dictionary"));
2543 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 if (!quiet)
2548 EMSG(_("E708: [:] must come last"));
2549 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002551
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 len = -1;
2553 if (*p == '.')
2554 {
2555 key = p + 1;
2556 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2557 ;
2558 if (len == 0)
2559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (!quiet)
2561 EMSG(_(e_emptykey));
2562 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 }
2564 p = key + len;
2565 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002566 else
2567 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002569 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 if (*p == ':')
2571 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002572 else
2573 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 empty1 = FALSE;
2575 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002577 if (get_tv_string_chk(&var1) == NULL)
2578 {
2579 /* not a number or string */
2580 clear_tv(&var1);
2581 return NULL;
2582 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 }
2584
2585 /* Optionally get the second index [ :expr]. */
2586 if (*p == ':')
2587 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002592 if (!empty1)
2593 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002595 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (rettv != NULL && (rettv->v_type != VAR_LIST
2597 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (!quiet)
2600 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 if (!empty1)
2602 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 }
2605 p = skipwhite(p + 1);
2606 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 else
2609 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2612 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 if (!empty1)
2614 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002617 if (get_tv_string_chk(&var2) == NULL)
2618 {
2619 /* not a number or string */
2620 if (!empty1)
2621 clear_tv(&var1);
2622 clear_tv(&var2);
2623 return NULL;
2624 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 if (!empty1)
2636 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641
2642 /* Skip to past ']'. */
2643 ++p;
2644 }
2645
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 {
2648 if (len == -1)
2649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002651 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 if (*key == NUL)
2653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
2655 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 }
2659 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002660 lp->ll_list = NULL;
2661 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002662 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002665 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (len == -1)
2671 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 }
2674 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (len == -1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 p = NULL;
2682 break;
2683 }
2684 if (len == -1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
2688 else
2689 {
2690 /*
2691 * Get the number and item for the only or first index of the List.
2692 */
2693 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 else
2696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002697 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 clear_tv(&var1);
2699 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 lp->ll_list = lp->ll_tv->vval.v_list;
2702 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2703 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002705 if (lp->ll_n1 < 0)
2706 {
2707 lp->ll_n1 = 0;
2708 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2709 }
2710 }
2711 if (lp->ll_li == NULL)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
2717
2718 /*
2719 * May need to find the item or absolute index for the second
2720 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 * When no index given: "lp->ll_empty2" is TRUE.
2722 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
2735
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2737 if (lp->ll_n1 < 0)
2738 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2739 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 }
2742
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002745 }
2746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 return p;
2748}
2749
2750/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002751 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 */
2753 static void
2754clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002755 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756{
2757 vim_free(lp->ll_exp_name);
2758 vim_free(lp->ll_newkey);
2759}
2760
2761/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002762 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002764 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 */
2766 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002767set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002770 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773{
2774 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002775 listitem_T *ri;
2776 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777
2778 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 cc = *endp;
2783 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002784 if (op != NULL && *op != '=')
2785 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002786 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002788 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002789 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002790 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002791 {
2792 if (tv_op(&tv, rettv, op) == OK)
2793 set_var(lp->ll_name, &tv, FALSE);
2794 clear_tv(&tv);
2795 }
2796 }
2797 else
2798 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002800 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002802 else if (tv_check_lock(lp->ll_newkey == NULL
2803 ? lp->ll_tv->v_lock
2804 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2805 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 else if (lp->ll_range)
2807 {
2808 /*
2809 * Assign the List values to the list items.
2810 */
2811 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002812 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 if (op != NULL && *op != '=')
2814 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2815 else
2816 {
2817 clear_tv(&lp->ll_li->li_tv);
2818 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2819 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 ri = ri->li_next;
2821 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2822 break;
2823 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002826 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002827 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 ri = NULL;
2829 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002830 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002831 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_li = lp->ll_li->li_next;
2833 ++lp->ll_n1;
2834 }
2835 if (ri != NULL)
2836 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002837 else if (lp->ll_empty2
2838 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 : lp->ll_n1 != lp->ll_n2)
2840 EMSG(_("E711: List value has not enough items"));
2841 }
2842 else
2843 {
2844 /*
2845 * Assign to a List or Dictionary item.
2846 */
2847 if (lp->ll_newkey != NULL)
2848 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 if (op != NULL && *op != '=')
2850 {
2851 EMSG2(_(e_letwrong), op);
2852 return;
2853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002856 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 if (di == NULL)
2858 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002859 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2860 {
2861 vim_free(di);
2862 return;
2863 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002865 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002866 else if (op != NULL && *op != '=')
2867 {
2868 tv_op(lp->ll_tv, rettv, op);
2869 return;
2870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002871 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 /*
2875 * Assign the value to the variable or list item.
2876 */
2877 if (copy)
2878 copy_tv(rettv, lp->ll_tv);
2879 else
2880 {
2881 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002882 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 }
2885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002886}
2887
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2890 * Returns OK or FAIL.
2891 */
2892 static int
2893tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 typval_T *tv1;
2895 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 char_u *op;
2897{
2898 long n;
2899 char_u numbuf[NUMBUFLEN];
2900 char_u *s;
2901
2902 /* Can't do anything with a Funcref or a Dict on the right. */
2903 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2904 {
2905 switch (tv1->v_type)
2906 {
2907 case VAR_DICT:
2908 case VAR_FUNC:
2909 break;
2910
2911 case VAR_LIST:
2912 if (*op != '+' || tv2->v_type != VAR_LIST)
2913 break;
2914 /* List += List */
2915 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2916 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2917 return OK;
2918
2919 case VAR_NUMBER:
2920 case VAR_STRING:
2921 if (tv2->v_type == VAR_LIST)
2922 break;
2923 if (*op == '+' || *op == '-')
2924 {
2925 /* nr += nr or nr -= nr*/
2926 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002927#ifdef FEAT_FLOAT
2928 if (tv2->v_type == VAR_FLOAT)
2929 {
2930 float_T f = n;
2931
2932 if (*op == '+')
2933 f += tv2->vval.v_float;
2934 else
2935 f -= tv2->vval.v_float;
2936 clear_tv(tv1);
2937 tv1->v_type = VAR_FLOAT;
2938 tv1->vval.v_float = f;
2939 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002941#endif
2942 {
2943 if (*op == '+')
2944 n += get_tv_number(tv2);
2945 else
2946 n -= get_tv_number(tv2);
2947 clear_tv(tv1);
2948 tv1->v_type = VAR_NUMBER;
2949 tv1->vval.v_number = n;
2950 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 }
2952 else
2953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002954 if (tv2->v_type == VAR_FLOAT)
2955 break;
2956
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 /* str .= str */
2958 s = get_tv_string(tv1);
2959 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2960 clear_tv(tv1);
2961 tv1->v_type = VAR_STRING;
2962 tv1->vval.v_string = s;
2963 }
2964 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002965
2966#ifdef FEAT_FLOAT
2967 case VAR_FLOAT:
2968 {
2969 float_T f;
2970
2971 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2972 && tv2->v_type != VAR_NUMBER
2973 && tv2->v_type != VAR_STRING))
2974 break;
2975 if (tv2->v_type == VAR_FLOAT)
2976 f = tv2->vval.v_float;
2977 else
2978 f = get_tv_number(tv2);
2979 if (*op == '+')
2980 tv1->vval.v_float += f;
2981 else
2982 tv1->vval.v_float -= f;
2983 }
2984 return OK;
2985#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 }
2987 }
2988
2989 EMSG2(_(e_letwrong), op);
2990 return FAIL;
2991}
2992
2993/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 * Add a watcher to a list.
2995 */
2996 static void
2997list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002998 list_T *l;
2999 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000{
3001 lw->lw_next = l->lv_watch;
3002 l->lv_watch = lw;
3003}
3004
3005/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003006 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007 * No warning when it isn't found...
3008 */
3009 static void
3010list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003011 list_T *l;
3012 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013{
Bram Moolenaar33570922005-01-25 22:26:29 +00003014 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003015
3016 lwp = &l->lv_watch;
3017 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3018 {
3019 if (lw == lwrem)
3020 {
3021 *lwp = lw->lw_next;
3022 break;
3023 }
3024 lwp = &lw->lw_next;
3025 }
3026}
3027
3028/*
3029 * Just before removing an item from a list: advance watchers to the next
3030 * item.
3031 */
3032 static void
3033list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003034 list_T *l;
3035 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003036{
Bram Moolenaar33570922005-01-25 22:26:29 +00003037 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003038
3039 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3040 if (lw->lw_item == item)
3041 lw->lw_item = item->li_next;
3042}
3043
3044/*
3045 * Evaluate the expression used in a ":for var in expr" command.
3046 * "arg" points to "var".
3047 * Set "*errp" to TRUE for an error, FALSE otherwise;
3048 * Return a pointer that holds the info. Null when there is an error.
3049 */
3050 void *
3051eval_for_line(arg, errp, nextcmdp, skip)
3052 char_u *arg;
3053 int *errp;
3054 char_u **nextcmdp;
3055 int skip;
3056{
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003059 typval_T tv;
3060 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061
3062 *errp = TRUE; /* default: there is an error */
3063
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003065 if (fi == NULL)
3066 return NULL;
3067
3068 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3069 if (expr == NULL)
3070 return fi;
3071
3072 expr = skipwhite(expr);
3073 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3074 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003075 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003076 return fi;
3077 }
3078
3079 if (skip)
3080 ++emsg_skip;
3081 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3082 {
3083 *errp = FALSE;
3084 if (!skip)
3085 {
3086 l = tv.vval.v_list;
3087 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003088 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003089 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003090 clear_tv(&tv);
3091 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092 else
3093 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003094 /* No need to increment the refcount, it's already set for the
3095 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096 fi->fi_list = l;
3097 list_add_watch(l, &fi->fi_lw);
3098 fi->fi_lw.lw_item = l->lv_first;
3099 }
3100 }
3101 }
3102 if (skip)
3103 --emsg_skip;
3104
3105 return fi;
3106}
3107
3108/*
3109 * Use the first item in a ":for" list. Advance to the next.
3110 * Assign the values to the variable (list). "arg" points to the first one.
3111 * Return TRUE when a valid item was found, FALSE when at end of list or
3112 * something wrong.
3113 */
3114 int
3115next_for_item(fi_void, arg)
3116 void *fi_void;
3117 char_u *arg;
3118{
Bram Moolenaar33570922005-01-25 22:26:29 +00003119 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122
3123 item = fi->fi_lw.lw_item;
3124 if (item == NULL)
3125 result = FALSE;
3126 else
3127 {
3128 fi->fi_lw.lw_item = item->li_next;
3129 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3130 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3131 }
3132 return result;
3133}
3134
3135/*
3136 * Free the structure used to store info used by ":for".
3137 */
3138 void
3139free_for_info(fi_void)
3140 void *fi_void;
3141{
Bram Moolenaar33570922005-01-25 22:26:29 +00003142 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003144 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003145 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003147 list_unref(fi->fi_list);
3148 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149 vim_free(fi);
3150}
3151
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3153
3154 void
3155set_context_for_expression(xp, arg, cmdidx)
3156 expand_T *xp;
3157 char_u *arg;
3158 cmdidx_T cmdidx;
3159{
3160 int got_eq = FALSE;
3161 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 if (cmdidx == CMD_let)
3165 {
3166 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003167 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 {
3169 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003170 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171 {
3172 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 if (vim_iswhite(*p))
3175 break;
3176 }
3177 return;
3178 }
3179 }
3180 else
3181 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3182 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 while ((xp->xp_pattern = vim_strpbrk(arg,
3184 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3185 {
3186 c = *xp->xp_pattern;
3187 if (c == '&')
3188 {
3189 c = xp->xp_pattern[1];
3190 if (c == '&')
3191 {
3192 ++xp->xp_pattern;
3193 xp->xp_context = cmdidx != CMD_let || got_eq
3194 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3195 }
3196 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003199 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3200 xp->xp_pattern += 2;
3201
3202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204 else if (c == '$')
3205 {
3206 /* environment variable */
3207 xp->xp_context = EXPAND_ENV_VARS;
3208 }
3209 else if (c == '=')
3210 {
3211 got_eq = TRUE;
3212 xp->xp_context = EXPAND_EXPRESSION;
3213 }
3214 else if (c == '<'
3215 && xp->xp_context == EXPAND_FUNCTIONS
3216 && vim_strchr(xp->xp_pattern, '(') == NULL)
3217 {
3218 /* Function name can start with "<SNR>" */
3219 break;
3220 }
3221 else if (cmdidx != CMD_let || got_eq)
3222 {
3223 if (c == '"') /* string */
3224 {
3225 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3226 if (c == '\\' && xp->xp_pattern[1] != NUL)
3227 ++xp->xp_pattern;
3228 xp->xp_context = EXPAND_NOTHING;
3229 }
3230 else if (c == '\'') /* literal string */
3231 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003232 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3234 /* skip */ ;
3235 xp->xp_context = EXPAND_NOTHING;
3236 }
3237 else if (c == '|')
3238 {
3239 if (xp->xp_pattern[1] == '|')
3240 {
3241 ++xp->xp_pattern;
3242 xp->xp_context = EXPAND_EXPRESSION;
3243 }
3244 else
3245 xp->xp_context = EXPAND_COMMANDS;
3246 }
3247 else
3248 xp->xp_context = EXPAND_EXPRESSION;
3249 }
3250 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251 /* Doesn't look like something valid, expand as an expression
3252 * anyway. */
3253 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 arg = xp->xp_pattern;
3255 if (*arg != NUL)
3256 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3257 /* skip */ ;
3258 }
3259 xp->xp_pattern = arg;
3260}
3261
3262#endif /* FEAT_CMDL_COMPL */
3263
3264/*
3265 * ":1,25call func(arg1, arg2)" function call.
3266 */
3267 void
3268ex_call(eap)
3269 exarg_T *eap;
3270{
3271 char_u *arg = eap->arg;
3272 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003274 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 linenr_T lnum;
3278 int doesrange;
3279 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003282 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003283 if (fudi.fd_newkey != NULL)
3284 {
3285 /* Still need to give an error message for missing key. */
3286 EMSG2(_(e_dictkey), fudi.fd_newkey);
3287 vim_free(fudi.fd_newkey);
3288 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003289 if (tofree == NULL)
3290 return;
3291
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003292 /* Increase refcount on dictionary, it could get deleted when evaluating
3293 * the arguments. */
3294 if (fudi.fd_dict != NULL)
3295 ++fudi.fd_dict->dv_refcount;
3296
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003297 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003298 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003299 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
Bram Moolenaar532c7802005-01-27 14:44:31 +00003301 /* Skip white space to allow ":call func ()". Not good, but required for
3302 * backward compatibility. */
3303 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003304 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
3306 if (*startarg != '(')
3307 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003308 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 goto end;
3310 }
3311
3312 /*
3313 * When skipping, evaluate the function once, to find the end of the
3314 * arguments.
3315 * When the function takes a range, this is discovered after the first
3316 * call, and the loop is broken.
3317 */
3318 if (eap->skip)
3319 {
3320 ++emsg_skip;
3321 lnum = eap->line2; /* do it once, also with an invalid range */
3322 }
3323 else
3324 lnum = eap->line1;
3325 for ( ; lnum <= eap->line2; ++lnum)
3326 {
3327 if (!eap->skip && eap->addr_count > 0)
3328 {
3329 curwin->w_cursor.lnum = lnum;
3330 curwin->w_cursor.col = 0;
3331 }
3332 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003333 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003334 eap->line1, eap->line2, &doesrange,
3335 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
3337 failed = TRUE;
3338 break;
3339 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003340
3341 /* Handle a function returning a Funcref, Dictionary or List. */
3342 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3343 {
3344 failed = TRUE;
3345 break;
3346 }
3347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003348 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (doesrange || eap->skip)
3350 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003353 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003354 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003355 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (aborting())
3357 break;
3358 }
3359 if (eap->skip)
3360 --emsg_skip;
3361
3362 if (!failed)
3363 {
3364 /* Check for trailing illegal characters and a following command. */
3365 if (!ends_excmd(*arg))
3366 {
3367 emsg_severe = TRUE;
3368 EMSG(_(e_trailing));
3369 }
3370 else
3371 eap->nextcmd = check_nextcmd(arg);
3372 }
3373
3374end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003375 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003376 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377}
3378
3379/*
3380 * ":unlet[!] var1 ... " command.
3381 */
3382 void
3383ex_unlet(eap)
3384 exarg_T *eap;
3385{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003386 ex_unletlock(eap, eap->arg, 0);
3387}
3388
3389/*
3390 * ":lockvar" and ":unlockvar" commands
3391 */
3392 void
3393ex_lockvar(eap)
3394 exarg_T *eap;
3395{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003397 int deep = 2;
3398
3399 if (eap->forceit)
3400 deep = -1;
3401 else if (vim_isdigit(*arg))
3402 {
3403 deep = getdigits(&arg);
3404 arg = skipwhite(arg);
3405 }
3406
3407 ex_unletlock(eap, arg, deep);
3408}
3409
3410/*
3411 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3412 */
3413 static void
3414ex_unletlock(eap, argstart, deep)
3415 exarg_T *eap;
3416 char_u *argstart;
3417 int deep;
3418{
3419 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003422 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
3424 do
3425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003426 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003427 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3428 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003429 if (lv.ll_name == NULL)
3430 error = TRUE; /* error but continue parsing */
3431 if (name_end == NULL || (!vim_iswhite(*name_end)
3432 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003434 if (name_end != NULL)
3435 {
3436 emsg_severe = TRUE;
3437 EMSG(_(e_trailing));
3438 }
3439 if (!(eap->skip || error))
3440 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 break;
3442 }
3443
3444 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003445 {
3446 if (eap->cmdidx == CMD_unlet)
3447 {
3448 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3449 error = TRUE;
3450 }
3451 else
3452 {
3453 if (do_lock_var(&lv, name_end, deep,
3454 eap->cmdidx == CMD_lockvar) == FAIL)
3455 error = TRUE;
3456 }
3457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003459 if (!eap->skip)
3460 clear_lval(&lv);
3461
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 arg = skipwhite(name_end);
3463 } while (!ends_excmd(*arg));
3464
3465 eap->nextcmd = check_nextcmd(arg);
3466}
3467
Bram Moolenaar8c711452005-01-14 21:53:12 +00003468 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003469do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003470 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003471 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003472 int forceit;
3473{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003474 int ret = OK;
3475 int cc;
3476
3477 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003479 cc = *name_end;
3480 *name_end = NUL;
3481
3482 /* Normal name or expanded name. */
3483 if (check_changedtick(lp->ll_name))
3484 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003485 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003486 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003487 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003488 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003489 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3490 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003491 else if (lp->ll_range)
3492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003493 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003494
3495 /* Delete a range of List items. */
3496 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3497 {
3498 li = lp->ll_li->li_next;
3499 listitem_remove(lp->ll_list, lp->ll_li);
3500 lp->ll_li = li;
3501 ++lp->ll_n1;
3502 }
3503 }
3504 else
3505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003506 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003507 /* unlet a List item. */
3508 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003509 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003510 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003511 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003512 }
3513
3514 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003515}
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517/*
3518 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003519 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 */
3521 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003522do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
Bram Moolenaar33570922005-01-25 22:26:29 +00003526 hashtab_T *ht;
3527 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003528 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003529 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaar33570922005-01-25 22:26:29 +00003531 ht = find_var_ht(name, &varname);
3532 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003534 hi = hash_find(ht, varname);
3535 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003536 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003537 di = HI2DI(hi);
3538 if (var_check_fixed(di->di_flags, name)
3539 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003540 return FAIL;
3541 delete_var(ht, hi);
3542 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003545 if (forceit)
3546 return OK;
3547 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 return FAIL;
3549}
3550
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003551/*
3552 * Lock or unlock variable indicated by "lp".
3553 * "deep" is the levels to go (-1 for unlimited);
3554 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3555 */
3556 static int
3557do_lock_var(lp, name_end, deep, lock)
3558 lval_T *lp;
3559 char_u *name_end;
3560 int deep;
3561 int lock;
3562{
3563 int ret = OK;
3564 int cc;
3565 dictitem_T *di;
3566
3567 if (deep == 0) /* nothing to do */
3568 return OK;
3569
3570 if (lp->ll_tv == NULL)
3571 {
3572 cc = *name_end;
3573 *name_end = NUL;
3574
3575 /* Normal name or expanded name. */
3576 if (check_changedtick(lp->ll_name))
3577 ret = FAIL;
3578 else
3579 {
3580 di = find_var(lp->ll_name, NULL);
3581 if (di == NULL)
3582 ret = FAIL;
3583 else
3584 {
3585 if (lock)
3586 di->di_flags |= DI_FLAGS_LOCK;
3587 else
3588 di->di_flags &= ~DI_FLAGS_LOCK;
3589 item_lock(&di->di_tv, deep, lock);
3590 }
3591 }
3592 *name_end = cc;
3593 }
3594 else if (lp->ll_range)
3595 {
3596 listitem_T *li = lp->ll_li;
3597
3598 /* (un)lock a range of List items. */
3599 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3600 {
3601 item_lock(&li->li_tv, deep, lock);
3602 li = li->li_next;
3603 ++lp->ll_n1;
3604 }
3605 }
3606 else if (lp->ll_list != NULL)
3607 /* (un)lock a List item. */
3608 item_lock(&lp->ll_li->li_tv, deep, lock);
3609 else
3610 /* un(lock) a Dictionary item. */
3611 item_lock(&lp->ll_di->di_tv, deep, lock);
3612
3613 return ret;
3614}
3615
3616/*
3617 * Lock or unlock an item. "deep" is nr of levels to go.
3618 */
3619 static void
3620item_lock(tv, deep, lock)
3621 typval_T *tv;
3622 int deep;
3623 int lock;
3624{
3625 static int recurse = 0;
3626 list_T *l;
3627 listitem_T *li;
3628 dict_T *d;
3629 hashitem_T *hi;
3630 int todo;
3631
3632 if (recurse >= DICT_MAXNEST)
3633 {
3634 EMSG(_("E743: variable nested too deep for (un)lock"));
3635 return;
3636 }
3637 if (deep == 0)
3638 return;
3639 ++recurse;
3640
3641 /* lock/unlock the item itself */
3642 if (lock)
3643 tv->v_lock |= VAR_LOCKED;
3644 else
3645 tv->v_lock &= ~VAR_LOCKED;
3646
3647 switch (tv->v_type)
3648 {
3649 case VAR_LIST:
3650 if ((l = tv->vval.v_list) != NULL)
3651 {
3652 if (lock)
3653 l->lv_lock |= VAR_LOCKED;
3654 else
3655 l->lv_lock &= ~VAR_LOCKED;
3656 if (deep < 0 || deep > 1)
3657 /* recursive: lock/unlock the items the List contains */
3658 for (li = l->lv_first; li != NULL; li = li->li_next)
3659 item_lock(&li->li_tv, deep - 1, lock);
3660 }
3661 break;
3662 case VAR_DICT:
3663 if ((d = tv->vval.v_dict) != NULL)
3664 {
3665 if (lock)
3666 d->dv_lock |= VAR_LOCKED;
3667 else
3668 d->dv_lock &= ~VAR_LOCKED;
3669 if (deep < 0 || deep > 1)
3670 {
3671 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003672 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3674 {
3675 if (!HASHITEM_EMPTY(hi))
3676 {
3677 --todo;
3678 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3679 }
3680 }
3681 }
3682 }
3683 }
3684 --recurse;
3685}
3686
Bram Moolenaara40058a2005-07-11 22:42:07 +00003687/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003688 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3689 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003690 */
3691 static int
3692tv_islocked(tv)
3693 typval_T *tv;
3694{
3695 return (tv->v_lock & VAR_LOCKED)
3696 || (tv->v_type == VAR_LIST
3697 && tv->vval.v_list != NULL
3698 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3699 || (tv->v_type == VAR_DICT
3700 && tv->vval.v_dict != NULL
3701 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3702}
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3705/*
3706 * Delete all "menutrans_" variables.
3707 */
3708 void
3709del_menutrans_vars()
3710{
Bram Moolenaar33570922005-01-25 22:26:29 +00003711 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003712 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
Bram Moolenaar33570922005-01-25 22:26:29 +00003714 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003715 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003716 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003717 {
3718 if (!HASHITEM_EMPTY(hi))
3719 {
3720 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3722 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003723 }
3724 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726}
3727#endif
3728
3729#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3730
3731/*
3732 * Local string buffer for the next two functions to store a variable name
3733 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3734 * get_user_var_name().
3735 */
3736
3737static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3738
3739static char_u *varnamebuf = NULL;
3740static int varnamebuflen = 0;
3741
3742/*
3743 * Function to concatenate a prefix and a variable name.
3744 */
3745 static char_u *
3746cat_prefix_varname(prefix, name)
3747 int prefix;
3748 char_u *name;
3749{
3750 int len;
3751
3752 len = (int)STRLEN(name) + 3;
3753 if (len > varnamebuflen)
3754 {
3755 vim_free(varnamebuf);
3756 len += 10; /* some additional space */
3757 varnamebuf = alloc(len);
3758 if (varnamebuf == NULL)
3759 {
3760 varnamebuflen = 0;
3761 return NULL;
3762 }
3763 varnamebuflen = len;
3764 }
3765 *varnamebuf = prefix;
3766 varnamebuf[1] = ':';
3767 STRCPY(varnamebuf + 2, name);
3768 return varnamebuf;
3769}
3770
3771/*
3772 * Function given to ExpandGeneric() to obtain the list of user defined
3773 * (global/buffer/window/built-in) variable names.
3774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 char_u *
3776get_user_var_name(xp, idx)
3777 expand_T *xp;
3778 int idx;
3779{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003780 static long_u gdone;
3781 static long_u bdone;
3782 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003783#ifdef FEAT_WINDOWS
3784 static long_u tdone;
3785#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003786 static int vidx;
3787 static hashitem_T *hi;
3788 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
3790 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003791 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003792 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003793#ifdef FEAT_WINDOWS
3794 tdone = 0;
3795#endif
3796 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003797
3798 /* Global variables */
3799 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003801 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003802 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003803 else
3804 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003805 while (HASHITEM_EMPTY(hi))
3806 ++hi;
3807 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3808 return cat_prefix_varname('g', hi->hi_key);
3809 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003811
3812 /* b: variables */
3813 ht = &curbuf->b_vars.dv_hashtab;
3814 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003816 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003817 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003818 else
3819 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003820 while (HASHITEM_EMPTY(hi))
3821 ++hi;
3822 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003826 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 return (char_u *)"b:changedtick";
3828 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003829
3830 /* w: variables */
3831 ht = &curwin->w_vars.dv_hashtab;
3832 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003834 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003835 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003836 else
3837 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003838 while (HASHITEM_EMPTY(hi))
3839 ++hi;
3840 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003842
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003843#ifdef FEAT_WINDOWS
3844 /* t: variables */
3845 ht = &curtab->tp_vars.dv_hashtab;
3846 if (tdone < ht->ht_used)
3847 {
3848 if (tdone++ == 0)
3849 hi = ht->ht_array;
3850 else
3851 ++hi;
3852 while (HASHITEM_EMPTY(hi))
3853 ++hi;
3854 return cat_prefix_varname('t', hi->hi_key);
3855 }
3856#endif
3857
Bram Moolenaar33570922005-01-25 22:26:29 +00003858 /* v: variables */
3859 if (vidx < VV_LEN)
3860 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862 vim_free(varnamebuf);
3863 varnamebuf = NULL;
3864 varnamebuflen = 0;
3865 return NULL;
3866}
3867
3868#endif /* FEAT_CMDL_COMPL */
3869
3870/*
3871 * types for expressions.
3872 */
3873typedef enum
3874{
3875 TYPE_UNKNOWN = 0
3876 , TYPE_EQUAL /* == */
3877 , TYPE_NEQUAL /* != */
3878 , TYPE_GREATER /* > */
3879 , TYPE_GEQUAL /* >= */
3880 , TYPE_SMALLER /* < */
3881 , TYPE_SEQUAL /* <= */
3882 , TYPE_MATCH /* =~ */
3883 , TYPE_NOMATCH /* !~ */
3884} exptype_T;
3885
3886/*
3887 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003888 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3890 */
3891
3892/*
3893 * Handle zero level expression.
3894 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003895 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003896 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 * Return OK or FAIL.
3898 */
3899 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 char_u **nextcmd;
3904 int evaluate;
3905{
3906 int ret;
3907 char_u *p;
3908
3909 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003910 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (ret == FAIL || !ends_excmd(*p))
3912 {
3913 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003914 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 /*
3916 * Report the invalid expression unless the expression evaluation has
3917 * been cancelled due to an aborting error, an interrupt, or an
3918 * exception.
3919 */
3920 if (!aborting())
3921 EMSG2(_(e_invexpr2), arg);
3922 ret = FAIL;
3923 }
3924 if (nextcmd != NULL)
3925 *nextcmd = check_nextcmd(p);
3926
3927 return ret;
3928}
3929
3930/*
3931 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003932 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 *
3934 * "arg" must point to the first non-white of the expression.
3935 * "arg" is advanced to the next non-white after the recognized expression.
3936 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003937 * Note: "rettv.v_lock" is not set.
3938 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 * Return OK or FAIL.
3940 */
3941 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 int evaluate;
3946{
3947 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950 /*
3951 * Get the first variable.
3952 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return FAIL;
3955
3956 if ((*arg)[0] == '?')
3957 {
3958 result = FALSE;
3959 if (evaluate)
3960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003961 int error = FALSE;
3962
3963 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003966 if (error)
3967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969
3970 /*
3971 * Get the second variable.
3972 */
3973 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003974 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return FAIL;
3976
3977 /*
3978 * Check for the ":".
3979 */
3980 if ((*arg)[0] != ':')
3981 {
3982 EMSG(_("E109: Missing ':' after '?'"));
3983 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003984 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return FAIL;
3986 }
3987
3988 /*
3989 * Get the third variable.
3990 */
3991 *arg = skipwhite(*arg + 1);
3992 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3993 {
3994 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 return FAIL;
3997 }
3998 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003999 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001
4002 return OK;
4003}
4004
4005/*
4006 * Handle first level expression:
4007 * expr2 || expr2 || expr2 logical OR
4008 *
4009 * "arg" must point to the first non-white of the expression.
4010 * "arg" is advanced to the next non-white after the recognized expression.
4011 *
4012 * Return OK or FAIL.
4013 */
4014 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 int evaluate;
4019{
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 long result;
4022 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004023 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 /*
4026 * Get the first variable.
4027 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return FAIL;
4030
4031 /*
4032 * Repeat until there is no following "||".
4033 */
4034 first = TRUE;
4035 result = FALSE;
4036 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4037 {
4038 if (evaluate && first)
4039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004040 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004043 if (error)
4044 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 first = FALSE;
4046 }
4047
4048 /*
4049 * Get the second variable.
4050 */
4051 *arg = skipwhite(*arg + 2);
4052 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4053 return FAIL;
4054
4055 /*
4056 * Compute the result.
4057 */
4058 if (evaluate && !result)
4059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004060 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004063 if (error)
4064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066 if (evaluate)
4067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004068 rettv->v_type = VAR_NUMBER;
4069 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 }
4072
4073 return OK;
4074}
4075
4076/*
4077 * Handle second level expression:
4078 * expr3 && expr3 && expr3 logical AND
4079 *
4080 * "arg" must point to the first non-white of the expression.
4081 * "arg" is advanced to the next non-white after the recognized expression.
4082 *
4083 * Return OK or FAIL.
4084 */
4085 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 int evaluate;
4090{
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 long result;
4093 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096 /*
4097 * Get the first variable.
4098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 return FAIL;
4101
4102 /*
4103 * Repeat until there is no following "&&".
4104 */
4105 first = TRUE;
4106 result = TRUE;
4107 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4108 {
4109 if (evaluate && first)
4110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004111 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 if (error)
4115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 first = FALSE;
4117 }
4118
4119 /*
4120 * Get the second variable.
4121 */
4122 *arg = skipwhite(*arg + 2);
4123 if (eval4(arg, &var2, evaluate && result) == FAIL)
4124 return FAIL;
4125
4126 /*
4127 * Compute the result.
4128 */
4129 if (evaluate && result)
4130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004131 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004134 if (error)
4135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 if (evaluate)
4138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 rettv->v_type = VAR_NUMBER;
4140 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142 }
4143
4144 return OK;
4145}
4146
4147/*
4148 * Handle third level expression:
4149 * var1 == var2
4150 * var1 =~ var2
4151 * var1 != var2
4152 * var1 !~ var2
4153 * var1 > var2
4154 * var1 >= var2
4155 * var1 < var2
4156 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004157 * var1 is var2
4158 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 *
4160 * "arg" must point to the first non-white of the expression.
4161 * "arg" is advanced to the next non-white after the recognized expression.
4162 *
4163 * Return OK or FAIL.
4164 */
4165 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 int evaluate;
4170{
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 char_u *p;
4173 int i;
4174 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004175 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 int len = 2;
4177 long n1, n2;
4178 char_u *s1, *s2;
4179 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4180 regmatch_T regmatch;
4181 int ic;
4182 char_u *save_cpo;
4183
4184 /*
4185 * Get the first variable.
4186 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return FAIL;
4189
4190 p = *arg;
4191 switch (p[0])
4192 {
4193 case '=': if (p[1] == '=')
4194 type = TYPE_EQUAL;
4195 else if (p[1] == '~')
4196 type = TYPE_MATCH;
4197 break;
4198 case '!': if (p[1] == '=')
4199 type = TYPE_NEQUAL;
4200 else if (p[1] == '~')
4201 type = TYPE_NOMATCH;
4202 break;
4203 case '>': if (p[1] != '=')
4204 {
4205 type = TYPE_GREATER;
4206 len = 1;
4207 }
4208 else
4209 type = TYPE_GEQUAL;
4210 break;
4211 case '<': if (p[1] != '=')
4212 {
4213 type = TYPE_SMALLER;
4214 len = 1;
4215 }
4216 else
4217 type = TYPE_SEQUAL;
4218 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004219 case 'i': if (p[1] == 's')
4220 {
4221 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4222 len = 5;
4223 if (!vim_isIDc(p[len]))
4224 {
4225 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4226 type_is = TRUE;
4227 }
4228 }
4229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231
4232 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004233 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 */
4235 if (type != TYPE_UNKNOWN)
4236 {
4237 /* extra question mark appended: ignore case */
4238 if (p[len] == '?')
4239 {
4240 ic = TRUE;
4241 ++len;
4242 }
4243 /* extra '#' appended: match case */
4244 else if (p[len] == '#')
4245 {
4246 ic = FALSE;
4247 ++len;
4248 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004249 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 else
4251 ic = p_ic;
4252
4253 /*
4254 * Get the second variable.
4255 */
4256 *arg = skipwhite(p + len);
4257 if (eval5(arg, &var2, evaluate) == FAIL)
4258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 return FAIL;
4261 }
4262
4263 if (evaluate)
4264 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004265 if (type_is && rettv->v_type != var2.v_type)
4266 {
4267 /* For "is" a different type always means FALSE, for "notis"
4268 * it means TRUE. */
4269 n1 = (type == TYPE_NEQUAL);
4270 }
4271 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4272 {
4273 if (type_is)
4274 {
4275 n1 = (rettv->v_type == var2.v_type
4276 && rettv->vval.v_list == var2.vval.v_list);
4277 if (type == TYPE_NEQUAL)
4278 n1 = !n1;
4279 }
4280 else if (rettv->v_type != var2.v_type
4281 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4282 {
4283 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004284 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004285 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004286 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004287 clear_tv(rettv);
4288 clear_tv(&var2);
4289 return FAIL;
4290 }
4291 else
4292 {
4293 /* Compare two Lists for being equal or unequal. */
4294 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4295 if (type == TYPE_NEQUAL)
4296 n1 = !n1;
4297 }
4298 }
4299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004300 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4301 {
4302 if (type_is)
4303 {
4304 n1 = (rettv->v_type == var2.v_type
4305 && rettv->vval.v_dict == var2.vval.v_dict);
4306 if (type == TYPE_NEQUAL)
4307 n1 = !n1;
4308 }
4309 else if (rettv->v_type != var2.v_type
4310 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4311 {
4312 if (rettv->v_type != var2.v_type)
4313 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4314 else
4315 EMSG(_("E736: Invalid operation for Dictionary"));
4316 clear_tv(rettv);
4317 clear_tv(&var2);
4318 return FAIL;
4319 }
4320 else
4321 {
4322 /* Compare two Dictionaries for being equal or unequal. */
4323 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4324 if (type == TYPE_NEQUAL)
4325 n1 = !n1;
4326 }
4327 }
4328
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004329 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4330 {
4331 if (rettv->v_type != var2.v_type
4332 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4333 {
4334 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004335 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004336 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004337 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004338 clear_tv(rettv);
4339 clear_tv(&var2);
4340 return FAIL;
4341 }
4342 else
4343 {
4344 /* Compare two Funcrefs for being equal or unequal. */
4345 if (rettv->vval.v_string == NULL
4346 || var2.vval.v_string == NULL)
4347 n1 = FALSE;
4348 else
4349 n1 = STRCMP(rettv->vval.v_string,
4350 var2.vval.v_string) == 0;
4351 if (type == TYPE_NEQUAL)
4352 n1 = !n1;
4353 }
4354 }
4355
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004356#ifdef FEAT_FLOAT
4357 /*
4358 * If one of the two variables is a float, compare as a float.
4359 * When using "=~" or "!~", always compare as string.
4360 */
4361 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4362 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4363 {
4364 float_T f1, f2;
4365
4366 if (rettv->v_type == VAR_FLOAT)
4367 f1 = rettv->vval.v_float;
4368 else
4369 f1 = get_tv_number(rettv);
4370 if (var2.v_type == VAR_FLOAT)
4371 f2 = var2.vval.v_float;
4372 else
4373 f2 = get_tv_number(&var2);
4374 n1 = FALSE;
4375 switch (type)
4376 {
4377 case TYPE_EQUAL: n1 = (f1 == f2); break;
4378 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4379 case TYPE_GREATER: n1 = (f1 > f2); break;
4380 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4381 case TYPE_SMALLER: n1 = (f1 < f2); break;
4382 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4383 case TYPE_UNKNOWN:
4384 case TYPE_MATCH:
4385 case TYPE_NOMATCH: break; /* avoid gcc warning */
4386 }
4387 }
4388#endif
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 /*
4391 * If one of the two variables is a number, compare as a number.
4392 * When using "=~" or "!~", always compare as string.
4393 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004394 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397 n1 = get_tv_number(rettv);
4398 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 switch (type)
4400 {
4401 case TYPE_EQUAL: n1 = (n1 == n2); break;
4402 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4403 case TYPE_GREATER: n1 = (n1 > n2); break;
4404 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4405 case TYPE_SMALLER: n1 = (n1 < n2); break;
4406 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4407 case TYPE_UNKNOWN:
4408 case TYPE_MATCH:
4409 case TYPE_NOMATCH: break; /* avoid gcc warning */
4410 }
4411 }
4412 else
4413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 s1 = get_tv_string_buf(rettv, buf1);
4415 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4417 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4418 else
4419 i = 0;
4420 n1 = FALSE;
4421 switch (type)
4422 {
4423 case TYPE_EQUAL: n1 = (i == 0); break;
4424 case TYPE_NEQUAL: n1 = (i != 0); break;
4425 case TYPE_GREATER: n1 = (i > 0); break;
4426 case TYPE_GEQUAL: n1 = (i >= 0); break;
4427 case TYPE_SMALLER: n1 = (i < 0); break;
4428 case TYPE_SEQUAL: n1 = (i <= 0); break;
4429
4430 case TYPE_MATCH:
4431 case TYPE_NOMATCH:
4432 /* avoid 'l' flag in 'cpoptions' */
4433 save_cpo = p_cpo;
4434 p_cpo = (char_u *)"";
4435 regmatch.regprog = vim_regcomp(s2,
4436 RE_MAGIC + RE_STRING);
4437 regmatch.rm_ic = ic;
4438 if (regmatch.regprog != NULL)
4439 {
4440 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4441 vim_free(regmatch.regprog);
4442 if (type == TYPE_NOMATCH)
4443 n1 = !n1;
4444 }
4445 p_cpo = save_cpo;
4446 break;
4447
4448 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4449 }
4450 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004451 clear_tv(rettv);
4452 clear_tv(&var2);
4453 rettv->v_type = VAR_NUMBER;
4454 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456 }
4457
4458 return OK;
4459}
4460
4461/*
4462 * Handle fourth level expression:
4463 * + number addition
4464 * - number subtraction
4465 * . string concatenation
4466 *
4467 * "arg" must point to the first non-white of the expression.
4468 * "arg" is advanced to the next non-white after the recognized expression.
4469 *
4470 * Return OK or FAIL.
4471 */
4472 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 int evaluate;
4477{
Bram Moolenaar33570922005-01-25 22:26:29 +00004478 typval_T var2;
4479 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 int op;
4481 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004482#ifdef FEAT_FLOAT
4483 float_T f1 = 0, f2 = 0;
4484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 char_u *s1, *s2;
4486 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4487 char_u *p;
4488
4489 /*
4490 * Get the first variable.
4491 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004492 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 return FAIL;
4494
4495 /*
4496 * Repeat computing, until no '+', '-' or '.' is following.
4497 */
4498 for (;;)
4499 {
4500 op = **arg;
4501 if (op != '+' && op != '-' && op != '.')
4502 break;
4503
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004504 if ((op != '+' || rettv->v_type != VAR_LIST)
4505#ifdef FEAT_FLOAT
4506 && (op == '.' || rettv->v_type != VAR_FLOAT)
4507#endif
4508 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004509 {
4510 /* For "list + ...", an illegal use of the first operand as
4511 * a number cannot be determined before evaluating the 2nd
4512 * operand: if this is also a list, all is ok.
4513 * For "something . ...", "something - ..." or "non-list + ...",
4514 * we know that the first operand needs to be a string or number
4515 * without evaluating the 2nd operand. So check before to avoid
4516 * side effects after an error. */
4517 if (evaluate && get_tv_string_chk(rettv) == NULL)
4518 {
4519 clear_tv(rettv);
4520 return FAIL;
4521 }
4522 }
4523
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /*
4525 * Get the second variable.
4526 */
4527 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004528 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004530 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return FAIL;
4532 }
4533
4534 if (evaluate)
4535 {
4536 /*
4537 * Compute the result.
4538 */
4539 if (op == '.')
4540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004541 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4542 s2 = get_tv_string_buf_chk(&var2, buf2);
4543 if (s2 == NULL) /* type error ? */
4544 {
4545 clear_tv(rettv);
4546 clear_tv(&var2);
4547 return FAIL;
4548 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004549 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004550 clear_tv(rettv);
4551 rettv->v_type = VAR_STRING;
4552 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004554 else if (op == '+' && rettv->v_type == VAR_LIST
4555 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 {
4557 /* concatenate Lists */
4558 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4559 &var3) == FAIL)
4560 {
4561 clear_tv(rettv);
4562 clear_tv(&var2);
4563 return FAIL;
4564 }
4565 clear_tv(rettv);
4566 *rettv = var3;
4567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 else
4569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004570 int error = FALSE;
4571
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004572#ifdef FEAT_FLOAT
4573 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004574 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004575 f1 = rettv->vval.v_float;
4576 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004577 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004578 else
4579#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004580 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004581 n1 = get_tv_number_chk(rettv, &error);
4582 if (error)
4583 {
4584 /* This can only happen for "list + non-list". For
4585 * "non-list + ..." or "something - ...", we returned
4586 * before evaluating the 2nd operand. */
4587 clear_tv(rettv);
4588 return FAIL;
4589 }
4590#ifdef FEAT_FLOAT
4591 if (var2.v_type == VAR_FLOAT)
4592 f1 = n1;
4593#endif
4594 }
4595#ifdef FEAT_FLOAT
4596 if (var2.v_type == VAR_FLOAT)
4597 {
4598 f2 = var2.vval.v_float;
4599 n2 = 0;
4600 }
4601 else
4602#endif
4603 {
4604 n2 = get_tv_number_chk(&var2, &error);
4605 if (error)
4606 {
4607 clear_tv(rettv);
4608 clear_tv(&var2);
4609 return FAIL;
4610 }
4611#ifdef FEAT_FLOAT
4612 if (rettv->v_type == VAR_FLOAT)
4613 f2 = n2;
4614#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004615 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004617
4618#ifdef FEAT_FLOAT
4619 /* If there is a float on either side the result is a float. */
4620 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4621 {
4622 if (op == '+')
4623 f1 = f1 + f2;
4624 else
4625 f1 = f1 - f2;
4626 rettv->v_type = VAR_FLOAT;
4627 rettv->vval.v_float = f1;
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630#endif
4631 {
4632 if (op == '+')
4633 n1 = n1 + n2;
4634 else
4635 n1 = n1 - n2;
4636 rettv->v_type = VAR_NUMBER;
4637 rettv->vval.v_number = n1;
4638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 }
4643 return OK;
4644}
4645
4646/*
4647 * Handle fifth level expression:
4648 * * number multiplication
4649 * / number division
4650 * % number modulo
4651 *
4652 * "arg" must point to the first non-white of the expression.
4653 * "arg" is advanced to the next non-white after the recognized expression.
4654 *
4655 * Return OK or FAIL.
4656 */
4657 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004658eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004662 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
Bram Moolenaar33570922005-01-25 22:26:29 +00004664 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 int op;
4666 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004667#ifdef FEAT_FLOAT
4668 int use_float = FALSE;
4669 float_T f1 = 0, f2;
4670#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 /*
4674 * Get the first variable.
4675 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004676 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 return FAIL;
4678
4679 /*
4680 * Repeat computing, until no '*', '/' or '%' is following.
4681 */
4682 for (;;)
4683 {
4684 op = **arg;
4685 if (op != '*' && op != '/' && op != '%')
4686 break;
4687
4688 if (evaluate)
4689 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004690#ifdef FEAT_FLOAT
4691 if (rettv->v_type == VAR_FLOAT)
4692 {
4693 f1 = rettv->vval.v_float;
4694 use_float = TRUE;
4695 n1 = 0;
4696 }
4697 else
4698#endif
4699 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 if (error)
4702 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 else
4705 n1 = 0;
4706
4707 /*
4708 * Get the second variable.
4709 */
4710 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004711 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 return FAIL;
4713
4714 if (evaluate)
4715 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716#ifdef FEAT_FLOAT
4717 if (var2.v_type == VAR_FLOAT)
4718 {
4719 if (!use_float)
4720 {
4721 f1 = n1;
4722 use_float = TRUE;
4723 }
4724 f2 = var2.vval.v_float;
4725 n2 = 0;
4726 }
4727 else
4728#endif
4729 {
4730 n2 = get_tv_number_chk(&var2, &error);
4731 clear_tv(&var2);
4732 if (error)
4733 return FAIL;
4734#ifdef FEAT_FLOAT
4735 if (use_float)
4736 f2 = n2;
4737#endif
4738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
4740 /*
4741 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004742 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744#ifdef FEAT_FLOAT
4745 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004747 if (op == '*')
4748 f1 = f1 * f2;
4749 else if (op == '/')
4750 {
4751 /* We rely on the floating point library to handle divide
4752 * by zero to result in "inf" and not a crash. */
4753 f1 = f1 / f2;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004757 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758 return FAIL;
4759 }
4760 rettv->v_type = VAR_FLOAT;
4761 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004766 if (op == '*')
4767 n1 = n1 * n2;
4768 else if (op == '/')
4769 {
4770 if (n2 == 0) /* give an error message? */
4771 {
4772 if (n1 == 0)
4773 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4774 else if (n1 < 0)
4775 n1 = -0x7fffffffL;
4776 else
4777 n1 = 0x7fffffffL;
4778 }
4779 else
4780 n1 = n1 / n2;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783 {
4784 if (n2 == 0) /* give an error message? */
4785 n1 = 0;
4786 else
4787 n1 = n1 % n2;
4788 }
4789 rettv->v_type = VAR_NUMBER;
4790 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
4793 }
4794
4795 return OK;
4796}
4797
4798/*
4799 * Handle sixth level expression:
4800 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004801 * "string" string constant
4802 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 * &option-name option value
4804 * @r register contents
4805 * identifier variable value
4806 * function() function call
4807 * $VAR environment variable
4808 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004809 * [expr, expr] List
4810 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 *
4812 * Also handle:
4813 * ! in front logical NOT
4814 * - in front unary minus
4815 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 * trailing [] subscript in String or List
4817 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 *
4819 * "arg" must point to the first non-white of the expression.
4820 * "arg" is advanced to the next non-white after the recognized expression.
4821 *
4822 * Return OK or FAIL.
4823 */
4824 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004825eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004829 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 long n;
4832 int len;
4833 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 char_u *start_leader, *end_leader;
4835 int ret = OK;
4836 char_u *alias;
4837
4838 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004840 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004842 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
4844 /*
4845 * Skip '!' and '-' characters. They are handled later.
4846 */
4847 start_leader = *arg;
4848 while (**arg == '!' || **arg == '-' || **arg == '+')
4849 *arg = skipwhite(*arg + 1);
4850 end_leader = *arg;
4851
4852 switch (**arg)
4853 {
4854 /*
4855 * Number constant.
4856 */
4857 case '0':
4858 case '1':
4859 case '2':
4860 case '3':
4861 case '4':
4862 case '5':
4863 case '6':
4864 case '7':
4865 case '8':
4866 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867 {
4868#ifdef FEAT_FLOAT
4869 char_u *p = skipdigits(*arg + 1);
4870 int get_float = FALSE;
4871
4872 /* We accept a float when the format matches
4873 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004874 * strict to avoid backwards compatibility problems.
4875 * Don't look for a float after the "." operator, so that
4876 * ":let vers = 1.2.3" doesn't fail. */
4877 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 get_float = TRUE;
4880 p = skipdigits(p + 2);
4881 if (*p == 'e' || *p == 'E')
4882 {
4883 ++p;
4884 if (*p == '-' || *p == '+')
4885 ++p;
4886 if (!vim_isdigit(*p))
4887 get_float = FALSE;
4888 else
4889 p = skipdigits(p + 1);
4890 }
4891 if (ASCII_ISALPHA(*p) || *p == '.')
4892 get_float = FALSE;
4893 }
4894 if (get_float)
4895 {
4896 float_T f;
4897
4898 *arg += string2float(*arg, &f);
4899 if (evaluate)
4900 {
4901 rettv->v_type = VAR_FLOAT;
4902 rettv->vval.v_float = f;
4903 }
4904 }
4905 else
4906#endif
4907 {
4908 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4909 *arg += len;
4910 if (evaluate)
4911 {
4912 rettv->v_type = VAR_NUMBER;
4913 rettv->vval.v_number = n;
4914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919 /*
4920 * String constant: "string".
4921 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004922 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 break;
4924
4925 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004926 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004928 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004929 break;
4930
4931 /*
4932 * List: [expr, expr]
4933 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004934 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 break;
4936
4937 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 * Dictionary: {key: val, key: val}
4939 */
4940 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4941 break;
4942
4943 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004944 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004946 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 break;
4948
4949 /*
4950 * Environment variable: $VAR.
4951 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004952 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 break;
4954
4955 /*
4956 * Register contents: @r.
4957 */
4958 case '@': ++*arg;
4959 if (evaluate)
4960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004961 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004962 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 if (**arg != NUL)
4965 ++*arg;
4966 break;
4967
4968 /*
4969 * nested expression: (expression).
4970 */
4971 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004972 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (**arg == ')')
4974 ++*arg;
4975 else if (ret == OK)
4976 {
4977 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004978 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 ret = FAIL;
4980 }
4981 break;
4982
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 break;
4985 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986
4987 if (ret == NOTDONE)
4988 {
4989 /*
4990 * Must be a variable or function name.
4991 * Can also be a curly-braces kind of name: {expr}.
4992 */
4993 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004994 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 if (alias != NULL)
4996 s = alias;
4997
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004998 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 ret = FAIL;
5000 else
5001 {
5002 if (**arg == '(') /* recursive! */
5003 {
5004 /* If "s" is the name of a variable of type VAR_FUNC
5005 * use its contents. */
5006 s = deref_func_name(s, &len);
5007
5008 /* Invoke the function. */
5009 ret = get_func_tv(s, len, rettv, arg,
5010 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005011 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005012 /* Stop the expression evaluation when immediately
5013 * aborting on error, or when an interrupt occurred or
5014 * an exception was thrown but not caught. */
5015 if (aborting())
5016 {
5017 if (ret == OK)
5018 clear_tv(rettv);
5019 ret = FAIL;
5020 }
5021 }
5022 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005023 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005024 else
5025 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 }
5027
5028 if (alias != NULL)
5029 vim_free(alias);
5030 }
5031
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 *arg = skipwhite(*arg);
5033
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005034 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5035 * expr(expr). */
5036 if (ret == OK)
5037 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
5039 /*
5040 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5041 */
5042 if (ret == OK && evaluate && end_leader > start_leader)
5043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005044 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 int val = 0;
5046#ifdef FEAT_FLOAT
5047 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005048
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005049 if (rettv->v_type == VAR_FLOAT)
5050 f = rettv->vval.v_float;
5051 else
5052#endif
5053 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005054 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005056 clear_tv(rettv);
5057 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005059 else
5060 {
5061 while (end_leader > start_leader)
5062 {
5063 --end_leader;
5064 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005065 {
5066#ifdef FEAT_FLOAT
5067 if (rettv->v_type == VAR_FLOAT)
5068 f = !f;
5069 else
5070#endif
5071 val = !val;
5072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005073 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005074 {
5075#ifdef FEAT_FLOAT
5076 if (rettv->v_type == VAR_FLOAT)
5077 f = -f;
5078 else
5079#endif
5080 val = -val;
5081 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005082 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005083#ifdef FEAT_FLOAT
5084 if (rettv->v_type == VAR_FLOAT)
5085 {
5086 clear_tv(rettv);
5087 rettv->vval.v_float = f;
5088 }
5089 else
5090#endif
5091 {
5092 clear_tv(rettv);
5093 rettv->v_type = VAR_NUMBER;
5094 rettv->vval.v_number = val;
5095 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098
5099 return ret;
5100}
5101
5102/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005103 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5104 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005105 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5106 */
5107 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005108eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005109 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005110 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005111 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005112 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113{
5114 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005115 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005116 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 long len = -1;
5118 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005122 if (rettv->v_type == VAR_FUNC
5123#ifdef FEAT_FLOAT
5124 || rettv->v_type == VAR_FLOAT
5125#endif
5126 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005128 if (verbose)
5129 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005130 return FAIL;
5131 }
5132
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005134 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 /*
5136 * dict.name
5137 */
5138 key = *arg + 1;
5139 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5140 ;
5141 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 }
5145 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 /*
5148 * something[idx]
5149 *
5150 * Get the (first) variable from inside the [].
5151 */
5152 *arg = skipwhite(*arg + 1);
5153 if (**arg == ':')
5154 empty1 = TRUE;
5155 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5156 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005157 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5158 {
5159 /* not a number or string */
5160 clear_tv(&var1);
5161 return FAIL;
5162 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163
5164 /*
5165 * Get the second variable from inside the [:].
5166 */
5167 if (**arg == ':')
5168 {
5169 range = TRUE;
5170 *arg = skipwhite(*arg + 1);
5171 if (**arg == ']')
5172 empty2 = TRUE;
5173 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175 if (!empty1)
5176 clear_tv(&var1);
5177 return FAIL;
5178 }
5179 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5180 {
5181 /* not a number or string */
5182 if (!empty1)
5183 clear_tv(&var1);
5184 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 return FAIL;
5186 }
5187 }
5188
5189 /* Check for the ']'. */
5190 if (**arg != ']')
5191 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005192 if (verbose)
5193 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 clear_tv(&var1);
5195 if (range)
5196 clear_tv(&var2);
5197 return FAIL;
5198 }
5199 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005200 }
5201
5202 if (evaluate)
5203 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 n1 = 0;
5205 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 n1 = get_tv_number(&var1);
5208 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209 }
5210 if (range)
5211 {
5212 if (empty2)
5213 n2 = -1;
5214 else
5215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005216 n2 = get_tv_number(&var2);
5217 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218 }
5219 }
5220
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005221 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222 {
5223 case VAR_NUMBER:
5224 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005225 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 len = (long)STRLEN(s);
5227 if (range)
5228 {
5229 /* The resulting variable is a substring. If the indexes
5230 * are out of range the result is empty. */
5231 if (n1 < 0)
5232 {
5233 n1 = len + n1;
5234 if (n1 < 0)
5235 n1 = 0;
5236 }
5237 if (n2 < 0)
5238 n2 = len + n2;
5239 else if (n2 >= len)
5240 n2 = len;
5241 if (n1 >= len || n2 < 0 || n1 > n2)
5242 s = NULL;
5243 else
5244 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5245 }
5246 else
5247 {
5248 /* The resulting variable is a string of a single
5249 * character. If the index is too big or negative the
5250 * result is empty. */
5251 if (n1 >= len || n1 < 0)
5252 s = NULL;
5253 else
5254 s = vim_strnsave(s + n1, 1);
5255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 clear_tv(rettv);
5257 rettv->v_type = VAR_STRING;
5258 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 break;
5260
5261 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005262 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 if (n1 < 0)
5264 n1 = len + n1;
5265 if (!empty1 && (n1 < 0 || n1 >= len))
5266 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005267 /* For a range we allow invalid values and return an empty
5268 * list. A list index out of range is an error. */
5269 if (!range)
5270 {
5271 if (verbose)
5272 EMSGN(_(e_listidx), n1);
5273 return FAIL;
5274 }
5275 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 }
5277 if (range)
5278 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 list_T *l;
5280 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281
5282 if (n2 < 0)
5283 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005284 else if (n2 >= len)
5285 n2 = len - 1;
5286 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005287 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 l = list_alloc();
5289 if (l == NULL)
5290 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005291 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 n1 <= n2; ++n1)
5293 {
5294 if (list_append_tv(l, &item->li_tv) == FAIL)
5295 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005296 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 return FAIL;
5298 }
5299 item = item->li_next;
5300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301 clear_tv(rettv);
5302 rettv->v_type = VAR_LIST;
5303 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005304 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 }
5306 else
5307 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005308 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005309 clear_tv(rettv);
5310 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311 }
5312 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313
5314 case VAR_DICT:
5315 if (range)
5316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005317 if (verbose)
5318 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319 if (len == -1)
5320 clear_tv(&var1);
5321 return FAIL;
5322 }
5323 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325
5326 if (len == -1)
5327 {
5328 key = get_tv_string(&var1);
5329 if (*key == NUL)
5330 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005331 if (verbose)
5332 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 clear_tv(&var1);
5334 return FAIL;
5335 }
5336 }
5337
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005338 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005340 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005341 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 if (len == -1)
5343 clear_tv(&var1);
5344 if (item == NULL)
5345 return FAIL;
5346
5347 copy_tv(&item->di_tv, &var1);
5348 clear_tv(rettv);
5349 *rettv = var1;
5350 }
5351 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 }
5353 }
5354
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 return OK;
5356}
5357
5358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 * Get an option value.
5360 * "arg" points to the '&' or '+' before the option name.
5361 * "arg" is advanced to character after the option name.
5362 * Return OK or FAIL.
5363 */
5364 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005367 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 int evaluate;
5369{
5370 char_u *option_end;
5371 long numval;
5372 char_u *stringval;
5373 int opt_type;
5374 int c;
5375 int working = (**arg == '+'); /* has("+option") */
5376 int ret = OK;
5377 int opt_flags;
5378
5379 /*
5380 * Isolate the option name and find its value.
5381 */
5382 option_end = find_option_end(arg, &opt_flags);
5383 if (option_end == NULL)
5384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 EMSG2(_("E112: Option name missing: %s"), *arg);
5387 return FAIL;
5388 }
5389
5390 if (!evaluate)
5391 {
5392 *arg = option_end;
5393 return OK;
5394 }
5395
5396 c = *option_end;
5397 *option_end = NUL;
5398 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401 if (opt_type == -3) /* invalid name */
5402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 EMSG2(_("E113: Unknown option: %s"), *arg);
5405 ret = FAIL;
5406 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005407 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 if (opt_type == -2) /* hidden string option */
5410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005411 rettv->v_type = VAR_STRING;
5412 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 }
5414 else if (opt_type == -1) /* hidden number option */
5415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005416 rettv->v_type = VAR_NUMBER;
5417 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
5419 else if (opt_type == 1) /* number option */
5420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005421 rettv->v_type = VAR_NUMBER;
5422 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424 else /* string option */
5425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005426 rettv->v_type = VAR_STRING;
5427 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
5429 }
5430 else if (working && (opt_type == -2 || opt_type == -1))
5431 ret = FAIL;
5432
5433 *option_end = c; /* put back for error messages */
5434 *arg = option_end;
5435
5436 return ret;
5437}
5438
5439/*
5440 * Allocate a variable for a string constant.
5441 * Return OK or FAIL.
5442 */
5443 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 int evaluate;
5448{
5449 char_u *p;
5450 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 int extra = 0;
5452
5453 /*
5454 * Find the end of the string, skipping backslashed characters.
5455 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005456 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 if (*p == '\\' && p[1] != NUL)
5459 {
5460 ++p;
5461 /* A "\<x>" form occupies at least 4 characters, and produces up
5462 * to 6 characters: reserve space for 2 extra */
5463 if (*p == '<')
5464 extra += 2;
5465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467
5468 if (*p != '"')
5469 {
5470 EMSG2(_("E114: Missing quote: %s"), *arg);
5471 return FAIL;
5472 }
5473
5474 /* If only parsing, set *arg and return here */
5475 if (!evaluate)
5476 {
5477 *arg = p + 1;
5478 return OK;
5479 }
5480
5481 /*
5482 * Copy the string into allocated memory, handling backslashed
5483 * characters.
5484 */
5485 name = alloc((unsigned)(p - *arg + extra));
5486 if (name == NULL)
5487 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 rettv->v_type = VAR_STRING;
5489 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {
5493 if (*p == '\\')
5494 {
5495 switch (*++p)
5496 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 case 'b': *name++ = BS; ++p; break;
5498 case 'e': *name++ = ESC; ++p; break;
5499 case 'f': *name++ = FF; ++p; break;
5500 case 'n': *name++ = NL; ++p; break;
5501 case 'r': *name++ = CAR; ++p; break;
5502 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
5504 case 'X': /* hex: "\x1", "\x12" */
5505 case 'x':
5506 case 'u': /* Unicode: "\u0023" */
5507 case 'U':
5508 if (vim_isxdigit(p[1]))
5509 {
5510 int n, nr;
5511 int c = toupper(*p);
5512
5513 if (c == 'X')
5514 n = 2;
5515 else
5516 n = 4;
5517 nr = 0;
5518 while (--n >= 0 && vim_isxdigit(p[1]))
5519 {
5520 ++p;
5521 nr = (nr << 4) + hex2nr(*p);
5522 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005523 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524#ifdef FEAT_MBYTE
5525 /* For "\u" store the number according to
5526 * 'encoding'. */
5527 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005528 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 else
5530#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005531 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 break;
5534
5535 /* octal: "\1", "\12", "\123" */
5536 case '0':
5537 case '1':
5538 case '2':
5539 case '3':
5540 case '4':
5541 case '5':
5542 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005543 case '7': *name = *p++ - '0';
5544 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546 *name = (*name << 3) + *p++ - '0';
5547 if (*p >= '0' && *p <= '7')
5548 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 break;
5552
5553 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005554 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 if (extra != 0)
5556 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005557 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 break;
5559 }
5560 /* FALLTHROUGH */
5561
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 break;
5564 }
5565 }
5566 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 *arg = p + 1;
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 return OK;
5574}
5575
5576/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 * Return OK or FAIL.
5579 */
5580 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 int evaluate;
5585{
5586 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005587 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005588 int reduce = 0;
5589
5590 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005592 */
5593 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005596 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005598 break;
5599 ++reduce;
5600 ++p;
5601 }
5602 }
5603
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005605 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005607 return FAIL;
5608 }
5609
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005611 if (!evaluate)
5612 {
5613 *arg = p + 1;
5614 return OK;
5615 }
5616
5617 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005618 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005619 */
5620 str = alloc((unsigned)((p - *arg) - reduce));
5621 if (str == NULL)
5622 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 rettv->v_type = VAR_STRING;
5624 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005625
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005628 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005631 break;
5632 ++p;
5633 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005635 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005637 *arg = p + 1;
5638
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005639 return OK;
5640}
5641
5642/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005643 * Allocate a variable for a List and fill it from "*arg".
5644 * Return OK or FAIL.
5645 */
5646 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005649 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650 int evaluate;
5651{
Bram Moolenaar33570922005-01-25 22:26:29 +00005652 list_T *l = NULL;
5653 typval_T tv;
5654 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005655
5656 if (evaluate)
5657 {
5658 l = list_alloc();
5659 if (l == NULL)
5660 return FAIL;
5661 }
5662
5663 *arg = skipwhite(*arg + 1);
5664 while (**arg != ']' && **arg != NUL)
5665 {
5666 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5667 goto failret;
5668 if (evaluate)
5669 {
5670 item = listitem_alloc();
5671 if (item != NULL)
5672 {
5673 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005674 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 list_append(l, item);
5676 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005677 else
5678 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 }
5680
5681 if (**arg == ']')
5682 break;
5683 if (**arg != ',')
5684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 goto failret;
5687 }
5688 *arg = skipwhite(*arg + 1);
5689 }
5690
5691 if (**arg != ']')
5692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694failret:
5695 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005696 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005697 return FAIL;
5698 }
5699
5700 *arg = skipwhite(*arg + 1);
5701 if (evaluate)
5702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005703 rettv->v_type = VAR_LIST;
5704 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705 ++l->lv_refcount;
5706 }
5707
5708 return OK;
5709}
5710
5711/*
5712 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005713 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005715 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716list_alloc()
5717{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005718 list_T *l;
5719
5720 l = (list_T *)alloc_clear(sizeof(list_T));
5721 if (l != NULL)
5722 {
5723 /* Prepend the list to the list of lists for garbage collection. */
5724 if (first_list != NULL)
5725 first_list->lv_used_prev = l;
5726 l->lv_used_prev = NULL;
5727 l->lv_used_next = first_list;
5728 first_list = l;
5729 }
5730 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731}
5732
5733/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005734 * Allocate an empty list for a return value.
5735 * Returns OK or FAIL.
5736 */
5737 static int
5738rettv_list_alloc(rettv)
5739 typval_T *rettv;
5740{
5741 list_T *l = list_alloc();
5742
5743 if (l == NULL)
5744 return FAIL;
5745
5746 rettv->vval.v_list = l;
5747 rettv->v_type = VAR_LIST;
5748 ++l->lv_refcount;
5749 return OK;
5750}
5751
5752/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 * Unreference a list: decrement the reference count and free it when it
5754 * becomes zero.
5755 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005756 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005758 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005760 if (l != NULL && --l->lv_refcount <= 0)
5761 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005762}
5763
5764/*
5765 * Free a list, including all items it points to.
5766 * Ignores the reference count.
5767 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005768 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005769list_free(l, recurse)
5770 list_T *l;
5771 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772{
Bram Moolenaar33570922005-01-25 22:26:29 +00005773 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005775 /* Remove the list from the list of lists for garbage collection. */
5776 if (l->lv_used_prev == NULL)
5777 first_list = l->lv_used_next;
5778 else
5779 l->lv_used_prev->lv_used_next = l->lv_used_next;
5780 if (l->lv_used_next != NULL)
5781 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5782
Bram Moolenaard9fba312005-06-26 22:34:35 +00005783 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005785 /* Remove the item before deleting it. */
5786 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005787 if (recurse || (item->li_tv.v_type != VAR_LIST
5788 && item->li_tv.v_type != VAR_DICT))
5789 clear_tv(&item->li_tv);
5790 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 }
5792 vim_free(l);
5793}
5794
5795/*
5796 * Allocate a list item.
5797 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799listitem_alloc()
5800{
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802}
5803
5804/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005805 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 */
5807 static void
5808listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005811 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005812 vim_free(item);
5813}
5814
5815/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005816 * Remove a list item from a List and free it. Also clears the value.
5817 */
5818 static void
5819listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 list_T *l;
5821 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005822{
5823 list_remove(l, item, item);
5824 listitem_free(item);
5825}
5826
5827/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 * Get the number of items in a list.
5829 */
5830 static long
5831list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005832 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834 if (l == NULL)
5835 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005836 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837}
5838
5839/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840 * Return TRUE when two lists have exactly the same values.
5841 */
5842 static int
5843list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005844 list_T *l1;
5845 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005846 int ic; /* ignore case for strings */
5847{
Bram Moolenaar33570922005-01-25 22:26:29 +00005848 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005849
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005850 if (l1 == NULL || l2 == NULL)
5851 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005852 if (l1 == l2)
5853 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005854 if (list_len(l1) != list_len(l2))
5855 return FALSE;
5856
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005857 for (item1 = l1->lv_first, item2 = l2->lv_first;
5858 item1 != NULL && item2 != NULL;
5859 item1 = item1->li_next, item2 = item2->li_next)
5860 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5861 return FALSE;
5862 return item1 == NULL && item2 == NULL;
5863}
5864
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005865#if defined(FEAT_PYTHON) || defined(PROTO)
5866/*
5867 * Return the dictitem that an entry in a hashtable points to.
5868 */
5869 dictitem_T *
5870dict_lookup(hi)
5871 hashitem_T *hi;
5872{
5873 return HI2DI(hi);
5874}
5875#endif
5876
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005877/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005878 * Return TRUE when two dictionaries have exactly the same key/values.
5879 */
5880 static int
5881dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005882 dict_T *d1;
5883 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005884 int ic; /* ignore case for strings */
5885{
Bram Moolenaar33570922005-01-25 22:26:29 +00005886 hashitem_T *hi;
5887 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005888 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005889
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005890 if (d1 == NULL || d2 == NULL)
5891 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005892 if (d1 == d2)
5893 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005894 if (dict_len(d1) != dict_len(d2))
5895 return FALSE;
5896
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005897 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005899 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005900 if (!HASHITEM_EMPTY(hi))
5901 {
5902 item2 = dict_find(d2, hi->hi_key, -1);
5903 if (item2 == NULL)
5904 return FALSE;
5905 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5906 return FALSE;
5907 --todo;
5908 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005909 }
5910 return TRUE;
5911}
5912
5913/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005914 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005915 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005916 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005917 */
5918 static int
5919tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 typval_T *tv1;
5921 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005922 int ic; /* ignore case */
5923{
5924 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005925 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005926 static int recursive = 0; /* cach recursive loops */
5927 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005929 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005930 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005931 /* Catch lists and dicts that have an endless loop by limiting
5932 * recursiveness to 1000. We guess they are equal then. */
5933 if (recursive >= 1000)
5934 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005935
5936 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005937 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005938 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005939 ++recursive;
5940 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5941 --recursive;
5942 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005943
5944 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005945 ++recursive;
5946 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5947 --recursive;
5948 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005949
5950 case VAR_FUNC:
5951 return (tv1->vval.v_string != NULL
5952 && tv2->vval.v_string != NULL
5953 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5954
5955 case VAR_NUMBER:
5956 return tv1->vval.v_number == tv2->vval.v_number;
5957
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005958#ifdef FEAT_FLOAT
5959 case VAR_FLOAT:
5960 return tv1->vval.v_float == tv2->vval.v_float;
5961#endif
5962
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005963 case VAR_STRING:
5964 s1 = get_tv_string_buf(tv1, buf1);
5965 s2 = get_tv_string_buf(tv2, buf2);
5966 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005968
5969 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005970 return TRUE;
5971}
5972
5973/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 * Locate item with index "n" in list "l" and return it.
5975 * A negative index is counted from the end; -1 is the last item.
5976 * Returns NULL when "n" is out of range.
5977 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005978 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 long n;
5982{
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 long idx;
5985
5986 if (l == NULL)
5987 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005988
5989 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005991 n = l->lv_len + n;
5992
5993 /* Check for index out of range. */
5994 if (n < 0 || n >= l->lv_len)
5995 return NULL;
5996
5997 /* When there is a cached index may start search from there. */
5998 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006000 if (n < l->lv_idx / 2)
6001 {
6002 /* closest to the start of the list */
6003 item = l->lv_first;
6004 idx = 0;
6005 }
6006 else if (n > (l->lv_idx + l->lv_len) / 2)
6007 {
6008 /* closest to the end of the list */
6009 item = l->lv_last;
6010 idx = l->lv_len - 1;
6011 }
6012 else
6013 {
6014 /* closest to the cached index */
6015 item = l->lv_idx_item;
6016 idx = l->lv_idx;
6017 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 }
6019 else
6020 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006021 if (n < l->lv_len / 2)
6022 {
6023 /* closest to the start of the list */
6024 item = l->lv_first;
6025 idx = 0;
6026 }
6027 else
6028 {
6029 /* closest to the end of the list */
6030 item = l->lv_last;
6031 idx = l->lv_len - 1;
6032 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006034
6035 while (n > idx)
6036 {
6037 /* search forward */
6038 item = item->li_next;
6039 ++idx;
6040 }
6041 while (n < idx)
6042 {
6043 /* search backward */
6044 item = item->li_prev;
6045 --idx;
6046 }
6047
6048 /* cache the used index */
6049 l->lv_idx = idx;
6050 l->lv_idx_item = item;
6051
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 return item;
6053}
6054
6055/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006056 * Get list item "l[idx]" as a number.
6057 */
6058 static long
6059list_find_nr(l, idx, errorp)
6060 list_T *l;
6061 long idx;
6062 int *errorp; /* set to TRUE when something wrong */
6063{
6064 listitem_T *li;
6065
6066 li = list_find(l, idx);
6067 if (li == NULL)
6068 {
6069 if (errorp != NULL)
6070 *errorp = TRUE;
6071 return -1L;
6072 }
6073 return get_tv_number_chk(&li->li_tv, errorp);
6074}
6075
6076/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006077 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6078 */
6079 char_u *
6080list_find_str(l, idx)
6081 list_T *l;
6082 long idx;
6083{
6084 listitem_T *li;
6085
6086 li = list_find(l, idx - 1);
6087 if (li == NULL)
6088 {
6089 EMSGN(_(e_listidx), idx);
6090 return NULL;
6091 }
6092 return get_tv_string(&li->li_tv);
6093}
6094
6095/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006096 * Locate "item" list "l" and return its index.
6097 * Returns -1 when "item" is not in the list.
6098 */
6099 static long
6100list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 list_T *l;
6102 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006103{
6104 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006106
6107 if (l == NULL)
6108 return -1;
6109 idx = 0;
6110 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6111 ++idx;
6112 if (li == NULL)
6113 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006114 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006115}
6116
6117/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118 * Append item "item" to the end of list "l".
6119 */
6120 static void
6121list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 list_T *l;
6123 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006124{
6125 if (l->lv_last == NULL)
6126 {
6127 /* empty list */
6128 l->lv_first = item;
6129 l->lv_last = item;
6130 item->li_prev = NULL;
6131 }
6132 else
6133 {
6134 l->lv_last->li_next = item;
6135 item->li_prev = l->lv_last;
6136 l->lv_last = item;
6137 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006138 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 item->li_next = NULL;
6140}
6141
6142/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006144 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 */
6146 static int
6147list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 list_T *l;
6149 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006151 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006152
Bram Moolenaar05159a02005-02-26 23:04:13 +00006153 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006155 copy_tv(tv, &li->li_tv);
6156 list_append(l, li);
6157 return OK;
6158}
6159
6160/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006161 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006162 * Return FAIL when out of memory.
6163 */
6164 int
6165list_append_dict(list, dict)
6166 list_T *list;
6167 dict_T *dict;
6168{
6169 listitem_T *li = listitem_alloc();
6170
6171 if (li == NULL)
6172 return FAIL;
6173 li->li_tv.v_type = VAR_DICT;
6174 li->li_tv.v_lock = 0;
6175 li->li_tv.vval.v_dict = dict;
6176 list_append(list, li);
6177 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 return OK;
6179}
6180
6181/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006182 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006183 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006184 * Returns FAIL when out of memory.
6185 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006186 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006187list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006188 list_T *l;
6189 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006190 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006191{
6192 listitem_T *li = listitem_alloc();
6193
6194 if (li == NULL)
6195 return FAIL;
6196 list_append(l, li);
6197 li->li_tv.v_type = VAR_STRING;
6198 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006199 if (str == NULL)
6200 li->li_tv.vval.v_string = NULL;
6201 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006202 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006203 return FAIL;
6204 return OK;
6205}
6206
6207/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006208 * Append "n" to list "l".
6209 * Returns FAIL when out of memory.
6210 */
6211 static int
6212list_append_number(l, n)
6213 list_T *l;
6214 varnumber_T n;
6215{
6216 listitem_T *li;
6217
6218 li = listitem_alloc();
6219 if (li == NULL)
6220 return FAIL;
6221 li->li_tv.v_type = VAR_NUMBER;
6222 li->li_tv.v_lock = 0;
6223 li->li_tv.vval.v_number = n;
6224 list_append(l, li);
6225 return OK;
6226}
6227
6228/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 * If "item" is NULL append at the end.
6231 * Return FAIL when out of memory.
6232 */
6233 static int
6234list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 list_T *l;
6236 typval_T *tv;
6237 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006238{
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240
6241 if (ni == NULL)
6242 return FAIL;
6243 copy_tv(tv, &ni->li_tv);
6244 if (item == NULL)
6245 /* Append new item at end of list. */
6246 list_append(l, ni);
6247 else
6248 {
6249 /* Insert new item before existing item. */
6250 ni->li_prev = item->li_prev;
6251 ni->li_next = item;
6252 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006253 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006254 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006255 ++l->lv_idx;
6256 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006257 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006258 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006259 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260 l->lv_idx_item = NULL;
6261 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006264 }
6265 return OK;
6266}
6267
6268/*
6269 * Extend "l1" with "l2".
6270 * If "bef" is NULL append at the end, otherwise insert before this item.
6271 * Returns FAIL when out of memory.
6272 */
6273 static int
6274list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 list_T *l1;
6276 list_T *l2;
6277 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006278{
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006280 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006281
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006282 /* We also quit the loop when we have inserted the original item count of
6283 * the list, avoid a hang when we extend a list with itself. */
6284 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006285 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6286 return FAIL;
6287 return OK;
6288}
6289
6290/*
6291 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6292 * Return FAIL when out of memory.
6293 */
6294 static int
6295list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 list_T *l1;
6297 list_T *l2;
6298 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006299{
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006301
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006302 if (l1 == NULL || l2 == NULL)
6303 return FAIL;
6304
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006305 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006306 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006307 if (l == NULL)
6308 return FAIL;
6309 tv->v_type = VAR_LIST;
6310 tv->vval.v_list = l;
6311
6312 /* append all items from the second list */
6313 return list_extend(l, l2, NULL);
6314}
6315
6316/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006317 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006319 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320 * Returns NULL when out of memory.
6321 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006323list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006324 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006326 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327{
Bram Moolenaar33570922005-01-25 22:26:29 +00006328 list_T *copy;
6329 listitem_T *item;
6330 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331
6332 if (orig == NULL)
6333 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334
6335 copy = list_alloc();
6336 if (copy != NULL)
6337 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006338 if (copyID != 0)
6339 {
6340 /* Do this before adding the items, because one of the items may
6341 * refer back to this list. */
6342 orig->lv_copyID = copyID;
6343 orig->lv_copylist = copy;
6344 }
6345 for (item = orig->lv_first; item != NULL && !got_int;
6346 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347 {
6348 ni = listitem_alloc();
6349 if (ni == NULL)
6350 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006351 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006352 {
6353 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6354 {
6355 vim_free(ni);
6356 break;
6357 }
6358 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006360 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006361 list_append(copy, ni);
6362 }
6363 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006364 if (item != NULL)
6365 {
6366 list_unref(copy);
6367 copy = NULL;
6368 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006369 }
6370
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 return copy;
6372}
6373
6374/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006376 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006378 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006379list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 list_T *l;
6381 listitem_T *item;
6382 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006383{
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006385
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006386 /* notify watchers */
6387 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006389 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006390 list_fix_watch(l, ip);
6391 if (ip == item2)
6392 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006394
6395 if (item2->li_next == NULL)
6396 l->lv_last = item->li_prev;
6397 else
6398 item2->li_next->li_prev = item->li_prev;
6399 if (item->li_prev == NULL)
6400 l->lv_first = item2->li_next;
6401 else
6402 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006403 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404}
6405
6406/*
6407 * Return an allocated string with the string representation of a list.
6408 * May return NULL.
6409 */
6410 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006411list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006413 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414{
6415 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416
6417 if (tv->vval.v_list == NULL)
6418 return NULL;
6419 ga_init2(&ga, (int)sizeof(char), 80);
6420 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006421 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006422 {
6423 vim_free(ga.ga_data);
6424 return NULL;
6425 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 ga_append(&ga, ']');
6427 ga_append(&ga, NUL);
6428 return (char_u *)ga.ga_data;
6429}
6430
6431/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006432 * Join list "l" into a string in "*gap", using separator "sep".
6433 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006435 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006436 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006437list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006438 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006440 char_u *sep;
6441 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006442 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006443{
6444 int first = TRUE;
6445 char_u *tofree;
6446 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006448 char_u *s;
6449
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006450 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006451 {
6452 if (first)
6453 first = FALSE;
6454 else
6455 ga_concat(gap, sep);
6456
6457 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006458 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006459 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006460 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006461 if (s != NULL)
6462 ga_concat(gap, s);
6463 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006464 if (s == NULL)
6465 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006466 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006468}
6469
6470/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006471 * Garbage collection for lists and dictionaries.
6472 *
6473 * We use reference counts to be able to free most items right away when they
6474 * are no longer used. But for composite items it's possible that it becomes
6475 * unused while the reference count is > 0: When there is a recursive
6476 * reference. Example:
6477 * :let l = [1, 2, 3]
6478 * :let d = {9: l}
6479 * :let l[1] = d
6480 *
6481 * Since this is quite unusual we handle this with garbage collection: every
6482 * once in a while find out which lists and dicts are not referenced from any
6483 * variable.
6484 *
6485 * Here is a good reference text about garbage collection (refers to Python
6486 * but it applies to all reference-counting mechanisms):
6487 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006488 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006489
6490/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006491 * Do garbage collection for lists and dicts.
6492 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006493 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006494 int
6495garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006496{
6497 dict_T *dd;
6498 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006499 int copyID = ++current_copyID;
6500 buf_T *buf;
6501 win_T *wp;
6502 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006503 funccall_T *fc, **pfc;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006504 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006505#ifdef FEAT_WINDOWS
6506 tabpage_T *tp;
6507#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006508
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006509 /* Only do this once. */
6510 want_garbage_collect = FALSE;
6511 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006512 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006513
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006514 /*
6515 * 1. Go through all accessible variables and mark all lists and dicts
6516 * with copyID.
6517 */
6518 /* script-local variables */
6519 for (i = 1; i <= ga_scripts.ga_len; ++i)
6520 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6521
6522 /* buffer-local variables */
6523 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6524 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6525
6526 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006527 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006528 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6529
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006530#ifdef FEAT_WINDOWS
6531 /* tabpage-local variables */
6532 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6533 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6534#endif
6535
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006536 /* global variables */
6537 set_ref_in_ht(&globvarht, copyID);
6538
6539 /* function-local variables */
6540 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6541 {
6542 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6543 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6544 }
6545
Bram Moolenaard812df62008-11-09 12:46:09 +00006546 /* v: vars */
6547 set_ref_in_ht(&vimvarht, copyID);
6548
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006549 /*
6550 * 2. Go through the list of dicts and free items without the copyID.
6551 */
6552 for (dd = first_dict; dd != NULL; )
6553 if (dd->dv_copyID != copyID)
6554 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006555 /* Free the Dictionary and ordinary items it contains, but don't
6556 * recurse into Lists and Dictionaries, they will be in the list
6557 * of dicts or list of lists. */
6558 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006559 did_free = TRUE;
6560
6561 /* restart, next dict may also have been freed */
6562 dd = first_dict;
6563 }
6564 else
6565 dd = dd->dv_used_next;
6566
6567 /*
6568 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006569 * But don't free a list that has a watcher (used in a for loop), these
6570 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006571 */
6572 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006573 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006574 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006575 /* Free the List and ordinary items it contains, but don't recurse
6576 * into Lists and Dictionaries, they will be in the list of dicts
6577 * or list of lists. */
6578 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006579 did_free = TRUE;
6580
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006581 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006582 ll = first_list;
6583 }
6584 else
6585 ll = ll->lv_used_next;
6586
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006587 /* check if any funccal can be freed now */
6588 for (pfc = &previous_funccal; *pfc != NULL; )
6589 {
6590 if (can_free_funccal(*pfc, copyID))
6591 {
6592 fc = *pfc;
6593 *pfc = fc->caller;
6594 free_funccal(fc, TRUE);
6595 did_free = TRUE;
6596 }
6597 else
6598 pfc = &(*pfc)->caller;
6599 }
6600
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006601 return did_free;
6602}
6603
6604/*
6605 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6606 */
6607 static void
6608set_ref_in_ht(ht, copyID)
6609 hashtab_T *ht;
6610 int copyID;
6611{
6612 int todo;
6613 hashitem_T *hi;
6614
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006615 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006616 for (hi = ht->ht_array; todo > 0; ++hi)
6617 if (!HASHITEM_EMPTY(hi))
6618 {
6619 --todo;
6620 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6621 }
6622}
6623
6624/*
6625 * Mark all lists and dicts referenced through list "l" with "copyID".
6626 */
6627 static void
6628set_ref_in_list(l, copyID)
6629 list_T *l;
6630 int copyID;
6631{
6632 listitem_T *li;
6633
6634 for (li = l->lv_first; li != NULL; li = li->li_next)
6635 set_ref_in_item(&li->li_tv, copyID);
6636}
6637
6638/*
6639 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6640 */
6641 static void
6642set_ref_in_item(tv, copyID)
6643 typval_T *tv;
6644 int copyID;
6645{
6646 dict_T *dd;
6647 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006648
6649 switch (tv->v_type)
6650 {
6651 case VAR_DICT:
6652 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006653 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006654 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006655 /* Didn't see this dict yet. */
6656 dd->dv_copyID = copyID;
6657 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006658 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006659 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006660
6661 case VAR_LIST:
6662 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006663 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006664 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006665 /* Didn't see this list yet. */
6666 ll->lv_copyID = copyID;
6667 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006668 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006669 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006670 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006671 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006672}
6673
6674/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006675 * Allocate an empty header for a dictionary.
6676 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006677 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006678dict_alloc()
6679{
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006681
Bram Moolenaar33570922005-01-25 22:26:29 +00006682 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006683 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006684 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006685 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006686 if (first_dict != NULL)
6687 first_dict->dv_used_prev = d;
6688 d->dv_used_next = first_dict;
6689 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006690 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006691
Bram Moolenaar33570922005-01-25 22:26:29 +00006692 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006693 d->dv_lock = 0;
6694 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006695 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006696 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006697 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006698}
6699
6700/*
6701 * Unreference a Dictionary: decrement the reference count and free it when it
6702 * becomes zero.
6703 */
6704 static void
6705dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006706 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006708 if (d != NULL && --d->dv_refcount <= 0)
6709 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006710}
6711
6712/*
6713 * Free a Dictionary, including all items it contains.
6714 * Ignores the reference count.
6715 */
6716 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006717dict_free(d, recurse)
6718 dict_T *d;
6719 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006720{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006721 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006722 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006723 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006724
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006725 /* Remove the dict from the list of dicts for garbage collection. */
6726 if (d->dv_used_prev == NULL)
6727 first_dict = d->dv_used_next;
6728 else
6729 d->dv_used_prev->dv_used_next = d->dv_used_next;
6730 if (d->dv_used_next != NULL)
6731 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6732
6733 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006735 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006736 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006737 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006738 if (!HASHITEM_EMPTY(hi))
6739 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006740 /* Remove the item before deleting it, just in case there is
6741 * something recursive causing trouble. */
6742 di = HI2DI(hi);
6743 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006744 if (recurse || (di->di_tv.v_type != VAR_LIST
6745 && di->di_tv.v_type != VAR_DICT))
6746 clear_tv(&di->di_tv);
6747 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006748 --todo;
6749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006750 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006751 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752 vim_free(d);
6753}
6754
6755/*
6756 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006757 * The "key" is copied to the new item.
6758 * Note that the value of the item "di_tv" still needs to be initialized!
6759 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006760 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006761 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006762dictitem_alloc(key)
6763 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006764{
Bram Moolenaar33570922005-01-25 22:26:29 +00006765 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006766
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006767 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006768 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006769 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006770 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006771 di->di_flags = 0;
6772 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006773 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774}
6775
6776/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006777 * Make a copy of a Dictionary item.
6778 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006780dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006781 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006782{
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006784
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006785 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6786 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006787 if (di != NULL)
6788 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006789 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006790 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006791 copy_tv(&org->di_tv, &di->di_tv);
6792 }
6793 return di;
6794}
6795
6796/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006797 * Remove item "item" from Dictionary "dict" and free it.
6798 */
6799 static void
6800dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006801 dict_T *dict;
6802 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006803{
Bram Moolenaar33570922005-01-25 22:26:29 +00006804 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006805
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006807 if (HASHITEM_EMPTY(hi))
6808 EMSG2(_(e_intern2), "dictitem_remove()");
6809 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006810 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006811 dictitem_free(item);
6812}
6813
6814/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006815 * Free a dict item. Also clears the value.
6816 */
6817 static void
6818dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006819 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006820{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006821 clear_tv(&item->di_tv);
6822 vim_free(item);
6823}
6824
6825/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006826 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6827 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006828 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006829 * Returns NULL when out of memory.
6830 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006831 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006832dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006833 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006834 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006835 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006836{
Bram Moolenaar33570922005-01-25 22:26:29 +00006837 dict_T *copy;
6838 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006839 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006840 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841
6842 if (orig == NULL)
6843 return NULL;
6844
6845 copy = dict_alloc();
6846 if (copy != NULL)
6847 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006848 if (copyID != 0)
6849 {
6850 orig->dv_copyID = copyID;
6851 orig->dv_copydict = copy;
6852 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006853 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006854 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006855 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006856 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006857 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006858 --todo;
6859
6860 di = dictitem_alloc(hi->hi_key);
6861 if (di == NULL)
6862 break;
6863 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006864 {
6865 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6866 copyID) == FAIL)
6867 {
6868 vim_free(di);
6869 break;
6870 }
6871 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006872 else
6873 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6874 if (dict_add(copy, di) == FAIL)
6875 {
6876 dictitem_free(di);
6877 break;
6878 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006880 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006881
Bram Moolenaare9a41262005-01-15 22:18:47 +00006882 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006883 if (todo > 0)
6884 {
6885 dict_unref(copy);
6886 copy = NULL;
6887 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006888 }
6889
6890 return copy;
6891}
6892
6893/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006894 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006895 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006896 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006897 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006898dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006899 dict_T *d;
6900 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006901{
Bram Moolenaar33570922005-01-25 22:26:29 +00006902 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006903}
6904
Bram Moolenaar8c711452005-01-14 21:53:12 +00006905/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006906 * Add a number or string entry to dictionary "d".
6907 * When "str" is NULL use number "nr", otherwise use "str".
6908 * Returns FAIL when out of memory and when key already exists.
6909 */
6910 int
6911dict_add_nr_str(d, key, nr, str)
6912 dict_T *d;
6913 char *key;
6914 long nr;
6915 char_u *str;
6916{
6917 dictitem_T *item;
6918
6919 item = dictitem_alloc((char_u *)key);
6920 if (item == NULL)
6921 return FAIL;
6922 item->di_tv.v_lock = 0;
6923 if (str == NULL)
6924 {
6925 item->di_tv.v_type = VAR_NUMBER;
6926 item->di_tv.vval.v_number = nr;
6927 }
6928 else
6929 {
6930 item->di_tv.v_type = VAR_STRING;
6931 item->di_tv.vval.v_string = vim_strsave(str);
6932 }
6933 if (dict_add(d, item) == FAIL)
6934 {
6935 dictitem_free(item);
6936 return FAIL;
6937 }
6938 return OK;
6939}
6940
6941/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942 * Get the number of items in a Dictionary.
6943 */
6944 static long
6945dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006947{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006948 if (d == NULL)
6949 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006950 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006951}
6952
6953/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006954 * Find item "key[len]" in Dictionary "d".
6955 * If "len" is negative use strlen(key).
6956 * Returns NULL when not found.
6957 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006959dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006960 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006961 char_u *key;
6962 int len;
6963{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006964#define AKEYLEN 200
6965 char_u buf[AKEYLEN];
6966 char_u *akey;
6967 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006970 if (len < 0)
6971 akey = key;
6972 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006973 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974 tofree = akey = vim_strnsave(key, len);
6975 if (akey == NULL)
6976 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006977 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006978 else
6979 {
6980 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006981 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006982 akey = buf;
6983 }
6984
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006986 vim_free(tofree);
6987 if (HASHITEM_EMPTY(hi))
6988 return NULL;
6989 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006990}
6991
6992/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006993 * Get a string item from a dictionary.
6994 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006995 * Returns NULL if the entry doesn't exist or out of memory.
6996 */
6997 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006998get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006999 dict_T *d;
7000 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007001 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007002{
7003 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007004 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007005
7006 di = dict_find(d, key, -1);
7007 if (di == NULL)
7008 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007009 s = get_tv_string(&di->di_tv);
7010 if (save && s != NULL)
7011 s = vim_strsave(s);
7012 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007013}
7014
7015/*
7016 * Get a number item from a dictionary.
7017 * Returns 0 if the entry doesn't exist or out of memory.
7018 */
7019 long
7020get_dict_number(d, key)
7021 dict_T *d;
7022 char_u *key;
7023{
7024 dictitem_T *di;
7025
7026 di = dict_find(d, key, -1);
7027 if (di == NULL)
7028 return 0;
7029 return get_tv_number(&di->di_tv);
7030}
7031
7032/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033 * Return an allocated string with the string representation of a Dictionary.
7034 * May return NULL.
7035 */
7036 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007037dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007039 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040{
7041 garray_T ga;
7042 int first = TRUE;
7043 char_u *tofree;
7044 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007046 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007047 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007048 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051 return NULL;
7052 ga_init2(&ga, (int)sizeof(char), 80);
7053 ga_append(&ga, '{');
7054
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007055 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007056 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007058 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007060 --todo;
7061
7062 if (first)
7063 first = FALSE;
7064 else
7065 ga_concat(&ga, (char_u *)", ");
7066
7067 tofree = string_quote(hi->hi_key, FALSE);
7068 if (tofree != NULL)
7069 {
7070 ga_concat(&ga, tofree);
7071 vim_free(tofree);
7072 }
7073 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007074 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 if (s != NULL)
7076 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007077 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007078 if (s == NULL)
7079 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007082 if (todo > 0)
7083 {
7084 vim_free(ga.ga_data);
7085 return NULL;
7086 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087
7088 ga_append(&ga, '}');
7089 ga_append(&ga, NUL);
7090 return (char_u *)ga.ga_data;
7091}
7092
7093/*
7094 * Allocate a variable for a Dictionary and fill it from "*arg".
7095 * Return OK or FAIL. Returns NOTDONE for {expr}.
7096 */
7097 static int
7098get_dict_tv(arg, rettv, evaluate)
7099 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007100 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007101 int evaluate;
7102{
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 dict_T *d = NULL;
7104 typval_T tvkey;
7105 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007106 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007109 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007110
7111 /*
7112 * First check if it's not a curly-braces thing: {expr}.
7113 * Must do this without evaluating, otherwise a function may be called
7114 * twice. Unfortunately this means we need to call eval1() twice for the
7115 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007116 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007118 if (*start != '}')
7119 {
7120 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7121 return FAIL;
7122 if (*start == '}')
7123 return NOTDONE;
7124 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125
7126 if (evaluate)
7127 {
7128 d = dict_alloc();
7129 if (d == NULL)
7130 return FAIL;
7131 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 tvkey.v_type = VAR_UNKNOWN;
7133 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134
7135 *arg = skipwhite(*arg + 1);
7136 while (**arg != '}' && **arg != NUL)
7137 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007138 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 goto failret;
7140 if (**arg != ':')
7141 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007142 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007143 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144 goto failret;
7145 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007146 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007147 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007148 key = get_tv_string_buf_chk(&tvkey, buf);
7149 if (key == NULL || *key == NUL)
7150 {
7151 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7152 if (key != NULL)
7153 EMSG(_(e_emptykey));
7154 clear_tv(&tvkey);
7155 goto failret;
7156 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158
7159 *arg = skipwhite(*arg + 1);
7160 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7161 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007162 if (evaluate)
7163 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164 goto failret;
7165 }
7166 if (evaluate)
7167 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007168 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 if (item != NULL)
7170 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007171 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173 clear_tv(&tv);
7174 goto failret;
7175 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007176 item = dictitem_alloc(key);
7177 clear_tv(&tvkey);
7178 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007181 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 if (dict_add(d, item) == FAIL)
7183 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184 }
7185 }
7186
7187 if (**arg == '}')
7188 break;
7189 if (**arg != ',')
7190 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007191 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192 goto failret;
7193 }
7194 *arg = skipwhite(*arg + 1);
7195 }
7196
7197 if (**arg != '}')
7198 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007199 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200failret:
7201 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007202 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203 return FAIL;
7204 }
7205
7206 *arg = skipwhite(*arg + 1);
7207 if (evaluate)
7208 {
7209 rettv->v_type = VAR_DICT;
7210 rettv->vval.v_dict = d;
7211 ++d->dv_refcount;
7212 }
7213
7214 return OK;
7215}
7216
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007218 * Return a string with the string representation of a variable.
7219 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007220 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007221 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007222 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007223 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007224 */
7225 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007226echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007227 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007228 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007229 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007230 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007231{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 static int recurse = 0;
7233 char_u *r = NULL;
7234
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007237 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 *tofree = NULL;
7239 return NULL;
7240 }
7241 ++recurse;
7242
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007243 switch (tv->v_type)
7244 {
7245 case VAR_FUNC:
7246 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007247 r = tv->vval.v_string;
7248 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007249
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007250 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007251 if (tv->vval.v_list == NULL)
7252 {
7253 *tofree = NULL;
7254 r = NULL;
7255 }
7256 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7257 {
7258 *tofree = NULL;
7259 r = (char_u *)"[...]";
7260 }
7261 else
7262 {
7263 tv->vval.v_list->lv_copyID = copyID;
7264 *tofree = list2string(tv, copyID);
7265 r = *tofree;
7266 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007267 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007268
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007270 if (tv->vval.v_dict == NULL)
7271 {
7272 *tofree = NULL;
7273 r = NULL;
7274 }
7275 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7276 {
7277 *tofree = NULL;
7278 r = (char_u *)"{...}";
7279 }
7280 else
7281 {
7282 tv->vval.v_dict->dv_copyID = copyID;
7283 *tofree = dict2string(tv, copyID);
7284 r = *tofree;
7285 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007287
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007288 case VAR_STRING:
7289 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290 *tofree = NULL;
7291 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007292 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007293
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007294#ifdef FEAT_FLOAT
7295 case VAR_FLOAT:
7296 *tofree = NULL;
7297 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7298 r = numbuf;
7299 break;
7300#endif
7301
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007302 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007303 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007305 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306
7307 --recurse;
7308 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007309}
7310
7311/*
7312 * Return a string with the string representation of a variable.
7313 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7314 * "numbuf" is used for a number.
7315 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007316 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007317 */
7318 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007319tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007321 char_u **tofree;
7322 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007323 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007324{
7325 switch (tv->v_type)
7326 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007327 case VAR_FUNC:
7328 *tofree = string_quote(tv->vval.v_string, TRUE);
7329 return *tofree;
7330 case VAR_STRING:
7331 *tofree = string_quote(tv->vval.v_string, FALSE);
7332 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007333#ifdef FEAT_FLOAT
7334 case VAR_FLOAT:
7335 *tofree = NULL;
7336 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7337 return numbuf;
7338#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007340 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007343 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007344 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007346 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007347}
7348
7349/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 * Return string "str" in ' quotes, doubling ' characters.
7351 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007353 */
7354 static char_u *
7355string_quote(str, function)
7356 char_u *str;
7357 int function;
7358{
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007360 char_u *p, *r, *s;
7361
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 len = (function ? 13 : 3);
7363 if (str != NULL)
7364 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007365 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 for (p = str; *p != NUL; mb_ptr_adv(p))
7367 if (*p == '\'')
7368 ++len;
7369 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007370 s = r = alloc(len);
7371 if (r != NULL)
7372 {
7373 if (function)
7374 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007376 r += 10;
7377 }
7378 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 if (str != NULL)
7381 for (p = str; *p != NUL; )
7382 {
7383 if (*p == '\'')
7384 *r++ = '\'';
7385 MB_COPY_CHAR(p, r);
7386 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007388 if (function)
7389 *r++ = ')';
7390 *r++ = NUL;
7391 }
7392 return s;
7393}
7394
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007395#ifdef FEAT_FLOAT
7396/*
7397 * Convert the string "text" to a floating point number.
7398 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7399 * this always uses a decimal point.
7400 * Returns the length of the text that was consumed.
7401 */
7402 static int
7403string2float(text, value)
7404 char_u *text;
7405 float_T *value; /* result stored here */
7406{
7407 char *s = (char *)text;
7408 float_T f;
7409
7410 f = strtod(s, &s);
7411 *value = f;
7412 return (int)((char_u *)s - text);
7413}
7414#endif
7415
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 * Get the value of an environment variable.
7418 * "arg" is pointing to the '$'. It is advanced to after the name.
7419 * If the environment variable was not set, silently assume it is empty.
7420 * Always return OK.
7421 */
7422 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 int evaluate;
7427{
7428 char_u *string = NULL;
7429 int len;
7430 int cc;
7431 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007432 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
7434 ++*arg;
7435 name = *arg;
7436 len = get_env_len(arg);
7437 if (evaluate)
7438 {
7439 if (len != 0)
7440 {
7441 cc = name[len];
7442 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007443 /* first try vim_getenv(), fast for normal environment vars */
7444 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007446 {
7447 if (!mustfree)
7448 string = vim_strsave(string);
7449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 else
7451 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007452 if (mustfree)
7453 vim_free(string);
7454
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 /* next try expanding things like $VIM and ${HOME} */
7456 string = expand_env_save(name - 1);
7457 if (string != NULL && *string == '$')
7458 {
7459 vim_free(string);
7460 string = NULL;
7461 }
7462 }
7463 name[len] = cc;
7464 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007465 rettv->v_type = VAR_STRING;
7466 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 }
7468
7469 return OK;
7470}
7471
7472/*
7473 * Array with names and number of arguments of all internal functions
7474 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7475 */
7476static struct fst
7477{
7478 char *f_name; /* function name */
7479 char f_min_argc; /* minimal number of arguments */
7480 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007482 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483} functions[] =
7484{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007485#ifdef FEAT_FLOAT
7486 {"abs", 1, 1, f_abs},
7487#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007488 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 {"append", 2, 2, f_append},
7490 {"argc", 0, 0, f_argc},
7491 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007492 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007493#ifdef FEAT_FLOAT
7494 {"atan", 1, 1, f_atan},
7495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007497 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 {"bufexists", 1, 1, f_bufexists},
7499 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7500 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7501 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7502 {"buflisted", 1, 1, f_buflisted},
7503 {"bufloaded", 1, 1, f_bufloaded},
7504 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007505 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 {"bufwinnr", 1, 1, f_bufwinnr},
7507 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007508 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007509 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007510#ifdef FEAT_FLOAT
7511 {"ceil", 1, 1, f_ceil},
7512#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007513 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 {"char2nr", 1, 1, f_char2nr},
7515 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007516 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007518#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007519 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007520 {"complete_add", 1, 1, f_complete_add},
7521 {"complete_check", 0, 0, f_complete_check},
7522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007524 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007525#ifdef FEAT_FLOAT
7526 {"cos", 1, 1, f_cos},
7527#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007528 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007530 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007531 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 {"delete", 1, 1, f_delete},
7533 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007534 {"diff_filler", 1, 1, f_diff_filler},
7535 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007536 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007538 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {"eventhandler", 0, 0, f_eventhandler},
7540 {"executable", 1, 1, f_executable},
7541 {"exists", 1, 1, f_exists},
7542 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007543 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007544 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7546 {"filereadable", 1, 1, f_filereadable},
7547 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007548 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007549 {"finddir", 1, 3, f_finddir},
7550 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007551#ifdef FEAT_FLOAT
7552 {"float2nr", 1, 1, f_float2nr},
7553 {"floor", 1, 1, f_floor},
7554#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007555 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {"fnamemodify", 2, 2, f_fnamemodify},
7557 {"foldclosed", 1, 1, f_foldclosed},
7558 {"foldclosedend", 1, 1, f_foldclosedend},
7559 {"foldlevel", 1, 1, f_foldlevel},
7560 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007561 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007563 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007564 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007565 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007566 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 {"getbufvar", 2, 2, f_getbufvar},
7568 {"getchar", 0, 1, f_getchar},
7569 {"getcharmod", 0, 0, f_getcharmod},
7570 {"getcmdline", 0, 0, f_getcmdline},
7571 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007572 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007574 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007575 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 {"getfsize", 1, 1, f_getfsize},
7577 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007578 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007579 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007580 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007581 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007582 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007583 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007584 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007585 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007587 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 {"getwinposx", 0, 0, f_getwinposx},
7589 {"getwinposy", 0, 0, f_getwinposy},
7590 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007591 {"glob", 1, 2, f_glob},
7592 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007594 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007595 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007596 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7598 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7599 {"histadd", 2, 2, f_histadd},
7600 {"histdel", 1, 2, f_histdel},
7601 {"histget", 1, 2, f_histget},
7602 {"histnr", 1, 1, f_histnr},
7603 {"hlID", 1, 1, f_hlID},
7604 {"hlexists", 1, 1, f_hlexists},
7605 {"hostname", 0, 0, f_hostname},
7606 {"iconv", 3, 3, f_iconv},
7607 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007608 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007609 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007611 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 {"inputrestore", 0, 0, f_inputrestore},
7613 {"inputsave", 0, 0, f_inputsave},
7614 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007615 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007617 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007619 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007622 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {"libcall", 3, 3, f_libcall},
7624 {"libcallnr", 3, 3, f_libcallnr},
7625 {"line", 1, 1, f_line},
7626 {"line2byte", 1, 1, f_line2byte},
7627 {"lispindent", 1, 1, f_lispindent},
7628 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007629#ifdef FEAT_FLOAT
7630 {"log10", 1, 1, f_log10},
7631#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007632 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007633 {"maparg", 1, 3, f_maparg},
7634 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007635 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007636 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007637 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007638 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007639 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007640 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007641 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007642 {"max", 1, 1, f_max},
7643 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007644#ifdef vim_mkdir
7645 {"mkdir", 1, 3, f_mkdir},
7646#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007647 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {"nextnonblank", 1, 1, f_nextnonblank},
7649 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007650 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007651#ifdef FEAT_FLOAT
7652 {"pow", 2, 2, f_pow},
7653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007655 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007656 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007658 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007659 {"reltime", 0, 2, f_reltime},
7660 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 {"remote_expr", 2, 3, f_remote_expr},
7662 {"remote_foreground", 1, 1, f_remote_foreground},
7663 {"remote_peek", 1, 2, f_remote_peek},
7664 {"remote_read", 1, 1, f_remote_read},
7665 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007666 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007668 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007670 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007671#ifdef FEAT_FLOAT
7672 {"round", 1, 1, f_round},
7673#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007674 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007675 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007676 {"searchpair", 3, 7, f_searchpair},
7677 {"searchpairpos", 3, 7, f_searchpairpos},
7678 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {"server2client", 2, 2, f_server2client},
7680 {"serverlist", 0, 0, f_serverlist},
7681 {"setbufvar", 3, 3, f_setbufvar},
7682 {"setcmdpos", 1, 1, f_setcmdpos},
7683 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007684 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007685 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007686 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007687 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007689 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007691 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007693#ifdef FEAT_FLOAT
7694 {"sin", 1, 1, f_sin},
7695#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007696 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007697 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007698 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007699 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007700 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007701#ifdef FEAT_FLOAT
7702 {"sqrt", 1, 1, f_sqrt},
7703 {"str2float", 1, 1, f_str2float},
7704#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007705 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706#ifdef HAVE_STRFTIME
7707 {"strftime", 1, 2, f_strftime},
7708#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007710 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {"strlen", 1, 1, f_strlen},
7712 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007713 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {"strtrans", 1, 1, f_strtrans},
7715 {"submatch", 1, 1, f_submatch},
7716 {"substitute", 4, 4, f_substitute},
7717 {"synID", 3, 3, f_synID},
7718 {"synIDattr", 2, 3, f_synIDattr},
7719 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007720 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007721 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007722 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007723 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007724 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007725 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007726 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007728 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 {"tolower", 1, 1, f_tolower},
7730 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007731 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007732#ifdef FEAT_FLOAT
7733 {"trunc", 1, 1, f_trunc},
7734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {"virtcol", 1, 1, f_virtcol},
7738 {"visualmode", 0, 1, f_visualmode},
7739 {"winbufnr", 1, 1, f_winbufnr},
7740 {"wincol", 0, 0, f_wincol},
7741 {"winheight", 1, 1, f_winheight},
7742 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007743 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007745 {"winrestview", 1, 1, f_winrestview},
7746 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007748 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749};
7750
7751#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7752
7753/*
7754 * Function given to ExpandGeneric() to obtain the list of internal
7755 * or user defined function names.
7756 */
7757 char_u *
7758get_function_name(xp, idx)
7759 expand_T *xp;
7760 int idx;
7761{
7762 static int intidx = -1;
7763 char_u *name;
7764
7765 if (idx == 0)
7766 intidx = -1;
7767 if (intidx < 0)
7768 {
7769 name = get_user_func_name(xp, idx);
7770 if (name != NULL)
7771 return name;
7772 }
7773 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7774 {
7775 STRCPY(IObuff, functions[intidx].f_name);
7776 STRCAT(IObuff, "(");
7777 if (functions[intidx].f_max_argc == 0)
7778 STRCAT(IObuff, ")");
7779 return IObuff;
7780 }
7781
7782 return NULL;
7783}
7784
7785/*
7786 * Function given to ExpandGeneric() to obtain the list of internal or
7787 * user defined variable or function names.
7788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 char_u *
7790get_expr_name(xp, idx)
7791 expand_T *xp;
7792 int idx;
7793{
7794 static int intidx = -1;
7795 char_u *name;
7796
7797 if (idx == 0)
7798 intidx = -1;
7799 if (intidx < 0)
7800 {
7801 name = get_function_name(xp, idx);
7802 if (name != NULL)
7803 return name;
7804 }
7805 return get_user_var_name(xp, ++intidx);
7806}
7807
7808#endif /* FEAT_CMDL_COMPL */
7809
7810/*
7811 * Find internal function in table above.
7812 * Return index, or -1 if not found
7813 */
7814 static int
7815find_internal_func(name)
7816 char_u *name; /* name of the function */
7817{
7818 int first = 0;
7819 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7820 int cmp;
7821 int x;
7822
7823 /*
7824 * Find the function name in the table. Binary search.
7825 */
7826 while (first <= last)
7827 {
7828 x = first + ((unsigned)(last - first) >> 1);
7829 cmp = STRCMP(name, functions[x].f_name);
7830 if (cmp < 0)
7831 last = x - 1;
7832 else if (cmp > 0)
7833 first = x + 1;
7834 else
7835 return x;
7836 }
7837 return -1;
7838}
7839
7840/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7842 * name it contains, otherwise return "name".
7843 */
7844 static char_u *
7845deref_func_name(name, lenp)
7846 char_u *name;
7847 int *lenp;
7848{
Bram Moolenaar33570922005-01-25 22:26:29 +00007849 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007850 int cc;
7851
7852 cc = name[*lenp];
7853 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007854 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007855 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007857 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 {
7860 *lenp = 0;
7861 return (char_u *)""; /* just in case */
7862 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007863 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 }
7866
7867 return name;
7868}
7869
7870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 * Allocate a variable for the result of a function.
7872 * Return OK or FAIL.
7873 */
7874 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007875get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7876 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 char_u *name; /* name of the function */
7878 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 char_u **arg; /* argument, pointing to the '(' */
7881 linenr_T firstline; /* first line of range */
7882 linenr_T lastline; /* last line of range */
7883 int *doesrange; /* return: function handled range */
7884 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 char_u *argp;
7888 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007889 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 int argcount = 0; /* number of arguments found */
7891
7892 /*
7893 * Get the arguments.
7894 */
7895 argp = *arg;
7896 while (argcount < MAX_FUNC_ARGS)
7897 {
7898 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7899 if (*argp == ')' || *argp == ',' || *argp == NUL)
7900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7902 {
7903 ret = FAIL;
7904 break;
7905 }
7906 ++argcount;
7907 if (*argp != ',')
7908 break;
7909 }
7910 if (*argp == ')')
7911 ++argp;
7912 else
7913 ret = FAIL;
7914
7915 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 {
7920 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007921 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007923 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925
7926 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
7929 *arg = skipwhite(argp);
7930 return ret;
7931}
7932
7933
7934/*
7935 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007936 * Return OK when the function can't be called, FAIL otherwise.
7937 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 */
7939 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 char_u *name; /* name of the function */
7943 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007946 typval_T *argvars; /* vars for arguments, must have "argcount"
7947 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 linenr_T firstline; /* first line of range */
7949 linenr_T lastline; /* last line of range */
7950 int *doesrange; /* return: function handled range */
7951 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
7954 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955#define ERROR_UNKNOWN 0
7956#define ERROR_TOOMANY 1
7957#define ERROR_TOOFEW 2
7958#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007959#define ERROR_DICT 4
7960#define ERROR_NONE 5
7961#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 int error = ERROR_NONE;
7963 int i;
7964 int llen;
7965 ufunc_T *fp;
7966 int cc;
7967#define FLEN_FIXED 40
7968 char_u fname_buf[FLEN_FIXED + 1];
7969 char_u *fname;
7970
7971 /*
7972 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7973 * Change <SNR>123_name() to K_SNR 123_name().
7974 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7975 */
7976 cc = name[len];
7977 name[len] = NUL;
7978 llen = eval_fname_script(name);
7979 if (llen > 0)
7980 {
7981 fname_buf[0] = K_SPECIAL;
7982 fname_buf[1] = KS_EXTRA;
7983 fname_buf[2] = (int)KE_SNR;
7984 i = 3;
7985 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7986 {
7987 if (current_SID <= 0)
7988 error = ERROR_SCRIPT;
7989 else
7990 {
7991 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7992 i = (int)STRLEN(fname_buf);
7993 }
7994 }
7995 if (i + STRLEN(name + llen) < FLEN_FIXED)
7996 {
7997 STRCPY(fname_buf + i, name + llen);
7998 fname = fname_buf;
7999 }
8000 else
8001 {
8002 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8003 if (fname == NULL)
8004 error = ERROR_OTHER;
8005 else
8006 {
8007 mch_memmove(fname, fname_buf, (size_t)i);
8008 STRCPY(fname + i, name + llen);
8009 }
8010 }
8011 }
8012 else
8013 fname = name;
8014
8015 *doesrange = FALSE;
8016
8017
8018 /* execute the function if no errors detected and executing */
8019 if (evaluate && error == ERROR_NONE)
8020 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008021 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8022 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 error = ERROR_UNKNOWN;
8024
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008025 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 {
8027 /*
8028 * User defined function.
8029 */
8030 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008031
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008033 /* Trigger FuncUndefined event, may load the function. */
8034 if (fp == NULL
8035 && apply_autocmds(EVENT_FUNCUNDEFINED,
8036 fname, fname, TRUE, NULL)
8037 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008039 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 fp = find_func(fname);
8041 }
8042#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008043 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008044 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008045 {
8046 /* loaded a package, search for the function again */
8047 fp = find_func(fname);
8048 }
8049
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 if (fp != NULL)
8051 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008052 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008054 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008056 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008058 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008059 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 else
8061 {
8062 /*
8063 * Call the user function.
8064 * Save and restore search patterns, script variables and
8065 * redo buffer.
8066 */
8067 save_search_patterns();
8068 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008069 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008071 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008072 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8073 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8074 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008075 /* Function was unreferenced while being used, free it
8076 * now. */
8077 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 restoreRedobuff();
8079 restore_search_patterns();
8080 error = ERROR_NONE;
8081 }
8082 }
8083 }
8084 else
8085 {
8086 /*
8087 * Find the function name in the table, call its implementation.
8088 */
8089 i = find_internal_func(fname);
8090 if (i >= 0)
8091 {
8092 if (argcount < functions[i].f_min_argc)
8093 error = ERROR_TOOFEW;
8094 else if (argcount > functions[i].f_max_argc)
8095 error = ERROR_TOOMANY;
8096 else
8097 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008098 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 error = ERROR_NONE;
8101 }
8102 }
8103 }
8104 /*
8105 * The function call (or "FuncUndefined" autocommand sequence) might
8106 * have been aborted by an error, an interrupt, or an explicitly thrown
8107 * exception that has not been caught so far. This situation can be
8108 * tested for by calling aborting(). For an error in an internal
8109 * function or for the "E132" error in call_user_func(), however, the
8110 * throw point at which the "force_abort" flag (temporarily reset by
8111 * emsg()) is normally updated has not been reached yet. We need to
8112 * update that flag first to make aborting() reliable.
8113 */
8114 update_force_abort();
8115 }
8116 if (error == ERROR_NONE)
8117 ret = OK;
8118
8119 /*
8120 * Report an error unless the argument evaluation or function call has been
8121 * cancelled due to an aborting error, an interrupt, or an exception.
8122 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008123 if (!aborting())
8124 {
8125 switch (error)
8126 {
8127 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008128 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008129 break;
8130 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008131 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008132 break;
8133 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008134 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008135 name);
8136 break;
8137 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008138 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008139 name);
8140 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008141 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008142 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008143 name);
8144 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008145 }
8146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147
8148 name[len] = cc;
8149 if (fname != name && fname != fname_buf)
8150 vim_free(fname);
8151
8152 return ret;
8153}
8154
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008155/*
8156 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008157 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008158 */
8159 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008160emsg_funcname(ermsg, name)
8161 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008162 char_u *name;
8163{
8164 char_u *p;
8165
8166 if (*name == K_SPECIAL)
8167 p = concat_str((char_u *)"<SNR>", name + 3);
8168 else
8169 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008170 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008171 if (p != name)
8172 vim_free(p);
8173}
8174
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008175/*
8176 * Return TRUE for a non-zero Number and a non-empty String.
8177 */
8178 static int
8179non_zero_arg(argvars)
8180 typval_T *argvars;
8181{
8182 return ((argvars[0].v_type == VAR_NUMBER
8183 && argvars[0].vval.v_number != 0)
8184 || (argvars[0].v_type == VAR_STRING
8185 && argvars[0].vval.v_string != NULL
8186 && *argvars[0].vval.v_string != NUL));
8187}
8188
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189/*********************************************
8190 * Implementation of the built-in functions
8191 */
8192
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193#ifdef FEAT_FLOAT
8194/*
8195 * "abs(expr)" function
8196 */
8197 static void
8198f_abs(argvars, rettv)
8199 typval_T *argvars;
8200 typval_T *rettv;
8201{
8202 if (argvars[0].v_type == VAR_FLOAT)
8203 {
8204 rettv->v_type = VAR_FLOAT;
8205 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8206 }
8207 else
8208 {
8209 varnumber_T n;
8210 int error = FALSE;
8211
8212 n = get_tv_number_chk(&argvars[0], &error);
8213 if (error)
8214 rettv->vval.v_number = -1;
8215 else if (n > 0)
8216 rettv->vval.v_number = n;
8217 else
8218 rettv->vval.v_number = -n;
8219 }
8220}
8221#endif
8222
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008224 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 */
8226 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008227f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 typval_T *argvars;
8229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230{
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008233 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008234 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008236 if ((l = argvars[0].vval.v_list) != NULL
8237 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8238 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 }
8241 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008242 EMSG(_(e_listreq));
8243}
8244
8245/*
8246 * "append(lnum, string/list)" function
8247 */
8248 static void
8249f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008250 typval_T *argvars;
8251 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008252{
8253 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008254 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008255 list_T *l = NULL;
8256 listitem_T *li = NULL;
8257 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008258 long added = 0;
8259
Bram Moolenaar0d660222005-01-07 21:51:51 +00008260 lnum = get_tv_lnum(argvars);
8261 if (lnum >= 0
8262 && lnum <= curbuf->b_ml.ml_line_count
8263 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008264 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008265 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008266 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008267 l = argvars[1].vval.v_list;
8268 if (l == NULL)
8269 return;
8270 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272 for (;;)
8273 {
8274 if (l == NULL)
8275 tv = &argvars[1]; /* append a string */
8276 else if (li == NULL)
8277 break; /* end of list */
8278 else
8279 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008280 line = get_tv_string_chk(tv);
8281 if (line == NULL) /* type error */
8282 {
8283 rettv->vval.v_number = 1; /* Failed */
8284 break;
8285 }
8286 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008287 ++added;
8288 if (l == NULL)
8289 break;
8290 li = li->li_next;
8291 }
8292
8293 appended_lines_mark(lnum, added);
8294 if (curwin->w_cursor.lnum > lnum)
8295 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008297 else
8298 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299}
8300
8301/*
8302 * "argc()" function
8303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008305f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008306 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310}
8311
8312/*
8313 * "argidx()" function
8314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008317 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321}
8322
8323/*
8324 * "argv(nr)" function
8325 */
8326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008327f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 typval_T *argvars;
8329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
8331 int idx;
8332
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008333 if (argvars[0].v_type != VAR_UNKNOWN)
8334 {
8335 idx = get_tv_number_chk(&argvars[0], NULL);
8336 if (idx >= 0 && idx < ARGCOUNT)
8337 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8338 else
8339 rettv->vval.v_string = NULL;
8340 rettv->v_type = VAR_STRING;
8341 }
8342 else if (rettv_list_alloc(rettv) == OK)
8343 for (idx = 0; idx < ARGCOUNT; ++idx)
8344 list_append_string(rettv->vval.v_list,
8345 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
8347
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008348#ifdef FEAT_FLOAT
8349static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8350
8351/*
8352 * Get the float value of "argvars[0]" into "f".
8353 * Returns FAIL when the argument is not a Number or Float.
8354 */
8355 static int
8356get_float_arg(argvars, f)
8357 typval_T *argvars;
8358 float_T *f;
8359{
8360 if (argvars[0].v_type == VAR_FLOAT)
8361 {
8362 *f = argvars[0].vval.v_float;
8363 return OK;
8364 }
8365 if (argvars[0].v_type == VAR_NUMBER)
8366 {
8367 *f = (float_T)argvars[0].vval.v_number;
8368 return OK;
8369 }
8370 EMSG(_("E808: Number or Float required"));
8371 return FAIL;
8372}
8373
8374/*
8375 * "atan()" function
8376 */
8377 static void
8378f_atan(argvars, rettv)
8379 typval_T *argvars;
8380 typval_T *rettv;
8381{
8382 float_T f;
8383
8384 rettv->v_type = VAR_FLOAT;
8385 if (get_float_arg(argvars, &f) == OK)
8386 rettv->vval.v_float = atan(f);
8387 else
8388 rettv->vval.v_float = 0.0;
8389}
8390#endif
8391
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392/*
8393 * "browse(save, title, initdir, default)" function
8394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008396f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
8400#ifdef FEAT_BROWSE
8401 int save;
8402 char_u *title;
8403 char_u *initdir;
8404 char_u *defname;
8405 char_u buf[NUMBUFLEN];
8406 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008409 save = get_tv_number_chk(&argvars[0], &error);
8410 title = get_tv_string_chk(&argvars[1]);
8411 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8412 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 if (error || title == NULL || initdir == NULL || defname == NULL)
8415 rettv->vval.v_string = NULL;
8416 else
8417 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008418 do_browse(save ? BROWSE_SAVE : 0,
8419 title, defname, NULL, initdir, NULL, curbuf);
8420#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008421 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008422#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008424}
8425
8426/*
8427 * "browsedir(title, initdir)" function
8428 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008430f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008431 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008432 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008433{
8434#ifdef FEAT_BROWSE
8435 char_u *title;
8436 char_u *initdir;
8437 char_u buf[NUMBUFLEN];
8438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008439 title = get_tv_string_chk(&argvars[0]);
8440 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 if (title == NULL || initdir == NULL)
8443 rettv->vval.v_string = NULL;
8444 else
8445 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008446 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451}
8452
Bram Moolenaar33570922005-01-25 22:26:29 +00008453static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008454
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455/*
8456 * Find a buffer by number or exact name.
8457 */
8458 static buf_T *
8459find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008460 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
8462 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008464 if (avar->v_type == VAR_NUMBER)
8465 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008466 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008468 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008469 if (buf == NULL)
8470 {
8471 /* No full path name match, try a match with a URL or a "nofile"
8472 * buffer, these don't use the full path. */
8473 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8474 if (buf->b_fname != NULL
8475 && (path_with_url(buf->b_fname)
8476#ifdef FEAT_QUICKFIX
8477 || bt_nofile(buf)
8478#endif
8479 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008480 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008481 break;
8482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 }
8484 return buf;
8485}
8486
8487/*
8488 * "bufexists(expr)" function
8489 */
8490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 typval_T *argvars;
8493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008495 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496}
8497
8498/*
8499 * "buflisted(expr)" function
8500 */
8501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 typval_T *argvars;
8504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506 buf_T *buf;
8507
8508 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510}
8511
8512/*
8513 * "bufloaded(expr)" function
8514 */
8515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008516f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 typval_T *argvars;
8518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519{
8520 buf_T *buf;
8521
8522 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008523 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524}
8525
Bram Moolenaar33570922005-01-25 22:26:29 +00008526static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008527
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528/*
8529 * Get buffer by number or pattern.
8530 */
8531 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008532get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008533 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008535 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 int save_magic;
8537 char_u *save_cpo;
8538 buf_T *buf;
8539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540 if (tv->v_type == VAR_NUMBER)
8541 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008542 if (tv->v_type != VAR_STRING)
8543 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 if (name == NULL || *name == NUL)
8545 return curbuf;
8546 if (name[0] == '$' && name[1] == NUL)
8547 return lastbuf;
8548
8549 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8550 save_magic = p_magic;
8551 p_magic = TRUE;
8552 save_cpo = p_cpo;
8553 p_cpo = (char_u *)"";
8554
8555 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8556 TRUE, FALSE));
8557
8558 p_magic = save_magic;
8559 p_cpo = save_cpo;
8560
8561 /* If not found, try expanding the name, like done for bufexists(). */
8562 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564
8565 return buf;
8566}
8567
8568/*
8569 * "bufname(expr)" function
8570 */
8571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 typval_T *argvars;
8574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576 buf_T *buf;
8577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008578 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 buf = get_buf_tv(&argvars[0]);
8581 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008583 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 --emsg_off;
8587}
8588
8589/*
8590 * "bufnr(expr)" function
8591 */
8592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008594 typval_T *argvars;
8595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008598 int error = FALSE;
8599 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008601 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008604 --emsg_off;
8605
8606 /* If the buffer isn't found and the second argument is not zero create a
8607 * new buffer. */
8608 if (buf == NULL
8609 && argvars[1].v_type != VAR_UNKNOWN
8610 && get_tv_number_chk(&argvars[1], &error) != 0
8611 && !error
8612 && (name = get_tv_string_chk(&argvars[0])) != NULL
8613 && !error)
8614 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620}
8621
8622/*
8623 * "bufwinnr(nr)" function
8624 */
8625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008626f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008627 typval_T *argvars;
8628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
8630#ifdef FEAT_WINDOWS
8631 win_T *wp;
8632 int winnr = 0;
8633#endif
8634 buf_T *buf;
8635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008636 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639#ifdef FEAT_WINDOWS
8640 for (wp = firstwin; wp; wp = wp->w_next)
8641 {
8642 ++winnr;
8643 if (wp->w_buffer == buf)
8644 break;
8645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008648 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649#endif
8650 --emsg_off;
8651}
8652
8653/*
8654 * "byte2line(byte)" function
8655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008658 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008662 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663#else
8664 long boff = 0;
8665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008670 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 (linenr_T)0, &boff);
8672#endif
8673}
8674
8675/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008676 * "byteidx()" function
8677 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008680 typval_T *argvars;
8681 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008682{
8683#ifdef FEAT_MBYTE
8684 char_u *t;
8685#endif
8686 char_u *str;
8687 long idx;
8688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008689 str = get_tv_string_chk(&argvars[0]);
8690 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008691 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008692 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008693 return;
8694
8695#ifdef FEAT_MBYTE
8696 t = str;
8697 for ( ; idx > 0; idx--)
8698 {
8699 if (*t == NUL) /* EOL reached */
8700 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008701 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008702 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008703 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008704#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008705 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008707#endif
8708}
8709
8710/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008711 * "call(func, arglist)" function
8712 */
8713 static void
8714f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008715 typval_T *argvars;
8716 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008717{
8718 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008719 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008720 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008721 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008722 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008724
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008725 if (argvars[1].v_type != VAR_LIST)
8726 {
8727 EMSG(_(e_listreq));
8728 return;
8729 }
8730 if (argvars[1].vval.v_list == NULL)
8731 return;
8732
8733 if (argvars[0].v_type == VAR_FUNC)
8734 func = argvars[0].vval.v_string;
8735 else
8736 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 if (*func == NUL)
8738 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008739
Bram Moolenaare9a41262005-01-15 22:18:47 +00008740 if (argvars[2].v_type != VAR_UNKNOWN)
8741 {
8742 if (argvars[2].v_type != VAR_DICT)
8743 {
8744 EMSG(_(e_dictreq));
8745 return;
8746 }
8747 selfdict = argvars[2].vval.v_dict;
8748 }
8749
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008750 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8751 item = item->li_next)
8752 {
8753 if (argc == MAX_FUNC_ARGS)
8754 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008755 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008756 break;
8757 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008758 /* Make a copy of each argument. This is needed to be able to set
8759 * v_lock to VAR_FIXED in the copy without changing the original list.
8760 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008761 copy_tv(&item->li_tv, &argv[argc++]);
8762 }
8763
8764 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008765 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008766 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8767 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008768
8769 /* Free the arguments. */
8770 while (argc > 0)
8771 clear_tv(&argv[--argc]);
8772}
8773
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008774#ifdef FEAT_FLOAT
8775/*
8776 * "ceil({float})" function
8777 */
8778 static void
8779f_ceil(argvars, rettv)
8780 typval_T *argvars;
8781 typval_T *rettv;
8782{
8783 float_T f;
8784
8785 rettv->v_type = VAR_FLOAT;
8786 if (get_float_arg(argvars, &f) == OK)
8787 rettv->vval.v_float = ceil(f);
8788 else
8789 rettv->vval.v_float = 0.0;
8790}
8791#endif
8792
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008793/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008794 * "changenr()" function
8795 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008796 static void
8797f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008798 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008799 typval_T *rettv;
8800{
8801 rettv->vval.v_number = curbuf->b_u_seq_cur;
8802}
8803
8804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 * "char2nr(string)" function
8806 */
8807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 typval_T *argvars;
8810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
8812#ifdef FEAT_MBYTE
8813 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008814 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 else
8816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818}
8819
8820/*
8821 * "cindent(lnum)" function
8822 */
8823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008825 typval_T *argvars;
8826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
8828#ifdef FEAT_CINDENT
8829 pos_T pos;
8830 linenr_T lnum;
8831
8832 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8835 {
8836 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008837 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 curwin->w_cursor = pos;
8839 }
8840 else
8841#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843}
8844
8845/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008846 * "clearmatches()" function
8847 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008848 static void
8849f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008850 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008851 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008852{
8853#ifdef FEAT_SEARCH_EXTRA
8854 clear_matches(curwin);
8855#endif
8856}
8857
8858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 * "col(string)" function
8860 */
8861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 typval_T *argvars;
8864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 colnr_T col = 0;
8867 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008868 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008870 fp = var2fpos(&argvars[0], FALSE, &fnum);
8871 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 {
8873 if (fp->col == MAXCOL)
8874 {
8875 /* '> can be MAXCOL, get the length of the line then */
8876 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008877 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 else
8879 col = MAXCOL;
8880 }
8881 else
8882 {
8883 col = fp->col + 1;
8884#ifdef FEAT_VIRTUALEDIT
8885 /* col(".") when the cursor is on the NUL at the end of the line
8886 * because of "coladd" can be seen as an extra column. */
8887 if (virtual_active() && fp == &curwin->w_cursor)
8888 {
8889 char_u *p = ml_get_cursor();
8890
8891 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8892 curwin->w_virtcol - curwin->w_cursor.coladd))
8893 {
8894# ifdef FEAT_MBYTE
8895 int l;
8896
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008897 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 col += l;
8899# else
8900 if (*p != NUL && p[1] == NUL)
8901 ++col;
8902# endif
8903 }
8904 }
8905#endif
8906 }
8907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008908 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909}
8910
Bram Moolenaar572cb562005-08-05 21:35:02 +00008911#if defined(FEAT_INS_EXPAND)
8912/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008913 * "complete()" function
8914 */
Bram Moolenaarade00832006-03-10 21:46:58 +00008915 static void
8916f_complete(argvars, rettv)
8917 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008918 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00008919{
8920 int startcol;
8921
8922 if ((State & INSERT) == 0)
8923 {
8924 EMSG(_("E785: complete() can only be used in Insert mode"));
8925 return;
8926 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008927
8928 /* Check for undo allowed here, because if something was already inserted
8929 * the line was already saved for undo and this check isn't done. */
8930 if (!undo_allowed())
8931 return;
8932
Bram Moolenaarade00832006-03-10 21:46:58 +00008933 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8934 {
8935 EMSG(_(e_invarg));
8936 return;
8937 }
8938
8939 startcol = get_tv_number_chk(&argvars[0], NULL);
8940 if (startcol <= 0)
8941 return;
8942
8943 set_completion(startcol - 1, argvars[1].vval.v_list);
8944}
8945
8946/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008947 * "complete_add()" function
8948 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00008949 static void
8950f_complete_add(argvars, rettv)
8951 typval_T *argvars;
8952 typval_T *rettv;
8953{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008954 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008955}
8956
8957/*
8958 * "complete_check()" function
8959 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00008960 static void
8961f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008962 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008963 typval_T *rettv;
8964{
8965 int saved = RedrawingDisabled;
8966
8967 RedrawingDisabled = 0;
8968 ins_compl_check_keys(0);
8969 rettv->vval.v_number = compl_interrupted;
8970 RedrawingDisabled = saved;
8971}
8972#endif
8973
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974/*
8975 * "confirm(message, buttons[, default [, type]])" function
8976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008979 typval_T *argvars UNUSED;
8980 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
8982#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8983 char_u *message;
8984 char_u *buttons = NULL;
8985 char_u buf[NUMBUFLEN];
8986 char_u buf2[NUMBUFLEN];
8987 int def = 1;
8988 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008989 char_u *typestr;
8990 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 message = get_tv_string_chk(&argvars[0]);
8993 if (message == NULL)
8994 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008995 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008997 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8998 if (buttons == NULL)
8999 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009003 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009005 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9006 if (typestr == NULL)
9007 error = TRUE;
9008 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 switch (TOUPPER_ASC(*typestr))
9011 {
9012 case 'E': type = VIM_ERROR; break;
9013 case 'Q': type = VIM_QUESTION; break;
9014 case 'I': type = VIM_INFO; break;
9015 case 'W': type = VIM_WARNING; break;
9016 case 'G': type = VIM_GENERIC; break;
9017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 }
9019 }
9020 }
9021 }
9022
9023 if (buttons == NULL || *buttons == NUL)
9024 buttons = (char_u *)_("&Ok");
9025
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009026 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009027 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029#endif
9030}
9031
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009032/*
9033 * "copy()" function
9034 */
9035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 typval_T *argvars;
9038 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009039{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009040 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009043#ifdef FEAT_FLOAT
9044/*
9045 * "cos()" function
9046 */
9047 static void
9048f_cos(argvars, rettv)
9049 typval_T *argvars;
9050 typval_T *rettv;
9051{
9052 float_T f;
9053
9054 rettv->v_type = VAR_FLOAT;
9055 if (get_float_arg(argvars, &f) == OK)
9056 rettv->vval.v_float = cos(f);
9057 else
9058 rettv->vval.v_float = 0.0;
9059}
9060#endif
9061
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009063 * "count()" function
9064 */
9065 static void
9066f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009067 typval_T *argvars;
9068 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009069{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009070 long n = 0;
9071 int ic = FALSE;
9072
Bram Moolenaare9a41262005-01-15 22:18:47 +00009073 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009074 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009075 listitem_T *li;
9076 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009077 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009078
Bram Moolenaare9a41262005-01-15 22:18:47 +00009079 if ((l = argvars[0].vval.v_list) != NULL)
9080 {
9081 li = l->lv_first;
9082 if (argvars[2].v_type != VAR_UNKNOWN)
9083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009084 int error = FALSE;
9085
9086 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009087 if (argvars[3].v_type != VAR_UNKNOWN)
9088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009089 idx = get_tv_number_chk(&argvars[3], &error);
9090 if (!error)
9091 {
9092 li = list_find(l, idx);
9093 if (li == NULL)
9094 EMSGN(_(e_listidx), idx);
9095 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009097 if (error)
9098 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009099 }
9100
9101 for ( ; li != NULL; li = li->li_next)
9102 if (tv_equal(&li->li_tv, &argvars[1], ic))
9103 ++n;
9104 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009105 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009106 else if (argvars[0].v_type == VAR_DICT)
9107 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 int todo;
9109 dict_T *d;
9110 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009111
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009112 if ((d = argvars[0].vval.v_dict) != NULL)
9113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 int error = FALSE;
9115
Bram Moolenaare9a41262005-01-15 22:18:47 +00009116 if (argvars[2].v_type != VAR_UNKNOWN)
9117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009118 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009119 if (argvars[3].v_type != VAR_UNKNOWN)
9120 EMSG(_(e_invarg));
9121 }
9122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009123 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009125 {
9126 if (!HASHITEM_EMPTY(hi))
9127 {
9128 --todo;
9129 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9130 ++n;
9131 }
9132 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009133 }
9134 }
9135 else
9136 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009137 rettv->vval.v_number = n;
9138}
9139
9140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9142 *
9143 * Checks the existence of a cscope connection.
9144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009147 typval_T *argvars UNUSED;
9148 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150#ifdef FEAT_CSCOPE
9151 int num = 0;
9152 char_u *dbpath = NULL;
9153 char_u *prepend = NULL;
9154 char_u buf[NUMBUFLEN];
9155
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009156 if (argvars[0].v_type != VAR_UNKNOWN
9157 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 num = (int)get_tv_number(&argvars[0]);
9160 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 }
9164
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166#endif
9167}
9168
9169/*
9170 * "cursor(lnum, col)" function
9171 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009172 * Moves the cursor to the specified line and column.
9173 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009177 typval_T *argvars;
9178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
9180 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009181#ifdef FEAT_VIRTUALEDIT
9182 long coladd = 0;
9183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009185 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009186 if (argvars[1].v_type == VAR_UNKNOWN)
9187 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009188 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009189
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009190 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009191 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009192 line = pos.lnum;
9193 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009194#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009195 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009196#endif
9197 }
9198 else
9199 {
9200 line = get_tv_lnum(argvars);
9201 col = get_tv_number_chk(&argvars[1], NULL);
9202#ifdef FEAT_VIRTUALEDIT
9203 if (argvars[2].v_type != VAR_UNKNOWN)
9204 coladd = get_tv_number_chk(&argvars[2], NULL);
9205#endif
9206 }
9207 if (line < 0 || col < 0
9208#ifdef FEAT_VIRTUALEDIT
9209 || coladd < 0
9210#endif
9211 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009212 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 if (line > 0)
9214 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 if (col > 0)
9216 curwin->w_cursor.col = col - 1;
9217#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009218 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219#endif
9220
9221 /* Make sure the cursor is in a valid position. */
9222 check_cursor();
9223#ifdef FEAT_MBYTE
9224 /* Correct cursor for multi-byte character. */
9225 if (has_mbyte)
9226 mb_adjust_cursor();
9227#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009228
9229 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009230 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231}
9232
9233/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009234 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 */
9236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009237f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009238 typval_T *argvars;
9239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009241 int noref = 0;
9242
9243 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009244 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009245 if (noref < 0 || noref > 1)
9246 EMSG(_(e_invarg));
9247 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009248 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
9250
9251/*
9252 * "delete()" function
9253 */
9254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009256 typval_T *argvars;
9257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009262 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263}
9264
9265/*
9266 * "did_filetype()" function
9267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009269f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009270 typval_T *argvars UNUSED;
9271 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272{
9273#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275#endif
9276}
9277
9278/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009279 * "diff_filler()" function
9280 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009283 typval_T *argvars UNUSED;
9284 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009285{
9286#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009288#endif
9289}
9290
9291/*
9292 * "diff_hlID()" function
9293 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009295f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009296 typval_T *argvars UNUSED;
9297 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009298{
9299#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009301 static linenr_T prev_lnum = 0;
9302 static int changedtick = 0;
9303 static int fnum = 0;
9304 static int change_start = 0;
9305 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009306 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009307 int filler_lines;
9308 int col;
9309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310 if (lnum < 0) /* ignore type error in {lnum} arg */
9311 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009312 if (lnum != prev_lnum
9313 || changedtick != curbuf->b_changedtick
9314 || fnum != curbuf->b_fnum)
9315 {
9316 /* New line, buffer, change: need to get the values. */
9317 filler_lines = diff_check(curwin, lnum);
9318 if (filler_lines < 0)
9319 {
9320 if (filler_lines == -1)
9321 {
9322 change_start = MAXCOL;
9323 change_end = -1;
9324 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9325 hlID = HLF_ADD; /* added line */
9326 else
9327 hlID = HLF_CHD; /* changed line */
9328 }
9329 else
9330 hlID = HLF_ADD; /* added line */
9331 }
9332 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009333 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009334 prev_lnum = lnum;
9335 changedtick = curbuf->b_changedtick;
9336 fnum = curbuf->b_fnum;
9337 }
9338
9339 if (hlID == HLF_CHD || hlID == HLF_TXD)
9340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009342 if (col >= change_start && col <= change_end)
9343 hlID = HLF_TXD; /* changed text */
9344 else
9345 hlID = HLF_CHD; /* changed line */
9346 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009347 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009348#endif
9349}
9350
9351/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009352 * "empty({expr})" function
9353 */
9354 static void
9355f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009356 typval_T *argvars;
9357 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009358{
9359 int n;
9360
9361 switch (argvars[0].v_type)
9362 {
9363 case VAR_STRING:
9364 case VAR_FUNC:
9365 n = argvars[0].vval.v_string == NULL
9366 || *argvars[0].vval.v_string == NUL;
9367 break;
9368 case VAR_NUMBER:
9369 n = argvars[0].vval.v_number == 0;
9370 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009371#ifdef FEAT_FLOAT
9372 case VAR_FLOAT:
9373 n = argvars[0].vval.v_float == 0.0;
9374 break;
9375#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009376 case VAR_LIST:
9377 n = argvars[0].vval.v_list == NULL
9378 || argvars[0].vval.v_list->lv_first == NULL;
9379 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009380 case VAR_DICT:
9381 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009382 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009383 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009384 default:
9385 EMSG2(_(e_intern2), "f_empty()");
9386 n = 0;
9387 }
9388
9389 rettv->vval.v_number = n;
9390}
9391
9392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 * "escape({string}, {chars})" function
9394 */
9395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009397 typval_T *argvars;
9398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
9400 char_u buf[NUMBUFLEN];
9401
Bram Moolenaar758711c2005-02-02 23:11:38 +00009402 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9403 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405}
9406
9407/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009408 * "eval()" function
9409 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009410 static void
9411f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009412 typval_T *argvars;
9413 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009414{
9415 char_u *s;
9416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009417 s = get_tv_string_chk(&argvars[0]);
9418 if (s != NULL)
9419 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9422 {
9423 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009424 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009425 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009426 else if (*s != NUL)
9427 EMSG(_(e_trailing));
9428}
9429
9430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 * "eventhandler()" function
9432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009435 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439}
9440
9441/*
9442 * "executable()" function
9443 */
9444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *argvars;
9447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450}
9451
9452/*
9453 * "exists()" function
9454 */
9455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 typval_T *argvars;
9458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 char_u *p;
9461 char_u *name;
9462 int n = FALSE;
9463 int len = 0;
9464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 if (*p == '$') /* environment variable */
9467 {
9468 /* first try "normal" environment variables (fast) */
9469 if (mch_getenv(p + 1) != NULL)
9470 n = TRUE;
9471 else
9472 {
9473 /* try expanding things like $VIM and ${HOME} */
9474 p = expand_env_save(p);
9475 if (p != NULL && *p != '$')
9476 n = TRUE;
9477 vim_free(p);
9478 }
9479 }
9480 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009483 if (*skipwhite(p) != NUL)
9484 n = FALSE; /* trailing garbage */
9485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 else if (*p == '*') /* internal or user defined function */
9487 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009488 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 }
9490 else if (*p == ':')
9491 {
9492 n = cmd_exists(p + 1);
9493 }
9494 else if (*p == '#')
9495 {
9496#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009497 if (p[1] == '#')
9498 n = autocmd_supported(p + 2);
9499 else
9500 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#endif
9502 }
9503 else /* internal variable */
9504 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009505 char_u *tofree;
9506 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009508 /* get_name_len() takes care of expanding curly braces */
9509 name = p;
9510 len = get_name_len(&p, &tofree, TRUE, FALSE);
9511 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009513 if (tofree != NULL)
9514 name = tofree;
9515 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9516 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009518 /* handle d.key, l[idx], f(expr) */
9519 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9520 if (n)
9521 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 }
9523 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009524 if (*p != NUL)
9525 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009527 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 }
9529
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531}
9532
9533/*
9534 * "expand()" function
9535 */
9536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009538 typval_T *argvars;
9539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540{
9541 char_u *s;
9542 int len;
9543 char_u *errormsg;
9544 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9545 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548 rettv->v_type = VAR_STRING;
9549 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 if (*s == '%' || *s == '#' || *s == '<')
9551 {
9552 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009553 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 --emsg_off;
9555 }
9556 else
9557 {
9558 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009559 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009560 if (argvars[1].v_type != VAR_UNKNOWN
9561 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 if (!error)
9564 {
9565 ExpandInit(&xpc);
9566 xpc.xp_context = EXPAND_FILES;
9567 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009568 }
9569 else
9570 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 }
9572}
9573
9574/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009575 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009576 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009577 */
9578 static void
9579f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009580 typval_T *argvars;
9581 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009582{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009583 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009584 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009585 list_T *l1, *l2;
9586 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009587 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009589
Bram Moolenaare9a41262005-01-15 22:18:47 +00009590 l1 = argvars[0].vval.v_list;
9591 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009592 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9593 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009594 {
9595 if (argvars[2].v_type != VAR_UNKNOWN)
9596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009597 before = get_tv_number_chk(&argvars[2], &error);
9598 if (error)
9599 return; /* type error; errmsg already given */
9600
Bram Moolenaar758711c2005-02-02 23:11:38 +00009601 if (before == l1->lv_len)
9602 item = NULL;
9603 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009604 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009605 item = list_find(l1, before);
9606 if (item == NULL)
9607 {
9608 EMSGN(_(e_listidx), before);
9609 return;
9610 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009611 }
9612 }
9613 else
9614 item = NULL;
9615 list_extend(l1, l2, item);
9616
Bram Moolenaare9a41262005-01-15 22:18:47 +00009617 copy_tv(&argvars[0], rettv);
9618 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009620 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9621 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009622 dict_T *d1, *d2;
9623 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 char_u *action;
9625 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009627 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009628
9629 d1 = argvars[0].vval.v_dict;
9630 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009631 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9632 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 {
9634 /* Check the third argument. */
9635 if (argvars[2].v_type != VAR_UNKNOWN)
9636 {
9637 static char *(av[]) = {"keep", "force", "error"};
9638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 action = get_tv_string_chk(&argvars[2]);
9640 if (action == NULL)
9641 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 for (i = 0; i < 3; ++i)
9643 if (STRCMP(action, av[i]) == 0)
9644 break;
9645 if (i == 3)
9646 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009647 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648 return;
9649 }
9650 }
9651 else
9652 action = (char_u *)"force";
9653
9654 /* Go over all entries in the second dict and add them to the
9655 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009656 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009657 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009659 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009661 --todo;
9662 di1 = dict_find(d1, hi2->hi_key, -1);
9663 if (di1 == NULL)
9664 {
9665 di1 = dictitem_copy(HI2DI(hi2));
9666 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9667 dictitem_free(di1);
9668 }
9669 else if (*action == 'e')
9670 {
9671 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9672 break;
9673 }
9674 else if (*action == 'f')
9675 {
9676 clear_tv(&di1->di_tv);
9677 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9678 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009679 }
9680 }
9681
Bram Moolenaare9a41262005-01-15 22:18:47 +00009682 copy_tv(&argvars[0], rettv);
9683 }
9684 }
9685 else
9686 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009687}
9688
9689/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009690 * "feedkeys()" function
9691 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009692 static void
9693f_feedkeys(argvars, rettv)
9694 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009695 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009696{
9697 int remap = TRUE;
9698 char_u *keys, *flags;
9699 char_u nbuf[NUMBUFLEN];
9700 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009701 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009702
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009703 /* This is not allowed in the sandbox. If the commands would still be
9704 * executed in the sandbox it would be OK, but it probably happens later,
9705 * when "sandbox" is no longer set. */
9706 if (check_secure())
9707 return;
9708
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009709 keys = get_tv_string(&argvars[0]);
9710 if (*keys != NUL)
9711 {
9712 if (argvars[1].v_type != VAR_UNKNOWN)
9713 {
9714 flags = get_tv_string_buf(&argvars[1], nbuf);
9715 for ( ; *flags != NUL; ++flags)
9716 {
9717 switch (*flags)
9718 {
9719 case 'n': remap = FALSE; break;
9720 case 'm': remap = TRUE; break;
9721 case 't': typed = TRUE; break;
9722 }
9723 }
9724 }
9725
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009726 /* Need to escape K_SPECIAL and CSI before putting the string in the
9727 * typeahead buffer. */
9728 keys_esc = vim_strsave_escape_csi(keys);
9729 if (keys_esc != NULL)
9730 {
9731 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009732 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009733 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009734 if (vgetc_busy)
9735 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009736 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009737 }
9738}
9739
9740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 * "filereadable()" function
9742 */
9743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009745 typval_T *argvars;
9746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009748 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 char_u *p;
9750 int n;
9751
Bram Moolenaarc236c162008-07-13 17:41:49 +00009752#ifndef O_NONBLOCK
9753# define O_NONBLOCK 0
9754#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009755 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009756 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9757 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 {
9759 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009760 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 }
9762 else
9763 n = FALSE;
9764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766}
9767
9768/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009769 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 * rights to write into.
9771 */
9772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009774 typval_T *argvars;
9775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009777 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778}
9779
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009780static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009781
9782 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009783findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009784 typval_T *argvars;
9785 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009786 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009787{
9788#ifdef FEAT_SEARCHPATH
9789 char_u *fname;
9790 char_u *fresult = NULL;
9791 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9792 char_u *p;
9793 char_u pathbuf[NUMBUFLEN];
9794 int count = 1;
9795 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009796 int error = FALSE;
9797#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009798
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009799 rettv->vval.v_string = NULL;
9800 rettv->v_type = VAR_STRING;
9801
9802#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009804
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009805 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009807 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9808 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009809 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009810 else
9811 {
9812 if (*p != NUL)
9813 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009815 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009816 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009817 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009818 }
9819
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009820 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9821 error = TRUE;
9822
9823 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009825 do
9826 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009827 if (rettv->v_type == VAR_STRING)
9828 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009829 fresult = find_file_in_path_option(first ? fname : NULL,
9830 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009831 0, first, path,
9832 find_what,
9833 curbuf->b_ffname,
9834 find_what == FINDFILE_DIR
9835 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009836 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009837
9838 if (fresult != NULL && rettv->v_type == VAR_LIST)
9839 list_append_string(rettv->vval.v_list, fresult, -1);
9840
9841 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009842 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009843
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009844 if (rettv->v_type == VAR_STRING)
9845 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009846#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009847}
9848
Bram Moolenaar33570922005-01-25 22:26:29 +00009849static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9850static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009851
9852/*
9853 * Implementation of map() and filter().
9854 */
9855 static void
9856filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009857 typval_T *argvars;
9858 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009859 int map;
9860{
9861 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009862 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009863 listitem_T *li, *nli;
9864 list_T *l = NULL;
9865 dictitem_T *di;
9866 hashtab_T *ht;
9867 hashitem_T *hi;
9868 dict_T *d = NULL;
9869 typval_T save_val;
9870 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009871 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009872 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009873 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009874 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009875
Bram Moolenaare9a41262005-01-15 22:18:47 +00009876 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009877 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009878 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009879 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009880 return;
9881 }
9882 else if (argvars[0].v_type == VAR_DICT)
9883 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009884 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009885 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009886 return;
9887 }
9888 else
9889 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009890 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009891 return;
9892 }
9893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 expr = get_tv_string_buf_chk(&argvars[1], buf);
9895 /* On type errors, the preceding call has already displayed an error
9896 * message. Avoid a misleading error message for an empty string that
9897 * was not passed as argument. */
9898 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009900 prepare_vimvar(VV_VAL, &save_val);
9901 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009902
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009903 /* We reset "did_emsg" to be able to detect whether an error
9904 * occurred during evaluation of the expression. */
9905 save_did_emsg = did_emsg;
9906 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009908 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009910 prepare_vimvar(VV_KEY, &save_key);
9911 vimvars[VV_KEY].vv_type = VAR_STRING;
9912
9913 ht = &d->dv_hashtab;
9914 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009915 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009916 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 if (!HASHITEM_EMPTY(hi))
9919 {
9920 --todo;
9921 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009922 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 break;
9924 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009925 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009926 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009927 break;
9928 if (!map && rem)
9929 dictitem_remove(d, di);
9930 clear_tv(&vimvars[VV_KEY].vv_tv);
9931 }
9932 }
9933 hash_unlock(ht);
9934
9935 restore_vimvar(VV_KEY, &save_key);
9936 }
9937 else
9938 {
9939 for (li = l->lv_first; li != NULL; li = nli)
9940 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009941 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009942 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009943 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009944 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009945 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009946 break;
9947 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009949 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009950 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009953
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009954 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009955 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009956
9957 copy_tv(&argvars[0], rettv);
9958}
9959
9960 static int
9961filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009962 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963 char_u *expr;
9964 int map;
9965 int *remp;
9966{
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009968 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009969 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970
Bram Moolenaar33570922005-01-25 22:26:29 +00009971 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009972 s = expr;
9973 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009974 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009975 if (*s != NUL) /* check for trailing chars after expr */
9976 {
9977 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009978 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009979 }
9980 if (map)
9981 {
9982 /* map(): replace the list item value */
9983 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009984 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009985 *tv = rettv;
9986 }
9987 else
9988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009989 int error = FALSE;
9990
Bram Moolenaare9a41262005-01-15 22:18:47 +00009991 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009992 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009993 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009994 /* On type error, nothing has been removed; return FAIL to stop the
9995 * loop. The error message was given by get_tv_number_chk(). */
9996 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009997 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009998 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009999 retval = OK;
10000theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010002 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010003}
10004
10005/*
10006 * "filter()" function
10007 */
10008 static void
10009f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010010 typval_T *argvars;
10011 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010012{
10013 filter_map(argvars, rettv, FALSE);
10014}
10015
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010017 * "finddir({fname}[, {path}[, {count}]])" function
10018 */
10019 static void
10020f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010021 typval_T *argvars;
10022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010023{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010024 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010025}
10026
10027/*
10028 * "findfile({fname}[, {path}[, {count}]])" function
10029 */
10030 static void
10031f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010032 typval_T *argvars;
10033 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010034{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010035 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010036}
10037
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010038#ifdef FEAT_FLOAT
10039/*
10040 * "float2nr({float})" function
10041 */
10042 static void
10043f_float2nr(argvars, rettv)
10044 typval_T *argvars;
10045 typval_T *rettv;
10046{
10047 float_T f;
10048
10049 if (get_float_arg(argvars, &f) == OK)
10050 {
10051 if (f < -0x7fffffff)
10052 rettv->vval.v_number = -0x7fffffff;
10053 else if (f > 0x7fffffff)
10054 rettv->vval.v_number = 0x7fffffff;
10055 else
10056 rettv->vval.v_number = (varnumber_T)f;
10057 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010058}
10059
10060/*
10061 * "floor({float})" function
10062 */
10063 static void
10064f_floor(argvars, rettv)
10065 typval_T *argvars;
10066 typval_T *rettv;
10067{
10068 float_T f;
10069
10070 rettv->v_type = VAR_FLOAT;
10071 if (get_float_arg(argvars, &f) == OK)
10072 rettv->vval.v_float = floor(f);
10073 else
10074 rettv->vval.v_float = 0.0;
10075}
10076#endif
10077
Bram Moolenaar0d660222005-01-07 21:51:51 +000010078/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010079 * "fnameescape({string})" function
10080 */
10081 static void
10082f_fnameescape(argvars, rettv)
10083 typval_T *argvars;
10084 typval_T *rettv;
10085{
10086 rettv->vval.v_string = vim_strsave_fnameescape(
10087 get_tv_string(&argvars[0]), FALSE);
10088 rettv->v_type = VAR_STRING;
10089}
10090
10091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 * "fnamemodify({fname}, {mods})" function
10093 */
10094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 typval_T *argvars;
10097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098{
10099 char_u *fname;
10100 char_u *mods;
10101 int usedlen = 0;
10102 int len;
10103 char_u *fbuf = NULL;
10104 char_u buf[NUMBUFLEN];
10105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 fname = get_tv_string_chk(&argvars[0]);
10107 mods = get_tv_string_buf_chk(&argvars[1], buf);
10108 if (fname == NULL || mods == NULL)
10109 fname = NULL;
10110 else
10111 {
10112 len = (int)STRLEN(fname);
10113 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010118 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 vim_free(fbuf);
10122}
10123
Bram Moolenaar33570922005-01-25 22:26:29 +000010124static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
10126/*
10127 * "foldclosed()" function
10128 */
10129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010131 typval_T *argvars;
10132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 int end;
10134{
10135#ifdef FEAT_FOLDING
10136 linenr_T lnum;
10137 linenr_T first, last;
10138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10141 {
10142 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10143 {
10144 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 return;
10149 }
10150 }
10151#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153}
10154
10155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010156 * "foldclosed()" function
10157 */
10158 static void
10159f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010160 typval_T *argvars;
10161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010162{
10163 foldclosed_both(argvars, rettv, FALSE);
10164}
10165
10166/*
10167 * "foldclosedend()" function
10168 */
10169 static void
10170f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010171 typval_T *argvars;
10172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010173{
10174 foldclosed_both(argvars, rettv, TRUE);
10175}
10176
10177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 * "foldlevel()" function
10179 */
10180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010182 typval_T *argvars;
10183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184{
10185#ifdef FEAT_FOLDING
10186 linenr_T lnum;
10187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010188 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192}
10193
10194/*
10195 * "foldtext()" function
10196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010198f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010199 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201{
10202#ifdef FEAT_FOLDING
10203 linenr_T lnum;
10204 char_u *s;
10205 char_u *r;
10206 int len;
10207 char *txt;
10208#endif
10209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010210 rettv->v_type = VAR_STRING;
10211 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010213 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10214 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10215 <= curbuf->b_ml.ml_line_count
10216 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 {
10218 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010219 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10220 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 {
10222 if (!linewhite(lnum))
10223 break;
10224 ++lnum;
10225 }
10226
10227 /* Find interesting text in this line. */
10228 s = skipwhite(ml_get(lnum));
10229 /* skip C comment-start */
10230 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010233 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010234 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010235 {
10236 s = skipwhite(ml_get(lnum + 1));
10237 if (*s == '*')
10238 s = skipwhite(s + 1);
10239 }
10240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 txt = _("+-%s%3ld lines: ");
10242 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 + 20 /* for %3ld */
10245 + STRLEN(s))); /* concatenated */
10246 if (r != NULL)
10247 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10249 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10250 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 len = (int)STRLEN(r);
10252 STRCAT(r, s);
10253 /* remove 'foldmarker' and 'commentstring' */
10254 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 }
10257 }
10258#endif
10259}
10260
10261/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010262 * "foldtextresult(lnum)" function
10263 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010265f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010266 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010267 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010268{
10269#ifdef FEAT_FOLDING
10270 linenr_T lnum;
10271 char_u *text;
10272 char_u buf[51];
10273 foldinfo_T foldinfo;
10274 int fold_count;
10275#endif
10276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 rettv->v_type = VAR_STRING;
10278 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010279#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 /* treat illegal types and illegal string values for {lnum} the same */
10282 if (lnum < 0)
10283 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010284 fold_count = foldedCount(curwin, lnum, &foldinfo);
10285 if (fold_count > 0)
10286 {
10287 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10288 &foldinfo, buf);
10289 if (text == buf)
10290 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010291 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010292 }
10293#endif
10294}
10295
10296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 * "foreground()" function
10298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010300f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010301 typval_T *argvars UNUSED;
10302 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304#ifdef FEAT_GUI
10305 if (gui.in_use)
10306 gui_mch_set_foreground();
10307#else
10308# ifdef WIN32
10309 win32_set_foreground();
10310# endif
10311#endif
10312}
10313
10314/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010315 * "function()" function
10316 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010319 typval_T *argvars;
10320 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010321{
10322 char_u *s;
10323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010325 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010326 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010327 /* Don't check an autoload name for existence here. */
10328 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010329 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010330 else
10331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010332 rettv->vval.v_string = vim_strsave(s);
10333 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010334 }
10335}
10336
10337/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010338 * "garbagecollect()" function
10339 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010340 static void
10341f_garbagecollect(argvars, rettv)
10342 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010343 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010344{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010345 /* This is postponed until we are back at the toplevel, because we may be
10346 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10347 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010348
10349 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10350 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010351}
10352
10353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010354 * "get()" function
10355 */
10356 static void
10357f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 typval_T *argvars;
10359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010360{
Bram Moolenaar33570922005-01-25 22:26:29 +000010361 listitem_T *li;
10362 list_T *l;
10363 dictitem_T *di;
10364 dict_T *d;
10365 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010366
Bram Moolenaare9a41262005-01-15 22:18:47 +000010367 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010368 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010369 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010371 int error = FALSE;
10372
10373 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10374 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010375 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010376 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010377 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010378 else if (argvars[0].v_type == VAR_DICT)
10379 {
10380 if ((d = argvars[0].vval.v_dict) != NULL)
10381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010382 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383 if (di != NULL)
10384 tv = &di->di_tv;
10385 }
10386 }
10387 else
10388 EMSG2(_(e_listdictarg), "get()");
10389
10390 if (tv == NULL)
10391 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010392 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010393 copy_tv(&argvars[2], rettv);
10394 }
10395 else
10396 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010397}
10398
Bram Moolenaar342337a2005-07-21 21:11:17 +000010399static 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 +000010400
10401/*
10402 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010403 * Return a range (from start to end) of lines in rettv from the specified
10404 * buffer.
10405 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010406 */
10407 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010408get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010409 buf_T *buf;
10410 linenr_T start;
10411 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010412 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010413 typval_T *rettv;
10414{
10415 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010416
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010417 if (retlist && rettv_list_alloc(rettv) == FAIL)
10418 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010419
10420 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10421 return;
10422
10423 if (!retlist)
10424 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010425 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10426 p = ml_get_buf(buf, start, FALSE);
10427 else
10428 p = (char_u *)"";
10429
10430 rettv->v_type = VAR_STRING;
10431 rettv->vval.v_string = vim_strsave(p);
10432 }
10433 else
10434 {
10435 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010436 return;
10437
10438 if (start < 1)
10439 start = 1;
10440 if (end > buf->b_ml.ml_line_count)
10441 end = buf->b_ml.ml_line_count;
10442 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010443 if (list_append_string(rettv->vval.v_list,
10444 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010445 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010446 }
10447}
10448
10449/*
10450 * "getbufline()" function
10451 */
10452 static void
10453f_getbufline(argvars, rettv)
10454 typval_T *argvars;
10455 typval_T *rettv;
10456{
10457 linenr_T lnum;
10458 linenr_T end;
10459 buf_T *buf;
10460
10461 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10462 ++emsg_off;
10463 buf = get_buf_tv(&argvars[0]);
10464 --emsg_off;
10465
Bram Moolenaar661b1822005-07-28 22:36:45 +000010466 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010467 if (argvars[2].v_type == VAR_UNKNOWN)
10468 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010469 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010470 end = get_tv_lnum_buf(&argvars[2], buf);
10471
Bram Moolenaar342337a2005-07-21 21:11:17 +000010472 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010473}
10474
Bram Moolenaar0d660222005-01-07 21:51:51 +000010475/*
10476 * "getbufvar()" function
10477 */
10478 static void
10479f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010480 typval_T *argvars;
10481 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010482{
10483 buf_T *buf;
10484 buf_T *save_curbuf;
10485 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010486 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10489 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010490 ++emsg_off;
10491 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010492
10493 rettv->v_type = VAR_STRING;
10494 rettv->vval.v_string = NULL;
10495
10496 if (buf != NULL && varname != NULL)
10497 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010498 /* set curbuf to be our buf, temporarily */
10499 save_curbuf = curbuf;
10500 curbuf = buf;
10501
Bram Moolenaar0d660222005-01-07 21:51:51 +000010502 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010503 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010504 else
10505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010506 if (*varname == NUL)
10507 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10508 * scope prefix before the NUL byte is required by
10509 * find_var_in_ht(). */
10510 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010511 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010512 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010513 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010514 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010515 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010516
10517 /* restore previous notion of curbuf */
10518 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010519 }
10520
10521 --emsg_off;
10522}
10523
10524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 * "getchar()" function
10526 */
10527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010529 typval_T *argvars;
10530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
10532 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010533 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010535 /* Position the cursor. Needed after a message that ends in a space. */
10536 windgoto(msg_row, msg_col);
10537
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 ++no_mapping;
10539 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010540 for (;;)
10541 {
10542 if (argvars[0].v_type == VAR_UNKNOWN)
10543 /* getchar(): blocking wait. */
10544 n = safe_vgetc();
10545 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10546 /* getchar(1): only check if char avail */
10547 n = vpeekc();
10548 else if (error || vpeekc() == NUL)
10549 /* illegal argument or getchar(0) and no char avail: return zero */
10550 n = 0;
10551 else
10552 /* getchar(0) and char avail: return char */
10553 n = safe_vgetc();
10554 if (n == K_IGNORE)
10555 continue;
10556 break;
10557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 --no_mapping;
10559 --allow_keys;
10560
Bram Moolenaar219b8702006-11-01 14:32:36 +000010561 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10562 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10563 vimvars[VV_MOUSE_COL].vv_nr = 0;
10564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 if (IS_SPECIAL(n) || mod_mask != 0)
10567 {
10568 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10569 int i = 0;
10570
10571 /* Turn a special key into three bytes, plus modifier. */
10572 if (mod_mask != 0)
10573 {
10574 temp[i++] = K_SPECIAL;
10575 temp[i++] = KS_MODIFIER;
10576 temp[i++] = mod_mask;
10577 }
10578 if (IS_SPECIAL(n))
10579 {
10580 temp[i++] = K_SPECIAL;
10581 temp[i++] = K_SECOND(n);
10582 temp[i++] = K_THIRD(n);
10583 }
10584#ifdef FEAT_MBYTE
10585 else if (has_mbyte)
10586 i += (*mb_char2bytes)(n, temp + i);
10587#endif
10588 else
10589 temp[i++] = n;
10590 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010591 rettv->v_type = VAR_STRING;
10592 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010593
10594#ifdef FEAT_MOUSE
10595 if (n == K_LEFTMOUSE
10596 || n == K_LEFTMOUSE_NM
10597 || n == K_LEFTDRAG
10598 || n == K_LEFTRELEASE
10599 || n == K_LEFTRELEASE_NM
10600 || n == K_MIDDLEMOUSE
10601 || n == K_MIDDLEDRAG
10602 || n == K_MIDDLERELEASE
10603 || n == K_RIGHTMOUSE
10604 || n == K_RIGHTDRAG
10605 || n == K_RIGHTRELEASE
10606 || n == K_X1MOUSE
10607 || n == K_X1DRAG
10608 || n == K_X1RELEASE
10609 || n == K_X2MOUSE
10610 || n == K_X2DRAG
10611 || n == K_X2RELEASE
10612 || n == K_MOUSEDOWN
10613 || n == K_MOUSEUP)
10614 {
10615 int row = mouse_row;
10616 int col = mouse_col;
10617 win_T *win;
10618 linenr_T lnum;
10619# ifdef FEAT_WINDOWS
10620 win_T *wp;
10621# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010622 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010623
10624 if (row >= 0 && col >= 0)
10625 {
10626 /* Find the window at the mouse coordinates and compute the
10627 * text position. */
10628 win = mouse_find_win(&row, &col);
10629 (void)mouse_comp_pos(win, &row, &col, &lnum);
10630# ifdef FEAT_WINDOWS
10631 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010632 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010633# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010634 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010635 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10636 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10637 }
10638 }
10639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 }
10641}
10642
10643/*
10644 * "getcharmod()" function
10645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010647f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010648 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010651 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652}
10653
10654/*
10655 * "getcmdline()" function
10656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010659 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010662 rettv->v_type = VAR_STRING;
10663 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664}
10665
10666/*
10667 * "getcmdpos()" function
10668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010671 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010674 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675}
10676
10677/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010678 * "getcmdtype()" function
10679 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010680 static void
10681f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010682 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010683 typval_T *rettv;
10684{
10685 rettv->v_type = VAR_STRING;
10686 rettv->vval.v_string = alloc(2);
10687 if (rettv->vval.v_string != NULL)
10688 {
10689 rettv->vval.v_string[0] = get_cmdline_type();
10690 rettv->vval.v_string[1] = NUL;
10691 }
10692}
10693
10694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 * "getcwd()" function
10696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010699 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
10702 char_u cwd[MAXPATHL];
10703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 else
10708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010711 if (rettv->vval.v_string != NULL)
10712 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713#endif
10714 }
10715}
10716
10717/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010718 * "getfontname()" function
10719 */
10720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010722 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010723 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010724{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725 rettv->v_type = VAR_STRING;
10726 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010727#ifdef FEAT_GUI
10728 if (gui.in_use)
10729 {
10730 GuiFont font;
10731 char_u *name = NULL;
10732
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010733 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010734 {
10735 /* Get the "Normal" font. Either the name saved by
10736 * hl_set_font_name() or from the font ID. */
10737 font = gui.norm_font;
10738 name = hl_get_font_name();
10739 }
10740 else
10741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010743 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10744 return;
10745 font = gui_mch_get_font(name, FALSE);
10746 if (font == NOFONT)
10747 return; /* Invalid font name, return empty string. */
10748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010750 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010751 gui_mch_free_font(font);
10752 }
10753#endif
10754}
10755
10756/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010757 * "getfperm({fname})" function
10758 */
10759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010761 typval_T *argvars;
10762 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010763{
10764 char_u *fname;
10765 struct stat st;
10766 char_u *perm = NULL;
10767 char_u flags[] = "rwx";
10768 int i;
10769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010773 if (mch_stat((char *)fname, &st) >= 0)
10774 {
10775 perm = vim_strsave((char_u *)"---------");
10776 if (perm != NULL)
10777 {
10778 for (i = 0; i < 9; i++)
10779 {
10780 if (st.st_mode & (1 << (8 - i)))
10781 perm[i] = flags[i % 3];
10782 }
10783 }
10784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010785 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010786}
10787
10788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 * "getfsize({fname})" function
10790 */
10791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010793 typval_T *argvars;
10794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795{
10796 char_u *fname;
10797 struct stat st;
10798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802
10803 if (mch_stat((char *)fname, &st) >= 0)
10804 {
10805 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010810
10811 /* non-perfect check for overflow */
10812 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10813 rettv->vval.v_number = -2;
10814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 }
10816 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818}
10819
10820/*
10821 * "getftime({fname})" function
10822 */
10823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010825 typval_T *argvars;
10826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827{
10828 char_u *fname;
10829 struct stat st;
10830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832
10833 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010834 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837}
10838
10839/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010840 * "getftype({fname})" function
10841 */
10842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010844 typval_T *argvars;
10845 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010846{
10847 char_u *fname;
10848 struct stat st;
10849 char_u *type = NULL;
10850 char *t;
10851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010855 if (mch_lstat((char *)fname, &st) >= 0)
10856 {
10857#ifdef S_ISREG
10858 if (S_ISREG(st.st_mode))
10859 t = "file";
10860 else if (S_ISDIR(st.st_mode))
10861 t = "dir";
10862# ifdef S_ISLNK
10863 else if (S_ISLNK(st.st_mode))
10864 t = "link";
10865# endif
10866# ifdef S_ISBLK
10867 else if (S_ISBLK(st.st_mode))
10868 t = "bdev";
10869# endif
10870# ifdef S_ISCHR
10871 else if (S_ISCHR(st.st_mode))
10872 t = "cdev";
10873# endif
10874# ifdef S_ISFIFO
10875 else if (S_ISFIFO(st.st_mode))
10876 t = "fifo";
10877# endif
10878# ifdef S_ISSOCK
10879 else if (S_ISSOCK(st.st_mode))
10880 t = "fifo";
10881# endif
10882 else
10883 t = "other";
10884#else
10885# ifdef S_IFMT
10886 switch (st.st_mode & S_IFMT)
10887 {
10888 case S_IFREG: t = "file"; break;
10889 case S_IFDIR: t = "dir"; break;
10890# ifdef S_IFLNK
10891 case S_IFLNK: t = "link"; break;
10892# endif
10893# ifdef S_IFBLK
10894 case S_IFBLK: t = "bdev"; break;
10895# endif
10896# ifdef S_IFCHR
10897 case S_IFCHR: t = "cdev"; break;
10898# endif
10899# ifdef S_IFIFO
10900 case S_IFIFO: t = "fifo"; break;
10901# endif
10902# ifdef S_IFSOCK
10903 case S_IFSOCK: t = "socket"; break;
10904# endif
10905 default: t = "other";
10906 }
10907# else
10908 if (mch_isdir(fname))
10909 t = "dir";
10910 else
10911 t = "file";
10912# endif
10913#endif
10914 type = vim_strsave((char_u *)t);
10915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010917}
10918
10919/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010920 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010921 */
10922 static void
10923f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 typval_T *argvars;
10925 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010926{
10927 linenr_T lnum;
10928 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010929 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010930
10931 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010932 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010933 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010934 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010935 retlist = FALSE;
10936 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010937 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010938 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010939 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010940 retlist = TRUE;
10941 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010942
Bram Moolenaar342337a2005-07-21 21:11:17 +000010943 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010944}
10945
10946/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010947 * "getmatches()" function
10948 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010949 static void
10950f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010951 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010952 typval_T *rettv;
10953{
10954#ifdef FEAT_SEARCH_EXTRA
10955 dict_T *dict;
10956 matchitem_T *cur = curwin->w_match_head;
10957
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010958 if (rettv_list_alloc(rettv) == OK)
10959 {
10960 while (cur != NULL)
10961 {
10962 dict = dict_alloc();
10963 if (dict == NULL)
10964 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010965 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10966 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10967 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10968 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10969 list_append_dict(rettv->vval.v_list, dict);
10970 cur = cur->next;
10971 }
10972 }
10973#endif
10974}
10975
10976/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000010977 * "getpid()" function
10978 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000010979 static void
10980f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010981 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000010982 typval_T *rettv;
10983{
10984 rettv->vval.v_number = mch_get_pid();
10985}
10986
10987/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010988 * "getpos(string)" function
10989 */
10990 static void
10991f_getpos(argvars, rettv)
10992 typval_T *argvars;
10993 typval_T *rettv;
10994{
10995 pos_T *fp;
10996 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010997 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010998
10999 if (rettv_list_alloc(rettv) == OK)
11000 {
11001 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011002 fp = var2fpos(&argvars[0], TRUE, &fnum);
11003 if (fnum != -1)
11004 list_append_number(l, (varnumber_T)fnum);
11005 else
11006 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011007 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11008 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011009 list_append_number(l, (fp != NULL)
11010 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011011 : (varnumber_T)0);
11012 list_append_number(l,
11013#ifdef FEAT_VIRTUALEDIT
11014 (fp != NULL) ? (varnumber_T)fp->coladd :
11015#endif
11016 (varnumber_T)0);
11017 }
11018 else
11019 rettv->vval.v_number = FALSE;
11020}
11021
11022/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011023 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011024 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011025 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011026f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011027 typval_T *argvars UNUSED;
11028 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011029{
11030#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011031 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011032#endif
11033
Bram Moolenaar2641f772005-03-25 21:58:17 +000011034#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011035 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011036 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011037 wp = NULL;
11038 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11039 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011040 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011041 if (wp == NULL)
11042 return;
11043 }
11044
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011045 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011046 }
11047#endif
11048}
11049
11050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 * "getreg()" function
11052 */
11053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057{
11058 char_u *strregname;
11059 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011060 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011063 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 strregname = get_tv_string_chk(&argvars[0]);
11066 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011067 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011068 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011071 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 regname = (strregname == NULL ? '"' : *strregname);
11073 if (regname == 0)
11074 regname = '"';
11075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 rettv->vval.v_string = error ? NULL :
11078 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079}
11080
11081/*
11082 * "getregtype()" function
11083 */
11084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011086 typval_T *argvars;
11087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
11089 char_u *strregname;
11090 int regname;
11091 char_u buf[NUMBUFLEN + 2];
11092 long reglen = 0;
11093
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011094 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 {
11096 strregname = get_tv_string_chk(&argvars[0]);
11097 if (strregname == NULL) /* type error; errmsg already given */
11098 {
11099 rettv->v_type = VAR_STRING;
11100 rettv->vval.v_string = NULL;
11101 return;
11102 }
11103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 else
11105 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107
11108 regname = (strregname == NULL ? '"' : *strregname);
11109 if (regname == 0)
11110 regname = '"';
11111
11112 buf[0] = NUL;
11113 buf[1] = NUL;
11114 switch (get_reg_type(regname, &reglen))
11115 {
11116 case MLINE: buf[0] = 'V'; break;
11117 case MCHAR: buf[0] = 'v'; break;
11118#ifdef FEAT_VISUAL
11119 case MBLOCK:
11120 buf[0] = Ctrl_V;
11121 sprintf((char *)buf + 1, "%ld", reglen + 1);
11122 break;
11123#endif
11124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125 rettv->v_type = VAR_STRING;
11126 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127}
11128
11129/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011130 * "gettabwinvar()" function
11131 */
11132 static void
11133f_gettabwinvar(argvars, rettv)
11134 typval_T *argvars;
11135 typval_T *rettv;
11136{
11137 getwinvar(argvars, rettv, 1);
11138}
11139
11140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 * "getwinposx()" function
11142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011145 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149#ifdef FEAT_GUI
11150 if (gui.in_use)
11151 {
11152 int x, y;
11153
11154 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 }
11157#endif
11158}
11159
11160/*
11161 * "getwinposy()" function
11162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011165 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169#ifdef FEAT_GUI
11170 if (gui.in_use)
11171 {
11172 int x, y;
11173
11174 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 }
11177#endif
11178}
11179
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011180/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011181 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011182 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011183 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011184find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011185 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011186 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011187{
11188#ifdef FEAT_WINDOWS
11189 win_T *wp;
11190#endif
11191 int nr;
11192
11193 nr = get_tv_number_chk(vp, NULL);
11194
11195#ifdef FEAT_WINDOWS
11196 if (nr < 0)
11197 return NULL;
11198 if (nr == 0)
11199 return curwin;
11200
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011201 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11202 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011203 if (--nr <= 0)
11204 break;
11205 return wp;
11206#else
11207 if (nr == 0 || nr == 1)
11208 return curwin;
11209 return NULL;
11210#endif
11211}
11212
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213/*
11214 * "getwinvar()" function
11215 */
11216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011218 typval_T *argvars;
11219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011221 getwinvar(argvars, rettv, 0);
11222}
11223
11224/*
11225 * getwinvar() and gettabwinvar()
11226 */
11227 static void
11228getwinvar(argvars, rettv, off)
11229 typval_T *argvars;
11230 typval_T *rettv;
11231 int off; /* 1 for gettabwinvar() */
11232{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 win_T *win, *oldcurwin;
11234 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011235 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011236 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011238#ifdef FEAT_WINDOWS
11239 if (off == 1)
11240 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11241 else
11242 tp = curtab;
11243#endif
11244 win = find_win_by_nr(&argvars[off], tp);
11245 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011246 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248 rettv->v_type = VAR_STRING;
11249 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250
11251 if (win != NULL && varname != NULL)
11252 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011253 /* Set curwin to be our win, temporarily. Also set curbuf, so
11254 * that we can get buffer-local options. */
11255 oldcurwin = curwin;
11256 curwin = win;
11257 curbuf = win->w_buffer;
11258
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 else
11262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011263 if (*varname == NUL)
11264 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11265 * scope prefix before the NUL byte is required by
11266 * find_var_in_ht(). */
11267 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011269 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011271 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011273
11274 /* restore previous notion of curwin */
11275 curwin = oldcurwin;
11276 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 }
11278
11279 --emsg_off;
11280}
11281
11282/*
11283 * "glob()" function
11284 */
11285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011287 typval_T *argvars;
11288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011290 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011292 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011294 /* When the optional second argument is non-zero, don't remove matches
11295 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11296 if (argvars[1].v_type != VAR_UNKNOWN
11297 && get_tv_number_chk(&argvars[1], &error))
11298 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011299 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011300 if (!error)
11301 {
11302 ExpandInit(&xpc);
11303 xpc.xp_context = EXPAND_FILES;
11304 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11305 NULL, flags, WILD_ALL);
11306 }
11307 else
11308 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309}
11310
11311/*
11312 * "globpath()" function
11313 */
11314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011316 typval_T *argvars;
11317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011319 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011321 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011322 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011324 /* When the optional second argument is non-zero, don't remove matches
11325 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11326 if (argvars[2].v_type != VAR_UNKNOWN
11327 && get_tv_number_chk(&argvars[2], &error))
11328 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011330 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 rettv->vval.v_string = NULL;
11332 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011333 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11334 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335}
11336
11337/*
11338 * "has()" function
11339 */
11340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011342 typval_T *argvars;
11343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344{
11345 int i;
11346 char_u *name;
11347 int n = FALSE;
11348 static char *(has_list[]) =
11349 {
11350#ifdef AMIGA
11351 "amiga",
11352# ifdef FEAT_ARP
11353 "arp",
11354# endif
11355#endif
11356#ifdef __BEOS__
11357 "beos",
11358#endif
11359#ifdef MSDOS
11360# ifdef DJGPP
11361 "dos32",
11362# else
11363 "dos16",
11364# endif
11365#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011366#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 "mac",
11368#endif
11369#if defined(MACOS_X_UNIX)
11370 "macunix",
11371#endif
11372#ifdef OS2
11373 "os2",
11374#endif
11375#ifdef __QNX__
11376 "qnx",
11377#endif
11378#ifdef RISCOS
11379 "riscos",
11380#endif
11381#ifdef UNIX
11382 "unix",
11383#endif
11384#ifdef VMS
11385 "vms",
11386#endif
11387#ifdef WIN16
11388 "win16",
11389#endif
11390#ifdef WIN32
11391 "win32",
11392#endif
11393#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11394 "win32unix",
11395#endif
11396#ifdef WIN64
11397 "win64",
11398#endif
11399#ifdef EBCDIC
11400 "ebcdic",
11401#endif
11402#ifndef CASE_INSENSITIVE_FILENAME
11403 "fname_case",
11404#endif
11405#ifdef FEAT_ARABIC
11406 "arabic",
11407#endif
11408#ifdef FEAT_AUTOCMD
11409 "autocmd",
11410#endif
11411#ifdef FEAT_BEVAL
11412 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011413# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11414 "balloon_multiline",
11415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416#endif
11417#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11418 "builtin_terms",
11419# ifdef ALL_BUILTIN_TCAPS
11420 "all_builtin_terms",
11421# endif
11422#endif
11423#ifdef FEAT_BYTEOFF
11424 "byte_offset",
11425#endif
11426#ifdef FEAT_CINDENT
11427 "cindent",
11428#endif
11429#ifdef FEAT_CLIENTSERVER
11430 "clientserver",
11431#endif
11432#ifdef FEAT_CLIPBOARD
11433 "clipboard",
11434#endif
11435#ifdef FEAT_CMDL_COMPL
11436 "cmdline_compl",
11437#endif
11438#ifdef FEAT_CMDHIST
11439 "cmdline_hist",
11440#endif
11441#ifdef FEAT_COMMENTS
11442 "comments",
11443#endif
11444#ifdef FEAT_CRYPT
11445 "cryptv",
11446#endif
11447#ifdef FEAT_CSCOPE
11448 "cscope",
11449#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011450#ifdef CURSOR_SHAPE
11451 "cursorshape",
11452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453#ifdef DEBUG
11454 "debug",
11455#endif
11456#ifdef FEAT_CON_DIALOG
11457 "dialog_con",
11458#endif
11459#ifdef FEAT_GUI_DIALOG
11460 "dialog_gui",
11461#endif
11462#ifdef FEAT_DIFF
11463 "diff",
11464#endif
11465#ifdef FEAT_DIGRAPHS
11466 "digraphs",
11467#endif
11468#ifdef FEAT_DND
11469 "dnd",
11470#endif
11471#ifdef FEAT_EMACS_TAGS
11472 "emacs_tags",
11473#endif
11474 "eval", /* always present, of course! */
11475#ifdef FEAT_EX_EXTRA
11476 "ex_extra",
11477#endif
11478#ifdef FEAT_SEARCH_EXTRA
11479 "extra_search",
11480#endif
11481#ifdef FEAT_FKMAP
11482 "farsi",
11483#endif
11484#ifdef FEAT_SEARCHPATH
11485 "file_in_path",
11486#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011487#if defined(UNIX) && !defined(USE_SYSTEM)
11488 "filterpipe",
11489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490#ifdef FEAT_FIND_ID
11491 "find_in_path",
11492#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011493#ifdef FEAT_FLOAT
11494 "float",
11495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496#ifdef FEAT_FOLDING
11497 "folding",
11498#endif
11499#ifdef FEAT_FOOTER
11500 "footer",
11501#endif
11502#if !defined(USE_SYSTEM) && defined(UNIX)
11503 "fork",
11504#endif
11505#ifdef FEAT_GETTEXT
11506 "gettext",
11507#endif
11508#ifdef FEAT_GUI
11509 "gui",
11510#endif
11511#ifdef FEAT_GUI_ATHENA
11512# ifdef FEAT_GUI_NEXTAW
11513 "gui_neXtaw",
11514# else
11515 "gui_athena",
11516# endif
11517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518#ifdef FEAT_GUI_GTK
11519 "gui_gtk",
11520# ifdef HAVE_GTK2
11521 "gui_gtk2",
11522# endif
11523#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011524#ifdef FEAT_GUI_GNOME
11525 "gui_gnome",
11526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527#ifdef FEAT_GUI_MAC
11528 "gui_mac",
11529#endif
11530#ifdef FEAT_GUI_MOTIF
11531 "gui_motif",
11532#endif
11533#ifdef FEAT_GUI_PHOTON
11534 "gui_photon",
11535#endif
11536#ifdef FEAT_GUI_W16
11537 "gui_win16",
11538#endif
11539#ifdef FEAT_GUI_W32
11540 "gui_win32",
11541#endif
11542#ifdef FEAT_HANGULIN
11543 "hangul_input",
11544#endif
11545#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11546 "iconv",
11547#endif
11548#ifdef FEAT_INS_EXPAND
11549 "insert_expand",
11550#endif
11551#ifdef FEAT_JUMPLIST
11552 "jumplist",
11553#endif
11554#ifdef FEAT_KEYMAP
11555 "keymap",
11556#endif
11557#ifdef FEAT_LANGMAP
11558 "langmap",
11559#endif
11560#ifdef FEAT_LIBCALL
11561 "libcall",
11562#endif
11563#ifdef FEAT_LINEBREAK
11564 "linebreak",
11565#endif
11566#ifdef FEAT_LISP
11567 "lispindent",
11568#endif
11569#ifdef FEAT_LISTCMDS
11570 "listcmds",
11571#endif
11572#ifdef FEAT_LOCALMAP
11573 "localmap",
11574#endif
11575#ifdef FEAT_MENU
11576 "menu",
11577#endif
11578#ifdef FEAT_SESSION
11579 "mksession",
11580#endif
11581#ifdef FEAT_MODIFY_FNAME
11582 "modify_fname",
11583#endif
11584#ifdef FEAT_MOUSE
11585 "mouse",
11586#endif
11587#ifdef FEAT_MOUSESHAPE
11588 "mouseshape",
11589#endif
11590#if defined(UNIX) || defined(VMS)
11591# ifdef FEAT_MOUSE_DEC
11592 "mouse_dec",
11593# endif
11594# ifdef FEAT_MOUSE_GPM
11595 "mouse_gpm",
11596# endif
11597# ifdef FEAT_MOUSE_JSB
11598 "mouse_jsbterm",
11599# endif
11600# ifdef FEAT_MOUSE_NET
11601 "mouse_netterm",
11602# endif
11603# ifdef FEAT_MOUSE_PTERM
11604 "mouse_pterm",
11605# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011606# ifdef FEAT_SYSMOUSE
11607 "mouse_sysmouse",
11608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609# ifdef FEAT_MOUSE_XTERM
11610 "mouse_xterm",
11611# endif
11612#endif
11613#ifdef FEAT_MBYTE
11614 "multi_byte",
11615#endif
11616#ifdef FEAT_MBYTE_IME
11617 "multi_byte_ime",
11618#endif
11619#ifdef FEAT_MULTI_LANG
11620 "multi_lang",
11621#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011622#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011623#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011624 "mzscheme",
11625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627#ifdef FEAT_OLE
11628 "ole",
11629#endif
11630#ifdef FEAT_OSFILETYPE
11631 "osfiletype",
11632#endif
11633#ifdef FEAT_PATH_EXTRA
11634 "path_extra",
11635#endif
11636#ifdef FEAT_PERL
11637#ifndef DYNAMIC_PERL
11638 "perl",
11639#endif
11640#endif
11641#ifdef FEAT_PYTHON
11642#ifndef DYNAMIC_PYTHON
11643 "python",
11644#endif
11645#endif
11646#ifdef FEAT_POSTSCRIPT
11647 "postscript",
11648#endif
11649#ifdef FEAT_PRINTER
11650 "printer",
11651#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011652#ifdef FEAT_PROFILE
11653 "profile",
11654#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011655#ifdef FEAT_RELTIME
11656 "reltime",
11657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658#ifdef FEAT_QUICKFIX
11659 "quickfix",
11660#endif
11661#ifdef FEAT_RIGHTLEFT
11662 "rightleft",
11663#endif
11664#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11665 "ruby",
11666#endif
11667#ifdef FEAT_SCROLLBIND
11668 "scrollbind",
11669#endif
11670#ifdef FEAT_CMDL_INFO
11671 "showcmd",
11672 "cmdline_info",
11673#endif
11674#ifdef FEAT_SIGNS
11675 "signs",
11676#endif
11677#ifdef FEAT_SMARTINDENT
11678 "smartindent",
11679#endif
11680#ifdef FEAT_SNIFF
11681 "sniff",
11682#endif
11683#ifdef FEAT_STL_OPT
11684 "statusline",
11685#endif
11686#ifdef FEAT_SUN_WORKSHOP
11687 "sun_workshop",
11688#endif
11689#ifdef FEAT_NETBEANS_INTG
11690 "netbeans_intg",
11691#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011692#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011693 "spell",
11694#endif
11695#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 "syntax",
11697#endif
11698#if defined(USE_SYSTEM) || !defined(UNIX)
11699 "system",
11700#endif
11701#ifdef FEAT_TAG_BINS
11702 "tag_binary",
11703#endif
11704#ifdef FEAT_TAG_OLDSTATIC
11705 "tag_old_static",
11706#endif
11707#ifdef FEAT_TAG_ANYWHITE
11708 "tag_any_white",
11709#endif
11710#ifdef FEAT_TCL
11711# ifndef DYNAMIC_TCL
11712 "tcl",
11713# endif
11714#endif
11715#ifdef TERMINFO
11716 "terminfo",
11717#endif
11718#ifdef FEAT_TERMRESPONSE
11719 "termresponse",
11720#endif
11721#ifdef FEAT_TEXTOBJ
11722 "textobjects",
11723#endif
11724#ifdef HAVE_TGETENT
11725 "tgetent",
11726#endif
11727#ifdef FEAT_TITLE
11728 "title",
11729#endif
11730#ifdef FEAT_TOOLBAR
11731 "toolbar",
11732#endif
11733#ifdef FEAT_USR_CMDS
11734 "user-commands", /* was accidentally included in 5.4 */
11735 "user_commands",
11736#endif
11737#ifdef FEAT_VIMINFO
11738 "viminfo",
11739#endif
11740#ifdef FEAT_VERTSPLIT
11741 "vertsplit",
11742#endif
11743#ifdef FEAT_VIRTUALEDIT
11744 "virtualedit",
11745#endif
11746#ifdef FEAT_VISUAL
11747 "visual",
11748#endif
11749#ifdef FEAT_VISUALEXTRA
11750 "visualextra",
11751#endif
11752#ifdef FEAT_VREPLACE
11753 "vreplace",
11754#endif
11755#ifdef FEAT_WILDIGN
11756 "wildignore",
11757#endif
11758#ifdef FEAT_WILDMENU
11759 "wildmenu",
11760#endif
11761#ifdef FEAT_WINDOWS
11762 "windows",
11763#endif
11764#ifdef FEAT_WAK
11765 "winaltkeys",
11766#endif
11767#ifdef FEAT_WRITEBACKUP
11768 "writebackup",
11769#endif
11770#ifdef FEAT_XIM
11771 "xim",
11772#endif
11773#ifdef FEAT_XFONTSET
11774 "xfontset",
11775#endif
11776#ifdef USE_XSMP
11777 "xsmp",
11778#endif
11779#ifdef USE_XSMP_INTERACT
11780 "xsmp_interact",
11781#endif
11782#ifdef FEAT_XCLIPBOARD
11783 "xterm_clipboard",
11784#endif
11785#ifdef FEAT_XTERM_SAVE
11786 "xterm_save",
11787#endif
11788#if defined(UNIX) && defined(FEAT_X11)
11789 "X11",
11790#endif
11791 NULL
11792 };
11793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 for (i = 0; has_list[i] != NULL; ++i)
11796 if (STRICMP(name, has_list[i]) == 0)
11797 {
11798 n = TRUE;
11799 break;
11800 }
11801
11802 if (n == FALSE)
11803 {
11804 if (STRNICMP(name, "patch", 5) == 0)
11805 n = has_patch(atoi((char *)name + 5));
11806 else if (STRICMP(name, "vim_starting") == 0)
11807 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000011808#ifdef FEAT_MBYTE
11809 else if (STRICMP(name, "multi_byte_encoding") == 0)
11810 n = has_mbyte;
11811#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000011812#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11813 else if (STRICMP(name, "balloon_multiline") == 0)
11814 n = multiline_balloon_available();
11815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816#ifdef DYNAMIC_TCL
11817 else if (STRICMP(name, "tcl") == 0)
11818 n = tcl_enabled(FALSE);
11819#endif
11820#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11821 else if (STRICMP(name, "iconv") == 0)
11822 n = iconv_enabled(FALSE);
11823#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011824#ifdef DYNAMIC_MZSCHEME
11825 else if (STRICMP(name, "mzscheme") == 0)
11826 n = mzscheme_enabled(FALSE);
11827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828#ifdef DYNAMIC_RUBY
11829 else if (STRICMP(name, "ruby") == 0)
11830 n = ruby_enabled(FALSE);
11831#endif
11832#ifdef DYNAMIC_PYTHON
11833 else if (STRICMP(name, "python") == 0)
11834 n = python_enabled(FALSE);
11835#endif
11836#ifdef DYNAMIC_PERL
11837 else if (STRICMP(name, "perl") == 0)
11838 n = perl_enabled(FALSE);
11839#endif
11840#ifdef FEAT_GUI
11841 else if (STRICMP(name, "gui_running") == 0)
11842 n = (gui.in_use || gui.starting);
11843# ifdef FEAT_GUI_W32
11844 else if (STRICMP(name, "gui_win32s") == 0)
11845 n = gui_is_win32s();
11846# endif
11847# ifdef FEAT_BROWSE
11848 else if (STRICMP(name, "browse") == 0)
11849 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11850# endif
11851#endif
11852#ifdef FEAT_SYN_HL
11853 else if (STRICMP(name, "syntax_items") == 0)
11854 n = syntax_present(curbuf);
11855#endif
11856#if defined(WIN3264)
11857 else if (STRICMP(name, "win95") == 0)
11858 n = mch_windows95();
11859#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011860#ifdef FEAT_NETBEANS_INTG
11861 else if (STRICMP(name, "netbeans_enabled") == 0)
11862 n = usingNetbeans;
11863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 }
11865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867}
11868
11869/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 * "has_key()" function
11871 */
11872 static void
11873f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011874 typval_T *argvars;
11875 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011876{
Bram Moolenaare9a41262005-01-15 22:18:47 +000011877 if (argvars[0].v_type != VAR_DICT)
11878 {
11879 EMSG(_(e_dictreq));
11880 return;
11881 }
11882 if (argvars[0].vval.v_dict == NULL)
11883 return;
11884
11885 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011886 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011887}
11888
11889/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011890 * "haslocaldir()" function
11891 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011892 static void
11893f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011894 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011895 typval_T *rettv;
11896{
11897 rettv->vval.v_number = (curwin->w_localdir != NULL);
11898}
11899
11900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 * "hasmapto()" function
11902 */
11903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011905 typval_T *argvars;
11906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907{
11908 char_u *name;
11909 char_u *mode;
11910 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011911 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011914 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 mode = (char_u *)"nvo";
11916 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011919 if (argvars[2].v_type != VAR_UNKNOWN)
11920 abbr = get_tv_number(&argvars[2]);
11921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922
Bram Moolenaar2c932302006-03-18 21:42:09 +000011923 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927}
11928
11929/*
11930 * "histadd()" function
11931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936{
11937#ifdef FEAT_CMDHIST
11938 int histype;
11939 char_u *str;
11940 char_u buf[NUMBUFLEN];
11941#endif
11942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 if (check_restricted() || check_secure())
11945 return;
11946#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011947 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11948 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 if (histype >= 0)
11950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011951 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 if (*str != NUL)
11953 {
11954 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 return;
11957 }
11958 }
11959#endif
11960}
11961
11962/*
11963 * "histdel()" function
11964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011967 typval_T *argvars UNUSED;
11968 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
11970#ifdef FEAT_CMDHIST
11971 int n;
11972 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011973 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011975 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11976 if (str == NULL)
11977 n = 0;
11978 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011981 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011983 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 else
11986 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011987 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 get_tv_string_buf(&argvars[1], buf));
11989 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990#endif
11991}
11992
11993/*
11994 * "histget()" function
11995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011998 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
12001#ifdef FEAT_CMDHIST
12002 int type;
12003 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012004 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012006 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12007 if (str == NULL)
12008 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012010 {
12011 type = get_histtype(str);
12012 if (argvars[1].v_type == VAR_UNKNOWN)
12013 idx = get_history_idx(type);
12014 else
12015 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12016 /* -1 on type error */
12017 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012020 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023}
12024
12025/*
12026 * "histnr()" function
12027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012030 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032{
12033 int i;
12034
12035#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012036 char_u *history = get_tv_string_chk(&argvars[0]);
12037
12038 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 if (i >= HIST_CMD && i < HIST_COUNT)
12040 i = get_history_idx(i);
12041 else
12042#endif
12043 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012044 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045}
12046
12047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 * "highlightID(name)" function
12049 */
12050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056}
12057
12058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012059 * "highlight_exists()" function
12060 */
12061 static void
12062f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012063 typval_T *argvars;
12064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012065{
12066 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12067}
12068
12069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 * "hostname()" function
12071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012074 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076{
12077 char_u hostname[256];
12078
12079 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080 rettv->v_type = VAR_STRING;
12081 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082}
12083
12084/*
12085 * iconv() function
12086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012089 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091{
12092#ifdef FEAT_MBYTE
12093 char_u buf1[NUMBUFLEN];
12094 char_u buf2[NUMBUFLEN];
12095 char_u *from, *to, *str;
12096 vimconv_T vimconv;
12097#endif
12098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099 rettv->v_type = VAR_STRING;
12100 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101
12102#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 str = get_tv_string(&argvars[0]);
12104 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12105 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 vimconv.vc_type = CONV_NONE;
12107 convert_setup(&vimconv, from, to);
12108
12109 /* If the encodings are equal, no conversion needed. */
12110 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012111 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114
12115 convert_setup(&vimconv, NULL, NULL);
12116 vim_free(from);
12117 vim_free(to);
12118#endif
12119}
12120
12121/*
12122 * "indent()" function
12123 */
12124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012126 typval_T *argvars;
12127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128{
12129 linenr_T lnum;
12130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012135 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136}
12137
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012138/*
12139 * "index()" function
12140 */
12141 static void
12142f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012143 typval_T *argvars;
12144 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012145{
Bram Moolenaar33570922005-01-25 22:26:29 +000012146 list_T *l;
12147 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012148 long idx = 0;
12149 int ic = FALSE;
12150
12151 rettv->vval.v_number = -1;
12152 if (argvars[0].v_type != VAR_LIST)
12153 {
12154 EMSG(_(e_listreq));
12155 return;
12156 }
12157 l = argvars[0].vval.v_list;
12158 if (l != NULL)
12159 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012160 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012163 int error = FALSE;
12164
Bram Moolenaar758711c2005-02-02 23:11:38 +000012165 /* Start at specified item. Use the cached index that list_find()
12166 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012167 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012168 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012169 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012170 ic = get_tv_number_chk(&argvars[3], &error);
12171 if (error)
12172 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012173 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012174
Bram Moolenaar758711c2005-02-02 23:11:38 +000012175 for ( ; item != NULL; item = item->li_next, ++idx)
12176 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012177 {
12178 rettv->vval.v_number = idx;
12179 break;
12180 }
12181 }
12182}
12183
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184static int inputsecret_flag = 0;
12185
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012186static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12187
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012189 * This function is used by f_input() and f_inputdialog() functions. The third
12190 * argument to f_input() specifies the type of completion to use at the
12191 * prompt. The third argument to f_inputdialog() specifies the value to return
12192 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 */
12194 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012195get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012196 typval_T *argvars;
12197 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012198 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012200 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 char_u *p = NULL;
12202 int c;
12203 char_u buf[NUMBUFLEN];
12204 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012205 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012206 int xp_type = EXPAND_NOTHING;
12207 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012209 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012210 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211
12212#ifdef NO_CONSOLE_INPUT
12213 /* While starting up, there is no place to enter text. */
12214 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216#endif
12217
12218 cmd_silent = FALSE; /* Want to see the prompt. */
12219 if (prompt != NULL)
12220 {
12221 /* Only the part of the message after the last NL is considered as
12222 * prompt for the command line */
12223 p = vim_strrchr(prompt, '\n');
12224 if (p == NULL)
12225 p = prompt;
12226 else
12227 {
12228 ++p;
12229 c = *p;
12230 *p = NUL;
12231 msg_start();
12232 msg_clr_eos();
12233 msg_puts_attr(prompt, echo_attr);
12234 msg_didout = FALSE;
12235 msg_starthere();
12236 *p = c;
12237 }
12238 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012240 if (argvars[1].v_type != VAR_UNKNOWN)
12241 {
12242 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12243 if (defstr != NULL)
12244 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012246 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012247 {
12248 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012249 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012250 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012251
Bram Moolenaar4463f292005-09-25 22:20:24 +000012252 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012253
Bram Moolenaar4463f292005-09-25 22:20:24 +000012254 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12255 if (xp_name == NULL)
12256 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012257
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012258 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012259
Bram Moolenaar4463f292005-09-25 22:20:24 +000012260 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12261 &xp_arg) == FAIL)
12262 return;
12263 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012264 }
12265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012266 if (defstr != NULL)
12267 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012268 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12269 xp_type, xp_arg);
12270
12271 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012273 /* since the user typed this, no need to wait for return */
12274 need_wait_return = FALSE;
12275 msg_didout = FALSE;
12276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 cmd_silent = cmd_silent_save;
12278}
12279
12280/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012281 * "input()" function
12282 * Also handles inputsecret() when inputsecret is set.
12283 */
12284 static void
12285f_input(argvars, rettv)
12286 typval_T *argvars;
12287 typval_T *rettv;
12288{
12289 get_user_input(argvars, rettv, FALSE);
12290}
12291
12292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 * "inputdialog()" function
12294 */
12295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012297 typval_T *argvars;
12298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299{
12300#if defined(FEAT_GUI_TEXTDIALOG)
12301 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12302 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12303 {
12304 char_u *message;
12305 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012306 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012308 message = get_tv_string_chk(&argvars[0]);
12309 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012310 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012311 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 else
12313 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012314 if (message != NULL && defstr != NULL
12315 && do_dialog(VIM_QUESTION, NULL, message,
12316 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 else
12319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012320 if (message != NULL && defstr != NULL
12321 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012322 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->vval.v_string = vim_strsave(
12324 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012328 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 }
12330 else
12331#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012332 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333}
12334
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012335/*
12336 * "inputlist()" function
12337 */
12338 static void
12339f_inputlist(argvars, rettv)
12340 typval_T *argvars;
12341 typval_T *rettv;
12342{
12343 listitem_T *li;
12344 int selected;
12345 int mouse_used;
12346
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012347#ifdef NO_CONSOLE_INPUT
12348 /* While starting up, there is no place to enter text. */
12349 if (no_console_input())
12350 return;
12351#endif
12352 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12353 {
12354 EMSG2(_(e_listarg), "inputlist()");
12355 return;
12356 }
12357
12358 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012359 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012360 lines_left = Rows; /* avoid more prompt */
12361 msg_scroll = TRUE;
12362 msg_clr_eos();
12363
12364 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12365 {
12366 msg_puts(get_tv_string(&li->li_tv));
12367 msg_putchar('\n');
12368 }
12369
12370 /* Ask for choice. */
12371 selected = prompt_for_number(&mouse_used);
12372 if (mouse_used)
12373 selected -= lines_left;
12374
12375 rettv->vval.v_number = selected;
12376}
12377
12378
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12380
12381/*
12382 * "inputrestore()" function
12383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012385f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012386 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388{
12389 if (ga_userinput.ga_len > 0)
12390 {
12391 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12393 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012394 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 }
12396 else if (p_verbose > 1)
12397 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012398 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 }
12401}
12402
12403/*
12404 * "inputsave()" function
12405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012411 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 if (ga_grow(&ga_userinput, 1) == OK)
12413 {
12414 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12415 + ga_userinput.ga_len);
12416 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012417 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 }
12419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421}
12422
12423/*
12424 * "inputsecret()" function
12425 */
12426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012427f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012428 typval_T *argvars;
12429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430{
12431 ++cmdline_star;
12432 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012433 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 --cmdline_star;
12435 --inputsecret_flag;
12436}
12437
12438/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012439 * "insert()" function
12440 */
12441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012442f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012443 typval_T *argvars;
12444 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012445{
12446 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012447 listitem_T *item;
12448 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012449 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012450
12451 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012452 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012453 else if ((l = argvars[0].vval.v_list) != NULL
12454 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012455 {
12456 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012457 before = get_tv_number_chk(&argvars[2], &error);
12458 if (error)
12459 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012460
Bram Moolenaar758711c2005-02-02 23:11:38 +000012461 if (before == l->lv_len)
12462 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012463 else
12464 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012465 item = list_find(l, before);
12466 if (item == NULL)
12467 {
12468 EMSGN(_(e_listidx), before);
12469 l = NULL;
12470 }
12471 }
12472 if (l != NULL)
12473 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012474 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012475 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012476 }
12477 }
12478}
12479
12480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 * "isdirectory()" function
12482 */
12483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012484f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012485 typval_T *argvars;
12486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489}
12490
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012491/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012492 * "islocked()" function
12493 */
12494 static void
12495f_islocked(argvars, rettv)
12496 typval_T *argvars;
12497 typval_T *rettv;
12498{
12499 lval_T lv;
12500 char_u *end;
12501 dictitem_T *di;
12502
12503 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012504 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12505 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012506 if (end != NULL && lv.ll_name != NULL)
12507 {
12508 if (*end != NUL)
12509 EMSG(_(e_trailing));
12510 else
12511 {
12512 if (lv.ll_tv == NULL)
12513 {
12514 if (check_changedtick(lv.ll_name))
12515 rettv->vval.v_number = 1; /* always locked */
12516 else
12517 {
12518 di = find_var(lv.ll_name, NULL);
12519 if (di != NULL)
12520 {
12521 /* Consider a variable locked when:
12522 * 1. the variable itself is locked
12523 * 2. the value of the variable is locked.
12524 * 3. the List or Dict value is locked.
12525 */
12526 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12527 || tv_islocked(&di->di_tv));
12528 }
12529 }
12530 }
12531 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012532 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012533 else if (lv.ll_newkey != NULL)
12534 EMSG2(_(e_dictkey), lv.ll_newkey);
12535 else if (lv.ll_list != NULL)
12536 /* List item. */
12537 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12538 else
12539 /* Dictionary item. */
12540 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12541 }
12542 }
12543
12544 clear_lval(&lv);
12545}
12546
Bram Moolenaar33570922005-01-25 22:26:29 +000012547static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012548
12549/*
12550 * Turn a dict into a list:
12551 * "what" == 0: list of keys
12552 * "what" == 1: list of values
12553 * "what" == 2: list of items
12554 */
12555 static void
12556dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012557 typval_T *argvars;
12558 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012559 int what;
12560{
Bram Moolenaar33570922005-01-25 22:26:29 +000012561 list_T *l2;
12562 dictitem_T *di;
12563 hashitem_T *hi;
12564 listitem_T *li;
12565 listitem_T *li2;
12566 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012567 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012568
Bram Moolenaar8c711452005-01-14 21:53:12 +000012569 if (argvars[0].v_type != VAR_DICT)
12570 {
12571 EMSG(_(e_dictreq));
12572 return;
12573 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012574 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012575 return;
12576
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012577 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012578 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012579
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012580 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012581 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012585 --todo;
12586 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012587
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012588 li = listitem_alloc();
12589 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012590 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012591 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012592
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012593 if (what == 0)
12594 {
12595 /* keys() */
12596 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012597 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012598 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12599 }
12600 else if (what == 1)
12601 {
12602 /* values() */
12603 copy_tv(&di->di_tv, &li->li_tv);
12604 }
12605 else
12606 {
12607 /* items() */
12608 l2 = list_alloc();
12609 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012610 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012611 li->li_tv.vval.v_list = l2;
12612 if (l2 == NULL)
12613 break;
12614 ++l2->lv_refcount;
12615
12616 li2 = listitem_alloc();
12617 if (li2 == NULL)
12618 break;
12619 list_append(l2, li2);
12620 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012621 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012622 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12623
12624 li2 = listitem_alloc();
12625 if (li2 == NULL)
12626 break;
12627 list_append(l2, li2);
12628 copy_tv(&di->di_tv, &li2->li_tv);
12629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012630 }
12631 }
12632}
12633
12634/*
12635 * "items(dict)" function
12636 */
12637 static void
12638f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 typval_T *argvars;
12640 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012641{
12642 dict_list(argvars, rettv, 2);
12643}
12644
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012646 * "join()" function
12647 */
12648 static void
12649f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012650 typval_T *argvars;
12651 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012652{
12653 garray_T ga;
12654 char_u *sep;
12655
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012656 if (argvars[0].v_type != VAR_LIST)
12657 {
12658 EMSG(_(e_listreq));
12659 return;
12660 }
12661 if (argvars[0].vval.v_list == NULL)
12662 return;
12663 if (argvars[1].v_type == VAR_UNKNOWN)
12664 sep = (char_u *)" ";
12665 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012667
12668 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012669
12670 if (sep != NULL)
12671 {
12672 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012673 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 ga_append(&ga, NUL);
12675 rettv->vval.v_string = (char_u *)ga.ga_data;
12676 }
12677 else
12678 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012679}
12680
12681/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012682 * "keys()" function
12683 */
12684 static void
12685f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012686 typval_T *argvars;
12687 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012688{
12689 dict_list(argvars, rettv, 0);
12690}
12691
12692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 * "last_buffer_nr()" function.
12694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012697 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699{
12700 int n = 0;
12701 buf_T *buf;
12702
12703 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12704 if (n < buf->b_fnum)
12705 n = buf->b_fnum;
12706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012708}
12709
12710/*
12711 * "len()" function
12712 */
12713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012715 typval_T *argvars;
12716 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012717{
12718 switch (argvars[0].v_type)
12719 {
12720 case VAR_STRING:
12721 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722 rettv->vval.v_number = (varnumber_T)STRLEN(
12723 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012724 break;
12725 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012727 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012728 case VAR_DICT:
12729 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12730 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012731 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012732 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012733 break;
12734 }
12735}
12736
Bram Moolenaar33570922005-01-25 22:26:29 +000012737static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012738
12739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012741 typval_T *argvars;
12742 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 int type;
12744{
12745#ifdef FEAT_LIBCALL
12746 char_u *string_in;
12747 char_u **string_result;
12748 int nr_result;
12749#endif
12750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012752 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012754
12755 if (check_restricted() || check_secure())
12756 return;
12757
12758#ifdef FEAT_LIBCALL
12759 /* The first two args must be strings, otherwise its meaningless */
12760 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12761 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012762 string_in = NULL;
12763 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012764 string_in = argvars[2].vval.v_string;
12765 if (type == VAR_NUMBER)
12766 string_result = NULL;
12767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012769 if (mch_libcall(argvars[0].vval.v_string,
12770 argvars[1].vval.v_string,
12771 string_in,
12772 argvars[2].vval.v_number,
12773 string_result,
12774 &nr_result) == OK
12775 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012777 }
12778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779}
12780
12781/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012782 * "libcall()" function
12783 */
12784 static void
12785f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *argvars;
12787 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012788{
12789 libcall_common(argvars, rettv, VAR_STRING);
12790}
12791
12792/*
12793 * "libcallnr()" function
12794 */
12795 static void
12796f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012797 typval_T *argvars;
12798 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012799{
12800 libcall_common(argvars, rettv, VAR_NUMBER);
12801}
12802
12803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 * "line(string)" function
12805 */
12806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012808 typval_T *argvars;
12809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810{
12811 linenr_T lnum = 0;
12812 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012813 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012815 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 if (fp != NULL)
12817 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819}
12820
12821/*
12822 * "line2byte(lnum)" function
12823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012826 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828{
12829#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831#else
12832 linenr_T lnum;
12833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12839 if (rettv->vval.v_number >= 0)
12840 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841#endif
12842}
12843
12844/*
12845 * "lispindent(lnum)" function
12846 */
12847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012849 typval_T *argvars;
12850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851{
12852#ifdef FEAT_LISP
12853 pos_T pos;
12854 linenr_T lnum;
12855
12856 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12859 {
12860 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862 curwin->w_cursor = pos;
12863 }
12864 else
12865#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867}
12868
12869/*
12870 * "localtime()" function
12871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012874 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878}
12879
Bram Moolenaar33570922005-01-25 22:26:29 +000012880static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881
12882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012884 typval_T *argvars;
12885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 int exact;
12887{
12888 char_u *keys;
12889 char_u *which;
12890 char_u buf[NUMBUFLEN];
12891 char_u *keys_buf = NULL;
12892 char_u *rhs;
12893 int mode;
12894 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012895 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896
12897 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 rettv->v_type = VAR_STRING;
12899 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 if (*keys == NUL)
12903 return;
12904
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012905 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012907 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012908 if (argvars[2].v_type != VAR_UNKNOWN)
12909 abbr = get_tv_number(&argvars[2]);
12910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 else
12912 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012913 if (which == NULL)
12914 return;
12915
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 mode = get_map_mode(&which, 0);
12917
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012918 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012919 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920 vim_free(keys_buf);
12921 if (rhs != NULL)
12922 {
12923 ga_init(&ga);
12924 ga.ga_itemsize = 1;
12925 ga.ga_growsize = 40;
12926
12927 while (*rhs != NUL)
12928 ga_concat(&ga, str2special(&rhs, FALSE));
12929
12930 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 }
12933}
12934
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012935#ifdef FEAT_FLOAT
12936/*
12937 * "log10()" function
12938 */
12939 static void
12940f_log10(argvars, rettv)
12941 typval_T *argvars;
12942 typval_T *rettv;
12943{
12944 float_T f;
12945
12946 rettv->v_type = VAR_FLOAT;
12947 if (get_float_arg(argvars, &f) == OK)
12948 rettv->vval.v_float = log10(f);
12949 else
12950 rettv->vval.v_float = 0.0;
12951}
12952#endif
12953
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012955 * "map()" function
12956 */
12957 static void
12958f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012959 typval_T *argvars;
12960 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012961{
12962 filter_map(argvars, rettv, TRUE);
12963}
12964
12965/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012966 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 */
12968 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012969f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012970 typval_T *argvars;
12971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012973 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974}
12975
12976/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012977 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 */
12979 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012980f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012981 typval_T *argvars;
12982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012984 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985}
12986
Bram Moolenaar33570922005-01-25 22:26:29 +000012987static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988
12989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012990find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012991 typval_T *argvars;
12992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 int type;
12994{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012995 char_u *str = NULL;
12996 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 char_u *pat;
12998 regmatch_T regmatch;
12999 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013000 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001 char_u *save_cpo;
13002 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013003 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013004 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013005 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013006 list_T *l = NULL;
13007 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013008 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013009 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010
13011 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13012 save_cpo = p_cpo;
13013 p_cpo = (char_u *)"";
13014
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013015 rettv->vval.v_number = -1;
13016 if (type == 3)
13017 {
13018 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013019 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013020 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013021 }
13022 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->v_type = VAR_STRING;
13025 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013028 if (argvars[0].v_type == VAR_LIST)
13029 {
13030 if ((l = argvars[0].vval.v_list) == NULL)
13031 goto theend;
13032 li = l->lv_first;
13033 }
13034 else
13035 expr = str = get_tv_string(&argvars[0]);
13036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013037 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13038 if (pat == NULL)
13039 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013040
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013041 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 int error = FALSE;
13044
13045 start = get_tv_number_chk(&argvars[2], &error);
13046 if (error)
13047 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013048 if (l != NULL)
13049 {
13050 li = list_find(l, start);
13051 if (li == NULL)
13052 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013053 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013054 }
13055 else
13056 {
13057 if (start < 0)
13058 start = 0;
13059 if (start > (long)STRLEN(str))
13060 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013061 /* When "count" argument is there ignore matches before "start",
13062 * otherwise skip part of the string. Differs when pattern is "^"
13063 * or "\<". */
13064 if (argvars[3].v_type != VAR_UNKNOWN)
13065 startcol = start;
13066 else
13067 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013068 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013069
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013070 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013071 nth = get_tv_number_chk(&argvars[3], &error);
13072 if (error)
13073 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 }
13075
13076 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13077 if (regmatch.regprog != NULL)
13078 {
13079 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013080
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013081 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013082 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013083 if (l != NULL)
13084 {
13085 if (li == NULL)
13086 {
13087 match = FALSE;
13088 break;
13089 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013090 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013091 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013092 if (str == NULL)
13093 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013094 }
13095
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013096 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013097
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013098 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013099 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013100 if (l == NULL && !match)
13101 break;
13102
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013103 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013104 if (l != NULL)
13105 {
13106 li = li->li_next;
13107 ++idx;
13108 }
13109 else
13110 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013111#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013112 startcol = (colnr_T)(regmatch.startp[0]
13113 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013114#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013115 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013116#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013117 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013118 }
13119
13120 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013122 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013123 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013124 int i;
13125
13126 /* return list with matched string and submatches */
13127 for (i = 0; i < NSUBEXP; ++i)
13128 {
13129 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013130 {
13131 if (list_append_string(rettv->vval.v_list,
13132 (char_u *)"", 0) == FAIL)
13133 break;
13134 }
13135 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013136 regmatch.startp[i],
13137 (int)(regmatch.endp[i] - regmatch.startp[i]))
13138 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013139 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013140 }
13141 }
13142 else if (type == 2)
13143 {
13144 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013145 if (l != NULL)
13146 copy_tv(&li->li_tv, rettv);
13147 else
13148 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013150 }
13151 else if (l != NULL)
13152 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 else
13154 {
13155 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 (varnumber_T)(regmatch.startp[0] - str);
13158 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013161 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 }
13163 }
13164 vim_free(regmatch.regprog);
13165 }
13166
13167theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013168 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 p_cpo = save_cpo;
13170}
13171
13172/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013173 * "match()" function
13174 */
13175 static void
13176f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *argvars;
13178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013179{
13180 find_some_match(argvars, rettv, 1);
13181}
13182
13183/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013184 * "matchadd()" function
13185 */
13186 static void
13187f_matchadd(argvars, rettv)
13188 typval_T *argvars;
13189 typval_T *rettv;
13190{
13191#ifdef FEAT_SEARCH_EXTRA
13192 char_u buf[NUMBUFLEN];
13193 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13194 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13195 int prio = 10; /* default priority */
13196 int id = -1;
13197 int error = FALSE;
13198
13199 rettv->vval.v_number = -1;
13200
13201 if (grp == NULL || pat == NULL)
13202 return;
13203 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013204 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013205 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013206 if (argvars[3].v_type != VAR_UNKNOWN)
13207 id = get_tv_number_chk(&argvars[3], &error);
13208 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013209 if (error == TRUE)
13210 return;
13211 if (id >= 1 && id <= 3)
13212 {
13213 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13214 return;
13215 }
13216
13217 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13218#endif
13219}
13220
13221/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013222 * "matcharg()" function
13223 */
13224 static void
13225f_matcharg(argvars, rettv)
13226 typval_T *argvars;
13227 typval_T *rettv;
13228{
13229 if (rettv_list_alloc(rettv) == OK)
13230 {
13231#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013232 int id = get_tv_number(&argvars[0]);
13233 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013234
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013235 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013236 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013237 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13238 {
13239 list_append_string(rettv->vval.v_list,
13240 syn_id2name(m->hlg_id), -1);
13241 list_append_string(rettv->vval.v_list, m->pattern, -1);
13242 }
13243 else
13244 {
13245 list_append_string(rettv->vval.v_list, NUL, -1);
13246 list_append_string(rettv->vval.v_list, NUL, -1);
13247 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013248 }
13249#endif
13250 }
13251}
13252
13253/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013254 * "matchdelete()" function
13255 */
13256 static void
13257f_matchdelete(argvars, rettv)
13258 typval_T *argvars;
13259 typval_T *rettv;
13260{
13261#ifdef FEAT_SEARCH_EXTRA
13262 rettv->vval.v_number = match_delete(curwin,
13263 (int)get_tv_number(&argvars[0]), TRUE);
13264#endif
13265}
13266
13267/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013268 * "matchend()" function
13269 */
13270 static void
13271f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013272 typval_T *argvars;
13273 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013274{
13275 find_some_match(argvars, rettv, 0);
13276}
13277
13278/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013279 * "matchlist()" function
13280 */
13281 static void
13282f_matchlist(argvars, rettv)
13283 typval_T *argvars;
13284 typval_T *rettv;
13285{
13286 find_some_match(argvars, rettv, 3);
13287}
13288
13289/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013290 * "matchstr()" function
13291 */
13292 static void
13293f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013294 typval_T *argvars;
13295 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013296{
13297 find_some_match(argvars, rettv, 2);
13298}
13299
Bram Moolenaar33570922005-01-25 22:26:29 +000013300static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013301
13302 static void
13303max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013304 typval_T *argvars;
13305 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013306 int domax;
13307{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013308 long n = 0;
13309 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013310 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013311
13312 if (argvars[0].v_type == VAR_LIST)
13313 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013314 list_T *l;
13315 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013316
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013317 l = argvars[0].vval.v_list;
13318 if (l != NULL)
13319 {
13320 li = l->lv_first;
13321 if (li != NULL)
13322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013323 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013324 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013325 {
13326 li = li->li_next;
13327 if (li == NULL)
13328 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013329 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013330 if (domax ? i > n : i < n)
13331 n = i;
13332 }
13333 }
13334 }
13335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013336 else if (argvars[0].v_type == VAR_DICT)
13337 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013338 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013339 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013340 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013341 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013342
13343 d = argvars[0].vval.v_dict;
13344 if (d != NULL)
13345 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013346 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013348 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013349 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013350 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013351 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013352 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013353 if (first)
13354 {
13355 n = i;
13356 first = FALSE;
13357 }
13358 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013359 n = i;
13360 }
13361 }
13362 }
13363 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013364 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013365 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013366 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013367}
13368
13369/*
13370 * "max()" function
13371 */
13372 static void
13373f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013374 typval_T *argvars;
13375 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013376{
13377 max_min(argvars, rettv, TRUE);
13378}
13379
13380/*
13381 * "min()" function
13382 */
13383 static void
13384f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013385 typval_T *argvars;
13386 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013387{
13388 max_min(argvars, rettv, FALSE);
13389}
13390
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013391static int mkdir_recurse __ARGS((char_u *dir, int prot));
13392
13393/*
13394 * Create the directory in which "dir" is located, and higher levels when
13395 * needed.
13396 */
13397 static int
13398mkdir_recurse(dir, prot)
13399 char_u *dir;
13400 int prot;
13401{
13402 char_u *p;
13403 char_u *updir;
13404 int r = FAIL;
13405
13406 /* Get end of directory name in "dir".
13407 * We're done when it's "/" or "c:/". */
13408 p = gettail_sep(dir);
13409 if (p <= get_past_head(dir))
13410 return OK;
13411
13412 /* If the directory exists we're done. Otherwise: create it.*/
13413 updir = vim_strnsave(dir, (int)(p - dir));
13414 if (updir == NULL)
13415 return FAIL;
13416 if (mch_isdir(updir))
13417 r = OK;
13418 else if (mkdir_recurse(updir, prot) == OK)
13419 r = vim_mkdir_emsg(updir, prot);
13420 vim_free(updir);
13421 return r;
13422}
13423
13424#ifdef vim_mkdir
13425/*
13426 * "mkdir()" function
13427 */
13428 static void
13429f_mkdir(argvars, rettv)
13430 typval_T *argvars;
13431 typval_T *rettv;
13432{
13433 char_u *dir;
13434 char_u buf[NUMBUFLEN];
13435 int prot = 0755;
13436
13437 rettv->vval.v_number = FAIL;
13438 if (check_restricted() || check_secure())
13439 return;
13440
13441 dir = get_tv_string_buf(&argvars[0], buf);
13442 if (argvars[1].v_type != VAR_UNKNOWN)
13443 {
13444 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013445 prot = get_tv_number_chk(&argvars[2], NULL);
13446 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013447 mkdir_recurse(dir, prot);
13448 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013449 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013450}
13451#endif
13452
Bram Moolenaar0d660222005-01-07 21:51:51 +000013453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 * "mode()" function
13455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013458 typval_T *argvars;
13459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013461 char_u buf[3];
13462
13463 buf[1] = NUL;
13464 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465
13466#ifdef FEAT_VISUAL
13467 if (VIsual_active)
13468 {
13469 if (VIsual_select)
13470 buf[0] = VIsual_mode + 's' - 'v';
13471 else
13472 buf[0] = VIsual_mode;
13473 }
13474 else
13475#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013476 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13477 || State == CONFIRM)
13478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013480 if (State == ASKMORE)
13481 buf[1] = 'm';
13482 else if (State == CONFIRM)
13483 buf[1] = '?';
13484 }
13485 else if (State == EXTERNCMD)
13486 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487 else if (State & INSERT)
13488 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013489#ifdef FEAT_VREPLACE
13490 if (State & VREPLACE_FLAG)
13491 {
13492 buf[0] = 'R';
13493 buf[1] = 'v';
13494 }
13495 else
13496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 if (State & REPLACE_FLAG)
13498 buf[0] = 'R';
13499 else
13500 buf[0] = 'i';
13501 }
13502 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013505 if (exmode_active)
13506 buf[1] = 'v';
13507 }
13508 else if (exmode_active)
13509 {
13510 buf[0] = 'c';
13511 buf[1] = 'e';
13512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013516 if (finish_op)
13517 buf[1] = 'o';
13518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013520 /* Clear out the minor mode when the argument is not a non-zero number or
13521 * non-empty string. */
13522 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013523 buf[1] = NUL;
13524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013525 rettv->vval.v_string = vim_strsave(buf);
13526 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527}
13528
13529/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013530 * "nextnonblank()" function
13531 */
13532 static void
13533f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 typval_T *argvars;
13535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536{
13537 linenr_T lnum;
13538
13539 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013541 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013542 {
13543 lnum = 0;
13544 break;
13545 }
13546 if (*skipwhite(ml_get(lnum)) != NUL)
13547 break;
13548 }
13549 rettv->vval.v_number = lnum;
13550}
13551
13552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 * "nr2char()" function
13554 */
13555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013556f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013557 typval_T *argvars;
13558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559{
13560 char_u buf[NUMBUFLEN];
13561
13562#ifdef FEAT_MBYTE
13563 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013564 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 else
13566#endif
13567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 buf[1] = NUL;
13570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->v_type = VAR_STRING;
13572 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013573}
13574
13575/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013576 * "pathshorten()" function
13577 */
13578 static void
13579f_pathshorten(argvars, rettv)
13580 typval_T *argvars;
13581 typval_T *rettv;
13582{
13583 char_u *p;
13584
13585 rettv->v_type = VAR_STRING;
13586 p = get_tv_string_chk(&argvars[0]);
13587 if (p == NULL)
13588 rettv->vval.v_string = NULL;
13589 else
13590 {
13591 p = vim_strsave(p);
13592 rettv->vval.v_string = p;
13593 if (p != NULL)
13594 shorten_dir(p);
13595 }
13596}
13597
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013598#ifdef FEAT_FLOAT
13599/*
13600 * "pow()" function
13601 */
13602 static void
13603f_pow(argvars, rettv)
13604 typval_T *argvars;
13605 typval_T *rettv;
13606{
13607 float_T fx, fy;
13608
13609 rettv->v_type = VAR_FLOAT;
13610 if (get_float_arg(argvars, &fx) == OK
13611 && get_float_arg(&argvars[1], &fy) == OK)
13612 rettv->vval.v_float = pow(fx, fy);
13613 else
13614 rettv->vval.v_float = 0.0;
13615}
13616#endif
13617
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013618/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013619 * "prevnonblank()" function
13620 */
13621 static void
13622f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013623 typval_T *argvars;
13624 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625{
13626 linenr_T lnum;
13627
13628 lnum = get_tv_lnum(argvars);
13629 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13630 lnum = 0;
13631 else
13632 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13633 --lnum;
13634 rettv->vval.v_number = lnum;
13635}
13636
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013637#ifdef HAVE_STDARG_H
13638/* This dummy va_list is here because:
13639 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13640 * - locally in the function results in a "used before set" warning
13641 * - using va_start() to initialize it gives "function with fixed args" error */
13642static va_list ap;
13643#endif
13644
Bram Moolenaar8c711452005-01-14 21:53:12 +000013645/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013646 * "printf()" function
13647 */
13648 static void
13649f_printf(argvars, rettv)
13650 typval_T *argvars;
13651 typval_T *rettv;
13652{
13653 rettv->v_type = VAR_STRING;
13654 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013655#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013656 {
13657 char_u buf[NUMBUFLEN];
13658 int len;
13659 char_u *s;
13660 int saved_did_emsg = did_emsg;
13661 char *fmt;
13662
13663 /* Get the required length, allocate the buffer and do it for real. */
13664 did_emsg = FALSE;
13665 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013666 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013667 if (!did_emsg)
13668 {
13669 s = alloc(len + 1);
13670 if (s != NULL)
13671 {
13672 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013673 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013674 }
13675 }
13676 did_emsg |= saved_did_emsg;
13677 }
13678#endif
13679}
13680
13681/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013682 * "pumvisible()" function
13683 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013684 static void
13685f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013686 typval_T *argvars UNUSED;
13687 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013688{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013689#ifdef FEAT_INS_EXPAND
13690 if (pum_visible())
13691 rettv->vval.v_number = 1;
13692#endif
13693}
13694
13695/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013696 * "range()" function
13697 */
13698 static void
13699f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013700 typval_T *argvars;
13701 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013702{
13703 long start;
13704 long end;
13705 long stride = 1;
13706 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013709 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013710 if (argvars[1].v_type == VAR_UNKNOWN)
13711 {
13712 end = start - 1;
13713 start = 0;
13714 }
13715 else
13716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013718 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013720 }
13721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013722 if (error)
13723 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013724 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013725 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013726 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013727 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013728 else
13729 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013730 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013731 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013732 if (list_append_number(rettv->vval.v_list,
13733 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013734 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013735 }
13736}
13737
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013738/*
13739 * "readfile()" function
13740 */
13741 static void
13742f_readfile(argvars, rettv)
13743 typval_T *argvars;
13744 typval_T *rettv;
13745{
13746 int binary = FALSE;
13747 char_u *fname;
13748 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013749 listitem_T *li;
13750#define FREAD_SIZE 200 /* optimized for text lines */
13751 char_u buf[FREAD_SIZE];
13752 int readlen; /* size of last fread() */
13753 int buflen; /* nr of valid chars in buf[] */
13754 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13755 int tolist; /* first byte in buf[] still to be put in list */
13756 int chop; /* how many CR to chop off */
13757 char_u *prev = NULL; /* previously read bytes, if any */
13758 int prevlen = 0; /* length of "prev" if not NULL */
13759 char_u *s;
13760 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013761 long maxline = MAXLNUM;
13762 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013763
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013764 if (argvars[1].v_type != VAR_UNKNOWN)
13765 {
13766 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13767 binary = TRUE;
13768 if (argvars[2].v_type != VAR_UNKNOWN)
13769 maxline = get_tv_number(&argvars[2]);
13770 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013771
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013772 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013773 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013774
13775 /* Always open the file in binary mode, library functions have a mind of
13776 * their own about CR-LF conversion. */
13777 fname = get_tv_string(&argvars[0]);
13778 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13779 {
13780 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13781 return;
13782 }
13783
13784 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013785 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013786 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013787 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013788 buflen = filtd + readlen;
13789 tolist = 0;
13790 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13791 {
13792 if (buf[filtd] == '\n' || readlen <= 0)
13793 {
13794 /* Only when in binary mode add an empty list item when the
13795 * last line ends in a '\n'. */
13796 if (!binary && readlen == 0 && filtd == 0)
13797 break;
13798
13799 /* Found end-of-line or end-of-file: add a text line to the
13800 * list. */
13801 chop = 0;
13802 if (!binary)
13803 while (filtd - chop - 1 >= tolist
13804 && buf[filtd - chop - 1] == '\r')
13805 ++chop;
13806 len = filtd - tolist - chop;
13807 if (prev == NULL)
13808 s = vim_strnsave(buf + tolist, len);
13809 else
13810 {
13811 s = alloc((unsigned)(prevlen + len + 1));
13812 if (s != NULL)
13813 {
13814 mch_memmove(s, prev, prevlen);
13815 vim_free(prev);
13816 prev = NULL;
13817 mch_memmove(s + prevlen, buf + tolist, len);
13818 s[prevlen + len] = NUL;
13819 }
13820 }
13821 tolist = filtd + 1;
13822
13823 li = listitem_alloc();
13824 if (li == NULL)
13825 {
13826 vim_free(s);
13827 break;
13828 }
13829 li->li_tv.v_type = VAR_STRING;
13830 li->li_tv.v_lock = 0;
13831 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013832 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013833
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013834 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013835 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013836 if (readlen <= 0)
13837 break;
13838 }
13839 else if (buf[filtd] == NUL)
13840 buf[filtd] = '\n';
13841 }
13842 if (readlen <= 0)
13843 break;
13844
13845 if (tolist == 0)
13846 {
13847 /* "buf" is full, need to move text to an allocated buffer */
13848 if (prev == NULL)
13849 {
13850 prev = vim_strnsave(buf, buflen);
13851 prevlen = buflen;
13852 }
13853 else
13854 {
13855 s = alloc((unsigned)(prevlen + buflen));
13856 if (s != NULL)
13857 {
13858 mch_memmove(s, prev, prevlen);
13859 mch_memmove(s + prevlen, buf, buflen);
13860 vim_free(prev);
13861 prev = s;
13862 prevlen += buflen;
13863 }
13864 }
13865 filtd = 0;
13866 }
13867 else
13868 {
13869 mch_memmove(buf, buf + tolist, buflen - tolist);
13870 filtd -= tolist;
13871 }
13872 }
13873
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013874 /*
13875 * For a negative line count use only the lines at the end of the file,
13876 * free the rest.
13877 */
13878 if (maxline < 0)
13879 while (cnt > -maxline)
13880 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013881 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013882 --cnt;
13883 }
13884
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013885 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013886 fclose(fd);
13887}
13888
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013889#if defined(FEAT_RELTIME)
13890static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13891
13892/*
13893 * Convert a List to proftime_T.
13894 * Return FAIL when there is something wrong.
13895 */
13896 static int
13897list2proftime(arg, tm)
13898 typval_T *arg;
13899 proftime_T *tm;
13900{
13901 long n1, n2;
13902 int error = FALSE;
13903
13904 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13905 || arg->vval.v_list->lv_len != 2)
13906 return FAIL;
13907 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13908 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13909# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013910 tm->HighPart = n1;
13911 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013912# else
13913 tm->tv_sec = n1;
13914 tm->tv_usec = n2;
13915# endif
13916 return error ? FAIL : OK;
13917}
13918#endif /* FEAT_RELTIME */
13919
13920/*
13921 * "reltime()" function
13922 */
13923 static void
13924f_reltime(argvars, rettv)
13925 typval_T *argvars;
13926 typval_T *rettv;
13927{
13928#ifdef FEAT_RELTIME
13929 proftime_T res;
13930 proftime_T start;
13931
13932 if (argvars[0].v_type == VAR_UNKNOWN)
13933 {
13934 /* No arguments: get current time. */
13935 profile_start(&res);
13936 }
13937 else if (argvars[1].v_type == VAR_UNKNOWN)
13938 {
13939 if (list2proftime(&argvars[0], &res) == FAIL)
13940 return;
13941 profile_end(&res);
13942 }
13943 else
13944 {
13945 /* Two arguments: compute the difference. */
13946 if (list2proftime(&argvars[0], &start) == FAIL
13947 || list2proftime(&argvars[1], &res) == FAIL)
13948 return;
13949 profile_sub(&res, &start);
13950 }
13951
13952 if (rettv_list_alloc(rettv) == OK)
13953 {
13954 long n1, n2;
13955
13956# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013957 n1 = res.HighPart;
13958 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013959# else
13960 n1 = res.tv_sec;
13961 n2 = res.tv_usec;
13962# endif
13963 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13964 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13965 }
13966#endif
13967}
13968
13969/*
13970 * "reltimestr()" function
13971 */
13972 static void
13973f_reltimestr(argvars, rettv)
13974 typval_T *argvars;
13975 typval_T *rettv;
13976{
13977#ifdef FEAT_RELTIME
13978 proftime_T tm;
13979#endif
13980
13981 rettv->v_type = VAR_STRING;
13982 rettv->vval.v_string = NULL;
13983#ifdef FEAT_RELTIME
13984 if (list2proftime(&argvars[0], &tm) == OK)
13985 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13986#endif
13987}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013988
Bram Moolenaar0d660222005-01-07 21:51:51 +000013989#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13990static void make_connection __ARGS((void));
13991static int check_connection __ARGS((void));
13992
13993 static void
13994make_connection()
13995{
13996 if (X_DISPLAY == NULL
13997# ifdef FEAT_GUI
13998 && !gui.in_use
13999# endif
14000 )
14001 {
14002 x_force_connect = TRUE;
14003 setup_term_clip();
14004 x_force_connect = FALSE;
14005 }
14006}
14007
14008 static int
14009check_connection()
14010{
14011 make_connection();
14012 if (X_DISPLAY == NULL)
14013 {
14014 EMSG(_("E240: No connection to Vim server"));
14015 return FAIL;
14016 }
14017 return OK;
14018}
14019#endif
14020
14021#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014022static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014023
14024 static void
14025remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014026 typval_T *argvars;
14027 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014028 int expr;
14029{
14030 char_u *server_name;
14031 char_u *keys;
14032 char_u *r = NULL;
14033 char_u buf[NUMBUFLEN];
14034# ifdef WIN32
14035 HWND w;
14036# else
14037 Window w;
14038# endif
14039
14040 if (check_restricted() || check_secure())
14041 return;
14042
14043# ifdef FEAT_X11
14044 if (check_connection() == FAIL)
14045 return;
14046# endif
14047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014048 server_name = get_tv_string_chk(&argvars[0]);
14049 if (server_name == NULL)
14050 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014051 keys = get_tv_string_buf(&argvars[1], buf);
14052# ifdef WIN32
14053 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14054# else
14055 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14056 < 0)
14057# endif
14058 {
14059 if (r != NULL)
14060 EMSG(r); /* sending worked but evaluation failed */
14061 else
14062 EMSG2(_("E241: Unable to send to %s"), server_name);
14063 return;
14064 }
14065
14066 rettv->vval.v_string = r;
14067
14068 if (argvars[2].v_type != VAR_UNKNOWN)
14069 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014070 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014071 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014072 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014073
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014074 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014075 v.di_tv.v_type = VAR_STRING;
14076 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077 idvar = get_tv_string_chk(&argvars[2]);
14078 if (idvar != NULL)
14079 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014080 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014081 }
14082}
14083#endif
14084
14085/*
14086 * "remote_expr()" function
14087 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014088 static void
14089f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014091 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014092{
14093 rettv->v_type = VAR_STRING;
14094 rettv->vval.v_string = NULL;
14095#ifdef FEAT_CLIENTSERVER
14096 remote_common(argvars, rettv, TRUE);
14097#endif
14098}
14099
14100/*
14101 * "remote_foreground()" function
14102 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014103 static void
14104f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014105 typval_T *argvars UNUSED;
14106 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014107{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108#ifdef FEAT_CLIENTSERVER
14109# ifdef WIN32
14110 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014111 {
14112 char_u *server_name = get_tv_string_chk(&argvars[0]);
14113
14114 if (server_name != NULL)
14115 serverForeground(server_name);
14116 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014117# else
14118 /* Send a foreground() expression to the server. */
14119 argvars[1].v_type = VAR_STRING;
14120 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14121 argvars[2].v_type = VAR_UNKNOWN;
14122 remote_common(argvars, rettv, TRUE);
14123 vim_free(argvars[1].vval.v_string);
14124# endif
14125#endif
14126}
14127
Bram Moolenaar0d660222005-01-07 21:51:51 +000014128 static void
14129f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014130 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014131 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014132{
14133#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014135 char_u *s = NULL;
14136# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014137 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014138# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014139 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014140
14141 if (check_restricted() || check_secure())
14142 {
14143 rettv->vval.v_number = -1;
14144 return;
14145 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 serverid = get_tv_string_chk(&argvars[0]);
14147 if (serverid == NULL)
14148 {
14149 rettv->vval.v_number = -1;
14150 return; /* type error; errmsg already given */
14151 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014152# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014153 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014154 if (n == 0)
14155 rettv->vval.v_number = -1;
14156 else
14157 {
14158 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14159 rettv->vval.v_number = (s != NULL);
14160 }
14161# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162 if (check_connection() == FAIL)
14163 return;
14164
14165 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014167# endif
14168
14169 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 char_u *retvar;
14172
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 v.di_tv.v_type = VAR_STRING;
14174 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 retvar = get_tv_string_chk(&argvars[1]);
14176 if (retvar != NULL)
14177 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014178 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014179 }
14180#else
14181 rettv->vval.v_number = -1;
14182#endif
14183}
14184
Bram Moolenaar0d660222005-01-07 21:51:51 +000014185 static void
14186f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014187 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014188 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014189{
14190 char_u *r = NULL;
14191
14192#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 char_u *serverid = get_tv_string_chk(&argvars[0]);
14194
14195 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014196 {
14197# ifdef WIN32
14198 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014199 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014200
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014201 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014202 if (n != 0)
14203 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14204 if (r == NULL)
14205# else
14206 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014208# endif
14209 EMSG(_("E277: Unable to read a server reply"));
14210 }
14211#endif
14212 rettv->v_type = VAR_STRING;
14213 rettv->vval.v_string = r;
14214}
14215
14216/*
14217 * "remote_send()" function
14218 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014219 static void
14220f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014221 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014222 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014223{
14224 rettv->v_type = VAR_STRING;
14225 rettv->vval.v_string = NULL;
14226#ifdef FEAT_CLIENTSERVER
14227 remote_common(argvars, rettv, FALSE);
14228#endif
14229}
14230
14231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014232 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014233 */
14234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014235f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014236 typval_T *argvars;
14237 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014238{
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 list_T *l;
14240 listitem_T *item, *item2;
14241 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014242 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014243 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014244 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014245 dict_T *d;
14246 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014247
Bram Moolenaar8c711452005-01-14 21:53:12 +000014248 if (argvars[0].v_type == VAR_DICT)
14249 {
14250 if (argvars[2].v_type != VAR_UNKNOWN)
14251 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014252 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014253 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 key = get_tv_string_chk(&argvars[1]);
14256 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014258 di = dict_find(d, key, -1);
14259 if (di == NULL)
14260 EMSG2(_(e_dictkey), key);
14261 else
14262 {
14263 *rettv = di->di_tv;
14264 init_tv(&di->di_tv);
14265 dictitem_remove(d, di);
14266 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014268 }
14269 }
14270 else if (argvars[0].v_type != VAR_LIST)
14271 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014272 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014273 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014275 int error = FALSE;
14276
14277 idx = get_tv_number_chk(&argvars[1], &error);
14278 if (error)
14279 ; /* type error: do nothing, errmsg already given */
14280 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014281 EMSGN(_(e_listidx), idx);
14282 else
14283 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014284 if (argvars[2].v_type == VAR_UNKNOWN)
14285 {
14286 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014287 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014288 *rettv = item->li_tv;
14289 vim_free(item);
14290 }
14291 else
14292 {
14293 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 end = get_tv_number_chk(&argvars[2], &error);
14295 if (error)
14296 ; /* type error: do nothing */
14297 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014298 EMSGN(_(e_listidx), end);
14299 else
14300 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014301 int cnt = 0;
14302
14303 for (li = item; li != NULL; li = li->li_next)
14304 {
14305 ++cnt;
14306 if (li == item2)
14307 break;
14308 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014309 if (li == NULL) /* didn't find "item2" after "item" */
14310 EMSG(_(e_invrange));
14311 else
14312 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014313 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014314 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014315 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014316 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014317 l->lv_first = item;
14318 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014319 item->li_prev = NULL;
14320 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014321 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014322 }
14323 }
14324 }
14325 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014326 }
14327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328}
14329
14330/*
14331 * "rename({from}, {to})" function
14332 */
14333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014334f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014335 typval_T *argvars;
14336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337{
14338 char_u buf[NUMBUFLEN];
14339
14340 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14344 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345}
14346
14347/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014348 * "repeat()" function
14349 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014350 static void
14351f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014352 typval_T *argvars;
14353 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014354{
14355 char_u *p;
14356 int n;
14357 int slen;
14358 int len;
14359 char_u *r;
14360 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014361
14362 n = get_tv_number(&argvars[1]);
14363 if (argvars[0].v_type == VAR_LIST)
14364 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014365 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014366 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014367 if (list_extend(rettv->vval.v_list,
14368 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014369 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014370 }
14371 else
14372 {
14373 p = get_tv_string(&argvars[0]);
14374 rettv->v_type = VAR_STRING;
14375 rettv->vval.v_string = NULL;
14376
14377 slen = (int)STRLEN(p);
14378 len = slen * n;
14379 if (len <= 0)
14380 return;
14381
14382 r = alloc(len + 1);
14383 if (r != NULL)
14384 {
14385 for (i = 0; i < n; i++)
14386 mch_memmove(r + i * slen, p, (size_t)slen);
14387 r[len] = NUL;
14388 }
14389
14390 rettv->vval.v_string = r;
14391 }
14392}
14393
14394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 * "resolve()" function
14396 */
14397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014399 typval_T *argvars;
14400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401{
14402 char_u *p;
14403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405#ifdef FEAT_SHORTCUT
14406 {
14407 char_u *v = NULL;
14408
14409 v = mch_resolve_shortcut(p);
14410 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014413 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 }
14415#else
14416# ifdef HAVE_READLINK
14417 {
14418 char_u buf[MAXPATHL + 1];
14419 char_u *cpy;
14420 int len;
14421 char_u *remain = NULL;
14422 char_u *q;
14423 int is_relative_to_current = FALSE;
14424 int has_trailing_pathsep = FALSE;
14425 int limit = 100;
14426
14427 p = vim_strsave(p);
14428
14429 if (p[0] == '.' && (vim_ispathsep(p[1])
14430 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14431 is_relative_to_current = TRUE;
14432
14433 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014434 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 has_trailing_pathsep = TRUE;
14436
14437 q = getnextcomp(p);
14438 if (*q != NUL)
14439 {
14440 /* Separate the first path component in "p", and keep the
14441 * remainder (beginning with the path separator). */
14442 remain = vim_strsave(q - 1);
14443 q[-1] = NUL;
14444 }
14445
14446 for (;;)
14447 {
14448 for (;;)
14449 {
14450 len = readlink((char *)p, (char *)buf, MAXPATHL);
14451 if (len <= 0)
14452 break;
14453 buf[len] = NUL;
14454
14455 if (limit-- == 0)
14456 {
14457 vim_free(p);
14458 vim_free(remain);
14459 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 goto fail;
14462 }
14463
14464 /* Ensure that the result will have a trailing path separator
14465 * if the argument has one. */
14466 if (remain == NULL && has_trailing_pathsep)
14467 add_pathsep(buf);
14468
14469 /* Separate the first path component in the link value and
14470 * concatenate the remainders. */
14471 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14472 if (*q != NUL)
14473 {
14474 if (remain == NULL)
14475 remain = vim_strsave(q - 1);
14476 else
14477 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014478 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 if (cpy != NULL)
14480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 vim_free(remain);
14482 remain = cpy;
14483 }
14484 }
14485 q[-1] = NUL;
14486 }
14487
14488 q = gettail(p);
14489 if (q > p && *q == NUL)
14490 {
14491 /* Ignore trailing path separator. */
14492 q[-1] = NUL;
14493 q = gettail(p);
14494 }
14495 if (q > p && !mch_isFullName(buf))
14496 {
14497 /* symlink is relative to directory of argument */
14498 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14499 if (cpy != NULL)
14500 {
14501 STRCPY(cpy, p);
14502 STRCPY(gettail(cpy), buf);
14503 vim_free(p);
14504 p = cpy;
14505 }
14506 }
14507 else
14508 {
14509 vim_free(p);
14510 p = vim_strsave(buf);
14511 }
14512 }
14513
14514 if (remain == NULL)
14515 break;
14516
14517 /* Append the first path component of "remain" to "p". */
14518 q = getnextcomp(remain + 1);
14519 len = q - remain - (*q != NUL);
14520 cpy = vim_strnsave(p, STRLEN(p) + len);
14521 if (cpy != NULL)
14522 {
14523 STRNCAT(cpy, remain, len);
14524 vim_free(p);
14525 p = cpy;
14526 }
14527 /* Shorten "remain". */
14528 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014529 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 else
14531 {
14532 vim_free(remain);
14533 remain = NULL;
14534 }
14535 }
14536
14537 /* If the result is a relative path name, make it explicitly relative to
14538 * the current directory if and only if the argument had this form. */
14539 if (!vim_ispathsep(*p))
14540 {
14541 if (is_relative_to_current
14542 && *p != NUL
14543 && !(p[0] == '.'
14544 && (p[1] == NUL
14545 || vim_ispathsep(p[1])
14546 || (p[1] == '.'
14547 && (p[2] == NUL
14548 || vim_ispathsep(p[2]))))))
14549 {
14550 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014551 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 if (cpy != NULL)
14553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 vim_free(p);
14555 p = cpy;
14556 }
14557 }
14558 else if (!is_relative_to_current)
14559 {
14560 /* Strip leading "./". */
14561 q = p;
14562 while (q[0] == '.' && vim_ispathsep(q[1]))
14563 q += 2;
14564 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014565 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 }
14567 }
14568
14569 /* Ensure that the result will have no trailing path separator
14570 * if the argument had none. But keep "/" or "//". */
14571 if (!has_trailing_pathsep)
14572 {
14573 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014574 if (after_pathsep(p, q))
14575 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 }
14577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014578 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 }
14580# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582# endif
14583#endif
14584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586
14587#ifdef HAVE_READLINK
14588fail:
14589#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014590 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591}
14592
14593/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014594 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 */
14596 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014598 typval_T *argvars;
14599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600{
Bram Moolenaar33570922005-01-25 22:26:29 +000014601 list_T *l;
14602 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604 if (argvars[0].v_type != VAR_LIST)
14605 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014606 else if ((l = argvars[0].vval.v_list) != NULL
14607 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014608 {
14609 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014610 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014611 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014612 while (li != NULL)
14613 {
14614 ni = li->li_prev;
14615 list_append(l, li);
14616 li = ni;
14617 }
14618 rettv->vval.v_list = l;
14619 rettv->v_type = VAR_LIST;
14620 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014621 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623}
14624
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014625#define SP_NOMOVE 0x01 /* don't move cursor */
14626#define SP_REPEAT 0x02 /* repeat to find outer pair */
14627#define SP_RETCOUNT 0x04 /* return matchcount */
14628#define SP_SETPCMARK 0x08 /* set previous context mark */
14629#define SP_START 0x10 /* accept match at start position */
14630#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14631#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014632
Bram Moolenaar33570922005-01-25 22:26:29 +000014633static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634
14635/*
14636 * Get flags for a search function.
14637 * Possibly sets "p_ws".
14638 * Returns BACKWARD, FORWARD or zero (for an error).
14639 */
14640 static int
14641get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643 int *flagsp;
14644{
14645 int dir = FORWARD;
14646 char_u *flags;
14647 char_u nbuf[NUMBUFLEN];
14648 int mask;
14649
14650 if (varp->v_type != VAR_UNKNOWN)
14651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014652 flags = get_tv_string_buf_chk(varp, nbuf);
14653 if (flags == NULL)
14654 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655 while (*flags != NUL)
14656 {
14657 switch (*flags)
14658 {
14659 case 'b': dir = BACKWARD; break;
14660 case 'w': p_ws = TRUE; break;
14661 case 'W': p_ws = FALSE; break;
14662 default: mask = 0;
14663 if (flagsp != NULL)
14664 switch (*flags)
14665 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014666 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014667 case 'e': mask = SP_END; break;
14668 case 'm': mask = SP_RETCOUNT; break;
14669 case 'n': mask = SP_NOMOVE; break;
14670 case 'p': mask = SP_SUBPAT; break;
14671 case 'r': mask = SP_REPEAT; break;
14672 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014673 }
14674 if (mask == 0)
14675 {
14676 EMSG2(_(e_invarg2), flags);
14677 dir = 0;
14678 }
14679 else
14680 *flagsp |= mask;
14681 }
14682 if (dir == 0)
14683 break;
14684 ++flags;
14685 }
14686 }
14687 return dir;
14688}
14689
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014691 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014693 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014694search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014695 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014696 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014697 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014699 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 char_u *pat;
14701 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014702 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 int save_p_ws = p_ws;
14704 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014705 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014706 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014707 proftime_T tm;
14708#ifdef FEAT_RELTIME
14709 long time_limit = 0;
14710#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014711 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014712 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014714 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014715 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014716 if (dir == 0)
14717 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014718 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014719 if (flags & SP_START)
14720 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014721 if (flags & SP_END)
14722 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014723
Bram Moolenaar76929292008-01-06 19:07:36 +000014724 /* Optional arguments: line number to stop searching and timeout. */
14725 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014726 {
14727 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14728 if (lnum_stop < 0)
14729 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014730#ifdef FEAT_RELTIME
14731 if (argvars[3].v_type != VAR_UNKNOWN)
14732 {
14733 time_limit = get_tv_number_chk(&argvars[3], NULL);
14734 if (time_limit < 0)
14735 goto theend;
14736 }
14737#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014738 }
14739
Bram Moolenaar76929292008-01-06 19:07:36 +000014740#ifdef FEAT_RELTIME
14741 /* Set the time limit, if there is one. */
14742 profile_setlimit(time_limit, &tm);
14743#endif
14744
Bram Moolenaar231334e2005-07-25 20:46:57 +000014745 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014746 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014747 * Check to make sure only those flags are set.
14748 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14749 * flags cannot be set. Check for that condition also.
14750 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014751 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014752 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014755 goto theend;
14756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014758 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014759 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014760 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014761 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014763 if (flags & SP_SUBPAT)
14764 retval = subpatnum;
14765 else
14766 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014767 if (flags & SP_SETPCMARK)
14768 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014770 if (match_pos != NULL)
14771 {
14772 /* Store the match cursor position */
14773 match_pos->lnum = pos.lnum;
14774 match_pos->col = pos.col + 1;
14775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776 /* "/$" will put the cursor after the end of the line, may need to
14777 * correct that here */
14778 check_cursor();
14779 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014780
14781 /* If 'n' flag is used: restore cursor position. */
14782 if (flags & SP_NOMOVE)
14783 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014784 else
14785 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014786theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014788
14789 return retval;
14790}
14791
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014792#ifdef FEAT_FLOAT
14793/*
14794 * "round({float})" function
14795 */
14796 static void
14797f_round(argvars, rettv)
14798 typval_T *argvars;
14799 typval_T *rettv;
14800{
14801 float_T f;
14802
14803 rettv->v_type = VAR_FLOAT;
14804 if (get_float_arg(argvars, &f) == OK)
14805 /* round() is not in C90, use ceil() or floor() instead. */
14806 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14807 else
14808 rettv->vval.v_float = 0.0;
14809}
14810#endif
14811
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014812/*
14813 * "search()" function
14814 */
14815 static void
14816f_search(argvars, rettv)
14817 typval_T *argvars;
14818 typval_T *rettv;
14819{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014820 int flags = 0;
14821
14822 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823}
14824
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014826 * "searchdecl()" function
14827 */
14828 static void
14829f_searchdecl(argvars, rettv)
14830 typval_T *argvars;
14831 typval_T *rettv;
14832{
14833 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014834 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014835 int error = FALSE;
14836 char_u *name;
14837
14838 rettv->vval.v_number = 1; /* default: FAIL */
14839
14840 name = get_tv_string_chk(&argvars[0]);
14841 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014842 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014843 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014844 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14845 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14846 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014847 if (!error && name != NULL)
14848 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014849 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014850}
14851
14852/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014853 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014855 static int
14856searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014857 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014858 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859{
14860 char_u *spat, *mpat, *epat;
14861 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 int dir;
14864 int flags = 0;
14865 char_u nbuf1[NUMBUFLEN];
14866 char_u nbuf2[NUMBUFLEN];
14867 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014868 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014869 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014870 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014873 spat = get_tv_string_chk(&argvars[0]);
14874 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14875 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14876 if (spat == NULL || mpat == NULL || epat == NULL)
14877 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879 /* Handle the optional fourth argument: flags */
14880 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014881 if (dir == 0)
14882 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014883
14884 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014885 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14886 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014887 if ((flags & (SP_END | SP_SUBPAT)) != 0
14888 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014889 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014890 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014891 goto theend;
14892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014894 /* Using 'r' implies 'W', otherwise it doesn't work. */
14895 if (flags & SP_REPEAT)
14896 p_ws = FALSE;
14897
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014898 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014899 if (argvars[3].v_type == VAR_UNKNOWN
14900 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 skip = (char_u *)"";
14902 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014904 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014905 if (argvars[5].v_type != VAR_UNKNOWN)
14906 {
14907 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14908 if (lnum_stop < 0)
14909 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014910#ifdef FEAT_RELTIME
14911 if (argvars[6].v_type != VAR_UNKNOWN)
14912 {
14913 time_limit = get_tv_number_chk(&argvars[6], NULL);
14914 if (time_limit < 0)
14915 goto theend;
14916 }
14917#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014918 }
14919 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014920 if (skip == NULL)
14921 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014923 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014924 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014925
14926theend:
14927 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014928
14929 return retval;
14930}
14931
14932/*
14933 * "searchpair()" function
14934 */
14935 static void
14936f_searchpair(argvars, rettv)
14937 typval_T *argvars;
14938 typval_T *rettv;
14939{
14940 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14941}
14942
14943/*
14944 * "searchpairpos()" function
14945 */
14946 static void
14947f_searchpairpos(argvars, rettv)
14948 typval_T *argvars;
14949 typval_T *rettv;
14950{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014951 pos_T match_pos;
14952 int lnum = 0;
14953 int col = 0;
14954
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014955 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014956 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014957
14958 if (searchpair_cmn(argvars, &match_pos) > 0)
14959 {
14960 lnum = match_pos.lnum;
14961 col = match_pos.col;
14962 }
14963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014964 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14965 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014966}
14967
14968/*
14969 * Search for a start/middle/end thing.
14970 * Used by searchpair(), see its documentation for the details.
14971 * Returns 0 or -1 for no match,
14972 */
14973 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014974do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14975 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014976 char_u *spat; /* start pattern */
14977 char_u *mpat; /* middle pattern */
14978 char_u *epat; /* end pattern */
14979 int dir; /* BACKWARD or FORWARD */
14980 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014981 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014982 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014983 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014984 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014985{
14986 char_u *save_cpo;
14987 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14988 long retval = 0;
14989 pos_T pos;
14990 pos_T firstpos;
14991 pos_T foundpos;
14992 pos_T save_cursor;
14993 pos_T save_pos;
14994 int n;
14995 int r;
14996 int nest = 1;
14997 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014998 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014999 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015000
15001 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15002 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015003 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015004
Bram Moolenaar76929292008-01-06 19:07:36 +000015005#ifdef FEAT_RELTIME
15006 /* Set the time limit, if there is one. */
15007 profile_setlimit(time_limit, &tm);
15008#endif
15009
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015010 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15011 * start/middle/end (pat3, for the top pair). */
15012 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15013 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15014 if (pat2 == NULL || pat3 == NULL)
15015 goto theend;
15016 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15017 if (*mpat == NUL)
15018 STRCPY(pat3, pat2);
15019 else
15020 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15021 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015022 if (flags & SP_START)
15023 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015024
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 save_cursor = curwin->w_cursor;
15026 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015027 clearpos(&firstpos);
15028 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 pat = pat3;
15030 for (;;)
15031 {
15032 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015033 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15035 /* didn't find it or found the first match again: FAIL */
15036 break;
15037
15038 if (firstpos.lnum == 0)
15039 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015040 if (equalpos(pos, foundpos))
15041 {
15042 /* Found the same position again. Can happen with a pattern that
15043 * has "\zs" at the end and searching backwards. Advance one
15044 * character and try again. */
15045 if (dir == BACKWARD)
15046 decl(&pos);
15047 else
15048 incl(&pos);
15049 }
15050 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015052 /* clear the start flag to avoid getting stuck here */
15053 options &= ~SEARCH_START;
15054
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 /* If the skip pattern matches, ignore this match. */
15056 if (*skip != NUL)
15057 {
15058 save_pos = curwin->w_cursor;
15059 curwin->w_cursor = pos;
15060 r = eval_to_bool(skip, &err, NULL, FALSE);
15061 curwin->w_cursor = save_pos;
15062 if (err)
15063 {
15064 /* Evaluating {skip} caused an error, break here. */
15065 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015066 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 break;
15068 }
15069 if (r)
15070 continue;
15071 }
15072
15073 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15074 {
15075 /* Found end when searching backwards or start when searching
15076 * forward: nested pair. */
15077 ++nest;
15078 pat = pat2; /* nested, don't search for middle */
15079 }
15080 else
15081 {
15082 /* Found end when searching forward or start when searching
15083 * backward: end of (nested) pair; or found middle in outer pair. */
15084 if (--nest == 1)
15085 pat = pat3; /* outer level, search for middle */
15086 }
15087
15088 if (nest == 0)
15089 {
15090 /* Found the match: return matchcount or line number. */
15091 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015092 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015094 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015095 if (flags & SP_SETPCMARK)
15096 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097 curwin->w_cursor = pos;
15098 if (!(flags & SP_REPEAT))
15099 break;
15100 nest = 1; /* search for next unmatched */
15101 }
15102 }
15103
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015104 if (match_pos != NULL)
15105 {
15106 /* Store the match cursor position */
15107 match_pos->lnum = curwin->w_cursor.lnum;
15108 match_pos->col = curwin->w_cursor.col + 1;
15109 }
15110
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015112 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 curwin->w_cursor = save_cursor;
15114
15115theend:
15116 vim_free(pat2);
15117 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015118 if (p_cpo == empty_option)
15119 p_cpo = save_cpo;
15120 else
15121 /* Darn, evaluating the {skip} expression changed the value. */
15122 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015123
15124 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125}
15126
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015127/*
15128 * "searchpos()" function
15129 */
15130 static void
15131f_searchpos(argvars, rettv)
15132 typval_T *argvars;
15133 typval_T *rettv;
15134{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015135 pos_T match_pos;
15136 int lnum = 0;
15137 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015138 int n;
15139 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015141 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015142 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015143
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015144 n = search_cmn(argvars, &match_pos, &flags);
15145 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015146 {
15147 lnum = match_pos.lnum;
15148 col = match_pos.col;
15149 }
15150
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015151 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15152 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015153 if (flags & SP_SUBPAT)
15154 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015155}
15156
15157
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 static void
15159f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015160 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163#ifdef FEAT_CLIENTSERVER
15164 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015165 char_u *server = get_tv_string_chk(&argvars[0]);
15166 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015169 if (server == NULL || reply == NULL)
15170 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 if (check_restricted() || check_secure())
15172 return;
15173# ifdef FEAT_X11
15174 if (check_connection() == FAIL)
15175 return;
15176# endif
15177
15178 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180 EMSG(_("E258: Unable to send to client"));
15181 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015183 rettv->vval.v_number = 0;
15184#else
15185 rettv->vval.v_number = -1;
15186#endif
15187}
15188
Bram Moolenaar0d660222005-01-07 21:51:51 +000015189 static void
15190f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015191 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193{
15194 char_u *r = NULL;
15195
15196#ifdef FEAT_CLIENTSERVER
15197# ifdef WIN32
15198 r = serverGetVimNames();
15199# else
15200 make_connection();
15201 if (X_DISPLAY != NULL)
15202 r = serverGetVimNames(X_DISPLAY);
15203# endif
15204#endif
15205 rettv->v_type = VAR_STRING;
15206 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207}
15208
15209/*
15210 * "setbufvar()" function
15211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015213f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015214 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015215 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216{
15217 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015220 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 char_u nbuf[NUMBUFLEN];
15222
15223 if (check_restricted() || check_secure())
15224 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015225 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15226 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015227 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 varp = &argvars[2];
15229
15230 if (buf != NULL && varname != NULL && varp != NULL)
15231 {
15232 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234
15235 if (*varname == '&')
15236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015237 long numval;
15238 char_u *strval;
15239 int error = FALSE;
15240
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015242 numval = get_tv_number_chk(varp, &error);
15243 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015244 if (!error && strval != NULL)
15245 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246 }
15247 else
15248 {
15249 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15250 if (bufvarname != NULL)
15251 {
15252 STRCPY(bufvarname, "b:");
15253 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015254 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 vim_free(bufvarname);
15256 }
15257 }
15258
15259 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262}
15263
15264/*
15265 * "setcmdpos()" function
15266 */
15267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015268f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015269 typval_T *argvars;
15270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015272 int pos = (int)get_tv_number(&argvars[0]) - 1;
15273
15274 if (pos >= 0)
15275 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276}
15277
15278/*
15279 * "setline()" function
15280 */
15281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015283 typval_T *argvars;
15284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285{
15286 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015287 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015288 list_T *l = NULL;
15289 listitem_T *li = NULL;
15290 long added = 0;
15291 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015293 lnum = get_tv_lnum(&argvars[0]);
15294 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015296 l = argvars[1].vval.v_list;
15297 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015299 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015300 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015301
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015302 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015303 for (;;)
15304 {
15305 if (l != NULL)
15306 {
15307 /* list argument, get next string */
15308 if (li == NULL)
15309 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015310 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015311 li = li->li_next;
15312 }
15313
15314 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015316 break;
15317 if (lnum <= curbuf->b_ml.ml_line_count)
15318 {
15319 /* existing line, replace it */
15320 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15321 {
15322 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015323 if (lnum == curwin->w_cursor.lnum)
15324 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015325 rettv->vval.v_number = 0; /* OK */
15326 }
15327 }
15328 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15329 {
15330 /* lnum is one past the last line, append the line */
15331 ++added;
15332 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15333 rettv->vval.v_number = 0; /* OK */
15334 }
15335
15336 if (l == NULL) /* only one string argument */
15337 break;
15338 ++lnum;
15339 }
15340
15341 if (added > 0)
15342 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343}
15344
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015345static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15346
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015348 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015349 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015350 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015351set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015352 win_T *wp UNUSED;
15353 typval_T *list_arg UNUSED;
15354 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015355 typval_T *rettv;
15356{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015357#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015358 char_u *act;
15359 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015360#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015361
Bram Moolenaar2641f772005-03-25 21:58:17 +000015362 rettv->vval.v_number = -1;
15363
15364#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015365 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015366 EMSG(_(e_listreq));
15367 else
15368 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015369 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015370
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015371 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015372 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015373 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015374 if (act == NULL)
15375 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015376 if (*act == 'a' || *act == 'r')
15377 action = *act;
15378 }
15379
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015380 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015381 rettv->vval.v_number = 0;
15382 }
15383#endif
15384}
15385
15386/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015387 * "setloclist()" function
15388 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015389 static void
15390f_setloclist(argvars, rettv)
15391 typval_T *argvars;
15392 typval_T *rettv;
15393{
15394 win_T *win;
15395
15396 rettv->vval.v_number = -1;
15397
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015398 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015399 if (win != NULL)
15400 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15401}
15402
15403/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015404 * "setmatches()" function
15405 */
15406 static void
15407f_setmatches(argvars, rettv)
15408 typval_T *argvars;
15409 typval_T *rettv;
15410{
15411#ifdef FEAT_SEARCH_EXTRA
15412 list_T *l;
15413 listitem_T *li;
15414 dict_T *d;
15415
15416 rettv->vval.v_number = -1;
15417 if (argvars[0].v_type != VAR_LIST)
15418 {
15419 EMSG(_(e_listreq));
15420 return;
15421 }
15422 if ((l = argvars[0].vval.v_list) != NULL)
15423 {
15424
15425 /* To some extent make sure that we are dealing with a list from
15426 * "getmatches()". */
15427 li = l->lv_first;
15428 while (li != NULL)
15429 {
15430 if (li->li_tv.v_type != VAR_DICT
15431 || (d = li->li_tv.vval.v_dict) == NULL)
15432 {
15433 EMSG(_(e_invarg));
15434 return;
15435 }
15436 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15437 && dict_find(d, (char_u *)"pattern", -1) != NULL
15438 && dict_find(d, (char_u *)"priority", -1) != NULL
15439 && dict_find(d, (char_u *)"id", -1) != NULL))
15440 {
15441 EMSG(_(e_invarg));
15442 return;
15443 }
15444 li = li->li_next;
15445 }
15446
15447 clear_matches(curwin);
15448 li = l->lv_first;
15449 while (li != NULL)
15450 {
15451 d = li->li_tv.vval.v_dict;
15452 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15453 get_dict_string(d, (char_u *)"pattern", FALSE),
15454 (int)get_dict_number(d, (char_u *)"priority"),
15455 (int)get_dict_number(d, (char_u *)"id"));
15456 li = li->li_next;
15457 }
15458 rettv->vval.v_number = 0;
15459 }
15460#endif
15461}
15462
15463/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015464 * "setpos()" function
15465 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015466 static void
15467f_setpos(argvars, rettv)
15468 typval_T *argvars;
15469 typval_T *rettv;
15470{
15471 pos_T pos;
15472 int fnum;
15473 char_u *name;
15474
Bram Moolenaar08250432008-02-13 11:42:46 +000015475 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015476 name = get_tv_string_chk(argvars);
15477 if (name != NULL)
15478 {
15479 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15480 {
15481 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015482 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015483 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015484 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015485 if (fnum == curbuf->b_fnum)
15486 {
15487 curwin->w_cursor = pos;
15488 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015489 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015490 }
15491 else
15492 EMSG(_(e_invarg));
15493 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015494 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15495 {
15496 /* set mark */
15497 if (setmark_pos(name[1], &pos, fnum) == OK)
15498 rettv->vval.v_number = 0;
15499 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015500 else
15501 EMSG(_(e_invarg));
15502 }
15503 }
15504}
15505
15506/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015507 * "setqflist()" function
15508 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015509 static void
15510f_setqflist(argvars, rettv)
15511 typval_T *argvars;
15512 typval_T *rettv;
15513{
15514 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15515}
15516
15517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518 * "setreg()" function
15519 */
15520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015522 typval_T *argvars;
15523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524{
15525 int regname;
15526 char_u *strregname;
15527 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015528 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 int append;
15530 char_u yank_type;
15531 long block_len;
15532
15533 block_len = -1;
15534 yank_type = MAUTO;
15535 append = FALSE;
15536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015537 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015540 if (strregname == NULL)
15541 return; /* type error; errmsg already given */
15542 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 if (regname == 0 || regname == '@')
15544 regname = '"';
15545 else if (regname == '=')
15546 return;
15547
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015548 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015550 stropt = get_tv_string_chk(&argvars[2]);
15551 if (stropt == NULL)
15552 return; /* type error */
15553 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 switch (*stropt)
15555 {
15556 case 'a': case 'A': /* append */
15557 append = TRUE;
15558 break;
15559 case 'v': case 'c': /* character-wise selection */
15560 yank_type = MCHAR;
15561 break;
15562 case 'V': case 'l': /* line-wise selection */
15563 yank_type = MLINE;
15564 break;
15565#ifdef FEAT_VISUAL
15566 case 'b': case Ctrl_V: /* block-wise selection */
15567 yank_type = MBLOCK;
15568 if (VIM_ISDIGIT(stropt[1]))
15569 {
15570 ++stropt;
15571 block_len = getdigits(&stropt) - 1;
15572 --stropt;
15573 }
15574 break;
15575#endif
15576 }
15577 }
15578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015579 strval = get_tv_string_chk(&argvars[1]);
15580 if (strval != NULL)
15581 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584}
15585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015586/*
15587 * "settabwinvar()" function
15588 */
15589 static void
15590f_settabwinvar(argvars, rettv)
15591 typval_T *argvars;
15592 typval_T *rettv;
15593{
15594 setwinvar(argvars, rettv, 1);
15595}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596
15597/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015598 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015601f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015602 typval_T *argvars;
15603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015605 setwinvar(argvars, rettv, 0);
15606}
15607
15608/*
15609 * "setwinvar()" and "settabwinvar()" functions
15610 */
15611 static void
15612setwinvar(argvars, rettv, off)
15613 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015614 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015615 int off;
15616{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 win_T *win;
15618#ifdef FEAT_WINDOWS
15619 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015620 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621#endif
15622 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015623 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015625 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626
15627 if (check_restricted() || check_secure())
15628 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015629
15630#ifdef FEAT_WINDOWS
15631 if (off == 1)
15632 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15633 else
15634 tp = curtab;
15635#endif
15636 win = find_win_by_nr(&argvars[off], tp);
15637 varname = get_tv_string_chk(&argvars[off + 1]);
15638 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639
15640 if (win != NULL && varname != NULL && varp != NULL)
15641 {
15642#ifdef FEAT_WINDOWS
15643 /* set curwin to be our win, temporarily */
15644 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015645 save_curtab = curtab;
15646 goto_tabpage_tp(tp);
15647 if (!win_valid(win))
15648 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 curwin = win;
15650 curbuf = curwin->w_buffer;
15651#endif
15652
15653 if (*varname == '&')
15654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015655 long numval;
15656 char_u *strval;
15657 int error = FALSE;
15658
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015660 numval = get_tv_number_chk(varp, &error);
15661 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015662 if (!error && strval != NULL)
15663 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 }
15665 else
15666 {
15667 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15668 if (winvarname != NULL)
15669 {
15670 STRCPY(winvarname, "w:");
15671 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015672 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 vim_free(winvarname);
15674 }
15675 }
15676
15677#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015678 /* Restore current tabpage and window, if still valid (autocomands can
15679 * make them invalid). */
15680 if (valid_tabpage(save_curtab))
15681 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 if (win_valid(save_curwin))
15683 {
15684 curwin = save_curwin;
15685 curbuf = curwin->w_buffer;
15686 }
15687#endif
15688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689}
15690
15691/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015692 * "shellescape({string})" function
15693 */
15694 static void
15695f_shellescape(argvars, rettv)
15696 typval_T *argvars;
15697 typval_T *rettv;
15698{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015699 rettv->vval.v_string = vim_strsave_shellescape(
15700 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015701 rettv->v_type = VAR_STRING;
15702}
15703
15704/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015705 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706 */
15707 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015708f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015709 typval_T *argvars;
15710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015712 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713
Bram Moolenaar0d660222005-01-07 21:51:51 +000015714 p = get_tv_string(&argvars[0]);
15715 rettv->vval.v_string = vim_strsave(p);
15716 simplify_filename(rettv->vval.v_string); /* simplify in place */
15717 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718}
15719
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015720#ifdef FEAT_FLOAT
15721/*
15722 * "sin()" function
15723 */
15724 static void
15725f_sin(argvars, rettv)
15726 typval_T *argvars;
15727 typval_T *rettv;
15728{
15729 float_T f;
15730
15731 rettv->v_type = VAR_FLOAT;
15732 if (get_float_arg(argvars, &f) == OK)
15733 rettv->vval.v_float = sin(f);
15734 else
15735 rettv->vval.v_float = 0.0;
15736}
15737#endif
15738
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739static int
15740#ifdef __BORLANDC__
15741 _RTLENTRYF
15742#endif
15743 item_compare __ARGS((const void *s1, const void *s2));
15744static int
15745#ifdef __BORLANDC__
15746 _RTLENTRYF
15747#endif
15748 item_compare2 __ARGS((const void *s1, const void *s2));
15749
15750static int item_compare_ic;
15751static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015752static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753#define ITEM_COMPARE_FAIL 999
15754
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758 static int
15759#ifdef __BORLANDC__
15760_RTLENTRYF
15761#endif
15762item_compare(s1, s2)
15763 const void *s1;
15764 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015766 char_u *p1, *p2;
15767 char_u *tofree1, *tofree2;
15768 int res;
15769 char_u numbuf1[NUMBUFLEN];
15770 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015772 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15773 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015774 if (p1 == NULL)
15775 p1 = (char_u *)"";
15776 if (p2 == NULL)
15777 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778 if (item_compare_ic)
15779 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015781 res = STRCMP(p1, p2);
15782 vim_free(tofree1);
15783 vim_free(tofree2);
15784 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785}
15786
15787 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788#ifdef __BORLANDC__
15789_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015791item_compare2(s1, s2)
15792 const void *s1;
15793 const void *s2;
15794{
15795 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015796 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015797 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015798 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015800 /* shortcut after failure in previous call; compare all items equal */
15801 if (item_compare_func_err)
15802 return 0;
15803
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015804 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15805 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015806 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15807 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015808
15809 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015810 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015811 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015812 clear_tv(&argv[0]);
15813 clear_tv(&argv[1]);
15814
15815 if (res == FAIL)
15816 res = ITEM_COMPARE_FAIL;
15817 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015818 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15819 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000015820 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821 clear_tv(&rettv);
15822 return res;
15823}
15824
15825/*
15826 * "sort({list})" function
15827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 typval_T *argvars;
15831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832{
Bram Moolenaar33570922005-01-25 22:26:29 +000015833 list_T *l;
15834 listitem_T *li;
15835 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015836 long len;
15837 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838
Bram Moolenaar0d660222005-01-07 21:51:51 +000015839 if (argvars[0].v_type != VAR_LIST)
15840 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 else
15842 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015843 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015844 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015845 return;
15846 rettv->vval.v_list = l;
15847 rettv->v_type = VAR_LIST;
15848 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849
Bram Moolenaar0d660222005-01-07 21:51:51 +000015850 len = list_len(l);
15851 if (len <= 1)
15852 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854 item_compare_ic = FALSE;
15855 item_compare_func = NULL;
15856 if (argvars[1].v_type != VAR_UNKNOWN)
15857 {
15858 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015859 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015860 else
15861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015862 int error = FALSE;
15863
15864 i = get_tv_number_chk(&argvars[1], &error);
15865 if (error)
15866 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015867 if (i == 1)
15868 item_compare_ic = TRUE;
15869 else
15870 item_compare_func = get_tv_string(&argvars[1]);
15871 }
15872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873
Bram Moolenaar0d660222005-01-07 21:51:51 +000015874 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015875 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015876 if (ptrs == NULL)
15877 return;
15878 i = 0;
15879 for (li = l->lv_first; li != NULL; li = li->li_next)
15880 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015882 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015883 /* test the compare function */
15884 if (item_compare_func != NULL
15885 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15886 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015887 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889 {
15890 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015891 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015892 item_compare_func == NULL ? item_compare : item_compare2);
15893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015894 if (!item_compare_func_err)
15895 {
15896 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015897 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015898 l->lv_len = 0;
15899 for (i = 0; i < len; ++i)
15900 list_append(l, ptrs[i]);
15901 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015902 }
15903
15904 vim_free(ptrs);
15905 }
15906}
15907
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015908/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015909 * "soundfold({word})" function
15910 */
15911 static void
15912f_soundfold(argvars, rettv)
15913 typval_T *argvars;
15914 typval_T *rettv;
15915{
15916 char_u *s;
15917
15918 rettv->v_type = VAR_STRING;
15919 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015920#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015921 rettv->vval.v_string = eval_soundfold(s);
15922#else
15923 rettv->vval.v_string = vim_strsave(s);
15924#endif
15925}
15926
15927/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015928 * "spellbadword()" function
15929 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015930 static void
15931f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015932 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015933 typval_T *rettv;
15934{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015935 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015936 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015937 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015938
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015939 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015940 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015941
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015942#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015943 if (argvars[0].v_type == VAR_UNKNOWN)
15944 {
15945 /* Find the start and length of the badly spelled word. */
15946 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15947 if (len != 0)
15948 word = ml_get_cursor();
15949 }
15950 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15951 {
15952 char_u *str = get_tv_string_chk(&argvars[0]);
15953 int capcol = -1;
15954
15955 if (str != NULL)
15956 {
15957 /* Check the argument for spelling. */
15958 while (*str != NUL)
15959 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015960 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015961 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015962 {
15963 word = str;
15964 break;
15965 }
15966 str += len;
15967 }
15968 }
15969 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015970#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015971
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015972 list_append_string(rettv->vval.v_list, word, len);
15973 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015974 attr == HLF_SPB ? "bad" :
15975 attr == HLF_SPR ? "rare" :
15976 attr == HLF_SPL ? "local" :
15977 attr == HLF_SPC ? "caps" :
15978 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015979}
15980
15981/*
15982 * "spellsuggest()" function
15983 */
15984 static void
15985f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015986 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015987 typval_T *rettv;
15988{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015989#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015990 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015991 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015992 int maxcount;
15993 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015994 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015995 listitem_T *li;
15996 int need_capital = FALSE;
15997#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015998
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015999 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016000 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016001
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016002#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016003 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16004 {
16005 str = get_tv_string(&argvars[0]);
16006 if (argvars[1].v_type != VAR_UNKNOWN)
16007 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016008 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016009 if (maxcount <= 0)
16010 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016011 if (argvars[2].v_type != VAR_UNKNOWN)
16012 {
16013 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16014 if (typeerr)
16015 return;
16016 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016017 }
16018 else
16019 maxcount = 25;
16020
Bram Moolenaar4770d092006-01-12 23:22:24 +000016021 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016022
16023 for (i = 0; i < ga.ga_len; ++i)
16024 {
16025 str = ((char_u **)ga.ga_data)[i];
16026
16027 li = listitem_alloc();
16028 if (li == NULL)
16029 vim_free(str);
16030 else
16031 {
16032 li->li_tv.v_type = VAR_STRING;
16033 li->li_tv.v_lock = 0;
16034 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016035 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016036 }
16037 }
16038 ga_clear(&ga);
16039 }
16040#endif
16041}
16042
Bram Moolenaar0d660222005-01-07 21:51:51 +000016043 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016044f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016045 typval_T *argvars;
16046 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016047{
16048 char_u *str;
16049 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016050 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016051 regmatch_T regmatch;
16052 char_u patbuf[NUMBUFLEN];
16053 char_u *save_cpo;
16054 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016055 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016056 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016057 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058
16059 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16060 save_cpo = p_cpo;
16061 p_cpo = (char_u *)"";
16062
16063 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016064 if (argvars[1].v_type != VAR_UNKNOWN)
16065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016066 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16067 if (pat == NULL)
16068 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016070 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016071 }
16072 if (pat == NULL || *pat == NUL)
16073 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016074
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016075 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016077 if (typeerr)
16078 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079
Bram Moolenaar0d660222005-01-07 21:51:51 +000016080 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16081 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016083 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016084 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016086 if (*str == NUL)
16087 match = FALSE; /* empty item at the end */
16088 else
16089 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090 if (match)
16091 end = regmatch.startp[0];
16092 else
16093 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016094 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16095 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016096 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016097 if (list_append_string(rettv->vval.v_list, str,
16098 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016099 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016100 }
16101 if (!match)
16102 break;
16103 /* Advance to just after the match. */
16104 if (regmatch.endp[0] > str)
16105 col = 0;
16106 else
16107 {
16108 /* Don't get stuck at the same match. */
16109#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016110 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016111#else
16112 col = 1;
16113#endif
16114 }
16115 str = regmatch.endp[0];
16116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117
Bram Moolenaar0d660222005-01-07 21:51:51 +000016118 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120
Bram Moolenaar0d660222005-01-07 21:51:51 +000016121 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122}
16123
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016124#ifdef FEAT_FLOAT
16125/*
16126 * "sqrt()" function
16127 */
16128 static void
16129f_sqrt(argvars, rettv)
16130 typval_T *argvars;
16131 typval_T *rettv;
16132{
16133 float_T f;
16134
16135 rettv->v_type = VAR_FLOAT;
16136 if (get_float_arg(argvars, &f) == OK)
16137 rettv->vval.v_float = sqrt(f);
16138 else
16139 rettv->vval.v_float = 0.0;
16140}
16141
16142/*
16143 * "str2float()" function
16144 */
16145 static void
16146f_str2float(argvars, rettv)
16147 typval_T *argvars;
16148 typval_T *rettv;
16149{
16150 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16151
16152 if (*p == '+')
16153 p = skipwhite(p + 1);
16154 (void)string2float(p, &rettv->vval.v_float);
16155 rettv->v_type = VAR_FLOAT;
16156}
16157#endif
16158
Bram Moolenaar2c932302006-03-18 21:42:09 +000016159/*
16160 * "str2nr()" function
16161 */
16162 static void
16163f_str2nr(argvars, rettv)
16164 typval_T *argvars;
16165 typval_T *rettv;
16166{
16167 int base = 10;
16168 char_u *p;
16169 long n;
16170
16171 if (argvars[1].v_type != VAR_UNKNOWN)
16172 {
16173 base = get_tv_number(&argvars[1]);
16174 if (base != 8 && base != 10 && base != 16)
16175 {
16176 EMSG(_(e_invarg));
16177 return;
16178 }
16179 }
16180
16181 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016182 if (*p == '+')
16183 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016184 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16185 rettv->vval.v_number = n;
16186}
16187
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188#ifdef HAVE_STRFTIME
16189/*
16190 * "strftime({format}[, {time}])" function
16191 */
16192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016193f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016194 typval_T *argvars;
16195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196{
16197 char_u result_buf[256];
16198 struct tm *curtime;
16199 time_t seconds;
16200 char_u *p;
16201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016202 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016204 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016205 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 seconds = time(NULL);
16207 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016208 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209 curtime = localtime(&seconds);
16210 /* MSVC returns NULL for an invalid value of seconds. */
16211 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016212 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 else
16214 {
16215# ifdef FEAT_MBYTE
16216 vimconv_T conv;
16217 char_u *enc;
16218
16219 conv.vc_type = CONV_NONE;
16220 enc = enc_locale();
16221 convert_setup(&conv, p_enc, enc);
16222 if (conv.vc_type != CONV_NONE)
16223 p = string_convert(&conv, p, NULL);
16224# endif
16225 if (p != NULL)
16226 (void)strftime((char *)result_buf, sizeof(result_buf),
16227 (char *)p, curtime);
16228 else
16229 result_buf[0] = NUL;
16230
16231# ifdef FEAT_MBYTE
16232 if (conv.vc_type != CONV_NONE)
16233 vim_free(p);
16234 convert_setup(&conv, enc, p_enc);
16235 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016236 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 else
16238# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016239 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240
16241# ifdef FEAT_MBYTE
16242 /* Release conversion descriptors */
16243 convert_setup(&conv, NULL, NULL);
16244 vim_free(enc);
16245# endif
16246 }
16247}
16248#endif
16249
16250/*
16251 * "stridx()" function
16252 */
16253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016254f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016255 typval_T *argvars;
16256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257{
16258 char_u buf[NUMBUFLEN];
16259 char_u *needle;
16260 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016261 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016263 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016265 needle = get_tv_string_chk(&argvars[1]);
16266 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016268 if (needle == NULL || haystack == NULL)
16269 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270
Bram Moolenaar33570922005-01-25 22:26:29 +000016271 if (argvars[2].v_type != VAR_UNKNOWN)
16272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016273 int error = FALSE;
16274
16275 start_idx = get_tv_number_chk(&argvars[2], &error);
16276 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016278 if (start_idx >= 0)
16279 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016280 }
16281
16282 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16283 if (pos != NULL)
16284 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285}
16286
16287/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016288 * "string()" function
16289 */
16290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016291f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 typval_T *argvars;
16293 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016294{
16295 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016296 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016298 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016299 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016300 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016301 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016302 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303}
16304
16305/*
16306 * "strlen()" function
16307 */
16308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016309f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016310 typval_T *argvars;
16311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016313 rettv->vval.v_number = (varnumber_T)(STRLEN(
16314 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315}
16316
16317/*
16318 * "strpart()" function
16319 */
16320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016321f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016322 typval_T *argvars;
16323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324{
16325 char_u *p;
16326 int n;
16327 int len;
16328 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016329 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016331 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332 slen = (int)STRLEN(p);
16333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016334 n = get_tv_number_chk(&argvars[1], &error);
16335 if (error)
16336 len = 0;
16337 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016338 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 else
16340 len = slen - n; /* default len: all bytes that are available. */
16341
16342 /*
16343 * Only return the overlap between the specified part and the actual
16344 * string.
16345 */
16346 if (n < 0)
16347 {
16348 len += n;
16349 n = 0;
16350 }
16351 else if (n > slen)
16352 n = slen;
16353 if (len < 0)
16354 len = 0;
16355 else if (n + len > slen)
16356 len = slen - n;
16357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016358 rettv->v_type = VAR_STRING;
16359 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360}
16361
16362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363 * "strridx()" function
16364 */
16365 static void
16366f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016367 typval_T *argvars;
16368 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369{
16370 char_u buf[NUMBUFLEN];
16371 char_u *needle;
16372 char_u *haystack;
16373 char_u *rest;
16374 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016375 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016377 needle = get_tv_string_chk(&argvars[1]);
16378 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016379
16380 rettv->vval.v_number = -1;
16381 if (needle == NULL || haystack == NULL)
16382 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016383
16384 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016385 if (argvars[2].v_type != VAR_UNKNOWN)
16386 {
16387 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016388 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016389 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016390 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016391 }
16392 else
16393 end_idx = haystack_len;
16394
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016396 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016398 lastmatch = haystack + end_idx;
16399 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016401 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016402 for (rest = haystack; *rest != '\0'; ++rest)
16403 {
16404 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016405 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406 break;
16407 lastmatch = rest;
16408 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016409 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410
16411 if (lastmatch == NULL)
16412 rettv->vval.v_number = -1;
16413 else
16414 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16415}
16416
16417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 * "strtrans()" function
16419 */
16420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016421f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016422 typval_T *argvars;
16423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016425 rettv->v_type = VAR_STRING;
16426 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427}
16428
16429/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430 * "submatch()" function
16431 */
16432 static void
16433f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016434 typval_T *argvars;
16435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436{
16437 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016438 rettv->vval.v_string =
16439 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440}
16441
16442/*
16443 * "substitute()" function
16444 */
16445 static void
16446f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016447 typval_T *argvars;
16448 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449{
16450 char_u patbuf[NUMBUFLEN];
16451 char_u subbuf[NUMBUFLEN];
16452 char_u flagsbuf[NUMBUFLEN];
16453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016454 char_u *str = get_tv_string_chk(&argvars[0]);
16455 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16456 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16457 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16458
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016460 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16461 rettv->vval.v_string = NULL;
16462 else
16463 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464}
16465
16466/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016467 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016470f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016471 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473{
16474 int id = 0;
16475#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016476 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 long col;
16478 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016479 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016481 lnum = get_tv_lnum(argvars); /* -1 on type error */
16482 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16483 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016486 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016487 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488#endif
16489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016490 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491}
16492
16493/*
16494 * "synIDattr(id, what [, mode])" function
16495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016497f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016498 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500{
16501 char_u *p = NULL;
16502#ifdef FEAT_SYN_HL
16503 int id;
16504 char_u *what;
16505 char_u *mode;
16506 char_u modebuf[NUMBUFLEN];
16507 int modec;
16508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016509 id = get_tv_number(&argvars[0]);
16510 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016511 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016513 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514 modec = TOLOWER_ASC(mode[0]);
16515 if (modec != 't' && modec != 'c'
16516#ifdef FEAT_GUI
16517 && modec != 'g'
16518#endif
16519 )
16520 modec = 0; /* replace invalid with current */
16521 }
16522 else
16523 {
16524#ifdef FEAT_GUI
16525 if (gui.in_use)
16526 modec = 'g';
16527 else
16528#endif
16529 if (t_colors > 1)
16530 modec = 'c';
16531 else
16532 modec = 't';
16533 }
16534
16535
16536 switch (TOLOWER_ASC(what[0]))
16537 {
16538 case 'b':
16539 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16540 p = highlight_color(id, what, modec);
16541 else /* bold */
16542 p = highlight_has_attr(id, HL_BOLD, modec);
16543 break;
16544
16545 case 'f': /* fg[#] */
16546 p = highlight_color(id, what, modec);
16547 break;
16548
16549 case 'i':
16550 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16551 p = highlight_has_attr(id, HL_INVERSE, modec);
16552 else /* italic */
16553 p = highlight_has_attr(id, HL_ITALIC, modec);
16554 break;
16555
16556 case 'n': /* name */
16557 p = get_highlight_name(NULL, id - 1);
16558 break;
16559
16560 case 'r': /* reverse */
16561 p = highlight_has_attr(id, HL_INVERSE, modec);
16562 break;
16563
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016564 case 's':
16565 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16566 p = highlight_color(id, what, modec);
16567 else /* standout */
16568 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 break;
16570
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016571 case 'u':
16572 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16573 /* underline */
16574 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16575 else
16576 /* undercurl */
16577 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 break;
16579 }
16580
16581 if (p != NULL)
16582 p = vim_strsave(p);
16583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016584 rettv->v_type = VAR_STRING;
16585 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586}
16587
16588/*
16589 * "synIDtrans(id)" function
16590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016592f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016593 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595{
16596 int id;
16597
16598#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016599 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600
16601 if (id > 0)
16602 id = syn_get_final_id(id);
16603 else
16604#endif
16605 id = 0;
16606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016607 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608}
16609
16610/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016611 * "synstack(lnum, col)" function
16612 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016613 static void
16614f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016615 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016616 typval_T *rettv;
16617{
16618#ifdef FEAT_SYN_HL
16619 long lnum;
16620 long col;
16621 int i;
16622 int id;
16623#endif
16624
16625 rettv->v_type = VAR_LIST;
16626 rettv->vval.v_list = NULL;
16627
16628#ifdef FEAT_SYN_HL
16629 lnum = get_tv_lnum(argvars); /* -1 on type error */
16630 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16631
16632 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016633 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016634 && rettv_list_alloc(rettv) != FAIL)
16635 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016636 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016637 for (i = 0; ; ++i)
16638 {
16639 id = syn_get_stack_item(i);
16640 if (id < 0)
16641 break;
16642 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16643 break;
16644 }
16645 }
16646#endif
16647}
16648
16649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 * "system()" function
16651 */
16652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016653f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016654 typval_T *argvars;
16655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016657 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016659 char_u *infile = NULL;
16660 char_u buf[NUMBUFLEN];
16661 int err = FALSE;
16662 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016664 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016665 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016666
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016667 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016668 {
16669 /*
16670 * Write the string to a temp file, to be used for input of the shell
16671 * command.
16672 */
16673 if ((infile = vim_tempname('i')) == NULL)
16674 {
16675 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016676 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016677 }
16678
16679 fd = mch_fopen((char *)infile, WRITEBIN);
16680 if (fd == NULL)
16681 {
16682 EMSG2(_(e_notopen), infile);
16683 goto done;
16684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016685 p = get_tv_string_buf_chk(&argvars[1], buf);
16686 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016687 {
16688 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016689 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016690 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016691 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16692 err = TRUE;
16693 if (fclose(fd) != 0)
16694 err = TRUE;
16695 if (err)
16696 {
16697 EMSG(_("E677: Error writing temp file"));
16698 goto done;
16699 }
16700 }
16701
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016702 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16703 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016704
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705#ifdef USE_CR
16706 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016707 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708 {
16709 char_u *s;
16710
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016711 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 {
16713 if (*s == CAR)
16714 *s = NL;
16715 }
16716 }
16717#else
16718# ifdef USE_CRNL
16719 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016720 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 {
16722 char_u *s, *d;
16723
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016724 d = res;
16725 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 {
16727 if (s[0] == CAR && s[1] == NL)
16728 ++s;
16729 *d++ = *s;
16730 }
16731 *d = NUL;
16732 }
16733# endif
16734#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016735
16736done:
16737 if (infile != NULL)
16738 {
16739 mch_remove(infile);
16740 vim_free(infile);
16741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016742 rettv->v_type = VAR_STRING;
16743 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744}
16745
16746/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016747 * "tabpagebuflist()" function
16748 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016749 static void
16750f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016751 typval_T *argvars UNUSED;
16752 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016753{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016754#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016755 tabpage_T *tp;
16756 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016757
16758 if (argvars[0].v_type == VAR_UNKNOWN)
16759 wp = firstwin;
16760 else
16761 {
16762 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16763 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016764 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016765 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016766 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016767 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016768 for (; wp != NULL; wp = wp->w_next)
16769 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016770 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016771 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016772 }
16773#endif
16774}
16775
16776
16777/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016778 * "tabpagenr()" function
16779 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016780 static void
16781f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016782 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016783 typval_T *rettv;
16784{
16785 int nr = 1;
16786#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016787 char_u *arg;
16788
16789 if (argvars[0].v_type != VAR_UNKNOWN)
16790 {
16791 arg = get_tv_string_chk(&argvars[0]);
16792 nr = 0;
16793 if (arg != NULL)
16794 {
16795 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016796 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016797 else
16798 EMSG2(_(e_invexpr2), arg);
16799 }
16800 }
16801 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016802 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016803#endif
16804 rettv->vval.v_number = nr;
16805}
16806
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016807
16808#ifdef FEAT_WINDOWS
16809static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16810
16811/*
16812 * Common code for tabpagewinnr() and winnr().
16813 */
16814 static int
16815get_winnr(tp, argvar)
16816 tabpage_T *tp;
16817 typval_T *argvar;
16818{
16819 win_T *twin;
16820 int nr = 1;
16821 win_T *wp;
16822 char_u *arg;
16823
16824 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16825 if (argvar->v_type != VAR_UNKNOWN)
16826 {
16827 arg = get_tv_string_chk(argvar);
16828 if (arg == NULL)
16829 nr = 0; /* type error; errmsg already given */
16830 else if (STRCMP(arg, "$") == 0)
16831 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16832 else if (STRCMP(arg, "#") == 0)
16833 {
16834 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16835 if (twin == NULL)
16836 nr = 0;
16837 }
16838 else
16839 {
16840 EMSG2(_(e_invexpr2), arg);
16841 nr = 0;
16842 }
16843 }
16844
16845 if (nr > 0)
16846 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16847 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016848 {
16849 if (wp == NULL)
16850 {
16851 /* didn't find it in this tabpage */
16852 nr = 0;
16853 break;
16854 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016855 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016856 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016857 return nr;
16858}
16859#endif
16860
16861/*
16862 * "tabpagewinnr()" function
16863 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016864 static void
16865f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016866 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016867 typval_T *rettv;
16868{
16869 int nr = 1;
16870#ifdef FEAT_WINDOWS
16871 tabpage_T *tp;
16872
16873 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16874 if (tp == NULL)
16875 nr = 0;
16876 else
16877 nr = get_winnr(tp, &argvars[1]);
16878#endif
16879 rettv->vval.v_number = nr;
16880}
16881
16882
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016883/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016884 * "tagfiles()" function
16885 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016886 static void
16887f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016888 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016889 typval_T *rettv;
16890{
16891 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016892 tagname_T tn;
16893 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016894
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016895 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016896 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016897
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016898 for (first = TRUE; ; first = FALSE)
16899 if (get_tagfname(&tn, first, fname) == FAIL
16900 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016901 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016902 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016903}
16904
16905/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016906 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016907 */
16908 static void
16909f_taglist(argvars, rettv)
16910 typval_T *argvars;
16911 typval_T *rettv;
16912{
16913 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016914
16915 tag_pattern = get_tv_string(&argvars[0]);
16916
16917 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016918 if (*tag_pattern == NUL)
16919 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016920
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016921 if (rettv_list_alloc(rettv) == OK)
16922 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016923}
16924
16925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 * "tempname()" function
16927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016929f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016930 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932{
16933 static int x = 'A';
16934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016935 rettv->v_type = VAR_STRING;
16936 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937
16938 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16939 * names. Skip 'I' and 'O', they are used for shell redirection. */
16940 do
16941 {
16942 if (x == 'Z')
16943 x = '0';
16944 else if (x == '9')
16945 x = 'A';
16946 else
16947 {
16948#ifdef EBCDIC
16949 if (x == 'I')
16950 x = 'J';
16951 else if (x == 'R')
16952 x = 'S';
16953 else
16954#endif
16955 ++x;
16956 }
16957 } while (x == 'I' || x == 'O');
16958}
16959
16960/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016961 * "test(list)" function: Just checking the walls...
16962 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000016963 static void
16964f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016965 typval_T *argvars UNUSED;
16966 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000016967{
16968 /* Used for unit testing. Change the code below to your liking. */
16969#if 0
16970 listitem_T *li;
16971 list_T *l;
16972 char_u *bad, *good;
16973
16974 if (argvars[0].v_type != VAR_LIST)
16975 return;
16976 l = argvars[0].vval.v_list;
16977 if (l == NULL)
16978 return;
16979 li = l->lv_first;
16980 if (li == NULL)
16981 return;
16982 bad = get_tv_string(&li->li_tv);
16983 li = li->li_next;
16984 if (li == NULL)
16985 return;
16986 good = get_tv_string(&li->li_tv);
16987 rettv->vval.v_number = test_edit_score(bad, good);
16988#endif
16989}
16990
16991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 * "tolower(string)" function
16993 */
16994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016995f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016996 typval_T *argvars;
16997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998{
16999 char_u *p;
17000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017001 p = vim_strsave(get_tv_string(&argvars[0]));
17002 rettv->v_type = VAR_STRING;
17003 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004
17005 if (p != NULL)
17006 while (*p != NUL)
17007 {
17008#ifdef FEAT_MBYTE
17009 int l;
17010
17011 if (enc_utf8)
17012 {
17013 int c, lc;
17014
17015 c = utf_ptr2char(p);
17016 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017017 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 /* TODO: reallocate string when byte count changes. */
17019 if (utf_char2len(lc) == l)
17020 utf_char2bytes(lc, p);
17021 p += l;
17022 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017023 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 p += l; /* skip multi-byte character */
17025 else
17026#endif
17027 {
17028 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17029 ++p;
17030 }
17031 }
17032}
17033
17034/*
17035 * "toupper(string)" function
17036 */
17037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017038f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017039 typval_T *argvars;
17040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017042 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044}
17045
17046/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017047 * "tr(string, fromstr, tostr)" function
17048 */
17049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017051 typval_T *argvars;
17052 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017053{
17054 char_u *instr;
17055 char_u *fromstr;
17056 char_u *tostr;
17057 char_u *p;
17058#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017059 int inlen;
17060 int fromlen;
17061 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017062 int idx;
17063 char_u *cpstr;
17064 int cplen;
17065 int first = TRUE;
17066#endif
17067 char_u buf[NUMBUFLEN];
17068 char_u buf2[NUMBUFLEN];
17069 garray_T ga;
17070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017071 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017072 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17073 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017074
17075 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017076 rettv->v_type = VAR_STRING;
17077 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 if (fromstr == NULL || tostr == NULL)
17079 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017080 ga_init2(&ga, (int)sizeof(char), 80);
17081
17082#ifdef FEAT_MBYTE
17083 if (!has_mbyte)
17084#endif
17085 /* not multi-byte: fromstr and tostr must be the same length */
17086 if (STRLEN(fromstr) != STRLEN(tostr))
17087 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017088#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017089error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017090#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017091 EMSG2(_(e_invarg2), fromstr);
17092 ga_clear(&ga);
17093 return;
17094 }
17095
17096 /* fromstr and tostr have to contain the same number of chars */
17097 while (*instr != NUL)
17098 {
17099#ifdef FEAT_MBYTE
17100 if (has_mbyte)
17101 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017102 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017103 cpstr = instr;
17104 cplen = inlen;
17105 idx = 0;
17106 for (p = fromstr; *p != NUL; p += fromlen)
17107 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017108 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017109 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17110 {
17111 for (p = tostr; *p != NUL; p += tolen)
17112 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017113 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017114 if (idx-- == 0)
17115 {
17116 cplen = tolen;
17117 cpstr = p;
17118 break;
17119 }
17120 }
17121 if (*p == NUL) /* tostr is shorter than fromstr */
17122 goto error;
17123 break;
17124 }
17125 ++idx;
17126 }
17127
17128 if (first && cpstr == instr)
17129 {
17130 /* Check that fromstr and tostr have the same number of
17131 * (multi-byte) characters. Done only once when a character
17132 * of instr doesn't appear in fromstr. */
17133 first = FALSE;
17134 for (p = tostr; *p != NUL; p += tolen)
17135 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017136 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017137 --idx;
17138 }
17139 if (idx != 0)
17140 goto error;
17141 }
17142
17143 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017144 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017145 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017146
17147 instr += inlen;
17148 }
17149 else
17150#endif
17151 {
17152 /* When not using multi-byte chars we can do it faster. */
17153 p = vim_strchr(fromstr, *instr);
17154 if (p != NULL)
17155 ga_append(&ga, tostr[p - fromstr]);
17156 else
17157 ga_append(&ga, *instr);
17158 ++instr;
17159 }
17160 }
17161
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017162 /* add a terminating NUL */
17163 ga_grow(&ga, 1);
17164 ga_append(&ga, NUL);
17165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017166 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017167}
17168
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017169#ifdef FEAT_FLOAT
17170/*
17171 * "trunc({float})" function
17172 */
17173 static void
17174f_trunc(argvars, rettv)
17175 typval_T *argvars;
17176 typval_T *rettv;
17177{
17178 float_T f;
17179
17180 rettv->v_type = VAR_FLOAT;
17181 if (get_float_arg(argvars, &f) == OK)
17182 /* trunc() is not in C90, use floor() or ceil() instead. */
17183 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17184 else
17185 rettv->vval.v_float = 0.0;
17186}
17187#endif
17188
Bram Moolenaar8299df92004-07-10 09:47:34 +000017189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 * "type(expr)" function
17191 */
17192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017193f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017194 typval_T *argvars;
17195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017197 int n;
17198
17199 switch (argvars[0].v_type)
17200 {
17201 case VAR_NUMBER: n = 0; break;
17202 case VAR_STRING: n = 1; break;
17203 case VAR_FUNC: n = 2; break;
17204 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017205 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017206#ifdef FEAT_FLOAT
17207 case VAR_FLOAT: n = 5; break;
17208#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017209 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17210 }
17211 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212}
17213
17214/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017215 * "values(dict)" function
17216 */
17217 static void
17218f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017219 typval_T *argvars;
17220 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017221{
17222 dict_list(argvars, rettv, 1);
17223}
17224
17225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 * "virtcol(string)" function
17227 */
17228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017229f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017230 typval_T *argvars;
17231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232{
17233 colnr_T vcol = 0;
17234 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017235 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017237 fp = var2fpos(&argvars[0], FALSE, &fnum);
17238 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17239 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 {
17241 getvvcol(curwin, fp, NULL, NULL, &vcol);
17242 ++vcol;
17243 }
17244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017245 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246}
17247
17248/*
17249 * "visualmode()" function
17250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017252f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017253 typval_T *argvars UNUSED;
17254 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255{
17256#ifdef FEAT_VISUAL
17257 char_u str[2];
17258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017259 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 str[0] = curbuf->b_visual_mode_eval;
17261 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017262 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263
17264 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017265 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267#endif
17268}
17269
17270/*
17271 * "winbufnr(nr)" function
17272 */
17273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017274f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017275 typval_T *argvars;
17276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277{
17278 win_T *wp;
17279
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017280 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017282 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017284 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285}
17286
17287/*
17288 * "wincol()" function
17289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017291f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294{
17295 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017296 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297}
17298
17299/*
17300 * "winheight(nr)" function
17301 */
17302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017303f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017304 typval_T *argvars;
17305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306{
17307 win_T *wp;
17308
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017309 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017311 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017313 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314}
17315
17316/*
17317 * "winline()" function
17318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017320f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017321 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323{
17324 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017325 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326}
17327
17328/*
17329 * "winnr()" function
17330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017332f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017333 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335{
17336 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017337
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017339 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017341 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342}
17343
17344/*
17345 * "winrestcmd()" function
17346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017349 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351{
17352#ifdef FEAT_WINDOWS
17353 win_T *wp;
17354 int winnr = 1;
17355 garray_T ga;
17356 char_u buf[50];
17357
17358 ga_init2(&ga, (int)sizeof(char), 70);
17359 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17360 {
17361 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17362 ga_concat(&ga, buf);
17363# ifdef FEAT_VERTSPLIT
17364 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17365 ga_concat(&ga, buf);
17366# endif
17367 ++winnr;
17368 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017369 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017371 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017373 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376}
17377
17378/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017379 * "winrestview()" function
17380 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017381 static void
17382f_winrestview(argvars, rettv)
17383 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017384 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017385{
17386 dict_T *dict;
17387
17388 if (argvars[0].v_type != VAR_DICT
17389 || (dict = argvars[0].vval.v_dict) == NULL)
17390 EMSG(_(e_invarg));
17391 else
17392 {
17393 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17394 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17395#ifdef FEAT_VIRTUALEDIT
17396 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17397#endif
17398 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017399 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017400
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017401 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017402#ifdef FEAT_DIFF
17403 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17404#endif
17405 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17406 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17407
17408 check_cursor();
17409 changed_cline_bef_curs();
17410 invalidate_botline();
17411 redraw_later(VALID);
17412
17413 if (curwin->w_topline == 0)
17414 curwin->w_topline = 1;
17415 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17416 curwin->w_topline = curbuf->b_ml.ml_line_count;
17417#ifdef FEAT_DIFF
17418 check_topfill(curwin, TRUE);
17419#endif
17420 }
17421}
17422
17423/*
17424 * "winsaveview()" function
17425 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017426 static void
17427f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017428 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017429 typval_T *rettv;
17430{
17431 dict_T *dict;
17432
17433 dict = dict_alloc();
17434 if (dict == NULL)
17435 return;
17436 rettv->v_type = VAR_DICT;
17437 rettv->vval.v_dict = dict;
17438 ++dict->dv_refcount;
17439
17440 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17441 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17442#ifdef FEAT_VIRTUALEDIT
17443 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17444#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017445 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017446 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17447
17448 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17449#ifdef FEAT_DIFF
17450 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17451#endif
17452 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17453 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17454}
17455
17456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 * "winwidth(nr)" function
17458 */
17459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017460f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 typval_T *argvars;
17462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463{
17464 win_T *wp;
17465
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017466 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017468 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469 else
17470#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017471 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017473 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474#endif
17475}
17476
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017478 * "writefile()" function
17479 */
17480 static void
17481f_writefile(argvars, rettv)
17482 typval_T *argvars;
17483 typval_T *rettv;
17484{
17485 int binary = FALSE;
17486 char_u *fname;
17487 FILE *fd;
17488 listitem_T *li;
17489 char_u *s;
17490 int ret = 0;
17491 int c;
17492
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017493 if (check_restricted() || check_secure())
17494 return;
17495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017496 if (argvars[0].v_type != VAR_LIST)
17497 {
17498 EMSG2(_(e_listarg), "writefile()");
17499 return;
17500 }
17501 if (argvars[0].vval.v_list == NULL)
17502 return;
17503
17504 if (argvars[2].v_type != VAR_UNKNOWN
17505 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17506 binary = TRUE;
17507
17508 /* Always open the file in binary mode, library functions have a mind of
17509 * their own about CR-LF conversion. */
17510 fname = get_tv_string(&argvars[1]);
17511 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17512 {
17513 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17514 ret = -1;
17515 }
17516 else
17517 {
17518 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17519 li = li->li_next)
17520 {
17521 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17522 {
17523 if (*s == '\n')
17524 c = putc(NUL, fd);
17525 else
17526 c = putc(*s, fd);
17527 if (c == EOF)
17528 {
17529 ret = -1;
17530 break;
17531 }
17532 }
17533 if (!binary || li->li_next != NULL)
17534 if (putc('\n', fd) == EOF)
17535 {
17536 ret = -1;
17537 break;
17538 }
17539 if (ret < 0)
17540 {
17541 EMSG(_(e_write));
17542 break;
17543 }
17544 }
17545 fclose(fd);
17546 }
17547
17548 rettv->vval.v_number = ret;
17549}
17550
17551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017553 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 */
17555 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017556var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017557 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017558 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017559 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017561 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017563 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564
Bram Moolenaara5525202006-03-02 22:52:09 +000017565 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017566 if (varp->v_type == VAR_LIST)
17567 {
17568 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017569 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017570 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017571 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017572
17573 l = varp->vval.v_list;
17574 if (l == NULL)
17575 return NULL;
17576
17577 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017578 pos.lnum = list_find_nr(l, 0L, &error);
17579 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017580 return NULL; /* invalid line number */
17581
17582 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017583 pos.col = list_find_nr(l, 1L, &error);
17584 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017585 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017586 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017587
17588 /* We accept "$" for the column number: last column. */
17589 li = list_find(l, 1L);
17590 if (li != NULL && li->li_tv.v_type == VAR_STRING
17591 && li->li_tv.vval.v_string != NULL
17592 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17593 pos.col = len + 1;
17594
Bram Moolenaara5525202006-03-02 22:52:09 +000017595 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017596 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017597 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017598 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017599
Bram Moolenaara5525202006-03-02 22:52:09 +000017600#ifdef FEAT_VIRTUALEDIT
17601 /* Get the virtual offset. Defaults to zero. */
17602 pos.coladd = list_find_nr(l, 2L, &error);
17603 if (error)
17604 pos.coladd = 0;
17605#endif
17606
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017607 return &pos;
17608 }
17609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017610 name = get_tv_string_chk(varp);
17611 if (name == NULL)
17612 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017613 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017615#ifdef FEAT_VISUAL
17616 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17617 {
17618 if (VIsual_active)
17619 return &VIsual;
17620 return &curwin->w_cursor;
17621 }
17622#endif
17623 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017625 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17627 return NULL;
17628 return pp;
17629 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017630
17631#ifdef FEAT_VIRTUALEDIT
17632 pos.coladd = 0;
17633#endif
17634
Bram Moolenaar477933c2007-07-17 14:32:23 +000017635 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017636 {
17637 pos.col = 0;
17638 if (name[1] == '0') /* "w0": first visible line */
17639 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017640 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017641 pos.lnum = curwin->w_topline;
17642 return &pos;
17643 }
17644 else if (name[1] == '$') /* "w$": last visible line */
17645 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017646 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017647 pos.lnum = curwin->w_botline - 1;
17648 return &pos;
17649 }
17650 }
17651 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017653 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 {
17655 pos.lnum = curbuf->b_ml.ml_line_count;
17656 pos.col = 0;
17657 }
17658 else
17659 {
17660 pos.lnum = curwin->w_cursor.lnum;
17661 pos.col = (colnr_T)STRLEN(ml_get_curline());
17662 }
17663 return &pos;
17664 }
17665 return NULL;
17666}
17667
17668/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017669 * Convert list in "arg" into a position and optional file number.
17670 * When "fnump" is NULL there is no file number, only 3 items.
17671 * Note that the column is passed on as-is, the caller may want to decrement
17672 * it to use 1 for the first column.
17673 * Return FAIL when conversion is not possible, doesn't check the position for
17674 * validity.
17675 */
17676 static int
17677list2fpos(arg, posp, fnump)
17678 typval_T *arg;
17679 pos_T *posp;
17680 int *fnump;
17681{
17682 list_T *l = arg->vval.v_list;
17683 long i = 0;
17684 long n;
17685
Bram Moolenaarbde35262006-07-23 20:12:24 +000017686 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17687 * when "fnump" isn't NULL and "coladd" is optional. */
17688 if (arg->v_type != VAR_LIST
17689 || l == NULL
17690 || l->lv_len < (fnump == NULL ? 2 : 3)
17691 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017692 return FAIL;
17693
17694 if (fnump != NULL)
17695 {
17696 n = list_find_nr(l, i++, NULL); /* fnum */
17697 if (n < 0)
17698 return FAIL;
17699 if (n == 0)
17700 n = curbuf->b_fnum; /* current buffer */
17701 *fnump = n;
17702 }
17703
17704 n = list_find_nr(l, i++, NULL); /* lnum */
17705 if (n < 0)
17706 return FAIL;
17707 posp->lnum = n;
17708
17709 n = list_find_nr(l, i++, NULL); /* col */
17710 if (n < 0)
17711 return FAIL;
17712 posp->col = n;
17713
17714#ifdef FEAT_VIRTUALEDIT
17715 n = list_find_nr(l, i, NULL);
17716 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017717 posp->coladd = 0;
17718 else
17719 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017720#endif
17721
17722 return OK;
17723}
17724
17725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 * Get the length of an environment variable name.
17727 * Advance "arg" to the first character after the name.
17728 * Return 0 for error.
17729 */
17730 static int
17731get_env_len(arg)
17732 char_u **arg;
17733{
17734 char_u *p;
17735 int len;
17736
17737 for (p = *arg; vim_isIDc(*p); ++p)
17738 ;
17739 if (p == *arg) /* no name found */
17740 return 0;
17741
17742 len = (int)(p - *arg);
17743 *arg = p;
17744 return len;
17745}
17746
17747/*
17748 * Get the length of the name of a function or internal variable.
17749 * "arg" is advanced to the first non-white character after the name.
17750 * Return 0 if something is wrong.
17751 */
17752 static int
17753get_id_len(arg)
17754 char_u **arg;
17755{
17756 char_u *p;
17757 int len;
17758
17759 /* Find the end of the name. */
17760 for (p = *arg; eval_isnamec(*p); ++p)
17761 ;
17762 if (p == *arg) /* no name found */
17763 return 0;
17764
17765 len = (int)(p - *arg);
17766 *arg = skipwhite(p);
17767
17768 return len;
17769}
17770
17771/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017772 * Get the length of the name of a variable or function.
17773 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017775 * Return -1 if curly braces expansion failed.
17776 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 * If the name contains 'magic' {}'s, expand them and return the
17778 * expanded name in an allocated string via 'alias' - caller must free.
17779 */
17780 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017781get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 char_u **arg;
17783 char_u **alias;
17784 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017785 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786{
17787 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 char_u *p;
17789 char_u *expr_start;
17790 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791
17792 *alias = NULL; /* default to no alias */
17793
17794 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17795 && (*arg)[2] == (int)KE_SNR)
17796 {
17797 /* hard coded <SNR>, already translated */
17798 *arg += 3;
17799 return get_id_len(arg) + 3;
17800 }
17801 len = eval_fname_script(*arg);
17802 if (len > 0)
17803 {
17804 /* literal "<SID>", "s:" or "<SNR>" */
17805 *arg += len;
17806 }
17807
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017809 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017811 p = find_name_end(*arg, &expr_start, &expr_end,
17812 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813 if (expr_start != NULL)
17814 {
17815 char_u *temp_string;
17816
17817 if (!evaluate)
17818 {
17819 len += (int)(p - *arg);
17820 *arg = skipwhite(p);
17821 return len;
17822 }
17823
17824 /*
17825 * Include any <SID> etc in the expanded string:
17826 * Thus the -len here.
17827 */
17828 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17829 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017830 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 *alias = temp_string;
17832 *arg = skipwhite(p);
17833 return (int)STRLEN(temp_string);
17834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835
17836 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017837 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 EMSG2(_(e_invexpr2), *arg);
17839
17840 return len;
17841}
17842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017843/*
17844 * Find the end of a variable or function name, taking care of magic braces.
17845 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17846 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017847 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017848 * Return a pointer to just after the name. Equal to "arg" if there is no
17849 * valid name.
17850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017852find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853 char_u *arg;
17854 char_u **expr_start;
17855 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017856 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017858 int mb_nest = 0;
17859 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 char_u *p;
17861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017862 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017864 *expr_start = NULL;
17865 *expr_end = NULL;
17866 }
17867
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017868 /* Quick check for valid starting character. */
17869 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17870 return arg;
17871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017872 for (p = arg; *p != NUL
17873 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017874 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017875 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017876 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017877 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017878 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017879 if (*p == '\'')
17880 {
17881 /* skip over 'string' to avoid counting [ and ] inside it. */
17882 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17883 ;
17884 if (*p == NUL)
17885 break;
17886 }
17887 else if (*p == '"')
17888 {
17889 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17890 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17891 if (*p == '\\' && p[1] != NUL)
17892 ++p;
17893 if (*p == NUL)
17894 break;
17895 }
17896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017897 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017899 if (*p == '[')
17900 ++br_nest;
17901 else if (*p == ']')
17902 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017905 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017907 if (*p == '{')
17908 {
17909 mb_nest++;
17910 if (expr_start != NULL && *expr_start == NULL)
17911 *expr_start = p;
17912 }
17913 else if (*p == '}')
17914 {
17915 mb_nest--;
17916 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17917 *expr_end = p;
17918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 }
17921
17922 return p;
17923}
17924
17925/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017926 * Expands out the 'magic' {}'s in a variable/function name.
17927 * Note that this can call itself recursively, to deal with
17928 * constructs like foo{bar}{baz}{bam}
17929 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17930 * "in_start" ^
17931 * "expr_start" ^
17932 * "expr_end" ^
17933 * "in_end" ^
17934 *
17935 * Returns a new allocated string, which the caller must free.
17936 * Returns NULL for failure.
17937 */
17938 static char_u *
17939make_expanded_name(in_start, expr_start, expr_end, in_end)
17940 char_u *in_start;
17941 char_u *expr_start;
17942 char_u *expr_end;
17943 char_u *in_end;
17944{
17945 char_u c1;
17946 char_u *retval = NULL;
17947 char_u *temp_result;
17948 char_u *nextcmd = NULL;
17949
17950 if (expr_end == NULL || in_end == NULL)
17951 return NULL;
17952 *expr_start = NUL;
17953 *expr_end = NUL;
17954 c1 = *in_end;
17955 *in_end = NUL;
17956
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017957 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017958 if (temp_result != NULL && nextcmd == NULL)
17959 {
17960 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17961 + (in_end - expr_end) + 1));
17962 if (retval != NULL)
17963 {
17964 STRCPY(retval, in_start);
17965 STRCAT(retval, temp_result);
17966 STRCAT(retval, expr_end + 1);
17967 }
17968 }
17969 vim_free(temp_result);
17970
17971 *in_end = c1; /* put char back for error messages */
17972 *expr_start = '{';
17973 *expr_end = '}';
17974
17975 if (retval != NULL)
17976 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017977 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017978 if (expr_start != NULL)
17979 {
17980 /* Further expansion! */
17981 temp_result = make_expanded_name(retval, expr_start,
17982 expr_end, temp_result);
17983 vim_free(retval);
17984 retval = temp_result;
17985 }
17986 }
17987
17988 return retval;
17989}
17990
17991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017993 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 */
17995 static int
17996eval_isnamec(c)
17997 int c;
17998{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017999 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18000}
18001
18002/*
18003 * Return TRUE if character "c" can be used as the first character in a
18004 * variable or function name (excluding '{' and '}').
18005 */
18006 static int
18007eval_isnamec1(c)
18008 int c;
18009{
18010 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011}
18012
18013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 * Set number v: variable to "val".
18015 */
18016 void
18017set_vim_var_nr(idx, val)
18018 int idx;
18019 long val;
18020{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018021 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022}
18023
18024/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018025 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 */
18027 long
18028get_vim_var_nr(idx)
18029 int idx;
18030{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018031 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032}
18033
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018034/*
18035 * Get string v: variable value. Uses a static buffer, can only be used once.
18036 */
18037 char_u *
18038get_vim_var_str(idx)
18039 int idx;
18040{
18041 return get_tv_string(&vimvars[idx].vv_tv);
18042}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018043
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018045 * Get List v: variable value. Caller must take care of reference count when
18046 * needed.
18047 */
18048 list_T *
18049get_vim_var_list(idx)
18050 int idx;
18051{
18052 return vimvars[idx].vv_list;
18053}
18054
18055/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018056 * Set v:count to "count" and v:count1 to "count1".
18057 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 */
18059 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018060set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 long count;
18062 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018063 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018065 if (set_prevcount)
18066 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018067 vimvars[VV_COUNT].vv_nr = count;
18068 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069}
18070
18071/*
18072 * Set string v: variable to a copy of "val".
18073 */
18074 void
18075set_vim_var_string(idx, val, len)
18076 int idx;
18077 char_u *val;
18078 int len; /* length of "val" to use or -1 (whole string) */
18079{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018080 /* Need to do this (at least) once, since we can't initialize a union.
18081 * Will always be invoked when "v:progname" is set. */
18082 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18083
Bram Moolenaare9a41262005-01-15 22:18:47 +000018084 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018086 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018088 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018090 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091}
18092
18093/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018094 * Set List v: variable to "val".
18095 */
18096 void
18097set_vim_var_list(idx, val)
18098 int idx;
18099 list_T *val;
18100{
18101 list_unref(vimvars[idx].vv_list);
18102 vimvars[idx].vv_list = val;
18103 if (val != NULL)
18104 ++val->lv_refcount;
18105}
18106
18107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 * Set v:register if needed.
18109 */
18110 void
18111set_reg_var(c)
18112 int c;
18113{
18114 char_u regname;
18115
18116 if (c == 0 || c == ' ')
18117 regname = '"';
18118 else
18119 regname = c;
18120 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018121 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 set_vim_var_string(VV_REG, &regname, 1);
18123}
18124
18125/*
18126 * Get or set v:exception. If "oldval" == NULL, return the current value.
18127 * Otherwise, restore the value to "oldval" and return NULL.
18128 * Must always be called in pairs to save and restore v:exception! Does not
18129 * take care of memory allocations.
18130 */
18131 char_u *
18132v_exception(oldval)
18133 char_u *oldval;
18134{
18135 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018136 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137
Bram Moolenaare9a41262005-01-15 22:18:47 +000018138 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 return NULL;
18140}
18141
18142/*
18143 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18144 * Otherwise, restore the value to "oldval" and return NULL.
18145 * Must always be called in pairs to save and restore v:throwpoint! Does not
18146 * take care of memory allocations.
18147 */
18148 char_u *
18149v_throwpoint(oldval)
18150 char_u *oldval;
18151{
18152 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018153 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154
Bram Moolenaare9a41262005-01-15 22:18:47 +000018155 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156 return NULL;
18157}
18158
18159#if defined(FEAT_AUTOCMD) || defined(PROTO)
18160/*
18161 * Set v:cmdarg.
18162 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18163 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18164 * Must always be called in pairs!
18165 */
18166 char_u *
18167set_cmdarg(eap, oldarg)
18168 exarg_T *eap;
18169 char_u *oldarg;
18170{
18171 char_u *oldval;
18172 char_u *newval;
18173 unsigned len;
18174
Bram Moolenaare9a41262005-01-15 22:18:47 +000018175 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018176 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018178 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018179 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018180 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 }
18182
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018183 if (eap->force_bin == FORCE_BIN)
18184 len = 6;
18185 else if (eap->force_bin == FORCE_NOBIN)
18186 len = 8;
18187 else
18188 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018189
18190 if (eap->read_edit)
18191 len += 7;
18192
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018193 if (eap->force_ff != 0)
18194 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18195# ifdef FEAT_MBYTE
18196 if (eap->force_enc != 0)
18197 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018198 if (eap->bad_char != 0)
18199 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018200# endif
18201
18202 newval = alloc(len + 1);
18203 if (newval == NULL)
18204 return NULL;
18205
18206 if (eap->force_bin == FORCE_BIN)
18207 sprintf((char *)newval, " ++bin");
18208 else if (eap->force_bin == FORCE_NOBIN)
18209 sprintf((char *)newval, " ++nobin");
18210 else
18211 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018212
18213 if (eap->read_edit)
18214 STRCAT(newval, " ++edit");
18215
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018216 if (eap->force_ff != 0)
18217 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18218 eap->cmd + eap->force_ff);
18219# ifdef FEAT_MBYTE
18220 if (eap->force_enc != 0)
18221 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18222 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018223 if (eap->bad_char != 0)
18224 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18225 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018226# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018227 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018228 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229}
18230#endif
18231
18232/*
18233 * Get the value of internal variable "name".
18234 * Return OK or FAIL.
18235 */
18236 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018237get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 char_u *name;
18239 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018240 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018241 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242{
18243 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018244 typval_T *tv = NULL;
18245 typval_T atv;
18246 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248
18249 /* truncate the name, so that we can use strcmp() */
18250 cc = name[len];
18251 name[len] = NUL;
18252
18253 /*
18254 * Check for "b:changedtick".
18255 */
18256 if (STRCMP(name, "b:changedtick") == 0)
18257 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018258 atv.v_type = VAR_NUMBER;
18259 atv.vval.v_number = curbuf->b_changedtick;
18260 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 }
18262
18263 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 * Check for user-defined variables.
18265 */
18266 else
18267 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018268 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018270 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 }
18272
Bram Moolenaare9a41262005-01-15 22:18:47 +000018273 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018275 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018276 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277 ret = FAIL;
18278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018279 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018280 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281
18282 name[len] = cc;
18283
18284 return ret;
18285}
18286
18287/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018288 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18289 * Also handle function call with Funcref variable: func(expr)
18290 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18291 */
18292 static int
18293handle_subscript(arg, rettv, evaluate, verbose)
18294 char_u **arg;
18295 typval_T *rettv;
18296 int evaluate; /* do more than finding the end */
18297 int verbose; /* give error messages */
18298{
18299 int ret = OK;
18300 dict_T *selfdict = NULL;
18301 char_u *s;
18302 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018303 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018304
18305 while (ret == OK
18306 && (**arg == '['
18307 || (**arg == '.' && rettv->v_type == VAR_DICT)
18308 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18309 && !vim_iswhite(*(*arg - 1)))
18310 {
18311 if (**arg == '(')
18312 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018313 /* need to copy the funcref so that we can clear rettv */
18314 functv = *rettv;
18315 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018316
18317 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018318 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018319 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018320 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18321 &len, evaluate, selfdict);
18322
18323 /* Clear the funcref afterwards, so that deleting it while
18324 * evaluating the arguments is possible (see test55). */
18325 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018326
18327 /* Stop the expression evaluation when immediately aborting on
18328 * error, or when an interrupt occurred or an exception was thrown
18329 * but not caught. */
18330 if (aborting())
18331 {
18332 if (ret == OK)
18333 clear_tv(rettv);
18334 ret = FAIL;
18335 }
18336 dict_unref(selfdict);
18337 selfdict = NULL;
18338 }
18339 else /* **arg == '[' || **arg == '.' */
18340 {
18341 dict_unref(selfdict);
18342 if (rettv->v_type == VAR_DICT)
18343 {
18344 selfdict = rettv->vval.v_dict;
18345 if (selfdict != NULL)
18346 ++selfdict->dv_refcount;
18347 }
18348 else
18349 selfdict = NULL;
18350 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18351 {
18352 clear_tv(rettv);
18353 ret = FAIL;
18354 }
18355 }
18356 }
18357 dict_unref(selfdict);
18358 return ret;
18359}
18360
18361/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018362 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018363 * value).
18364 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018365 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018366alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018367{
Bram Moolenaar33570922005-01-25 22:26:29 +000018368 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018369}
18370
18371/*
18372 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 * The string "s" must have been allocated, it is consumed.
18374 * Return NULL for out of memory, the variable otherwise.
18375 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018376 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018377alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 char_u *s;
18379{
Bram Moolenaar33570922005-01-25 22:26:29 +000018380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018382 rettv = alloc_tv();
18383 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018385 rettv->v_type = VAR_STRING;
18386 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 }
18388 else
18389 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018390 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391}
18392
18393/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018394 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018396 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018397free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018398 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399{
18400 if (varp != NULL)
18401 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018402 switch (varp->v_type)
18403 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018404 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018405 func_unref(varp->vval.v_string);
18406 /*FALLTHROUGH*/
18407 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018408 vim_free(varp->vval.v_string);
18409 break;
18410 case VAR_LIST:
18411 list_unref(varp->vval.v_list);
18412 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018413 case VAR_DICT:
18414 dict_unref(varp->vval.v_dict);
18415 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018416 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018417#ifdef FEAT_FLOAT
18418 case VAR_FLOAT:
18419#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018420 case VAR_UNKNOWN:
18421 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018422 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018423 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018424 break;
18425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426 vim_free(varp);
18427 }
18428}
18429
18430/*
18431 * Free the memory for a variable value and set the value to NULL or 0.
18432 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018433 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018434clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018435 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436{
18437 if (varp != NULL)
18438 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018439 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018441 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018442 func_unref(varp->vval.v_string);
18443 /*FALLTHROUGH*/
18444 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018445 vim_free(varp->vval.v_string);
18446 varp->vval.v_string = NULL;
18447 break;
18448 case VAR_LIST:
18449 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018450 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018451 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018452 case VAR_DICT:
18453 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018454 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018455 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018456 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018457 varp->vval.v_number = 0;
18458 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018459#ifdef FEAT_FLOAT
18460 case VAR_FLOAT:
18461 varp->vval.v_float = 0.0;
18462 break;
18463#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018464 case VAR_UNKNOWN:
18465 break;
18466 default:
18467 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018469 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 }
18471}
18472
18473/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018474 * Set the value of a variable to NULL without freeing items.
18475 */
18476 static void
18477init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018478 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479{
18480 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018481 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018482}
18483
18484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 * Get the number value of a variable.
18486 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018487 * For incompatible types, return 0.
18488 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18489 * caller of incompatible types: it sets *denote to TRUE if "denote"
18490 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 */
18492 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018493get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018494 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018496 int error = FALSE;
18497
18498 return get_tv_number_chk(varp, &error); /* return 0L on error */
18499}
18500
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018501 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018502get_tv_number_chk(varp, denote)
18503 typval_T *varp;
18504 int *denote;
18505{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018506 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018508 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018510 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018511 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018512#ifdef FEAT_FLOAT
18513 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018514 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018515 break;
18516#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018517 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018518 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018519 break;
18520 case VAR_STRING:
18521 if (varp->vval.v_string != NULL)
18522 vim_str2nr(varp->vval.v_string, NULL, NULL,
18523 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018524 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018525 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018526 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018527 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018528 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018529 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018530 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018531 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018532 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018533 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018535 if (denote == NULL) /* useful for values that must be unsigned */
18536 n = -1;
18537 else
18538 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018539 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540}
18541
18542/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018543 * Get the lnum from the first argument.
18544 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018545 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 */
18547 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018548get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018549 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550{
Bram Moolenaar33570922005-01-25 22:26:29 +000018551 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 linenr_T lnum;
18553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018554 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 if (lnum == 0) /* no valid number, try using line() */
18556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018557 rettv.v_type = VAR_NUMBER;
18558 f_line(argvars, &rettv);
18559 lnum = rettv.vval.v_number;
18560 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 }
18562 return lnum;
18563}
18564
18565/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018566 * Get the lnum from the first argument.
18567 * Also accepts "$", then "buf" is used.
18568 * Returns 0 on error.
18569 */
18570 static linenr_T
18571get_tv_lnum_buf(argvars, buf)
18572 typval_T *argvars;
18573 buf_T *buf;
18574{
18575 if (argvars[0].v_type == VAR_STRING
18576 && argvars[0].vval.v_string != NULL
18577 && argvars[0].vval.v_string[0] == '$'
18578 && buf != NULL)
18579 return buf->b_ml.ml_line_count;
18580 return get_tv_number_chk(&argvars[0], NULL);
18581}
18582
18583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 * Get the string value of a variable.
18585 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018586 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18587 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 * If the String variable has never been set, return an empty string.
18589 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018590 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18591 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 */
18593 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018595 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596{
18597 static char_u mybuf[NUMBUFLEN];
18598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018599 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600}
18601
18602 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018603get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018604 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018605 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018607 char_u *res = get_tv_string_buf_chk(varp, buf);
18608
18609 return res != NULL ? res : (char_u *)"";
18610}
18611
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018612 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018613get_tv_string_chk(varp)
18614 typval_T *varp;
18615{
18616 static char_u mybuf[NUMBUFLEN];
18617
18618 return get_tv_string_buf_chk(varp, mybuf);
18619}
18620
18621 static char_u *
18622get_tv_string_buf_chk(varp, buf)
18623 typval_T *varp;
18624 char_u *buf;
18625{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018626 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018628 case VAR_NUMBER:
18629 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18630 return buf;
18631 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018632 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018633 break;
18634 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018635 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018636 break;
18637 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018638 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018639 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018640#ifdef FEAT_FLOAT
18641 case VAR_FLOAT:
18642 EMSG(_("E806: using Float as a String"));
18643 break;
18644#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018645 case VAR_STRING:
18646 if (varp->vval.v_string != NULL)
18647 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018648 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018649 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018650 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018651 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018653 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654}
18655
18656/*
18657 * Find variable "name" in the list of variables.
18658 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018659 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018660 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018661 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018663 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018664find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018669 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670
Bram Moolenaara7043832005-01-21 11:56:39 +000018671 ht = find_var_ht(name, &varname);
18672 if (htp != NULL)
18673 *htp = ht;
18674 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018676 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677}
18678
18679/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018680 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018681 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018683 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018684find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018685 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018686 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018687 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018688{
Bram Moolenaar33570922005-01-25 22:26:29 +000018689 hashitem_T *hi;
18690
18691 if (*varname == NUL)
18692 {
18693 /* Must be something like "s:", otherwise "ht" would be NULL. */
18694 switch (varname[-2])
18695 {
18696 case 's': return &SCRIPT_SV(current_SID).sv_var;
18697 case 'g': return &globvars_var;
18698 case 'v': return &vimvars_var;
18699 case 'b': return &curbuf->b_bufvar;
18700 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018701#ifdef FEAT_WINDOWS
18702 case 't': return &curtab->tp_winvar;
18703#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018704 case 'l': return current_funccal == NULL
18705 ? NULL : &current_funccal->l_vars_var;
18706 case 'a': return current_funccal == NULL
18707 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018708 }
18709 return NULL;
18710 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018711
18712 hi = hash_find(ht, varname);
18713 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018714 {
18715 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018716 * worked find the variable again. Don't auto-load a script if it was
18717 * loaded already, otherwise it would be loaded every time when
18718 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018719 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018720 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018721 hi = hash_find(ht, varname);
18722 if (HASHITEM_EMPTY(hi))
18723 return NULL;
18724 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018725 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018726}
18727
18728/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018729 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018730 * Set "varname" to the start of name without ':'.
18731 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018732 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018733find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 char_u *name;
18735 char_u **varname;
18736{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018737 hashitem_T *hi;
18738
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 if (name[1] != ':')
18740 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018741 /* The name must not start with a colon or #. */
18742 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 return NULL;
18744 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018745
18746 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018747 hi = hash_find(&compat_hashtab, name);
18748 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018749 return &compat_hashtab;
18750
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018752 return &globvarht; /* global variable */
18753 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 }
18755 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018756 if (*name == 'g') /* global variable */
18757 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018758 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18759 */
18760 if (vim_strchr(name + 2, ':') != NULL
18761 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018762 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018764 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018766 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018767#ifdef FEAT_WINDOWS
18768 if (*name == 't') /* tab page variable */
18769 return &curtab->tp_vars.dv_hashtab;
18770#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 if (*name == 'v') /* v: variable */
18772 return &vimvarht;
18773 if (*name == 'a' && current_funccal != NULL) /* function argument */
18774 return &current_funccal->l_avars.dv_hashtab;
18775 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18776 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 if (*name == 's' /* script variable */
18778 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18779 return &SCRIPT_VARS(current_SID);
18780 return NULL;
18781}
18782
18783/*
18784 * Get the string value of a (global/local) variable.
18785 * Returns NULL when it doesn't exist.
18786 */
18787 char_u *
18788get_var_value(name)
18789 char_u *name;
18790{
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792
Bram Moolenaara7043832005-01-21 11:56:39 +000018793 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 if (v == NULL)
18795 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018796 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797}
18798
18799/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018800 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 * sourcing this script and when executing functions defined in the script.
18802 */
18803 void
18804new_script_vars(id)
18805 scid_T id;
18806{
Bram Moolenaara7043832005-01-21 11:56:39 +000018807 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018808 hashtab_T *ht;
18809 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018810
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18812 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018813 /* Re-allocating ga_data means that an ht_array pointing to
18814 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018815 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018816 for (i = 1; i <= ga_scripts.ga_len; ++i)
18817 {
18818 ht = &SCRIPT_VARS(i);
18819 if (ht->ht_mask == HT_INIT_SIZE - 1)
18820 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018821 sv = &SCRIPT_SV(i);
18822 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018823 }
18824
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 while (ga_scripts.ga_len < id)
18826 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018827 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18828 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830 }
18831 }
18832}
18833
18834/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018835 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18836 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 */
18838 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018839init_var_dict(dict, dict_var)
18840 dict_T *dict;
18841 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842{
Bram Moolenaar33570922005-01-25 22:26:29 +000018843 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000018844 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaar33570922005-01-25 22:26:29 +000018845 dict_var->di_tv.vval.v_dict = dict;
18846 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018847 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018848 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18849 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850}
18851
18852/*
18853 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018854 * Frees all allocated variables and the value they contain.
18855 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 */
18857 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018858vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018859 hashtab_T *ht;
18860{
18861 vars_clear_ext(ht, TRUE);
18862}
18863
18864/*
18865 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18866 */
18867 static void
18868vars_clear_ext(ht, free_val)
18869 hashtab_T *ht;
18870 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871{
Bram Moolenaara7043832005-01-21 11:56:39 +000018872 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018873 hashitem_T *hi;
18874 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875
Bram Moolenaar33570922005-01-25 22:26:29 +000018876 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018877 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018878 for (hi = ht->ht_array; todo > 0; ++hi)
18879 {
18880 if (!HASHITEM_EMPTY(hi))
18881 {
18882 --todo;
18883
Bram Moolenaar33570922005-01-25 22:26:29 +000018884 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018885 * ht_array might change then. hash_clear() takes care of it
18886 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018887 v = HI2DI(hi);
18888 if (free_val)
18889 clear_tv(&v->di_tv);
18890 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18891 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018892 }
18893 }
18894 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018895 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896}
18897
Bram Moolenaara7043832005-01-21 11:56:39 +000018898/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018899 * Delete a variable from hashtab "ht" at item "hi".
18900 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018903delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018904 hashtab_T *ht;
18905 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906{
Bram Moolenaar33570922005-01-25 22:26:29 +000018907 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018908
18909 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018910 clear_tv(&di->di_tv);
18911 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912}
18913
18914/*
18915 * List the value of one internal variable.
18916 */
18917 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018918list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018919 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018921 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018923 char_u *tofree;
18924 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018925 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018926
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018927 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018928 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018929 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018930 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931}
18932
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018934list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 char_u *prefix;
18936 char_u *name;
18937 int type;
18938 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018939 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940{
Bram Moolenaar31859182007-08-14 20:41:13 +000018941 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18942 msg_start();
18943 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 if (name != NULL) /* "a:" vars don't have a name stored */
18945 msg_puts(name);
18946 msg_putchar(' ');
18947 msg_advance(22);
18948 if (type == VAR_NUMBER)
18949 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018950 else if (type == VAR_FUNC)
18951 msg_putchar('*');
18952 else if (type == VAR_LIST)
18953 {
18954 msg_putchar('[');
18955 if (*string == '[')
18956 ++string;
18957 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018958 else if (type == VAR_DICT)
18959 {
18960 msg_putchar('{');
18961 if (*string == '{')
18962 ++string;
18963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964 else
18965 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018966
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018968
18969 if (type == VAR_FUNC)
18970 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018971 if (*first)
18972 {
18973 msg_clr_eos();
18974 *first = FALSE;
18975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976}
18977
18978/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018979 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 * If the variable already exists, the value is updated.
18981 * Otherwise the variable is created.
18982 */
18983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018984set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018986 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018987 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988{
Bram Moolenaar33570922005-01-25 22:26:29 +000018989 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018991 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018992 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018994 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018995 {
18996 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18997 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18998 ? name[2] : name[0]))
18999 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019000 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019001 return;
19002 }
19003 if (function_exists(name))
19004 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019005 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019006 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019007 return;
19008 }
19009 }
19010
Bram Moolenaara7043832005-01-21 11:56:39 +000019011 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019012 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019013 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019014 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019015 return;
19016 }
19017
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019018 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019019 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019021 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019022 if (var_check_ro(v->di_flags, name)
19023 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019024 return;
19025 if (v->di_tv.v_type != tv->v_type
19026 && !((v->di_tv.v_type == VAR_STRING
19027 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019028 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019029 || tv->v_type == VAR_NUMBER))
19030#ifdef FEAT_FLOAT
19031 && !((v->di_tv.v_type == VAR_NUMBER
19032 || v->di_tv.v_type == VAR_FLOAT)
19033 && (tv->v_type == VAR_NUMBER
19034 || tv->v_type == VAR_FLOAT))
19035#endif
19036 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019037 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019038 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019039 return;
19040 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019041
19042 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019043 * Handle setting internal v: variables separately: we don't change
19044 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019045 */
19046 if (ht == &vimvarht)
19047 {
19048 if (v->di_tv.v_type == VAR_STRING)
19049 {
19050 vim_free(v->di_tv.vval.v_string);
19051 if (copy || tv->v_type != VAR_STRING)
19052 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19053 else
19054 {
19055 /* Take over the string to avoid an extra alloc/free. */
19056 v->di_tv.vval.v_string = tv->vval.v_string;
19057 tv->vval.v_string = NULL;
19058 }
19059 }
19060 else if (v->di_tv.v_type != VAR_NUMBER)
19061 EMSG2(_(e_intern2), "set_var()");
19062 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019063 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019064 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019065 if (STRCMP(varname, "searchforward") == 0)
19066 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19067 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019068 return;
19069 }
19070
19071 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 }
19073 else /* add a new variable */
19074 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019075 /* Can't add "v:" variable. */
19076 if (ht == &vimvarht)
19077 {
19078 EMSG2(_(e_illvar), name);
19079 return;
19080 }
19081
Bram Moolenaar92124a32005-06-17 22:03:40 +000019082 /* Make sure the variable name is valid. */
19083 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019084 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19085 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019086 {
19087 EMSG2(_(e_illvar), varname);
19088 return;
19089 }
19090
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019091 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19092 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019093 if (v == NULL)
19094 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019095 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019096 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019098 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 return;
19100 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019101 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019103
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019104 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019105 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019106 else
19107 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019108 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019109 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019110 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112}
19113
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019114/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019115 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019116 * Also give an error message.
19117 */
19118 static int
19119var_check_ro(flags, name)
19120 int flags;
19121 char_u *name;
19122{
19123 if (flags & DI_FLAGS_RO)
19124 {
19125 EMSG2(_(e_readonlyvar), name);
19126 return TRUE;
19127 }
19128 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19129 {
19130 EMSG2(_(e_readonlysbx), name);
19131 return TRUE;
19132 }
19133 return FALSE;
19134}
19135
19136/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019137 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19138 * Also give an error message.
19139 */
19140 static int
19141var_check_fixed(flags, name)
19142 int flags;
19143 char_u *name;
19144{
19145 if (flags & DI_FLAGS_FIX)
19146 {
19147 EMSG2(_("E795: Cannot delete variable %s"), name);
19148 return TRUE;
19149 }
19150 return FALSE;
19151}
19152
19153/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019154 * Return TRUE if typeval "tv" is set to be locked (immutable).
19155 * Also give an error message, using "name".
19156 */
19157 static int
19158tv_check_lock(lock, name)
19159 int lock;
19160 char_u *name;
19161{
19162 if (lock & VAR_LOCKED)
19163 {
19164 EMSG2(_("E741: Value is locked: %s"),
19165 name == NULL ? (char_u *)_("Unknown") : name);
19166 return TRUE;
19167 }
19168 if (lock & VAR_FIXED)
19169 {
19170 EMSG2(_("E742: Cannot change value of %s"),
19171 name == NULL ? (char_u *)_("Unknown") : name);
19172 return TRUE;
19173 }
19174 return FALSE;
19175}
19176
19177/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019179 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019180 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019181 * It is OK for "from" and "to" to point to the same item. This is used to
19182 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019186 typval_T *from;
19187 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019189 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019190 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019191 switch (from->v_type)
19192 {
19193 case VAR_NUMBER:
19194 to->vval.v_number = from->vval.v_number;
19195 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019196#ifdef FEAT_FLOAT
19197 case VAR_FLOAT:
19198 to->vval.v_float = from->vval.v_float;
19199 break;
19200#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019201 case VAR_STRING:
19202 case VAR_FUNC:
19203 if (from->vval.v_string == NULL)
19204 to->vval.v_string = NULL;
19205 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019206 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019207 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019208 if (from->v_type == VAR_FUNC)
19209 func_ref(to->vval.v_string);
19210 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019211 break;
19212 case VAR_LIST:
19213 if (from->vval.v_list == NULL)
19214 to->vval.v_list = NULL;
19215 else
19216 {
19217 to->vval.v_list = from->vval.v_list;
19218 ++to->vval.v_list->lv_refcount;
19219 }
19220 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019221 case VAR_DICT:
19222 if (from->vval.v_dict == NULL)
19223 to->vval.v_dict = NULL;
19224 else
19225 {
19226 to->vval.v_dict = from->vval.v_dict;
19227 ++to->vval.v_dict->dv_refcount;
19228 }
19229 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019230 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019232 break;
19233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234}
19235
19236/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019237 * Make a copy of an item.
19238 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019239 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19240 * reference to an already copied list/dict can be used.
19241 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019242 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019243 static int
19244item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019245 typval_T *from;
19246 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019247 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019248 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019249{
19250 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019251 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019252
Bram Moolenaar33570922005-01-25 22:26:29 +000019253 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019254 {
19255 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019256 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019257 }
19258 ++recurse;
19259
19260 switch (from->v_type)
19261 {
19262 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019263#ifdef FEAT_FLOAT
19264 case VAR_FLOAT:
19265#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019266 case VAR_STRING:
19267 case VAR_FUNC:
19268 copy_tv(from, to);
19269 break;
19270 case VAR_LIST:
19271 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019272 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019273 if (from->vval.v_list == NULL)
19274 to->vval.v_list = NULL;
19275 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19276 {
19277 /* use the copy made earlier */
19278 to->vval.v_list = from->vval.v_list->lv_copylist;
19279 ++to->vval.v_list->lv_refcount;
19280 }
19281 else
19282 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19283 if (to->vval.v_list == NULL)
19284 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019285 break;
19286 case VAR_DICT:
19287 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019288 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019289 if (from->vval.v_dict == NULL)
19290 to->vval.v_dict = NULL;
19291 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19292 {
19293 /* use the copy made earlier */
19294 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19295 ++to->vval.v_dict->dv_refcount;
19296 }
19297 else
19298 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19299 if (to->vval.v_dict == NULL)
19300 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019301 break;
19302 default:
19303 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019304 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019305 }
19306 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019307 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019308}
19309
19310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 * ":echo expr1 ..." print each argument separated with a space, add a
19312 * newline at the end.
19313 * ":echon expr1 ..." print each argument plain.
19314 */
19315 void
19316ex_echo(eap)
19317 exarg_T *eap;
19318{
19319 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019320 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019321 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322 char_u *p;
19323 int needclr = TRUE;
19324 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019325 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326
19327 if (eap->skip)
19328 ++emsg_skip;
19329 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19330 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019331 /* If eval1() causes an error message the text from the command may
19332 * still need to be cleared. E.g., "echo 22,44". */
19333 need_clr_eos = needclr;
19334
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019336 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 {
19338 /*
19339 * Report the invalid expression unless the expression evaluation
19340 * has been cancelled due to an aborting error, an interrupt, or an
19341 * exception.
19342 */
19343 if (!aborting())
19344 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019345 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 break;
19347 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019348 need_clr_eos = FALSE;
19349
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 if (!eap->skip)
19351 {
19352 if (atstart)
19353 {
19354 atstart = FALSE;
19355 /* Call msg_start() after eval1(), evaluating the expression
19356 * may cause a message to appear. */
19357 if (eap->cmdidx == CMD_echo)
19358 msg_start();
19359 }
19360 else if (eap->cmdidx == CMD_echo)
19361 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019362 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019363 if (p != NULL)
19364 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019366 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019368 if (*p != TAB && needclr)
19369 {
19370 /* remove any text still there from the command */
19371 msg_clr_eos();
19372 needclr = FALSE;
19373 }
19374 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375 }
19376 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019377 {
19378#ifdef FEAT_MBYTE
19379 if (has_mbyte)
19380 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019381 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019382
19383 (void)msg_outtrans_len_attr(p, i, echo_attr);
19384 p += i - 1;
19385 }
19386 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019388 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019391 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019393 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 arg = skipwhite(arg);
19395 }
19396 eap->nextcmd = check_nextcmd(arg);
19397
19398 if (eap->skip)
19399 --emsg_skip;
19400 else
19401 {
19402 /* remove text that may still be there from the command */
19403 if (needclr)
19404 msg_clr_eos();
19405 if (eap->cmdidx == CMD_echo)
19406 msg_end();
19407 }
19408}
19409
19410/*
19411 * ":echohl {name}".
19412 */
19413 void
19414ex_echohl(eap)
19415 exarg_T *eap;
19416{
19417 int id;
19418
19419 id = syn_name2id(eap->arg);
19420 if (id == 0)
19421 echo_attr = 0;
19422 else
19423 echo_attr = syn_id2attr(id);
19424}
19425
19426/*
19427 * ":execute expr1 ..." execute the result of an expression.
19428 * ":echomsg expr1 ..." Print a message
19429 * ":echoerr expr1 ..." Print an error
19430 * Each gets spaces around each argument and a newline at the end for
19431 * echo commands
19432 */
19433 void
19434ex_execute(eap)
19435 exarg_T *eap;
19436{
19437 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019438 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 int ret = OK;
19440 char_u *p;
19441 garray_T ga;
19442 int len;
19443 int save_did_emsg;
19444
19445 ga_init2(&ga, 1, 80);
19446
19447 if (eap->skip)
19448 ++emsg_skip;
19449 while (*arg != NUL && *arg != '|' && *arg != '\n')
19450 {
19451 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019452 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 {
19454 /*
19455 * Report the invalid expression unless the expression evaluation
19456 * has been cancelled due to an aborting error, an interrupt, or an
19457 * exception.
19458 */
19459 if (!aborting())
19460 EMSG2(_(e_invexpr2), p);
19461 ret = FAIL;
19462 break;
19463 }
19464
19465 if (!eap->skip)
19466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019467 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 len = (int)STRLEN(p);
19469 if (ga_grow(&ga, len + 2) == FAIL)
19470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019471 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 ret = FAIL;
19473 break;
19474 }
19475 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 ga.ga_len += len;
19479 }
19480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 arg = skipwhite(arg);
19483 }
19484
19485 if (ret != FAIL && ga.ga_data != NULL)
19486 {
19487 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019490 out_flush();
19491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 else if (eap->cmdidx == CMD_echoerr)
19493 {
19494 /* We don't want to abort following commands, restore did_emsg. */
19495 save_did_emsg = did_emsg;
19496 EMSG((char_u *)ga.ga_data);
19497 if (!force_abort)
19498 did_emsg = save_did_emsg;
19499 }
19500 else if (eap->cmdidx == CMD_execute)
19501 do_cmdline((char_u *)ga.ga_data,
19502 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19503 }
19504
19505 ga_clear(&ga);
19506
19507 if (eap->skip)
19508 --emsg_skip;
19509
19510 eap->nextcmd = check_nextcmd(arg);
19511}
19512
19513/*
19514 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19515 * "arg" points to the "&" or '+' when called, to "option" when returning.
19516 * Returns NULL when no option name found. Otherwise pointer to the char
19517 * after the option name.
19518 */
19519 static char_u *
19520find_option_end(arg, opt_flags)
19521 char_u **arg;
19522 int *opt_flags;
19523{
19524 char_u *p = *arg;
19525
19526 ++p;
19527 if (*p == 'g' && p[1] == ':')
19528 {
19529 *opt_flags = OPT_GLOBAL;
19530 p += 2;
19531 }
19532 else if (*p == 'l' && p[1] == ':')
19533 {
19534 *opt_flags = OPT_LOCAL;
19535 p += 2;
19536 }
19537 else
19538 *opt_flags = 0;
19539
19540 if (!ASCII_ISALPHA(*p))
19541 return NULL;
19542 *arg = p;
19543
19544 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19545 p += 4; /* termcap option */
19546 else
19547 while (ASCII_ISALPHA(*p))
19548 ++p;
19549 return p;
19550}
19551
19552/*
19553 * ":function"
19554 */
19555 void
19556ex_function(eap)
19557 exarg_T *eap;
19558{
19559 char_u *theline;
19560 int j;
19561 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 char_u *name = NULL;
19564 char_u *p;
19565 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019566 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 garray_T newargs;
19568 garray_T newlines;
19569 int varargs = FALSE;
19570 int mustend = FALSE;
19571 int flags = 0;
19572 ufunc_T *fp;
19573 int indent;
19574 int nesting;
19575 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019576 dictitem_T *v;
19577 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019578 static int func_nr = 0; /* number for nameless function */
19579 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019580 hashtab_T *ht;
19581 int todo;
19582 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019583 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584
19585 /*
19586 * ":function" without argument: list functions.
19587 */
19588 if (ends_excmd(*eap->arg))
19589 {
19590 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019591 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019592 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019593 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019594 {
19595 if (!HASHITEM_EMPTY(hi))
19596 {
19597 --todo;
19598 fp = HI2UF(hi);
19599 if (!isdigit(*fp->uf_name))
19600 list_func_head(fp, FALSE);
19601 }
19602 }
19603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 eap->nextcmd = check_nextcmd(eap->arg);
19605 return;
19606 }
19607
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019608 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019609 * ":function /pat": list functions matching pattern.
19610 */
19611 if (*eap->arg == '/')
19612 {
19613 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19614 if (!eap->skip)
19615 {
19616 regmatch_T regmatch;
19617
19618 c = *p;
19619 *p = NUL;
19620 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19621 *p = c;
19622 if (regmatch.regprog != NULL)
19623 {
19624 regmatch.rm_ic = p_ic;
19625
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019626 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019627 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19628 {
19629 if (!HASHITEM_EMPTY(hi))
19630 {
19631 --todo;
19632 fp = HI2UF(hi);
19633 if (!isdigit(*fp->uf_name)
19634 && vim_regexec(&regmatch, fp->uf_name, 0))
19635 list_func_head(fp, FALSE);
19636 }
19637 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000019638 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019639 }
19640 }
19641 if (*p == '/')
19642 ++p;
19643 eap->nextcmd = check_nextcmd(p);
19644 return;
19645 }
19646
19647 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019648 * Get the function name. There are these situations:
19649 * func normal function name
19650 * "name" == func, "fudi.fd_dict" == NULL
19651 * dict.func new dictionary entry
19652 * "name" == NULL, "fudi.fd_dict" set,
19653 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19654 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019655 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019656 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19657 * dict.func existing dict entry that's not a Funcref
19658 * "name" == NULL, "fudi.fd_dict" set,
19659 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019662 name = trans_function_name(&p, eap->skip, 0, &fudi);
19663 paren = (vim_strchr(p, '(') != NULL);
19664 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 {
19666 /*
19667 * Return on an invalid expression in braces, unless the expression
19668 * evaluation has been cancelled due to an aborting error, an
19669 * interrupt, or an exception.
19670 */
19671 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019672 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019673 if (!eap->skip && fudi.fd_newkey != NULL)
19674 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019675 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 else
19679 eap->skip = TRUE;
19680 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019681
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682 /* An error in a function call during evaluation of an expression in magic
19683 * braces should not cause the function not to be defined. */
19684 saved_did_emsg = did_emsg;
19685 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686
19687 /*
19688 * ":function func" with only function name: list function.
19689 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019690 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 {
19692 if (!ends_excmd(*skipwhite(p)))
19693 {
19694 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019695 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 }
19697 eap->nextcmd = check_nextcmd(p);
19698 if (eap->nextcmd != NULL)
19699 *p = NUL;
19700 if (!eap->skip && !got_int)
19701 {
19702 fp = find_func(name);
19703 if (fp != NULL)
19704 {
19705 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019706 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019708 if (FUNCLINE(fp, j) == NULL)
19709 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 msg_putchar('\n');
19711 msg_outnum((long)(j + 1));
19712 if (j < 9)
19713 msg_putchar(' ');
19714 if (j < 99)
19715 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019716 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 out_flush(); /* show a line at a time */
19718 ui_breakcheck();
19719 }
19720 if (!got_int)
19721 {
19722 msg_putchar('\n');
19723 msg_puts((char_u *)" endfunction");
19724 }
19725 }
19726 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000019727 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019729 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 }
19731
19732 /*
19733 * ":function name(arg1, arg2)" Define function.
19734 */
19735 p = skipwhite(p);
19736 if (*p != '(')
19737 {
19738 if (!eap->skip)
19739 {
19740 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019741 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 }
19743 /* attempt to continue by skipping some text */
19744 if (vim_strchr(p, '(') != NULL)
19745 p = vim_strchr(p, '(');
19746 }
19747 p = skipwhite(p + 1);
19748
19749 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19750 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19751
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019752 if (!eap->skip)
19753 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019754 /* Check the name of the function. Unless it's a dictionary function
19755 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019756 if (name != NULL)
19757 arg = name;
19758 else
19759 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019760 if (arg != NULL && (fudi.fd_di == NULL
19761 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019762 {
19763 if (*arg == K_SPECIAL)
19764 j = 3;
19765 else
19766 j = 0;
19767 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19768 : eval_isnamec(arg[j])))
19769 ++j;
19770 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000019771 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019772 }
19773 }
19774
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 /*
19776 * Isolate the arguments: "arg1, arg2, ...)"
19777 */
19778 while (*p != ')')
19779 {
19780 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19781 {
19782 varargs = TRUE;
19783 p += 3;
19784 mustend = TRUE;
19785 }
19786 else
19787 {
19788 arg = p;
19789 while (ASCII_ISALNUM(*p) || *p == '_')
19790 ++p;
19791 if (arg == p || isdigit(*arg)
19792 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19793 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19794 {
19795 if (!eap->skip)
19796 EMSG2(_("E125: Illegal argument: %s"), arg);
19797 break;
19798 }
19799 if (ga_grow(&newargs, 1) == FAIL)
19800 goto erret;
19801 c = *p;
19802 *p = NUL;
19803 arg = vim_strsave(arg);
19804 if (arg == NULL)
19805 goto erret;
19806 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19807 *p = c;
19808 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 if (*p == ',')
19810 ++p;
19811 else
19812 mustend = TRUE;
19813 }
19814 p = skipwhite(p);
19815 if (mustend && *p != ')')
19816 {
19817 if (!eap->skip)
19818 EMSG2(_(e_invarg2), eap->arg);
19819 break;
19820 }
19821 }
19822 ++p; /* skip the ')' */
19823
Bram Moolenaare9a41262005-01-15 22:18:47 +000019824 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 for (;;)
19826 {
19827 p = skipwhite(p);
19828 if (STRNCMP(p, "range", 5) == 0)
19829 {
19830 flags |= FC_RANGE;
19831 p += 5;
19832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019833 else if (STRNCMP(p, "dict", 4) == 0)
19834 {
19835 flags |= FC_DICT;
19836 p += 4;
19837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 else if (STRNCMP(p, "abort", 5) == 0)
19839 {
19840 flags |= FC_ABORT;
19841 p += 5;
19842 }
19843 else
19844 break;
19845 }
19846
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019847 /* When there is a line break use what follows for the function body.
19848 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19849 if (*p == '\n')
19850 line_arg = p + 1;
19851 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 EMSG(_(e_trailing));
19853
19854 /*
19855 * Read the body of the function, until ":endfunction" is found.
19856 */
19857 if (KeyTyped)
19858 {
19859 /* Check if the function already exists, don't let the user type the
19860 * whole function before telling him it doesn't work! For a script we
19861 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019862 if (!eap->skip && !eap->forceit)
19863 {
19864 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19865 EMSG(_(e_funcdict));
19866 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019867 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019870 if (!eap->skip && did_emsg)
19871 goto erret;
19872
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873 msg_putchar('\n'); /* don't overwrite the function name */
19874 cmdline_row = msg_row;
19875 }
19876
19877 indent = 2;
19878 nesting = 0;
19879 for (;;)
19880 {
19881 msg_scroll = TRUE;
19882 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019883 sourcing_lnum_off = sourcing_lnum;
19884
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019885 if (line_arg != NULL)
19886 {
19887 /* Use eap->arg, split up in parts by line breaks. */
19888 theline = line_arg;
19889 p = vim_strchr(theline, '\n');
19890 if (p == NULL)
19891 line_arg += STRLEN(line_arg);
19892 else
19893 {
19894 *p = NUL;
19895 line_arg = p + 1;
19896 }
19897 }
19898 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 theline = getcmdline(':', 0L, indent);
19900 else
19901 theline = eap->getline(':', eap->cookie, indent);
19902 if (KeyTyped)
19903 lines_left = Rows - 1;
19904 if (theline == NULL)
19905 {
19906 EMSG(_("E126: Missing :endfunction"));
19907 goto erret;
19908 }
19909
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019910 /* Detect line continuation: sourcing_lnum increased more than one. */
19911 if (sourcing_lnum > sourcing_lnum_off + 1)
19912 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19913 else
19914 sourcing_lnum_off = 0;
19915
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 if (skip_until != NULL)
19917 {
19918 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19919 * don't check for ":endfunc". */
19920 if (STRCMP(theline, skip_until) == 0)
19921 {
19922 vim_free(skip_until);
19923 skip_until = NULL;
19924 }
19925 }
19926 else
19927 {
19928 /* skip ':' and blanks*/
19929 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19930 ;
19931
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019932 /* Check for "endfunction". */
19933 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019935 if (line_arg == NULL)
19936 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 break;
19938 }
19939
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019940 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 * at "end". */
19942 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19943 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019944 else if (STRNCMP(p, "if", 2) == 0
19945 || STRNCMP(p, "wh", 2) == 0
19946 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 || STRNCMP(p, "try", 3) == 0)
19948 indent += 2;
19949
19950 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019951 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019953 if (*p == '!')
19954 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 p += eval_fname_script(p);
19956 if (ASCII_ISALPHA(*p))
19957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019958 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 if (*skipwhite(p) == '(')
19960 {
19961 ++nesting;
19962 indent += 2;
19963 }
19964 }
19965 }
19966
19967 /* Check for ":append" or ":insert". */
19968 p = skip_range(p, NULL);
19969 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19970 || (p[0] == 'i'
19971 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19972 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19973 skip_until = vim_strsave((char_u *)".");
19974
19975 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19976 arg = skipwhite(skiptowhite(p));
19977 if (arg[0] == '<' && arg[1] =='<'
19978 && ((p[0] == 'p' && p[1] == 'y'
19979 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19980 || (p[0] == 'p' && p[1] == 'e'
19981 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19982 || (p[0] == 't' && p[1] == 'c'
19983 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19984 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19985 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019986 || (p[0] == 'm' && p[1] == 'z'
19987 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 ))
19989 {
19990 /* ":python <<" continues until a dot, like ":append" */
19991 p = skipwhite(arg + 2);
19992 if (*p == NUL)
19993 skip_until = vim_strsave((char_u *)".");
19994 else
19995 skip_until = vim_strsave(p);
19996 }
19997 }
19998
19999 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020000 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020001 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020002 if (line_arg == NULL)
20003 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020005 }
20006
20007 /* Copy the line to newly allocated memory. get_one_sourceline()
20008 * allocates 250 bytes per line, this saves 80% on average. The cost
20009 * is an extra alloc/free. */
20010 p = vim_strsave(theline);
20011 if (p != NULL)
20012 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020013 if (line_arg == NULL)
20014 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020015 theline = p;
20016 }
20017
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020018 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20019
20020 /* Add NULL lines for continuation lines, so that the line count is
20021 * equal to the index in the growarray. */
20022 while (sourcing_lnum_off-- > 0)
20023 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020024
20025 /* Check for end of eap->arg. */
20026 if (line_arg != NULL && *line_arg == NUL)
20027 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 }
20029
20030 /* Don't define the function when skipping commands or when an error was
20031 * detected. */
20032 if (eap->skip || did_emsg)
20033 goto erret;
20034
20035 /*
20036 * If there are no errors, add the function
20037 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020038 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020039 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020040 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020042 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020043 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020044 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020045 goto erret;
20046 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020047
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020048 fp = find_func(name);
20049 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020051 if (!eap->forceit)
20052 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020053 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020054 goto erret;
20055 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020056 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020057 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020058 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020059 name);
20060 goto erret;
20061 }
20062 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020063 ga_clear_strings(&(fp->uf_args));
20064 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020065 vim_free(name);
20066 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 }
20069 else
20070 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020071 char numbuf[20];
20072
20073 fp = NULL;
20074 if (fudi.fd_newkey == NULL && !eap->forceit)
20075 {
20076 EMSG(_(e_funcdict));
20077 goto erret;
20078 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020079 if (fudi.fd_di == NULL)
20080 {
20081 /* Can't add a function to a locked dictionary */
20082 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20083 goto erret;
20084 }
20085 /* Can't change an existing function if it is locked */
20086 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20087 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020088
20089 /* Give the function a sequential number. Can only be used with a
20090 * Funcref! */
20091 vim_free(name);
20092 sprintf(numbuf, "%d", ++func_nr);
20093 name = vim_strsave((char_u *)numbuf);
20094 if (name == NULL)
20095 goto erret;
20096 }
20097
20098 if (fp == NULL)
20099 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020100 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020101 {
20102 int slen, plen;
20103 char_u *scriptname;
20104
20105 /* Check that the autoload name matches the script name. */
20106 j = FAIL;
20107 if (sourcing_name != NULL)
20108 {
20109 scriptname = autoload_name(name);
20110 if (scriptname != NULL)
20111 {
20112 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020113 plen = (int)STRLEN(p);
20114 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020115 if (slen > plen && fnamecmp(p,
20116 sourcing_name + slen - plen) == 0)
20117 j = OK;
20118 vim_free(scriptname);
20119 }
20120 }
20121 if (j == FAIL)
20122 {
20123 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20124 goto erret;
20125 }
20126 }
20127
20128 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 if (fp == NULL)
20130 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020131
20132 if (fudi.fd_dict != NULL)
20133 {
20134 if (fudi.fd_di == NULL)
20135 {
20136 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020137 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020138 if (fudi.fd_di == NULL)
20139 {
20140 vim_free(fp);
20141 goto erret;
20142 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020143 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20144 {
20145 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020146 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020147 goto erret;
20148 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020149 }
20150 else
20151 /* overwrite existing dict entry */
20152 clear_tv(&fudi.fd_di->di_tv);
20153 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020154 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020155 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020156 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020157
20158 /* behave like "dict" was used */
20159 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020160 }
20161
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020163 STRCPY(fp->uf_name, name);
20164 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020166 fp->uf_args = newargs;
20167 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020168#ifdef FEAT_PROFILE
20169 fp->uf_tml_count = NULL;
20170 fp->uf_tml_total = NULL;
20171 fp->uf_tml_self = NULL;
20172 fp->uf_profiling = FALSE;
20173 if (prof_def_func())
20174 func_do_profile(fp);
20175#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020176 fp->uf_varargs = varargs;
20177 fp->uf_flags = flags;
20178 fp->uf_calls = 0;
20179 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020180 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181
20182erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 ga_clear_strings(&newargs);
20184 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020185ret_free:
20186 vim_free(skip_until);
20187 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190}
20191
20192/*
20193 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020194 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020196 * flags:
20197 * TFN_INT: internal function name OK
20198 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 * Advances "pp" to just after the function name (if no error).
20200 */
20201 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020202trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 char_u **pp;
20204 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020205 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020206 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020208 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 char_u *start;
20210 char_u *end;
20211 int lead;
20212 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020214 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020215
20216 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020217 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020219
20220 /* Check for hard coded <SNR>: already translated function ID (from a user
20221 * command). */
20222 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20223 && (*pp)[2] == (int)KE_SNR)
20224 {
20225 *pp += 3;
20226 len = get_id_len(pp) + 3;
20227 return vim_strnsave(start, len);
20228 }
20229
20230 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20231 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020233 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020235
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020236 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20237 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020238 if (end == start)
20239 {
20240 if (!skip)
20241 EMSG(_("E129: Function name required"));
20242 goto theend;
20243 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020244 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020245 {
20246 /*
20247 * Report an invalid expression in braces, unless the expression
20248 * evaluation has been cancelled due to an aborting error, an
20249 * interrupt, or an exception.
20250 */
20251 if (!aborting())
20252 {
20253 if (end != NULL)
20254 EMSG2(_(e_invarg2), start);
20255 }
20256 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020257 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020258 goto theend;
20259 }
20260
20261 if (lv.ll_tv != NULL)
20262 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020263 if (fdp != NULL)
20264 {
20265 fdp->fd_dict = lv.ll_dict;
20266 fdp->fd_newkey = lv.ll_newkey;
20267 lv.ll_newkey = NULL;
20268 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020269 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020270 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20271 {
20272 name = vim_strsave(lv.ll_tv->vval.v_string);
20273 *pp = end;
20274 }
20275 else
20276 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020277 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20278 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020279 EMSG(_(e_funcref));
20280 else
20281 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020282 name = NULL;
20283 }
20284 goto theend;
20285 }
20286
20287 if (lv.ll_name == NULL)
20288 {
20289 /* Error found, but continue after the function name. */
20290 *pp = end;
20291 goto theend;
20292 }
20293
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020294 /* Check if the name is a Funcref. If so, use the value. */
20295 if (lv.ll_exp_name != NULL)
20296 {
20297 len = (int)STRLEN(lv.ll_exp_name);
20298 name = deref_func_name(lv.ll_exp_name, &len);
20299 if (name == lv.ll_exp_name)
20300 name = NULL;
20301 }
20302 else
20303 {
20304 len = (int)(end - *pp);
20305 name = deref_func_name(*pp, &len);
20306 if (name == *pp)
20307 name = NULL;
20308 }
20309 if (name != NULL)
20310 {
20311 name = vim_strsave(name);
20312 *pp = end;
20313 goto theend;
20314 }
20315
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020316 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020317 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020318 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020319 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20320 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20321 {
20322 /* When there was "s:" already or the name expanded to get a
20323 * leading "s:" then remove it. */
20324 lv.ll_name += 2;
20325 len -= 2;
20326 lead = 2;
20327 }
20328 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020329 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020330 {
20331 if (lead == 2) /* skip over "s:" */
20332 lv.ll_name += 2;
20333 len = (int)(end - lv.ll_name);
20334 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020335
20336 /*
20337 * Copy the function name to allocated memory.
20338 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20339 * Accept <SNR>123_name() outside a script.
20340 */
20341 if (skip)
20342 lead = 0; /* do nothing */
20343 else if (lead > 0)
20344 {
20345 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020346 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20347 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020348 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020349 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020350 if (current_SID <= 0)
20351 {
20352 EMSG(_(e_usingsid));
20353 goto theend;
20354 }
20355 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20356 lead += (int)STRLEN(sid_buf);
20357 }
20358 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020359 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020360 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020361 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020362 goto theend;
20363 }
20364 name = alloc((unsigned)(len + lead + 1));
20365 if (name != NULL)
20366 {
20367 if (lead > 0)
20368 {
20369 name[0] = K_SPECIAL;
20370 name[1] = KS_EXTRA;
20371 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020372 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020373 STRCPY(name + 3, sid_buf);
20374 }
20375 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20376 name[len + lead] = NUL;
20377 }
20378 *pp = end;
20379
20380theend:
20381 clear_lval(&lv);
20382 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383}
20384
20385/*
20386 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20387 * Return 2 if "p" starts with "s:".
20388 * Return 0 otherwise.
20389 */
20390 static int
20391eval_fname_script(p)
20392 char_u *p;
20393{
20394 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20395 || STRNICMP(p + 1, "SNR>", 4) == 0))
20396 return 5;
20397 if (p[0] == 's' && p[1] == ':')
20398 return 2;
20399 return 0;
20400}
20401
20402/*
20403 * Return TRUE if "p" starts with "<SID>" or "s:".
20404 * Only works if eval_fname_script() returned non-zero for "p"!
20405 */
20406 static int
20407eval_fname_sid(p)
20408 char_u *p;
20409{
20410 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20411}
20412
20413/*
20414 * List the head of the function: "name(arg1, arg2)".
20415 */
20416 static void
20417list_func_head(fp, indent)
20418 ufunc_T *fp;
20419 int indent;
20420{
20421 int j;
20422
20423 msg_start();
20424 if (indent)
20425 MSG_PUTS(" ");
20426 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020427 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 {
20429 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020430 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 }
20432 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020433 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020435 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 {
20437 if (j)
20438 MSG_PUTS(", ");
20439 msg_puts(FUNCARG(fp, j));
20440 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020441 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 {
20443 if (j)
20444 MSG_PUTS(", ");
20445 MSG_PUTS("...");
20446 }
20447 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020448 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020449 if (p_verbose > 0)
20450 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451}
20452
20453/*
20454 * Find a function by name, return pointer to it in ufuncs.
20455 * Return NULL for unknown function.
20456 */
20457 static ufunc_T *
20458find_func(name)
20459 char_u *name;
20460{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020461 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020463 hi = hash_find(&func_hashtab, name);
20464 if (!HASHITEM_EMPTY(hi))
20465 return HI2UF(hi);
20466 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467}
20468
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020469#if defined(EXITFREE) || defined(PROTO)
20470 void
20471free_all_functions()
20472{
20473 hashitem_T *hi;
20474
20475 /* Need to start all over every time, because func_free() may change the
20476 * hash table. */
20477 while (func_hashtab.ht_used > 0)
20478 for (hi = func_hashtab.ht_array; ; ++hi)
20479 if (!HASHITEM_EMPTY(hi))
20480 {
20481 func_free(HI2UF(hi));
20482 break;
20483 }
20484}
20485#endif
20486
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020487/*
20488 * Return TRUE if a function "name" exists.
20489 */
20490 static int
20491function_exists(name)
20492 char_u *name;
20493{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020494 char_u *nm = name;
20495 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020496 int n = FALSE;
20497
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020498 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020499 nm = skipwhite(nm);
20500
20501 /* Only accept "funcname", "funcname ", "funcname (..." and
20502 * "funcname(...", not "funcname!...". */
20503 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020504 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020505 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020506 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020507 else
20508 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020509 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020510 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020511 return n;
20512}
20513
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020514/*
20515 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020516 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020517 */
20518 static int
20519builtin_function(name)
20520 char_u *name;
20521{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020522 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20523 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020524}
20525
Bram Moolenaar05159a02005-02-26 23:04:13 +000020526#if defined(FEAT_PROFILE) || defined(PROTO)
20527/*
20528 * Start profiling function "fp".
20529 */
20530 static void
20531func_do_profile(fp)
20532 ufunc_T *fp;
20533{
20534 fp->uf_tm_count = 0;
20535 profile_zero(&fp->uf_tm_self);
20536 profile_zero(&fp->uf_tm_total);
20537 if (fp->uf_tml_count == NULL)
20538 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20539 (sizeof(int) * fp->uf_lines.ga_len));
20540 if (fp->uf_tml_total == NULL)
20541 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20542 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20543 if (fp->uf_tml_self == NULL)
20544 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20545 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20546 fp->uf_tml_idx = -1;
20547 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20548 || fp->uf_tml_self == NULL)
20549 return; /* out of memory */
20550
20551 fp->uf_profiling = TRUE;
20552}
20553
20554/*
20555 * Dump the profiling results for all functions in file "fd".
20556 */
20557 void
20558func_dump_profile(fd)
20559 FILE *fd;
20560{
20561 hashitem_T *hi;
20562 int todo;
20563 ufunc_T *fp;
20564 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020565 ufunc_T **sorttab;
20566 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020567
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020568 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020569 if (todo == 0)
20570 return; /* nothing to dump */
20571
Bram Moolenaar73830342005-02-28 22:48:19 +000020572 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20573
Bram Moolenaar05159a02005-02-26 23:04:13 +000020574 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20575 {
20576 if (!HASHITEM_EMPTY(hi))
20577 {
20578 --todo;
20579 fp = HI2UF(hi);
20580 if (fp->uf_profiling)
20581 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020582 if (sorttab != NULL)
20583 sorttab[st_len++] = fp;
20584
Bram Moolenaar05159a02005-02-26 23:04:13 +000020585 if (fp->uf_name[0] == K_SPECIAL)
20586 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20587 else
20588 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20589 if (fp->uf_tm_count == 1)
20590 fprintf(fd, "Called 1 time\n");
20591 else
20592 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20593 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20594 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20595 fprintf(fd, "\n");
20596 fprintf(fd, "count total (s) self (s)\n");
20597
20598 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20599 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020600 if (FUNCLINE(fp, i) == NULL)
20601 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020602 prof_func_line(fd, fp->uf_tml_count[i],
20603 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020604 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20605 }
20606 fprintf(fd, "\n");
20607 }
20608 }
20609 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020610
20611 if (sorttab != NULL && st_len > 0)
20612 {
20613 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20614 prof_total_cmp);
20615 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20616 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20617 prof_self_cmp);
20618 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20619 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020620
20621 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020622}
Bram Moolenaar73830342005-02-28 22:48:19 +000020623
20624 static void
20625prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20626 FILE *fd;
20627 ufunc_T **sorttab;
20628 int st_len;
20629 char *title;
20630 int prefer_self; /* when equal print only self time */
20631{
20632 int i;
20633 ufunc_T *fp;
20634
20635 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20636 fprintf(fd, "count total (s) self (s) function\n");
20637 for (i = 0; i < 20 && i < st_len; ++i)
20638 {
20639 fp = sorttab[i];
20640 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20641 prefer_self);
20642 if (fp->uf_name[0] == K_SPECIAL)
20643 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20644 else
20645 fprintf(fd, " %s()\n", fp->uf_name);
20646 }
20647 fprintf(fd, "\n");
20648}
20649
20650/*
20651 * Print the count and times for one function or function line.
20652 */
20653 static void
20654prof_func_line(fd, count, total, self, prefer_self)
20655 FILE *fd;
20656 int count;
20657 proftime_T *total;
20658 proftime_T *self;
20659 int prefer_self; /* when equal print only self time */
20660{
20661 if (count > 0)
20662 {
20663 fprintf(fd, "%5d ", count);
20664 if (prefer_self && profile_equal(total, self))
20665 fprintf(fd, " ");
20666 else
20667 fprintf(fd, "%s ", profile_msg(total));
20668 if (!prefer_self && profile_equal(total, self))
20669 fprintf(fd, " ");
20670 else
20671 fprintf(fd, "%s ", profile_msg(self));
20672 }
20673 else
20674 fprintf(fd, " ");
20675}
20676
20677/*
20678 * Compare function for total time sorting.
20679 */
20680 static int
20681#ifdef __BORLANDC__
20682_RTLENTRYF
20683#endif
20684prof_total_cmp(s1, s2)
20685 const void *s1;
20686 const void *s2;
20687{
20688 ufunc_T *p1, *p2;
20689
20690 p1 = *(ufunc_T **)s1;
20691 p2 = *(ufunc_T **)s2;
20692 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20693}
20694
20695/*
20696 * Compare function for self time sorting.
20697 */
20698 static int
20699#ifdef __BORLANDC__
20700_RTLENTRYF
20701#endif
20702prof_self_cmp(s1, s2)
20703 const void *s1;
20704 const void *s2;
20705{
20706 ufunc_T *p1, *p2;
20707
20708 p1 = *(ufunc_T **)s1;
20709 p2 = *(ufunc_T **)s2;
20710 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20711}
20712
Bram Moolenaar05159a02005-02-26 23:04:13 +000020713#endif
20714
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020715/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020716 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020717 * Return TRUE if a package was loaded.
20718 */
20719 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020720script_autoload(name, reload)
20721 char_u *name;
20722 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020723{
20724 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020725 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020726 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020727 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020728
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020729 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020730 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020731 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020732 return FALSE;
20733
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020734 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020735
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020736 /* Find the name in the list of previously loaded package names. Skip
20737 * "autoload/", it's always the same. */
20738 for (i = 0; i < ga_loaded.ga_len; ++i)
20739 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20740 break;
20741 if (!reload && i < ga_loaded.ga_len)
20742 ret = FALSE; /* was loaded already */
20743 else
20744 {
20745 /* Remember the name if it wasn't loaded already. */
20746 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20747 {
20748 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20749 tofree = NULL;
20750 }
20751
20752 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020753 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020754 ret = TRUE;
20755 }
20756
20757 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020758 return ret;
20759}
20760
20761/*
20762 * Return the autoload script name for a function or variable name.
20763 * Returns NULL when out of memory.
20764 */
20765 static char_u *
20766autoload_name(name)
20767 char_u *name;
20768{
20769 char_u *p;
20770 char_u *scriptname;
20771
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020772 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020773 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20774 if (scriptname == NULL)
20775 return FALSE;
20776 STRCPY(scriptname, "autoload/");
20777 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020778 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020779 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020780 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020781 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020782 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020783}
20784
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20786
20787/*
20788 * Function given to ExpandGeneric() to obtain the list of user defined
20789 * function names.
20790 */
20791 char_u *
20792get_user_func_name(xp, idx)
20793 expand_T *xp;
20794 int idx;
20795{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020796 static long_u done;
20797 static hashitem_T *hi;
20798 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799
20800 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020802 done = 0;
20803 hi = func_hashtab.ht_array;
20804 }
20805 if (done < func_hashtab.ht_used)
20806 {
20807 if (done++ > 0)
20808 ++hi;
20809 while (HASHITEM_EMPTY(hi))
20810 ++hi;
20811 fp = HI2UF(hi);
20812
20813 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20814 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815
20816 cat_func_name(IObuff, fp);
20817 if (xp->xp_context != EXPAND_USER_FUNC)
20818 {
20819 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020820 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 STRCAT(IObuff, ")");
20822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 return IObuff;
20824 }
20825 return NULL;
20826}
20827
20828#endif /* FEAT_CMDL_COMPL */
20829
20830/*
20831 * Copy the function name of "fp" to buffer "buf".
20832 * "buf" must be able to hold the function name plus three bytes.
20833 * Takes care of script-local function names.
20834 */
20835 static void
20836cat_func_name(buf, fp)
20837 char_u *buf;
20838 ufunc_T *fp;
20839{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020840 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 {
20842 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020843 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 }
20845 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020846 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847}
20848
20849/*
20850 * ":delfunction {name}"
20851 */
20852 void
20853ex_delfunction(eap)
20854 exarg_T *eap;
20855{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020856 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 char_u *p;
20858 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020859 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860
20861 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020862 name = trans_function_name(&p, eap->skip, 0, &fudi);
20863 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020865 {
20866 if (fudi.fd_dict != NULL && !eap->skip)
20867 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 if (!ends_excmd(*skipwhite(p)))
20871 {
20872 vim_free(name);
20873 EMSG(_(e_trailing));
20874 return;
20875 }
20876 eap->nextcmd = check_nextcmd(p);
20877 if (eap->nextcmd != NULL)
20878 *p = NUL;
20879
20880 if (!eap->skip)
20881 fp = find_func(name);
20882 vim_free(name);
20883
20884 if (!eap->skip)
20885 {
20886 if (fp == NULL)
20887 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020888 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 return;
20890 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020891 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 {
20893 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20894 return;
20895 }
20896
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020897 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020899 /* Delete the dict item that refers to the function, it will
20900 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020901 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020903 else
20904 func_free(fp);
20905 }
20906}
20907
20908/*
20909 * Free a function and remove it from the list of functions.
20910 */
20911 static void
20912func_free(fp)
20913 ufunc_T *fp;
20914{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020915 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020916
20917 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020918 ga_clear_strings(&(fp->uf_args));
20919 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020920#ifdef FEAT_PROFILE
20921 vim_free(fp->uf_tml_count);
20922 vim_free(fp->uf_tml_total);
20923 vim_free(fp->uf_tml_self);
20924#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020925
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020926 /* remove the function from the function hashtable */
20927 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20928 if (HASHITEM_EMPTY(hi))
20929 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020930 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020931 hash_remove(&func_hashtab, hi);
20932
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020933 vim_free(fp);
20934}
20935
20936/*
20937 * Unreference a Function: decrement the reference count and free it when it
20938 * becomes zero. Only for numbered functions.
20939 */
20940 static void
20941func_unref(name)
20942 char_u *name;
20943{
20944 ufunc_T *fp;
20945
20946 if (name != NULL && isdigit(*name))
20947 {
20948 fp = find_func(name);
20949 if (fp == NULL)
20950 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020951 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020952 {
20953 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020954 * when "uf_calls" becomes zero. */
20955 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020956 func_free(fp);
20957 }
20958 }
20959}
20960
20961/*
20962 * Count a reference to a Function.
20963 */
20964 static void
20965func_ref(name)
20966 char_u *name;
20967{
20968 ufunc_T *fp;
20969
20970 if (name != NULL && isdigit(*name))
20971 {
20972 fp = find_func(name);
20973 if (fp == NULL)
20974 EMSG2(_(e_intern2), "func_ref()");
20975 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020976 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 }
20978}
20979
20980/*
20981 * Call a user function.
20982 */
20983 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020984call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 ufunc_T *fp; /* pointer to function */
20986 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020987 typval_T *argvars; /* arguments */
20988 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 linenr_T firstline; /* first line of range */
20990 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020991 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992{
Bram Moolenaar33570922005-01-25 22:26:29 +000020993 char_u *save_sourcing_name;
20994 linenr_T save_sourcing_lnum;
20995 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020996 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020997 int save_did_emsg;
20998 static int depth = 0;
20999 dictitem_T *v;
21000 int fixvar_idx = 0; /* index in fixvar[] */
21001 int i;
21002 int ai;
21003 char_u numbuf[NUMBUFLEN];
21004 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021005#ifdef FEAT_PROFILE
21006 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021007 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009
21010 /* If depth of calling is getting too high, don't execute the function */
21011 if (depth >= p_mfd)
21012 {
21013 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021014 rettv->v_type = VAR_NUMBER;
21015 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 return;
21017 }
21018 ++depth;
21019
21020 line_breakcheck(); /* check for CTRL-C hit */
21021
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021022 fc = (funccall_T *)alloc(sizeof(funccall_T));
21023 fc->caller = current_funccal;
21024 current_funccal = fc;
21025 fc->func = fp;
21026 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021027 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021028 fc->linenr = 0;
21029 fc->returned = FALSE;
21030 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021032 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21033 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034
Bram Moolenaar33570922005-01-25 22:26:29 +000021035 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021036 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021037 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21038 * each argument variable and saves a lot of time.
21039 */
21040 /*
21041 * Init l: variables.
21042 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021043 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021044 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021045 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021046 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21047 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021048 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021049 name = v->di_key;
21050 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021051 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021052 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021053 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021054 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021055 v->di_tv.vval.v_dict = selfdict;
21056 ++selfdict->dv_refcount;
21057 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021058
Bram Moolenaar33570922005-01-25 22:26:29 +000021059 /*
21060 * Init a: variables.
21061 * Set a:0 to "argcount".
21062 * Set a:000 to a list with room for the "..." arguments.
21063 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021064 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21065 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021066 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021067 /* Use "name" to avoid a warning from some compiler that checks the
21068 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021069 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021070 name = v->di_key;
21071 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021072 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021073 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021074 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021075 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021076 v->di_tv.vval.v_list = &fc->l_varlist;
21077 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21078 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21079 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021080
21081 /*
21082 * Set a:firstline to "firstline" and a:lastline to "lastline".
21083 * Set a:name to named arguments.
21084 * Set a:N to the "..." arguments.
21085 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021086 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021087 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021088 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021089 (varnumber_T)lastline);
21090 for (i = 0; i < argcount; ++i)
21091 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021092 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021093 if (ai < 0)
21094 /* named argument a:name */
21095 name = FUNCARG(fp, i);
21096 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021097 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021098 /* "..." argument a:1, a:2, etc. */
21099 sprintf((char *)numbuf, "%d", ai + 1);
21100 name = numbuf;
21101 }
21102 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21103 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021104 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021105 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21106 }
21107 else
21108 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021109 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21110 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021111 if (v == NULL)
21112 break;
21113 v->di_flags = DI_FLAGS_RO;
21114 }
21115 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021116 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021117
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021118 /* Note: the values are copied directly to avoid alloc/free.
21119 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021120 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021121 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021122
21123 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21124 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021125 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21126 fc->l_listitems[ai].li_tv = argvars[i];
21127 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021128 }
21129 }
21130
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 /* Don't redraw while executing the function. */
21132 ++RedrawingDisabled;
21133 save_sourcing_name = sourcing_name;
21134 save_sourcing_lnum = sourcing_lnum;
21135 sourcing_lnum = 1;
21136 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021137 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 if (sourcing_name != NULL)
21139 {
21140 if (save_sourcing_name != NULL
21141 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21142 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21143 else
21144 STRCPY(sourcing_name, "function ");
21145 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21146
21147 if (p_verbose >= 12)
21148 {
21149 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021150 verbose_enter_scroll();
21151
Bram Moolenaar555b2802005-05-19 21:08:39 +000021152 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 if (p_verbose >= 14)
21154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021156 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021157 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021158 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159
21160 msg_puts((char_u *)"(");
21161 for (i = 0; i < argcount; ++i)
21162 {
21163 if (i > 0)
21164 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021165 if (argvars[i].v_type == VAR_NUMBER)
21166 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 else
21168 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021169 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21170 if (s != NULL)
21171 {
21172 trunc_string(s, buf, MSG_BUF_CLEN);
21173 msg_puts(buf);
21174 vim_free(tofree);
21175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 }
21177 }
21178 msg_puts((char_u *)")");
21179 }
21180 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021181
21182 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 --no_wait_return;
21184 }
21185 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021186#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021187 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021188 {
21189 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21190 func_do_profile(fp);
21191 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021192 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021193 {
21194 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021195 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021196 profile_zero(&fp->uf_tm_children);
21197 }
21198 script_prof_save(&wait_start);
21199 }
21200#endif
21201
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021203 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 save_did_emsg = did_emsg;
21205 did_emsg = FALSE;
21206
21207 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021208 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21210
21211 --RedrawingDisabled;
21212
21213 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021214 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021216 clear_tv(rettv);
21217 rettv->v_type = VAR_NUMBER;
21218 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 }
21220
Bram Moolenaar05159a02005-02-26 23:04:13 +000021221#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021222 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021223 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021224 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021225 profile_end(&call_start);
21226 profile_sub_wait(&wait_start, &call_start);
21227 profile_add(&fp->uf_tm_total, &call_start);
21228 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021229 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021230 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021231 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21232 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021233 }
21234 }
21235#endif
21236
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 /* when being verbose, mention the return value */
21238 if (p_verbose >= 12)
21239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021241 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021244 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021245 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021246 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021247 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021250 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021251 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021252 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021253 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021254
Bram Moolenaar555b2802005-05-19 21:08:39 +000021255 /* The value may be very long. Skip the middle part, so that we
21256 * have some idea how it starts and ends. smsg() would always
21257 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021258 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021259 if (s != NULL)
21260 {
21261 trunc_string(s, buf, MSG_BUF_CLEN);
21262 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21263 vim_free(tofree);
21264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 }
21266 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021267
21268 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 --no_wait_return;
21270 }
21271
21272 vim_free(sourcing_name);
21273 sourcing_name = save_sourcing_name;
21274 sourcing_lnum = save_sourcing_lnum;
21275 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021276#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021277 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021278 script_prof_restore(&wait_start);
21279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280
21281 if (p_verbose >= 12 && sourcing_name != NULL)
21282 {
21283 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021284 verbose_enter_scroll();
21285
Bram Moolenaar555b2802005-05-19 21:08:39 +000021286 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021288
21289 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 --no_wait_return;
21291 }
21292
21293 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021294 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021296
21297 /* if the a:000 list and the a: dict are not referenced we can free the
21298 * funccall_T and what's in it. */
21299 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21300 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21301 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21302 {
21303 free_funccal(fc, FALSE);
21304 }
21305 else
21306 {
21307 hashitem_T *hi;
21308 listitem_T *li;
21309 int todo;
21310
21311 /* "fc" is still in use. This can happen when returning "a:000" or
21312 * assigning "l:" to a global variable.
21313 * Link "fc" in the list for garbage collection later. */
21314 fc->caller = previous_funccal;
21315 previous_funccal = fc;
21316
21317 /* Make a copy of the a: variables, since we didn't do that above. */
21318 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21319 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21320 {
21321 if (!HASHITEM_EMPTY(hi))
21322 {
21323 --todo;
21324 v = HI2DI(hi);
21325 copy_tv(&v->di_tv, &v->di_tv);
21326 }
21327 }
21328
21329 /* Make a copy of the a:000 items, since we didn't do that above. */
21330 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21331 copy_tv(&li->li_tv, &li->li_tv);
21332 }
21333}
21334
21335/*
21336 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021337 * referenced from anywhere.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021338 */
21339 static int
21340can_free_funccal(fc, copyID)
21341 funccall_T *fc;
21342 int copyID;
21343{
21344 return (fc->l_varlist.lv_copyID != copyID
21345 && fc->l_vars.dv_copyID != copyID
21346 && fc->l_avars.dv_copyID != copyID);
21347}
21348
21349/*
21350 * Free "fc" and what it contains.
21351 */
21352 static void
21353free_funccal(fc, free_val)
21354 funccall_T *fc;
21355 int free_val; /* a: vars were allocated */
21356{
21357 listitem_T *li;
21358
21359 /* The a: variables typevals may not have been allocated, only free the
21360 * allocated variables. */
21361 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21362
21363 /* free all l: variables */
21364 vars_clear(&fc->l_vars.dv_hashtab);
21365
21366 /* Free the a:000 variables if they were allocated. */
21367 if (free_val)
21368 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21369 clear_tv(&li->li_tv);
21370
21371 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372}
21373
21374/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021375 * Add a number variable "name" to dict "dp" with value "nr".
21376 */
21377 static void
21378add_nr_var(dp, v, name, nr)
21379 dict_T *dp;
21380 dictitem_T *v;
21381 char *name;
21382 varnumber_T nr;
21383{
21384 STRCPY(v->di_key, name);
21385 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21386 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21387 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021388 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021389 v->di_tv.vval.v_number = nr;
21390}
21391
21392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 * ":return [expr]"
21394 */
21395 void
21396ex_return(eap)
21397 exarg_T *eap;
21398{
21399 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021400 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 int returning = FALSE;
21402
21403 if (current_funccal == NULL)
21404 {
21405 EMSG(_("E133: :return not inside a function"));
21406 return;
21407 }
21408
21409 if (eap->skip)
21410 ++emsg_skip;
21411
21412 eap->nextcmd = NULL;
21413 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021414 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 {
21416 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021417 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021419 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 }
21421 /* It's safer to return also on error. */
21422 else if (!eap->skip)
21423 {
21424 /*
21425 * Return unless the expression evaluation has been cancelled due to an
21426 * aborting error, an interrupt, or an exception.
21427 */
21428 if (!aborting())
21429 returning = do_return(eap, FALSE, TRUE, NULL);
21430 }
21431
21432 /* When skipping or the return gets pending, advance to the next command
21433 * in this line (!returning). Otherwise, ignore the rest of the line.
21434 * Following lines will be ignored by get_func_line(). */
21435 if (returning)
21436 eap->nextcmd = NULL;
21437 else if (eap->nextcmd == NULL) /* no argument */
21438 eap->nextcmd = check_nextcmd(arg);
21439
21440 if (eap->skip)
21441 --emsg_skip;
21442}
21443
21444/*
21445 * Return from a function. Possibly makes the return pending. Also called
21446 * for a pending return at the ":endtry" or after returning from an extra
21447 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021448 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021449 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 * FALSE when the return gets pending.
21451 */
21452 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021453do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 exarg_T *eap;
21455 int reanimate;
21456 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021457 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458{
21459 int idx;
21460 struct condstack *cstack = eap->cstack;
21461
21462 if (reanimate)
21463 /* Undo the return. */
21464 current_funccal->returned = FALSE;
21465
21466 /*
21467 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21468 * not in its finally clause (which then is to be executed next) is found.
21469 * In this case, make the ":return" pending for execution at the ":endtry".
21470 * Otherwise, return normally.
21471 */
21472 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21473 if (idx >= 0)
21474 {
21475 cstack->cs_pending[idx] = CSTP_RETURN;
21476
21477 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021478 /* A pending return again gets pending. "rettv" points to an
21479 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021481 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 else
21483 {
21484 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021485 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021487 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021489 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 {
21491 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021492 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021493 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 else
21495 EMSG(_(e_outofmem));
21496 }
21497 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021498 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499
21500 if (reanimate)
21501 {
21502 /* The pending return value could be overwritten by a ":return"
21503 * without argument in a finally clause; reset the default
21504 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021505 current_funccal->rettv->v_type = VAR_NUMBER;
21506 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 }
21508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021509 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510 }
21511 else
21512 {
21513 current_funccal->returned = TRUE;
21514
21515 /* If the return is carried out now, store the return value. For
21516 * a return immediately after reanimation, the value is already
21517 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021518 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021520 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021521 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021523 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 }
21525 }
21526
21527 return idx < 0;
21528}
21529
21530/*
21531 * Free the variable with a pending return value.
21532 */
21533 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021534discard_pending_return(rettv)
21535 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536{
Bram Moolenaar33570922005-01-25 22:26:29 +000021537 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538}
21539
21540/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021541 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 * is an allocated string. Used by report_pending() for verbose messages.
21543 */
21544 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021545get_return_cmd(rettv)
21546 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021548 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021549 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021550 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021552 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021553 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021554 if (s == NULL)
21555 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021556
21557 STRCPY(IObuff, ":return ");
21558 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21559 if (STRLEN(s) + 8 >= IOSIZE)
21560 STRCPY(IObuff + IOSIZE - 4, "...");
21561 vim_free(tofree);
21562 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563}
21564
21565/*
21566 * Get next function line.
21567 * Called by do_cmdline() to get the next line.
21568 * Returns allocated string, or NULL for end of function.
21569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 char_u *
21571get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021572 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021574 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575{
Bram Moolenaar33570922005-01-25 22:26:29 +000021576 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021577 ufunc_T *fp = fcp->func;
21578 char_u *retval;
21579 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580
21581 /* If breakpoints have been added/deleted need to check for it. */
21582 if (fcp->dbg_tick != debug_tick)
21583 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021584 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 sourcing_lnum);
21586 fcp->dbg_tick = debug_tick;
21587 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021588#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021589 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021590 func_line_end(cookie);
21591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592
Bram Moolenaar05159a02005-02-26 23:04:13 +000021593 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021594 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21595 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 retval = NULL;
21597 else
21598 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021599 /* Skip NULL lines (continuation lines). */
21600 while (fcp->linenr < gap->ga_len
21601 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21602 ++fcp->linenr;
21603 if (fcp->linenr >= gap->ga_len)
21604 retval = NULL;
21605 else
21606 {
21607 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21608 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021609#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021610 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021611 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021612#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 }
21615
21616 /* Did we encounter a breakpoint? */
21617 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21618 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021619 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021621 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 sourcing_lnum);
21623 fcp->dbg_tick = debug_tick;
21624 }
21625
21626 return retval;
21627}
21628
Bram Moolenaar05159a02005-02-26 23:04:13 +000021629#if defined(FEAT_PROFILE) || defined(PROTO)
21630/*
21631 * Called when starting to read a function line.
21632 * "sourcing_lnum" must be correct!
21633 * When skipping lines it may not actually be executed, but we won't find out
21634 * until later and we need to store the time now.
21635 */
21636 void
21637func_line_start(cookie)
21638 void *cookie;
21639{
21640 funccall_T *fcp = (funccall_T *)cookie;
21641 ufunc_T *fp = fcp->func;
21642
21643 if (fp->uf_profiling && sourcing_lnum >= 1
21644 && sourcing_lnum <= fp->uf_lines.ga_len)
21645 {
21646 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021647 /* Skip continuation lines. */
21648 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21649 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021650 fp->uf_tml_execed = FALSE;
21651 profile_start(&fp->uf_tml_start);
21652 profile_zero(&fp->uf_tml_children);
21653 profile_get_wait(&fp->uf_tml_wait);
21654 }
21655}
21656
21657/*
21658 * Called when actually executing a function line.
21659 */
21660 void
21661func_line_exec(cookie)
21662 void *cookie;
21663{
21664 funccall_T *fcp = (funccall_T *)cookie;
21665 ufunc_T *fp = fcp->func;
21666
21667 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21668 fp->uf_tml_execed = TRUE;
21669}
21670
21671/*
21672 * Called when done with a function line.
21673 */
21674 void
21675func_line_end(cookie)
21676 void *cookie;
21677{
21678 funccall_T *fcp = (funccall_T *)cookie;
21679 ufunc_T *fp = fcp->func;
21680
21681 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21682 {
21683 if (fp->uf_tml_execed)
21684 {
21685 ++fp->uf_tml_count[fp->uf_tml_idx];
21686 profile_end(&fp->uf_tml_start);
21687 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021688 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021689 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21690 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021691 }
21692 fp->uf_tml_idx = -1;
21693 }
21694}
21695#endif
21696
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697/*
21698 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021699 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 */
21701 int
21702func_has_ended(cookie)
21703 void *cookie;
21704{
Bram Moolenaar33570922005-01-25 22:26:29 +000021705 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706
21707 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21708 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021709 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 || fcp->returned);
21711}
21712
21713/*
21714 * return TRUE if cookie indicates a function which "abort"s on errors.
21715 */
21716 int
21717func_has_abort(cookie)
21718 void *cookie;
21719{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021720 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721}
21722
21723#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21724typedef enum
21725{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021726 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21727 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21728 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729} var_flavour_T;
21730
21731static var_flavour_T var_flavour __ARGS((char_u *varname));
21732
21733 static var_flavour_T
21734var_flavour(varname)
21735 char_u *varname;
21736{
21737 char_u *p = varname;
21738
21739 if (ASCII_ISUPPER(*p))
21740 {
21741 while (*(++p))
21742 if (ASCII_ISLOWER(*p))
21743 return VAR_FLAVOUR_SESSION;
21744 return VAR_FLAVOUR_VIMINFO;
21745 }
21746 else
21747 return VAR_FLAVOUR_DEFAULT;
21748}
21749#endif
21750
21751#if defined(FEAT_VIMINFO) || defined(PROTO)
21752/*
21753 * Restore global vars that start with a capital from the viminfo file
21754 */
21755 int
21756read_viminfo_varlist(virp, writing)
21757 vir_T *virp;
21758 int writing;
21759{
21760 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021761 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021762 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763
21764 if (!writing && (find_viminfo_parameter('!') != NULL))
21765 {
21766 tab = vim_strchr(virp->vir_line + 1, '\t');
21767 if (tab != NULL)
21768 {
21769 *tab++ = '\0'; /* isolate the variable name */
21770 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021771 type = VAR_STRING;
21772#ifdef FEAT_FLOAT
21773 else if (*tab == 'F')
21774 type = VAR_FLOAT;
21775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776
21777 tab = vim_strchr(tab, '\t');
21778 if (tab != NULL)
21779 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021780 tv.v_type = type;
21781 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021782 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021784#ifdef FEAT_FLOAT
21785 else if (type == VAR_FLOAT)
21786 (void)string2float(tab + 1, &tv.vval.v_float);
21787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021788 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021789 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021790 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021791 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021792 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 }
21794 }
21795 }
21796
21797 return viminfo_readline(virp);
21798}
21799
21800/*
21801 * Write global vars that start with a capital to the viminfo file
21802 */
21803 void
21804write_viminfo_varlist(fp)
21805 FILE *fp;
21806{
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 hashitem_T *hi;
21808 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021809 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021810 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021811 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021812 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021813 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814
21815 if (find_viminfo_parameter('!') == NULL)
21816 return;
21817
21818 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021819
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021820 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021821 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021823 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021825 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021826 this_var = HI2DI(hi);
21827 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021828 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021829 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021830 {
21831 case VAR_STRING: s = "STR"; break;
21832 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021833#ifdef FEAT_FLOAT
21834 case VAR_FLOAT: s = "FLO"; break;
21835#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021836 default: continue;
21837 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021838 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021839 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021840 if (p != NULL)
21841 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021842 vim_free(tofree);
21843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844 }
21845 }
21846}
21847#endif
21848
21849#if defined(FEAT_SESSION) || defined(PROTO)
21850 int
21851store_session_globals(fd)
21852 FILE *fd;
21853{
Bram Moolenaar33570922005-01-25 22:26:29 +000021854 hashitem_T *hi;
21855 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021856 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 char_u *p, *t;
21858
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021859 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021860 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021862 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021864 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021865 this_var = HI2DI(hi);
21866 if ((this_var->di_tv.v_type == VAR_NUMBER
21867 || this_var->di_tv.v_type == VAR_STRING)
21868 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021869 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021870 /* Escape special characters with a backslash. Turn a LF and
21871 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021872 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021873 (char_u *)"\\\"\n\r");
21874 if (p == NULL) /* out of memory */
21875 break;
21876 for (t = p; *t != NUL; ++t)
21877 if (*t == '\n')
21878 *t = 'n';
21879 else if (*t == '\r')
21880 *t = 'r';
21881 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 this_var->di_key,
21883 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21884 : ' ',
21885 p,
21886 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21887 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021888 || put_eol(fd) == FAIL)
21889 {
21890 vim_free(p);
21891 return FAIL;
21892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 vim_free(p);
21894 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021895#ifdef FEAT_FLOAT
21896 else if (this_var->di_tv.v_type == VAR_FLOAT
21897 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21898 {
21899 float_T f = this_var->di_tv.vval.v_float;
21900 int sign = ' ';
21901
21902 if (f < 0)
21903 {
21904 f = -f;
21905 sign = '-';
21906 }
21907 if ((fprintf(fd, "let %s = %c&%f",
21908 this_var->di_key, sign, f) < 0)
21909 || put_eol(fd) == FAIL)
21910 return FAIL;
21911 }
21912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 }
21914 }
21915 return OK;
21916}
21917#endif
21918
Bram Moolenaar661b1822005-07-28 22:36:45 +000021919/*
21920 * Display script name where an item was last set.
21921 * Should only be invoked when 'verbose' is non-zero.
21922 */
21923 void
21924last_set_msg(scriptID)
21925 scid_T scriptID;
21926{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021927 char_u *p;
21928
Bram Moolenaar661b1822005-07-28 22:36:45 +000021929 if (scriptID != 0)
21930 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021931 p = home_replace_save(NULL, get_scriptname(scriptID));
21932 if (p != NULL)
21933 {
21934 verbose_enter();
21935 MSG_PUTS(_("\n\tLast set from "));
21936 MSG_PUTS(p);
21937 vim_free(p);
21938 verbose_leave();
21939 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021940 }
21941}
21942
Bram Moolenaard812df62008-11-09 12:46:09 +000021943/*
21944 * List v:oldfiles in a nice way.
21945 */
Bram Moolenaard812df62008-11-09 12:46:09 +000021946 void
21947ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021948 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000021949{
21950 list_T *l = vimvars[VV_OLDFILES].vv_list;
21951 listitem_T *li;
21952 int nr = 0;
21953
21954 if (l == NULL)
21955 msg((char_u *)_("No old files"));
21956 else
21957 {
21958 msg_start();
21959 msg_scroll = TRUE;
21960 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
21961 {
21962 msg_outnum((long)++nr);
21963 MSG_PUTS(": ");
21964 msg_outtrans(get_tv_string(&li->li_tv));
21965 msg_putchar('\n');
21966 out_flush(); /* output one line at a time */
21967 ui_breakcheck();
21968 }
21969 /* Assume "got_int" was set to truncate the listing. */
21970 got_int = FALSE;
21971
21972#ifdef FEAT_BROWSE_CMD
21973 if (cmdmod.browse)
21974 {
21975 quit_more = FALSE;
21976 nr = prompt_for_number(FALSE);
21977 msg_starthere();
21978 if (nr > 0)
21979 {
21980 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
21981 (long)nr);
21982
21983 if (p != NULL)
21984 {
21985 p = expand_env_save(p);
21986 eap->arg = p;
21987 eap->cmdidx = CMD_edit;
21988 cmdmod.browse = FALSE;
21989 do_exedit(eap, NULL);
21990 vim_free(p);
21991 }
21992 }
21993 }
21994#endif
21995 }
21996}
21997
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998#endif /* FEAT_EVAL */
21999
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022001#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002
22003#ifdef WIN3264
22004/*
22005 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22006 */
22007static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22008static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22009static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22010
22011/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022012 * Get the short path (8.3) for the filename in "fnamep".
22013 * Only works for a valid file name.
22014 * When the path gets longer "fnamep" is changed and the allocated buffer
22015 * is put in "bufp".
22016 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22017 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018 */
22019 static int
22020get_short_pathname(fnamep, bufp, fnamelen)
22021 char_u **fnamep;
22022 char_u **bufp;
22023 int *fnamelen;
22024{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022025 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 char_u *newbuf;
22027
22028 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 l = GetShortPathName(*fnamep, *fnamep, len);
22030 if (l > len - 1)
22031 {
22032 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022033 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034 newbuf = vim_strnsave(*fnamep, l);
22035 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022036 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037
22038 vim_free(*bufp);
22039 *fnamep = *bufp = newbuf;
22040
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022041 /* Really should always succeed, as the buffer is big enough. */
22042 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 }
22044
22045 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022046 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047}
22048
22049/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022050 * Get the short path (8.3) for the filename in "fname". The converted
22051 * path is returned in "bufp".
22052 *
22053 * Some of the directories specified in "fname" may not exist. This function
22054 * will shorten the existing directories at the beginning of the path and then
22055 * append the remaining non-existing path.
22056 *
22057 * fname - Pointer to the filename to shorten. On return, contains the
22058 * pointer to the shortened pathname
22059 * bufp - Pointer to an allocated buffer for the filename.
22060 * fnamelen - Length of the filename pointed to by fname
22061 *
22062 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 */
22064 static int
22065shortpath_for_invalid_fname(fname, bufp, fnamelen)
22066 char_u **fname;
22067 char_u **bufp;
22068 int *fnamelen;
22069{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022070 char_u *short_fname, *save_fname, *pbuf_unused;
22071 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022073 int old_len, len;
22074 int new_len, sfx_len;
22075 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076
22077 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022078 old_len = *fnamelen;
22079 save_fname = vim_strnsave(*fname, old_len);
22080 pbuf_unused = NULL;
22081 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022083 endp = save_fname + old_len - 1; /* Find the end of the copy */
22084 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022086 /*
22087 * Try shortening the supplied path till it succeeds by removing one
22088 * directory at a time from the tail of the path.
22089 */
22090 len = 0;
22091 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022093 /* go back one path-separator */
22094 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22095 --endp;
22096 if (endp <= save_fname)
22097 break; /* processed the complete path */
22098
22099 /*
22100 * Replace the path separator with a NUL and try to shorten the
22101 * resulting path.
22102 */
22103 ch = *endp;
22104 *endp = 0;
22105 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022106 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022107 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22108 {
22109 retval = FAIL;
22110 goto theend;
22111 }
22112 *endp = ch; /* preserve the string */
22113
22114 if (len > 0)
22115 break; /* successfully shortened the path */
22116
22117 /* failed to shorten the path. Skip the path separator */
22118 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 }
22120
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022121 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022123 /*
22124 * Succeeded in shortening the path. Now concatenate the shortened
22125 * path with the remaining path at the tail.
22126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022128 /* Compute the length of the new path. */
22129 sfx_len = (int)(save_endp - endp) + 1;
22130 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022132 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022134 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022136 /* There is not enough space in the currently allocated string,
22137 * copy it to a buffer big enough. */
22138 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022140 {
22141 retval = FAIL;
22142 goto theend;
22143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 }
22145 else
22146 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022147 /* Transfer short_fname to the main buffer (it's big enough),
22148 * unless get_short_pathname() did its work in-place. */
22149 *fname = *bufp = save_fname;
22150 if (short_fname != save_fname)
22151 vim_strncpy(save_fname, short_fname, len);
22152 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022154
22155 /* concat the not-shortened part of the path */
22156 vim_strncpy(*fname + len, endp, sfx_len);
22157 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022159
22160theend:
22161 vim_free(pbuf_unused);
22162 vim_free(save_fname);
22163
22164 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165}
22166
22167/*
22168 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022169 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 */
22171 static int
22172shortpath_for_partial(fnamep, bufp, fnamelen)
22173 char_u **fnamep;
22174 char_u **bufp;
22175 int *fnamelen;
22176{
22177 int sepcount, len, tflen;
22178 char_u *p;
22179 char_u *pbuf, *tfname;
22180 int hasTilde;
22181
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022182 /* Count up the path separators from the RHS.. so we know which part
22183 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022185 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 if (vim_ispathsep(*p))
22187 ++sepcount;
22188
22189 /* Need full path first (use expand_env() to remove a "~/") */
22190 hasTilde = (**fnamep == '~');
22191 if (hasTilde)
22192 pbuf = tfname = expand_env_save(*fnamep);
22193 else
22194 pbuf = tfname = FullName_save(*fnamep, FALSE);
22195
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022196 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022198 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22199 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200
22201 if (len == 0)
22202 {
22203 /* Don't have a valid filename, so shorten the rest of the
22204 * path if we can. This CAN give us invalid 8.3 filenames, but
22205 * there's not a lot of point in guessing what it might be.
22206 */
22207 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022208 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22209 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 }
22211
22212 /* Count the paths backward to find the beginning of the desired string. */
22213 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022214 {
22215#ifdef FEAT_MBYTE
22216 if (has_mbyte)
22217 p -= mb_head_off(tfname, p);
22218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 if (vim_ispathsep(*p))
22220 {
22221 if (sepcount == 0 || (hasTilde && sepcount == 1))
22222 break;
22223 else
22224 sepcount --;
22225 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 if (hasTilde)
22228 {
22229 --p;
22230 if (p >= tfname)
22231 *p = '~';
22232 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022233 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 }
22235 else
22236 ++p;
22237
22238 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22239 vim_free(*bufp);
22240 *fnamelen = (int)STRLEN(p);
22241 *bufp = pbuf;
22242 *fnamep = p;
22243
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022244 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245}
22246#endif /* WIN3264 */
22247
22248/*
22249 * Adjust a filename, according to a string of modifiers.
22250 * *fnamep must be NUL terminated when called. When returning, the length is
22251 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022252 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 * When there is an error, *fnamep is set to NULL.
22254 */
22255 int
22256modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22257 char_u *src; /* string with modifiers */
22258 int *usedlen; /* characters after src that are used */
22259 char_u **fnamep; /* file name so far */
22260 char_u **bufp; /* buffer for allocated file name or NULL */
22261 int *fnamelen; /* length of fnamep */
22262{
22263 int valid = 0;
22264 char_u *tail;
22265 char_u *s, *p, *pbuf;
22266 char_u dirname[MAXPATHL];
22267 int c;
22268 int has_fullname = 0;
22269#ifdef WIN3264
22270 int has_shortname = 0;
22271#endif
22272
22273repeat:
22274 /* ":p" - full path/file_name */
22275 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22276 {
22277 has_fullname = 1;
22278
22279 valid |= VALID_PATH;
22280 *usedlen += 2;
22281
22282 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22283 if ((*fnamep)[0] == '~'
22284#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22285 && ((*fnamep)[1] == '/'
22286# ifdef BACKSLASH_IN_FILENAME
22287 || (*fnamep)[1] == '\\'
22288# endif
22289 || (*fnamep)[1] == NUL)
22290
22291#endif
22292 )
22293 {
22294 *fnamep = expand_env_save(*fnamep);
22295 vim_free(*bufp); /* free any allocated file name */
22296 *bufp = *fnamep;
22297 if (*fnamep == NULL)
22298 return -1;
22299 }
22300
22301 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022302 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 {
22304 if (vim_ispathsep(*p)
22305 && p[1] == '.'
22306 && (p[2] == NUL
22307 || vim_ispathsep(p[2])
22308 || (p[2] == '.'
22309 && (p[3] == NUL || vim_ispathsep(p[3])))))
22310 break;
22311 }
22312
22313 /* FullName_save() is slow, don't use it when not needed. */
22314 if (*p != NUL || !vim_isAbsName(*fnamep))
22315 {
22316 *fnamep = FullName_save(*fnamep, *p != NUL);
22317 vim_free(*bufp); /* free any allocated file name */
22318 *bufp = *fnamep;
22319 if (*fnamep == NULL)
22320 return -1;
22321 }
22322
22323 /* Append a path separator to a directory. */
22324 if (mch_isdir(*fnamep))
22325 {
22326 /* Make room for one or two extra characters. */
22327 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22328 vim_free(*bufp); /* free any allocated file name */
22329 *bufp = *fnamep;
22330 if (*fnamep == NULL)
22331 return -1;
22332 add_pathsep(*fnamep);
22333 }
22334 }
22335
22336 /* ":." - path relative to the current directory */
22337 /* ":~" - path relative to the home directory */
22338 /* ":8" - shortname path - postponed till after */
22339 while (src[*usedlen] == ':'
22340 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22341 {
22342 *usedlen += 2;
22343 if (c == '8')
22344 {
22345#ifdef WIN3264
22346 has_shortname = 1; /* Postpone this. */
22347#endif
22348 continue;
22349 }
22350 pbuf = NULL;
22351 /* Need full path first (use expand_env() to remove a "~/") */
22352 if (!has_fullname)
22353 {
22354 if (c == '.' && **fnamep == '~')
22355 p = pbuf = expand_env_save(*fnamep);
22356 else
22357 p = pbuf = FullName_save(*fnamep, FALSE);
22358 }
22359 else
22360 p = *fnamep;
22361
22362 has_fullname = 0;
22363
22364 if (p != NULL)
22365 {
22366 if (c == '.')
22367 {
22368 mch_dirname(dirname, MAXPATHL);
22369 s = shorten_fname(p, dirname);
22370 if (s != NULL)
22371 {
22372 *fnamep = s;
22373 if (pbuf != NULL)
22374 {
22375 vim_free(*bufp); /* free any allocated file name */
22376 *bufp = pbuf;
22377 pbuf = NULL;
22378 }
22379 }
22380 }
22381 else
22382 {
22383 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22384 /* Only replace it when it starts with '~' */
22385 if (*dirname == '~')
22386 {
22387 s = vim_strsave(dirname);
22388 if (s != NULL)
22389 {
22390 *fnamep = s;
22391 vim_free(*bufp);
22392 *bufp = s;
22393 }
22394 }
22395 }
22396 vim_free(pbuf);
22397 }
22398 }
22399
22400 tail = gettail(*fnamep);
22401 *fnamelen = (int)STRLEN(*fnamep);
22402
22403 /* ":h" - head, remove "/file_name", can be repeated */
22404 /* Don't remove the first "/" or "c:\" */
22405 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22406 {
22407 valid |= VALID_HEAD;
22408 *usedlen += 2;
22409 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022410 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022411 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 *fnamelen = (int)(tail - *fnamep);
22413#ifdef VMS
22414 if (*fnamelen > 0)
22415 *fnamelen += 1; /* the path separator is part of the path */
22416#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022417 if (*fnamelen == 0)
22418 {
22419 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22420 p = vim_strsave((char_u *)".");
22421 if (p == NULL)
22422 return -1;
22423 vim_free(*bufp);
22424 *bufp = *fnamep = tail = p;
22425 *fnamelen = 1;
22426 }
22427 else
22428 {
22429 while (tail > s && !after_pathsep(s, tail))
22430 mb_ptr_back(*fnamep, tail);
22431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 }
22433
22434 /* ":8" - shortname */
22435 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22436 {
22437 *usedlen += 2;
22438#ifdef WIN3264
22439 has_shortname = 1;
22440#endif
22441 }
22442
22443#ifdef WIN3264
22444 /* Check shortname after we have done 'heads' and before we do 'tails'
22445 */
22446 if (has_shortname)
22447 {
22448 pbuf = NULL;
22449 /* Copy the string if it is shortened by :h */
22450 if (*fnamelen < (int)STRLEN(*fnamep))
22451 {
22452 p = vim_strnsave(*fnamep, *fnamelen);
22453 if (p == 0)
22454 return -1;
22455 vim_free(*bufp);
22456 *bufp = *fnamep = p;
22457 }
22458
22459 /* Split into two implementations - makes it easier. First is where
22460 * there isn't a full name already, second is where there is.
22461 */
22462 if (!has_fullname && !vim_isAbsName(*fnamep))
22463 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022464 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 return -1;
22466 }
22467 else
22468 {
22469 int l;
22470
22471 /* Simple case, already have the full-name
22472 * Nearly always shorter, so try first time. */
22473 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022474 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 return -1;
22476
22477 if (l == 0)
22478 {
22479 /* Couldn't find the filename.. search the paths.
22480 */
22481 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022482 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483 return -1;
22484 }
22485 *fnamelen = l;
22486 }
22487 }
22488#endif /* WIN3264 */
22489
22490 /* ":t" - tail, just the basename */
22491 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22492 {
22493 *usedlen += 2;
22494 *fnamelen -= (int)(tail - *fnamep);
22495 *fnamep = tail;
22496 }
22497
22498 /* ":e" - extension, can be repeated */
22499 /* ":r" - root, without extension, can be repeated */
22500 while (src[*usedlen] == ':'
22501 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22502 {
22503 /* find a '.' in the tail:
22504 * - for second :e: before the current fname
22505 * - otherwise: The last '.'
22506 */
22507 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22508 s = *fnamep - 2;
22509 else
22510 s = *fnamep + *fnamelen - 1;
22511 for ( ; s > tail; --s)
22512 if (s[0] == '.')
22513 break;
22514 if (src[*usedlen + 1] == 'e') /* :e */
22515 {
22516 if (s > tail)
22517 {
22518 *fnamelen += (int)(*fnamep - (s + 1));
22519 *fnamep = s + 1;
22520#ifdef VMS
22521 /* cut version from the extension */
22522 s = *fnamep + *fnamelen - 1;
22523 for ( ; s > *fnamep; --s)
22524 if (s[0] == ';')
22525 break;
22526 if (s > *fnamep)
22527 *fnamelen = s - *fnamep;
22528#endif
22529 }
22530 else if (*fnamep <= tail)
22531 *fnamelen = 0;
22532 }
22533 else /* :r */
22534 {
22535 if (s > tail) /* remove one extension */
22536 *fnamelen = (int)(s - *fnamep);
22537 }
22538 *usedlen += 2;
22539 }
22540
22541 /* ":s?pat?foo?" - substitute */
22542 /* ":gs?pat?foo?" - global substitute */
22543 if (src[*usedlen] == ':'
22544 && (src[*usedlen + 1] == 's'
22545 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22546 {
22547 char_u *str;
22548 char_u *pat;
22549 char_u *sub;
22550 int sep;
22551 char_u *flags;
22552 int didit = FALSE;
22553
22554 flags = (char_u *)"";
22555 s = src + *usedlen + 2;
22556 if (src[*usedlen + 1] == 'g')
22557 {
22558 flags = (char_u *)"g";
22559 ++s;
22560 }
22561
22562 sep = *s++;
22563 if (sep)
22564 {
22565 /* find end of pattern */
22566 p = vim_strchr(s, sep);
22567 if (p != NULL)
22568 {
22569 pat = vim_strnsave(s, (int)(p - s));
22570 if (pat != NULL)
22571 {
22572 s = p + 1;
22573 /* find end of substitution */
22574 p = vim_strchr(s, sep);
22575 if (p != NULL)
22576 {
22577 sub = vim_strnsave(s, (int)(p - s));
22578 str = vim_strnsave(*fnamep, *fnamelen);
22579 if (sub != NULL && str != NULL)
22580 {
22581 *usedlen = (int)(p + 1 - src);
22582 s = do_string_sub(str, pat, sub, flags);
22583 if (s != NULL)
22584 {
22585 *fnamep = s;
22586 *fnamelen = (int)STRLEN(s);
22587 vim_free(*bufp);
22588 *bufp = s;
22589 didit = TRUE;
22590 }
22591 }
22592 vim_free(sub);
22593 vim_free(str);
22594 }
22595 vim_free(pat);
22596 }
22597 }
22598 /* after using ":s", repeat all the modifiers */
22599 if (didit)
22600 goto repeat;
22601 }
22602 }
22603
22604 return valid;
22605}
22606
22607/*
22608 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22609 * "flags" can be "g" to do a global substitute.
22610 * Returns an allocated string, NULL for error.
22611 */
22612 char_u *
22613do_string_sub(str, pat, sub, flags)
22614 char_u *str;
22615 char_u *pat;
22616 char_u *sub;
22617 char_u *flags;
22618{
22619 int sublen;
22620 regmatch_T regmatch;
22621 int i;
22622 int do_all;
22623 char_u *tail;
22624 garray_T ga;
22625 char_u *ret;
22626 char_u *save_cpo;
22627
22628 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22629 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022630 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631
22632 ga_init2(&ga, 1, 200);
22633
22634 do_all = (flags[0] == 'g');
22635
22636 regmatch.rm_ic = p_ic;
22637 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22638 if (regmatch.regprog != NULL)
22639 {
22640 tail = str;
22641 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22642 {
22643 /*
22644 * Get some space for a temporary buffer to do the substitution
22645 * into. It will contain:
22646 * - The text up to where the match is.
22647 * - The substituted text.
22648 * - The text after the match.
22649 */
22650 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22651 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22652 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22653 {
22654 ga_clear(&ga);
22655 break;
22656 }
22657
22658 /* copy the text up to where the match is */
22659 i = (int)(regmatch.startp[0] - tail);
22660 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22661 /* add the substituted text */
22662 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22663 + ga.ga_len + i, TRUE, TRUE, FALSE);
22664 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 /* avoid getting stuck on a match with an empty string */
22666 if (tail == regmatch.endp[0])
22667 {
22668 if (*tail == NUL)
22669 break;
22670 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22671 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 }
22673 else
22674 {
22675 tail = regmatch.endp[0];
22676 if (*tail == NUL)
22677 break;
22678 }
22679 if (!do_all)
22680 break;
22681 }
22682
22683 if (ga.ga_data != NULL)
22684 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22685
22686 vim_free(regmatch.regprog);
22687 }
22688
22689 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22690 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022691 if (p_cpo == empty_option)
22692 p_cpo = save_cpo;
22693 else
22694 /* Darn, evaluating {sub} expression changed the value. */
22695 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696
22697 return ret;
22698}
22699
22700#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */