blob: 10732ccade6effef9b3e5cc5e3d8e39170fa9fdc [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.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 */
134static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135#define COPYID_INC 2
136#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137
138/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000142typedef struct
143{
144 dictitem_T sv_var;
145 dict_T sv_dict;
146} scriptvar_T;
147
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200148static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
149#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
150#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152static int echo_attr = 0; /* attributes used for ":echo" */
153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000154/* Values for trans_function_name() argument: */
155#define TFN_INT 1 /* internal function name OK */
156#define TFN_QUIET 2 /* no error messages */
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Structure to hold info for a user function.
160 */
161typedef struct ufunc ufunc_T;
162
163struct ufunc
164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000165 int uf_varargs; /* variable nr of arguments */
166 int uf_flags;
167 int uf_calls; /* nr of active calls */
168 garray_T uf_args; /* arguments */
169 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170#ifdef FEAT_PROFILE
171 int uf_profiling; /* TRUE when func is being profiled */
172 /* profiling the function as a whole */
173 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000174 proftime_T uf_tm_total; /* time spent in function + children */
175 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176 proftime_T uf_tm_children; /* time spent in children this call */
177 /* profiling the function per line */
178 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000179 proftime_T *uf_tml_total; /* time spent in a line + children */
180 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000181 proftime_T uf_tml_start; /* start time for current line */
182 proftime_T uf_tml_children; /* time spent in children for this line */
183 proftime_T uf_tml_wait; /* start wait time for current line */
184 int uf_tml_idx; /* index of line being timed; -1 if none */
185 int uf_tml_execed; /* line being timed was executed */
186#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000187 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 int uf_refcount; /* for numbered function: reference count */
190 char_u uf_name[1]; /* name of function (actually longer); can
191 start with <SNR>123_ (<SNR> is K_SPECIAL
192 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193};
194
195/* function flags */
196#define FC_ABORT 1 /* abort function on error */
197#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000198#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000203static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000205/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000206static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
207
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208/* list heads for garbage collection */
209static dict_T *first_dict = NULL; /* list of all dicts */
210static list_T *first_list = NULL; /* list of all lists */
211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000212/* From user function to hashitem and back. */
213static ufunc_T dumuf;
214#define UF2HIKEY(fp) ((fp)->uf_name)
215#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
216#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
217
218#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
219#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
Bram Moolenaar33570922005-01-25 22:26:29 +0000221#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
222#define VAR_SHORT_LEN 20 /* short variable name length */
223#define FIXVAR_CNT 12 /* number of fixed variables */
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000226typedef struct funccall_S funccall_T;
227
228struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229{
230 ufunc_T *func; /* function being called */
231 int linenr; /* next line to be executed */
232 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000233 struct /* fixed variables for arguments */
234 {
235 dictitem_T var; /* variable (without room for name) */
236 char_u room[VAR_SHORT_LEN]; /* room for the name */
237 } fixvar[FIXVAR_CNT];
238 dict_T l_vars; /* l: local function variables */
239 dictitem_T l_vars_var; /* variable for l: scope */
240 dict_T l_avars; /* a: argument variables */
241 dictitem_T l_avars_var; /* variable for a: scope */
242 list_T l_varlist; /* list for a:000 */
243 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
244 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 linenr_T breakpoint; /* next line with breakpoint or zero */
246 int dbg_tick; /* debug_tick when breakpoint was set */
247 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000248#ifdef FEAT_PROFILE
249 proftime_T prof_child; /* time spent in a child */
250#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000251 funccall_T *caller; /* calling function or NULL */
252};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253
254/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000255 * Info used by a ":for" loop.
256 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000257typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258{
259 int fi_semicolon; /* TRUE if ending in '; var]' */
260 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261 listwatch_T fi_lw; /* keep an eye on the item used. */
262 list_T *fi_list; /* list being used */
263} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000266 * Struct used by trans_function_name()
267 */
268typedef struct
269{
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000271 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dictitem_T *fd_di; /* Dictionary item used */
273} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000274
Bram Moolenaara7043832005-01-21 11:56:39 +0000275
276/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 * Array to hold the value of v: variables.
278 * The value is in a dictitem, so that it can also be used in the v: scope.
279 * The reason to use this table anyway is for very quick access to the
280 * variables with the VV_ defines.
281 */
282#include "version.h"
283
284/* values for vv_flags: */
285#define VV_COMPAT 1 /* compatible, also used without "v:" */
286#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000287#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000289#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
291static struct vimvar
292{
293 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294 dictitem_T vv_di; /* value and name for key */
295 char vv_filler[16]; /* space for LONGEST name below!!! */
296 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
297} vimvars[VV_LEN] =
298{
299 /*
300 * The order here must match the VV_ defines in vim.h!
301 * Initializing a union does not work, leave tv.vval empty to get zero's.
302 */
303 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("count1", VAR_NUMBER), VV_RO},
305 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
306 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
307 {VV_NAME("warningmsg", VAR_STRING), 0},
308 {VV_NAME("statusmsg", VAR_STRING), 0},
309 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
311 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
313 {VV_NAME("termresponse", VAR_STRING), VV_RO},
314 {VV_NAME("fname", VAR_STRING), VV_RO},
315 {VV_NAME("lang", VAR_STRING), VV_RO},
316 {VV_NAME("lc_time", VAR_STRING), VV_RO},
317 {VV_NAME("ctype", VAR_STRING), VV_RO},
318 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
319 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
320 {VV_NAME("fname_in", VAR_STRING), VV_RO},
321 {VV_NAME("fname_out", VAR_STRING), VV_RO},
322 {VV_NAME("fname_new", VAR_STRING), VV_RO},
323 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
324 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
325 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
327 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
328 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("progname", VAR_STRING), VV_RO},
330 {VV_NAME("servername", VAR_STRING), VV_RO},
331 {VV_NAME("dying", VAR_NUMBER), VV_RO},
332 {VV_NAME("exception", VAR_STRING), VV_RO},
333 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
334 {VV_NAME("register", VAR_STRING), VV_RO},
335 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
336 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000337 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
338 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000339 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000340 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
341 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000342 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
344 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000347 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000348 {VV_NAME("swapname", VAR_STRING), VV_RO},
349 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000350 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000351 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000352 {VV_NAME("mouse_win", VAR_NUMBER), 0},
353 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
354 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000355 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000356 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000357 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000358};
359
360/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361#define vv_type vv_di.di_tv.v_type
362#define vv_nr vv_di.di_tv.vval.v_number
363#define vv_float vv_di.di_tv.vval.v_float
364#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000365#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000367
368/*
369 * The v: variables are stored in dictionary "vimvardict".
370 * "vimvars_var" is the variable that is used for the "l:" scope.
371 */
372static dict_T vimvardict;
373static dictitem_T vimvars_var;
374#define vimvarht vimvardict.dv_hashtab
375
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
377static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
378#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
379static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
380#endif
381static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
382static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
383static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
385static void list_glob_vars __ARGS((int *first));
386static void list_buf_vars __ARGS((int *first));
387static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_vim_vars __ARGS((int *first));
392static void list_script_vars __ARGS((int *first));
393static void list_func_vars __ARGS((int *first));
394static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000395static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
396static int check_changedtick __ARGS((char_u *arg));
397static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
398static void clear_lval __ARGS((lval_T *lp));
399static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
400static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
401static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
402static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
403static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
404static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
405static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
406static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
407static void item_lock __ARGS((typval_T *tv, int deep, int lock));
408static int tv_islocked __ARGS((typval_T *tv));
409
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
411static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000416static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
417static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000419static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000424static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static listitem_T *listitem_alloc __ARGS((void));
426static void listitem_free __ARGS((listitem_T *item));
427static void listitem_remove __ARGS((list_T *l, listitem_T *item));
428static long list_len __ARGS((list_T *l));
429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000433static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
438static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
439static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000440static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *list2string __ARGS((typval_T *tv, int copyID));
443static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000444static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000445static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
446static void set_ref_in_list __ARGS((list_T *l, int copyID));
447static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000449static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
451static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000452static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000454static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000456static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
457static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#ifdef FEAT_FLOAT
460static int string2float __ARGS((char_u *text, float_T *value));
461#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
463static int find_internal_func __ARGS((char_u *name));
464static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
465static 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));
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +0200466static int call_func __ARGS((char_u *func_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 +0000467static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000468static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
471static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200472static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#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
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200480static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#ifdef FEAT_FLOAT
496static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000498static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000501static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000503#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000504static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
507#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000510#ifdef FEAT_FLOAT
511static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200512static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
517static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200528#ifdef FEAT_FLOAT
529static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
530#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000531static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000533static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000539#ifdef FEAT_FLOAT
540static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200542static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000544static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000553static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000555static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000556static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000561static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000569static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000570static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000571static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000572static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200575static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000576static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000584static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000598static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000604static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200617static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000618static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000624static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000625static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000626static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000628static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000632#ifdef vim_mkdir
633static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
634#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100636#ifdef FEAT_MZSCHEME
637static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000641static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
643static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000646static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000647static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000649static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000650static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000662#ifdef FEAT_FLOAT
663static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
664#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000666static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000668static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000675static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000676static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000677static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000678static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200680static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000681static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000683static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000685#ifdef FEAT_FLOAT
686static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200687static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000690static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000691static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
697#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000698static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699#ifdef HAVE_STRFTIME
700static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
701#endif
702static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000713static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000715static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000716static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000717static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000718static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000719static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000721static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200722#ifdef FEAT_FLOAT
723static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
725#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#ifdef FEAT_FLOAT
730static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
731#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200733static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000743static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000746static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000748static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000749static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static int get_env_len __ARGS((char_u **arg));
751static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000752static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000753static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
754#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
755#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
756 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000757static 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 +0000758static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000759static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
761static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static typval_T *alloc_tv __ARGS((void));
763static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000764static void init_tv __ARGS((typval_T *varp));
765static long get_tv_number __ARGS((typval_T *varp));
766static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000767static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static char_u *get_tv_string __ARGS((typval_T *varp));
769static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000770static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000772static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
774static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
775static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000776static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
777static 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 +0000778static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
779static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000780static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000781static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000782static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
784static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
785static int eval_fname_script __ARGS((char_u *p));
786static int eval_fname_sid __ARGS((char_u *p));
787static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static ufunc_T *find_func __ARGS((char_u *name));
789static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000790static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791#ifdef FEAT_PROFILE
792static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000793static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
794static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
795static int
796# ifdef __BORLANDC__
797 _RTLENTRYF
798# endif
799 prof_total_cmp __ARGS((const void *s1, const void *s2));
800static int
801# ifdef __BORLANDC__
802 _RTLENTRYF
803# endif
804 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000806static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000807static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000808static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static void func_free __ARGS((ufunc_T *fp));
810static void func_unref __ARGS((char_u *name));
811static void func_ref __ARGS((char_u *name));
812static 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 +0000813static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
814static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000815static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000816static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
817static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000818static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000819static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000820static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200822
823#ifdef EBCDIC
824static int compare_func_name __ARGS((const void *s1, const void *s2));
825static void sortFunctions __ARGS(());
826#endif
827
828
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000829/* Character used as separated in autoload function/variable names. */
830#define AUTOLOAD_CHAR '#'
831
Bram Moolenaar33570922005-01-25 22:26:29 +0000832/*
833 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000834 */
835 void
836eval_init()
837{
Bram Moolenaar33570922005-01-25 22:26:29 +0000838 int i;
839 struct vimvar *p;
840
841 init_var_dict(&globvardict, &globvars_var);
842 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000843 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000844 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000845
846 for (i = 0; i < VV_LEN; ++i)
847 {
848 p = &vimvars[i];
849 STRCPY(p->vv_di.di_key, p->vv_name);
850 if (p->vv_flags & VV_RO)
851 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
852 else if (p->vv_flags & VV_RO_SBX)
853 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
854 else
855 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000856
857 /* add to v: scope dict, unless the value is not always available */
858 if (p->vv_type != VAR_UNKNOWN)
859 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000860 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000861 /* add to compat scope dict */
862 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000864 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200865
866#ifdef EBCDIC
867 /*
868 * Sort the function table, to enable binary sort.
869 */
870 sortFunctions();
871#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000872}
873
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000874#if defined(EXITFREE) || defined(PROTO)
875 void
876eval_clear()
877{
878 int i;
879 struct vimvar *p;
880
881 for (i = 0; i < VV_LEN; ++i)
882 {
883 p = &vimvars[i];
884 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000885 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000886 vim_free(p->vv_str);
887 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000888 }
889 else if (p->vv_di.di_tv.v_type == VAR_LIST)
890 {
891 list_unref(p->vv_list);
892 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000893 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000894 }
895 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000896 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000897 hash_clear(&compat_hashtab);
898
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000900
901 /* global variables */
902 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000903
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000904 /* autoloaded script names */
905 ga_clear_strings(&ga_loaded);
906
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200907 /* script-local variables */
908 for (i = 1; i <= ga_scripts.ga_len; ++i)
909 {
910 vars_clear(&SCRIPT_VARS(i));
911 vim_free(SCRIPT_SV(i));
912 }
913 ga_clear(&ga_scripts);
914
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000915 /* unreferenced lists and dicts */
916 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000917
918 /* functions */
919 free_all_functions();
920 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921}
922#endif
923
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 * Return the name of the executed function.
926 */
927 char_u *
928func_name(cookie)
929 void *cookie;
930{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000931 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932}
933
934/*
935 * Return the address holding the next breakpoint line for a funccall cookie.
936 */
937 linenr_T *
938func_breakpoint(cookie)
939 void *cookie;
940{
Bram Moolenaar33570922005-01-25 22:26:29 +0000941 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942}
943
944/*
945 * Return the address holding the debug tick for a funccall cookie.
946 */
947 int *
948func_dbg_tick(cookie)
949 void *cookie;
950{
Bram Moolenaar33570922005-01-25 22:26:29 +0000951 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952}
953
954/*
955 * Return the nesting level for a funccall cookie.
956 */
957 int
958func_level(cookie)
959 void *cookie;
960{
Bram Moolenaar33570922005-01-25 22:26:29 +0000961 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000965funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000967/* pointer to list of previously used funccal, still around because some
968 * item in it is still being used. */
969funccall_T *previous_funccal = NULL;
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/*
972 * Return TRUE when a function was ended by a ":return" command.
973 */
974 int
975current_func_returned()
976{
977 return current_funccal->returned;
978}
979
980
981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 * Set an internal variable to a string value. Creates the variable if it does
983 * not already exist.
984 */
985 void
986set_internal_string_var(name, value)
987 char_u *name;
988 char_u *value;
989{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000990 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000991 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
993 val = vim_strsave(value);
994 if (val != NULL)
995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000996 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000997 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000999 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001000 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 }
1003}
1004
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001005static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001006static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007static char_u *redir_endp = NULL;
1008static char_u *redir_varname = NULL;
1009
1010/*
1011 * Start recording command output to a variable
1012 * Returns OK if successfully completed the setup. FAIL otherwise.
1013 */
1014 int
1015var_redir_start(name, append)
1016 char_u *name;
1017 int append; /* append to an existing variable */
1018{
1019 int save_emsg;
1020 int err;
1021 typval_T tv;
1022
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001023 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001024 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025 {
1026 EMSG(_(e_invarg));
1027 return FAIL;
1028 }
1029
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001030 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031 redir_varname = vim_strsave(name);
1032 if (redir_varname == NULL)
1033 return FAIL;
1034
1035 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1036 if (redir_lval == NULL)
1037 {
1038 var_redir_stop();
1039 return FAIL;
1040 }
1041
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001042 /* The output is stored in growarray "redir_ga" until redirection ends. */
1043 ga_init2(&redir_ga, (int)sizeof(char), 500);
1044
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001046 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1047 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001048 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1049 {
1050 if (redir_endp != NULL && *redir_endp != NUL)
1051 /* Trailing characters are present after the variable name */
1052 EMSG(_(e_trailing));
1053 else
1054 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001055 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 var_redir_stop();
1057 return FAIL;
1058 }
1059
1060 /* check if we can write to the variable: set it to or append an empty
1061 * string */
1062 save_emsg = did_emsg;
1063 did_emsg = FALSE;
1064 tv.v_type = VAR_STRING;
1065 tv.vval.v_string = (char_u *)"";
1066 if (append)
1067 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1068 else
1069 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1070 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001071 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (err)
1073 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 var_redir_stop();
1076 return FAIL;
1077 }
1078 if (redir_lval->ll_newkey != NULL)
1079 {
1080 /* Dictionary item was created, don't do it again. */
1081 vim_free(redir_lval->ll_newkey);
1082 redir_lval->ll_newkey = NULL;
1083 }
1084
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001089 * Append "value[value_len]" to the variable set by var_redir_start().
1090 * The actual appending is postponed until redirection ends, because the value
1091 * appended may in fact be the string we write to, changing it may cause freed
1092 * memory to be used:
1093 * :redir => foo
1094 * :let foo
1095 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 */
1097 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001098var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001099 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001102 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103
1104 if (redir_lval == NULL)
1105 return;
1106
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001108 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001110 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001113 {
1114 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 }
1117 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119}
1120
1121/*
1122 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001123 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 */
1125 void
1126var_redir_stop()
1127{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 typval_T tv;
1129
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 if (redir_lval != NULL)
1131 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001132 /* If there was no error: assign the text to the variable. */
1133 if (redir_endp != NULL)
1134 {
1135 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1136 tv.v_type = VAR_STRING;
1137 tv.vval.v_string = redir_ga.ga_data;
1138 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1139 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 /* free the collected output */
1142 vim_free(redir_ga.ga_data);
1143 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001144
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 clear_lval(redir_lval);
1146 vim_free(redir_lval);
1147 redir_lval = NULL;
1148 }
1149 vim_free(redir_varname);
1150 redir_varname = NULL;
1151}
1152
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153# if defined(FEAT_MBYTE) || defined(PROTO)
1154 int
1155eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1156 char_u *enc_from;
1157 char_u *enc_to;
1158 char_u *fname_from;
1159 char_u *fname_to;
1160{
1161 int err = FALSE;
1162
1163 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1164 set_vim_var_string(VV_CC_TO, enc_to, -1);
1165 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1166 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1167 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1168 err = TRUE;
1169 set_vim_var_string(VV_CC_FROM, NULL, -1);
1170 set_vim_var_string(VV_CC_TO, NULL, -1);
1171 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1172 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1173
1174 if (err)
1175 return FAIL;
1176 return OK;
1177}
1178# endif
1179
1180# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1181 int
1182eval_printexpr(fname, args)
1183 char_u *fname;
1184 char_u *args;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_FNAME_IN, fname, -1);
1189 set_vim_var_string(VV_CMDARG, args, -1);
1190 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1191 err = TRUE;
1192 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1193 set_vim_var_string(VV_CMDARG, NULL, -1);
1194
1195 if (err)
1196 {
1197 mch_remove(fname);
1198 return FAIL;
1199 }
1200 return OK;
1201}
1202# endif
1203
1204# if defined(FEAT_DIFF) || defined(PROTO)
1205 void
1206eval_diff(origfile, newfile, outfile)
1207 char_u *origfile;
1208 char_u *newfile;
1209 char_u *outfile;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1214 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1215 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1216 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1219 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1220}
1221
1222 void
1223eval_patch(origfile, difffile, outfile)
1224 char_u *origfile;
1225 char_u *difffile;
1226 char_u *outfile;
1227{
1228 int err;
1229
1230 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1231 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1232 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1233 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1234 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1235 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1236 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1237}
1238# endif
1239
1240/*
1241 * Top level evaluation function, returning a boolean.
1242 * Sets "error" to TRUE if there was an error.
1243 * Return TRUE or FALSE.
1244 */
1245 int
1246eval_to_bool(arg, error, nextcmd, skip)
1247 char_u *arg;
1248 int *error;
1249 char_u **nextcmd;
1250 int skip; /* only parse, don't execute */
1251{
Bram Moolenaar33570922005-01-25 22:26:29 +00001252 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 int retval = FALSE;
1254
1255 if (skip)
1256 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001257 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 else
1260 {
1261 *error = FALSE;
1262 if (!skip)
1263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001264 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267 }
1268 if (skip)
1269 --emsg_skip;
1270
1271 return retval;
1272}
1273
1274/*
1275 * Top level evaluation function, returning a string. If "skip" is TRUE,
1276 * only parsing to "nextcmd" is done, without reporting errors. Return
1277 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1278 */
1279 char_u *
1280eval_to_string_skip(arg, nextcmd, skip)
1281 char_u *arg;
1282 char_u **nextcmd;
1283 int skip; /* only parse, don't execute */
1284{
Bram Moolenaar33570922005-01-25 22:26:29 +00001285 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 char_u *retval;
1287
1288 if (skip)
1289 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 retval = NULL;
1292 else
1293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001294 retval = vim_strsave(get_tv_string(&tv));
1295 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 }
1297 if (skip)
1298 --emsg_skip;
1299
1300 return retval;
1301}
1302
1303/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001304 * Skip over an expression at "*pp".
1305 * Return FAIL for an error, OK otherwise.
1306 */
1307 int
1308skip_expr(pp)
1309 char_u **pp;
1310{
Bram Moolenaar33570922005-01-25 22:26:29 +00001311 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001312
1313 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001314 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001315}
1316
1317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001319 * When "convert" is TRUE convert a List into a sequence of lines and convert
1320 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 * Return pointer to allocated memory, or NULL for failure.
1322 */
1323 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001324eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 char_u *arg;
1326 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001327 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
Bram Moolenaar33570922005-01-25 22:26:29 +00001329 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001331 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001332#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001333 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 retval = NULL;
1338 else
1339 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001341 {
1342 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001343 if (tv.vval.v_list != NULL)
1344 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001345 ga_append(&ga, NUL);
1346 retval = (char_u *)ga.ga_data;
1347 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348#ifdef FEAT_FLOAT
1349 else if (convert && tv.v_type == VAR_FLOAT)
1350 {
1351 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1352 retval = vim_strsave(numbuf);
1353 }
1354#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001355 else
1356 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359
1360 return retval;
1361}
1362
1363/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001364 * Call eval_to_string() without using current local variables and using
1365 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 */
1367 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001368eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 char_u *arg;
1370 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001371 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
1373 char_u *retval;
1374 void *save_funccalp;
1375
1376 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001377 if (use_sandbox)
1378 ++sandbox;
1379 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001380 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001381 if (use_sandbox)
1382 --sandbox;
1383 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 restore_funccal(save_funccalp);
1385 return retval;
1386}
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388/*
1389 * Top level evaluation function, returning a number.
1390 * Evaluates "expr" silently.
1391 * Returns -1 for an error.
1392 */
1393 int
1394eval_to_number(expr)
1395 char_u *expr;
1396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001399 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
1401 ++emsg_off;
1402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 retval = -1;
1405 else
1406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001407 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 }
1410 --emsg_off;
1411
1412 return retval;
1413}
1414
Bram Moolenaara40058a2005-07-11 22:42:07 +00001415/*
1416 * Prepare v: variable "idx" to be used.
1417 * Save the current typeval in "save_tv".
1418 * When not used yet add the variable to the v: hashtable.
1419 */
1420 static void
1421prepare_vimvar(idx, save_tv)
1422 int idx;
1423 typval_T *save_tv;
1424{
1425 *save_tv = vimvars[idx].vv_tv;
1426 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1427 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1428}
1429
1430/*
1431 * Restore v: variable "idx" to typeval "save_tv".
1432 * When no longer defined, remove the variable from the v: hashtable.
1433 */
1434 static void
1435restore_vimvar(idx, save_tv)
1436 int idx;
1437 typval_T *save_tv;
1438{
1439 hashitem_T *hi;
1440
Bram Moolenaara40058a2005-07-11 22:42:07 +00001441 vimvars[idx].vv_tv = *save_tv;
1442 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1443 {
1444 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1445 if (HASHITEM_EMPTY(hi))
1446 EMSG2(_(e_intern2), "restore_vimvar()");
1447 else
1448 hash_remove(&vimvarht, hi);
1449 }
1450}
1451
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001452#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001453/*
1454 * Evaluate an expression to a list with suggestions.
1455 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001456 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001457 */
1458 list_T *
1459eval_spell_expr(badword, expr)
1460 char_u *badword;
1461 char_u *expr;
1462{
1463 typval_T save_val;
1464 typval_T rettv;
1465 list_T *list = NULL;
1466 char_u *p = skipwhite(expr);
1467
1468 /* Set "v:val" to the bad word. */
1469 prepare_vimvar(VV_VAL, &save_val);
1470 vimvars[VV_VAL].vv_type = VAR_STRING;
1471 vimvars[VV_VAL].vv_str = badword;
1472 if (p_verbose == 0)
1473 ++emsg_off;
1474
1475 if (eval1(&p, &rettv, TRUE) == OK)
1476 {
1477 if (rettv.v_type != VAR_LIST)
1478 clear_tv(&rettv);
1479 else
1480 list = rettv.vval.v_list;
1481 }
1482
1483 if (p_verbose == 0)
1484 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001485 restore_vimvar(VV_VAL, &save_val);
1486
1487 return list;
1488}
1489
1490/*
1491 * "list" is supposed to contain two items: a word and a number. Return the
1492 * word in "pp" and the number as the return value.
1493 * Return -1 if anything isn't right.
1494 * Used to get the good word and score from the eval_spell_expr() result.
1495 */
1496 int
1497get_spellword(list, pp)
1498 list_T *list;
1499 char_u **pp;
1500{
1501 listitem_T *li;
1502
1503 li = list->lv_first;
1504 if (li == NULL)
1505 return -1;
1506 *pp = get_tv_string(&li->li_tv);
1507
1508 li = li->li_next;
1509 if (li == NULL)
1510 return -1;
1511 return get_tv_number(&li->li_tv);
1512}
1513#endif
1514
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001515/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001516 * Top level evaluation function.
1517 * Returns an allocated typval_T with the result.
1518 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001519 */
1520 typval_T *
1521eval_expr(arg, nextcmd)
1522 char_u *arg;
1523 char_u **nextcmd;
1524{
1525 typval_T *tv;
1526
1527 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001528 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001529 {
1530 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001531 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001532 }
1533
1534 return tv;
1535}
1536
1537
Bram Moolenaar4f688582007-07-24 12:34:30 +00001538#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1539 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001541 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001542 * Uses argv[argc] for the function arguments. Only Number and String
1543 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001544 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001546 static int
1547call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 char_u *func;
1549 int argc;
1550 char_u **argv;
1551 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
Bram Moolenaar33570922005-01-25 22:26:29 +00001554 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 long n;
1556 int len;
1557 int i;
1558 int doesrange;
1559 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001560 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001562 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
1566 for (i = 0; i < argc; i++)
1567 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001568 /* Pass a NULL or empty argument as an empty string */
1569 if (argv[i] == NULL || *argv[i] == NUL)
1570 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001571 argvars[i].v_type = VAR_STRING;
1572 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001573 continue;
1574 }
1575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 /* Recognize a number argument, the others must be strings. */
1577 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1578 if (len != 0 && len == (int)STRLEN(argv[i]))
1579 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001580 argvars[i].v_type = VAR_NUMBER;
1581 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583 else
1584 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001585 argvars[i].v_type = VAR_STRING;
1586 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 }
1589
1590 if (safe)
1591 {
1592 save_funccalp = save_funccal();
1593 ++sandbox;
1594 }
1595
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001596 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1597 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if (safe)
1601 {
1602 --sandbox;
1603 restore_funccal(save_funccalp);
1604 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001605 vim_free(argvars);
1606
1607 if (ret == FAIL)
1608 clear_tv(rettv);
1609
1610 return ret;
1611}
1612
Bram Moolenaar4f688582007-07-24 12:34:30 +00001613# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001615 * Call vimL function "func" and return the result as a string.
1616 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 * Uses argv[argc] for the function arguments.
1618 */
1619 void *
1620call_func_retstr(func, argc, argv, safe)
1621 char_u *func;
1622 int argc;
1623 char_u **argv;
1624 int safe; /* use the sandbox */
1625{
1626 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001627 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628
1629 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1630 return NULL;
1631
1632 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 return retval;
1635}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001636# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637
Bram Moolenaar4f688582007-07-24 12:34:30 +00001638# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001639/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001640 * Call vimL function "func" and return the result as a number.
1641 * Returns -1 when calling the function fails.
1642 * Uses argv[argc] for the function arguments.
1643 */
1644 long
1645call_func_retnr(func, argc, argv, safe)
1646 char_u *func;
1647 int argc;
1648 char_u **argv;
1649 int safe; /* use the sandbox */
1650{
1651 typval_T rettv;
1652 long retval;
1653
1654 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1655 return -1;
1656
1657 retval = get_tv_number_chk(&rettv, NULL);
1658 clear_tv(&rettv);
1659 return retval;
1660}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001661# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001662
1663/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001664 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001666 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 */
1668 void *
1669call_func_retlist(func, argc, argv, safe)
1670 char_u *func;
1671 int argc;
1672 char_u **argv;
1673 int safe; /* use the sandbox */
1674{
1675 typval_T rettv;
1676
1677 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1678 return NULL;
1679
1680 if (rettv.v_type != VAR_LIST)
1681 {
1682 clear_tv(&rettv);
1683 return NULL;
1684 }
1685
1686 return rettv.vval.v_list;
1687}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688#endif
1689
Bram Moolenaar4f688582007-07-24 12:34:30 +00001690
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691/*
1692 * Save the current function call pointer, and set it to NULL.
1693 * Used when executing autocommands and for ":source".
1694 */
1695 void *
1696save_funccal()
1697{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001698 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 current_funccal = NULL;
1701 return (void *)fc;
1702}
1703
1704 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001705restore_funccal(vfc)
1706 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001708 funccall_T *fc = (funccall_T *)vfc;
1709
1710 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711}
1712
Bram Moolenaar05159a02005-02-26 23:04:13 +00001713#if defined(FEAT_PROFILE) || defined(PROTO)
1714/*
1715 * Prepare profiling for entering a child or something else that is not
1716 * counted for the script/function itself.
1717 * Should always be called in pair with prof_child_exit().
1718 */
1719 void
1720prof_child_enter(tm)
1721 proftime_T *tm; /* place to store waittime */
1722{
1723 funccall_T *fc = current_funccal;
1724
1725 if (fc != NULL && fc->func->uf_profiling)
1726 profile_start(&fc->prof_child);
1727 script_prof_save(tm);
1728}
1729
1730/*
1731 * Take care of time spent in a child.
1732 * Should always be called after prof_child_enter().
1733 */
1734 void
1735prof_child_exit(tm)
1736 proftime_T *tm; /* where waittime was stored */
1737{
1738 funccall_T *fc = current_funccal;
1739
1740 if (fc != NULL && fc->func->uf_profiling)
1741 {
1742 profile_end(&fc->prof_child);
1743 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1744 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1745 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1746 }
1747 script_prof_restore(tm);
1748}
1749#endif
1750
1751
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752#ifdef FEAT_FOLDING
1753/*
1754 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1755 * it in "*cp". Doesn't give error messages.
1756 */
1757 int
1758eval_foldexpr(arg, cp)
1759 char_u *arg;
1760 int *cp;
1761{
Bram Moolenaar33570922005-01-25 22:26:29 +00001762 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 int retval;
1764 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001765 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1766 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
1768 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001769 if (use_sandbox)
1770 ++sandbox;
1771 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 retval = 0;
1775 else
1776 {
1777 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 if (tv.v_type == VAR_NUMBER)
1779 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001780 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 retval = 0;
1782 else
1783 {
1784 /* If the result is a string, check if there is a non-digit before
1785 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001786 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (!VIM_ISDIGIT(*s) && *s != '-')
1788 *cp = *s++;
1789 retval = atol((char *)s);
1790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
1793 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001794 if (use_sandbox)
1795 --sandbox;
1796 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 return retval;
1799}
1800#endif
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 * ":let" list all variable values
1804 * ":let var1 var2" list variable values
1805 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001806 * ":let var += expr" assignment command.
1807 * ":let var -= expr" assignment command.
1808 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 */
1811 void
1812ex_let(eap)
1813 exarg_T *eap;
1814{
1815 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001817 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 int var_count = 0;
1820 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001822 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001823 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
Bram Moolenaardb552d602006-03-23 22:59:57 +00001825 argend = skip_var_list(arg, &var_count, &semicolon);
1826 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001828 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1829 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001830 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001833 /*
1834 * ":let" without "=": list variables
1835 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 if (*arg == '[')
1837 EMSG(_(e_invarg));
1838 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001840 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001844 list_glob_vars(&first);
1845 list_buf_vars(&first);
1846 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001847#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001848 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001849#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001850 list_script_vars(&first);
1851 list_func_vars(&first);
1852 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 eap->nextcmd = check_nextcmd(arg);
1855 }
1856 else
1857 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001858 op[0] = '=';
1859 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001860 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001861 {
1862 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1863 op[0] = expr[-1]; /* +=, -= or .= */
1864 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (eap->skip)
1868 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (eap->skip)
1871 {
1872 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 --emsg_skip;
1875 }
1876 else if (i != FAIL)
1877 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
1882 }
1883}
1884
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885/*
1886 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1887 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 * When "nextchars" is not NULL it points to a string with characters that
1889 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1890 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 * Returns OK or FAIL;
1892 */
1893 static int
1894ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1895 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 int copy; /* copy values from "tv", don't move */
1898 int semicolon; /* from skip_var_list() */
1899 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901{
1902 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001903 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 listitem_T *item;
1906 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907
1908 if (*arg != '[')
1909 {
1910 /*
1911 * ":let var = expr" or ":for var in list"
1912 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 return FAIL;
1915 return OK;
1916 }
1917
1918 /*
1919 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1920 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001921 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 {
1923 EMSG(_(e_listreq));
1924 return FAIL;
1925 }
1926
1927 i = list_len(l);
1928 if (semicolon == 0 && var_count < i)
1929 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001930 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 return FAIL;
1932 }
1933 if (var_count - semicolon > i)
1934 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001935 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 return FAIL;
1937 }
1938
1939 item = l->lv_first;
1940 while (*arg != ']')
1941 {
1942 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 item = item->li_next;
1945 if (arg == NULL)
1946 return FAIL;
1947
1948 arg = skipwhite(arg);
1949 if (*arg == ';')
1950 {
1951 /* Put the rest of the list (may be empty) in the var after ';'.
1952 * Create a new list for this. */
1953 l = list_alloc();
1954 if (l == NULL)
1955 return FAIL;
1956 while (item != NULL)
1957 {
1958 list_append_tv(l, &item->li_tv);
1959 item = item->li_next;
1960 }
1961
1962 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001963 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 ltv.vval.v_list = l;
1965 l->lv_refcount = 1;
1966
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1968 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 clear_tv(&ltv);
1970 if (arg == NULL)
1971 return FAIL;
1972 break;
1973 }
1974 else if (*arg != ',' && *arg != ']')
1975 {
1976 EMSG2(_(e_intern2), "ex_let_vars()");
1977 return FAIL;
1978 }
1979 }
1980
1981 return OK;
1982}
1983
1984/*
1985 * Skip over assignable variable "var" or list of variables "[var, var]".
1986 * Used for ":let varvar = expr" and ":for varvar in expr".
1987 * For "[var, var]" increment "*var_count" for each variable.
1988 * for "[var, var; var]" set "semicolon".
1989 * Return NULL for an error.
1990 */
1991 static char_u *
1992skip_var_list(arg, var_count, semicolon)
1993 char_u *arg;
1994 int *var_count;
1995 int *semicolon;
1996{
1997 char_u *p, *s;
1998
1999 if (*arg == '[')
2000 {
2001 /* "[var, var]": find the matching ']'. */
2002 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002003 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 {
2005 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2006 s = skip_var_one(p);
2007 if (s == p)
2008 {
2009 EMSG2(_(e_invarg2), p);
2010 return NULL;
2011 }
2012 ++*var_count;
2013
2014 p = skipwhite(s);
2015 if (*p == ']')
2016 break;
2017 else if (*p == ';')
2018 {
2019 if (*semicolon == 1)
2020 {
2021 EMSG(_("Double ; in list of variables"));
2022 return NULL;
2023 }
2024 *semicolon = 1;
2025 }
2026 else if (*p != ',')
2027 {
2028 EMSG2(_(e_invarg2), p);
2029 return NULL;
2030 }
2031 }
2032 return p + 1;
2033 }
2034 else
2035 return skip_var_one(arg);
2036}
2037
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002038/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002039 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002040 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 static char_u *
2043skip_var_one(arg)
2044 char_u *arg;
2045{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002046 if (*arg == '@' && arg[1] != NUL)
2047 return arg + 2;
2048 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2049 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050}
2051
Bram Moolenaara7043832005-01-21 11:56:39 +00002052/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002053 * List variables for hashtab "ht" with prefix "prefix".
2054 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002055 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002057list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002058 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002059 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002060 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002061 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002062{
Bram Moolenaar33570922005-01-25 22:26:29 +00002063 hashitem_T *hi;
2064 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002065 int todo;
2066
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002067 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002068 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2069 {
2070 if (!HASHITEM_EMPTY(hi))
2071 {
2072 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 di = HI2DI(hi);
2074 if (empty || di->di_tv.v_type != VAR_STRING
2075 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002076 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 }
2078 }
2079}
2080
2081/*
2082 * List global variables.
2083 */
2084 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085list_glob_vars(first)
2086 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002088 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002089}
2090
2091/*
2092 * List buffer variables.
2093 */
2094 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095list_buf_vars(first)
2096 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002097{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098 char_u numbuf[NUMBUFLEN];
2099
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2101 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002102
2103 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2105 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002106}
2107
2108/*
2109 * List window variables.
2110 */
2111 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112list_win_vars(first)
2113 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002114{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2116 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002117}
2118
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002119#ifdef FEAT_WINDOWS
2120/*
2121 * List tab page variables.
2122 */
2123 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124list_tab_vars(first)
2125 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002126{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2128 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002129}
2130#endif
2131
Bram Moolenaara7043832005-01-21 11:56:39 +00002132/*
2133 * List Vim variables.
2134 */
2135 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136list_vim_vars(first)
2137 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140}
2141
2142/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002143 * List script-local variables, if there is a script.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_script_vars(first)
2147 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002148{
2149 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2151 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002152}
2153
2154/*
2155 * List function variables, if there is a function.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_func_vars(first)
2159 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002160{
2161 if (current_funccal != NULL)
2162 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164}
2165
2166/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167 * List variables in "arg".
2168 */
2169 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171 exarg_T *eap;
2172 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174{
2175 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002176 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002178 char_u *name_start;
2179 char_u *arg_subsc;
2180 char_u *tofree;
2181 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182
2183 while (!ends_excmd(*arg) && !got_int)
2184 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002185 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002187 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2189 {
2190 emsg_severe = TRUE;
2191 EMSG(_(e_trailing));
2192 break;
2193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002195 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 /* get_name_len() takes care of expanding curly braces */
2198 name_start = name = arg;
2199 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2200 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 /* This is mainly to keep test 49 working: when expanding
2203 * curly braces fails overrule the exception error message. */
2204 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 emsg_severe = TRUE;
2207 EMSG2(_(e_invarg2), arg);
2208 break;
2209 }
2210 error = TRUE;
2211 }
2212 else
2213 {
2214 if (tofree != NULL)
2215 name = tofree;
2216 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 else
2219 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 /* handle d.key, l[idx], f(expr) */
2221 arg_subsc = arg;
2222 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002223 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002225 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002227 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002229 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 case 'g': list_glob_vars(first); break;
2231 case 'b': list_buf_vars(first); break;
2232 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002234 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002235#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002236 case 'v': list_vim_vars(first); break;
2237 case 's': list_script_vars(first); break;
2238 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 default:
2240 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 }
2243 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 {
2245 char_u numbuf[NUMBUFLEN];
2246 char_u *tf;
2247 int c;
2248 char_u *s;
2249
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002250 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 c = *arg;
2252 *arg = NUL;
2253 list_one_var_a((char_u *)"",
2254 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 tv.v_type,
2256 s == NULL ? (char_u *)"" : s,
2257 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 *arg = c;
2259 vim_free(tf);
2260 }
2261 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 }
2264 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265
2266 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268
2269 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 }
2271
2272 return arg;
2273}
2274
2275/*
2276 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2277 * Returns a pointer to the char just after the var name.
2278 * Returns NULL if there is an error.
2279 */
2280 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002281ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002283 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002285 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002286 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287{
2288 int c1;
2289 char_u *name;
2290 char_u *p;
2291 char_u *arg_end = NULL;
2292 int len;
2293 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002294 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295
2296 /*
2297 * ":let $VAR = expr": Set environment variable.
2298 */
2299 if (*arg == '$')
2300 {
2301 /* Find the end of the name. */
2302 ++arg;
2303 name = arg;
2304 len = get_env_len(&arg);
2305 if (len == 0)
2306 EMSG2(_(e_invarg2), name - 1);
2307 else
2308 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 if (op != NULL && (*op == '+' || *op == '-'))
2310 EMSG2(_(e_letwrong), op);
2311 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002312 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 EMSG(_(e_letunexp));
2314 else
2315 {
2316 c1 = name[len];
2317 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002318 p = get_tv_string_chk(tv);
2319 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 {
2321 int mustfree = FALSE;
2322 char_u *s = vim_getenv(name, &mustfree);
2323
2324 if (s != NULL)
2325 {
2326 p = tofree = concat_str(s, p);
2327 if (mustfree)
2328 vim_free(s);
2329 }
2330 }
2331 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002332 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334 if (STRICMP(name, "HOME") == 0)
2335 init_homedir();
2336 else if (didset_vim && STRICMP(name, "VIM") == 0)
2337 didset_vim = FALSE;
2338 else if (didset_vimruntime
2339 && STRICMP(name, "VIMRUNTIME") == 0)
2340 didset_vimruntime = FALSE;
2341 arg_end = arg;
2342 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 }
2346 }
2347 }
2348
2349 /*
2350 * ":let &option = expr": Set option value.
2351 * ":let &l:option = expr": Set local option value.
2352 * ":let &g:option = expr": Set global option value.
2353 */
2354 else if (*arg == '&')
2355 {
2356 /* Find the end of the name. */
2357 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002358 if (p == NULL || (endchars != NULL
2359 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 EMSG(_(e_letunexp));
2361 else
2362 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 long n;
2364 int opt_type;
2365 long numval;
2366 char_u *stringval = NULL;
2367 char_u *s;
2368
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 c1 = *p;
2370 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371
2372 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002373 s = get_tv_string_chk(tv); /* != NULL if number or string */
2374 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 {
2376 opt_type = get_option_value(arg, &numval,
2377 &stringval, opt_flags);
2378 if ((opt_type == 1 && *op == '.')
2379 || (opt_type == 0 && *op != '.'))
2380 EMSG2(_(e_letwrong), op);
2381 else
2382 {
2383 if (opt_type == 1) /* number */
2384 {
2385 if (*op == '+')
2386 n = numval + n;
2387 else
2388 n = numval - n;
2389 }
2390 else if (opt_type == 0 && stringval != NULL) /* string */
2391 {
2392 s = concat_str(stringval, s);
2393 vim_free(stringval);
2394 stringval = s;
2395 }
2396 }
2397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002398 if (s != NULL)
2399 {
2400 set_option_value(arg, n, s, opt_flags);
2401 arg_end = p;
2402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 }
2406 }
2407
2408 /*
2409 * ":let @r = expr": Set register contents.
2410 */
2411 else if (*arg == '@')
2412 {
2413 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 if (op != NULL && (*op == '+' || *op == '-'))
2415 EMSG2(_(e_letwrong), op);
2416 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002417 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 EMSG(_(e_letunexp));
2419 else
2420 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002421 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 char_u *s;
2423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 p = get_tv_string_chk(tv);
2425 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002427 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 if (s != NULL)
2429 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002430 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 vim_free(s);
2432 }
2433 }
2434 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002437 arg_end = arg + 1;
2438 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002439 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 }
2441 }
2442
2443 /*
2444 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002447 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002449 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002451 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2455 EMSG(_(e_letunexp));
2456 else
2457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 arg_end = p;
2460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 }
2464
2465 else
2466 EMSG2(_(e_invarg2), arg);
2467
2468 return arg_end;
2469}
2470
2471/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2473 */
2474 static int
2475check_changedtick(arg)
2476 char_u *arg;
2477{
2478 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2479 {
2480 EMSG2(_(e_readonlyvar), arg);
2481 return TRUE;
2482 }
2483 return FALSE;
2484}
2485
2486/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 * Get an lval: variable, Dict item or List item that can be assigned a value
2488 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2489 * "name.key", "name.key[expr]" etc.
2490 * Indexing only works if "name" is an existing List or Dictionary.
2491 * "name" points to the start of the name.
2492 * If "rettv" is not NULL it points to the value to be assigned.
2493 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2494 * wrong; must end in space or cmd separator.
2495 *
2496 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002497 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002499 */
2500 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002501get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 typval_T *rettv;
2504 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 int unlet;
2506 int skip;
2507 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002508 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 char_u *expr_start, *expr_end;
2512 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 dictitem_T *v;
2514 typval_T var1;
2515 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524
2525 if (skip)
2526 {
2527 /* When skipping just find the end of the name. */
2528 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002529 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 }
2531
2532 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002533 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 if (expr_start != NULL)
2535 {
2536 /* Don't expand the name when we already know there is an error. */
2537 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2538 && *p != '[' && *p != '.')
2539 {
2540 EMSG(_(e_trailing));
2541 return NULL;
2542 }
2543
2544 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2545 if (lp->ll_exp_name == NULL)
2546 {
2547 /* Report an invalid expression in braces, unless the
2548 * expression evaluation has been cancelled due to an
2549 * aborting error, an interrupt, or an exception. */
2550 if (!aborting() && !quiet)
2551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002552 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 EMSG2(_(e_invarg2), name);
2554 return NULL;
2555 }
2556 }
2557 lp->ll_name = lp->ll_exp_name;
2558 }
2559 else
2560 lp->ll_name = name;
2561
2562 /* Without [idx] or .key we are done. */
2563 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2564 return p;
2565
2566 cc = *p;
2567 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002568 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 if (v == NULL && !quiet)
2570 EMSG2(_(e_undefvar), lp->ll_name);
2571 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 if (v == NULL)
2573 return NULL;
2574
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 /*
2576 * Loop until no more [idx] or .key is following.
2577 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2582 && !(lp->ll_tv->v_type == VAR_DICT
2583 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (!quiet)
2586 EMSG(_("E689: Can only index a List or Dictionary"));
2587 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (!quiet)
2592 EMSG(_("E708: [:] must come last"));
2593 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002595
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 len = -1;
2597 if (*p == '.')
2598 {
2599 key = p + 1;
2600 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2601 ;
2602 if (len == 0)
2603 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (!quiet)
2605 EMSG(_(e_emptykey));
2606 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002607 }
2608 p = key + len;
2609 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610 else
2611 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002613 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 if (*p == ':')
2615 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616 else
2617 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 empty1 = FALSE;
2619 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002621 if (get_tv_string_chk(&var1) == NULL)
2622 {
2623 /* not a number or string */
2624 clear_tv(&var1);
2625 return NULL;
2626 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 }
2628
2629 /* Optionally get the second index [ :expr]. */
2630 if (*p == ':')
2631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002635 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 if (!empty1)
2637 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002639 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (rettv != NULL && (rettv->v_type != VAR_LIST
2641 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
2644 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 if (!empty1)
2646 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 }
2649 p = skipwhite(p + 1);
2650 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 else
2653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2656 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002661 if (get_tv_string_chk(&var2) == NULL)
2662 {
2663 /* not a number or string */
2664 if (!empty1)
2665 clear_tv(&var1);
2666 clear_tv(&var2);
2667 return NULL;
2668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
2678 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (!empty1)
2680 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
2685
2686 /* Skip to past ']'. */
2687 ++p;
2688 }
2689
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
2692 if (len == -1)
2693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (*key == NUL)
2697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002704 lp->ll_list = NULL;
2705 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002706 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002709 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (len == -1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (len == -1)
2723 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 p = NULL;
2726 break;
2727 }
2728 if (len == -1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 }
2732 else
2733 {
2734 /*
2735 * Get the number and item for the only or first index of the List.
2736 */
2737 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 else
2740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002741 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 clear_tv(&var1);
2743 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002744 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_list = lp->ll_tv->vval.v_list;
2746 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2747 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002749 if (lp->ll_n1 < 0)
2750 {
2751 lp->ll_n1 = 0;
2752 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2753 }
2754 }
2755 if (lp->ll_li == NULL)
2756 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
2761
2762 /*
2763 * May need to find the item or absolute index for the second
2764 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 * When no index given: "lp->ll_empty2" is TRUE.
2766 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002770 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 }
2779
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2781 if (lp->ll_n1 < 0)
2782 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2783 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002785 }
2786
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002789 }
2790
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return p;
2792}
2793
2794/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002795 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 */
2797 static void
2798clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002799 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800{
2801 vim_free(lp->ll_exp_name);
2802 vim_free(lp->ll_newkey);
2803}
2804
2805/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002806 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002808 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 */
2810 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002811set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002812 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002814 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002816 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817{
2818 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 listitem_T *ri;
2820 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821
2822 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002823 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 cc = *endp;
2827 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002828 if (op != NULL && *op != '=')
2829 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002830 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002831
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002832 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002833 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002834 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002835 {
2836 if (tv_op(&tv, rettv, op) == OK)
2837 set_var(lp->ll_name, &tv, FALSE);
2838 clear_tv(&tv);
2839 }
2840 }
2841 else
2842 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002844 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002846 else if (tv_check_lock(lp->ll_newkey == NULL
2847 ? lp->ll_tv->v_lock
2848 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2849 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 else if (lp->ll_range)
2851 {
2852 /*
2853 * Assign the List values to the list items.
2854 */
2855 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002856 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 if (op != NULL && *op != '=')
2858 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2859 else
2860 {
2861 clear_tv(&lp->ll_li->li_tv);
2862 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2863 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 ri = ri->li_next;
2865 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2866 break;
2867 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002868 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002870 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 ri = NULL;
2873 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002874 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002875 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_li = lp->ll_li->li_next;
2877 ++lp->ll_n1;
2878 }
2879 if (ri != NULL)
2880 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 else if (lp->ll_empty2
2882 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 : lp->ll_n1 != lp->ll_n2)
2884 EMSG(_("E711: List value has not enough items"));
2885 }
2886 else
2887 {
2888 /*
2889 * Assign to a List or Dictionary item.
2890 */
2891 if (lp->ll_newkey != NULL)
2892 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 if (op != NULL && *op != '=')
2894 {
2895 EMSG2(_(e_letwrong), op);
2896 return;
2897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002900 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 if (di == NULL)
2902 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002903 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2904 {
2905 vim_free(di);
2906 return;
2907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002909 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 else if (op != NULL && *op != '=')
2911 {
2912 tv_op(lp->ll_tv, rettv, op);
2913 return;
2914 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 /*
2919 * Assign the value to the variable or list item.
2920 */
2921 if (copy)
2922 copy_tv(rettv, lp->ll_tv);
2923 else
2924 {
2925 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002926 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002928 }
2929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930}
2931
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002932/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002933 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2934 * Returns OK or FAIL.
2935 */
2936 static int
2937tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002938 typval_T *tv1;
2939 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 char_u *op;
2941{
2942 long n;
2943 char_u numbuf[NUMBUFLEN];
2944 char_u *s;
2945
2946 /* Can't do anything with a Funcref or a Dict on the right. */
2947 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2948 {
2949 switch (tv1->v_type)
2950 {
2951 case VAR_DICT:
2952 case VAR_FUNC:
2953 break;
2954
2955 case VAR_LIST:
2956 if (*op != '+' || tv2->v_type != VAR_LIST)
2957 break;
2958 /* List += List */
2959 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2960 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2961 return OK;
2962
2963 case VAR_NUMBER:
2964 case VAR_STRING:
2965 if (tv2->v_type == VAR_LIST)
2966 break;
2967 if (*op == '+' || *op == '-')
2968 {
2969 /* nr += nr or nr -= nr*/
2970 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002971#ifdef FEAT_FLOAT
2972 if (tv2->v_type == VAR_FLOAT)
2973 {
2974 float_T f = n;
2975
2976 if (*op == '+')
2977 f += tv2->vval.v_float;
2978 else
2979 f -= tv2->vval.v_float;
2980 clear_tv(tv1);
2981 tv1->v_type = VAR_FLOAT;
2982 tv1->vval.v_float = f;
2983 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985#endif
2986 {
2987 if (*op == '+')
2988 n += get_tv_number(tv2);
2989 else
2990 n -= get_tv_number(tv2);
2991 clear_tv(tv1);
2992 tv1->v_type = VAR_NUMBER;
2993 tv1->vval.v_number = n;
2994 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002995 }
2996 else
2997 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002998 if (tv2->v_type == VAR_FLOAT)
2999 break;
3000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 /* str .= str */
3002 s = get_tv_string(tv1);
3003 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3004 clear_tv(tv1);
3005 tv1->v_type = VAR_STRING;
3006 tv1->vval.v_string = s;
3007 }
3008 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003009
3010#ifdef FEAT_FLOAT
3011 case VAR_FLOAT:
3012 {
3013 float_T f;
3014
3015 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3016 && tv2->v_type != VAR_NUMBER
3017 && tv2->v_type != VAR_STRING))
3018 break;
3019 if (tv2->v_type == VAR_FLOAT)
3020 f = tv2->vval.v_float;
3021 else
3022 f = get_tv_number(tv2);
3023 if (*op == '+')
3024 tv1->vval.v_float += f;
3025 else
3026 tv1->vval.v_float -= f;
3027 }
3028 return OK;
3029#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003030 }
3031 }
3032
3033 EMSG2(_(e_letwrong), op);
3034 return FAIL;
3035}
3036
3037/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003038 * Add a watcher to a list.
3039 */
3040 static void
3041list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003042 list_T *l;
3043 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044{
3045 lw->lw_next = l->lv_watch;
3046 l->lv_watch = lw;
3047}
3048
3049/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003050 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003051 * No warning when it isn't found...
3052 */
3053 static void
3054list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003055 list_T *l;
3056 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003057{
Bram Moolenaar33570922005-01-25 22:26:29 +00003058 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059
3060 lwp = &l->lv_watch;
3061 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3062 {
3063 if (lw == lwrem)
3064 {
3065 *lwp = lw->lw_next;
3066 break;
3067 }
3068 lwp = &lw->lw_next;
3069 }
3070}
3071
3072/*
3073 * Just before removing an item from a list: advance watchers to the next
3074 * item.
3075 */
3076 static void
3077list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003078 list_T *l;
3079 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003080{
Bram Moolenaar33570922005-01-25 22:26:29 +00003081 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003082
3083 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3084 if (lw->lw_item == item)
3085 lw->lw_item = item->li_next;
3086}
3087
3088/*
3089 * Evaluate the expression used in a ":for var in expr" command.
3090 * "arg" points to "var".
3091 * Set "*errp" to TRUE for an error, FALSE otherwise;
3092 * Return a pointer that holds the info. Null when there is an error.
3093 */
3094 void *
3095eval_for_line(arg, errp, nextcmdp, skip)
3096 char_u *arg;
3097 int *errp;
3098 char_u **nextcmdp;
3099 int skip;
3100{
Bram Moolenaar33570922005-01-25 22:26:29 +00003101 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003102 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003103 typval_T tv;
3104 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003105
3106 *errp = TRUE; /* default: there is an error */
3107
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109 if (fi == NULL)
3110 return NULL;
3111
3112 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3113 if (expr == NULL)
3114 return fi;
3115
3116 expr = skipwhite(expr);
3117 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3118 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003119 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120 return fi;
3121 }
3122
3123 if (skip)
3124 ++emsg_skip;
3125 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3126 {
3127 *errp = FALSE;
3128 if (!skip)
3129 {
3130 l = tv.vval.v_list;
3131 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003132 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003134 clear_tv(&tv);
3135 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136 else
3137 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003138 /* No need to increment the refcount, it's already set for the
3139 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140 fi->fi_list = l;
3141 list_add_watch(l, &fi->fi_lw);
3142 fi->fi_lw.lw_item = l->lv_first;
3143 }
3144 }
3145 }
3146 if (skip)
3147 --emsg_skip;
3148
3149 return fi;
3150}
3151
3152/*
3153 * Use the first item in a ":for" list. Advance to the next.
3154 * Assign the values to the variable (list). "arg" points to the first one.
3155 * Return TRUE when a valid item was found, FALSE when at end of list or
3156 * something wrong.
3157 */
3158 int
3159next_for_item(fi_void, arg)
3160 void *fi_void;
3161 char_u *arg;
3162{
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003165 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166
3167 item = fi->fi_lw.lw_item;
3168 if (item == NULL)
3169 result = FALSE;
3170 else
3171 {
3172 fi->fi_lw.lw_item = item->li_next;
3173 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3174 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3175 }
3176 return result;
3177}
3178
3179/*
3180 * Free the structure used to store info used by ":for".
3181 */
3182 void
3183free_for_info(fi_void)
3184 void *fi_void;
3185{
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003188 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003189 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003191 list_unref(fi->fi_list);
3192 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 vim_free(fi);
3194}
3195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3197
3198 void
3199set_context_for_expression(xp, arg, cmdidx)
3200 expand_T *xp;
3201 char_u *arg;
3202 cmdidx_T cmdidx;
3203{
3204 int got_eq = FALSE;
3205 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 if (cmdidx == CMD_let)
3209 {
3210 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003211 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212 {
3213 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003214 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 {
3216 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 if (vim_iswhite(*p))
3219 break;
3220 }
3221 return;
3222 }
3223 }
3224 else
3225 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3226 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 while ((xp->xp_pattern = vim_strpbrk(arg,
3228 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3229 {
3230 c = *xp->xp_pattern;
3231 if (c == '&')
3232 {
3233 c = xp->xp_pattern[1];
3234 if (c == '&')
3235 {
3236 ++xp->xp_pattern;
3237 xp->xp_context = cmdidx != CMD_let || got_eq
3238 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3239 }
3240 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003243 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3244 xp->xp_pattern += 2;
3245
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 }
3248 else if (c == '$')
3249 {
3250 /* environment variable */
3251 xp->xp_context = EXPAND_ENV_VARS;
3252 }
3253 else if (c == '=')
3254 {
3255 got_eq = TRUE;
3256 xp->xp_context = EXPAND_EXPRESSION;
3257 }
3258 else if (c == '<'
3259 && xp->xp_context == EXPAND_FUNCTIONS
3260 && vim_strchr(xp->xp_pattern, '(') == NULL)
3261 {
3262 /* Function name can start with "<SNR>" */
3263 break;
3264 }
3265 else if (cmdidx != CMD_let || got_eq)
3266 {
3267 if (c == '"') /* string */
3268 {
3269 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3270 if (c == '\\' && xp->xp_pattern[1] != NUL)
3271 ++xp->xp_pattern;
3272 xp->xp_context = EXPAND_NOTHING;
3273 }
3274 else if (c == '\'') /* literal string */
3275 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003276 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3278 /* skip */ ;
3279 xp->xp_context = EXPAND_NOTHING;
3280 }
3281 else if (c == '|')
3282 {
3283 if (xp->xp_pattern[1] == '|')
3284 {
3285 ++xp->xp_pattern;
3286 xp->xp_context = EXPAND_EXPRESSION;
3287 }
3288 else
3289 xp->xp_context = EXPAND_COMMANDS;
3290 }
3291 else
3292 xp->xp_context = EXPAND_EXPRESSION;
3293 }
3294 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 /* Doesn't look like something valid, expand as an expression
3296 * anyway. */
3297 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 arg = xp->xp_pattern;
3299 if (*arg != NUL)
3300 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3301 /* skip */ ;
3302 }
3303 xp->xp_pattern = arg;
3304}
3305
3306#endif /* FEAT_CMDL_COMPL */
3307
3308/*
3309 * ":1,25call func(arg1, arg2)" function call.
3310 */
3311 void
3312ex_call(eap)
3313 exarg_T *eap;
3314{
3315 char_u *arg = eap->arg;
3316 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003318 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 linenr_T lnum;
3322 int doesrange;
3323 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003326 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003327 if (fudi.fd_newkey != NULL)
3328 {
3329 /* Still need to give an error message for missing key. */
3330 EMSG2(_(e_dictkey), fudi.fd_newkey);
3331 vim_free(fudi.fd_newkey);
3332 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003333 if (tofree == NULL)
3334 return;
3335
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003336 /* Increase refcount on dictionary, it could get deleted when evaluating
3337 * the arguments. */
3338 if (fudi.fd_dict != NULL)
3339 ++fudi.fd_dict->dv_refcount;
3340
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003341 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003342 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003343 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaar532c7802005-01-27 14:44:31 +00003345 /* Skip white space to allow ":call func ()". Not good, but required for
3346 * backward compatibility. */
3347 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003348 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 if (*startarg != '(')
3351 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003352 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 goto end;
3354 }
3355
3356 /*
3357 * When skipping, evaluate the function once, to find the end of the
3358 * arguments.
3359 * When the function takes a range, this is discovered after the first
3360 * call, and the loop is broken.
3361 */
3362 if (eap->skip)
3363 {
3364 ++emsg_skip;
3365 lnum = eap->line2; /* do it once, also with an invalid range */
3366 }
3367 else
3368 lnum = eap->line1;
3369 for ( ; lnum <= eap->line2; ++lnum)
3370 {
3371 if (!eap->skip && eap->addr_count > 0)
3372 {
3373 curwin->w_cursor.lnum = lnum;
3374 curwin->w_cursor.col = 0;
3375 }
3376 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003377 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003378 eap->line1, eap->line2, &doesrange,
3379 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
3381 failed = TRUE;
3382 break;
3383 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003384
3385 /* Handle a function returning a Funcref, Dictionary or List. */
3386 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3387 {
3388 failed = TRUE;
3389 break;
3390 }
3391
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003392 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 if (doesrange || eap->skip)
3394 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003395
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003397 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003398 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003399 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (aborting())
3401 break;
3402 }
3403 if (eap->skip)
3404 --emsg_skip;
3405
3406 if (!failed)
3407 {
3408 /* Check for trailing illegal characters and a following command. */
3409 if (!ends_excmd(*arg))
3410 {
3411 emsg_severe = TRUE;
3412 EMSG(_(e_trailing));
3413 }
3414 else
3415 eap->nextcmd = check_nextcmd(arg);
3416 }
3417
3418end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003419 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003420 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421}
3422
3423/*
3424 * ":unlet[!] var1 ... " command.
3425 */
3426 void
3427ex_unlet(eap)
3428 exarg_T *eap;
3429{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003430 ex_unletlock(eap, eap->arg, 0);
3431}
3432
3433/*
3434 * ":lockvar" and ":unlockvar" commands
3435 */
3436 void
3437ex_lockvar(eap)
3438 exarg_T *eap;
3439{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003441 int deep = 2;
3442
3443 if (eap->forceit)
3444 deep = -1;
3445 else if (vim_isdigit(*arg))
3446 {
3447 deep = getdigits(&arg);
3448 arg = skipwhite(arg);
3449 }
3450
3451 ex_unletlock(eap, arg, deep);
3452}
3453
3454/*
3455 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3456 */
3457 static void
3458ex_unletlock(eap, argstart, deep)
3459 exarg_T *eap;
3460 char_u *argstart;
3461 int deep;
3462{
3463 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003466 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
3468 do
3469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003470 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003471 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3472 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003473 if (lv.ll_name == NULL)
3474 error = TRUE; /* error but continue parsing */
3475 if (name_end == NULL || (!vim_iswhite(*name_end)
3476 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003478 if (name_end != NULL)
3479 {
3480 emsg_severe = TRUE;
3481 EMSG(_(e_trailing));
3482 }
3483 if (!(eap->skip || error))
3484 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 break;
3486 }
3487
3488 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003489 {
3490 if (eap->cmdidx == CMD_unlet)
3491 {
3492 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3493 error = TRUE;
3494 }
3495 else
3496 {
3497 if (do_lock_var(&lv, name_end, deep,
3498 eap->cmdidx == CMD_lockvar) == FAIL)
3499 error = TRUE;
3500 }
3501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003503 if (!eap->skip)
3504 clear_lval(&lv);
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 arg = skipwhite(name_end);
3507 } while (!ends_excmd(*arg));
3508
3509 eap->nextcmd = check_nextcmd(arg);
3510}
3511
Bram Moolenaar8c711452005-01-14 21:53:12 +00003512 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003513do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003514 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003515 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003516 int forceit;
3517{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003518 int ret = OK;
3519 int cc;
3520
3521 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003522 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003523 cc = *name_end;
3524 *name_end = NUL;
3525
3526 /* Normal name or expanded name. */
3527 if (check_changedtick(lp->ll_name))
3528 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003529 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003530 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003531 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003532 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003533 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3534 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003535 else if (lp->ll_range)
3536 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003537 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538
3539 /* Delete a range of List items. */
3540 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3541 {
3542 li = lp->ll_li->li_next;
3543 listitem_remove(lp->ll_list, lp->ll_li);
3544 lp->ll_li = li;
3545 ++lp->ll_n1;
3546 }
3547 }
3548 else
3549 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003550 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 /* unlet a List item. */
3552 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003554 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003555 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 }
3557
3558 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559}
3560
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561/*
3562 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003563 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 */
3565 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003566do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003568 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
Bram Moolenaar33570922005-01-25 22:26:29 +00003570 hashtab_T *ht;
3571 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003572 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003573 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaar33570922005-01-25 22:26:29 +00003575 ht = find_var_ht(name, &varname);
3576 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003578 hi = hash_find(ht, varname);
3579 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003580 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003581 di = HI2DI(hi);
3582 if (var_check_fixed(di->di_flags, name)
3583 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 return FAIL;
3585 delete_var(ht, hi);
3586 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 if (forceit)
3590 return OK;
3591 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 return FAIL;
3593}
3594
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003595/*
3596 * Lock or unlock variable indicated by "lp".
3597 * "deep" is the levels to go (-1 for unlimited);
3598 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3599 */
3600 static int
3601do_lock_var(lp, name_end, deep, lock)
3602 lval_T *lp;
3603 char_u *name_end;
3604 int deep;
3605 int lock;
3606{
3607 int ret = OK;
3608 int cc;
3609 dictitem_T *di;
3610
3611 if (deep == 0) /* nothing to do */
3612 return OK;
3613
3614 if (lp->ll_tv == NULL)
3615 {
3616 cc = *name_end;
3617 *name_end = NUL;
3618
3619 /* Normal name or expanded name. */
3620 if (check_changedtick(lp->ll_name))
3621 ret = FAIL;
3622 else
3623 {
3624 di = find_var(lp->ll_name, NULL);
3625 if (di == NULL)
3626 ret = FAIL;
3627 else
3628 {
3629 if (lock)
3630 di->di_flags |= DI_FLAGS_LOCK;
3631 else
3632 di->di_flags &= ~DI_FLAGS_LOCK;
3633 item_lock(&di->di_tv, deep, lock);
3634 }
3635 }
3636 *name_end = cc;
3637 }
3638 else if (lp->ll_range)
3639 {
3640 listitem_T *li = lp->ll_li;
3641
3642 /* (un)lock a range of List items. */
3643 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3644 {
3645 item_lock(&li->li_tv, deep, lock);
3646 li = li->li_next;
3647 ++lp->ll_n1;
3648 }
3649 }
3650 else if (lp->ll_list != NULL)
3651 /* (un)lock a List item. */
3652 item_lock(&lp->ll_li->li_tv, deep, lock);
3653 else
3654 /* un(lock) a Dictionary item. */
3655 item_lock(&lp->ll_di->di_tv, deep, lock);
3656
3657 return ret;
3658}
3659
3660/*
3661 * Lock or unlock an item. "deep" is nr of levels to go.
3662 */
3663 static void
3664item_lock(tv, deep, lock)
3665 typval_T *tv;
3666 int deep;
3667 int lock;
3668{
3669 static int recurse = 0;
3670 list_T *l;
3671 listitem_T *li;
3672 dict_T *d;
3673 hashitem_T *hi;
3674 int todo;
3675
3676 if (recurse >= DICT_MAXNEST)
3677 {
3678 EMSG(_("E743: variable nested too deep for (un)lock"));
3679 return;
3680 }
3681 if (deep == 0)
3682 return;
3683 ++recurse;
3684
3685 /* lock/unlock the item itself */
3686 if (lock)
3687 tv->v_lock |= VAR_LOCKED;
3688 else
3689 tv->v_lock &= ~VAR_LOCKED;
3690
3691 switch (tv->v_type)
3692 {
3693 case VAR_LIST:
3694 if ((l = tv->vval.v_list) != NULL)
3695 {
3696 if (lock)
3697 l->lv_lock |= VAR_LOCKED;
3698 else
3699 l->lv_lock &= ~VAR_LOCKED;
3700 if (deep < 0 || deep > 1)
3701 /* recursive: lock/unlock the items the List contains */
3702 for (li = l->lv_first; li != NULL; li = li->li_next)
3703 item_lock(&li->li_tv, deep - 1, lock);
3704 }
3705 break;
3706 case VAR_DICT:
3707 if ((d = tv->vval.v_dict) != NULL)
3708 {
3709 if (lock)
3710 d->dv_lock |= VAR_LOCKED;
3711 else
3712 d->dv_lock &= ~VAR_LOCKED;
3713 if (deep < 0 || deep > 1)
3714 {
3715 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003716 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003717 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3718 {
3719 if (!HASHITEM_EMPTY(hi))
3720 {
3721 --todo;
3722 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3723 }
3724 }
3725 }
3726 }
3727 }
3728 --recurse;
3729}
3730
Bram Moolenaara40058a2005-07-11 22:42:07 +00003731/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003732 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3733 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003734 */
3735 static int
3736tv_islocked(tv)
3737 typval_T *tv;
3738{
3739 return (tv->v_lock & VAR_LOCKED)
3740 || (tv->v_type == VAR_LIST
3741 && tv->vval.v_list != NULL
3742 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3743 || (tv->v_type == VAR_DICT
3744 && tv->vval.v_dict != NULL
3745 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3746}
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3749/*
3750 * Delete all "menutrans_" variables.
3751 */
3752 void
3753del_menutrans_vars()
3754{
Bram Moolenaar33570922005-01-25 22:26:29 +00003755 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003756 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Bram Moolenaar33570922005-01-25 22:26:29 +00003758 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003759 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 {
3762 if (!HASHITEM_EMPTY(hi))
3763 {
3764 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3766 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003767 }
3768 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003769 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770}
3771#endif
3772
3773#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3774
3775/*
3776 * Local string buffer for the next two functions to store a variable name
3777 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3778 * get_user_var_name().
3779 */
3780
3781static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3782
3783static char_u *varnamebuf = NULL;
3784static int varnamebuflen = 0;
3785
3786/*
3787 * Function to concatenate a prefix and a variable name.
3788 */
3789 static char_u *
3790cat_prefix_varname(prefix, name)
3791 int prefix;
3792 char_u *name;
3793{
3794 int len;
3795
3796 len = (int)STRLEN(name) + 3;
3797 if (len > varnamebuflen)
3798 {
3799 vim_free(varnamebuf);
3800 len += 10; /* some additional space */
3801 varnamebuf = alloc(len);
3802 if (varnamebuf == NULL)
3803 {
3804 varnamebuflen = 0;
3805 return NULL;
3806 }
3807 varnamebuflen = len;
3808 }
3809 *varnamebuf = prefix;
3810 varnamebuf[1] = ':';
3811 STRCPY(varnamebuf + 2, name);
3812 return varnamebuf;
3813}
3814
3815/*
3816 * Function given to ExpandGeneric() to obtain the list of user defined
3817 * (global/buffer/window/built-in) variable names.
3818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 char_u *
3820get_user_var_name(xp, idx)
3821 expand_T *xp;
3822 int idx;
3823{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003824 static long_u gdone;
3825 static long_u bdone;
3826 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003827#ifdef FEAT_WINDOWS
3828 static long_u tdone;
3829#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003830 static int vidx;
3831 static hashitem_T *hi;
3832 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003835 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003836 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003837#ifdef FEAT_WINDOWS
3838 tdone = 0;
3839#endif
3840 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003841
3842 /* Global variables */
3843 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003845 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003846 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003847 else
3848 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003849 while (HASHITEM_EMPTY(hi))
3850 ++hi;
3851 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3852 return cat_prefix_varname('g', hi->hi_key);
3853 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003855
3856 /* b: variables */
3857 ht = &curbuf->b_vars.dv_hashtab;
3858 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003861 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003862 else
3863 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003864 while (HASHITEM_EMPTY(hi))
3865 ++hi;
3866 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003868 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003870 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 return (char_u *)"b:changedtick";
3872 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003873
3874 /* w: variables */
3875 ht = &curwin->w_vars.dv_hashtab;
3876 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003878 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003879 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003880 else
3881 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003882 while (HASHITEM_EMPTY(hi))
3883 ++hi;
3884 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003886
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003887#ifdef FEAT_WINDOWS
3888 /* t: variables */
3889 ht = &curtab->tp_vars.dv_hashtab;
3890 if (tdone < ht->ht_used)
3891 {
3892 if (tdone++ == 0)
3893 hi = ht->ht_array;
3894 else
3895 ++hi;
3896 while (HASHITEM_EMPTY(hi))
3897 ++hi;
3898 return cat_prefix_varname('t', hi->hi_key);
3899 }
3900#endif
3901
Bram Moolenaar33570922005-01-25 22:26:29 +00003902 /* v: variables */
3903 if (vidx < VV_LEN)
3904 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
3906 vim_free(varnamebuf);
3907 varnamebuf = NULL;
3908 varnamebuflen = 0;
3909 return NULL;
3910}
3911
3912#endif /* FEAT_CMDL_COMPL */
3913
3914/*
3915 * types for expressions.
3916 */
3917typedef enum
3918{
3919 TYPE_UNKNOWN = 0
3920 , TYPE_EQUAL /* == */
3921 , TYPE_NEQUAL /* != */
3922 , TYPE_GREATER /* > */
3923 , TYPE_GEQUAL /* >= */
3924 , TYPE_SMALLER /* < */
3925 , TYPE_SEQUAL /* <= */
3926 , TYPE_MATCH /* =~ */
3927 , TYPE_NOMATCH /* !~ */
3928} exptype_T;
3929
3930/*
3931 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003932 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3934 */
3935
3936/*
3937 * Handle zero level expression.
3938 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003939 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003940 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 * Return OK or FAIL.
3942 */
3943 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 char_u **nextcmd;
3948 int evaluate;
3949{
3950 int ret;
3951 char_u *p;
3952
3953 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 if (ret == FAIL || !ends_excmd(*p))
3956 {
3957 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003958 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 /*
3960 * Report the invalid expression unless the expression evaluation has
3961 * been cancelled due to an aborting error, an interrupt, or an
3962 * exception.
3963 */
3964 if (!aborting())
3965 EMSG2(_(e_invexpr2), arg);
3966 ret = FAIL;
3967 }
3968 if (nextcmd != NULL)
3969 *nextcmd = check_nextcmd(p);
3970
3971 return ret;
3972}
3973
3974/*
3975 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003976 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 *
3978 * "arg" must point to the first non-white of the expression.
3979 * "arg" is advanced to the next non-white after the recognized expression.
3980 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003981 * Note: "rettv.v_lock" is not set.
3982 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 * Return OK or FAIL.
3984 */
3985 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003986eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 int evaluate;
3990{
3991 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 /*
3995 * Get the first variable.
3996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 return FAIL;
3999
4000 if ((*arg)[0] == '?')
4001 {
4002 result = FALSE;
4003 if (evaluate)
4004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004005 int error = FALSE;
4006
4007 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004009 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004010 if (error)
4011 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013
4014 /*
4015 * Get the second variable.
4016 */
4017 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 return FAIL;
4020
4021 /*
4022 * Check for the ":".
4023 */
4024 if ((*arg)[0] != ':')
4025 {
4026 EMSG(_("E109: Missing ':' after '?'"));
4027 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return FAIL;
4030 }
4031
4032 /*
4033 * Get the third variable.
4034 */
4035 *arg = skipwhite(*arg + 1);
4036 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4037 {
4038 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 return FAIL;
4041 }
4042 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
4045
4046 return OK;
4047}
4048
4049/*
4050 * Handle first level expression:
4051 * expr2 || expr2 || expr2 logical OR
4052 *
4053 * "arg" must point to the first non-white of the expression.
4054 * "arg" is advanced to the next non-white after the recognized expression.
4055 *
4056 * Return OK or FAIL.
4057 */
4058 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 int evaluate;
4063{
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 long result;
4066 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004067 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 /*
4070 * Get the first variable.
4071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return FAIL;
4074
4075 /*
4076 * Repeat until there is no following "||".
4077 */
4078 first = TRUE;
4079 result = FALSE;
4080 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4081 {
4082 if (evaluate && first)
4083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004084 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004087 if (error)
4088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 first = FALSE;
4090 }
4091
4092 /*
4093 * Get the second variable.
4094 */
4095 *arg = skipwhite(*arg + 2);
4096 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4097 return FAIL;
4098
4099 /*
4100 * Compute the result.
4101 */
4102 if (evaluate && !result)
4103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004107 if (error)
4108 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 if (evaluate)
4111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 rettv->v_type = VAR_NUMBER;
4113 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 }
4116
4117 return OK;
4118}
4119
4120/*
4121 * Handle second level expression:
4122 * expr3 && expr3 && expr3 logical AND
4123 *
4124 * "arg" must point to the first non-white of the expression.
4125 * "arg" is advanced to the next non-white after the recognized expression.
4126 *
4127 * Return OK or FAIL.
4128 */
4129 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 int evaluate;
4134{
Bram Moolenaar33570922005-01-25 22:26:29 +00004135 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 long result;
4137 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004138 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140 /*
4141 * Get the first variable.
4142 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return FAIL;
4145
4146 /*
4147 * Repeat until there is no following "&&".
4148 */
4149 first = TRUE;
4150 result = TRUE;
4151 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4152 {
4153 if (evaluate && first)
4154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004155 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004158 if (error)
4159 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 first = FALSE;
4161 }
4162
4163 /*
4164 * Get the second variable.
4165 */
4166 *arg = skipwhite(*arg + 2);
4167 if (eval4(arg, &var2, evaluate && result) == FAIL)
4168 return FAIL;
4169
4170 /*
4171 * Compute the result.
4172 */
4173 if (evaluate && result)
4174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 if (error)
4179 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 if (evaluate)
4182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 rettv->v_type = VAR_NUMBER;
4184 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186 }
4187
4188 return OK;
4189}
4190
4191/*
4192 * Handle third level expression:
4193 * var1 == var2
4194 * var1 =~ var2
4195 * var1 != var2
4196 * var1 !~ var2
4197 * var1 > var2
4198 * var1 >= var2
4199 * var1 < var2
4200 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004201 * var1 is var2
4202 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 *
4204 * "arg" must point to the first non-white of the expression.
4205 * "arg" is advanced to the next non-white after the recognized expression.
4206 *
4207 * Return OK or FAIL.
4208 */
4209 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 int evaluate;
4214{
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 char_u *p;
4217 int i;
4218 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004219 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 int len = 2;
4221 long n1, n2;
4222 char_u *s1, *s2;
4223 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4224 regmatch_T regmatch;
4225 int ic;
4226 char_u *save_cpo;
4227
4228 /*
4229 * Get the first variable.
4230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return FAIL;
4233
4234 p = *arg;
4235 switch (p[0])
4236 {
4237 case '=': if (p[1] == '=')
4238 type = TYPE_EQUAL;
4239 else if (p[1] == '~')
4240 type = TYPE_MATCH;
4241 break;
4242 case '!': if (p[1] == '=')
4243 type = TYPE_NEQUAL;
4244 else if (p[1] == '~')
4245 type = TYPE_NOMATCH;
4246 break;
4247 case '>': if (p[1] != '=')
4248 {
4249 type = TYPE_GREATER;
4250 len = 1;
4251 }
4252 else
4253 type = TYPE_GEQUAL;
4254 break;
4255 case '<': if (p[1] != '=')
4256 {
4257 type = TYPE_SMALLER;
4258 len = 1;
4259 }
4260 else
4261 type = TYPE_SEQUAL;
4262 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004263 case 'i': if (p[1] == 's')
4264 {
4265 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4266 len = 5;
4267 if (!vim_isIDc(p[len]))
4268 {
4269 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4270 type_is = TRUE;
4271 }
4272 }
4273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275
4276 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004277 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 */
4279 if (type != TYPE_UNKNOWN)
4280 {
4281 /* extra question mark appended: ignore case */
4282 if (p[len] == '?')
4283 {
4284 ic = TRUE;
4285 ++len;
4286 }
4287 /* extra '#' appended: match case */
4288 else if (p[len] == '#')
4289 {
4290 ic = FALSE;
4291 ++len;
4292 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004293 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 else
4295 ic = p_ic;
4296
4297 /*
4298 * Get the second variable.
4299 */
4300 *arg = skipwhite(p + len);
4301 if (eval5(arg, &var2, evaluate) == FAIL)
4302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 return FAIL;
4305 }
4306
4307 if (evaluate)
4308 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004309 if (type_is && rettv->v_type != var2.v_type)
4310 {
4311 /* For "is" a different type always means FALSE, for "notis"
4312 * it means TRUE. */
4313 n1 = (type == TYPE_NEQUAL);
4314 }
4315 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4316 {
4317 if (type_is)
4318 {
4319 n1 = (rettv->v_type == var2.v_type
4320 && rettv->vval.v_list == var2.vval.v_list);
4321 if (type == TYPE_NEQUAL)
4322 n1 = !n1;
4323 }
4324 else if (rettv->v_type != var2.v_type
4325 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4326 {
4327 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004328 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004329 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004330 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004331 clear_tv(rettv);
4332 clear_tv(&var2);
4333 return FAIL;
4334 }
4335 else
4336 {
4337 /* Compare two Lists for being equal or unequal. */
4338 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4339 if (type == TYPE_NEQUAL)
4340 n1 = !n1;
4341 }
4342 }
4343
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004344 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4345 {
4346 if (type_is)
4347 {
4348 n1 = (rettv->v_type == var2.v_type
4349 && rettv->vval.v_dict == var2.vval.v_dict);
4350 if (type == TYPE_NEQUAL)
4351 n1 = !n1;
4352 }
4353 else if (rettv->v_type != var2.v_type
4354 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4355 {
4356 if (rettv->v_type != var2.v_type)
4357 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4358 else
4359 EMSG(_("E736: Invalid operation for Dictionary"));
4360 clear_tv(rettv);
4361 clear_tv(&var2);
4362 return FAIL;
4363 }
4364 else
4365 {
4366 /* Compare two Dictionaries for being equal or unequal. */
4367 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4368 if (type == TYPE_NEQUAL)
4369 n1 = !n1;
4370 }
4371 }
4372
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4374 {
4375 if (rettv->v_type != var2.v_type
4376 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4377 {
4378 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004379 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004380 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004381 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 clear_tv(rettv);
4383 clear_tv(&var2);
4384 return FAIL;
4385 }
4386 else
4387 {
4388 /* Compare two Funcrefs for being equal or unequal. */
4389 if (rettv->vval.v_string == NULL
4390 || var2.vval.v_string == NULL)
4391 n1 = FALSE;
4392 else
4393 n1 = STRCMP(rettv->vval.v_string,
4394 var2.vval.v_string) == 0;
4395 if (type == TYPE_NEQUAL)
4396 n1 = !n1;
4397 }
4398 }
4399
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004400#ifdef FEAT_FLOAT
4401 /*
4402 * If one of the two variables is a float, compare as a float.
4403 * When using "=~" or "!~", always compare as string.
4404 */
4405 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4406 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4407 {
4408 float_T f1, f2;
4409
4410 if (rettv->v_type == VAR_FLOAT)
4411 f1 = rettv->vval.v_float;
4412 else
4413 f1 = get_tv_number(rettv);
4414 if (var2.v_type == VAR_FLOAT)
4415 f2 = var2.vval.v_float;
4416 else
4417 f2 = get_tv_number(&var2);
4418 n1 = FALSE;
4419 switch (type)
4420 {
4421 case TYPE_EQUAL: n1 = (f1 == f2); break;
4422 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4423 case TYPE_GREATER: n1 = (f1 > f2); break;
4424 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4425 case TYPE_SMALLER: n1 = (f1 < f2); break;
4426 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4427 case TYPE_UNKNOWN:
4428 case TYPE_MATCH:
4429 case TYPE_NOMATCH: break; /* avoid gcc warning */
4430 }
4431 }
4432#endif
4433
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 /*
4435 * If one of the two variables is a number, compare as a number.
4436 * When using "=~" or "!~", always compare as string.
4437 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004438 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004441 n1 = get_tv_number(rettv);
4442 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 switch (type)
4444 {
4445 case TYPE_EQUAL: n1 = (n1 == n2); break;
4446 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4447 case TYPE_GREATER: n1 = (n1 > n2); break;
4448 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4449 case TYPE_SMALLER: n1 = (n1 < n2); break;
4450 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4451 case TYPE_UNKNOWN:
4452 case TYPE_MATCH:
4453 case TYPE_NOMATCH: break; /* avoid gcc warning */
4454 }
4455 }
4456 else
4457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 s1 = get_tv_string_buf(rettv, buf1);
4459 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4461 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4462 else
4463 i = 0;
4464 n1 = FALSE;
4465 switch (type)
4466 {
4467 case TYPE_EQUAL: n1 = (i == 0); break;
4468 case TYPE_NEQUAL: n1 = (i != 0); break;
4469 case TYPE_GREATER: n1 = (i > 0); break;
4470 case TYPE_GEQUAL: n1 = (i >= 0); break;
4471 case TYPE_SMALLER: n1 = (i < 0); break;
4472 case TYPE_SEQUAL: n1 = (i <= 0); break;
4473
4474 case TYPE_MATCH:
4475 case TYPE_NOMATCH:
4476 /* avoid 'l' flag in 'cpoptions' */
4477 save_cpo = p_cpo;
4478 p_cpo = (char_u *)"";
4479 regmatch.regprog = vim_regcomp(s2,
4480 RE_MAGIC + RE_STRING);
4481 regmatch.rm_ic = ic;
4482 if (regmatch.regprog != NULL)
4483 {
4484 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4485 vim_free(regmatch.regprog);
4486 if (type == TYPE_NOMATCH)
4487 n1 = !n1;
4488 }
4489 p_cpo = save_cpo;
4490 break;
4491
4492 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4493 }
4494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004495 clear_tv(rettv);
4496 clear_tv(&var2);
4497 rettv->v_type = VAR_NUMBER;
4498 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 }
4501
4502 return OK;
4503}
4504
4505/*
4506 * Handle fourth level expression:
4507 * + number addition
4508 * - number subtraction
4509 * . string concatenation
4510 *
4511 * "arg" must point to the first non-white of the expression.
4512 * "arg" is advanced to the next non-white after the recognized expression.
4513 *
4514 * Return OK or FAIL.
4515 */
4516 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004517eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 int evaluate;
4521{
Bram Moolenaar33570922005-01-25 22:26:29 +00004522 typval_T var2;
4523 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 int op;
4525 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004526#ifdef FEAT_FLOAT
4527 float_T f1 = 0, f2 = 0;
4528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 char_u *s1, *s2;
4530 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4531 char_u *p;
4532
4533 /*
4534 * Get the first variable.
4535 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004536 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 return FAIL;
4538
4539 /*
4540 * Repeat computing, until no '+', '-' or '.' is following.
4541 */
4542 for (;;)
4543 {
4544 op = **arg;
4545 if (op != '+' && op != '-' && op != '.')
4546 break;
4547
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004548 if ((op != '+' || rettv->v_type != VAR_LIST)
4549#ifdef FEAT_FLOAT
4550 && (op == '.' || rettv->v_type != VAR_FLOAT)
4551#endif
4552 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004553 {
4554 /* For "list + ...", an illegal use of the first operand as
4555 * a number cannot be determined before evaluating the 2nd
4556 * operand: if this is also a list, all is ok.
4557 * For "something . ...", "something - ..." or "non-list + ...",
4558 * we know that the first operand needs to be a string or number
4559 * without evaluating the 2nd operand. So check before to avoid
4560 * side effects after an error. */
4561 if (evaluate && get_tv_string_chk(rettv) == NULL)
4562 {
4563 clear_tv(rettv);
4564 return FAIL;
4565 }
4566 }
4567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 /*
4569 * Get the second variable.
4570 */
4571 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004572 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 return FAIL;
4576 }
4577
4578 if (evaluate)
4579 {
4580 /*
4581 * Compute the result.
4582 */
4583 if (op == '.')
4584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004585 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4586 s2 = get_tv_string_buf_chk(&var2, buf2);
4587 if (s2 == NULL) /* type error ? */
4588 {
4589 clear_tv(rettv);
4590 clear_tv(&var2);
4591 return FAIL;
4592 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004593 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004594 clear_tv(rettv);
4595 rettv->v_type = VAR_STRING;
4596 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004598 else if (op == '+' && rettv->v_type == VAR_LIST
4599 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004600 {
4601 /* concatenate Lists */
4602 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4603 &var3) == FAIL)
4604 {
4605 clear_tv(rettv);
4606 clear_tv(&var2);
4607 return FAIL;
4608 }
4609 clear_tv(rettv);
4610 *rettv = var3;
4611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 else
4613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004614 int error = FALSE;
4615
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004616#ifdef FEAT_FLOAT
4617 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004618 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004619 f1 = rettv->vval.v_float;
4620 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004621 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004622 else
4623#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004625 n1 = get_tv_number_chk(rettv, &error);
4626 if (error)
4627 {
4628 /* This can only happen for "list + non-list". For
4629 * "non-list + ..." or "something - ...", we returned
4630 * before evaluating the 2nd operand. */
4631 clear_tv(rettv);
4632 return FAIL;
4633 }
4634#ifdef FEAT_FLOAT
4635 if (var2.v_type == VAR_FLOAT)
4636 f1 = n1;
4637#endif
4638 }
4639#ifdef FEAT_FLOAT
4640 if (var2.v_type == VAR_FLOAT)
4641 {
4642 f2 = var2.vval.v_float;
4643 n2 = 0;
4644 }
4645 else
4646#endif
4647 {
4648 n2 = get_tv_number_chk(&var2, &error);
4649 if (error)
4650 {
4651 clear_tv(rettv);
4652 clear_tv(&var2);
4653 return FAIL;
4654 }
4655#ifdef FEAT_FLOAT
4656 if (rettv->v_type == VAR_FLOAT)
4657 f2 = n2;
4658#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004661
4662#ifdef FEAT_FLOAT
4663 /* If there is a float on either side the result is a float. */
4664 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4665 {
4666 if (op == '+')
4667 f1 = f1 + f2;
4668 else
4669 f1 = f1 - f2;
4670 rettv->v_type = VAR_FLOAT;
4671 rettv->vval.v_float = f1;
4672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004674#endif
4675 {
4676 if (op == '+')
4677 n1 = n1 + n2;
4678 else
4679 n1 = n1 - n2;
4680 rettv->v_type = VAR_NUMBER;
4681 rettv->vval.v_number = n1;
4682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686 }
4687 return OK;
4688}
4689
4690/*
4691 * Handle fifth level expression:
4692 * * number multiplication
4693 * / number division
4694 * % number modulo
4695 *
4696 * "arg" must point to the first non-white of the expression.
4697 * "arg" is advanced to the next non-white after the recognized expression.
4698 *
4699 * Return OK or FAIL.
4700 */
4701 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004702eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 int op;
4710 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711#ifdef FEAT_FLOAT
4712 int use_float = FALSE;
4713 float_T f1 = 0, f2;
4714#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
4717 /*
4718 * Get the first variable.
4719 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004720 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 return FAIL;
4722
4723 /*
4724 * Repeat computing, until no '*', '/' or '%' is following.
4725 */
4726 for (;;)
4727 {
4728 op = **arg;
4729 if (op != '*' && op != '/' && op != '%')
4730 break;
4731
4732 if (evaluate)
4733 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734#ifdef FEAT_FLOAT
4735 if (rettv->v_type == VAR_FLOAT)
4736 {
4737 f1 = rettv->vval.v_float;
4738 use_float = TRUE;
4739 n1 = 0;
4740 }
4741 else
4742#endif
4743 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004745 if (error)
4746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 }
4748 else
4749 n1 = 0;
4750
4751 /*
4752 * Get the second variable.
4753 */
4754 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004755 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 return FAIL;
4757
4758 if (evaluate)
4759 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760#ifdef FEAT_FLOAT
4761 if (var2.v_type == VAR_FLOAT)
4762 {
4763 if (!use_float)
4764 {
4765 f1 = n1;
4766 use_float = TRUE;
4767 }
4768 f2 = var2.vval.v_float;
4769 n2 = 0;
4770 }
4771 else
4772#endif
4773 {
4774 n2 = get_tv_number_chk(&var2, &error);
4775 clear_tv(&var2);
4776 if (error)
4777 return FAIL;
4778#ifdef FEAT_FLOAT
4779 if (use_float)
4780 f2 = n2;
4781#endif
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784 /*
4785 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788#ifdef FEAT_FLOAT
4789 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791 if (op == '*')
4792 f1 = f1 * f2;
4793 else if (op == '/')
4794 {
4795 /* We rely on the floating point library to handle divide
4796 * by zero to result in "inf" and not a crash. */
4797 f1 = f1 / f2;
4798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004801 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802 return FAIL;
4803 }
4804 rettv->v_type = VAR_FLOAT;
4805 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004810 if (op == '*')
4811 n1 = n1 * n2;
4812 else if (op == '/')
4813 {
4814 if (n2 == 0) /* give an error message? */
4815 {
4816 if (n1 == 0)
4817 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4818 else if (n1 < 0)
4819 n1 = -0x7fffffffL;
4820 else
4821 n1 = 0x7fffffffL;
4822 }
4823 else
4824 n1 = n1 / n2;
4825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827 {
4828 if (n2 == 0) /* give an error message? */
4829 n1 = 0;
4830 else
4831 n1 = n1 % n2;
4832 }
4833 rettv->v_type = VAR_NUMBER;
4834 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
4837 }
4838
4839 return OK;
4840}
4841
4842/*
4843 * Handle sixth level expression:
4844 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004845 * "string" string constant
4846 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 * &option-name option value
4848 * @r register contents
4849 * identifier variable value
4850 * function() function call
4851 * $VAR environment variable
4852 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004853 * [expr, expr] List
4854 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 *
4856 * Also handle:
4857 * ! in front logical NOT
4858 * - in front unary minus
4859 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004860 * trailing [] subscript in String or List
4861 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 *
4863 * "arg" must point to the first non-white of the expression.
4864 * "arg" is advanced to the next non-white after the recognized expression.
4865 *
4866 * Return OK or FAIL.
4867 */
4868 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004869eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004873 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 long n;
4876 int len;
4877 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 char_u *start_leader, *end_leader;
4879 int ret = OK;
4880 char_u *alias;
4881
4882 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004884 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 /*
4889 * Skip '!' and '-' characters. They are handled later.
4890 */
4891 start_leader = *arg;
4892 while (**arg == '!' || **arg == '-' || **arg == '+')
4893 *arg = skipwhite(*arg + 1);
4894 end_leader = *arg;
4895
4896 switch (**arg)
4897 {
4898 /*
4899 * Number constant.
4900 */
4901 case '0':
4902 case '1':
4903 case '2':
4904 case '3':
4905 case '4':
4906 case '5':
4907 case '6':
4908 case '7':
4909 case '8':
4910 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 {
4912#ifdef FEAT_FLOAT
4913 char_u *p = skipdigits(*arg + 1);
4914 int get_float = FALSE;
4915
4916 /* We accept a float when the format matches
4917 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004918 * strict to avoid backwards compatibility problems.
4919 * Don't look for a float after the "." operator, so that
4920 * ":let vers = 1.2.3" doesn't fail. */
4921 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923 get_float = TRUE;
4924 p = skipdigits(p + 2);
4925 if (*p == 'e' || *p == 'E')
4926 {
4927 ++p;
4928 if (*p == '-' || *p == '+')
4929 ++p;
4930 if (!vim_isdigit(*p))
4931 get_float = FALSE;
4932 else
4933 p = skipdigits(p + 1);
4934 }
4935 if (ASCII_ISALPHA(*p) || *p == '.')
4936 get_float = FALSE;
4937 }
4938 if (get_float)
4939 {
4940 float_T f;
4941
4942 *arg += string2float(*arg, &f);
4943 if (evaluate)
4944 {
4945 rettv->v_type = VAR_FLOAT;
4946 rettv->vval.v_float = f;
4947 }
4948 }
4949 else
4950#endif
4951 {
4952 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4953 *arg += len;
4954 if (evaluate)
4955 {
4956 rettv->v_type = VAR_NUMBER;
4957 rettv->vval.v_number = n;
4958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
4960 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963 /*
4964 * String constant: "string".
4965 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004966 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 break;
4968
4969 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004972 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004973 break;
4974
4975 /*
4976 * List: [expr, expr]
4977 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004978 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 break;
4980
4981 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 * Dictionary: {key: val, key: val}
4983 */
4984 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4985 break;
4986
4987 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004988 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004990 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 break;
4992
4993 /*
4994 * Environment variable: $VAR.
4995 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004996 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 break;
4998
4999 /*
5000 * Register contents: @r.
5001 */
5002 case '@': ++*arg;
5003 if (evaluate)
5004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005006 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 if (**arg != NUL)
5009 ++*arg;
5010 break;
5011
5012 /*
5013 * nested expression: (expression).
5014 */
5015 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (**arg == ')')
5018 ++*arg;
5019 else if (ret == OK)
5020 {
5021 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005022 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 ret = FAIL;
5024 }
5025 break;
5026
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 break;
5029 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030
5031 if (ret == NOTDONE)
5032 {
5033 /*
5034 * Must be a variable or function name.
5035 * Can also be a curly-braces kind of name: {expr}.
5036 */
5037 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005038 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 if (alias != NULL)
5040 s = alias;
5041
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005042 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 ret = FAIL;
5044 else
5045 {
5046 if (**arg == '(') /* recursive! */
5047 {
5048 /* If "s" is the name of a variable of type VAR_FUNC
5049 * use its contents. */
5050 s = deref_func_name(s, &len);
5051
5052 /* Invoke the function. */
5053 ret = get_func_tv(s, len, rettv, arg,
5054 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005055 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 /* Stop the expression evaluation when immediately
5057 * aborting on error, or when an interrupt occurred or
5058 * an exception was thrown but not caught. */
5059 if (aborting())
5060 {
5061 if (ret == OK)
5062 clear_tv(rettv);
5063 ret = FAIL;
5064 }
5065 }
5066 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005067 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005068 else
5069 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 }
5071
5072 if (alias != NULL)
5073 vim_free(alias);
5074 }
5075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 *arg = skipwhite(*arg);
5077
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005078 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5079 * expr(expr). */
5080 if (ret == OK)
5081 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
5083 /*
5084 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5085 */
5086 if (ret == OK && evaluate && end_leader > start_leader)
5087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005088 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005089 int val = 0;
5090#ifdef FEAT_FLOAT
5091 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005092
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 if (rettv->v_type == VAR_FLOAT)
5094 f = rettv->vval.v_float;
5095 else
5096#endif
5097 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005098 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005100 clear_tv(rettv);
5101 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005103 else
5104 {
5105 while (end_leader > start_leader)
5106 {
5107 --end_leader;
5108 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 {
5110#ifdef FEAT_FLOAT
5111 if (rettv->v_type == VAR_FLOAT)
5112 f = !f;
5113 else
5114#endif
5115 val = !val;
5116 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005117 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 {
5119#ifdef FEAT_FLOAT
5120 if (rettv->v_type == VAR_FLOAT)
5121 f = -f;
5122 else
5123#endif
5124 val = -val;
5125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005127#ifdef FEAT_FLOAT
5128 if (rettv->v_type == VAR_FLOAT)
5129 {
5130 clear_tv(rettv);
5131 rettv->vval.v_float = f;
5132 }
5133 else
5134#endif
5135 {
5136 clear_tv(rettv);
5137 rettv->v_type = VAR_NUMBER;
5138 rettv->vval.v_number = val;
5139 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142
5143 return ret;
5144}
5145
5146/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005147 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5148 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5150 */
5151 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005152eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005153 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005154 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005156 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157{
5158 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005159 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 long len = -1;
5162 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005163 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005166 if (rettv->v_type == VAR_FUNC
5167#ifdef FEAT_FLOAT
5168 || rettv->v_type == VAR_FLOAT
5169#endif
5170 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005171 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005172 if (verbose)
5173 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 return FAIL;
5175 }
5176
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 /*
5180 * dict.name
5181 */
5182 key = *arg + 1;
5183 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5184 ;
5185 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 }
5189 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191 /*
5192 * something[idx]
5193 *
5194 * Get the (first) variable from inside the [].
5195 */
5196 *arg = skipwhite(*arg + 1);
5197 if (**arg == ':')
5198 empty1 = TRUE;
5199 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5200 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5202 {
5203 /* not a number or string */
5204 clear_tv(&var1);
5205 return FAIL;
5206 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207
5208 /*
5209 * Get the second variable from inside the [:].
5210 */
5211 if (**arg == ':')
5212 {
5213 range = TRUE;
5214 *arg = skipwhite(*arg + 1);
5215 if (**arg == ']')
5216 empty2 = TRUE;
5217 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 if (!empty1)
5220 clear_tv(&var1);
5221 return FAIL;
5222 }
5223 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5224 {
5225 /* not a number or string */
5226 if (!empty1)
5227 clear_tv(&var1);
5228 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 return FAIL;
5230 }
5231 }
5232
5233 /* Check for the ']'. */
5234 if (**arg != ']')
5235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236 if (verbose)
5237 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 clear_tv(&var1);
5239 if (range)
5240 clear_tv(&var2);
5241 return FAIL;
5242 }
5243 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 }
5245
5246 if (evaluate)
5247 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 n1 = 0;
5249 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005251 n1 = get_tv_number(&var1);
5252 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 }
5254 if (range)
5255 {
5256 if (empty2)
5257 n2 = -1;
5258 else
5259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 n2 = get_tv_number(&var2);
5261 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 }
5263 }
5264
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005265 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 {
5267 case VAR_NUMBER:
5268 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005269 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 len = (long)STRLEN(s);
5271 if (range)
5272 {
5273 /* The resulting variable is a substring. If the indexes
5274 * are out of range the result is empty. */
5275 if (n1 < 0)
5276 {
5277 n1 = len + n1;
5278 if (n1 < 0)
5279 n1 = 0;
5280 }
5281 if (n2 < 0)
5282 n2 = len + n2;
5283 else if (n2 >= len)
5284 n2 = len;
5285 if (n1 >= len || n2 < 0 || n1 > n2)
5286 s = NULL;
5287 else
5288 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5289 }
5290 else
5291 {
5292 /* The resulting variable is a string of a single
5293 * character. If the index is too big or negative the
5294 * result is empty. */
5295 if (n1 >= len || n1 < 0)
5296 s = NULL;
5297 else
5298 s = vim_strnsave(s + n1, 1);
5299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005300 clear_tv(rettv);
5301 rettv->v_type = VAR_STRING;
5302 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 break;
5304
5305 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005306 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 if (n1 < 0)
5308 n1 = len + n1;
5309 if (!empty1 && (n1 < 0 || n1 >= len))
5310 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005311 /* For a range we allow invalid values and return an empty
5312 * list. A list index out of range is an error. */
5313 if (!range)
5314 {
5315 if (verbose)
5316 EMSGN(_(e_listidx), n1);
5317 return FAIL;
5318 }
5319 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 }
5321 if (range)
5322 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005323 list_T *l;
5324 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325
5326 if (n2 < 0)
5327 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005328 else if (n2 >= len)
5329 n2 = len - 1;
5330 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005331 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 l = list_alloc();
5333 if (l == NULL)
5334 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005335 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 n1 <= n2; ++n1)
5337 {
5338 if (list_append_tv(l, &item->li_tv) == FAIL)
5339 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005340 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 return FAIL;
5342 }
5343 item = item->li_next;
5344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005345 clear_tv(rettv);
5346 rettv->v_type = VAR_LIST;
5347 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005348 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 }
5350 else
5351 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005352 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005353 clear_tv(rettv);
5354 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 }
5356 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357
5358 case VAR_DICT:
5359 if (range)
5360 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005361 if (verbose)
5362 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 if (len == -1)
5364 clear_tv(&var1);
5365 return FAIL;
5366 }
5367 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005368 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369
5370 if (len == -1)
5371 {
5372 key = get_tv_string(&var1);
5373 if (*key == NUL)
5374 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005375 if (verbose)
5376 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377 clear_tv(&var1);
5378 return FAIL;
5379 }
5380 }
5381
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005382 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005384 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005385 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 if (len == -1)
5387 clear_tv(&var1);
5388 if (item == NULL)
5389 return FAIL;
5390
5391 copy_tv(&item->di_tv, &var1);
5392 clear_tv(rettv);
5393 *rettv = var1;
5394 }
5395 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 }
5398
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 return OK;
5400}
5401
5402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 * Get an option value.
5404 * "arg" points to the '&' or '+' before the option name.
5405 * "arg" is advanced to character after the option name.
5406 * Return OK or FAIL.
5407 */
5408 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 int evaluate;
5413{
5414 char_u *option_end;
5415 long numval;
5416 char_u *stringval;
5417 int opt_type;
5418 int c;
5419 int working = (**arg == '+'); /* has("+option") */
5420 int ret = OK;
5421 int opt_flags;
5422
5423 /*
5424 * Isolate the option name and find its value.
5425 */
5426 option_end = find_option_end(arg, &opt_flags);
5427 if (option_end == NULL)
5428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 EMSG2(_("E112: Option name missing: %s"), *arg);
5431 return FAIL;
5432 }
5433
5434 if (!evaluate)
5435 {
5436 *arg = option_end;
5437 return OK;
5438 }
5439
5440 c = *option_end;
5441 *option_end = NUL;
5442 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005443 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445 if (opt_type == -3) /* invalid name */
5446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 EMSG2(_("E113: Unknown option: %s"), *arg);
5449 ret = FAIL;
5450 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005451 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 {
5453 if (opt_type == -2) /* hidden string option */
5454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005455 rettv->v_type = VAR_STRING;
5456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458 else if (opt_type == -1) /* hidden number option */
5459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 rettv->v_type = VAR_NUMBER;
5461 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
5463 else if (opt_type == 1) /* number option */
5464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 rettv->v_type = VAR_NUMBER;
5466 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 else /* string option */
5469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 rettv->v_type = VAR_STRING;
5471 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473 }
5474 else if (working && (opt_type == -2 || opt_type == -1))
5475 ret = FAIL;
5476
5477 *option_end = c; /* put back for error messages */
5478 *arg = option_end;
5479
5480 return ret;
5481}
5482
5483/*
5484 * Allocate a variable for a string constant.
5485 * Return OK or FAIL.
5486 */
5487 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005488get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 int evaluate;
5492{
5493 char_u *p;
5494 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 int extra = 0;
5496
5497 /*
5498 * Find the end of the string, skipping backslashed characters.
5499 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005500 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 {
5502 if (*p == '\\' && p[1] != NUL)
5503 {
5504 ++p;
5505 /* A "\<x>" form occupies at least 4 characters, and produces up
5506 * to 6 characters: reserve space for 2 extra */
5507 if (*p == '<')
5508 extra += 2;
5509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
5511
5512 if (*p != '"')
5513 {
5514 EMSG2(_("E114: Missing quote: %s"), *arg);
5515 return FAIL;
5516 }
5517
5518 /* If only parsing, set *arg and return here */
5519 if (!evaluate)
5520 {
5521 *arg = p + 1;
5522 return OK;
5523 }
5524
5525 /*
5526 * Copy the string into allocated memory, handling backslashed
5527 * characters.
5528 */
5529 name = alloc((unsigned)(p - *arg + extra));
5530 if (name == NULL)
5531 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 rettv->v_type = VAR_STRING;
5533 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 if (*p == '\\')
5538 {
5539 switch (*++p)
5540 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005541 case 'b': *name++ = BS; ++p; break;
5542 case 'e': *name++ = ESC; ++p; break;
5543 case 'f': *name++ = FF; ++p; break;
5544 case 'n': *name++ = NL; ++p; break;
5545 case 'r': *name++ = CAR; ++p; break;
5546 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 case 'X': /* hex: "\x1", "\x12" */
5549 case 'x':
5550 case 'u': /* Unicode: "\u0023" */
5551 case 'U':
5552 if (vim_isxdigit(p[1]))
5553 {
5554 int n, nr;
5555 int c = toupper(*p);
5556
5557 if (c == 'X')
5558 n = 2;
5559 else
5560 n = 4;
5561 nr = 0;
5562 while (--n >= 0 && vim_isxdigit(p[1]))
5563 {
5564 ++p;
5565 nr = (nr << 4) + hex2nr(*p);
5566 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568#ifdef FEAT_MBYTE
5569 /* For "\u" store the number according to
5570 * 'encoding'. */
5571 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 else
5574#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 break;
5578
5579 /* octal: "\1", "\12", "\123" */
5580 case '0':
5581 case '1':
5582 case '2':
5583 case '3':
5584 case '4':
5585 case '5':
5586 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 case '7': *name = *p++ - '0';
5588 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005590 *name = (*name << 3) + *p++ - '0';
5591 if (*p >= '0' && *p <= '7')
5592 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 break;
5596
5597 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (extra != 0)
5600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 break;
5603 }
5604 /* FALLTHROUGH */
5605
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 break;
5608 }
5609 }
5610 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 *arg = p + 1;
5616
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 return OK;
5618}
5619
5620/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 * Return OK or FAIL.
5623 */
5624 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 int evaluate;
5629{
5630 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005631 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005632 int reduce = 0;
5633
5634 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005636 */
5637 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005639 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005640 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005642 break;
5643 ++reduce;
5644 ++p;
5645 }
5646 }
5647
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005649 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005651 return FAIL;
5652 }
5653
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005655 if (!evaluate)
5656 {
5657 *arg = p + 1;
5658 return OK;
5659 }
5660
5661 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005663 */
5664 str = alloc((unsigned)((p - *arg) - reduce));
5665 if (str == NULL)
5666 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 rettv->v_type = VAR_STRING;
5668 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005669
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005675 break;
5676 ++p;
5677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005681 *arg = p + 1;
5682
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005683 return OK;
5684}
5685
5686/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 * Allocate a variable for a List and fill it from "*arg".
5688 * Return OK or FAIL.
5689 */
5690 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005691get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005693 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 int evaluate;
5695{
Bram Moolenaar33570922005-01-25 22:26:29 +00005696 list_T *l = NULL;
5697 typval_T tv;
5698 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699
5700 if (evaluate)
5701 {
5702 l = list_alloc();
5703 if (l == NULL)
5704 return FAIL;
5705 }
5706
5707 *arg = skipwhite(*arg + 1);
5708 while (**arg != ']' && **arg != NUL)
5709 {
5710 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5711 goto failret;
5712 if (evaluate)
5713 {
5714 item = listitem_alloc();
5715 if (item != NULL)
5716 {
5717 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005718 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 list_append(l, item);
5720 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005721 else
5722 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723 }
5724
5725 if (**arg == ']')
5726 break;
5727 if (**arg != ',')
5728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730 goto failret;
5731 }
5732 *arg = skipwhite(*arg + 1);
5733 }
5734
5735 if (**arg != ']')
5736 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738failret:
5739 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005740 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 return FAIL;
5742 }
5743
5744 *arg = skipwhite(*arg + 1);
5745 if (evaluate)
5746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005747 rettv->v_type = VAR_LIST;
5748 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 ++l->lv_refcount;
5750 }
5751
5752 return OK;
5753}
5754
5755/*
5756 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005757 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005758 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005759 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760list_alloc()
5761{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005762 list_T *l;
5763
5764 l = (list_T *)alloc_clear(sizeof(list_T));
5765 if (l != NULL)
5766 {
5767 /* Prepend the list to the list of lists for garbage collection. */
5768 if (first_list != NULL)
5769 first_list->lv_used_prev = l;
5770 l->lv_used_prev = NULL;
5771 l->lv_used_next = first_list;
5772 first_list = l;
5773 }
5774 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775}
5776
5777/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005778 * Allocate an empty list for a return value.
5779 * Returns OK or FAIL.
5780 */
5781 static int
5782rettv_list_alloc(rettv)
5783 typval_T *rettv;
5784{
5785 list_T *l = list_alloc();
5786
5787 if (l == NULL)
5788 return FAIL;
5789
5790 rettv->vval.v_list = l;
5791 rettv->v_type = VAR_LIST;
5792 ++l->lv_refcount;
5793 return OK;
5794}
5795
5796/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 * Unreference a list: decrement the reference count and free it when it
5798 * becomes zero.
5799 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005800 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005802 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005804 if (l != NULL && --l->lv_refcount <= 0)
5805 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806}
5807
5808/*
5809 * Free a list, including all items it points to.
5810 * Ignores the reference count.
5811 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005812 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005813list_free(l, recurse)
5814 list_T *l;
5815 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816{
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005819 /* Remove the list from the list of lists for garbage collection. */
5820 if (l->lv_used_prev == NULL)
5821 first_list = l->lv_used_next;
5822 else
5823 l->lv_used_prev->lv_used_next = l->lv_used_next;
5824 if (l->lv_used_next != NULL)
5825 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5826
Bram Moolenaard9fba312005-06-26 22:34:35 +00005827 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005829 /* Remove the item before deleting it. */
5830 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005831 if (recurse || (item->li_tv.v_type != VAR_LIST
5832 && item->li_tv.v_type != VAR_DICT))
5833 clear_tv(&item->li_tv);
5834 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 }
5836 vim_free(l);
5837}
5838
5839/*
5840 * Allocate a list item.
5841 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005842 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843listitem_alloc()
5844{
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846}
5847
5848/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005849 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 */
5851 static void
5852listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005853 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005855 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 vim_free(item);
5857}
5858
5859/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005860 * Remove a list item from a List and free it. Also clears the value.
5861 */
5862 static void
5863listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 list_T *l;
5865 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005866{
5867 list_remove(l, item, item);
5868 listitem_free(item);
5869}
5870
5871/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872 * Get the number of items in a list.
5873 */
5874 static long
5875list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005876 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878 if (l == NULL)
5879 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005880 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881}
5882
5883/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005884 * Return TRUE when two lists have exactly the same values.
5885 */
5886 static int
5887list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005888 list_T *l1;
5889 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005890 int ic; /* ignore case for strings */
5891{
Bram Moolenaar33570922005-01-25 22:26:29 +00005892 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005893
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005894 if (l1 == NULL || l2 == NULL)
5895 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005896 if (l1 == l2)
5897 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005898 if (list_len(l1) != list_len(l2))
5899 return FALSE;
5900
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005901 for (item1 = l1->lv_first, item2 = l2->lv_first;
5902 item1 != NULL && item2 != NULL;
5903 item1 = item1->li_next, item2 = item2->li_next)
5904 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5905 return FALSE;
5906 return item1 == NULL && item2 == NULL;
5907}
5908
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01005909#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5910 || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005911/*
5912 * Return the dictitem that an entry in a hashtable points to.
5913 */
5914 dictitem_T *
5915dict_lookup(hi)
5916 hashitem_T *hi;
5917{
5918 return HI2DI(hi);
5919}
5920#endif
5921
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005922/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005923 * Return TRUE when two dictionaries have exactly the same key/values.
5924 */
5925 static int
5926dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 dict_T *d1;
5928 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005929 int ic; /* ignore case for strings */
5930{
Bram Moolenaar33570922005-01-25 22:26:29 +00005931 hashitem_T *hi;
5932 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005933 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005934
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005935 if (d1 == NULL || d2 == NULL)
5936 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005937 if (d1 == d2)
5938 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005939 if (dict_len(d1) != dict_len(d2))
5940 return FALSE;
5941
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005942 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005944 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005945 if (!HASHITEM_EMPTY(hi))
5946 {
5947 item2 = dict_find(d2, hi->hi_key, -1);
5948 if (item2 == NULL)
5949 return FALSE;
5950 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5951 return FALSE;
5952 --todo;
5953 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005954 }
5955 return TRUE;
5956}
5957
5958/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005959 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005960 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005961 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005962 */
5963 static int
5964tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 typval_T *tv1;
5966 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967 int ic; /* ignore case */
5968{
5969 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005970 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005971 static int recursive = 0; /* cach recursive loops */
5972 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005973
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005974 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005975 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005976 /* Catch lists and dicts that have an endless loop by limiting
5977 * recursiveness to 1000. We guess they are equal then. */
5978 if (recursive >= 1000)
5979 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005980
5981 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005983 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005984 ++recursive;
5985 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5986 --recursive;
5987 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005988
5989 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005990 ++recursive;
5991 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5992 --recursive;
5993 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005994
5995 case VAR_FUNC:
5996 return (tv1->vval.v_string != NULL
5997 && tv2->vval.v_string != NULL
5998 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5999
6000 case VAR_NUMBER:
6001 return tv1->vval.v_number == tv2->vval.v_number;
6002
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006003#ifdef FEAT_FLOAT
6004 case VAR_FLOAT:
6005 return tv1->vval.v_float == tv2->vval.v_float;
6006#endif
6007
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006008 case VAR_STRING:
6009 s1 = get_tv_string_buf(tv1, buf1);
6010 s2 = get_tv_string_buf(tv2, buf2);
6011 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006012 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006013
6014 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006015 return TRUE;
6016}
6017
6018/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 * Locate item with index "n" in list "l" and return it.
6020 * A negative index is counted from the end; -1 is the last item.
6021 * Returns NULL when "n" is out of range.
6022 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 long n;
6027{
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 long idx;
6030
6031 if (l == NULL)
6032 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006033
6034 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006036 n = l->lv_len + n;
6037
6038 /* Check for index out of range. */
6039 if (n < 0 || n >= l->lv_len)
6040 return NULL;
6041
6042 /* When there is a cached index may start search from there. */
6043 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006045 if (n < l->lv_idx / 2)
6046 {
6047 /* closest to the start of the list */
6048 item = l->lv_first;
6049 idx = 0;
6050 }
6051 else if (n > (l->lv_idx + l->lv_len) / 2)
6052 {
6053 /* closest to the end of the list */
6054 item = l->lv_last;
6055 idx = l->lv_len - 1;
6056 }
6057 else
6058 {
6059 /* closest to the cached index */
6060 item = l->lv_idx_item;
6061 idx = l->lv_idx;
6062 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 }
6064 else
6065 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006066 if (n < l->lv_len / 2)
6067 {
6068 /* closest to the start of the list */
6069 item = l->lv_first;
6070 idx = 0;
6071 }
6072 else
6073 {
6074 /* closest to the end of the list */
6075 item = l->lv_last;
6076 idx = l->lv_len - 1;
6077 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006079
6080 while (n > idx)
6081 {
6082 /* search forward */
6083 item = item->li_next;
6084 ++idx;
6085 }
6086 while (n < idx)
6087 {
6088 /* search backward */
6089 item = item->li_prev;
6090 --idx;
6091 }
6092
6093 /* cache the used index */
6094 l->lv_idx = idx;
6095 l->lv_idx_item = item;
6096
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 return item;
6098}
6099
6100/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006101 * Get list item "l[idx]" as a number.
6102 */
6103 static long
6104list_find_nr(l, idx, errorp)
6105 list_T *l;
6106 long idx;
6107 int *errorp; /* set to TRUE when something wrong */
6108{
6109 listitem_T *li;
6110
6111 li = list_find(l, idx);
6112 if (li == NULL)
6113 {
6114 if (errorp != NULL)
6115 *errorp = TRUE;
6116 return -1L;
6117 }
6118 return get_tv_number_chk(&li->li_tv, errorp);
6119}
6120
6121/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006122 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6123 */
6124 char_u *
6125list_find_str(l, idx)
6126 list_T *l;
6127 long idx;
6128{
6129 listitem_T *li;
6130
6131 li = list_find(l, idx - 1);
6132 if (li == NULL)
6133 {
6134 EMSGN(_(e_listidx), idx);
6135 return NULL;
6136 }
6137 return get_tv_string(&li->li_tv);
6138}
6139
6140/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006141 * Locate "item" list "l" and return its index.
6142 * Returns -1 when "item" is not in the list.
6143 */
6144 static long
6145list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 list_T *l;
6147 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006148{
6149 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006151
6152 if (l == NULL)
6153 return -1;
6154 idx = 0;
6155 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6156 ++idx;
6157 if (li == NULL)
6158 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006159 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006160}
6161
6162/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163 * Append item "item" to the end of list "l".
6164 */
6165 static void
6166list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 list_T *l;
6168 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169{
6170 if (l->lv_last == NULL)
6171 {
6172 /* empty list */
6173 l->lv_first = item;
6174 l->lv_last = item;
6175 item->li_prev = NULL;
6176 }
6177 else
6178 {
6179 l->lv_last->li_next = item;
6180 item->li_prev = l->lv_last;
6181 l->lv_last = item;
6182 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006183 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 item->li_next = NULL;
6185}
6186
6187/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006189 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006190 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006191 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 list_T *l;
6194 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006196 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197
Bram Moolenaar05159a02005-02-26 23:04:13 +00006198 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006200 copy_tv(tv, &li->li_tv);
6201 list_append(l, li);
6202 return OK;
6203}
6204
6205/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006206 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006207 * Return FAIL when out of memory.
6208 */
6209 int
6210list_append_dict(list, dict)
6211 list_T *list;
6212 dict_T *dict;
6213{
6214 listitem_T *li = listitem_alloc();
6215
6216 if (li == NULL)
6217 return FAIL;
6218 li->li_tv.v_type = VAR_DICT;
6219 li->li_tv.v_lock = 0;
6220 li->li_tv.vval.v_dict = dict;
6221 list_append(list, li);
6222 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006223 return OK;
6224}
6225
6226/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006227 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006228 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006229 * Returns FAIL when out of memory.
6230 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006231 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006232list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006233 list_T *l;
6234 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006235 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006236{
6237 listitem_T *li = listitem_alloc();
6238
6239 if (li == NULL)
6240 return FAIL;
6241 list_append(l, li);
6242 li->li_tv.v_type = VAR_STRING;
6243 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006244 if (str == NULL)
6245 li->li_tv.vval.v_string = NULL;
6246 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006247 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006248 return FAIL;
6249 return OK;
6250}
6251
6252/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006253 * Append "n" to list "l".
6254 * Returns FAIL when out of memory.
6255 */
6256 static int
6257list_append_number(l, n)
6258 list_T *l;
6259 varnumber_T n;
6260{
6261 listitem_T *li;
6262
6263 li = listitem_alloc();
6264 if (li == NULL)
6265 return FAIL;
6266 li->li_tv.v_type = VAR_NUMBER;
6267 li->li_tv.v_lock = 0;
6268 li->li_tv.vval.v_number = n;
6269 list_append(l, li);
6270 return OK;
6271}
6272
6273/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006275 * If "item" is NULL append at the end.
6276 * Return FAIL when out of memory.
6277 */
6278 static int
6279list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 list_T *l;
6281 typval_T *tv;
6282 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006283{
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006285
6286 if (ni == NULL)
6287 return FAIL;
6288 copy_tv(tv, &ni->li_tv);
6289 if (item == NULL)
6290 /* Append new item at end of list. */
6291 list_append(l, ni);
6292 else
6293 {
6294 /* Insert new item before existing item. */
6295 ni->li_prev = item->li_prev;
6296 ni->li_next = item;
6297 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006298 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006299 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 ++l->lv_idx;
6301 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006302 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006304 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006305 l->lv_idx_item = NULL;
6306 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006307 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 }
6310 return OK;
6311}
6312
6313/*
6314 * Extend "l1" with "l2".
6315 * If "bef" is NULL append at the end, otherwise insert before this item.
6316 * Returns FAIL when out of memory.
6317 */
6318 static int
6319list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 list_T *l1;
6321 list_T *l2;
6322 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006323{
Bram Moolenaar33570922005-01-25 22:26:29 +00006324 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006325 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006326
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006327 /* We also quit the loop when we have inserted the original item count of
6328 * the list, avoid a hang when we extend a list with itself. */
6329 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006330 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6331 return FAIL;
6332 return OK;
6333}
6334
6335/*
6336 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6337 * Return FAIL when out of memory.
6338 */
6339 static int
6340list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 list_T *l1;
6342 list_T *l2;
6343 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006344{
Bram Moolenaar33570922005-01-25 22:26:29 +00006345 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006346
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006347 if (l1 == NULL || l2 == NULL)
6348 return FAIL;
6349
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006350 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006351 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352 if (l == NULL)
6353 return FAIL;
6354 tv->v_type = VAR_LIST;
6355 tv->vval.v_list = l;
6356
6357 /* append all items from the second list */
6358 return list_extend(l, l2, NULL);
6359}
6360
6361/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006362 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006364 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006365 * Returns NULL when out of memory.
6366 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006368list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006371 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372{
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 list_T *copy;
6374 listitem_T *item;
6375 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376
6377 if (orig == NULL)
6378 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379
6380 copy = list_alloc();
6381 if (copy != NULL)
6382 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006383 if (copyID != 0)
6384 {
6385 /* Do this before adding the items, because one of the items may
6386 * refer back to this list. */
6387 orig->lv_copyID = copyID;
6388 orig->lv_copylist = copy;
6389 }
6390 for (item = orig->lv_first; item != NULL && !got_int;
6391 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 {
6393 ni = listitem_alloc();
6394 if (ni == NULL)
6395 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006396 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006397 {
6398 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6399 {
6400 vim_free(ni);
6401 break;
6402 }
6403 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006405 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406 list_append(copy, ni);
6407 }
6408 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006409 if (item != NULL)
6410 {
6411 list_unref(copy);
6412 copy = NULL;
6413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 }
6415
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 return copy;
6417}
6418
6419/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006421 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006424list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 list_T *l;
6426 listitem_T *item;
6427 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428{
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 /* notify watchers */
6432 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006434 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435 list_fix_watch(l, ip);
6436 if (ip == item2)
6437 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439
6440 if (item2->li_next == NULL)
6441 l->lv_last = item->li_prev;
6442 else
6443 item2->li_next->li_prev = item->li_prev;
6444 if (item->li_prev == NULL)
6445 l->lv_first = item2->li_next;
6446 else
6447 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006448 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006449}
6450
6451/*
6452 * Return an allocated string with the string representation of a list.
6453 * May return NULL.
6454 */
6455 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006456list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006457 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006458 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459{
6460 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461
6462 if (tv->vval.v_list == NULL)
6463 return NULL;
6464 ga_init2(&ga, (int)sizeof(char), 80);
6465 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006466 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467 {
6468 vim_free(ga.ga_data);
6469 return NULL;
6470 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006471 ga_append(&ga, ']');
6472 ga_append(&ga, NUL);
6473 return (char_u *)ga.ga_data;
6474}
6475
6476/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006477 * Join list "l" into a string in "*gap", using separator "sep".
6478 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006479 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006480 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006482list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006483 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006485 char_u *sep;
6486 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006488{
6489 int first = TRUE;
6490 char_u *tofree;
6491 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493 char_u *s;
6494
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006496 {
6497 if (first)
6498 first = FALSE;
6499 else
6500 ga_concat(gap, sep);
6501
6502 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006503 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006504 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006505 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006506 if (s != NULL)
6507 ga_concat(gap, s);
6508 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 if (s == NULL)
6510 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006511 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006512 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006513 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006514}
6515
6516/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006517 * Garbage collection for lists and dictionaries.
6518 *
6519 * We use reference counts to be able to free most items right away when they
6520 * are no longer used. But for composite items it's possible that it becomes
6521 * unused while the reference count is > 0: When there is a recursive
6522 * reference. Example:
6523 * :let l = [1, 2, 3]
6524 * :let d = {9: l}
6525 * :let l[1] = d
6526 *
6527 * Since this is quite unusual we handle this with garbage collection: every
6528 * once in a while find out which lists and dicts are not referenced from any
6529 * variable.
6530 *
6531 * Here is a good reference text about garbage collection (refers to Python
6532 * but it applies to all reference-counting mechanisms):
6533 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006534 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006535
6536/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006537 * Do garbage collection for lists and dicts.
6538 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006539 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006540 int
6541garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006542{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006543 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006544 buf_T *buf;
6545 win_T *wp;
6546 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006547 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006548 int did_free;
6549 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006550#ifdef FEAT_WINDOWS
6551 tabpage_T *tp;
6552#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006553
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006554 /* Only do this once. */
6555 want_garbage_collect = FALSE;
6556 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006557 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006558
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006559 /* We advance by two because we add one for items referenced through
6560 * previous_funccal. */
6561 current_copyID += COPYID_INC;
6562 copyID = current_copyID;
6563
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006564 /*
6565 * 1. Go through all accessible variables and mark all lists and dicts
6566 * with copyID.
6567 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006568
6569 /* Don't free variables in the previous_funccal list unless they are only
6570 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006571 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006572 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6573 {
6574 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6575 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6576 }
6577
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006578 /* script-local variables */
6579 for (i = 1; i <= ga_scripts.ga_len; ++i)
6580 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6581
6582 /* buffer-local variables */
6583 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6584 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6585
6586 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006587 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006588 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6589
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006590#ifdef FEAT_WINDOWS
6591 /* tabpage-local variables */
6592 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6593 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6594#endif
6595
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006596 /* global variables */
6597 set_ref_in_ht(&globvarht, copyID);
6598
6599 /* function-local variables */
6600 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6601 {
6602 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6603 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6604 }
6605
Bram Moolenaard812df62008-11-09 12:46:09 +00006606 /* v: vars */
6607 set_ref_in_ht(&vimvarht, copyID);
6608
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006609 /*
6610 * 2. Free lists and dictionaries that are not referenced.
6611 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006612 did_free = free_unref_items(copyID);
6613
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006614 /*
6615 * 3. Check if any funccal can be freed now.
6616 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006617 for (pfc = &previous_funccal; *pfc != NULL; )
6618 {
6619 if (can_free_funccal(*pfc, copyID))
6620 {
6621 fc = *pfc;
6622 *pfc = fc->caller;
6623 free_funccal(fc, TRUE);
6624 did_free = TRUE;
6625 did_free_funccal = TRUE;
6626 }
6627 else
6628 pfc = &(*pfc)->caller;
6629 }
6630 if (did_free_funccal)
6631 /* When a funccal was freed some more items might be garbage
6632 * collected, so run again. */
6633 (void)garbage_collect();
6634
6635 return did_free;
6636}
6637
6638/*
6639 * Free lists and dictionaries that are no longer referenced.
6640 */
6641 static int
6642free_unref_items(copyID)
6643 int copyID;
6644{
6645 dict_T *dd;
6646 list_T *ll;
6647 int did_free = FALSE;
6648
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006649 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006650 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006651 */
6652 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006653 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006654 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006655 /* Free the Dictionary and ordinary items it contains, but don't
6656 * recurse into Lists and Dictionaries, they will be in the list
6657 * of dicts or list of lists. */
6658 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006659 did_free = TRUE;
6660
6661 /* restart, next dict may also have been freed */
6662 dd = first_dict;
6663 }
6664 else
6665 dd = dd->dv_used_next;
6666
6667 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006668 * Go through the list of lists and free items without the copyID.
6669 * But don't free a list that has a watcher (used in a for loop), these
6670 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006671 */
6672 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006673 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6674 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006675 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006676 /* Free the List and ordinary items it contains, but don't recurse
6677 * into Lists and Dictionaries, they will be in the list of dicts
6678 * or list of lists. */
6679 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006680 did_free = TRUE;
6681
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006682 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006683 ll = first_list;
6684 }
6685 else
6686 ll = ll->lv_used_next;
6687
6688 return did_free;
6689}
6690
6691/*
6692 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6693 */
6694 static void
6695set_ref_in_ht(ht, copyID)
6696 hashtab_T *ht;
6697 int copyID;
6698{
6699 int todo;
6700 hashitem_T *hi;
6701
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006702 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006703 for (hi = ht->ht_array; todo > 0; ++hi)
6704 if (!HASHITEM_EMPTY(hi))
6705 {
6706 --todo;
6707 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6708 }
6709}
6710
6711/*
6712 * Mark all lists and dicts referenced through list "l" with "copyID".
6713 */
6714 static void
6715set_ref_in_list(l, copyID)
6716 list_T *l;
6717 int copyID;
6718{
6719 listitem_T *li;
6720
6721 for (li = l->lv_first; li != NULL; li = li->li_next)
6722 set_ref_in_item(&li->li_tv, copyID);
6723}
6724
6725/*
6726 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6727 */
6728 static void
6729set_ref_in_item(tv, copyID)
6730 typval_T *tv;
6731 int copyID;
6732{
6733 dict_T *dd;
6734 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735
6736 switch (tv->v_type)
6737 {
6738 case VAR_DICT:
6739 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006740 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006742 /* Didn't see this dict yet. */
6743 dd->dv_copyID = copyID;
6744 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006745 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006746 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006747
6748 case VAR_LIST:
6749 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006750 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006751 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752 /* Didn't see this list yet. */
6753 ll->lv_copyID = copyID;
6754 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006755 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006757 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006758 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006759}
6760
6761/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006762 * Allocate an empty header for a dictionary.
6763 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006764 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765dict_alloc()
6766{
Bram Moolenaar33570922005-01-25 22:26:29 +00006767 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006768
Bram Moolenaar33570922005-01-25 22:26:29 +00006769 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006770 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006771 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006772 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006773 if (first_dict != NULL)
6774 first_dict->dv_used_prev = d;
6775 d->dv_used_next = first_dict;
6776 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006777 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006780 d->dv_lock = 0;
6781 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006782 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006783 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006784 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006785}
6786
6787/*
6788 * Unreference a Dictionary: decrement the reference count and free it when it
6789 * becomes zero.
6790 */
6791 static void
6792dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006793 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006794{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006795 if (d != NULL && --d->dv_refcount <= 0)
6796 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006797}
6798
6799/*
6800 * Free a Dictionary, including all items it contains.
6801 * Ignores the reference count.
6802 */
6803 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006804dict_free(d, recurse)
6805 dict_T *d;
6806 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006807{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006808 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006810 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006811
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006812 /* Remove the dict from the list of dicts for garbage collection. */
6813 if (d->dv_used_prev == NULL)
6814 first_dict = d->dv_used_next;
6815 else
6816 d->dv_used_prev->dv_used_next = d->dv_used_next;
6817 if (d->dv_used_next != NULL)
6818 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6819
6820 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006821 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006822 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006823 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006825 if (!HASHITEM_EMPTY(hi))
6826 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006827 /* Remove the item before deleting it, just in case there is
6828 * something recursive causing trouble. */
6829 di = HI2DI(hi);
6830 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006831 if (recurse || (di->di_tv.v_type != VAR_LIST
6832 && di->di_tv.v_type != VAR_DICT))
6833 clear_tv(&di->di_tv);
6834 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006835 --todo;
6836 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006837 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006839 vim_free(d);
6840}
6841
6842/*
6843 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006844 * The "key" is copied to the new item.
6845 * Note that the value of the item "di_tv" still needs to be initialized!
6846 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006847 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006848 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006849dictitem_alloc(key)
6850 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006851{
Bram Moolenaar33570922005-01-25 22:26:29 +00006852 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006853
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006854 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006855 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006857 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006858 di->di_flags = 0;
6859 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006860 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006861}
6862
6863/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006864 * Make a copy of a Dictionary item.
6865 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006866 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006867dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006868 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006869{
Bram Moolenaar33570922005-01-25 22:26:29 +00006870 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006871
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006872 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6873 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006874 if (di != NULL)
6875 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006876 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006877 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006878 copy_tv(&org->di_tv, &di->di_tv);
6879 }
6880 return di;
6881}
6882
6883/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006884 * Remove item "item" from Dictionary "dict" and free it.
6885 */
6886 static void
6887dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006888 dict_T *dict;
6889 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006890{
Bram Moolenaar33570922005-01-25 22:26:29 +00006891 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006892
Bram Moolenaar33570922005-01-25 22:26:29 +00006893 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006894 if (HASHITEM_EMPTY(hi))
6895 EMSG2(_(e_intern2), "dictitem_remove()");
6896 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006897 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006898 dictitem_free(item);
6899}
6900
6901/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006902 * Free a dict item. Also clears the value.
6903 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006904 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006905dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006906 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006907{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006908 clear_tv(&item->di_tv);
6909 vim_free(item);
6910}
6911
6912/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006913 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6914 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006915 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006916 * Returns NULL when out of memory.
6917 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006919dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006920 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006921 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006922 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006923{
Bram Moolenaar33570922005-01-25 22:26:29 +00006924 dict_T *copy;
6925 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006926 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006927 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006928
6929 if (orig == NULL)
6930 return NULL;
6931
6932 copy = dict_alloc();
6933 if (copy != NULL)
6934 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006935 if (copyID != 0)
6936 {
6937 orig->dv_copyID = copyID;
6938 orig->dv_copydict = copy;
6939 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006940 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006941 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006943 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006944 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006945 --todo;
6946
6947 di = dictitem_alloc(hi->hi_key);
6948 if (di == NULL)
6949 break;
6950 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006951 {
6952 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6953 copyID) == FAIL)
6954 {
6955 vim_free(di);
6956 break;
6957 }
6958 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006959 else
6960 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6961 if (dict_add(copy, di) == FAIL)
6962 {
6963 dictitem_free(di);
6964 break;
6965 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006968
Bram Moolenaare9a41262005-01-15 22:18:47 +00006969 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006970 if (todo > 0)
6971 {
6972 dict_unref(copy);
6973 copy = NULL;
6974 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006975 }
6976
6977 return copy;
6978}
6979
6980/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006981 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006982 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006983 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006984 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006985dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006986 dict_T *d;
6987 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006988{
Bram Moolenaar33570922005-01-25 22:26:29 +00006989 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006990}
6991
Bram Moolenaar8c711452005-01-14 21:53:12 +00006992/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006993 * Add a number or string entry to dictionary "d".
6994 * When "str" is NULL use number "nr", otherwise use "str".
6995 * Returns FAIL when out of memory and when key already exists.
6996 */
6997 int
6998dict_add_nr_str(d, key, nr, str)
6999 dict_T *d;
7000 char *key;
7001 long nr;
7002 char_u *str;
7003{
7004 dictitem_T *item;
7005
7006 item = dictitem_alloc((char_u *)key);
7007 if (item == NULL)
7008 return FAIL;
7009 item->di_tv.v_lock = 0;
7010 if (str == NULL)
7011 {
7012 item->di_tv.v_type = VAR_NUMBER;
7013 item->di_tv.vval.v_number = nr;
7014 }
7015 else
7016 {
7017 item->di_tv.v_type = VAR_STRING;
7018 item->di_tv.vval.v_string = vim_strsave(str);
7019 }
7020 if (dict_add(d, item) == FAIL)
7021 {
7022 dictitem_free(item);
7023 return FAIL;
7024 }
7025 return OK;
7026}
7027
7028/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007029 * Get the number of items in a Dictionary.
7030 */
7031 static long
7032dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007033 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007034{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035 if (d == NULL)
7036 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007037 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007038}
7039
7040/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007041 * Find item "key[len]" in Dictionary "d".
7042 * If "len" is negative use strlen(key).
7043 * Returns NULL when not found.
7044 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007045 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007046dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007047 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007048 char_u *key;
7049 int len;
7050{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051#define AKEYLEN 200
7052 char_u buf[AKEYLEN];
7053 char_u *akey;
7054 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007055 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007056
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007057 if (len < 0)
7058 akey = key;
7059 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007061 tofree = akey = vim_strnsave(key, len);
7062 if (akey == NULL)
7063 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007064 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007065 else
7066 {
7067 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007068 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007069 akey = buf;
7070 }
7071
Bram Moolenaar33570922005-01-25 22:26:29 +00007072 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007073 vim_free(tofree);
7074 if (HASHITEM_EMPTY(hi))
7075 return NULL;
7076 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007077}
7078
7079/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007080 * Get a string item from a dictionary.
7081 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007082 * Returns NULL if the entry doesn't exist or out of memory.
7083 */
7084 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007085get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007086 dict_T *d;
7087 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007088 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007089{
7090 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007091 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007092
7093 di = dict_find(d, key, -1);
7094 if (di == NULL)
7095 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007096 s = get_tv_string(&di->di_tv);
7097 if (save && s != NULL)
7098 s = vim_strsave(s);
7099 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007100}
7101
7102/*
7103 * Get a number item from a dictionary.
7104 * Returns 0 if the entry doesn't exist or out of memory.
7105 */
7106 long
7107get_dict_number(d, key)
7108 dict_T *d;
7109 char_u *key;
7110{
7111 dictitem_T *di;
7112
7113 di = dict_find(d, key, -1);
7114 if (di == NULL)
7115 return 0;
7116 return get_tv_number(&di->di_tv);
7117}
7118
7119/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120 * Return an allocated string with the string representation of a Dictionary.
7121 * May return NULL.
7122 */
7123 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007124dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007126 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007127{
7128 garray_T ga;
7129 int first = TRUE;
7130 char_u *tofree;
7131 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007135 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007137 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007138 return NULL;
7139 ga_init2(&ga, (int)sizeof(char), 80);
7140 ga_append(&ga, '{');
7141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007142 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007143 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007145 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007147 --todo;
7148
7149 if (first)
7150 first = FALSE;
7151 else
7152 ga_concat(&ga, (char_u *)", ");
7153
7154 tofree = string_quote(hi->hi_key, FALSE);
7155 if (tofree != NULL)
7156 {
7157 ga_concat(&ga, tofree);
7158 vim_free(tofree);
7159 }
7160 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007161 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 if (s != NULL)
7163 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007165 if (s == NULL)
7166 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007167 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007169 if (todo > 0)
7170 {
7171 vim_free(ga.ga_data);
7172 return NULL;
7173 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174
7175 ga_append(&ga, '}');
7176 ga_append(&ga, NUL);
7177 return (char_u *)ga.ga_data;
7178}
7179
7180/*
7181 * Allocate a variable for a Dictionary and fill it from "*arg".
7182 * Return OK or FAIL. Returns NOTDONE for {expr}.
7183 */
7184 static int
7185get_dict_tv(arg, rettv, evaluate)
7186 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 int evaluate;
7189{
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 dict_T *d = NULL;
7191 typval_T tvkey;
7192 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007193 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197
7198 /*
7199 * First check if it's not a curly-braces thing: {expr}.
7200 * Must do this without evaluating, otherwise a function may be called
7201 * twice. Unfortunately this means we need to call eval1() twice for the
7202 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 if (*start != '}')
7206 {
7207 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7208 return FAIL;
7209 if (*start == '}')
7210 return NOTDONE;
7211 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212
7213 if (evaluate)
7214 {
7215 d = dict_alloc();
7216 if (d == NULL)
7217 return FAIL;
7218 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007219 tvkey.v_type = VAR_UNKNOWN;
7220 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221
7222 *arg = skipwhite(*arg + 1);
7223 while (**arg != '}' && **arg != NUL)
7224 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007225 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226 goto failret;
7227 if (**arg != ':')
7228 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007229 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007230 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231 goto failret;
7232 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007233 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007235 key = get_tv_string_buf_chk(&tvkey, buf);
7236 if (key == NULL || *key == NUL)
7237 {
7238 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7239 if (key != NULL)
7240 EMSG(_(e_emptykey));
7241 clear_tv(&tvkey);
7242 goto failret;
7243 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245
7246 *arg = skipwhite(*arg + 1);
7247 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7248 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007249 if (evaluate)
7250 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251 goto failret;
7252 }
7253 if (evaluate)
7254 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256 if (item != NULL)
7257 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007258 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260 clear_tv(&tv);
7261 goto failret;
7262 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263 item = dictitem_alloc(key);
7264 clear_tv(&tvkey);
7265 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007268 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 if (dict_add(d, item) == FAIL)
7270 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 }
7272 }
7273
7274 if (**arg == '}')
7275 break;
7276 if (**arg != ',')
7277 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007278 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 goto failret;
7280 }
7281 *arg = skipwhite(*arg + 1);
7282 }
7283
7284 if (**arg != '}')
7285 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007286 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287failret:
7288 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007289 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 return FAIL;
7291 }
7292
7293 *arg = skipwhite(*arg + 1);
7294 if (evaluate)
7295 {
7296 rettv->v_type = VAR_DICT;
7297 rettv->vval.v_dict = d;
7298 ++d->dv_refcount;
7299 }
7300
7301 return OK;
7302}
7303
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007305 * Return a string with the string representation of a variable.
7306 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007307 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007308 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007309 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007310 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007311 */
7312 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007313echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007315 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007316 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007317 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007318{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 static int recurse = 0;
7320 char_u *r = NULL;
7321
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007324 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 *tofree = NULL;
7326 return NULL;
7327 }
7328 ++recurse;
7329
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007330 switch (tv->v_type)
7331 {
7332 case VAR_FUNC:
7333 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334 r = tv->vval.v_string;
7335 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007336
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007338 if (tv->vval.v_list == NULL)
7339 {
7340 *tofree = NULL;
7341 r = NULL;
7342 }
7343 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7344 {
7345 *tofree = NULL;
7346 r = (char_u *)"[...]";
7347 }
7348 else
7349 {
7350 tv->vval.v_list->lv_copyID = copyID;
7351 *tofree = list2string(tv, copyID);
7352 r = *tofree;
7353 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007354 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007355
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007357 if (tv->vval.v_dict == NULL)
7358 {
7359 *tofree = NULL;
7360 r = NULL;
7361 }
7362 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7363 {
7364 *tofree = NULL;
7365 r = (char_u *)"{...}";
7366 }
7367 else
7368 {
7369 tv->vval.v_dict->dv_copyID = copyID;
7370 *tofree = dict2string(tv, copyID);
7371 r = *tofree;
7372 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007375 case VAR_STRING:
7376 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377 *tofree = NULL;
7378 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007379 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007380
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007381#ifdef FEAT_FLOAT
7382 case VAR_FLOAT:
7383 *tofree = NULL;
7384 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7385 r = numbuf;
7386 break;
7387#endif
7388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007389 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007390 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007391 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393
7394 --recurse;
7395 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007396}
7397
7398/*
7399 * Return a string with the string representation of a variable.
7400 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7401 * "numbuf" is used for a number.
7402 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007403 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007404 */
7405 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007406tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007408 char_u **tofree;
7409 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007410 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007411{
7412 switch (tv->v_type)
7413 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007414 case VAR_FUNC:
7415 *tofree = string_quote(tv->vval.v_string, TRUE);
7416 return *tofree;
7417 case VAR_STRING:
7418 *tofree = string_quote(tv->vval.v_string, FALSE);
7419 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007420#ifdef FEAT_FLOAT
7421 case VAR_FLOAT:
7422 *tofree = NULL;
7423 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7424 return numbuf;
7425#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007427 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007430 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007432 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007433 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007434}
7435
7436/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 * Return string "str" in ' quotes, doubling ' characters.
7438 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007440 */
7441 static char_u *
7442string_quote(str, function)
7443 char_u *str;
7444 int function;
7445{
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007447 char_u *p, *r, *s;
7448
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 len = (function ? 13 : 3);
7450 if (str != NULL)
7451 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007452 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 for (p = str; *p != NUL; mb_ptr_adv(p))
7454 if (*p == '\'')
7455 ++len;
7456 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007457 s = r = alloc(len);
7458 if (r != NULL)
7459 {
7460 if (function)
7461 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007463 r += 10;
7464 }
7465 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007466 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 if (str != NULL)
7468 for (p = str; *p != NUL; )
7469 {
7470 if (*p == '\'')
7471 *r++ = '\'';
7472 MB_COPY_CHAR(p, r);
7473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007475 if (function)
7476 *r++ = ')';
7477 *r++ = NUL;
7478 }
7479 return s;
7480}
7481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007482#ifdef FEAT_FLOAT
7483/*
7484 * Convert the string "text" to a floating point number.
7485 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7486 * this always uses a decimal point.
7487 * Returns the length of the text that was consumed.
7488 */
7489 static int
7490string2float(text, value)
7491 char_u *text;
7492 float_T *value; /* result stored here */
7493{
7494 char *s = (char *)text;
7495 float_T f;
7496
7497 f = strtod(s, &s);
7498 *value = f;
7499 return (int)((char_u *)s - text);
7500}
7501#endif
7502
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 * Get the value of an environment variable.
7505 * "arg" is pointing to the '$'. It is advanced to after the name.
7506 * If the environment variable was not set, silently assume it is empty.
7507 * Always return OK.
7508 */
7509 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007510get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 int evaluate;
7514{
7515 char_u *string = NULL;
7516 int len;
7517 int cc;
7518 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007519 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
7521 ++*arg;
7522 name = *arg;
7523 len = get_env_len(arg);
7524 if (evaluate)
7525 {
7526 if (len != 0)
7527 {
7528 cc = name[len];
7529 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007530 /* first try vim_getenv(), fast for normal environment vars */
7531 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007533 {
7534 if (!mustfree)
7535 string = vim_strsave(string);
7536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 else
7538 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007539 if (mustfree)
7540 vim_free(string);
7541
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 /* next try expanding things like $VIM and ${HOME} */
7543 string = expand_env_save(name - 1);
7544 if (string != NULL && *string == '$')
7545 {
7546 vim_free(string);
7547 string = NULL;
7548 }
7549 }
7550 name[len] = cc;
7551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007552 rettv->v_type = VAR_STRING;
7553 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
7555
7556 return OK;
7557}
7558
7559/*
7560 * Array with names and number of arguments of all internal functions
7561 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7562 */
7563static struct fst
7564{
7565 char *f_name; /* function name */
7566 char f_min_argc; /* minimal number of arguments */
7567 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007569 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570} functions[] =
7571{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007572#ifdef FEAT_FLOAT
7573 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007574 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007575#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007576 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {"append", 2, 2, f_append},
7578 {"argc", 0, 0, f_argc},
7579 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007580 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007581#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007582 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007583 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007584 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007587 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 {"bufexists", 1, 1, f_bufexists},
7589 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7590 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7591 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7592 {"buflisted", 1, 1, f_buflisted},
7593 {"bufloaded", 1, 1, f_bufloaded},
7594 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007595 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {"bufwinnr", 1, 1, f_bufwinnr},
7597 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007598 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007599 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007600#ifdef FEAT_FLOAT
7601 {"ceil", 1, 1, f_ceil},
7602#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007603 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 {"char2nr", 1, 1, f_char2nr},
7605 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007606 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007608#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007609 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007610 {"complete_add", 1, 1, f_complete_add},
7611 {"complete_check", 0, 0, f_complete_check},
7612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007615#ifdef FEAT_FLOAT
7616 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007617 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007618#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007619 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007621 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007622 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {"delete", 1, 1, f_delete},
7624 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007625 {"diff_filler", 1, 1, f_diff_filler},
7626 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007627 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007629 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {"eventhandler", 0, 0, f_eventhandler},
7631 {"executable", 1, 1, f_executable},
7632 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007633#ifdef FEAT_FLOAT
7634 {"exp", 1, 1, f_exp},
7635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007637 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007638 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7640 {"filereadable", 1, 1, f_filereadable},
7641 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007642 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007643 {"finddir", 1, 3, f_finddir},
7644 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007645#ifdef FEAT_FLOAT
7646 {"float2nr", 1, 1, f_float2nr},
7647 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007648 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007649#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007650 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {"fnamemodify", 2, 2, f_fnamemodify},
7652 {"foldclosed", 1, 1, f_foldclosed},
7653 {"foldclosedend", 1, 1, f_foldclosedend},
7654 {"foldlevel", 1, 1, f_foldlevel},
7655 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007656 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007658 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007659 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007660 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007661 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {"getbufvar", 2, 2, f_getbufvar},
7663 {"getchar", 0, 1, f_getchar},
7664 {"getcharmod", 0, 0, f_getcharmod},
7665 {"getcmdline", 0, 0, f_getcmdline},
7666 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007667 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007669 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007670 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {"getfsize", 1, 1, f_getfsize},
7672 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007673 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007674 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007675 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007676 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007677 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007678 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007679 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007680 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007682 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007683 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {"getwinposx", 0, 0, f_getwinposx},
7685 {"getwinposy", 0, 0, f_getwinposy},
7686 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007687 {"glob", 1, 2, f_glob},
7688 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007691 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007692 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7694 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7695 {"histadd", 2, 2, f_histadd},
7696 {"histdel", 1, 2, f_histdel},
7697 {"histget", 1, 2, f_histget},
7698 {"histnr", 1, 1, f_histnr},
7699 {"hlID", 1, 1, f_hlID},
7700 {"hlexists", 1, 1, f_hlexists},
7701 {"hostname", 0, 0, f_hostname},
7702 {"iconv", 3, 3, f_iconv},
7703 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007705 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007707 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {"inputrestore", 0, 0, f_inputrestore},
7709 {"inputsave", 0, 0, f_inputsave},
7710 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007711 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007713 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {"libcall", 3, 3, f_libcall},
7720 {"libcallnr", 3, 3, f_libcallnr},
7721 {"line", 1, 1, f_line},
7722 {"line2byte", 1, 1, f_line2byte},
7723 {"lispindent", 1, 1, f_lispindent},
7724 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007725#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007726 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007727 {"log10", 1, 1, f_log10},
7728#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007730 {"maparg", 1, 3, f_maparg},
7731 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007732 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007733 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007734 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007735 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007736 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007737 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007738 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007739 {"max", 1, 1, f_max},
7740 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007741#ifdef vim_mkdir
7742 {"mkdir", 1, 3, f_mkdir},
7743#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007744 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007745#ifdef FEAT_MZSCHEME
7746 {"mzeval", 1, 1, f_mzeval},
7747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {"nextnonblank", 1, 1, f_nextnonblank},
7749 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007750 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007751#ifdef FEAT_FLOAT
7752 {"pow", 2, 2, f_pow},
7753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007755 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007756 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007757 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007758 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007759 {"reltime", 0, 2, f_reltime},
7760 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {"remote_expr", 2, 3, f_remote_expr},
7762 {"remote_foreground", 1, 1, f_remote_foreground},
7763 {"remote_peek", 1, 2, f_remote_peek},
7764 {"remote_read", 1, 1, f_remote_read},
7765 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007766 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007768 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007770 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007771#ifdef FEAT_FLOAT
7772 {"round", 1, 1, f_round},
7773#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007774 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007775 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007776 {"searchpair", 3, 7, f_searchpair},
7777 {"searchpairpos", 3, 7, f_searchpairpos},
7778 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 {"server2client", 2, 2, f_server2client},
7780 {"serverlist", 0, 0, f_serverlist},
7781 {"setbufvar", 3, 3, f_setbufvar},
7782 {"setcmdpos", 1, 1, f_setcmdpos},
7783 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007784 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007785 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007786 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007787 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007789 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007790 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007792 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007794#ifdef FEAT_FLOAT
7795 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007796 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007797#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007798 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007799 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007800 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007801 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007802 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007803#ifdef FEAT_FLOAT
7804 {"sqrt", 1, 1, f_sqrt},
7805 {"str2float", 1, 1, f_str2float},
7806#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007807 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808#ifdef HAVE_STRFTIME
7809 {"strftime", 1, 2, f_strftime},
7810#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007811 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {"strlen", 1, 1, f_strlen},
7814 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007815 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {"strtrans", 1, 1, f_strtrans},
7817 {"submatch", 1, 1, f_submatch},
7818 {"substitute", 4, 4, f_substitute},
7819 {"synID", 3, 3, f_synID},
7820 {"synIDattr", 2, 3, f_synIDattr},
7821 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007822 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007823 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007824 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007825 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007826 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007827 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007828 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007829#ifdef FEAT_FLOAT
7830 {"tan", 1, 1, f_tan},
7831 {"tanh", 1, 1, f_tanh},
7832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007834 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 {"tolower", 1, 1, f_tolower},
7836 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007837 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#ifdef FEAT_FLOAT
7839 {"trunc", 1, 1, f_trunc},
7840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007842 {"undofile", 1, 1, f_undofile},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007843 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"virtcol", 1, 1, f_virtcol},
7845 {"visualmode", 0, 1, f_visualmode},
7846 {"winbufnr", 1, 1, f_winbufnr},
7847 {"wincol", 0, 0, f_wincol},
7848 {"winheight", 1, 1, f_winheight},
7849 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007850 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007852 {"winrestview", 1, 1, f_winrestview},
7853 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007855 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856};
7857
7858#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7859
7860/*
7861 * Function given to ExpandGeneric() to obtain the list of internal
7862 * or user defined function names.
7863 */
7864 char_u *
7865get_function_name(xp, idx)
7866 expand_T *xp;
7867 int idx;
7868{
7869 static int intidx = -1;
7870 char_u *name;
7871
7872 if (idx == 0)
7873 intidx = -1;
7874 if (intidx < 0)
7875 {
7876 name = get_user_func_name(xp, idx);
7877 if (name != NULL)
7878 return name;
7879 }
7880 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7881 {
7882 STRCPY(IObuff, functions[intidx].f_name);
7883 STRCAT(IObuff, "(");
7884 if (functions[intidx].f_max_argc == 0)
7885 STRCAT(IObuff, ")");
7886 return IObuff;
7887 }
7888
7889 return NULL;
7890}
7891
7892/*
7893 * Function given to ExpandGeneric() to obtain the list of internal or
7894 * user defined variable or function names.
7895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 char_u *
7897get_expr_name(xp, idx)
7898 expand_T *xp;
7899 int idx;
7900{
7901 static int intidx = -1;
7902 char_u *name;
7903
7904 if (idx == 0)
7905 intidx = -1;
7906 if (intidx < 0)
7907 {
7908 name = get_function_name(xp, idx);
7909 if (name != NULL)
7910 return name;
7911 }
7912 return get_user_var_name(xp, ++intidx);
7913}
7914
7915#endif /* FEAT_CMDL_COMPL */
7916
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007917#if defined(EBCDIC) || defined(PROTO)
7918/*
7919 * Compare struct fst by function name.
7920 */
7921 static int
7922compare_func_name(s1, s2)
7923 const void *s1;
7924 const void *s2;
7925{
7926 struct fst *p1 = (struct fst *)s1;
7927 struct fst *p2 = (struct fst *)s2;
7928
7929 return STRCMP(p1->f_name, p2->f_name);
7930}
7931
7932/*
7933 * Sort the function table by function name.
7934 * The sorting of the table above is ASCII dependant.
7935 * On machines using EBCDIC we have to sort it.
7936 */
7937 static void
7938sortFunctions()
7939{
7940 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7941
7942 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
7943}
7944#endif
7945
7946
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947/*
7948 * Find internal function in table above.
7949 * Return index, or -1 if not found
7950 */
7951 static int
7952find_internal_func(name)
7953 char_u *name; /* name of the function */
7954{
7955 int first = 0;
7956 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7957 int cmp;
7958 int x;
7959
7960 /*
7961 * Find the function name in the table. Binary search.
7962 */
7963 while (first <= last)
7964 {
7965 x = first + ((unsigned)(last - first) >> 1);
7966 cmp = STRCMP(name, functions[x].f_name);
7967 if (cmp < 0)
7968 last = x - 1;
7969 else if (cmp > 0)
7970 first = x + 1;
7971 else
7972 return x;
7973 }
7974 return -1;
7975}
7976
7977/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007978 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7979 * name it contains, otherwise return "name".
7980 */
7981 static char_u *
7982deref_func_name(name, lenp)
7983 char_u *name;
7984 int *lenp;
7985{
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007987 int cc;
7988
7989 cc = name[*lenp];
7990 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007991 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007992 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007994 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007995 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007996 {
7997 *lenp = 0;
7998 return (char_u *)""; /* just in case */
7999 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008000 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008002 }
8003
8004 return name;
8005}
8006
8007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 * Allocate a variable for the result of a function.
8009 * Return OK or FAIL.
8010 */
8011 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008012get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8013 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 char_u *name; /* name of the function */
8015 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 char_u **arg; /* argument, pointing to the '(' */
8018 linenr_T firstline; /* first line of range */
8019 linenr_T lastline; /* last line of range */
8020 int *doesrange; /* return: function handled range */
8021 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008022 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024 char_u *argp;
8025 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008026 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 int argcount = 0; /* number of arguments found */
8028
8029 /*
8030 * Get the arguments.
8031 */
8032 argp = *arg;
8033 while (argcount < MAX_FUNC_ARGS)
8034 {
8035 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8036 if (*argp == ')' || *argp == ',' || *argp == NUL)
8037 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8039 {
8040 ret = FAIL;
8041 break;
8042 }
8043 ++argcount;
8044 if (*argp != ',')
8045 break;
8046 }
8047 if (*argp == ')')
8048 ++argp;
8049 else
8050 ret = FAIL;
8051
8052 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008053 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008054 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 {
8057 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008058 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008060 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
8063 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065
8066 *arg = skipwhite(argp);
8067 return ret;
8068}
8069
8070
8071/*
8072 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008073 * Return OK when the function can't be called, FAIL otherwise.
8074 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 */
8076 static int
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008077call_func(func_name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008078 doesrange, evaluate, selfdict)
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008079 char_u *func_name; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008081 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008083 typval_T *argvars; /* vars for arguments, must have "argcount"
8084 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 linenr_T firstline; /* first line of range */
8086 linenr_T lastline; /* last line of range */
8087 int *doesrange; /* return: function handled range */
8088 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008089 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090{
8091 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092#define ERROR_UNKNOWN 0
8093#define ERROR_TOOMANY 1
8094#define ERROR_TOOFEW 2
8095#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008096#define ERROR_DICT 4
8097#define ERROR_NONE 5
8098#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 int error = ERROR_NONE;
8100 int i;
8101 int llen;
8102 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103#define FLEN_FIXED 40
8104 char_u fname_buf[FLEN_FIXED + 1];
8105 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008106 char_u *name;
8107
8108 /* Make a copy of the name, if it comes from a funcref variable it could
8109 * be changed or deleted in the called function. */
8110 name = vim_strnsave(func_name, len);
8111 if (name == NULL)
8112 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113
8114 /*
8115 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8116 * Change <SNR>123_name() to K_SNR 123_name().
8117 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 llen = eval_fname_script(name);
8120 if (llen > 0)
8121 {
8122 fname_buf[0] = K_SPECIAL;
8123 fname_buf[1] = KS_EXTRA;
8124 fname_buf[2] = (int)KE_SNR;
8125 i = 3;
8126 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8127 {
8128 if (current_SID <= 0)
8129 error = ERROR_SCRIPT;
8130 else
8131 {
8132 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8133 i = (int)STRLEN(fname_buf);
8134 }
8135 }
8136 if (i + STRLEN(name + llen) < FLEN_FIXED)
8137 {
8138 STRCPY(fname_buf + i, name + llen);
8139 fname = fname_buf;
8140 }
8141 else
8142 {
8143 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8144 if (fname == NULL)
8145 error = ERROR_OTHER;
8146 else
8147 {
8148 mch_memmove(fname, fname_buf, (size_t)i);
8149 STRCPY(fname + i, name + llen);
8150 }
8151 }
8152 }
8153 else
8154 fname = name;
8155
8156 *doesrange = FALSE;
8157
8158
8159 /* execute the function if no errors detected and executing */
8160 if (evaluate && error == ERROR_NONE)
8161 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008162 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8163 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 error = ERROR_UNKNOWN;
8165
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008166 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {
8168 /*
8169 * User defined function.
8170 */
8171 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008172
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008174 /* Trigger FuncUndefined event, may load the function. */
8175 if (fp == NULL
8176 && apply_autocmds(EVENT_FUNCUNDEFINED,
8177 fname, fname, TRUE, NULL)
8178 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008180 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 fp = find_func(fname);
8182 }
8183#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008184 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008185 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008186 {
8187 /* loaded a package, search for the function again */
8188 fp = find_func(fname);
8189 }
8190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 if (fp != NULL)
8192 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008193 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008195 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008197 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008199 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008200 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 else
8202 {
8203 /*
8204 * Call the user function.
8205 * Save and restore search patterns, script variables and
8206 * redo buffer.
8207 */
8208 save_search_patterns();
8209 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008210 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008211 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008212 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008213 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8214 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8215 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008216 /* Function was unreferenced while being used, free it
8217 * now. */
8218 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 restoreRedobuff();
8220 restore_search_patterns();
8221 error = ERROR_NONE;
8222 }
8223 }
8224 }
8225 else
8226 {
8227 /*
8228 * Find the function name in the table, call its implementation.
8229 */
8230 i = find_internal_func(fname);
8231 if (i >= 0)
8232 {
8233 if (argcount < functions[i].f_min_argc)
8234 error = ERROR_TOOFEW;
8235 else if (argcount > functions[i].f_max_argc)
8236 error = ERROR_TOOMANY;
8237 else
8238 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008239 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008240 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 error = ERROR_NONE;
8242 }
8243 }
8244 }
8245 /*
8246 * The function call (or "FuncUndefined" autocommand sequence) might
8247 * have been aborted by an error, an interrupt, or an explicitly thrown
8248 * exception that has not been caught so far. This situation can be
8249 * tested for by calling aborting(). For an error in an internal
8250 * function or for the "E132" error in call_user_func(), however, the
8251 * throw point at which the "force_abort" flag (temporarily reset by
8252 * emsg()) is normally updated has not been reached yet. We need to
8253 * update that flag first to make aborting() reliable.
8254 */
8255 update_force_abort();
8256 }
8257 if (error == ERROR_NONE)
8258 ret = OK;
8259
8260 /*
8261 * Report an error unless the argument evaluation or function call has been
8262 * cancelled due to an aborting error, an interrupt, or an exception.
8263 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008264 if (!aborting())
8265 {
8266 switch (error)
8267 {
8268 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008269 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008270 break;
8271 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008272 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008273 break;
8274 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008275 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008276 name);
8277 break;
8278 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008279 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008280 name);
8281 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008282 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008283 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008284 name);
8285 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008286 }
8287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 if (fname != name && fname != fname_buf)
8290 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008291 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
8293 return ret;
8294}
8295
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008296/*
8297 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008298 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008299 */
8300 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008301emsg_funcname(ermsg, name)
8302 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008303 char_u *name;
8304{
8305 char_u *p;
8306
8307 if (*name == K_SPECIAL)
8308 p = concat_str((char_u *)"<SNR>", name + 3);
8309 else
8310 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008311 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008312 if (p != name)
8313 vim_free(p);
8314}
8315
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008316/*
8317 * Return TRUE for a non-zero Number and a non-empty String.
8318 */
8319 static int
8320non_zero_arg(argvars)
8321 typval_T *argvars;
8322{
8323 return ((argvars[0].v_type == VAR_NUMBER
8324 && argvars[0].vval.v_number != 0)
8325 || (argvars[0].v_type == VAR_STRING
8326 && argvars[0].vval.v_string != NULL
8327 && *argvars[0].vval.v_string != NUL));
8328}
8329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330/*********************************************
8331 * Implementation of the built-in functions
8332 */
8333
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008334#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008335static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8336
8337/*
8338 * Get the float value of "argvars[0]" into "f".
8339 * Returns FAIL when the argument is not a Number or Float.
8340 */
8341 static int
8342get_float_arg(argvars, f)
8343 typval_T *argvars;
8344 float_T *f;
8345{
8346 if (argvars[0].v_type == VAR_FLOAT)
8347 {
8348 *f = argvars[0].vval.v_float;
8349 return OK;
8350 }
8351 if (argvars[0].v_type == VAR_NUMBER)
8352 {
8353 *f = (float_T)argvars[0].vval.v_number;
8354 return OK;
8355 }
8356 EMSG(_("E808: Number or Float required"));
8357 return FAIL;
8358}
8359
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008360/*
8361 * "abs(expr)" function
8362 */
8363 static void
8364f_abs(argvars, rettv)
8365 typval_T *argvars;
8366 typval_T *rettv;
8367{
8368 if (argvars[0].v_type == VAR_FLOAT)
8369 {
8370 rettv->v_type = VAR_FLOAT;
8371 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8372 }
8373 else
8374 {
8375 varnumber_T n;
8376 int error = FALSE;
8377
8378 n = get_tv_number_chk(&argvars[0], &error);
8379 if (error)
8380 rettv->vval.v_number = -1;
8381 else if (n > 0)
8382 rettv->vval.v_number = n;
8383 else
8384 rettv->vval.v_number = -n;
8385 }
8386}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008387
8388/*
8389 * "acos()" function
8390 */
8391 static void
8392f_acos(argvars, rettv)
8393 typval_T *argvars;
8394 typval_T *rettv;
8395{
8396 float_T f;
8397
8398 rettv->v_type = VAR_FLOAT;
8399 if (get_float_arg(argvars, &f) == OK)
8400 rettv->vval.v_float = acos(f);
8401 else
8402 rettv->vval.v_float = 0.0;
8403}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404#endif
8405
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008407 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 */
8409 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008410f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008411 typval_T *argvars;
8412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
Bram Moolenaar33570922005-01-25 22:26:29 +00008414 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008416 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008417 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008419 if ((l = argvars[0].vval.v_list) != NULL
8420 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8421 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008422 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008423 }
8424 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008425 EMSG(_(e_listreq));
8426}
8427
8428/*
8429 * "append(lnum, string/list)" function
8430 */
8431 static void
8432f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 typval_T *argvars;
8434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008435{
8436 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008437 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008438 list_T *l = NULL;
8439 listitem_T *li = NULL;
8440 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008441 long added = 0;
8442
Bram Moolenaar0d660222005-01-07 21:51:51 +00008443 lnum = get_tv_lnum(argvars);
8444 if (lnum >= 0
8445 && lnum <= curbuf->b_ml.ml_line_count
8446 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008447 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008448 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008449 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008450 l = argvars[1].vval.v_list;
8451 if (l == NULL)
8452 return;
8453 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008454 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008455 for (;;)
8456 {
8457 if (l == NULL)
8458 tv = &argvars[1]; /* append a string */
8459 else if (li == NULL)
8460 break; /* end of list */
8461 else
8462 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008463 line = get_tv_string_chk(tv);
8464 if (line == NULL) /* type error */
8465 {
8466 rettv->vval.v_number = 1; /* Failed */
8467 break;
8468 }
8469 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008470 ++added;
8471 if (l == NULL)
8472 break;
8473 li = li->li_next;
8474 }
8475
8476 appended_lines_mark(lnum, added);
8477 if (curwin->w_cursor.lnum > lnum)
8478 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008480 else
8481 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482}
8483
8484/*
8485 * "argc()" function
8486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008489 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493}
8494
8495/*
8496 * "argidx()" function
8497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008500 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008503 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504}
8505
8506/*
8507 * "argv(nr)" function
8508 */
8509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008510f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008511 typval_T *argvars;
8512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513{
8514 int idx;
8515
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008516 if (argvars[0].v_type != VAR_UNKNOWN)
8517 {
8518 idx = get_tv_number_chk(&argvars[0], NULL);
8519 if (idx >= 0 && idx < ARGCOUNT)
8520 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8521 else
8522 rettv->vval.v_string = NULL;
8523 rettv->v_type = VAR_STRING;
8524 }
8525 else if (rettv_list_alloc(rettv) == OK)
8526 for (idx = 0; idx < ARGCOUNT; ++idx)
8527 list_append_string(rettv->vval.v_list,
8528 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529}
8530
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008531#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008532/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008533 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008534 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008535 static void
8536f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008537 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008538 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008539{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008540 float_T f;
8541
8542 rettv->v_type = VAR_FLOAT;
8543 if (get_float_arg(argvars, &f) == OK)
8544 rettv->vval.v_float = asin(f);
8545 else
8546 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008547}
8548
8549/*
8550 * "atan()" function
8551 */
8552 static void
8553f_atan(argvars, rettv)
8554 typval_T *argvars;
8555 typval_T *rettv;
8556{
8557 float_T f;
8558
8559 rettv->v_type = VAR_FLOAT;
8560 if (get_float_arg(argvars, &f) == OK)
8561 rettv->vval.v_float = atan(f);
8562 else
8563 rettv->vval.v_float = 0.0;
8564}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008565
8566/*
8567 * "atan2()" function
8568 */
8569 static void
8570f_atan2(argvars, rettv)
8571 typval_T *argvars;
8572 typval_T *rettv;
8573{
8574 float_T fx, fy;
8575
8576 rettv->v_type = VAR_FLOAT;
8577 if (get_float_arg(argvars, &fx) == OK
8578 && get_float_arg(&argvars[1], &fy) == OK)
8579 rettv->vval.v_float = atan2(fx, fy);
8580 else
8581 rettv->vval.v_float = 0.0;
8582}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008583#endif
8584
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585/*
8586 * "browse(save, title, initdir, default)" function
8587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008590 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593#ifdef FEAT_BROWSE
8594 int save;
8595 char_u *title;
8596 char_u *initdir;
8597 char_u *defname;
8598 char_u buf[NUMBUFLEN];
8599 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008600 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008602 save = get_tv_number_chk(&argvars[0], &error);
8603 title = get_tv_string_chk(&argvars[1]);
8604 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8605 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008607 if (error || title == NULL || initdir == NULL || defname == NULL)
8608 rettv->vval.v_string = NULL;
8609 else
8610 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008611 do_browse(save ? BROWSE_SAVE : 0,
8612 title, defname, NULL, initdir, NULL, curbuf);
8613#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008614 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008615#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008616 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008617}
8618
8619/*
8620 * "browsedir(title, initdir)" function
8621 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008623f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008624 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008626{
8627#ifdef FEAT_BROWSE
8628 char_u *title;
8629 char_u *initdir;
8630 char_u buf[NUMBUFLEN];
8631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008632 title = get_tv_string_chk(&argvars[0]);
8633 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008635 if (title == NULL || initdir == NULL)
8636 rettv->vval.v_string = NULL;
8637 else
8638 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008639 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644}
8645
Bram Moolenaar33570922005-01-25 22:26:29 +00008646static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008647
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648/*
8649 * Find a buffer by number or exact name.
8650 */
8651 static buf_T *
8652find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654{
8655 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008657 if (avar->v_type == VAR_NUMBER)
8658 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008659 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008662 if (buf == NULL)
8663 {
8664 /* No full path name match, try a match with a URL or a "nofile"
8665 * buffer, these don't use the full path. */
8666 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8667 if (buf->b_fname != NULL
8668 && (path_with_url(buf->b_fname)
8669#ifdef FEAT_QUICKFIX
8670 || bt_nofile(buf)
8671#endif
8672 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008673 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008674 break;
8675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 }
8677 return buf;
8678}
8679
8680/*
8681 * "bufexists(expr)" function
8682 */
8683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 typval_T *argvars;
8686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008688 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689}
8690
8691/*
8692 * "buflisted(expr)" function
8693 */
8694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 typval_T *argvars;
8697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 buf_T *buf;
8700
8701 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703}
8704
8705/*
8706 * "bufloaded(expr)" function
8707 */
8708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008710 typval_T *argvars;
8711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713 buf_T *buf;
8714
8715 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717}
8718
Bram Moolenaar33570922005-01-25 22:26:29 +00008719static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721/*
8722 * Get buffer by number or pattern.
8723 */
8724 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 int save_magic;
8730 char_u *save_cpo;
8731 buf_T *buf;
8732
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008733 if (tv->v_type == VAR_NUMBER)
8734 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008735 if (tv->v_type != VAR_STRING)
8736 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 if (name == NULL || *name == NUL)
8738 return curbuf;
8739 if (name[0] == '$' && name[1] == NUL)
8740 return lastbuf;
8741
8742 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8743 save_magic = p_magic;
8744 p_magic = TRUE;
8745 save_cpo = p_cpo;
8746 p_cpo = (char_u *)"";
8747
8748 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8749 TRUE, FALSE));
8750
8751 p_magic = save_magic;
8752 p_cpo = save_cpo;
8753
8754 /* If not found, try expanding the name, like done for bufexists(). */
8755 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008756 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757
8758 return buf;
8759}
8760
8761/*
8762 * "bufname(expr)" function
8763 */
8764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008766 typval_T *argvars;
8767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
8769 buf_T *buf;
8770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773 buf = get_buf_tv(&argvars[0]);
8774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 --emsg_off;
8780}
8781
8782/*
8783 * "bufnr(expr)" function
8784 */
8785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 typval_T *argvars;
8788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789{
8790 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008791 int error = FALSE;
8792 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008797 --emsg_off;
8798
8799 /* If the buffer isn't found and the second argument is not zero create a
8800 * new buffer. */
8801 if (buf == NULL
8802 && argvars[1].v_type != VAR_UNKNOWN
8803 && get_tv_number_chk(&argvars[1], &error) != 0
8804 && !error
8805 && (name = get_tv_string_chk(&argvars[0])) != NULL
8806 && !error)
8807 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8808
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813}
8814
8815/*
8816 * "bufwinnr(nr)" function
8817 */
8818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008819f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008820 typval_T *argvars;
8821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822{
8823#ifdef FEAT_WINDOWS
8824 win_T *wp;
8825 int winnr = 0;
8826#endif
8827 buf_T *buf;
8828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008829 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832#ifdef FEAT_WINDOWS
8833 for (wp = firstwin; wp; wp = wp->w_next)
8834 {
8835 ++winnr;
8836 if (wp->w_buffer == buf)
8837 break;
8838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842#endif
8843 --emsg_off;
8844}
8845
8846/*
8847 * "byte2line(byte)" function
8848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008850f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008851 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
8854#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856#else
8857 long boff = 0;
8858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008859 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 (linenr_T)0, &boff);
8865#endif
8866}
8867
8868/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008869 * "byteidx()" function
8870 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008873 typval_T *argvars;
8874 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008875{
8876#ifdef FEAT_MBYTE
8877 char_u *t;
8878#endif
8879 char_u *str;
8880 long idx;
8881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882 str = get_tv_string_chk(&argvars[0]);
8883 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008885 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008886 return;
8887
8888#ifdef FEAT_MBYTE
8889 t = str;
8890 for ( ; idx > 0; idx--)
8891 {
8892 if (*t == NUL) /* EOL reached */
8893 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008894 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008895 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008896 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008897#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008898 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008900#endif
8901}
8902
8903/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008904 * "call(func, arglist)" function
8905 */
8906 static void
8907f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008908 typval_T *argvars;
8909 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008910{
8911 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008912 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008913 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008915 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008917
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008918 if (argvars[1].v_type != VAR_LIST)
8919 {
8920 EMSG(_(e_listreq));
8921 return;
8922 }
8923 if (argvars[1].vval.v_list == NULL)
8924 return;
8925
8926 if (argvars[0].v_type == VAR_FUNC)
8927 func = argvars[0].vval.v_string;
8928 else
8929 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008930 if (*func == NUL)
8931 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008932
Bram Moolenaare9a41262005-01-15 22:18:47 +00008933 if (argvars[2].v_type != VAR_UNKNOWN)
8934 {
8935 if (argvars[2].v_type != VAR_DICT)
8936 {
8937 EMSG(_(e_dictreq));
8938 return;
8939 }
8940 selfdict = argvars[2].vval.v_dict;
8941 }
8942
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008943 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8944 item = item->li_next)
8945 {
8946 if (argc == MAX_FUNC_ARGS)
8947 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008948 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008949 break;
8950 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008951 /* Make a copy of each argument. This is needed to be able to set
8952 * v_lock to VAR_FIXED in the copy without changing the original list.
8953 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008954 copy_tv(&item->li_tv, &argv[argc++]);
8955 }
8956
8957 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008958 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008959 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8960 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008961
8962 /* Free the arguments. */
8963 while (argc > 0)
8964 clear_tv(&argv[--argc]);
8965}
8966
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008967#ifdef FEAT_FLOAT
8968/*
8969 * "ceil({float})" function
8970 */
8971 static void
8972f_ceil(argvars, rettv)
8973 typval_T *argvars;
8974 typval_T *rettv;
8975{
8976 float_T f;
8977
8978 rettv->v_type = VAR_FLOAT;
8979 if (get_float_arg(argvars, &f) == OK)
8980 rettv->vval.v_float = ceil(f);
8981 else
8982 rettv->vval.v_float = 0.0;
8983}
8984#endif
8985
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008986/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008987 * "changenr()" function
8988 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008989 static void
8990f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008991 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008992 typval_T *rettv;
8993{
8994 rettv->vval.v_number = curbuf->b_u_seq_cur;
8995}
8996
8997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 * "char2nr(string)" function
8999 */
9000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 typval_T *argvars;
9003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
9005#ifdef FEAT_MBYTE
9006 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009007 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 else
9009#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011}
9012
9013/*
9014 * "cindent(lnum)" function
9015 */
9016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009018 typval_T *argvars;
9019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
9021#ifdef FEAT_CINDENT
9022 pos_T pos;
9023 linenr_T lnum;
9024
9025 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9028 {
9029 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 curwin->w_cursor = pos;
9032 }
9033 else
9034#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036}
9037
9038/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009039 * "clearmatches()" function
9040 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009041 static void
9042f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009043 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009044 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009045{
9046#ifdef FEAT_SEARCH_EXTRA
9047 clear_matches(curwin);
9048#endif
9049}
9050
9051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 * "col(string)" function
9053 */
9054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009056 typval_T *argvars;
9057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 colnr_T col = 0;
9060 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009061 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009063 fp = var2fpos(&argvars[0], FALSE, &fnum);
9064 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 {
9066 if (fp->col == MAXCOL)
9067 {
9068 /* '> can be MAXCOL, get the length of the line then */
9069 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009070 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 else
9072 col = MAXCOL;
9073 }
9074 else
9075 {
9076 col = fp->col + 1;
9077#ifdef FEAT_VIRTUALEDIT
9078 /* col(".") when the cursor is on the NUL at the end of the line
9079 * because of "coladd" can be seen as an extra column. */
9080 if (virtual_active() && fp == &curwin->w_cursor)
9081 {
9082 char_u *p = ml_get_cursor();
9083
9084 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9085 curwin->w_virtcol - curwin->w_cursor.coladd))
9086 {
9087# ifdef FEAT_MBYTE
9088 int l;
9089
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009090 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 col += l;
9092# else
9093 if (*p != NUL && p[1] == NUL)
9094 ++col;
9095# endif
9096 }
9097 }
9098#endif
9099 }
9100 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103
Bram Moolenaar572cb562005-08-05 21:35:02 +00009104#if defined(FEAT_INS_EXPAND)
9105/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009106 * "complete()" function
9107 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009108 static void
9109f_complete(argvars, rettv)
9110 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009111 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009112{
9113 int startcol;
9114
9115 if ((State & INSERT) == 0)
9116 {
9117 EMSG(_("E785: complete() can only be used in Insert mode"));
9118 return;
9119 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009120
9121 /* Check for undo allowed here, because if something was already inserted
9122 * the line was already saved for undo and this check isn't done. */
9123 if (!undo_allowed())
9124 return;
9125
Bram Moolenaarade00832006-03-10 21:46:58 +00009126 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9127 {
9128 EMSG(_(e_invarg));
9129 return;
9130 }
9131
9132 startcol = get_tv_number_chk(&argvars[0], NULL);
9133 if (startcol <= 0)
9134 return;
9135
9136 set_completion(startcol - 1, argvars[1].vval.v_list);
9137}
9138
9139/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009140 * "complete_add()" function
9141 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009142 static void
9143f_complete_add(argvars, rettv)
9144 typval_T *argvars;
9145 typval_T *rettv;
9146{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009147 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009148}
9149
9150/*
9151 * "complete_check()" function
9152 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009153 static void
9154f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009155 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009156 typval_T *rettv;
9157{
9158 int saved = RedrawingDisabled;
9159
9160 RedrawingDisabled = 0;
9161 ins_compl_check_keys(0);
9162 rettv->vval.v_number = compl_interrupted;
9163 RedrawingDisabled = saved;
9164}
9165#endif
9166
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167/*
9168 * "confirm(message, buttons[, default [, type]])" function
9169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009172 typval_T *argvars UNUSED;
9173 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
9175#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9176 char_u *message;
9177 char_u *buttons = NULL;
9178 char_u buf[NUMBUFLEN];
9179 char_u buf2[NUMBUFLEN];
9180 int def = 1;
9181 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 char_u *typestr;
9183 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 message = get_tv_string_chk(&argvars[0]);
9186 if (message == NULL)
9187 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009188 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009190 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9191 if (buttons == NULL)
9192 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009193 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009195 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009196 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9199 if (typestr == NULL)
9200 error = TRUE;
9201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009203 switch (TOUPPER_ASC(*typestr))
9204 {
9205 case 'E': type = VIM_ERROR; break;
9206 case 'Q': type = VIM_QUESTION; break;
9207 case 'I': type = VIM_INFO; break;
9208 case 'W': type = VIM_WARNING; break;
9209 case 'G': type = VIM_GENERIC; break;
9210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 }
9212 }
9213 }
9214 }
9215
9216 if (buttons == NULL || *buttons == NUL)
9217 buttons = (char_u *)_("&Ok");
9218
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009219 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009220 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222#endif
9223}
9224
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009225/*
9226 * "copy()" function
9227 */
9228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009230 typval_T *argvars;
9231 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009232{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009233 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009236#ifdef FEAT_FLOAT
9237/*
9238 * "cos()" function
9239 */
9240 static void
9241f_cos(argvars, rettv)
9242 typval_T *argvars;
9243 typval_T *rettv;
9244{
9245 float_T f;
9246
9247 rettv->v_type = VAR_FLOAT;
9248 if (get_float_arg(argvars, &f) == OK)
9249 rettv->vval.v_float = cos(f);
9250 else
9251 rettv->vval.v_float = 0.0;
9252}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009253
9254/*
9255 * "cosh()" function
9256 */
9257 static void
9258f_cosh(argvars, rettv)
9259 typval_T *argvars;
9260 typval_T *rettv;
9261{
9262 float_T f;
9263
9264 rettv->v_type = VAR_FLOAT;
9265 if (get_float_arg(argvars, &f) == OK)
9266 rettv->vval.v_float = cosh(f);
9267 else
9268 rettv->vval.v_float = 0.0;
9269}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009270#endif
9271
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009273 * "count()" function
9274 */
9275 static void
9276f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009277 typval_T *argvars;
9278 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009279{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009280 long n = 0;
9281 int ic = FALSE;
9282
Bram Moolenaare9a41262005-01-15 22:18:47 +00009283 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009284 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009285 listitem_T *li;
9286 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009287 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009288
Bram Moolenaare9a41262005-01-15 22:18:47 +00009289 if ((l = argvars[0].vval.v_list) != NULL)
9290 {
9291 li = l->lv_first;
9292 if (argvars[2].v_type != VAR_UNKNOWN)
9293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009294 int error = FALSE;
9295
9296 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009297 if (argvars[3].v_type != VAR_UNKNOWN)
9298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009299 idx = get_tv_number_chk(&argvars[3], &error);
9300 if (!error)
9301 {
9302 li = list_find(l, idx);
9303 if (li == NULL)
9304 EMSGN(_(e_listidx), idx);
9305 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009306 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009307 if (error)
9308 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009309 }
9310
9311 for ( ; li != NULL; li = li->li_next)
9312 if (tv_equal(&li->li_tv, &argvars[1], ic))
9313 ++n;
9314 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009315 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009316 else if (argvars[0].v_type == VAR_DICT)
9317 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009318 int todo;
9319 dict_T *d;
9320 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009321
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009322 if ((d = argvars[0].vval.v_dict) != NULL)
9323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009324 int error = FALSE;
9325
Bram Moolenaare9a41262005-01-15 22:18:47 +00009326 if (argvars[2].v_type != VAR_UNKNOWN)
9327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009328 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009329 if (argvars[3].v_type != VAR_UNKNOWN)
9330 EMSG(_(e_invarg));
9331 }
9332
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009333 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009334 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009335 {
9336 if (!HASHITEM_EMPTY(hi))
9337 {
9338 --todo;
9339 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9340 ++n;
9341 }
9342 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009343 }
9344 }
9345 else
9346 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009347 rettv->vval.v_number = n;
9348}
9349
9350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9352 *
9353 * Checks the existence of a cscope connection.
9354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009356f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009357 typval_T *argvars UNUSED;
9358 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359{
9360#ifdef FEAT_CSCOPE
9361 int num = 0;
9362 char_u *dbpath = NULL;
9363 char_u *prepend = NULL;
9364 char_u buf[NUMBUFLEN];
9365
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009366 if (argvars[0].v_type != VAR_UNKNOWN
9367 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009369 num = (int)get_tv_number(&argvars[0]);
9370 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009371 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 }
9374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376#endif
9377}
9378
9379/*
9380 * "cursor(lnum, col)" function
9381 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009382 * Moves the cursor to the specified line and column.
9383 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009386f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009387 typval_T *argvars;
9388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 long line, col;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009391#ifdef FEAT_CONCEAL
9392 linenr_T oldline = curwin->w_cursor.lnum;
9393#endif
Bram Moolenaara5525202006-03-02 22:52:09 +00009394#ifdef FEAT_VIRTUALEDIT
9395 long coladd = 0;
9396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009398 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009399 if (argvars[1].v_type == VAR_UNKNOWN)
9400 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009401 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009402
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009403 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009404 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009405 line = pos.lnum;
9406 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009407#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009408 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009409#endif
9410 }
9411 else
9412 {
9413 line = get_tv_lnum(argvars);
9414 col = get_tv_number_chk(&argvars[1], NULL);
9415#ifdef FEAT_VIRTUALEDIT
9416 if (argvars[2].v_type != VAR_UNKNOWN)
9417 coladd = get_tv_number_chk(&argvars[2], NULL);
9418#endif
9419 }
9420 if (line < 0 || col < 0
9421#ifdef FEAT_VIRTUALEDIT
9422 || coladd < 0
9423#endif
9424 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009425 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 if (line > 0)
9427 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 if (col > 0)
9429 curwin->w_cursor.col = col - 1;
9430#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009431 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432#endif
9433
9434 /* Make sure the cursor is in a valid position. */
9435 check_cursor();
9436#ifdef FEAT_MBYTE
9437 /* Correct cursor for multi-byte character. */
9438 if (has_mbyte)
9439 mb_adjust_cursor();
9440#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009441
9442 curwin->w_set_curswant = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009443#ifdef FEAT_CONCEAL
9444 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
9445 {
9446 update_single_line(curwin, oldline);
9447 update_single_line(curwin, curwin->w_cursor.lnum);
9448 }
9449#endif
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009450 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451}
9452
9453/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009454 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 */
9456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009458 typval_T *argvars;
9459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009461 int noref = 0;
9462
9463 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009464 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009465 if (noref < 0 || noref > 1)
9466 EMSG(_(e_invarg));
9467 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009468 {
9469 current_copyID += COPYID_INC;
9470 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472}
9473
9474/*
9475 * "delete()" function
9476 */
9477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009479 typval_T *argvars;
9480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481{
9482 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486}
9487
9488/*
9489 * "did_filetype()" function
9490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009493 typval_T *argvars UNUSED;
9494 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495{
9496#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498#endif
9499}
9500
9501/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009502 * "diff_filler()" function
9503 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009505f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009506 typval_T *argvars UNUSED;
9507 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009508{
9509#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009511#endif
9512}
9513
9514/*
9515 * "diff_hlID()" function
9516 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009519 typval_T *argvars UNUSED;
9520 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009521{
9522#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009524 static linenr_T prev_lnum = 0;
9525 static int changedtick = 0;
9526 static int fnum = 0;
9527 static int change_start = 0;
9528 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009529 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009530 int filler_lines;
9531 int col;
9532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 if (lnum < 0) /* ignore type error in {lnum} arg */
9534 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009535 if (lnum != prev_lnum
9536 || changedtick != curbuf->b_changedtick
9537 || fnum != curbuf->b_fnum)
9538 {
9539 /* New line, buffer, change: need to get the values. */
9540 filler_lines = diff_check(curwin, lnum);
9541 if (filler_lines < 0)
9542 {
9543 if (filler_lines == -1)
9544 {
9545 change_start = MAXCOL;
9546 change_end = -1;
9547 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9548 hlID = HLF_ADD; /* added line */
9549 else
9550 hlID = HLF_CHD; /* changed line */
9551 }
9552 else
9553 hlID = HLF_ADD; /* added line */
9554 }
9555 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009556 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009557 prev_lnum = lnum;
9558 changedtick = curbuf->b_changedtick;
9559 fnum = curbuf->b_fnum;
9560 }
9561
9562 if (hlID == HLF_CHD || hlID == HLF_TXD)
9563 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009565 if (col >= change_start && col <= change_end)
9566 hlID = HLF_TXD; /* changed text */
9567 else
9568 hlID = HLF_CHD; /* changed line */
9569 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009570 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009571#endif
9572}
9573
9574/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009575 * "empty({expr})" function
9576 */
9577 static void
9578f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009579 typval_T *argvars;
9580 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009581{
9582 int n;
9583
9584 switch (argvars[0].v_type)
9585 {
9586 case VAR_STRING:
9587 case VAR_FUNC:
9588 n = argvars[0].vval.v_string == NULL
9589 || *argvars[0].vval.v_string == NUL;
9590 break;
9591 case VAR_NUMBER:
9592 n = argvars[0].vval.v_number == 0;
9593 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009594#ifdef FEAT_FLOAT
9595 case VAR_FLOAT:
9596 n = argvars[0].vval.v_float == 0.0;
9597 break;
9598#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009599 case VAR_LIST:
9600 n = argvars[0].vval.v_list == NULL
9601 || argvars[0].vval.v_list->lv_first == NULL;
9602 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009603 case VAR_DICT:
9604 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009605 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009606 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009607 default:
9608 EMSG2(_(e_intern2), "f_empty()");
9609 n = 0;
9610 }
9611
9612 rettv->vval.v_number = n;
9613}
9614
9615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 * "escape({string}, {chars})" function
9617 */
9618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009620 typval_T *argvars;
9621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 char_u buf[NUMBUFLEN];
9624
Bram Moolenaar758711c2005-02-02 23:11:38 +00009625 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9626 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628}
9629
9630/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009631 * "eval()" function
9632 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009633 static void
9634f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 typval_T *argvars;
9636 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009637{
9638 char_u *s;
9639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009640 s = get_tv_string_chk(&argvars[0]);
9641 if (s != NULL)
9642 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009644 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9645 {
9646 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009647 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009648 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009649 else if (*s != NUL)
9650 EMSG(_(e_trailing));
9651}
9652
9653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 * "eventhandler()" function
9655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009658 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662}
9663
9664/*
9665 * "executable()" function
9666 */
9667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009668f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009669 typval_T *argvars;
9670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673}
9674
9675/*
9676 * "exists()" function
9677 */
9678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 typval_T *argvars;
9681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 char_u *p;
9684 char_u *name;
9685 int n = FALSE;
9686 int len = 0;
9687
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 if (*p == '$') /* environment variable */
9690 {
9691 /* first try "normal" environment variables (fast) */
9692 if (mch_getenv(p + 1) != NULL)
9693 n = TRUE;
9694 else
9695 {
9696 /* try expanding things like $VIM and ${HOME} */
9697 p = expand_env_save(p);
9698 if (p != NULL && *p != '$')
9699 n = TRUE;
9700 vim_free(p);
9701 }
9702 }
9703 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009706 if (*skipwhite(p) != NUL)
9707 n = FALSE; /* trailing garbage */
9708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 else if (*p == '*') /* internal or user defined function */
9710 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009711 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 }
9713 else if (*p == ':')
9714 {
9715 n = cmd_exists(p + 1);
9716 }
9717 else if (*p == '#')
9718 {
9719#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009720 if (p[1] == '#')
9721 n = autocmd_supported(p + 2);
9722 else
9723 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724#endif
9725 }
9726 else /* internal variable */
9727 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009728 char_u *tofree;
9729 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009731 /* get_name_len() takes care of expanding curly braces */
9732 name = p;
9733 len = get_name_len(&p, &tofree, TRUE, FALSE);
9734 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009736 if (tofree != NULL)
9737 name = tofree;
9738 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9739 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009741 /* handle d.key, l[idx], f(expr) */
9742 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9743 if (n)
9744 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 }
9746 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009747 if (*p != NUL)
9748 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009750 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 }
9752
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754}
9755
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009756#ifdef FEAT_FLOAT
9757/*
9758 * "exp()" function
9759 */
9760 static void
9761f_exp(argvars, rettv)
9762 typval_T *argvars;
9763 typval_T *rettv;
9764{
9765 float_T f;
9766
9767 rettv->v_type = VAR_FLOAT;
9768 if (get_float_arg(argvars, &f) == OK)
9769 rettv->vval.v_float = exp(f);
9770 else
9771 rettv->vval.v_float = 0.0;
9772}
9773#endif
9774
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775/*
9776 * "expand()" function
9777 */
9778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009780 typval_T *argvars;
9781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
9783 char_u *s;
9784 int len;
9785 char_u *errormsg;
9786 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9787 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009788 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->v_type = VAR_STRING;
9791 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 if (*s == '%' || *s == '#' || *s == '<')
9793 {
9794 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009795 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 --emsg_off;
9797 }
9798 else
9799 {
9800 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009801 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 if (argvars[1].v_type != VAR_UNKNOWN
9803 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 if (!error)
9806 {
9807 ExpandInit(&xpc);
9808 xpc.xp_context = EXPAND_FILES;
9809 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009810 }
9811 else
9812 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 }
9814}
9815
9816/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009817 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009818 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009819 */
9820 static void
9821f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009822 typval_T *argvars;
9823 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009824{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009825 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009826 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009827 list_T *l1, *l2;
9828 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009829 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009830 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009831
Bram Moolenaare9a41262005-01-15 22:18:47 +00009832 l1 = argvars[0].vval.v_list;
9833 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009834 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9835 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009836 {
9837 if (argvars[2].v_type != VAR_UNKNOWN)
9838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 before = get_tv_number_chk(&argvars[2], &error);
9840 if (error)
9841 return; /* type error; errmsg already given */
9842
Bram Moolenaar758711c2005-02-02 23:11:38 +00009843 if (before == l1->lv_len)
9844 item = NULL;
9845 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009846 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009847 item = list_find(l1, before);
9848 if (item == NULL)
9849 {
9850 EMSGN(_(e_listidx), before);
9851 return;
9852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009853 }
9854 }
9855 else
9856 item = NULL;
9857 list_extend(l1, l2, item);
9858
Bram Moolenaare9a41262005-01-15 22:18:47 +00009859 copy_tv(&argvars[0], rettv);
9860 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009861 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009862 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9863 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 dict_T *d1, *d2;
9865 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009866 char_u *action;
9867 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009868 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009869 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009870
9871 d1 = argvars[0].vval.v_dict;
9872 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009873 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9874 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009875 {
9876 /* Check the third argument. */
9877 if (argvars[2].v_type != VAR_UNKNOWN)
9878 {
9879 static char *(av[]) = {"keep", "force", "error"};
9880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 action = get_tv_string_chk(&argvars[2]);
9882 if (action == NULL)
9883 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009884 for (i = 0; i < 3; ++i)
9885 if (STRCMP(action, av[i]) == 0)
9886 break;
9887 if (i == 3)
9888 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009889 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009890 return;
9891 }
9892 }
9893 else
9894 action = (char_u *)"force";
9895
9896 /* Go over all entries in the second dict and add them to the
9897 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009898 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009899 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009901 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009903 --todo;
9904 di1 = dict_find(d1, hi2->hi_key, -1);
9905 if (di1 == NULL)
9906 {
9907 di1 = dictitem_copy(HI2DI(hi2));
9908 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9909 dictitem_free(di1);
9910 }
9911 else if (*action == 'e')
9912 {
9913 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9914 break;
9915 }
9916 else if (*action == 'f')
9917 {
9918 clear_tv(&di1->di_tv);
9919 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9920 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 }
9922 }
9923
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924 copy_tv(&argvars[0], rettv);
9925 }
9926 }
9927 else
9928 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009929}
9930
9931/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009932 * "feedkeys()" function
9933 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009934 static void
9935f_feedkeys(argvars, rettv)
9936 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009937 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009938{
9939 int remap = TRUE;
9940 char_u *keys, *flags;
9941 char_u nbuf[NUMBUFLEN];
9942 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009943 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009944
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009945 /* This is not allowed in the sandbox. If the commands would still be
9946 * executed in the sandbox it would be OK, but it probably happens later,
9947 * when "sandbox" is no longer set. */
9948 if (check_secure())
9949 return;
9950
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009951 keys = get_tv_string(&argvars[0]);
9952 if (*keys != NUL)
9953 {
9954 if (argvars[1].v_type != VAR_UNKNOWN)
9955 {
9956 flags = get_tv_string_buf(&argvars[1], nbuf);
9957 for ( ; *flags != NUL; ++flags)
9958 {
9959 switch (*flags)
9960 {
9961 case 'n': remap = FALSE; break;
9962 case 'm': remap = TRUE; break;
9963 case 't': typed = TRUE; break;
9964 }
9965 }
9966 }
9967
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009968 /* Need to escape K_SPECIAL and CSI before putting the string in the
9969 * typeahead buffer. */
9970 keys_esc = vim_strsave_escape_csi(keys);
9971 if (keys_esc != NULL)
9972 {
9973 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009974 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009975 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009976 if (vgetc_busy)
9977 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009978 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009979 }
9980}
9981
9982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 * "filereadable()" function
9984 */
9985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009987 typval_T *argvars;
9988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009990 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 char_u *p;
9992 int n;
9993
Bram Moolenaarc236c162008-07-13 17:41:49 +00009994#ifndef O_NONBLOCK
9995# define O_NONBLOCK 0
9996#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009998 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9999 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 {
10001 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010002 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 }
10004 else
10005 n = FALSE;
10006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008}
10009
10010/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010011 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 * rights to write into.
10013 */
10014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010016 typval_T *argvars;
10017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010019 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020}
10021
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010022static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010023
10024 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010025findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010026 typval_T *argvars;
10027 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010028 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010029{
10030#ifdef FEAT_SEARCHPATH
10031 char_u *fname;
10032 char_u *fresult = NULL;
10033 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10034 char_u *p;
10035 char_u pathbuf[NUMBUFLEN];
10036 int count = 1;
10037 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010038 int error = FALSE;
10039#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010040
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010041 rettv->vval.v_string = NULL;
10042 rettv->v_type = VAR_STRING;
10043
10044#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010046
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010047 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010049 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10050 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010051 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010052 else
10053 {
10054 if (*p != NUL)
10055 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010057 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010058 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010059 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010060 }
10061
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010062 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10063 error = TRUE;
10064
10065 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010067 do
10068 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010069 if (rettv->v_type == VAR_STRING)
10070 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010071 fresult = find_file_in_path_option(first ? fname : NULL,
10072 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010073 0, first, path,
10074 find_what,
10075 curbuf->b_ffname,
10076 find_what == FINDFILE_DIR
10077 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010078 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010079
10080 if (fresult != NULL && rettv->v_type == VAR_LIST)
10081 list_append_string(rettv->vval.v_list, fresult, -1);
10082
10083 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010085
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010086 if (rettv->v_type == VAR_STRING)
10087 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010088#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010089}
10090
Bram Moolenaar33570922005-01-25 22:26:29 +000010091static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10092static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010093
10094/*
10095 * Implementation of map() and filter().
10096 */
10097 static void
10098filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010099 typval_T *argvars;
10100 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010101 int map;
10102{
10103 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010104 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010105 listitem_T *li, *nli;
10106 list_T *l = NULL;
10107 dictitem_T *di;
10108 hashtab_T *ht;
10109 hashitem_T *hi;
10110 dict_T *d = NULL;
10111 typval_T save_val;
10112 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010113 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010114 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010115 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010116 int save_did_emsg;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010117 int index = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010118
Bram Moolenaare9a41262005-01-15 22:18:47 +000010119 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010120 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010121 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010122 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010123 return;
10124 }
10125 else if (argvars[0].v_type == VAR_DICT)
10126 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010127 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010128 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010129 return;
10130 }
10131 else
10132 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010133 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010134 return;
10135 }
10136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 expr = get_tv_string_buf_chk(&argvars[1], buf);
10138 /* On type errors, the preceding call has already displayed an error
10139 * message. Avoid a misleading error message for an empty string that
10140 * was not passed as argument. */
10141 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010143 prepare_vimvar(VV_VAL, &save_val);
10144 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010145
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010146 /* We reset "did_emsg" to be able to detect whether an error
10147 * occurred during evaluation of the expression. */
10148 save_did_emsg = did_emsg;
10149 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010150
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010151 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010152 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010154 vimvars[VV_KEY].vv_type = VAR_STRING;
10155
10156 ht = &d->dv_hashtab;
10157 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010158 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010161 if (!HASHITEM_EMPTY(hi))
10162 {
10163 --todo;
10164 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010165 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010166 break;
10167 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010168 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010169 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010170 break;
10171 if (!map && rem)
10172 dictitem_remove(d, di);
10173 clear_tv(&vimvars[VV_KEY].vv_tv);
10174 }
10175 }
10176 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 }
10178 else
10179 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010180 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 for (li = l->lv_first; li != NULL; li = nli)
10183 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010184 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010185 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 nli = li->li_next;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010187 vimvars[VV_KEY].vv_nr = index;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010188 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010189 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010190 break;
10191 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010192 listitem_remove(l, li);
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010193 ++index;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010194 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010195 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010196
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010197 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010199
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010200 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010202
10203 copy_tv(&argvars[0], rettv);
10204}
10205
10206 static int
10207filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010208 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 char_u *expr;
10210 int map;
10211 int *remp;
10212{
Bram Moolenaar33570922005-01-25 22:26:29 +000010213 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010215 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216
Bram Moolenaar33570922005-01-25 22:26:29 +000010217 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010218 s = expr;
10219 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010220 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010221 if (*s != NUL) /* check for trailing chars after expr */
10222 {
10223 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010224 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 }
10226 if (map)
10227 {
10228 /* map(): replace the list item value */
10229 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010230 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010231 *tv = rettv;
10232 }
10233 else
10234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 int error = FALSE;
10236
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010239 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 /* On type error, nothing has been removed; return FAIL to stop the
10241 * loop. The error message was given by get_tv_number_chk(). */
10242 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010243 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010244 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010245 retval = OK;
10246theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010248 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010249}
10250
10251/*
10252 * "filter()" function
10253 */
10254 static void
10255f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010256 typval_T *argvars;
10257 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010258{
10259 filter_map(argvars, rettv, FALSE);
10260}
10261
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010262/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010263 * "finddir({fname}[, {path}[, {count}]])" function
10264 */
10265 static void
10266f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010267 typval_T *argvars;
10268 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010269{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010270 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010271}
10272
10273/*
10274 * "findfile({fname}[, {path}[, {count}]])" function
10275 */
10276 static void
10277f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010278 typval_T *argvars;
10279 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010280{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010281 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010282}
10283
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010284#ifdef FEAT_FLOAT
10285/*
10286 * "float2nr({float})" function
10287 */
10288 static void
10289f_float2nr(argvars, rettv)
10290 typval_T *argvars;
10291 typval_T *rettv;
10292{
10293 float_T f;
10294
10295 if (get_float_arg(argvars, &f) == OK)
10296 {
10297 if (f < -0x7fffffff)
10298 rettv->vval.v_number = -0x7fffffff;
10299 else if (f > 0x7fffffff)
10300 rettv->vval.v_number = 0x7fffffff;
10301 else
10302 rettv->vval.v_number = (varnumber_T)f;
10303 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010304}
10305
10306/*
10307 * "floor({float})" function
10308 */
10309 static void
10310f_floor(argvars, rettv)
10311 typval_T *argvars;
10312 typval_T *rettv;
10313{
10314 float_T f;
10315
10316 rettv->v_type = VAR_FLOAT;
10317 if (get_float_arg(argvars, &f) == OK)
10318 rettv->vval.v_float = floor(f);
10319 else
10320 rettv->vval.v_float = 0.0;
10321}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010322
10323/*
10324 * "fmod()" function
10325 */
10326 static void
10327f_fmod(argvars, rettv)
10328 typval_T *argvars;
10329 typval_T *rettv;
10330{
10331 float_T fx, fy;
10332
10333 rettv->v_type = VAR_FLOAT;
10334 if (get_float_arg(argvars, &fx) == OK
10335 && get_float_arg(&argvars[1], &fy) == OK)
10336 rettv->vval.v_float = fmod(fx, fy);
10337 else
10338 rettv->vval.v_float = 0.0;
10339}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010340#endif
10341
Bram Moolenaar0d660222005-01-07 21:51:51 +000010342/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010343 * "fnameescape({string})" function
10344 */
10345 static void
10346f_fnameescape(argvars, rettv)
10347 typval_T *argvars;
10348 typval_T *rettv;
10349{
10350 rettv->vval.v_string = vim_strsave_fnameescape(
10351 get_tv_string(&argvars[0]), FALSE);
10352 rettv->v_type = VAR_STRING;
10353}
10354
10355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 * "fnamemodify({fname}, {mods})" function
10357 */
10358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010359f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010360 typval_T *argvars;
10361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362{
10363 char_u *fname;
10364 char_u *mods;
10365 int usedlen = 0;
10366 int len;
10367 char_u *fbuf = NULL;
10368 char_u buf[NUMBUFLEN];
10369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010370 fname = get_tv_string_chk(&argvars[0]);
10371 mods = get_tv_string_buf_chk(&argvars[1], buf);
10372 if (fname == NULL || mods == NULL)
10373 fname = NULL;
10374 else
10375 {
10376 len = (int)STRLEN(fname);
10377 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010380 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010382 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010384 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 vim_free(fbuf);
10386}
10387
Bram Moolenaar33570922005-01-25 22:26:29 +000010388static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389
10390/*
10391 * "foldclosed()" function
10392 */
10393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010394foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010395 typval_T *argvars;
10396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 int end;
10398{
10399#ifdef FEAT_FOLDING
10400 linenr_T lnum;
10401 linenr_T first, last;
10402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010403 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10405 {
10406 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10407 {
10408 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010409 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010411 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 return;
10413 }
10414 }
10415#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010416 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417}
10418
10419/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010420 * "foldclosed()" function
10421 */
10422 static void
10423f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010424 typval_T *argvars;
10425 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010426{
10427 foldclosed_both(argvars, rettv, FALSE);
10428}
10429
10430/*
10431 * "foldclosedend()" function
10432 */
10433 static void
10434f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010435 typval_T *argvars;
10436 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010437{
10438 foldclosed_both(argvars, rettv, TRUE);
10439}
10440
10441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 * "foldlevel()" function
10443 */
10444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010446 typval_T *argvars;
10447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448{
10449#ifdef FEAT_FOLDING
10450 linenr_T lnum;
10451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456}
10457
10458/*
10459 * "foldtext()" function
10460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010463 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465{
10466#ifdef FEAT_FOLDING
10467 linenr_T lnum;
10468 char_u *s;
10469 char_u *r;
10470 int len;
10471 char *txt;
10472#endif
10473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010474 rettv->v_type = VAR_STRING;
10475 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010477 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10478 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10479 <= curbuf->b_ml.ml_line_count
10480 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 {
10482 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010483 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10484 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 {
10486 if (!linewhite(lnum))
10487 break;
10488 ++lnum;
10489 }
10490
10491 /* Find interesting text in this line. */
10492 s = skipwhite(ml_get(lnum));
10493 /* skip C comment-start */
10494 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010497 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010498 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010499 {
10500 s = skipwhite(ml_get(lnum + 1));
10501 if (*s == '*')
10502 s = skipwhite(s + 1);
10503 }
10504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 txt = _("+-%s%3ld lines: ");
10506 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010507 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 + 20 /* for %3ld */
10509 + STRLEN(s))); /* concatenated */
10510 if (r != NULL)
10511 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010512 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10513 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10514 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 len = (int)STRLEN(r);
10516 STRCAT(r, s);
10517 /* remove 'foldmarker' and 'commentstring' */
10518 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010519 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 }
10521 }
10522#endif
10523}
10524
10525/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010526 * "foldtextresult(lnum)" function
10527 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010530 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010531 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010532{
10533#ifdef FEAT_FOLDING
10534 linenr_T lnum;
10535 char_u *text;
10536 char_u buf[51];
10537 foldinfo_T foldinfo;
10538 int fold_count;
10539#endif
10540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541 rettv->v_type = VAR_STRING;
10542 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010543#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 /* treat illegal types and illegal string values for {lnum} the same */
10546 if (lnum < 0)
10547 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010548 fold_count = foldedCount(curwin, lnum, &foldinfo);
10549 if (fold_count > 0)
10550 {
10551 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10552 &foldinfo, buf);
10553 if (text == buf)
10554 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010556 }
10557#endif
10558}
10559
10560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 * "foreground()" function
10562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010565 typval_T *argvars UNUSED;
10566 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568#ifdef FEAT_GUI
10569 if (gui.in_use)
10570 gui_mch_set_foreground();
10571#else
10572# ifdef WIN32
10573 win32_set_foreground();
10574# endif
10575#endif
10576}
10577
10578/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010579 * "function()" function
10580 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 typval_T *argvars;
10584 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010585{
10586 char_u *s;
10587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010589 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010590 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010591 /* Don't check an autoload name for existence here. */
10592 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010593 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010594 else
10595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596 rettv->vval.v_string = vim_strsave(s);
10597 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010598 }
10599}
10600
10601/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010602 * "garbagecollect()" function
10603 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010604 static void
10605f_garbagecollect(argvars, rettv)
10606 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010607 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010608{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010609 /* This is postponed until we are back at the toplevel, because we may be
10610 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10611 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010612
10613 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10614 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010615}
10616
10617/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010618 * "get()" function
10619 */
10620 static void
10621f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010622 typval_T *argvars;
10623 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010624{
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 listitem_T *li;
10626 list_T *l;
10627 dictitem_T *di;
10628 dict_T *d;
10629 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010630
Bram Moolenaare9a41262005-01-15 22:18:47 +000010631 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010632 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010633 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010635 int error = FALSE;
10636
10637 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10638 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010639 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010640 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010642 else if (argvars[0].v_type == VAR_DICT)
10643 {
10644 if ((d = argvars[0].vval.v_dict) != NULL)
10645 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010646 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010647 if (di != NULL)
10648 tv = &di->di_tv;
10649 }
10650 }
10651 else
10652 EMSG2(_(e_listdictarg), "get()");
10653
10654 if (tv == NULL)
10655 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010656 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010657 copy_tv(&argvars[2], rettv);
10658 }
10659 else
10660 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010661}
10662
Bram Moolenaar342337a2005-07-21 21:11:17 +000010663static 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 +000010664
10665/*
10666 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010667 * Return a range (from start to end) of lines in rettv from the specified
10668 * buffer.
10669 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010670 */
10671 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010672get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010673 buf_T *buf;
10674 linenr_T start;
10675 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010676 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010677 typval_T *rettv;
10678{
10679 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010680
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010681 if (retlist && rettv_list_alloc(rettv) == FAIL)
10682 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010683
10684 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10685 return;
10686
10687 if (!retlist)
10688 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010689 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10690 p = ml_get_buf(buf, start, FALSE);
10691 else
10692 p = (char_u *)"";
10693
10694 rettv->v_type = VAR_STRING;
10695 rettv->vval.v_string = vim_strsave(p);
10696 }
10697 else
10698 {
10699 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010700 return;
10701
10702 if (start < 1)
10703 start = 1;
10704 if (end > buf->b_ml.ml_line_count)
10705 end = buf->b_ml.ml_line_count;
10706 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010707 if (list_append_string(rettv->vval.v_list,
10708 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010709 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010710 }
10711}
10712
10713/*
10714 * "getbufline()" function
10715 */
10716 static void
10717f_getbufline(argvars, rettv)
10718 typval_T *argvars;
10719 typval_T *rettv;
10720{
10721 linenr_T lnum;
10722 linenr_T end;
10723 buf_T *buf;
10724
10725 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10726 ++emsg_off;
10727 buf = get_buf_tv(&argvars[0]);
10728 --emsg_off;
10729
Bram Moolenaar661b1822005-07-28 22:36:45 +000010730 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010731 if (argvars[2].v_type == VAR_UNKNOWN)
10732 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010733 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010734 end = get_tv_lnum_buf(&argvars[2], buf);
10735
Bram Moolenaar342337a2005-07-21 21:11:17 +000010736 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010737}
10738
Bram Moolenaar0d660222005-01-07 21:51:51 +000010739/*
10740 * "getbufvar()" function
10741 */
10742 static void
10743f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010744 typval_T *argvars;
10745 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010746{
10747 buf_T *buf;
10748 buf_T *save_curbuf;
10749 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10753 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010754 ++emsg_off;
10755 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010756
10757 rettv->v_type = VAR_STRING;
10758 rettv->vval.v_string = NULL;
10759
10760 if (buf != NULL && varname != NULL)
10761 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010762 /* set curbuf to be our buf, temporarily */
10763 save_curbuf = curbuf;
10764 curbuf = buf;
10765
Bram Moolenaar0d660222005-01-07 21:51:51 +000010766 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010767 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010768 else
10769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010770 if (*varname == NUL)
10771 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10772 * scope prefix before the NUL byte is required by
10773 * find_var_in_ht(). */
10774 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010775 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010776 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010777 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010778 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010779 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010780
10781 /* restore previous notion of curbuf */
10782 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010783 }
10784
10785 --emsg_off;
10786}
10787
10788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 * "getchar()" function
10790 */
10791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792f_getchar(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 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010797 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010799 /* Position the cursor. Needed after a message that ends in a space. */
10800 windgoto(msg_row, msg_col);
10801
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 ++no_mapping;
10803 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010804 for (;;)
10805 {
10806 if (argvars[0].v_type == VAR_UNKNOWN)
10807 /* getchar(): blocking wait. */
10808 n = safe_vgetc();
10809 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10810 /* getchar(1): only check if char avail */
10811 n = vpeekc();
10812 else if (error || vpeekc() == NUL)
10813 /* illegal argument or getchar(0) and no char avail: return zero */
10814 n = 0;
10815 else
10816 /* getchar(0) and char avail: return char */
10817 n = safe_vgetc();
10818 if (n == K_IGNORE)
10819 continue;
10820 break;
10821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 --no_mapping;
10823 --allow_keys;
10824
Bram Moolenaar219b8702006-11-01 14:32:36 +000010825 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10826 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10827 vimvars[VV_MOUSE_COL].vv_nr = 0;
10828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 if (IS_SPECIAL(n) || mod_mask != 0)
10831 {
10832 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10833 int i = 0;
10834
10835 /* Turn a special key into three bytes, plus modifier. */
10836 if (mod_mask != 0)
10837 {
10838 temp[i++] = K_SPECIAL;
10839 temp[i++] = KS_MODIFIER;
10840 temp[i++] = mod_mask;
10841 }
10842 if (IS_SPECIAL(n))
10843 {
10844 temp[i++] = K_SPECIAL;
10845 temp[i++] = K_SECOND(n);
10846 temp[i++] = K_THIRD(n);
10847 }
10848#ifdef FEAT_MBYTE
10849 else if (has_mbyte)
10850 i += (*mb_char2bytes)(n, temp + i);
10851#endif
10852 else
10853 temp[i++] = n;
10854 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855 rettv->v_type = VAR_STRING;
10856 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010857
10858#ifdef FEAT_MOUSE
10859 if (n == K_LEFTMOUSE
10860 || n == K_LEFTMOUSE_NM
10861 || n == K_LEFTDRAG
10862 || n == K_LEFTRELEASE
10863 || n == K_LEFTRELEASE_NM
10864 || n == K_MIDDLEMOUSE
10865 || n == K_MIDDLEDRAG
10866 || n == K_MIDDLERELEASE
10867 || n == K_RIGHTMOUSE
10868 || n == K_RIGHTDRAG
10869 || n == K_RIGHTRELEASE
10870 || n == K_X1MOUSE
10871 || n == K_X1DRAG
10872 || n == K_X1RELEASE
10873 || n == K_X2MOUSE
10874 || n == K_X2DRAG
10875 || n == K_X2RELEASE
10876 || n == K_MOUSEDOWN
10877 || n == K_MOUSEUP)
10878 {
10879 int row = mouse_row;
10880 int col = mouse_col;
10881 win_T *win;
10882 linenr_T lnum;
10883# ifdef FEAT_WINDOWS
10884 win_T *wp;
10885# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010886 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010887
10888 if (row >= 0 && col >= 0)
10889 {
10890 /* Find the window at the mouse coordinates and compute the
10891 * text position. */
10892 win = mouse_find_win(&row, &col);
10893 (void)mouse_comp_pos(win, &row, &col, &lnum);
10894# ifdef FEAT_WINDOWS
10895 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010896 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010897# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010898 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010899 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10900 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10901 }
10902 }
10903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 }
10905}
10906
10907/*
10908 * "getcharmod()" function
10909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010911f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010912 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916}
10917
10918/*
10919 * "getcmdline()" function
10920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 rettv->v_type = VAR_STRING;
10927 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928}
10929
10930/*
10931 * "getcmdpos()" function
10932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010935 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939}
10940
10941/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010942 * "getcmdtype()" function
10943 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010944 static void
10945f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010946 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010947 typval_T *rettv;
10948{
10949 rettv->v_type = VAR_STRING;
10950 rettv->vval.v_string = alloc(2);
10951 if (rettv->vval.v_string != NULL)
10952 {
10953 rettv->vval.v_string[0] = get_cmdline_type();
10954 rettv->vval.v_string[1] = NUL;
10955 }
10956}
10957
10958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 * "getcwd()" function
10960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010962f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010963 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965{
10966 char_u cwd[MAXPATHL];
10967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010970 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 else
10972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010975 if (rettv->vval.v_string != NULL)
10976 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977#endif
10978 }
10979}
10980
10981/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010982 * "getfontname()" function
10983 */
10984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010986 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010987 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010989 rettv->v_type = VAR_STRING;
10990 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010991#ifdef FEAT_GUI
10992 if (gui.in_use)
10993 {
10994 GuiFont font;
10995 char_u *name = NULL;
10996
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010997 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010998 {
10999 /* Get the "Normal" font. Either the name saved by
11000 * hl_set_font_name() or from the font ID. */
11001 font = gui.norm_font;
11002 name = hl_get_font_name();
11003 }
11004 else
11005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011007 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11008 return;
11009 font = gui_mch_get_font(name, FALSE);
11010 if (font == NOFONT)
11011 return; /* Invalid font name, return empty string. */
11012 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011014 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011015 gui_mch_free_font(font);
11016 }
11017#endif
11018}
11019
11020/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011021 * "getfperm({fname})" function
11022 */
11023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011025 typval_T *argvars;
11026 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011027{
11028 char_u *fname;
11029 struct stat st;
11030 char_u *perm = NULL;
11031 char_u flags[] = "rwx";
11032 int i;
11033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011037 if (mch_stat((char *)fname, &st) >= 0)
11038 {
11039 perm = vim_strsave((char_u *)"---------");
11040 if (perm != NULL)
11041 {
11042 for (i = 0; i < 9; i++)
11043 {
11044 if (st.st_mode & (1 << (8 - i)))
11045 perm[i] = flags[i % 3];
11046 }
11047 }
11048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011050}
11051
11052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 * "getfsize({fname})" function
11054 */
11055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011057 typval_T *argvars;
11058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059{
11060 char_u *fname;
11061 struct stat st;
11062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066
11067 if (mch_stat((char *)fname, &st) >= 0)
11068 {
11069 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011074
11075 /* non-perfect check for overflow */
11076 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11077 rettv->vval.v_number = -2;
11078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 }
11080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082}
11083
11084/*
11085 * "getftime({fname})" function
11086 */
11087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011089 typval_T *argvars;
11090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091{
11092 char_u *fname;
11093 struct stat st;
11094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096
11097 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101}
11102
11103/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011104 * "getftype({fname})" function
11105 */
11106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011108 typval_T *argvars;
11109 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011110{
11111 char_u *fname;
11112 struct stat st;
11113 char_u *type = NULL;
11114 char *t;
11115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011118 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011119 if (mch_lstat((char *)fname, &st) >= 0)
11120 {
11121#ifdef S_ISREG
11122 if (S_ISREG(st.st_mode))
11123 t = "file";
11124 else if (S_ISDIR(st.st_mode))
11125 t = "dir";
11126# ifdef S_ISLNK
11127 else if (S_ISLNK(st.st_mode))
11128 t = "link";
11129# endif
11130# ifdef S_ISBLK
11131 else if (S_ISBLK(st.st_mode))
11132 t = "bdev";
11133# endif
11134# ifdef S_ISCHR
11135 else if (S_ISCHR(st.st_mode))
11136 t = "cdev";
11137# endif
11138# ifdef S_ISFIFO
11139 else if (S_ISFIFO(st.st_mode))
11140 t = "fifo";
11141# endif
11142# ifdef S_ISSOCK
11143 else if (S_ISSOCK(st.st_mode))
11144 t = "fifo";
11145# endif
11146 else
11147 t = "other";
11148#else
11149# ifdef S_IFMT
11150 switch (st.st_mode & S_IFMT)
11151 {
11152 case S_IFREG: t = "file"; break;
11153 case S_IFDIR: t = "dir"; break;
11154# ifdef S_IFLNK
11155 case S_IFLNK: t = "link"; break;
11156# endif
11157# ifdef S_IFBLK
11158 case S_IFBLK: t = "bdev"; break;
11159# endif
11160# ifdef S_IFCHR
11161 case S_IFCHR: t = "cdev"; break;
11162# endif
11163# ifdef S_IFIFO
11164 case S_IFIFO: t = "fifo"; break;
11165# endif
11166# ifdef S_IFSOCK
11167 case S_IFSOCK: t = "socket"; break;
11168# endif
11169 default: t = "other";
11170 }
11171# else
11172 if (mch_isdir(fname))
11173 t = "dir";
11174 else
11175 t = "file";
11176# endif
11177#endif
11178 type = vim_strsave((char_u *)t);
11179 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011181}
11182
11183/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011184 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011185 */
11186 static void
11187f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 typval_T *argvars;
11189 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011190{
11191 linenr_T lnum;
11192 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011193 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011194
11195 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011196 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011197 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011198 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011199 retlist = FALSE;
11200 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011201 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011202 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011203 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011204 retlist = TRUE;
11205 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011206
Bram Moolenaar342337a2005-07-21 21:11:17 +000011207 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011208}
11209
11210/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011211 * "getmatches()" function
11212 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011213 static void
11214f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011215 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011216 typval_T *rettv;
11217{
11218#ifdef FEAT_SEARCH_EXTRA
11219 dict_T *dict;
11220 matchitem_T *cur = curwin->w_match_head;
11221
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011222 if (rettv_list_alloc(rettv) == OK)
11223 {
11224 while (cur != NULL)
11225 {
11226 dict = dict_alloc();
11227 if (dict == NULL)
11228 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011229 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11230 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11231 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11232 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11233 list_append_dict(rettv->vval.v_list, dict);
11234 cur = cur->next;
11235 }
11236 }
11237#endif
11238}
11239
11240/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011241 * "getpid()" function
11242 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011243 static void
11244f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011245 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011246 typval_T *rettv;
11247{
11248 rettv->vval.v_number = mch_get_pid();
11249}
11250
11251/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011252 * "getpos(string)" function
11253 */
11254 static void
11255f_getpos(argvars, rettv)
11256 typval_T *argvars;
11257 typval_T *rettv;
11258{
11259 pos_T *fp;
11260 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011261 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011262
11263 if (rettv_list_alloc(rettv) == OK)
11264 {
11265 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011266 fp = var2fpos(&argvars[0], TRUE, &fnum);
11267 if (fnum != -1)
11268 list_append_number(l, (varnumber_T)fnum);
11269 else
11270 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011271 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11272 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011273 list_append_number(l, (fp != NULL)
11274 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011275 : (varnumber_T)0);
11276 list_append_number(l,
11277#ifdef FEAT_VIRTUALEDIT
11278 (fp != NULL) ? (varnumber_T)fp->coladd :
11279#endif
11280 (varnumber_T)0);
11281 }
11282 else
11283 rettv->vval.v_number = FALSE;
11284}
11285
11286/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011287 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011288 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011289 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011290f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011291 typval_T *argvars UNUSED;
11292 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011293{
11294#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011295 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011296#endif
11297
Bram Moolenaar2641f772005-03-25 21:58:17 +000011298#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011299 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011300 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011301 wp = NULL;
11302 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11303 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011304 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011305 if (wp == NULL)
11306 return;
11307 }
11308
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011309 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011310 }
11311#endif
11312}
11313
11314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 * "getreg()" function
11316 */
11317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321{
11322 char_u *strregname;
11323 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011324 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011325 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011327 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011329 strregname = get_tv_string_chk(&argvars[0]);
11330 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011331 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011335 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 regname = (strregname == NULL ? '"' : *strregname);
11337 if (regname == 0)
11338 regname = '"';
11339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 rettv->vval.v_string = error ? NULL :
11342 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343}
11344
11345/*
11346 * "getregtype()" function
11347 */
11348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011350 typval_T *argvars;
11351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352{
11353 char_u *strregname;
11354 int regname;
11355 char_u buf[NUMBUFLEN + 2];
11356 long reglen = 0;
11357
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011358 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 {
11360 strregname = get_tv_string_chk(&argvars[0]);
11361 if (strregname == NULL) /* type error; errmsg already given */
11362 {
11363 rettv->v_type = VAR_STRING;
11364 rettv->vval.v_string = NULL;
11365 return;
11366 }
11367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 else
11369 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011370 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371
11372 regname = (strregname == NULL ? '"' : *strregname);
11373 if (regname == 0)
11374 regname = '"';
11375
11376 buf[0] = NUL;
11377 buf[1] = NUL;
11378 switch (get_reg_type(regname, &reglen))
11379 {
11380 case MLINE: buf[0] = 'V'; break;
11381 case MCHAR: buf[0] = 'v'; break;
11382#ifdef FEAT_VISUAL
11383 case MBLOCK:
11384 buf[0] = Ctrl_V;
11385 sprintf((char *)buf + 1, "%ld", reglen + 1);
11386 break;
11387#endif
11388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 rettv->v_type = VAR_STRING;
11390 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391}
11392
11393/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011394 * "gettabvar()" function
11395 */
11396 static void
11397f_gettabvar(argvars, rettv)
11398 typval_T *argvars;
11399 typval_T *rettv;
11400{
11401 tabpage_T *tp;
11402 dictitem_T *v;
11403 char_u *varname;
11404
11405 rettv->v_type = VAR_STRING;
11406 rettv->vval.v_string = NULL;
11407
11408 varname = get_tv_string_chk(&argvars[1]);
11409 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11410 if (tp != NULL && varname != NULL)
11411 {
11412 /* look up the variable */
11413 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11414 if (v != NULL)
11415 copy_tv(&v->di_tv, rettv);
11416 }
11417}
11418
11419/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011420 * "gettabwinvar()" function
11421 */
11422 static void
11423f_gettabwinvar(argvars, rettv)
11424 typval_T *argvars;
11425 typval_T *rettv;
11426{
11427 getwinvar(argvars, rettv, 1);
11428}
11429
11430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 * "getwinposx()" function
11432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011435 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439#ifdef FEAT_GUI
11440 if (gui.in_use)
11441 {
11442 int x, y;
11443
11444 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 }
11447#endif
11448}
11449
11450/*
11451 * "getwinposy()" function
11452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011455 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459#ifdef FEAT_GUI
11460 if (gui.in_use)
11461 {
11462 int x, y;
11463
11464 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 }
11467#endif
11468}
11469
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011470/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011471 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011472 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011473 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011474find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011475 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011476 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011477{
11478#ifdef FEAT_WINDOWS
11479 win_T *wp;
11480#endif
11481 int nr;
11482
11483 nr = get_tv_number_chk(vp, NULL);
11484
11485#ifdef FEAT_WINDOWS
11486 if (nr < 0)
11487 return NULL;
11488 if (nr == 0)
11489 return curwin;
11490
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011491 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11492 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011493 if (--nr <= 0)
11494 break;
11495 return wp;
11496#else
11497 if (nr == 0 || nr == 1)
11498 return curwin;
11499 return NULL;
11500#endif
11501}
11502
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503/*
11504 * "getwinvar()" function
11505 */
11506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011508 typval_T *argvars;
11509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011511 getwinvar(argvars, rettv, 0);
11512}
11513
11514/*
11515 * getwinvar() and gettabwinvar()
11516 */
11517 static void
11518getwinvar(argvars, rettv, off)
11519 typval_T *argvars;
11520 typval_T *rettv;
11521 int off; /* 1 for gettabwinvar() */
11522{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 win_T *win, *oldcurwin;
11524 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011525 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011526 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011528#ifdef FEAT_WINDOWS
11529 if (off == 1)
11530 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11531 else
11532 tp = curtab;
11533#endif
11534 win = find_win_by_nr(&argvars[off], tp);
11535 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011536 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538 rettv->v_type = VAR_STRING;
11539 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540
11541 if (win != NULL && varname != NULL)
11542 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011543 /* Set curwin to be our win, temporarily. Also set curbuf, so
11544 * that we can get buffer-local options. */
11545 oldcurwin = curwin;
11546 curwin = win;
11547 curbuf = win->w_buffer;
11548
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 else
11552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011553 if (*varname == NUL)
11554 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11555 * scope prefix before the NUL byte is required by
11556 * find_var_in_ht(). */
11557 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011559 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011561 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011563
11564 /* restore previous notion of curwin */
11565 curwin = oldcurwin;
11566 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 }
11568
11569 --emsg_off;
11570}
11571
11572/*
11573 * "glob()" function
11574 */
11575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011577 typval_T *argvars;
11578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011580 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011582 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011584 /* When the optional second argument is non-zero, don't remove matches
11585 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11586 if (argvars[1].v_type != VAR_UNKNOWN
11587 && get_tv_number_chk(&argvars[1], &error))
11588 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011590 if (!error)
11591 {
11592 ExpandInit(&xpc);
11593 xpc.xp_context = EXPAND_FILES;
11594 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11595 NULL, flags, WILD_ALL);
11596 }
11597 else
11598 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599}
11600
11601/*
11602 * "globpath()" function
11603 */
11604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011606 typval_T *argvars;
11607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011609 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011611 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011612 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011614 /* When the optional second argument is non-zero, don't remove matches
11615 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11616 if (argvars[2].v_type != VAR_UNKNOWN
11617 && get_tv_number_chk(&argvars[2], &error))
11618 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011620 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011621 rettv->vval.v_string = NULL;
11622 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011623 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11624 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625}
11626
11627/*
11628 * "has()" function
11629 */
11630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011632 typval_T *argvars;
11633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634{
11635 int i;
11636 char_u *name;
11637 int n = FALSE;
11638 static char *(has_list[]) =
11639 {
11640#ifdef AMIGA
11641 "amiga",
11642# ifdef FEAT_ARP
11643 "arp",
11644# endif
11645#endif
11646#ifdef __BEOS__
11647 "beos",
11648#endif
11649#ifdef MSDOS
11650# ifdef DJGPP
11651 "dos32",
11652# else
11653 "dos16",
11654# endif
11655#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011656#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 "mac",
11658#endif
11659#if defined(MACOS_X_UNIX)
11660 "macunix",
11661#endif
11662#ifdef OS2
11663 "os2",
11664#endif
11665#ifdef __QNX__
11666 "qnx",
11667#endif
11668#ifdef RISCOS
11669 "riscos",
11670#endif
11671#ifdef UNIX
11672 "unix",
11673#endif
11674#ifdef VMS
11675 "vms",
11676#endif
11677#ifdef WIN16
11678 "win16",
11679#endif
11680#ifdef WIN32
11681 "win32",
11682#endif
11683#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11684 "win32unix",
11685#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011686#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 "win64",
11688#endif
11689#ifdef EBCDIC
11690 "ebcdic",
11691#endif
11692#ifndef CASE_INSENSITIVE_FILENAME
11693 "fname_case",
11694#endif
11695#ifdef FEAT_ARABIC
11696 "arabic",
11697#endif
11698#ifdef FEAT_AUTOCMD
11699 "autocmd",
11700#endif
11701#ifdef FEAT_BEVAL
11702 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011703# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11704 "balloon_multiline",
11705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706#endif
11707#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11708 "builtin_terms",
11709# ifdef ALL_BUILTIN_TCAPS
11710 "all_builtin_terms",
11711# endif
11712#endif
11713#ifdef FEAT_BYTEOFF
11714 "byte_offset",
11715#endif
11716#ifdef FEAT_CINDENT
11717 "cindent",
11718#endif
11719#ifdef FEAT_CLIENTSERVER
11720 "clientserver",
11721#endif
11722#ifdef FEAT_CLIPBOARD
11723 "clipboard",
11724#endif
11725#ifdef FEAT_CMDL_COMPL
11726 "cmdline_compl",
11727#endif
11728#ifdef FEAT_CMDHIST
11729 "cmdline_hist",
11730#endif
11731#ifdef FEAT_COMMENTS
11732 "comments",
11733#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011734#ifdef FEAT_CONCEAL
11735 "conceal",
11736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737#ifdef FEAT_CRYPT
11738 "cryptv",
11739#endif
11740#ifdef FEAT_CSCOPE
11741 "cscope",
11742#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011743#ifdef FEAT_CURSORBIND
11744 "cursorbind",
11745#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011746#ifdef CURSOR_SHAPE
11747 "cursorshape",
11748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749#ifdef DEBUG
11750 "debug",
11751#endif
11752#ifdef FEAT_CON_DIALOG
11753 "dialog_con",
11754#endif
11755#ifdef FEAT_GUI_DIALOG
11756 "dialog_gui",
11757#endif
11758#ifdef FEAT_DIFF
11759 "diff",
11760#endif
11761#ifdef FEAT_DIGRAPHS
11762 "digraphs",
11763#endif
11764#ifdef FEAT_DND
11765 "dnd",
11766#endif
11767#ifdef FEAT_EMACS_TAGS
11768 "emacs_tags",
11769#endif
11770 "eval", /* always present, of course! */
11771#ifdef FEAT_EX_EXTRA
11772 "ex_extra",
11773#endif
11774#ifdef FEAT_SEARCH_EXTRA
11775 "extra_search",
11776#endif
11777#ifdef FEAT_FKMAP
11778 "farsi",
11779#endif
11780#ifdef FEAT_SEARCHPATH
11781 "file_in_path",
11782#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011783#if defined(UNIX) && !defined(USE_SYSTEM)
11784 "filterpipe",
11785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786#ifdef FEAT_FIND_ID
11787 "find_in_path",
11788#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011789#ifdef FEAT_FLOAT
11790 "float",
11791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792#ifdef FEAT_FOLDING
11793 "folding",
11794#endif
11795#ifdef FEAT_FOOTER
11796 "footer",
11797#endif
11798#if !defined(USE_SYSTEM) && defined(UNIX)
11799 "fork",
11800#endif
11801#ifdef FEAT_GETTEXT
11802 "gettext",
11803#endif
11804#ifdef FEAT_GUI
11805 "gui",
11806#endif
11807#ifdef FEAT_GUI_ATHENA
11808# ifdef FEAT_GUI_NEXTAW
11809 "gui_neXtaw",
11810# else
11811 "gui_athena",
11812# endif
11813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814#ifdef FEAT_GUI_GTK
11815 "gui_gtk",
11816# ifdef HAVE_GTK2
11817 "gui_gtk2",
11818# endif
11819#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011820#ifdef FEAT_GUI_GNOME
11821 "gui_gnome",
11822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823#ifdef FEAT_GUI_MAC
11824 "gui_mac",
11825#endif
11826#ifdef FEAT_GUI_MOTIF
11827 "gui_motif",
11828#endif
11829#ifdef FEAT_GUI_PHOTON
11830 "gui_photon",
11831#endif
11832#ifdef FEAT_GUI_W16
11833 "gui_win16",
11834#endif
11835#ifdef FEAT_GUI_W32
11836 "gui_win32",
11837#endif
11838#ifdef FEAT_HANGULIN
11839 "hangul_input",
11840#endif
11841#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11842 "iconv",
11843#endif
11844#ifdef FEAT_INS_EXPAND
11845 "insert_expand",
11846#endif
11847#ifdef FEAT_JUMPLIST
11848 "jumplist",
11849#endif
11850#ifdef FEAT_KEYMAP
11851 "keymap",
11852#endif
11853#ifdef FEAT_LANGMAP
11854 "langmap",
11855#endif
11856#ifdef FEAT_LIBCALL
11857 "libcall",
11858#endif
11859#ifdef FEAT_LINEBREAK
11860 "linebreak",
11861#endif
11862#ifdef FEAT_LISP
11863 "lispindent",
11864#endif
11865#ifdef FEAT_LISTCMDS
11866 "listcmds",
11867#endif
11868#ifdef FEAT_LOCALMAP
11869 "localmap",
11870#endif
11871#ifdef FEAT_MENU
11872 "menu",
11873#endif
11874#ifdef FEAT_SESSION
11875 "mksession",
11876#endif
11877#ifdef FEAT_MODIFY_FNAME
11878 "modify_fname",
11879#endif
11880#ifdef FEAT_MOUSE
11881 "mouse",
11882#endif
11883#ifdef FEAT_MOUSESHAPE
11884 "mouseshape",
11885#endif
11886#if defined(UNIX) || defined(VMS)
11887# ifdef FEAT_MOUSE_DEC
11888 "mouse_dec",
11889# endif
11890# ifdef FEAT_MOUSE_GPM
11891 "mouse_gpm",
11892# endif
11893# ifdef FEAT_MOUSE_JSB
11894 "mouse_jsbterm",
11895# endif
11896# ifdef FEAT_MOUSE_NET
11897 "mouse_netterm",
11898# endif
11899# ifdef FEAT_MOUSE_PTERM
11900 "mouse_pterm",
11901# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011902# ifdef FEAT_SYSMOUSE
11903 "mouse_sysmouse",
11904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905# ifdef FEAT_MOUSE_XTERM
11906 "mouse_xterm",
11907# endif
11908#endif
11909#ifdef FEAT_MBYTE
11910 "multi_byte",
11911#endif
11912#ifdef FEAT_MBYTE_IME
11913 "multi_byte_ime",
11914#endif
11915#ifdef FEAT_MULTI_LANG
11916 "multi_lang",
11917#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011918#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011919#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011920 "mzscheme",
11921#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923#ifdef FEAT_OLE
11924 "ole",
11925#endif
11926#ifdef FEAT_OSFILETYPE
11927 "osfiletype",
11928#endif
11929#ifdef FEAT_PATH_EXTRA
11930 "path_extra",
11931#endif
11932#ifdef FEAT_PERL
11933#ifndef DYNAMIC_PERL
11934 "perl",
11935#endif
11936#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011937#ifdef FEAT_PERSISTENT_UNDO
11938 "persistent_undo",
11939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940#ifdef FEAT_PYTHON
11941#ifndef DYNAMIC_PYTHON
11942 "python",
11943#endif
11944#endif
11945#ifdef FEAT_POSTSCRIPT
11946 "postscript",
11947#endif
11948#ifdef FEAT_PRINTER
11949 "printer",
11950#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011951#ifdef FEAT_PROFILE
11952 "profile",
11953#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011954#ifdef FEAT_RELTIME
11955 "reltime",
11956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957#ifdef FEAT_QUICKFIX
11958 "quickfix",
11959#endif
11960#ifdef FEAT_RIGHTLEFT
11961 "rightleft",
11962#endif
11963#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11964 "ruby",
11965#endif
11966#ifdef FEAT_SCROLLBIND
11967 "scrollbind",
11968#endif
11969#ifdef FEAT_CMDL_INFO
11970 "showcmd",
11971 "cmdline_info",
11972#endif
11973#ifdef FEAT_SIGNS
11974 "signs",
11975#endif
11976#ifdef FEAT_SMARTINDENT
11977 "smartindent",
11978#endif
11979#ifdef FEAT_SNIFF
11980 "sniff",
11981#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000011982#ifdef STARTUPTIME
11983 "startuptime",
11984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985#ifdef FEAT_STL_OPT
11986 "statusline",
11987#endif
11988#ifdef FEAT_SUN_WORKSHOP
11989 "sun_workshop",
11990#endif
11991#ifdef FEAT_NETBEANS_INTG
11992 "netbeans_intg",
11993#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011994#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011995 "spell",
11996#endif
11997#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 "syntax",
11999#endif
12000#if defined(USE_SYSTEM) || !defined(UNIX)
12001 "system",
12002#endif
12003#ifdef FEAT_TAG_BINS
12004 "tag_binary",
12005#endif
12006#ifdef FEAT_TAG_OLDSTATIC
12007 "tag_old_static",
12008#endif
12009#ifdef FEAT_TAG_ANYWHITE
12010 "tag_any_white",
12011#endif
12012#ifdef FEAT_TCL
12013# ifndef DYNAMIC_TCL
12014 "tcl",
12015# endif
12016#endif
12017#ifdef TERMINFO
12018 "terminfo",
12019#endif
12020#ifdef FEAT_TERMRESPONSE
12021 "termresponse",
12022#endif
12023#ifdef FEAT_TEXTOBJ
12024 "textobjects",
12025#endif
12026#ifdef HAVE_TGETENT
12027 "tgetent",
12028#endif
12029#ifdef FEAT_TITLE
12030 "title",
12031#endif
12032#ifdef FEAT_TOOLBAR
12033 "toolbar",
12034#endif
12035#ifdef FEAT_USR_CMDS
12036 "user-commands", /* was accidentally included in 5.4 */
12037 "user_commands",
12038#endif
12039#ifdef FEAT_VIMINFO
12040 "viminfo",
12041#endif
12042#ifdef FEAT_VERTSPLIT
12043 "vertsplit",
12044#endif
12045#ifdef FEAT_VIRTUALEDIT
12046 "virtualedit",
12047#endif
12048#ifdef FEAT_VISUAL
12049 "visual",
12050#endif
12051#ifdef FEAT_VISUALEXTRA
12052 "visualextra",
12053#endif
12054#ifdef FEAT_VREPLACE
12055 "vreplace",
12056#endif
12057#ifdef FEAT_WILDIGN
12058 "wildignore",
12059#endif
12060#ifdef FEAT_WILDMENU
12061 "wildmenu",
12062#endif
12063#ifdef FEAT_WINDOWS
12064 "windows",
12065#endif
12066#ifdef FEAT_WAK
12067 "winaltkeys",
12068#endif
12069#ifdef FEAT_WRITEBACKUP
12070 "writebackup",
12071#endif
12072#ifdef FEAT_XIM
12073 "xim",
12074#endif
12075#ifdef FEAT_XFONTSET
12076 "xfontset",
12077#endif
12078#ifdef USE_XSMP
12079 "xsmp",
12080#endif
12081#ifdef USE_XSMP_INTERACT
12082 "xsmp_interact",
12083#endif
12084#ifdef FEAT_XCLIPBOARD
12085 "xterm_clipboard",
12086#endif
12087#ifdef FEAT_XTERM_SAVE
12088 "xterm_save",
12089#endif
12090#if defined(UNIX) && defined(FEAT_X11)
12091 "X11",
12092#endif
12093 NULL
12094 };
12095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 for (i = 0; has_list[i] != NULL; ++i)
12098 if (STRICMP(name, has_list[i]) == 0)
12099 {
12100 n = TRUE;
12101 break;
12102 }
12103
12104 if (n == FALSE)
12105 {
12106 if (STRNICMP(name, "patch", 5) == 0)
12107 n = has_patch(atoi((char *)name + 5));
12108 else if (STRICMP(name, "vim_starting") == 0)
12109 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012110#ifdef FEAT_MBYTE
12111 else if (STRICMP(name, "multi_byte_encoding") == 0)
12112 n = has_mbyte;
12113#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012114#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12115 else if (STRICMP(name, "balloon_multiline") == 0)
12116 n = multiline_balloon_available();
12117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118#ifdef DYNAMIC_TCL
12119 else if (STRICMP(name, "tcl") == 0)
12120 n = tcl_enabled(FALSE);
12121#endif
12122#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12123 else if (STRICMP(name, "iconv") == 0)
12124 n = iconv_enabled(FALSE);
12125#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012126#ifdef DYNAMIC_MZSCHEME
12127 else if (STRICMP(name, "mzscheme") == 0)
12128 n = mzscheme_enabled(FALSE);
12129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130#ifdef DYNAMIC_RUBY
12131 else if (STRICMP(name, "ruby") == 0)
12132 n = ruby_enabled(FALSE);
12133#endif
12134#ifdef DYNAMIC_PYTHON
12135 else if (STRICMP(name, "python") == 0)
12136 n = python_enabled(FALSE);
12137#endif
12138#ifdef DYNAMIC_PERL
12139 else if (STRICMP(name, "perl") == 0)
12140 n = perl_enabled(FALSE);
12141#endif
12142#ifdef FEAT_GUI
12143 else if (STRICMP(name, "gui_running") == 0)
12144 n = (gui.in_use || gui.starting);
12145# ifdef FEAT_GUI_W32
12146 else if (STRICMP(name, "gui_win32s") == 0)
12147 n = gui_is_win32s();
12148# endif
12149# ifdef FEAT_BROWSE
12150 else if (STRICMP(name, "browse") == 0)
12151 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12152# endif
12153#endif
12154#ifdef FEAT_SYN_HL
12155 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012156 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157#endif
12158#if defined(WIN3264)
12159 else if (STRICMP(name, "win95") == 0)
12160 n = mch_windows95();
12161#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012162#ifdef FEAT_NETBEANS_INTG
12163 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012164 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 }
12167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169}
12170
12171/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012172 * "has_key()" function
12173 */
12174 static void
12175f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012176 typval_T *argvars;
12177 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012178{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012179 if (argvars[0].v_type != VAR_DICT)
12180 {
12181 EMSG(_(e_dictreq));
12182 return;
12183 }
12184 if (argvars[0].vval.v_dict == NULL)
12185 return;
12186
12187 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012188 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012189}
12190
12191/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012192 * "haslocaldir()" function
12193 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012194 static void
12195f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012196 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012197 typval_T *rettv;
12198{
12199 rettv->vval.v_number = (curwin->w_localdir != NULL);
12200}
12201
12202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 * "hasmapto()" function
12204 */
12205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012207 typval_T *argvars;
12208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209{
12210 char_u *name;
12211 char_u *mode;
12212 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012213 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012215 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012216 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 mode = (char_u *)"nvo";
12218 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012220 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012221 if (argvars[2].v_type != VAR_UNKNOWN)
12222 abbr = get_tv_number(&argvars[2]);
12223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224
Bram Moolenaar2c932302006-03-18 21:42:09 +000012225 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012226 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012228 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229}
12230
12231/*
12232 * "histadd()" function
12233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012236 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238{
12239#ifdef FEAT_CMDHIST
12240 int histype;
12241 char_u *str;
12242 char_u buf[NUMBUFLEN];
12243#endif
12244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 if (check_restricted() || check_secure())
12247 return;
12248#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012249 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12250 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251 if (histype >= 0)
12252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012253 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254 if (*str != NUL)
12255 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012256 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 return;
12260 }
12261 }
12262#endif
12263}
12264
12265/*
12266 * "histdel()" function
12267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012269f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012270 typval_T *argvars UNUSED;
12271 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272{
12273#ifdef FEAT_CMDHIST
12274 int n;
12275 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012276 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012278 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12279 if (str == NULL)
12280 n = 0;
12281 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012283 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012284 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012286 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012287 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 else
12289 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012290 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 get_tv_string_buf(&argvars[1], buf));
12292 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293#endif
12294}
12295
12296/*
12297 * "histget()" function
12298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012301 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303{
12304#ifdef FEAT_CMDHIST
12305 int type;
12306 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012307 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012309 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12310 if (str == NULL)
12311 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012313 {
12314 type = get_histtype(str);
12315 if (argvars[1].v_type == VAR_UNKNOWN)
12316 idx = get_history_idx(type);
12317 else
12318 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12319 /* -1 on type error */
12320 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012325 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326}
12327
12328/*
12329 * "histnr()" function
12330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012332f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012333 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335{
12336 int i;
12337
12338#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012339 char_u *history = get_tv_string_chk(&argvars[0]);
12340
12341 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 if (i >= HIST_CMD && i < HIST_COUNT)
12343 i = get_history_idx(i);
12344 else
12345#endif
12346 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348}
12349
12350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 * "highlightID(name)" function
12352 */
12353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012355 typval_T *argvars;
12356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012358 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359}
12360
12361/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012362 * "highlight_exists()" function
12363 */
12364 static void
12365f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012366 typval_T *argvars;
12367 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012368{
12369 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12370}
12371
12372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 * "hostname()" function
12374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012377 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379{
12380 char_u hostname[256];
12381
12382 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 rettv->v_type = VAR_STRING;
12384 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385}
12386
12387/*
12388 * iconv() function
12389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012392 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394{
12395#ifdef FEAT_MBYTE
12396 char_u buf1[NUMBUFLEN];
12397 char_u buf2[NUMBUFLEN];
12398 char_u *from, *to, *str;
12399 vimconv_T vimconv;
12400#endif
12401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012402 rettv->v_type = VAR_STRING;
12403 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404
12405#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 str = get_tv_string(&argvars[0]);
12407 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12408 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 vimconv.vc_type = CONV_NONE;
12410 convert_setup(&vimconv, from, to);
12411
12412 /* If the encodings are equal, no conversion needed. */
12413 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012414 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012416 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417
12418 convert_setup(&vimconv, NULL, NULL);
12419 vim_free(from);
12420 vim_free(to);
12421#endif
12422}
12423
12424/*
12425 * "indent()" function
12426 */
12427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012429 typval_T *argvars;
12430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431{
12432 linenr_T lnum;
12433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012434 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012436 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012438 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439}
12440
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012441/*
12442 * "index()" function
12443 */
12444 static void
12445f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012446 typval_T *argvars;
12447 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012448{
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 list_T *l;
12450 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012451 long idx = 0;
12452 int ic = FALSE;
12453
12454 rettv->vval.v_number = -1;
12455 if (argvars[0].v_type != VAR_LIST)
12456 {
12457 EMSG(_(e_listreq));
12458 return;
12459 }
12460 l = argvars[0].vval.v_list;
12461 if (l != NULL)
12462 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012463 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012464 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012466 int error = FALSE;
12467
Bram Moolenaar758711c2005-02-02 23:11:38 +000012468 /* Start at specified item. Use the cached index that list_find()
12469 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012470 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012471 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012472 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012473 ic = get_tv_number_chk(&argvars[3], &error);
12474 if (error)
12475 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012476 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012477
Bram Moolenaar758711c2005-02-02 23:11:38 +000012478 for ( ; item != NULL; item = item->li_next, ++idx)
12479 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012480 {
12481 rettv->vval.v_number = idx;
12482 break;
12483 }
12484 }
12485}
12486
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487static int inputsecret_flag = 0;
12488
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012489static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12490
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012492 * This function is used by f_input() and f_inputdialog() functions. The third
12493 * argument to f_input() specifies the type of completion to use at the
12494 * prompt. The third argument to f_inputdialog() specifies the value to return
12495 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496 */
12497 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012498get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012499 typval_T *argvars;
12500 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012501 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012503 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504 char_u *p = NULL;
12505 int c;
12506 char_u buf[NUMBUFLEN];
12507 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012508 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012509 int xp_type = EXPAND_NOTHING;
12510 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012513 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514
12515#ifdef NO_CONSOLE_INPUT
12516 /* While starting up, there is no place to enter text. */
12517 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519#endif
12520
12521 cmd_silent = FALSE; /* Want to see the prompt. */
12522 if (prompt != NULL)
12523 {
12524 /* Only the part of the message after the last NL is considered as
12525 * prompt for the command line */
12526 p = vim_strrchr(prompt, '\n');
12527 if (p == NULL)
12528 p = prompt;
12529 else
12530 {
12531 ++p;
12532 c = *p;
12533 *p = NUL;
12534 msg_start();
12535 msg_clr_eos();
12536 msg_puts_attr(prompt, echo_attr);
12537 msg_didout = FALSE;
12538 msg_starthere();
12539 *p = c;
12540 }
12541 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012543 if (argvars[1].v_type != VAR_UNKNOWN)
12544 {
12545 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12546 if (defstr != NULL)
12547 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012549 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012550 {
12551 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012552 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012553 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012554
Bram Moolenaar4463f292005-09-25 22:20:24 +000012555 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012556
Bram Moolenaar4463f292005-09-25 22:20:24 +000012557 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12558 if (xp_name == NULL)
12559 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012560
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012561 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012562
Bram Moolenaar4463f292005-09-25 22:20:24 +000012563 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12564 &xp_arg) == FAIL)
12565 return;
12566 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012567 }
12568
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012569 if (defstr != NULL)
12570 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012571 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12572 xp_type, xp_arg);
12573
12574 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012576 /* since the user typed this, no need to wait for return */
12577 need_wait_return = FALSE;
12578 msg_didout = FALSE;
12579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580 cmd_silent = cmd_silent_save;
12581}
12582
12583/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012584 * "input()" function
12585 * Also handles inputsecret() when inputsecret is set.
12586 */
12587 static void
12588f_input(argvars, rettv)
12589 typval_T *argvars;
12590 typval_T *rettv;
12591{
12592 get_user_input(argvars, rettv, FALSE);
12593}
12594
12595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 * "inputdialog()" function
12597 */
12598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012600 typval_T *argvars;
12601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602{
12603#if defined(FEAT_GUI_TEXTDIALOG)
12604 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12605 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12606 {
12607 char_u *message;
12608 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012611 message = get_tv_string_chk(&argvars[0]);
12612 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012613 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012614 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 else
12616 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012617 if (message != NULL && defstr != NULL
12618 && do_dialog(VIM_QUESTION, NULL, message,
12619 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012620 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 else
12622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 if (message != NULL && defstr != NULL
12624 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012625 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012626 rettv->vval.v_string = vim_strsave(
12627 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012629 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012631 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632 }
12633 else
12634#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012635 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636}
12637
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012638/*
12639 * "inputlist()" function
12640 */
12641 static void
12642f_inputlist(argvars, rettv)
12643 typval_T *argvars;
12644 typval_T *rettv;
12645{
12646 listitem_T *li;
12647 int selected;
12648 int mouse_used;
12649
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012650#ifdef NO_CONSOLE_INPUT
12651 /* While starting up, there is no place to enter text. */
12652 if (no_console_input())
12653 return;
12654#endif
12655 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12656 {
12657 EMSG2(_(e_listarg), "inputlist()");
12658 return;
12659 }
12660
12661 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012662 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012663 lines_left = Rows; /* avoid more prompt */
12664 msg_scroll = TRUE;
12665 msg_clr_eos();
12666
12667 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12668 {
12669 msg_puts(get_tv_string(&li->li_tv));
12670 msg_putchar('\n');
12671 }
12672
12673 /* Ask for choice. */
12674 selected = prompt_for_number(&mouse_used);
12675 if (mouse_used)
12676 selected -= lines_left;
12677
12678 rettv->vval.v_number = selected;
12679}
12680
12681
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12683
12684/*
12685 * "inputrestore()" function
12686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691{
12692 if (ga_userinput.ga_len > 0)
12693 {
12694 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12696 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012697 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 }
12699 else if (p_verbose > 1)
12700 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012701 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012702 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703 }
12704}
12705
12706/*
12707 * "inputsave()" function
12708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012711 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012714 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 if (ga_grow(&ga_userinput, 1) == OK)
12716 {
12717 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12718 + ga_userinput.ga_len);
12719 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012720 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721 }
12722 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724}
12725
12726/*
12727 * "inputsecret()" function
12728 */
12729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012731 typval_T *argvars;
12732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733{
12734 ++cmdline_star;
12735 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 --cmdline_star;
12738 --inputsecret_flag;
12739}
12740
12741/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012742 * "insert()" function
12743 */
12744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012746 typval_T *argvars;
12747 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012748{
12749 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012750 listitem_T *item;
12751 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012753
12754 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012755 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012756 else if ((l = argvars[0].vval.v_list) != NULL
12757 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012758 {
12759 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 before = get_tv_number_chk(&argvars[2], &error);
12761 if (error)
12762 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012763
Bram Moolenaar758711c2005-02-02 23:11:38 +000012764 if (before == l->lv_len)
12765 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012766 else
12767 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012768 item = list_find(l, before);
12769 if (item == NULL)
12770 {
12771 EMSGN(_(e_listidx), before);
12772 l = NULL;
12773 }
12774 }
12775 if (l != NULL)
12776 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012777 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012778 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012779 }
12780 }
12781}
12782
12783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 * "isdirectory()" function
12785 */
12786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 typval_T *argvars;
12789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792}
12793
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012794/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012795 * "islocked()" function
12796 */
12797 static void
12798f_islocked(argvars, rettv)
12799 typval_T *argvars;
12800 typval_T *rettv;
12801{
12802 lval_T lv;
12803 char_u *end;
12804 dictitem_T *di;
12805
12806 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012807 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12808 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012809 if (end != NULL && lv.ll_name != NULL)
12810 {
12811 if (*end != NUL)
12812 EMSG(_(e_trailing));
12813 else
12814 {
12815 if (lv.ll_tv == NULL)
12816 {
12817 if (check_changedtick(lv.ll_name))
12818 rettv->vval.v_number = 1; /* always locked */
12819 else
12820 {
12821 di = find_var(lv.ll_name, NULL);
12822 if (di != NULL)
12823 {
12824 /* Consider a variable locked when:
12825 * 1. the variable itself is locked
12826 * 2. the value of the variable is locked.
12827 * 3. the List or Dict value is locked.
12828 */
12829 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12830 || tv_islocked(&di->di_tv));
12831 }
12832 }
12833 }
12834 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012835 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012836 else if (lv.ll_newkey != NULL)
12837 EMSG2(_(e_dictkey), lv.ll_newkey);
12838 else if (lv.ll_list != NULL)
12839 /* List item. */
12840 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12841 else
12842 /* Dictionary item. */
12843 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12844 }
12845 }
12846
12847 clear_lval(&lv);
12848}
12849
Bram Moolenaar33570922005-01-25 22:26:29 +000012850static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012851
12852/*
12853 * Turn a dict into a list:
12854 * "what" == 0: list of keys
12855 * "what" == 1: list of values
12856 * "what" == 2: list of items
12857 */
12858 static void
12859dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012860 typval_T *argvars;
12861 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012862 int what;
12863{
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 list_T *l2;
12865 dictitem_T *di;
12866 hashitem_T *hi;
12867 listitem_T *li;
12868 listitem_T *li2;
12869 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012870 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012871
Bram Moolenaar8c711452005-01-14 21:53:12 +000012872 if (argvars[0].v_type != VAR_DICT)
12873 {
12874 EMSG(_(e_dictreq));
12875 return;
12876 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012877 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012878 return;
12879
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012880 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012881 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012882
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012883 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012884 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012885 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012886 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012887 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012888 --todo;
12889 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012890
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012891 li = listitem_alloc();
12892 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012893 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012894 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012895
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012896 if (what == 0)
12897 {
12898 /* keys() */
12899 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012900 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012901 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12902 }
12903 else if (what == 1)
12904 {
12905 /* values() */
12906 copy_tv(&di->di_tv, &li->li_tv);
12907 }
12908 else
12909 {
12910 /* items() */
12911 l2 = list_alloc();
12912 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012913 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012914 li->li_tv.vval.v_list = l2;
12915 if (l2 == NULL)
12916 break;
12917 ++l2->lv_refcount;
12918
12919 li2 = listitem_alloc();
12920 if (li2 == NULL)
12921 break;
12922 list_append(l2, li2);
12923 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012924 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012925 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12926
12927 li2 = listitem_alloc();
12928 if (li2 == NULL)
12929 break;
12930 list_append(l2, li2);
12931 copy_tv(&di->di_tv, &li2->li_tv);
12932 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012933 }
12934 }
12935}
12936
12937/*
12938 * "items(dict)" function
12939 */
12940 static void
12941f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012942 typval_T *argvars;
12943 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012944{
12945 dict_list(argvars, rettv, 2);
12946}
12947
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012949 * "join()" function
12950 */
12951 static void
12952f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012953 typval_T *argvars;
12954 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012955{
12956 garray_T ga;
12957 char_u *sep;
12958
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012959 if (argvars[0].v_type != VAR_LIST)
12960 {
12961 EMSG(_(e_listreq));
12962 return;
12963 }
12964 if (argvars[0].vval.v_list == NULL)
12965 return;
12966 if (argvars[1].v_type == VAR_UNKNOWN)
12967 sep = (char_u *)" ";
12968 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012969 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012970
12971 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012972
12973 if (sep != NULL)
12974 {
12975 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012976 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012977 ga_append(&ga, NUL);
12978 rettv->vval.v_string = (char_u *)ga.ga_data;
12979 }
12980 else
12981 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012982}
12983
12984/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012985 * "keys()" function
12986 */
12987 static void
12988f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012989 typval_T *argvars;
12990 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012991{
12992 dict_list(argvars, rettv, 0);
12993}
12994
12995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996 * "last_buffer_nr()" function.
12997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013000 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002{
13003 int n = 0;
13004 buf_T *buf;
13005
13006 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13007 if (n < buf->b_fnum)
13008 n = buf->b_fnum;
13009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013011}
13012
13013/*
13014 * "len()" function
13015 */
13016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013017f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013018 typval_T *argvars;
13019 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013020{
13021 switch (argvars[0].v_type)
13022 {
13023 case VAR_STRING:
13024 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013025 rettv->vval.v_number = (varnumber_T)STRLEN(
13026 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013027 break;
13028 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013029 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013030 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013031 case VAR_DICT:
13032 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13033 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013034 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013035 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013036 break;
13037 }
13038}
13039
Bram Moolenaar33570922005-01-25 22:26:29 +000013040static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013041
13042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013044 typval_T *argvars;
13045 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013046 int type;
13047{
13048#ifdef FEAT_LIBCALL
13049 char_u *string_in;
13050 char_u **string_result;
13051 int nr_result;
13052#endif
13053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013055 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013057
13058 if (check_restricted() || check_secure())
13059 return;
13060
13061#ifdef FEAT_LIBCALL
13062 /* The first two args must be strings, otherwise its meaningless */
13063 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13064 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013065 string_in = NULL;
13066 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013067 string_in = argvars[2].vval.v_string;
13068 if (type == VAR_NUMBER)
13069 string_result = NULL;
13070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013072 if (mch_libcall(argvars[0].vval.v_string,
13073 argvars[1].vval.v_string,
13074 string_in,
13075 argvars[2].vval.v_number,
13076 string_result,
13077 &nr_result) == OK
13078 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013080 }
13081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082}
13083
13084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013085 * "libcall()" function
13086 */
13087 static void
13088f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013089 typval_T *argvars;
13090 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013091{
13092 libcall_common(argvars, rettv, VAR_STRING);
13093}
13094
13095/*
13096 * "libcallnr()" function
13097 */
13098 static void
13099f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013100 typval_T *argvars;
13101 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013102{
13103 libcall_common(argvars, rettv, VAR_NUMBER);
13104}
13105
13106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 * "line(string)" function
13108 */
13109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013110f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013111 typval_T *argvars;
13112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113{
13114 linenr_T lnum = 0;
13115 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013116 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013118 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 if (fp != NULL)
13120 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122}
13123
13124/*
13125 * "line2byte(lnum)" function
13126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013129 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131{
13132#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134#else
13135 linenr_T lnum;
13136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13142 if (rettv->vval.v_number >= 0)
13143 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144#endif
13145}
13146
13147/*
13148 * "lispindent(lnum)" function
13149 */
13150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013151f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013152 typval_T *argvars;
13153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154{
13155#ifdef FEAT_LISP
13156 pos_T pos;
13157 linenr_T lnum;
13158
13159 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13162 {
13163 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013164 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165 curwin->w_cursor = pos;
13166 }
13167 else
13168#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170}
13171
13172/*
13173 * "localtime()" function
13174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013177 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181}
13182
Bram Moolenaar33570922005-01-25 22:26:29 +000013183static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184
13185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 typval_T *argvars;
13188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 int exact;
13190{
13191 char_u *keys;
13192 char_u *which;
13193 char_u buf[NUMBUFLEN];
13194 char_u *keys_buf = NULL;
13195 char_u *rhs;
13196 int mode;
13197 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013198 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199
13200 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013201 rettv->v_type = VAR_STRING;
13202 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013204 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 if (*keys == NUL)
13206 return;
13207
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013208 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013210 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013211 if (argvars[2].v_type != VAR_UNKNOWN)
13212 abbr = get_tv_number(&argvars[2]);
13213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 else
13215 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 if (which == NULL)
13217 return;
13218
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 mode = get_map_mode(&which, 0);
13220
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013221 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013222 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223 vim_free(keys_buf);
13224 if (rhs != NULL)
13225 {
13226 ga_init(&ga);
13227 ga.ga_itemsize = 1;
13228 ga.ga_growsize = 40;
13229
13230 while (*rhs != NUL)
13231 ga_concat(&ga, str2special(&rhs, FALSE));
13232
13233 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 }
13236}
13237
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013238#ifdef FEAT_FLOAT
13239/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013240 * "log()" function
13241 */
13242 static void
13243f_log(argvars, rettv)
13244 typval_T *argvars;
13245 typval_T *rettv;
13246{
13247 float_T f;
13248
13249 rettv->v_type = VAR_FLOAT;
13250 if (get_float_arg(argvars, &f) == OK)
13251 rettv->vval.v_float = log(f);
13252 else
13253 rettv->vval.v_float = 0.0;
13254}
13255
13256/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013257 * "log10()" function
13258 */
13259 static void
13260f_log10(argvars, rettv)
13261 typval_T *argvars;
13262 typval_T *rettv;
13263{
13264 float_T f;
13265
13266 rettv->v_type = VAR_FLOAT;
13267 if (get_float_arg(argvars, &f) == OK)
13268 rettv->vval.v_float = log10(f);
13269 else
13270 rettv->vval.v_float = 0.0;
13271}
13272#endif
13273
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013275 * "map()" function
13276 */
13277 static void
13278f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013279 typval_T *argvars;
13280 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013281{
13282 filter_map(argvars, rettv, TRUE);
13283}
13284
13285/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013286 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 */
13288 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013289f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013290 typval_T *argvars;
13291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013293 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294}
13295
13296/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013297 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 */
13299 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013300f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013301 typval_T *argvars;
13302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013304 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305}
13306
Bram Moolenaar33570922005-01-25 22:26:29 +000013307static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308
13309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013311 typval_T *argvars;
13312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313 int type;
13314{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013315 char_u *str = NULL;
13316 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 char_u *pat;
13318 regmatch_T regmatch;
13319 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013320 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 char_u *save_cpo;
13322 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013323 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013324 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013325 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013326 list_T *l = NULL;
13327 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013328 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013329 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330
13331 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13332 save_cpo = p_cpo;
13333 p_cpo = (char_u *)"";
13334
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013335 rettv->vval.v_number = -1;
13336 if (type == 3)
13337 {
13338 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013339 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013340 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013341 }
13342 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344 rettv->v_type = VAR_STRING;
13345 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013348 if (argvars[0].v_type == VAR_LIST)
13349 {
13350 if ((l = argvars[0].vval.v_list) == NULL)
13351 goto theend;
13352 li = l->lv_first;
13353 }
13354 else
13355 expr = str = get_tv_string(&argvars[0]);
13356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013357 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13358 if (pat == NULL)
13359 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013360
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013361 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013363 int error = FALSE;
13364
13365 start = get_tv_number_chk(&argvars[2], &error);
13366 if (error)
13367 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013368 if (l != NULL)
13369 {
13370 li = list_find(l, start);
13371 if (li == NULL)
13372 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013373 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013374 }
13375 else
13376 {
13377 if (start < 0)
13378 start = 0;
13379 if (start > (long)STRLEN(str))
13380 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013381 /* When "count" argument is there ignore matches before "start",
13382 * otherwise skip part of the string. Differs when pattern is "^"
13383 * or "\<". */
13384 if (argvars[3].v_type != VAR_UNKNOWN)
13385 startcol = start;
13386 else
13387 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013388 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013389
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013390 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013391 nth = get_tv_number_chk(&argvars[3], &error);
13392 if (error)
13393 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 }
13395
13396 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13397 if (regmatch.regprog != NULL)
13398 {
13399 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013400
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013401 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013402 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013403 if (l != NULL)
13404 {
13405 if (li == NULL)
13406 {
13407 match = FALSE;
13408 break;
13409 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013410 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013411 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013412 if (str == NULL)
13413 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013414 }
13415
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013416 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013417
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013418 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013419 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013420 if (l == NULL && !match)
13421 break;
13422
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013423 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013424 if (l != NULL)
13425 {
13426 li = li->li_next;
13427 ++idx;
13428 }
13429 else
13430 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013431#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013432 startcol = (colnr_T)(regmatch.startp[0]
13433 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013434#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013435 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013436#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013437 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013438 }
13439
13440 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013442 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013443 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013444 int i;
13445
13446 /* return list with matched string and submatches */
13447 for (i = 0; i < NSUBEXP; ++i)
13448 {
13449 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013450 {
13451 if (list_append_string(rettv->vval.v_list,
13452 (char_u *)"", 0) == FAIL)
13453 break;
13454 }
13455 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013456 regmatch.startp[i],
13457 (int)(regmatch.endp[i] - regmatch.startp[i]))
13458 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013459 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013460 }
13461 }
13462 else if (type == 2)
13463 {
13464 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013465 if (l != NULL)
13466 copy_tv(&li->li_tv, rettv);
13467 else
13468 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013470 }
13471 else if (l != NULL)
13472 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473 else
13474 {
13475 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477 (varnumber_T)(regmatch.startp[0] - str);
13478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013481 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 }
13483 }
13484 vim_free(regmatch.regprog);
13485 }
13486
13487theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013488 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 p_cpo = save_cpo;
13490}
13491
13492/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013493 * "match()" function
13494 */
13495 static void
13496f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013497 typval_T *argvars;
13498 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013499{
13500 find_some_match(argvars, rettv, 1);
13501}
13502
13503/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013504 * "matchadd()" function
13505 */
13506 static void
13507f_matchadd(argvars, rettv)
13508 typval_T *argvars;
13509 typval_T *rettv;
13510{
13511#ifdef FEAT_SEARCH_EXTRA
13512 char_u buf[NUMBUFLEN];
13513 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13514 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13515 int prio = 10; /* default priority */
13516 int id = -1;
13517 int error = FALSE;
13518
13519 rettv->vval.v_number = -1;
13520
13521 if (grp == NULL || pat == NULL)
13522 return;
13523 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013524 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013525 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013526 if (argvars[3].v_type != VAR_UNKNOWN)
13527 id = get_tv_number_chk(&argvars[3], &error);
13528 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013529 if (error == TRUE)
13530 return;
13531 if (id >= 1 && id <= 3)
13532 {
13533 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13534 return;
13535 }
13536
13537 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13538#endif
13539}
13540
13541/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013542 * "matcharg()" function
13543 */
13544 static void
13545f_matcharg(argvars, rettv)
13546 typval_T *argvars;
13547 typval_T *rettv;
13548{
13549 if (rettv_list_alloc(rettv) == OK)
13550 {
13551#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013552 int id = get_tv_number(&argvars[0]);
13553 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013554
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013555 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013556 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013557 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13558 {
13559 list_append_string(rettv->vval.v_list,
13560 syn_id2name(m->hlg_id), -1);
13561 list_append_string(rettv->vval.v_list, m->pattern, -1);
13562 }
13563 else
13564 {
13565 list_append_string(rettv->vval.v_list, NUL, -1);
13566 list_append_string(rettv->vval.v_list, NUL, -1);
13567 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013568 }
13569#endif
13570 }
13571}
13572
13573/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013574 * "matchdelete()" function
13575 */
13576 static void
13577f_matchdelete(argvars, rettv)
13578 typval_T *argvars;
13579 typval_T *rettv;
13580{
13581#ifdef FEAT_SEARCH_EXTRA
13582 rettv->vval.v_number = match_delete(curwin,
13583 (int)get_tv_number(&argvars[0]), TRUE);
13584#endif
13585}
13586
13587/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013588 * "matchend()" function
13589 */
13590 static void
13591f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013592 typval_T *argvars;
13593 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013594{
13595 find_some_match(argvars, rettv, 0);
13596}
13597
13598/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013599 * "matchlist()" function
13600 */
13601 static void
13602f_matchlist(argvars, rettv)
13603 typval_T *argvars;
13604 typval_T *rettv;
13605{
13606 find_some_match(argvars, rettv, 3);
13607}
13608
13609/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013610 * "matchstr()" function
13611 */
13612 static void
13613f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013614 typval_T *argvars;
13615 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013616{
13617 find_some_match(argvars, rettv, 2);
13618}
13619
Bram Moolenaar33570922005-01-25 22:26:29 +000013620static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013621
13622 static void
13623max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013624 typval_T *argvars;
13625 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013626 int domax;
13627{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013628 long n = 0;
13629 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013631
13632 if (argvars[0].v_type == VAR_LIST)
13633 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013634 list_T *l;
13635 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013636
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013637 l = argvars[0].vval.v_list;
13638 if (l != NULL)
13639 {
13640 li = l->lv_first;
13641 if (li != NULL)
13642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013643 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013644 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013645 {
13646 li = li->li_next;
13647 if (li == NULL)
13648 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013649 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013650 if (domax ? i > n : i < n)
13651 n = i;
13652 }
13653 }
13654 }
13655 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013656 else if (argvars[0].v_type == VAR_DICT)
13657 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013658 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013659 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013660 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013661 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013662
13663 d = argvars[0].vval.v_dict;
13664 if (d != NULL)
13665 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013666 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013667 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013669 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013670 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013671 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013672 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013673 if (first)
13674 {
13675 n = i;
13676 first = FALSE;
13677 }
13678 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013679 n = i;
13680 }
13681 }
13682 }
13683 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013684 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013685 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013686 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013687}
13688
13689/*
13690 * "max()" function
13691 */
13692 static void
13693f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013694 typval_T *argvars;
13695 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013696{
13697 max_min(argvars, rettv, TRUE);
13698}
13699
13700/*
13701 * "min()" function
13702 */
13703 static void
13704f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013705 typval_T *argvars;
13706 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013707{
13708 max_min(argvars, rettv, FALSE);
13709}
13710
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013711static int mkdir_recurse __ARGS((char_u *dir, int prot));
13712
13713/*
13714 * Create the directory in which "dir" is located, and higher levels when
13715 * needed.
13716 */
13717 static int
13718mkdir_recurse(dir, prot)
13719 char_u *dir;
13720 int prot;
13721{
13722 char_u *p;
13723 char_u *updir;
13724 int r = FAIL;
13725
13726 /* Get end of directory name in "dir".
13727 * We're done when it's "/" or "c:/". */
13728 p = gettail_sep(dir);
13729 if (p <= get_past_head(dir))
13730 return OK;
13731
13732 /* If the directory exists we're done. Otherwise: create it.*/
13733 updir = vim_strnsave(dir, (int)(p - dir));
13734 if (updir == NULL)
13735 return FAIL;
13736 if (mch_isdir(updir))
13737 r = OK;
13738 else if (mkdir_recurse(updir, prot) == OK)
13739 r = vim_mkdir_emsg(updir, prot);
13740 vim_free(updir);
13741 return r;
13742}
13743
13744#ifdef vim_mkdir
13745/*
13746 * "mkdir()" function
13747 */
13748 static void
13749f_mkdir(argvars, rettv)
13750 typval_T *argvars;
13751 typval_T *rettv;
13752{
13753 char_u *dir;
13754 char_u buf[NUMBUFLEN];
13755 int prot = 0755;
13756
13757 rettv->vval.v_number = FAIL;
13758 if (check_restricted() || check_secure())
13759 return;
13760
13761 dir = get_tv_string_buf(&argvars[0], buf);
13762 if (argvars[1].v_type != VAR_UNKNOWN)
13763 {
13764 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 prot = get_tv_number_chk(&argvars[2], NULL);
13766 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013767 mkdir_recurse(dir, prot);
13768 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013770}
13771#endif
13772
Bram Moolenaar0d660222005-01-07 21:51:51 +000013773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 * "mode()" function
13775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013777f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013778 typval_T *argvars;
13779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013781 char_u buf[3];
13782
13783 buf[1] = NUL;
13784 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785
13786#ifdef FEAT_VISUAL
13787 if (VIsual_active)
13788 {
13789 if (VIsual_select)
13790 buf[0] = VIsual_mode + 's' - 'v';
13791 else
13792 buf[0] = VIsual_mode;
13793 }
13794 else
13795#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013796 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13797 || State == CONFIRM)
13798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013800 if (State == ASKMORE)
13801 buf[1] = 'm';
13802 else if (State == CONFIRM)
13803 buf[1] = '?';
13804 }
13805 else if (State == EXTERNCMD)
13806 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 else if (State & INSERT)
13808 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013809#ifdef FEAT_VREPLACE
13810 if (State & VREPLACE_FLAG)
13811 {
13812 buf[0] = 'R';
13813 buf[1] = 'v';
13814 }
13815 else
13816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 if (State & REPLACE_FLAG)
13818 buf[0] = 'R';
13819 else
13820 buf[0] = 'i';
13821 }
13822 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013825 if (exmode_active)
13826 buf[1] = 'v';
13827 }
13828 else if (exmode_active)
13829 {
13830 buf[0] = 'c';
13831 buf[1] = 'e';
13832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013836 if (finish_op)
13837 buf[1] = 'o';
13838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013840 /* Clear out the minor mode when the argument is not a non-zero number or
13841 * non-empty string. */
13842 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013843 buf[1] = NUL;
13844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013845 rettv->vval.v_string = vim_strsave(buf);
13846 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847}
13848
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013849#ifdef FEAT_MZSCHEME
13850/*
13851 * "mzeval()" function
13852 */
13853 static void
13854f_mzeval(argvars, rettv)
13855 typval_T *argvars;
13856 typval_T *rettv;
13857{
13858 char_u *str;
13859 char_u buf[NUMBUFLEN];
13860
13861 str = get_tv_string_buf(&argvars[0], buf);
13862 do_mzeval(str, rettv);
13863}
13864#endif
13865
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013867 * "nextnonblank()" function
13868 */
13869 static void
13870f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013871 typval_T *argvars;
13872 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013873{
13874 linenr_T lnum;
13875
13876 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013878 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013879 {
13880 lnum = 0;
13881 break;
13882 }
13883 if (*skipwhite(ml_get(lnum)) != NUL)
13884 break;
13885 }
13886 rettv->vval.v_number = lnum;
13887}
13888
13889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 * "nr2char()" function
13891 */
13892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013893f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013894 typval_T *argvars;
13895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896{
13897 char_u buf[NUMBUFLEN];
13898
13899#ifdef FEAT_MBYTE
13900 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013901 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 else
13903#endif
13904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013905 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 buf[1] = NUL;
13907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 rettv->v_type = VAR_STRING;
13909 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013910}
13911
13912/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013913 * "pathshorten()" function
13914 */
13915 static void
13916f_pathshorten(argvars, rettv)
13917 typval_T *argvars;
13918 typval_T *rettv;
13919{
13920 char_u *p;
13921
13922 rettv->v_type = VAR_STRING;
13923 p = get_tv_string_chk(&argvars[0]);
13924 if (p == NULL)
13925 rettv->vval.v_string = NULL;
13926 else
13927 {
13928 p = vim_strsave(p);
13929 rettv->vval.v_string = p;
13930 if (p != NULL)
13931 shorten_dir(p);
13932 }
13933}
13934
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013935#ifdef FEAT_FLOAT
13936/*
13937 * "pow()" function
13938 */
13939 static void
13940f_pow(argvars, rettv)
13941 typval_T *argvars;
13942 typval_T *rettv;
13943{
13944 float_T fx, fy;
13945
13946 rettv->v_type = VAR_FLOAT;
13947 if (get_float_arg(argvars, &fx) == OK
13948 && get_float_arg(&argvars[1], &fy) == OK)
13949 rettv->vval.v_float = pow(fx, fy);
13950 else
13951 rettv->vval.v_float = 0.0;
13952}
13953#endif
13954
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013955/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956 * "prevnonblank()" function
13957 */
13958 static void
13959f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013960 typval_T *argvars;
13961 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013962{
13963 linenr_T lnum;
13964
13965 lnum = get_tv_lnum(argvars);
13966 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13967 lnum = 0;
13968 else
13969 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13970 --lnum;
13971 rettv->vval.v_number = lnum;
13972}
13973
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013974#ifdef HAVE_STDARG_H
13975/* This dummy va_list is here because:
13976 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13977 * - locally in the function results in a "used before set" warning
13978 * - using va_start() to initialize it gives "function with fixed args" error */
13979static va_list ap;
13980#endif
13981
Bram Moolenaar8c711452005-01-14 21:53:12 +000013982/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013983 * "printf()" function
13984 */
13985 static void
13986f_printf(argvars, rettv)
13987 typval_T *argvars;
13988 typval_T *rettv;
13989{
13990 rettv->v_type = VAR_STRING;
13991 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013992#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013993 {
13994 char_u buf[NUMBUFLEN];
13995 int len;
13996 char_u *s;
13997 int saved_did_emsg = did_emsg;
13998 char *fmt;
13999
14000 /* Get the required length, allocate the buffer and do it for real. */
14001 did_emsg = FALSE;
14002 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014003 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014004 if (!did_emsg)
14005 {
14006 s = alloc(len + 1);
14007 if (s != NULL)
14008 {
14009 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014010 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014011 }
14012 }
14013 did_emsg |= saved_did_emsg;
14014 }
14015#endif
14016}
14017
14018/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014019 * "pumvisible()" function
14020 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014021 static void
14022f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014023 typval_T *argvars UNUSED;
14024 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014025{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014026#ifdef FEAT_INS_EXPAND
14027 if (pum_visible())
14028 rettv->vval.v_number = 1;
14029#endif
14030}
14031
14032/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014033 * "range()" function
14034 */
14035 static void
14036f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014037 typval_T *argvars;
14038 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014039{
14040 long start;
14041 long end;
14042 long stride = 1;
14043 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014044 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014046 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014047 if (argvars[1].v_type == VAR_UNKNOWN)
14048 {
14049 end = start - 1;
14050 start = 0;
14051 }
14052 else
14053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014054 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014055 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014056 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014057 }
14058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014059 if (error)
14060 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014061 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014062 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014063 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014064 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014065 else
14066 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014067 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014068 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014069 if (list_append_number(rettv->vval.v_list,
14070 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014071 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014072 }
14073}
14074
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014075/*
14076 * "readfile()" function
14077 */
14078 static void
14079f_readfile(argvars, rettv)
14080 typval_T *argvars;
14081 typval_T *rettv;
14082{
14083 int binary = FALSE;
14084 char_u *fname;
14085 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014086 listitem_T *li;
14087#define FREAD_SIZE 200 /* optimized for text lines */
14088 char_u buf[FREAD_SIZE];
14089 int readlen; /* size of last fread() */
14090 int buflen; /* nr of valid chars in buf[] */
14091 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14092 int tolist; /* first byte in buf[] still to be put in list */
14093 int chop; /* how many CR to chop off */
14094 char_u *prev = NULL; /* previously read bytes, if any */
14095 int prevlen = 0; /* length of "prev" if not NULL */
14096 char_u *s;
14097 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014098 long maxline = MAXLNUM;
14099 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014100
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014101 if (argvars[1].v_type != VAR_UNKNOWN)
14102 {
14103 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14104 binary = TRUE;
14105 if (argvars[2].v_type != VAR_UNKNOWN)
14106 maxline = get_tv_number(&argvars[2]);
14107 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014108
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014109 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014110 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014111
14112 /* Always open the file in binary mode, library functions have a mind of
14113 * their own about CR-LF conversion. */
14114 fname = get_tv_string(&argvars[0]);
14115 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14116 {
14117 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14118 return;
14119 }
14120
14121 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014122 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014123 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014124 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014125 buflen = filtd + readlen;
14126 tolist = 0;
14127 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14128 {
14129 if (buf[filtd] == '\n' || readlen <= 0)
14130 {
14131 /* Only when in binary mode add an empty list item when the
14132 * last line ends in a '\n'. */
14133 if (!binary && readlen == 0 && filtd == 0)
14134 break;
14135
14136 /* Found end-of-line or end-of-file: add a text line to the
14137 * list. */
14138 chop = 0;
14139 if (!binary)
14140 while (filtd - chop - 1 >= tolist
14141 && buf[filtd - chop - 1] == '\r')
14142 ++chop;
14143 len = filtd - tolist - chop;
14144 if (prev == NULL)
14145 s = vim_strnsave(buf + tolist, len);
14146 else
14147 {
14148 s = alloc((unsigned)(prevlen + len + 1));
14149 if (s != NULL)
14150 {
14151 mch_memmove(s, prev, prevlen);
14152 vim_free(prev);
14153 prev = NULL;
14154 mch_memmove(s + prevlen, buf + tolist, len);
14155 s[prevlen + len] = NUL;
14156 }
14157 }
14158 tolist = filtd + 1;
14159
14160 li = listitem_alloc();
14161 if (li == NULL)
14162 {
14163 vim_free(s);
14164 break;
14165 }
14166 li->li_tv.v_type = VAR_STRING;
14167 li->li_tv.v_lock = 0;
14168 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014169 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014170
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014171 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014172 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014173 if (readlen <= 0)
14174 break;
14175 }
14176 else if (buf[filtd] == NUL)
14177 buf[filtd] = '\n';
14178 }
14179 if (readlen <= 0)
14180 break;
14181
14182 if (tolist == 0)
14183 {
14184 /* "buf" is full, need to move text to an allocated buffer */
14185 if (prev == NULL)
14186 {
14187 prev = vim_strnsave(buf, buflen);
14188 prevlen = buflen;
14189 }
14190 else
14191 {
14192 s = alloc((unsigned)(prevlen + buflen));
14193 if (s != NULL)
14194 {
14195 mch_memmove(s, prev, prevlen);
14196 mch_memmove(s + prevlen, buf, buflen);
14197 vim_free(prev);
14198 prev = s;
14199 prevlen += buflen;
14200 }
14201 }
14202 filtd = 0;
14203 }
14204 else
14205 {
14206 mch_memmove(buf, buf + tolist, buflen - tolist);
14207 filtd -= tolist;
14208 }
14209 }
14210
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014211 /*
14212 * For a negative line count use only the lines at the end of the file,
14213 * free the rest.
14214 */
14215 if (maxline < 0)
14216 while (cnt > -maxline)
14217 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014218 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014219 --cnt;
14220 }
14221
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014222 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014223 fclose(fd);
14224}
14225
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014226#if defined(FEAT_RELTIME)
14227static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14228
14229/*
14230 * Convert a List to proftime_T.
14231 * Return FAIL when there is something wrong.
14232 */
14233 static int
14234list2proftime(arg, tm)
14235 typval_T *arg;
14236 proftime_T *tm;
14237{
14238 long n1, n2;
14239 int error = FALSE;
14240
14241 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14242 || arg->vval.v_list->lv_len != 2)
14243 return FAIL;
14244 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14245 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14246# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014247 tm->HighPart = n1;
14248 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014249# else
14250 tm->tv_sec = n1;
14251 tm->tv_usec = n2;
14252# endif
14253 return error ? FAIL : OK;
14254}
14255#endif /* FEAT_RELTIME */
14256
14257/*
14258 * "reltime()" function
14259 */
14260 static void
14261f_reltime(argvars, rettv)
14262 typval_T *argvars;
14263 typval_T *rettv;
14264{
14265#ifdef FEAT_RELTIME
14266 proftime_T res;
14267 proftime_T start;
14268
14269 if (argvars[0].v_type == VAR_UNKNOWN)
14270 {
14271 /* No arguments: get current time. */
14272 profile_start(&res);
14273 }
14274 else if (argvars[1].v_type == VAR_UNKNOWN)
14275 {
14276 if (list2proftime(&argvars[0], &res) == FAIL)
14277 return;
14278 profile_end(&res);
14279 }
14280 else
14281 {
14282 /* Two arguments: compute the difference. */
14283 if (list2proftime(&argvars[0], &start) == FAIL
14284 || list2proftime(&argvars[1], &res) == FAIL)
14285 return;
14286 profile_sub(&res, &start);
14287 }
14288
14289 if (rettv_list_alloc(rettv) == OK)
14290 {
14291 long n1, n2;
14292
14293# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014294 n1 = res.HighPart;
14295 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014296# else
14297 n1 = res.tv_sec;
14298 n2 = res.tv_usec;
14299# endif
14300 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14301 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14302 }
14303#endif
14304}
14305
14306/*
14307 * "reltimestr()" function
14308 */
14309 static void
14310f_reltimestr(argvars, rettv)
14311 typval_T *argvars;
14312 typval_T *rettv;
14313{
14314#ifdef FEAT_RELTIME
14315 proftime_T tm;
14316#endif
14317
14318 rettv->v_type = VAR_STRING;
14319 rettv->vval.v_string = NULL;
14320#ifdef FEAT_RELTIME
14321 if (list2proftime(&argvars[0], &tm) == OK)
14322 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14323#endif
14324}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014325
Bram Moolenaar0d660222005-01-07 21:51:51 +000014326#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14327static void make_connection __ARGS((void));
14328static int check_connection __ARGS((void));
14329
14330 static void
14331make_connection()
14332{
14333 if (X_DISPLAY == NULL
14334# ifdef FEAT_GUI
14335 && !gui.in_use
14336# endif
14337 )
14338 {
14339 x_force_connect = TRUE;
14340 setup_term_clip();
14341 x_force_connect = FALSE;
14342 }
14343}
14344
14345 static int
14346check_connection()
14347{
14348 make_connection();
14349 if (X_DISPLAY == NULL)
14350 {
14351 EMSG(_("E240: No connection to Vim server"));
14352 return FAIL;
14353 }
14354 return OK;
14355}
14356#endif
14357
14358#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014359static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014360
14361 static void
14362remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014363 typval_T *argvars;
14364 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014365 int expr;
14366{
14367 char_u *server_name;
14368 char_u *keys;
14369 char_u *r = NULL;
14370 char_u buf[NUMBUFLEN];
14371# ifdef WIN32
14372 HWND w;
14373# else
14374 Window w;
14375# endif
14376
14377 if (check_restricted() || check_secure())
14378 return;
14379
14380# ifdef FEAT_X11
14381 if (check_connection() == FAIL)
14382 return;
14383# endif
14384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 server_name = get_tv_string_chk(&argvars[0]);
14386 if (server_name == NULL)
14387 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014388 keys = get_tv_string_buf(&argvars[1], buf);
14389# ifdef WIN32
14390 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14391# else
14392 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14393 < 0)
14394# endif
14395 {
14396 if (r != NULL)
14397 EMSG(r); /* sending worked but evaluation failed */
14398 else
14399 EMSG2(_("E241: Unable to send to %s"), server_name);
14400 return;
14401 }
14402
14403 rettv->vval.v_string = r;
14404
14405 if (argvars[2].v_type != VAR_UNKNOWN)
14406 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014408 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014410
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014411 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014412 v.di_tv.v_type = VAR_STRING;
14413 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 idvar = get_tv_string_chk(&argvars[2]);
14415 if (idvar != NULL)
14416 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014417 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014418 }
14419}
14420#endif
14421
14422/*
14423 * "remote_expr()" function
14424 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425 static void
14426f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014427 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014429{
14430 rettv->v_type = VAR_STRING;
14431 rettv->vval.v_string = NULL;
14432#ifdef FEAT_CLIENTSERVER
14433 remote_common(argvars, rettv, TRUE);
14434#endif
14435}
14436
14437/*
14438 * "remote_foreground()" function
14439 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014440 static void
14441f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014442 typval_T *argvars UNUSED;
14443 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014444{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445#ifdef FEAT_CLIENTSERVER
14446# ifdef WIN32
14447 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014448 {
14449 char_u *server_name = get_tv_string_chk(&argvars[0]);
14450
14451 if (server_name != NULL)
14452 serverForeground(server_name);
14453 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014454# else
14455 /* Send a foreground() expression to the server. */
14456 argvars[1].v_type = VAR_STRING;
14457 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14458 argvars[2].v_type = VAR_UNKNOWN;
14459 remote_common(argvars, rettv, TRUE);
14460 vim_free(argvars[1].vval.v_string);
14461# endif
14462#endif
14463}
14464
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465 static void
14466f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014467 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014468 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014469{
14470#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014471 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014472 char_u *s = NULL;
14473# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014474 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014475# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014476 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477
14478 if (check_restricted() || check_secure())
14479 {
14480 rettv->vval.v_number = -1;
14481 return;
14482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014483 serverid = get_tv_string_chk(&argvars[0]);
14484 if (serverid == NULL)
14485 {
14486 rettv->vval.v_number = -1;
14487 return; /* type error; errmsg already given */
14488 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014489# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014490 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491 if (n == 0)
14492 rettv->vval.v_number = -1;
14493 else
14494 {
14495 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14496 rettv->vval.v_number = (s != NULL);
14497 }
14498# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014499 if (check_connection() == FAIL)
14500 return;
14501
14502 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014503 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014504# endif
14505
14506 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014508 char_u *retvar;
14509
Bram Moolenaar33570922005-01-25 22:26:29 +000014510 v.di_tv.v_type = VAR_STRING;
14511 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014512 retvar = get_tv_string_chk(&argvars[1]);
14513 if (retvar != NULL)
14514 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014515 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014516 }
14517#else
14518 rettv->vval.v_number = -1;
14519#endif
14520}
14521
Bram Moolenaar0d660222005-01-07 21:51:51 +000014522 static void
14523f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014524 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014525 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014526{
14527 char_u *r = NULL;
14528
14529#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 char_u *serverid = get_tv_string_chk(&argvars[0]);
14531
14532 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014533 {
14534# ifdef WIN32
14535 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014536 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014537
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014538 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014539 if (n != 0)
14540 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14541 if (r == NULL)
14542# else
14543 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014544 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014545# endif
14546 EMSG(_("E277: Unable to read a server reply"));
14547 }
14548#endif
14549 rettv->v_type = VAR_STRING;
14550 rettv->vval.v_string = r;
14551}
14552
14553/*
14554 * "remote_send()" function
14555 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556 static void
14557f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014558 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014559 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014560{
14561 rettv->v_type = VAR_STRING;
14562 rettv->vval.v_string = NULL;
14563#ifdef FEAT_CLIENTSERVER
14564 remote_common(argvars, rettv, FALSE);
14565#endif
14566}
14567
14568/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014569 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014570 */
14571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014573 typval_T *argvars;
14574 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014575{
Bram Moolenaar33570922005-01-25 22:26:29 +000014576 list_T *l;
14577 listitem_T *item, *item2;
14578 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014579 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014580 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014581 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014582 dict_T *d;
14583 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014584
Bram Moolenaar8c711452005-01-14 21:53:12 +000014585 if (argvars[0].v_type == VAR_DICT)
14586 {
14587 if (argvars[2].v_type != VAR_UNKNOWN)
14588 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014589 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014590 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014592 key = get_tv_string_chk(&argvars[1]);
14593 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014595 di = dict_find(d, key, -1);
14596 if (di == NULL)
14597 EMSG2(_(e_dictkey), key);
14598 else
14599 {
14600 *rettv = di->di_tv;
14601 init_tv(&di->di_tv);
14602 dictitem_remove(d, di);
14603 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014605 }
14606 }
14607 else if (argvars[0].v_type != VAR_LIST)
14608 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014609 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014610 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014612 int error = FALSE;
14613
14614 idx = get_tv_number_chk(&argvars[1], &error);
14615 if (error)
14616 ; /* type error: do nothing, errmsg already given */
14617 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014618 EMSGN(_(e_listidx), idx);
14619 else
14620 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014621 if (argvars[2].v_type == VAR_UNKNOWN)
14622 {
14623 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014624 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014625 *rettv = item->li_tv;
14626 vim_free(item);
14627 }
14628 else
14629 {
14630 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014631 end = get_tv_number_chk(&argvars[2], &error);
14632 if (error)
14633 ; /* type error: do nothing */
14634 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014635 EMSGN(_(e_listidx), end);
14636 else
14637 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014638 int cnt = 0;
14639
14640 for (li = item; li != NULL; li = li->li_next)
14641 {
14642 ++cnt;
14643 if (li == item2)
14644 break;
14645 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014646 if (li == NULL) /* didn't find "item2" after "item" */
14647 EMSG(_(e_invrange));
14648 else
14649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014650 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014651 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014652 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014653 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014654 l->lv_first = item;
14655 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014656 item->li_prev = NULL;
14657 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014658 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014659 }
14660 }
14661 }
14662 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014663 }
14664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665}
14666
14667/*
14668 * "rename({from}, {to})" function
14669 */
14670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014671f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014672 typval_T *argvars;
14673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674{
14675 char_u buf[NUMBUFLEN];
14676
14677 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014680 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14681 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682}
14683
14684/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014685 * "repeat()" function
14686 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014687 static void
14688f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014689 typval_T *argvars;
14690 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014691{
14692 char_u *p;
14693 int n;
14694 int slen;
14695 int len;
14696 char_u *r;
14697 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014698
14699 n = get_tv_number(&argvars[1]);
14700 if (argvars[0].v_type == VAR_LIST)
14701 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014702 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014703 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014704 if (list_extend(rettv->vval.v_list,
14705 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014706 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014707 }
14708 else
14709 {
14710 p = get_tv_string(&argvars[0]);
14711 rettv->v_type = VAR_STRING;
14712 rettv->vval.v_string = NULL;
14713
14714 slen = (int)STRLEN(p);
14715 len = slen * n;
14716 if (len <= 0)
14717 return;
14718
14719 r = alloc(len + 1);
14720 if (r != NULL)
14721 {
14722 for (i = 0; i < n; i++)
14723 mch_memmove(r + i * slen, p, (size_t)slen);
14724 r[len] = NUL;
14725 }
14726
14727 rettv->vval.v_string = r;
14728 }
14729}
14730
14731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732 * "resolve()" function
14733 */
14734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 typval_T *argvars;
14737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738{
14739 char_u *p;
14740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014741 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742#ifdef FEAT_SHORTCUT
14743 {
14744 char_u *v = NULL;
14745
14746 v = mch_resolve_shortcut(p);
14747 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014750 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 }
14752#else
14753# ifdef HAVE_READLINK
14754 {
14755 char_u buf[MAXPATHL + 1];
14756 char_u *cpy;
14757 int len;
14758 char_u *remain = NULL;
14759 char_u *q;
14760 int is_relative_to_current = FALSE;
14761 int has_trailing_pathsep = FALSE;
14762 int limit = 100;
14763
14764 p = vim_strsave(p);
14765
14766 if (p[0] == '.' && (vim_ispathsep(p[1])
14767 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14768 is_relative_to_current = TRUE;
14769
14770 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014771 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772 has_trailing_pathsep = TRUE;
14773
14774 q = getnextcomp(p);
14775 if (*q != NUL)
14776 {
14777 /* Separate the first path component in "p", and keep the
14778 * remainder (beginning with the path separator). */
14779 remain = vim_strsave(q - 1);
14780 q[-1] = NUL;
14781 }
14782
14783 for (;;)
14784 {
14785 for (;;)
14786 {
14787 len = readlink((char *)p, (char *)buf, MAXPATHL);
14788 if (len <= 0)
14789 break;
14790 buf[len] = NUL;
14791
14792 if (limit-- == 0)
14793 {
14794 vim_free(p);
14795 vim_free(remain);
14796 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014797 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 goto fail;
14799 }
14800
14801 /* Ensure that the result will have a trailing path separator
14802 * if the argument has one. */
14803 if (remain == NULL && has_trailing_pathsep)
14804 add_pathsep(buf);
14805
14806 /* Separate the first path component in the link value and
14807 * concatenate the remainders. */
14808 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14809 if (*q != NUL)
14810 {
14811 if (remain == NULL)
14812 remain = vim_strsave(q - 1);
14813 else
14814 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014815 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 if (cpy != NULL)
14817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818 vim_free(remain);
14819 remain = cpy;
14820 }
14821 }
14822 q[-1] = NUL;
14823 }
14824
14825 q = gettail(p);
14826 if (q > p && *q == NUL)
14827 {
14828 /* Ignore trailing path separator. */
14829 q[-1] = NUL;
14830 q = gettail(p);
14831 }
14832 if (q > p && !mch_isFullName(buf))
14833 {
14834 /* symlink is relative to directory of argument */
14835 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14836 if (cpy != NULL)
14837 {
14838 STRCPY(cpy, p);
14839 STRCPY(gettail(cpy), buf);
14840 vim_free(p);
14841 p = cpy;
14842 }
14843 }
14844 else
14845 {
14846 vim_free(p);
14847 p = vim_strsave(buf);
14848 }
14849 }
14850
14851 if (remain == NULL)
14852 break;
14853
14854 /* Append the first path component of "remain" to "p". */
14855 q = getnextcomp(remain + 1);
14856 len = q - remain - (*q != NUL);
14857 cpy = vim_strnsave(p, STRLEN(p) + len);
14858 if (cpy != NULL)
14859 {
14860 STRNCAT(cpy, remain, len);
14861 vim_free(p);
14862 p = cpy;
14863 }
14864 /* Shorten "remain". */
14865 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014866 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 else
14868 {
14869 vim_free(remain);
14870 remain = NULL;
14871 }
14872 }
14873
14874 /* If the result is a relative path name, make it explicitly relative to
14875 * the current directory if and only if the argument had this form. */
14876 if (!vim_ispathsep(*p))
14877 {
14878 if (is_relative_to_current
14879 && *p != NUL
14880 && !(p[0] == '.'
14881 && (p[1] == NUL
14882 || vim_ispathsep(p[1])
14883 || (p[1] == '.'
14884 && (p[2] == NUL
14885 || vim_ispathsep(p[2]))))))
14886 {
14887 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014888 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889 if (cpy != NULL)
14890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891 vim_free(p);
14892 p = cpy;
14893 }
14894 }
14895 else if (!is_relative_to_current)
14896 {
14897 /* Strip leading "./". */
14898 q = p;
14899 while (q[0] == '.' && vim_ispathsep(q[1]))
14900 q += 2;
14901 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014902 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903 }
14904 }
14905
14906 /* Ensure that the result will have no trailing path separator
14907 * if the argument had none. But keep "/" or "//". */
14908 if (!has_trailing_pathsep)
14909 {
14910 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014911 if (after_pathsep(p, q))
14912 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913 }
14914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916 }
14917# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014918 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919# endif
14920#endif
14921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923
14924#ifdef HAVE_READLINK
14925fail:
14926#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014927 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928}
14929
14930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 */
14933 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014934f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014935 typval_T *argvars;
14936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937{
Bram Moolenaar33570922005-01-25 22:26:29 +000014938 list_T *l;
14939 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940
Bram Moolenaar0d660222005-01-07 21:51:51 +000014941 if (argvars[0].v_type != VAR_LIST)
14942 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014943 else if ((l = argvars[0].vval.v_list) != NULL
14944 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014945 {
14946 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014947 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014948 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014949 while (li != NULL)
14950 {
14951 ni = li->li_prev;
14952 list_append(l, li);
14953 li = ni;
14954 }
14955 rettv->vval.v_list = l;
14956 rettv->v_type = VAR_LIST;
14957 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014958 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960}
14961
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014962#define SP_NOMOVE 0x01 /* don't move cursor */
14963#define SP_REPEAT 0x02 /* repeat to find outer pair */
14964#define SP_RETCOUNT 0x04 /* return matchcount */
14965#define SP_SETPCMARK 0x08 /* set previous context mark */
14966#define SP_START 0x10 /* accept match at start position */
14967#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14968#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014969
Bram Moolenaar33570922005-01-25 22:26:29 +000014970static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014971
14972/*
14973 * Get flags for a search function.
14974 * Possibly sets "p_ws".
14975 * Returns BACKWARD, FORWARD or zero (for an error).
14976 */
14977 static int
14978get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014979 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980 int *flagsp;
14981{
14982 int dir = FORWARD;
14983 char_u *flags;
14984 char_u nbuf[NUMBUFLEN];
14985 int mask;
14986
14987 if (varp->v_type != VAR_UNKNOWN)
14988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014989 flags = get_tv_string_buf_chk(varp, nbuf);
14990 if (flags == NULL)
14991 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992 while (*flags != NUL)
14993 {
14994 switch (*flags)
14995 {
14996 case 'b': dir = BACKWARD; break;
14997 case 'w': p_ws = TRUE; break;
14998 case 'W': p_ws = FALSE; break;
14999 default: mask = 0;
15000 if (flagsp != NULL)
15001 switch (*flags)
15002 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015003 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015004 case 'e': mask = SP_END; break;
15005 case 'm': mask = SP_RETCOUNT; break;
15006 case 'n': mask = SP_NOMOVE; break;
15007 case 'p': mask = SP_SUBPAT; break;
15008 case 'r': mask = SP_REPEAT; break;
15009 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015010 }
15011 if (mask == 0)
15012 {
15013 EMSG2(_(e_invarg2), flags);
15014 dir = 0;
15015 }
15016 else
15017 *flagsp |= mask;
15018 }
15019 if (dir == 0)
15020 break;
15021 ++flags;
15022 }
15023 }
15024 return dir;
15025}
15026
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015028 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015030 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015031search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015033 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015034 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015036 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 char_u *pat;
15038 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015039 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 int save_p_ws = p_ws;
15041 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015042 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015043 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015044 proftime_T tm;
15045#ifdef FEAT_RELTIME
15046 long time_limit = 0;
15047#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015048 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015049 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015052 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015053 if (dir == 0)
15054 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015055 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015056 if (flags & SP_START)
15057 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015058 if (flags & SP_END)
15059 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015060
Bram Moolenaar76929292008-01-06 19:07:36 +000015061 /* Optional arguments: line number to stop searching and timeout. */
15062 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015063 {
15064 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15065 if (lnum_stop < 0)
15066 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015067#ifdef FEAT_RELTIME
15068 if (argvars[3].v_type != VAR_UNKNOWN)
15069 {
15070 time_limit = get_tv_number_chk(&argvars[3], NULL);
15071 if (time_limit < 0)
15072 goto theend;
15073 }
15074#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015075 }
15076
Bram Moolenaar76929292008-01-06 19:07:36 +000015077#ifdef FEAT_RELTIME
15078 /* Set the time limit, if there is one. */
15079 profile_setlimit(time_limit, &tm);
15080#endif
15081
Bram Moolenaar231334e2005-07-25 20:46:57 +000015082 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015083 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015084 * Check to make sure only those flags are set.
15085 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15086 * flags cannot be set. Check for that condition also.
15087 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015088 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015089 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015091 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015092 goto theend;
15093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015095 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015096 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015097 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015098 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015099 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015100 if (flags & SP_SUBPAT)
15101 retval = subpatnum;
15102 else
15103 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015104 if (flags & SP_SETPCMARK)
15105 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015107 if (match_pos != NULL)
15108 {
15109 /* Store the match cursor position */
15110 match_pos->lnum = pos.lnum;
15111 match_pos->col = pos.col + 1;
15112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 /* "/$" will put the cursor after the end of the line, may need to
15114 * correct that here */
15115 check_cursor();
15116 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015117
15118 /* If 'n' flag is used: restore cursor position. */
15119 if (flags & SP_NOMOVE)
15120 curwin->w_cursor = save_cursor;
Bram Moolenaar860cae12010-06-05 23:22:07 +020015121#ifdef FEAT_CONCEAL
15122 else if (curwin->w_p_conceal
15123 && save_cursor.lnum != curwin->w_cursor.lnum)
15124 {
15125 curwin->w_set_curswant = TRUE;
15126 update_single_line(curwin, save_cursor.lnum);
15127 update_single_line(curwin, curwin->w_cursor.lnum);
15128 }
15129#endif
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015130 else
15131 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015132theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015134
15135 return retval;
15136}
15137
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015138#ifdef FEAT_FLOAT
15139/*
15140 * "round({float})" function
15141 */
15142 static void
15143f_round(argvars, rettv)
15144 typval_T *argvars;
15145 typval_T *rettv;
15146{
15147 float_T f;
15148
15149 rettv->v_type = VAR_FLOAT;
15150 if (get_float_arg(argvars, &f) == OK)
15151 /* round() is not in C90, use ceil() or floor() instead. */
15152 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15153 else
15154 rettv->vval.v_float = 0.0;
15155}
15156#endif
15157
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015158/*
15159 * "search()" function
15160 */
15161 static void
15162f_search(argvars, rettv)
15163 typval_T *argvars;
15164 typval_T *rettv;
15165{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015166 int flags = 0;
15167
15168 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169}
15170
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015172 * "searchdecl()" function
15173 */
15174 static void
15175f_searchdecl(argvars, rettv)
15176 typval_T *argvars;
15177 typval_T *rettv;
15178{
15179 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015180 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015181 int error = FALSE;
15182 char_u *name;
15183
15184 rettv->vval.v_number = 1; /* default: FAIL */
15185
15186 name = get_tv_string_chk(&argvars[0]);
15187 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015188 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015189 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015190 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15191 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15192 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015193 if (!error && name != NULL)
15194 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015195 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015196}
15197
15198/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015199 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015201 static int
15202searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015203 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015204 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205{
15206 char_u *spat, *mpat, *epat;
15207 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 int dir;
15210 int flags = 0;
15211 char_u nbuf1[NUMBUFLEN];
15212 char_u nbuf2[NUMBUFLEN];
15213 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015214 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015215 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015216 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015219 spat = get_tv_string_chk(&argvars[0]);
15220 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15221 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15222 if (spat == NULL || mpat == NULL || epat == NULL)
15223 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015224
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225 /* Handle the optional fourth argument: flags */
15226 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015227 if (dir == 0)
15228 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015229
15230 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015231 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15232 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015233 if ((flags & (SP_END | SP_SUBPAT)) != 0
15234 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015235 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015236 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015237 goto theend;
15238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015240 /* Using 'r' implies 'W', otherwise it doesn't work. */
15241 if (flags & SP_REPEAT)
15242 p_ws = FALSE;
15243
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015244 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015245 if (argvars[3].v_type == VAR_UNKNOWN
15246 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247 skip = (char_u *)"";
15248 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015251 if (argvars[5].v_type != VAR_UNKNOWN)
15252 {
15253 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15254 if (lnum_stop < 0)
15255 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015256#ifdef FEAT_RELTIME
15257 if (argvars[6].v_type != VAR_UNKNOWN)
15258 {
15259 time_limit = get_tv_number_chk(&argvars[6], NULL);
15260 if (time_limit < 0)
15261 goto theend;
15262 }
15263#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015264 }
15265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266 if (skip == NULL)
15267 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015269 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015270 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015271
15272theend:
15273 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015274
15275 return retval;
15276}
15277
15278/*
15279 * "searchpair()" function
15280 */
15281 static void
15282f_searchpair(argvars, rettv)
15283 typval_T *argvars;
15284 typval_T *rettv;
15285{
15286 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15287}
15288
15289/*
15290 * "searchpairpos()" function
15291 */
15292 static void
15293f_searchpairpos(argvars, rettv)
15294 typval_T *argvars;
15295 typval_T *rettv;
15296{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015297 pos_T match_pos;
15298 int lnum = 0;
15299 int col = 0;
15300
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015301 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015302 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015303
15304 if (searchpair_cmn(argvars, &match_pos) > 0)
15305 {
15306 lnum = match_pos.lnum;
15307 col = match_pos.col;
15308 }
15309
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015310 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15311 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015312}
15313
15314/*
15315 * Search for a start/middle/end thing.
15316 * Used by searchpair(), see its documentation for the details.
15317 * Returns 0 or -1 for no match,
15318 */
15319 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015320do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15321 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015322 char_u *spat; /* start pattern */
15323 char_u *mpat; /* middle pattern */
15324 char_u *epat; /* end pattern */
15325 int dir; /* BACKWARD or FORWARD */
15326 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015327 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015328 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015329 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015330 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015331{
15332 char_u *save_cpo;
15333 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15334 long retval = 0;
15335 pos_T pos;
15336 pos_T firstpos;
15337 pos_T foundpos;
15338 pos_T save_cursor;
15339 pos_T save_pos;
15340 int n;
15341 int r;
15342 int nest = 1;
15343 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015344 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015345 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015346
15347 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15348 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015349 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015350
Bram Moolenaar76929292008-01-06 19:07:36 +000015351#ifdef FEAT_RELTIME
15352 /* Set the time limit, if there is one. */
15353 profile_setlimit(time_limit, &tm);
15354#endif
15355
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015356 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15357 * start/middle/end (pat3, for the top pair). */
15358 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15359 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15360 if (pat2 == NULL || pat3 == NULL)
15361 goto theend;
15362 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15363 if (*mpat == NUL)
15364 STRCPY(pat3, pat2);
15365 else
15366 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15367 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015368 if (flags & SP_START)
15369 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015370
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 save_cursor = curwin->w_cursor;
15372 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015373 clearpos(&firstpos);
15374 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 pat = pat3;
15376 for (;;)
15377 {
15378 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015379 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15381 /* didn't find it or found the first match again: FAIL */
15382 break;
15383
15384 if (firstpos.lnum == 0)
15385 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015386 if (equalpos(pos, foundpos))
15387 {
15388 /* Found the same position again. Can happen with a pattern that
15389 * has "\zs" at the end and searching backwards. Advance one
15390 * character and try again. */
15391 if (dir == BACKWARD)
15392 decl(&pos);
15393 else
15394 incl(&pos);
15395 }
15396 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015398 /* clear the start flag to avoid getting stuck here */
15399 options &= ~SEARCH_START;
15400
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 /* If the skip pattern matches, ignore this match. */
15402 if (*skip != NUL)
15403 {
15404 save_pos = curwin->w_cursor;
15405 curwin->w_cursor = pos;
15406 r = eval_to_bool(skip, &err, NULL, FALSE);
15407 curwin->w_cursor = save_pos;
15408 if (err)
15409 {
15410 /* Evaluating {skip} caused an error, break here. */
15411 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015412 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 break;
15414 }
15415 if (r)
15416 continue;
15417 }
15418
15419 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15420 {
15421 /* Found end when searching backwards or start when searching
15422 * forward: nested pair. */
15423 ++nest;
15424 pat = pat2; /* nested, don't search for middle */
15425 }
15426 else
15427 {
15428 /* Found end when searching forward or start when searching
15429 * backward: end of (nested) pair; or found middle in outer pair. */
15430 if (--nest == 1)
15431 pat = pat3; /* outer level, search for middle */
15432 }
15433
15434 if (nest == 0)
15435 {
15436 /* Found the match: return matchcount or line number. */
15437 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015438 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015440 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015441 if (flags & SP_SETPCMARK)
15442 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 curwin->w_cursor = pos;
15444 if (!(flags & SP_REPEAT))
15445 break;
15446 nest = 1; /* search for next unmatched */
15447 }
15448 }
15449
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015450 if (match_pos != NULL)
15451 {
15452 /* Store the match cursor position */
15453 match_pos->lnum = curwin->w_cursor.lnum;
15454 match_pos->col = curwin->w_cursor.col + 1;
15455 }
15456
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015458 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459 curwin->w_cursor = save_cursor;
15460
15461theend:
15462 vim_free(pat2);
15463 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015464 if (p_cpo == empty_option)
15465 p_cpo = save_cpo;
15466 else
15467 /* Darn, evaluating the {skip} expression changed the value. */
15468 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015469
15470 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471}
15472
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015473/*
15474 * "searchpos()" function
15475 */
15476 static void
15477f_searchpos(argvars, rettv)
15478 typval_T *argvars;
15479 typval_T *rettv;
15480{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015481 pos_T match_pos;
15482 int lnum = 0;
15483 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015484 int n;
15485 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015486
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015487 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015488 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015489
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015490 n = search_cmn(argvars, &match_pos, &flags);
15491 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015492 {
15493 lnum = match_pos.lnum;
15494 col = match_pos.col;
15495 }
15496
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015497 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15498 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015499 if (flags & SP_SUBPAT)
15500 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015501}
15502
15503
Bram Moolenaar0d660222005-01-07 21:51:51 +000015504 static void
15505f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015506 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015509#ifdef FEAT_CLIENTSERVER
15510 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015511 char_u *server = get_tv_string_chk(&argvars[0]);
15512 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513
Bram Moolenaar0d660222005-01-07 21:51:51 +000015514 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015515 if (server == NULL || reply == NULL)
15516 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015517 if (check_restricted() || check_secure())
15518 return;
15519# ifdef FEAT_X11
15520 if (check_connection() == FAIL)
15521 return;
15522# endif
15523
15524 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015526 EMSG(_("E258: Unable to send to client"));
15527 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015529 rettv->vval.v_number = 0;
15530#else
15531 rettv->vval.v_number = -1;
15532#endif
15533}
15534
Bram Moolenaar0d660222005-01-07 21:51:51 +000015535 static void
15536f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015537 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015539{
15540 char_u *r = NULL;
15541
15542#ifdef FEAT_CLIENTSERVER
15543# ifdef WIN32
15544 r = serverGetVimNames();
15545# else
15546 make_connection();
15547 if (X_DISPLAY != NULL)
15548 r = serverGetVimNames(X_DISPLAY);
15549# endif
15550#endif
15551 rettv->v_type = VAR_STRING;
15552 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553}
15554
15555/*
15556 * "setbufvar()" function
15557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015560 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015561 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562{
15563 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 char_u nbuf[NUMBUFLEN];
15568
15569 if (check_restricted() || check_secure())
15570 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015571 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15572 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 varp = &argvars[2];
15575
15576 if (buf != NULL && varname != NULL && varp != NULL)
15577 {
15578 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580
15581 if (*varname == '&')
15582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 long numval;
15584 char_u *strval;
15585 int error = FALSE;
15586
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 numval = get_tv_number_chk(varp, &error);
15589 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015590 if (!error && strval != NULL)
15591 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 }
15593 else
15594 {
15595 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15596 if (bufvarname != NULL)
15597 {
15598 STRCPY(bufvarname, "b:");
15599 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015600 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 vim_free(bufvarname);
15602 }
15603 }
15604
15605 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608}
15609
15610/*
15611 * "setcmdpos()" function
15612 */
15613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015614f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015615 typval_T *argvars;
15616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015618 int pos = (int)get_tv_number(&argvars[0]) - 1;
15619
15620 if (pos >= 0)
15621 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622}
15623
15624/*
15625 * "setline()" function
15626 */
15627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015628f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015629 typval_T *argvars;
15630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631{
15632 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015633 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015634 list_T *l = NULL;
15635 listitem_T *li = NULL;
15636 long added = 0;
15637 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015639 lnum = get_tv_lnum(&argvars[0]);
15640 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015642 l = argvars[1].vval.v_list;
15643 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015645 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015647
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015648 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015649 for (;;)
15650 {
15651 if (l != NULL)
15652 {
15653 /* list argument, get next string */
15654 if (li == NULL)
15655 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015656 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015657 li = li->li_next;
15658 }
15659
15660 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015661 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015662 break;
15663 if (lnum <= curbuf->b_ml.ml_line_count)
15664 {
15665 /* existing line, replace it */
15666 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15667 {
15668 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015669 if (lnum == curwin->w_cursor.lnum)
15670 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015671 rettv->vval.v_number = 0; /* OK */
15672 }
15673 }
15674 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15675 {
15676 /* lnum is one past the last line, append the line */
15677 ++added;
15678 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15679 rettv->vval.v_number = 0; /* OK */
15680 }
15681
15682 if (l == NULL) /* only one string argument */
15683 break;
15684 ++lnum;
15685 }
15686
15687 if (added > 0)
15688 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689}
15690
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015691static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15692
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015694 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015695 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015696 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015697set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015698 win_T *wp UNUSED;
15699 typval_T *list_arg UNUSED;
15700 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015701 typval_T *rettv;
15702{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015703#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015704 char_u *act;
15705 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015706#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015707
Bram Moolenaar2641f772005-03-25 21:58:17 +000015708 rettv->vval.v_number = -1;
15709
15710#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015711 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015712 EMSG(_(e_listreq));
15713 else
15714 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015715 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015716
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015717 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015718 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015719 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015720 if (act == NULL)
15721 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015722 if (*act == 'a' || *act == 'r')
15723 action = *act;
15724 }
15725
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015726 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015727 rettv->vval.v_number = 0;
15728 }
15729#endif
15730}
15731
15732/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015733 * "setloclist()" function
15734 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015735 static void
15736f_setloclist(argvars, rettv)
15737 typval_T *argvars;
15738 typval_T *rettv;
15739{
15740 win_T *win;
15741
15742 rettv->vval.v_number = -1;
15743
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015744 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015745 if (win != NULL)
15746 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15747}
15748
15749/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015750 * "setmatches()" function
15751 */
15752 static void
15753f_setmatches(argvars, rettv)
15754 typval_T *argvars;
15755 typval_T *rettv;
15756{
15757#ifdef FEAT_SEARCH_EXTRA
15758 list_T *l;
15759 listitem_T *li;
15760 dict_T *d;
15761
15762 rettv->vval.v_number = -1;
15763 if (argvars[0].v_type != VAR_LIST)
15764 {
15765 EMSG(_(e_listreq));
15766 return;
15767 }
15768 if ((l = argvars[0].vval.v_list) != NULL)
15769 {
15770
15771 /* To some extent make sure that we are dealing with a list from
15772 * "getmatches()". */
15773 li = l->lv_first;
15774 while (li != NULL)
15775 {
15776 if (li->li_tv.v_type != VAR_DICT
15777 || (d = li->li_tv.vval.v_dict) == NULL)
15778 {
15779 EMSG(_(e_invarg));
15780 return;
15781 }
15782 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15783 && dict_find(d, (char_u *)"pattern", -1) != NULL
15784 && dict_find(d, (char_u *)"priority", -1) != NULL
15785 && dict_find(d, (char_u *)"id", -1) != NULL))
15786 {
15787 EMSG(_(e_invarg));
15788 return;
15789 }
15790 li = li->li_next;
15791 }
15792
15793 clear_matches(curwin);
15794 li = l->lv_first;
15795 while (li != NULL)
15796 {
15797 d = li->li_tv.vval.v_dict;
15798 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15799 get_dict_string(d, (char_u *)"pattern", FALSE),
15800 (int)get_dict_number(d, (char_u *)"priority"),
15801 (int)get_dict_number(d, (char_u *)"id"));
15802 li = li->li_next;
15803 }
15804 rettv->vval.v_number = 0;
15805 }
15806#endif
15807}
15808
15809/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015810 * "setpos()" function
15811 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015812 static void
15813f_setpos(argvars, rettv)
15814 typval_T *argvars;
15815 typval_T *rettv;
15816{
15817 pos_T pos;
15818 int fnum;
15819 char_u *name;
15820
Bram Moolenaar08250432008-02-13 11:42:46 +000015821 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015822 name = get_tv_string_chk(argvars);
15823 if (name != NULL)
15824 {
15825 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15826 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015827 if (--pos.col < 0)
15828 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015829 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015830 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015831 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015832 if (fnum == curbuf->b_fnum)
15833 {
15834 curwin->w_cursor = pos;
15835 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015836 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015837 }
15838 else
15839 EMSG(_(e_invarg));
15840 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015841 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15842 {
15843 /* set mark */
15844 if (setmark_pos(name[1], &pos, fnum) == OK)
15845 rettv->vval.v_number = 0;
15846 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015847 else
15848 EMSG(_(e_invarg));
15849 }
15850 }
15851}
15852
15853/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015854 * "setqflist()" function
15855 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015856 static void
15857f_setqflist(argvars, rettv)
15858 typval_T *argvars;
15859 typval_T *rettv;
15860{
15861 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15862}
15863
15864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 * "setreg()" function
15866 */
15867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015868f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015869 typval_T *argvars;
15870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871{
15872 int regname;
15873 char_u *strregname;
15874 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015875 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 int append;
15877 char_u yank_type;
15878 long block_len;
15879
15880 block_len = -1;
15881 yank_type = MAUTO;
15882 append = FALSE;
15883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015884 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015885 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015887 if (strregname == NULL)
15888 return; /* type error; errmsg already given */
15889 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 if (regname == 0 || regname == '@')
15891 regname = '"';
15892 else if (regname == '=')
15893 return;
15894
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015895 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015897 stropt = get_tv_string_chk(&argvars[2]);
15898 if (stropt == NULL)
15899 return; /* type error */
15900 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 switch (*stropt)
15902 {
15903 case 'a': case 'A': /* append */
15904 append = TRUE;
15905 break;
15906 case 'v': case 'c': /* character-wise selection */
15907 yank_type = MCHAR;
15908 break;
15909 case 'V': case 'l': /* line-wise selection */
15910 yank_type = MLINE;
15911 break;
15912#ifdef FEAT_VISUAL
15913 case 'b': case Ctrl_V: /* block-wise selection */
15914 yank_type = MBLOCK;
15915 if (VIM_ISDIGIT(stropt[1]))
15916 {
15917 ++stropt;
15918 block_len = getdigits(&stropt) - 1;
15919 --stropt;
15920 }
15921 break;
15922#endif
15923 }
15924 }
15925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015926 strval = get_tv_string_chk(&argvars[1]);
15927 if (strval != NULL)
15928 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015930 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931}
15932
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015933/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015934 * "settabvar()" function
15935 */
15936 static void
15937f_settabvar(argvars, rettv)
15938 typval_T *argvars;
15939 typval_T *rettv;
15940{
15941 tabpage_T *save_curtab;
15942 char_u *varname, *tabvarname;
15943 typval_T *varp;
15944 tabpage_T *tp;
15945
15946 rettv->vval.v_number = 0;
15947
15948 if (check_restricted() || check_secure())
15949 return;
15950
15951 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15952 varname = get_tv_string_chk(&argvars[1]);
15953 varp = &argvars[2];
15954
15955 if (tp != NULL && varname != NULL && varp != NULL)
15956 {
15957 save_curtab = curtab;
15958 goto_tabpage_tp(tp);
15959
15960 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
15961 if (tabvarname != NULL)
15962 {
15963 STRCPY(tabvarname, "t:");
15964 STRCPY(tabvarname + 2, varname);
15965 set_var(tabvarname, varp, TRUE);
15966 vim_free(tabvarname);
15967 }
15968
15969 /* Restore current tabpage */
15970 if (valid_tabpage(save_curtab))
15971 goto_tabpage_tp(save_curtab);
15972 }
15973}
15974
15975/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015976 * "settabwinvar()" function
15977 */
15978 static void
15979f_settabwinvar(argvars, rettv)
15980 typval_T *argvars;
15981 typval_T *rettv;
15982{
15983 setwinvar(argvars, rettv, 1);
15984}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985
15986/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015987 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015990f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015991 typval_T *argvars;
15992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015994 setwinvar(argvars, rettv, 0);
15995}
15996
15997/*
15998 * "setwinvar()" and "settabwinvar()" functions
15999 */
16000 static void
16001setwinvar(argvars, rettv, off)
16002 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016003 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016004 int off;
16005{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 win_T *win;
16007#ifdef FEAT_WINDOWS
16008 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016009 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010#endif
16011 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016012 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016014 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015
16016 if (check_restricted() || check_secure())
16017 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016018
16019#ifdef FEAT_WINDOWS
16020 if (off == 1)
16021 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16022 else
16023 tp = curtab;
16024#endif
16025 win = find_win_by_nr(&argvars[off], tp);
16026 varname = get_tv_string_chk(&argvars[off + 1]);
16027 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028
16029 if (win != NULL && varname != NULL && varp != NULL)
16030 {
16031#ifdef FEAT_WINDOWS
16032 /* set curwin to be our win, temporarily */
16033 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016034 save_curtab = curtab;
16035 goto_tabpage_tp(tp);
16036 if (!win_valid(win))
16037 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 curwin = win;
16039 curbuf = curwin->w_buffer;
16040#endif
16041
16042 if (*varname == '&')
16043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016044 long numval;
16045 char_u *strval;
16046 int error = FALSE;
16047
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016049 numval = get_tv_number_chk(varp, &error);
16050 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016051 if (!error && strval != NULL)
16052 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 }
16054 else
16055 {
16056 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16057 if (winvarname != NULL)
16058 {
16059 STRCPY(winvarname, "w:");
16060 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016061 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 vim_free(winvarname);
16063 }
16064 }
16065
16066#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016067 /* Restore current tabpage and window, if still valid (autocomands can
16068 * make them invalid). */
16069 if (valid_tabpage(save_curtab))
16070 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 if (win_valid(save_curwin))
16072 {
16073 curwin = save_curwin;
16074 curbuf = curwin->w_buffer;
16075 }
16076#endif
16077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078}
16079
16080/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016081 * "shellescape({string})" function
16082 */
16083 static void
16084f_shellescape(argvars, rettv)
16085 typval_T *argvars;
16086 typval_T *rettv;
16087{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016088 rettv->vval.v_string = vim_strsave_shellescape(
16089 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016090 rettv->v_type = VAR_STRING;
16091}
16092
16093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016094 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 */
16096 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016097f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016098 typval_T *argvars;
16099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016101 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102
Bram Moolenaar0d660222005-01-07 21:51:51 +000016103 p = get_tv_string(&argvars[0]);
16104 rettv->vval.v_string = vim_strsave(p);
16105 simplify_filename(rettv->vval.v_string); /* simplify in place */
16106 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107}
16108
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016109#ifdef FEAT_FLOAT
16110/*
16111 * "sin()" function
16112 */
16113 static void
16114f_sin(argvars, rettv)
16115 typval_T *argvars;
16116 typval_T *rettv;
16117{
16118 float_T f;
16119
16120 rettv->v_type = VAR_FLOAT;
16121 if (get_float_arg(argvars, &f) == OK)
16122 rettv->vval.v_float = sin(f);
16123 else
16124 rettv->vval.v_float = 0.0;
16125}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016126
16127/*
16128 * "sinh()" function
16129 */
16130 static void
16131f_sinh(argvars, rettv)
16132 typval_T *argvars;
16133 typval_T *rettv;
16134{
16135 float_T f;
16136
16137 rettv->v_type = VAR_FLOAT;
16138 if (get_float_arg(argvars, &f) == OK)
16139 rettv->vval.v_float = sinh(f);
16140 else
16141 rettv->vval.v_float = 0.0;
16142}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016143#endif
16144
Bram Moolenaar0d660222005-01-07 21:51:51 +000016145static int
16146#ifdef __BORLANDC__
16147 _RTLENTRYF
16148#endif
16149 item_compare __ARGS((const void *s1, const void *s2));
16150static int
16151#ifdef __BORLANDC__
16152 _RTLENTRYF
16153#endif
16154 item_compare2 __ARGS((const void *s1, const void *s2));
16155
16156static int item_compare_ic;
16157static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016158static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016159#define ITEM_COMPARE_FAIL 999
16160
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164 static int
16165#ifdef __BORLANDC__
16166_RTLENTRYF
16167#endif
16168item_compare(s1, s2)
16169 const void *s1;
16170 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172 char_u *p1, *p2;
16173 char_u *tofree1, *tofree2;
16174 int res;
16175 char_u numbuf1[NUMBUFLEN];
16176 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016178 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16179 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016180 if (p1 == NULL)
16181 p1 = (char_u *)"";
16182 if (p2 == NULL)
16183 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016184 if (item_compare_ic)
16185 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187 res = STRCMP(p1, p2);
16188 vim_free(tofree1);
16189 vim_free(tofree2);
16190 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191}
16192
16193 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016194#ifdef __BORLANDC__
16195_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197item_compare2(s1, s2)
16198 const void *s1;
16199 const void *s2;
16200{
16201 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016202 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016203 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016204 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016206 /* shortcut after failure in previous call; compare all items equal */
16207 if (item_compare_func_err)
16208 return 0;
16209
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016210 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16211 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16213 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016214
16215 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016216 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016217 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218 clear_tv(&argv[0]);
16219 clear_tv(&argv[1]);
16220
16221 if (res == FAIL)
16222 res = ITEM_COMPARE_FAIL;
16223 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016224 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16225 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016226 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227 clear_tv(&rettv);
16228 return res;
16229}
16230
16231/*
16232 * "sort({list})" function
16233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016235f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016236 typval_T *argvars;
16237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238{
Bram Moolenaar33570922005-01-25 22:26:29 +000016239 list_T *l;
16240 listitem_T *li;
16241 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242 long len;
16243 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244
Bram Moolenaar0d660222005-01-07 21:51:51 +000016245 if (argvars[0].v_type != VAR_LIST)
16246 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 else
16248 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016250 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251 return;
16252 rettv->vval.v_list = l;
16253 rettv->v_type = VAR_LIST;
16254 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255
Bram Moolenaar0d660222005-01-07 21:51:51 +000016256 len = list_len(l);
16257 if (len <= 1)
16258 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260 item_compare_ic = FALSE;
16261 item_compare_func = NULL;
16262 if (argvars[1].v_type != VAR_UNKNOWN)
16263 {
16264 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016265 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016266 else
16267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016268 int error = FALSE;
16269
16270 i = get_tv_number_chk(&argvars[1], &error);
16271 if (error)
16272 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016273 if (i == 1)
16274 item_compare_ic = TRUE;
16275 else
16276 item_compare_func = get_tv_string(&argvars[1]);
16277 }
16278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279
Bram Moolenaar0d660222005-01-07 21:51:51 +000016280 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282 if (ptrs == NULL)
16283 return;
16284 i = 0;
16285 for (li = l->lv_first; li != NULL; li = li->li_next)
16286 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016288 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016289 /* test the compare function */
16290 if (item_compare_func != NULL
16291 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16292 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016293 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295 {
16296 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016297 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016298 item_compare_func == NULL ? item_compare : item_compare2);
16299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016300 if (!item_compare_func_err)
16301 {
16302 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016303 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016304 l->lv_len = 0;
16305 for (i = 0; i < len; ++i)
16306 list_append(l, ptrs[i]);
16307 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 }
16309
16310 vim_free(ptrs);
16311 }
16312}
16313
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016314/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016315 * "soundfold({word})" function
16316 */
16317 static void
16318f_soundfold(argvars, rettv)
16319 typval_T *argvars;
16320 typval_T *rettv;
16321{
16322 char_u *s;
16323
16324 rettv->v_type = VAR_STRING;
16325 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016326#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016327 rettv->vval.v_string = eval_soundfold(s);
16328#else
16329 rettv->vval.v_string = vim_strsave(s);
16330#endif
16331}
16332
16333/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016334 * "spellbadword()" function
16335 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016336 static void
16337f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016338 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016339 typval_T *rettv;
16340{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016341 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016342 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016343 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016344
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016345 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016346 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016347
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016348#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016349 if (argvars[0].v_type == VAR_UNKNOWN)
16350 {
16351 /* Find the start and length of the badly spelled word. */
16352 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16353 if (len != 0)
16354 word = ml_get_cursor();
16355 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016356 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016357 {
16358 char_u *str = get_tv_string_chk(&argvars[0]);
16359 int capcol = -1;
16360
16361 if (str != NULL)
16362 {
16363 /* Check the argument for spelling. */
16364 while (*str != NUL)
16365 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016366 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016367 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016368 {
16369 word = str;
16370 break;
16371 }
16372 str += len;
16373 }
16374 }
16375 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016376#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016377
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016378 list_append_string(rettv->vval.v_list, word, len);
16379 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016380 attr == HLF_SPB ? "bad" :
16381 attr == HLF_SPR ? "rare" :
16382 attr == HLF_SPL ? "local" :
16383 attr == HLF_SPC ? "caps" :
16384 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016385}
16386
16387/*
16388 * "spellsuggest()" function
16389 */
16390 static void
16391f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016392 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016393 typval_T *rettv;
16394{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016395#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016396 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016397 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016398 int maxcount;
16399 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016400 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016401 listitem_T *li;
16402 int need_capital = FALSE;
16403#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016404
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016405 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016406 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016407
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016408#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016409 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016410 {
16411 str = get_tv_string(&argvars[0]);
16412 if (argvars[1].v_type != VAR_UNKNOWN)
16413 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016414 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016415 if (maxcount <= 0)
16416 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016417 if (argvars[2].v_type != VAR_UNKNOWN)
16418 {
16419 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16420 if (typeerr)
16421 return;
16422 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016423 }
16424 else
16425 maxcount = 25;
16426
Bram Moolenaar4770d092006-01-12 23:22:24 +000016427 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016428
16429 for (i = 0; i < ga.ga_len; ++i)
16430 {
16431 str = ((char_u **)ga.ga_data)[i];
16432
16433 li = listitem_alloc();
16434 if (li == NULL)
16435 vim_free(str);
16436 else
16437 {
16438 li->li_tv.v_type = VAR_STRING;
16439 li->li_tv.v_lock = 0;
16440 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016441 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016442 }
16443 }
16444 ga_clear(&ga);
16445 }
16446#endif
16447}
16448
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016450f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016451 typval_T *argvars;
16452 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016453{
16454 char_u *str;
16455 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016456 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457 regmatch_T regmatch;
16458 char_u patbuf[NUMBUFLEN];
16459 char_u *save_cpo;
16460 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016461 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016462 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016463 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464
16465 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16466 save_cpo = p_cpo;
16467 p_cpo = (char_u *)"";
16468
16469 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016470 if (argvars[1].v_type != VAR_UNKNOWN)
16471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016472 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16473 if (pat == NULL)
16474 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016475 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016476 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016477 }
16478 if (pat == NULL || *pat == NUL)
16479 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016481 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016483 if (typeerr)
16484 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485
Bram Moolenaar0d660222005-01-07 21:51:51 +000016486 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16487 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016490 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016491 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016492 if (*str == NUL)
16493 match = FALSE; /* empty item at the end */
16494 else
16495 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016496 if (match)
16497 end = regmatch.startp[0];
16498 else
16499 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016500 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16501 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016502 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016503 if (list_append_string(rettv->vval.v_list, str,
16504 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016505 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016506 }
16507 if (!match)
16508 break;
16509 /* Advance to just after the match. */
16510 if (regmatch.endp[0] > str)
16511 col = 0;
16512 else
16513 {
16514 /* Don't get stuck at the same match. */
16515#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016516 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517#else
16518 col = 1;
16519#endif
16520 }
16521 str = regmatch.endp[0];
16522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526
Bram Moolenaar0d660222005-01-07 21:51:51 +000016527 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528}
16529
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016530#ifdef FEAT_FLOAT
16531/*
16532 * "sqrt()" function
16533 */
16534 static void
16535f_sqrt(argvars, rettv)
16536 typval_T *argvars;
16537 typval_T *rettv;
16538{
16539 float_T f;
16540
16541 rettv->v_type = VAR_FLOAT;
16542 if (get_float_arg(argvars, &f) == OK)
16543 rettv->vval.v_float = sqrt(f);
16544 else
16545 rettv->vval.v_float = 0.0;
16546}
16547
16548/*
16549 * "str2float()" function
16550 */
16551 static void
16552f_str2float(argvars, rettv)
16553 typval_T *argvars;
16554 typval_T *rettv;
16555{
16556 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16557
16558 if (*p == '+')
16559 p = skipwhite(p + 1);
16560 (void)string2float(p, &rettv->vval.v_float);
16561 rettv->v_type = VAR_FLOAT;
16562}
16563#endif
16564
Bram Moolenaar2c932302006-03-18 21:42:09 +000016565/*
16566 * "str2nr()" function
16567 */
16568 static void
16569f_str2nr(argvars, rettv)
16570 typval_T *argvars;
16571 typval_T *rettv;
16572{
16573 int base = 10;
16574 char_u *p;
16575 long n;
16576
16577 if (argvars[1].v_type != VAR_UNKNOWN)
16578 {
16579 base = get_tv_number(&argvars[1]);
16580 if (base != 8 && base != 10 && base != 16)
16581 {
16582 EMSG(_(e_invarg));
16583 return;
16584 }
16585 }
16586
16587 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016588 if (*p == '+')
16589 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016590 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16591 rettv->vval.v_number = n;
16592}
16593
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594#ifdef HAVE_STRFTIME
16595/*
16596 * "strftime({format}[, {time}])" function
16597 */
16598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016599f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016600 typval_T *argvars;
16601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602{
16603 char_u result_buf[256];
16604 struct tm *curtime;
16605 time_t seconds;
16606 char_u *p;
16607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016608 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016610 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016611 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 seconds = time(NULL);
16613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016614 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 curtime = localtime(&seconds);
16616 /* MSVC returns NULL for an invalid value of seconds. */
16617 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016618 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 else
16620 {
16621# ifdef FEAT_MBYTE
16622 vimconv_T conv;
16623 char_u *enc;
16624
16625 conv.vc_type = CONV_NONE;
16626 enc = enc_locale();
16627 convert_setup(&conv, p_enc, enc);
16628 if (conv.vc_type != CONV_NONE)
16629 p = string_convert(&conv, p, NULL);
16630# endif
16631 if (p != NULL)
16632 (void)strftime((char *)result_buf, sizeof(result_buf),
16633 (char *)p, curtime);
16634 else
16635 result_buf[0] = NUL;
16636
16637# ifdef FEAT_MBYTE
16638 if (conv.vc_type != CONV_NONE)
16639 vim_free(p);
16640 convert_setup(&conv, enc, p_enc);
16641 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016642 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 else
16644# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016645 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646
16647# ifdef FEAT_MBYTE
16648 /* Release conversion descriptors */
16649 convert_setup(&conv, NULL, NULL);
16650 vim_free(enc);
16651# endif
16652 }
16653}
16654#endif
16655
16656/*
16657 * "stridx()" function
16658 */
16659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016660f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016661 typval_T *argvars;
16662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663{
16664 char_u buf[NUMBUFLEN];
16665 char_u *needle;
16666 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016667 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016669 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016671 needle = get_tv_string_chk(&argvars[1]);
16672 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016673 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016674 if (needle == NULL || haystack == NULL)
16675 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676
Bram Moolenaar33570922005-01-25 22:26:29 +000016677 if (argvars[2].v_type != VAR_UNKNOWN)
16678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016679 int error = FALSE;
16680
16681 start_idx = get_tv_number_chk(&argvars[2], &error);
16682 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016684 if (start_idx >= 0)
16685 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016686 }
16687
16688 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16689 if (pos != NULL)
16690 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691}
16692
16693/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016694 * "string()" function
16695 */
16696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016697f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016698 typval_T *argvars;
16699 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016700{
16701 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016702 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016704 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016705 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016706 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016707 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016708 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709}
16710
16711/*
16712 * "strlen()" function
16713 */
16714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016715f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016716 typval_T *argvars;
16717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719 rettv->vval.v_number = (varnumber_T)(STRLEN(
16720 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721}
16722
16723/*
16724 * "strpart()" function
16725 */
16726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016727f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016728 typval_T *argvars;
16729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730{
16731 char_u *p;
16732 int n;
16733 int len;
16734 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016735 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016737 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738 slen = (int)STRLEN(p);
16739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016740 n = get_tv_number_chk(&argvars[1], &error);
16741 if (error)
16742 len = 0;
16743 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016744 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 else
16746 len = slen - n; /* default len: all bytes that are available. */
16747
16748 /*
16749 * Only return the overlap between the specified part and the actual
16750 * string.
16751 */
16752 if (n < 0)
16753 {
16754 len += n;
16755 n = 0;
16756 }
16757 else if (n > slen)
16758 n = slen;
16759 if (len < 0)
16760 len = 0;
16761 else if (n + len > slen)
16762 len = slen - n;
16763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016764 rettv->v_type = VAR_STRING;
16765 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766}
16767
16768/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769 * "strridx()" function
16770 */
16771 static void
16772f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016773 typval_T *argvars;
16774 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016775{
16776 char_u buf[NUMBUFLEN];
16777 char_u *needle;
16778 char_u *haystack;
16779 char_u *rest;
16780 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016781 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016783 needle = get_tv_string_chk(&argvars[1]);
16784 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016785
16786 rettv->vval.v_number = -1;
16787 if (needle == NULL || haystack == NULL)
16788 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016789
16790 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016791 if (argvars[2].v_type != VAR_UNKNOWN)
16792 {
16793 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016794 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016795 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016796 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016797 }
16798 else
16799 end_idx = haystack_len;
16800
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016802 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016803 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016804 lastmatch = haystack + end_idx;
16805 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016806 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016807 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016808 for (rest = haystack; *rest != '\0'; ++rest)
16809 {
16810 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016811 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016812 break;
16813 lastmatch = rest;
16814 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016815 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016816
16817 if (lastmatch == NULL)
16818 rettv->vval.v_number = -1;
16819 else
16820 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16821}
16822
16823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 * "strtrans()" function
16825 */
16826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016827f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016828 typval_T *argvars;
16829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016831 rettv->v_type = VAR_STRING;
16832 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833}
16834
16835/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016836 * "submatch()" function
16837 */
16838 static void
16839f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016840 typval_T *argvars;
16841 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016842{
16843 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016844 rettv->vval.v_string =
16845 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016846}
16847
16848/*
16849 * "substitute()" function
16850 */
16851 static void
16852f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016853 typval_T *argvars;
16854 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855{
16856 char_u patbuf[NUMBUFLEN];
16857 char_u subbuf[NUMBUFLEN];
16858 char_u flagsbuf[NUMBUFLEN];
16859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016860 char_u *str = get_tv_string_chk(&argvars[0]);
16861 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16862 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16863 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16864
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16867 rettv->vval.v_string = NULL;
16868 else
16869 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016870}
16871
16872/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016873 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016876f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016877 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879{
16880 int id = 0;
16881#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016882 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 long col;
16884 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016885 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016887 lnum = get_tv_lnum(argvars); /* -1 on type error */
16888 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16889 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016891 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016892 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016893 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894#endif
16895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016896 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897}
16898
16899/*
16900 * "synIDattr(id, what [, mode])" function
16901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016903f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906{
16907 char_u *p = NULL;
16908#ifdef FEAT_SYN_HL
16909 int id;
16910 char_u *what;
16911 char_u *mode;
16912 char_u modebuf[NUMBUFLEN];
16913 int modec;
16914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016915 id = get_tv_number(&argvars[0]);
16916 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016917 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016919 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 modec = TOLOWER_ASC(mode[0]);
16921 if (modec != 't' && modec != 'c'
16922#ifdef FEAT_GUI
16923 && modec != 'g'
16924#endif
16925 )
16926 modec = 0; /* replace invalid with current */
16927 }
16928 else
16929 {
16930#ifdef FEAT_GUI
16931 if (gui.in_use)
16932 modec = 'g';
16933 else
16934#endif
16935 if (t_colors > 1)
16936 modec = 'c';
16937 else
16938 modec = 't';
16939 }
16940
16941
16942 switch (TOLOWER_ASC(what[0]))
16943 {
16944 case 'b':
16945 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16946 p = highlight_color(id, what, modec);
16947 else /* bold */
16948 p = highlight_has_attr(id, HL_BOLD, modec);
16949 break;
16950
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016951 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 p = highlight_color(id, what, modec);
16953 break;
16954
16955 case 'i':
16956 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16957 p = highlight_has_attr(id, HL_INVERSE, modec);
16958 else /* italic */
16959 p = highlight_has_attr(id, HL_ITALIC, modec);
16960 break;
16961
16962 case 'n': /* name */
16963 p = get_highlight_name(NULL, id - 1);
16964 break;
16965
16966 case 'r': /* reverse */
16967 p = highlight_has_attr(id, HL_INVERSE, modec);
16968 break;
16969
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016970 case 's':
16971 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16972 p = highlight_color(id, what, modec);
16973 else /* standout */
16974 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975 break;
16976
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016977 case 'u':
16978 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16979 /* underline */
16980 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16981 else
16982 /* undercurl */
16983 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 break;
16985 }
16986
16987 if (p != NULL)
16988 p = vim_strsave(p);
16989#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016990 rettv->v_type = VAR_STRING;
16991 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992}
16993
16994/*
16995 * "synIDtrans(id)" function
16996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016998f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016999 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001{
17002 int id;
17003
17004#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017005 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006
17007 if (id > 0)
17008 id = syn_get_final_id(id);
17009 else
17010#endif
17011 id = 0;
17012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017013 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014}
17015
17016/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017017 * "synstack(lnum, col)" function
17018 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017019 static void
17020f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017021 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017022 typval_T *rettv;
17023{
17024#ifdef FEAT_SYN_HL
17025 long lnum;
17026 long col;
17027 int i;
17028 int id;
17029#endif
17030
17031 rettv->v_type = VAR_LIST;
17032 rettv->vval.v_list = NULL;
17033
17034#ifdef FEAT_SYN_HL
17035 lnum = get_tv_lnum(argvars); /* -1 on type error */
17036 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17037
17038 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000017039 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017040 && rettv_list_alloc(rettv) != FAIL)
17041 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017042 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017043 for (i = 0; ; ++i)
17044 {
17045 id = syn_get_stack_item(i);
17046 if (id < 0)
17047 break;
17048 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17049 break;
17050 }
17051 }
17052#endif
17053}
17054
17055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 * "system()" function
17057 */
17058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017059f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017060 typval_T *argvars;
17061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017063 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017065 char_u *infile = NULL;
17066 char_u buf[NUMBUFLEN];
17067 int err = FALSE;
17068 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017070 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017071 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017072
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017073 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017074 {
17075 /*
17076 * Write the string to a temp file, to be used for input of the shell
17077 * command.
17078 */
17079 if ((infile = vim_tempname('i')) == NULL)
17080 {
17081 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017082 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017083 }
17084
17085 fd = mch_fopen((char *)infile, WRITEBIN);
17086 if (fd == NULL)
17087 {
17088 EMSG2(_(e_notopen), infile);
17089 goto done;
17090 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017091 p = get_tv_string_buf_chk(&argvars[1], buf);
17092 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017093 {
17094 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017095 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017096 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017097 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17098 err = TRUE;
17099 if (fclose(fd) != 0)
17100 err = TRUE;
17101 if (err)
17102 {
17103 EMSG(_("E677: Error writing temp file"));
17104 goto done;
17105 }
17106 }
17107
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017108 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17109 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017110
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111#ifdef USE_CR
17112 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017113 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 {
17115 char_u *s;
17116
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017117 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 {
17119 if (*s == CAR)
17120 *s = NL;
17121 }
17122 }
17123#else
17124# ifdef USE_CRNL
17125 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017126 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 {
17128 char_u *s, *d;
17129
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017130 d = res;
17131 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 {
17133 if (s[0] == CAR && s[1] == NL)
17134 ++s;
17135 *d++ = *s;
17136 }
17137 *d = NUL;
17138 }
17139# endif
17140#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017141
17142done:
17143 if (infile != NULL)
17144 {
17145 mch_remove(infile);
17146 vim_free(infile);
17147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017148 rettv->v_type = VAR_STRING;
17149 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150}
17151
17152/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017153 * "tabpagebuflist()" function
17154 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017155 static void
17156f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017157 typval_T *argvars UNUSED;
17158 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017159{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017160#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017161 tabpage_T *tp;
17162 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017163
17164 if (argvars[0].v_type == VAR_UNKNOWN)
17165 wp = firstwin;
17166 else
17167 {
17168 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17169 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017170 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017171 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017172 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017173 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017174 for (; wp != NULL; wp = wp->w_next)
17175 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017176 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017177 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017178 }
17179#endif
17180}
17181
17182
17183/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017184 * "tabpagenr()" function
17185 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017186 static void
17187f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017188 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017189 typval_T *rettv;
17190{
17191 int nr = 1;
17192#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017193 char_u *arg;
17194
17195 if (argvars[0].v_type != VAR_UNKNOWN)
17196 {
17197 arg = get_tv_string_chk(&argvars[0]);
17198 nr = 0;
17199 if (arg != NULL)
17200 {
17201 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017202 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017203 else
17204 EMSG2(_(e_invexpr2), arg);
17205 }
17206 }
17207 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017208 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017209#endif
17210 rettv->vval.v_number = nr;
17211}
17212
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017213
17214#ifdef FEAT_WINDOWS
17215static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17216
17217/*
17218 * Common code for tabpagewinnr() and winnr().
17219 */
17220 static int
17221get_winnr(tp, argvar)
17222 tabpage_T *tp;
17223 typval_T *argvar;
17224{
17225 win_T *twin;
17226 int nr = 1;
17227 win_T *wp;
17228 char_u *arg;
17229
17230 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17231 if (argvar->v_type != VAR_UNKNOWN)
17232 {
17233 arg = get_tv_string_chk(argvar);
17234 if (arg == NULL)
17235 nr = 0; /* type error; errmsg already given */
17236 else if (STRCMP(arg, "$") == 0)
17237 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17238 else if (STRCMP(arg, "#") == 0)
17239 {
17240 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17241 if (twin == NULL)
17242 nr = 0;
17243 }
17244 else
17245 {
17246 EMSG2(_(e_invexpr2), arg);
17247 nr = 0;
17248 }
17249 }
17250
17251 if (nr > 0)
17252 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17253 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017254 {
17255 if (wp == NULL)
17256 {
17257 /* didn't find it in this tabpage */
17258 nr = 0;
17259 break;
17260 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017261 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017262 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017263 return nr;
17264}
17265#endif
17266
17267/*
17268 * "tabpagewinnr()" function
17269 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017270 static void
17271f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017272 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017273 typval_T *rettv;
17274{
17275 int nr = 1;
17276#ifdef FEAT_WINDOWS
17277 tabpage_T *tp;
17278
17279 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17280 if (tp == NULL)
17281 nr = 0;
17282 else
17283 nr = get_winnr(tp, &argvars[1]);
17284#endif
17285 rettv->vval.v_number = nr;
17286}
17287
17288
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017289/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017290 * "tagfiles()" function
17291 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017292 static void
17293f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017294 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017295 typval_T *rettv;
17296{
17297 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017298 tagname_T tn;
17299 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017300
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017301 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017302 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017304 for (first = TRUE; ; first = FALSE)
17305 if (get_tagfname(&tn, first, fname) == FAIL
17306 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017307 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017308 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017309}
17310
17311/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017312 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017313 */
17314 static void
17315f_taglist(argvars, rettv)
17316 typval_T *argvars;
17317 typval_T *rettv;
17318{
17319 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017320
17321 tag_pattern = get_tv_string(&argvars[0]);
17322
17323 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017324 if (*tag_pattern == NUL)
17325 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017326
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017327 if (rettv_list_alloc(rettv) == OK)
17328 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017329}
17330
17331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 * "tempname()" function
17333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017336 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338{
17339 static int x = 'A';
17340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017341 rettv->v_type = VAR_STRING;
17342 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343
17344 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17345 * names. Skip 'I' and 'O', they are used for shell redirection. */
17346 do
17347 {
17348 if (x == 'Z')
17349 x = '0';
17350 else if (x == '9')
17351 x = 'A';
17352 else
17353 {
17354#ifdef EBCDIC
17355 if (x == 'I')
17356 x = 'J';
17357 else if (x == 'R')
17358 x = 'S';
17359 else
17360#endif
17361 ++x;
17362 }
17363 } while (x == 'I' || x == 'O');
17364}
17365
17366/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017367 * "test(list)" function: Just checking the walls...
17368 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017369 static void
17370f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017371 typval_T *argvars UNUSED;
17372 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017373{
17374 /* Used for unit testing. Change the code below to your liking. */
17375#if 0
17376 listitem_T *li;
17377 list_T *l;
17378 char_u *bad, *good;
17379
17380 if (argvars[0].v_type != VAR_LIST)
17381 return;
17382 l = argvars[0].vval.v_list;
17383 if (l == NULL)
17384 return;
17385 li = l->lv_first;
17386 if (li == NULL)
17387 return;
17388 bad = get_tv_string(&li->li_tv);
17389 li = li->li_next;
17390 if (li == NULL)
17391 return;
17392 good = get_tv_string(&li->li_tv);
17393 rettv->vval.v_number = test_edit_score(bad, good);
17394#endif
17395}
17396
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017397#ifdef FEAT_FLOAT
17398/*
17399 * "tan()" function
17400 */
17401 static void
17402f_tan(argvars, rettv)
17403 typval_T *argvars;
17404 typval_T *rettv;
17405{
17406 float_T f;
17407
17408 rettv->v_type = VAR_FLOAT;
17409 if (get_float_arg(argvars, &f) == OK)
17410 rettv->vval.v_float = tan(f);
17411 else
17412 rettv->vval.v_float = 0.0;
17413}
17414
17415/*
17416 * "tanh()" function
17417 */
17418 static void
17419f_tanh(argvars, rettv)
17420 typval_T *argvars;
17421 typval_T *rettv;
17422{
17423 float_T f;
17424
17425 rettv->v_type = VAR_FLOAT;
17426 if (get_float_arg(argvars, &f) == OK)
17427 rettv->vval.v_float = tanh(f);
17428 else
17429 rettv->vval.v_float = 0.0;
17430}
17431#endif
17432
Bram Moolenaard52d9742005-08-21 22:20:28 +000017433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 * "tolower(string)" function
17435 */
17436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017437f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017438 typval_T *argvars;
17439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440{
17441 char_u *p;
17442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017443 p = vim_strsave(get_tv_string(&argvars[0]));
17444 rettv->v_type = VAR_STRING;
17445 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446
17447 if (p != NULL)
17448 while (*p != NUL)
17449 {
17450#ifdef FEAT_MBYTE
17451 int l;
17452
17453 if (enc_utf8)
17454 {
17455 int c, lc;
17456
17457 c = utf_ptr2char(p);
17458 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017459 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 /* TODO: reallocate string when byte count changes. */
17461 if (utf_char2len(lc) == l)
17462 utf_char2bytes(lc, p);
17463 p += l;
17464 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017465 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466 p += l; /* skip multi-byte character */
17467 else
17468#endif
17469 {
17470 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17471 ++p;
17472 }
17473 }
17474}
17475
17476/*
17477 * "toupper(string)" function
17478 */
17479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017480f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017481 typval_T *argvars;
17482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017484 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017485 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486}
17487
17488/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017489 * "tr(string, fromstr, tostr)" function
17490 */
17491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017493 typval_T *argvars;
17494 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017495{
17496 char_u *instr;
17497 char_u *fromstr;
17498 char_u *tostr;
17499 char_u *p;
17500#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017501 int inlen;
17502 int fromlen;
17503 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017504 int idx;
17505 char_u *cpstr;
17506 int cplen;
17507 int first = TRUE;
17508#endif
17509 char_u buf[NUMBUFLEN];
17510 char_u buf2[NUMBUFLEN];
17511 garray_T ga;
17512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017513 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017514 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17515 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017516
17517 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017518 rettv->v_type = VAR_STRING;
17519 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017520 if (fromstr == NULL || tostr == NULL)
17521 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017522 ga_init2(&ga, (int)sizeof(char), 80);
17523
17524#ifdef FEAT_MBYTE
17525 if (!has_mbyte)
17526#endif
17527 /* not multi-byte: fromstr and tostr must be the same length */
17528 if (STRLEN(fromstr) != STRLEN(tostr))
17529 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017530#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017531error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017532#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017533 EMSG2(_(e_invarg2), fromstr);
17534 ga_clear(&ga);
17535 return;
17536 }
17537
17538 /* fromstr and tostr have to contain the same number of chars */
17539 while (*instr != NUL)
17540 {
17541#ifdef FEAT_MBYTE
17542 if (has_mbyte)
17543 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017544 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017545 cpstr = instr;
17546 cplen = inlen;
17547 idx = 0;
17548 for (p = fromstr; *p != NUL; p += fromlen)
17549 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017550 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017551 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17552 {
17553 for (p = tostr; *p != NUL; p += tolen)
17554 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017555 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017556 if (idx-- == 0)
17557 {
17558 cplen = tolen;
17559 cpstr = p;
17560 break;
17561 }
17562 }
17563 if (*p == NUL) /* tostr is shorter than fromstr */
17564 goto error;
17565 break;
17566 }
17567 ++idx;
17568 }
17569
17570 if (first && cpstr == instr)
17571 {
17572 /* Check that fromstr and tostr have the same number of
17573 * (multi-byte) characters. Done only once when a character
17574 * of instr doesn't appear in fromstr. */
17575 first = FALSE;
17576 for (p = tostr; *p != NUL; p += tolen)
17577 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017578 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017579 --idx;
17580 }
17581 if (idx != 0)
17582 goto error;
17583 }
17584
17585 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017586 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017587 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017588
17589 instr += inlen;
17590 }
17591 else
17592#endif
17593 {
17594 /* When not using multi-byte chars we can do it faster. */
17595 p = vim_strchr(fromstr, *instr);
17596 if (p != NULL)
17597 ga_append(&ga, tostr[p - fromstr]);
17598 else
17599 ga_append(&ga, *instr);
17600 ++instr;
17601 }
17602 }
17603
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017604 /* add a terminating NUL */
17605 ga_grow(&ga, 1);
17606 ga_append(&ga, NUL);
17607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017608 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017609}
17610
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017611#ifdef FEAT_FLOAT
17612/*
17613 * "trunc({float})" function
17614 */
17615 static void
17616f_trunc(argvars, rettv)
17617 typval_T *argvars;
17618 typval_T *rettv;
17619{
17620 float_T f;
17621
17622 rettv->v_type = VAR_FLOAT;
17623 if (get_float_arg(argvars, &f) == OK)
17624 /* trunc() is not in C90, use floor() or ceil() instead. */
17625 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17626 else
17627 rettv->vval.v_float = 0.0;
17628}
17629#endif
17630
Bram Moolenaar8299df92004-07-10 09:47:34 +000017631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 * "type(expr)" function
17633 */
17634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017635f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017636 typval_T *argvars;
17637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017639 int n;
17640
17641 switch (argvars[0].v_type)
17642 {
17643 case VAR_NUMBER: n = 0; break;
17644 case VAR_STRING: n = 1; break;
17645 case VAR_FUNC: n = 2; break;
17646 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017647 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017648#ifdef FEAT_FLOAT
17649 case VAR_FLOAT: n = 5; break;
17650#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017651 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17652 }
17653 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654}
17655
17656/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017657 * "undofile(name)" function
17658 */
17659 static void
17660f_undofile(argvars, rettv)
17661 typval_T *argvars;
17662 typval_T *rettv;
17663{
17664 rettv->v_type = VAR_STRING;
17665#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017666 {
17667 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17668
17669 if (ffname != NULL)
17670 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17671 vim_free(ffname);
17672 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017673#else
17674 rettv->vval.v_string = NULL;
17675#endif
17676}
17677
17678/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017679 * "values(dict)" function
17680 */
17681 static void
17682f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017683 typval_T *argvars;
17684 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017685{
17686 dict_list(argvars, rettv, 1);
17687}
17688
17689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 * "virtcol(string)" function
17691 */
17692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017693f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017694 typval_T *argvars;
17695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696{
17697 colnr_T vcol = 0;
17698 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017699 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017701 fp = var2fpos(&argvars[0], FALSE, &fnum);
17702 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17703 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 {
17705 getvvcol(curwin, fp, NULL, NULL, &vcol);
17706 ++vcol;
17707 }
17708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017709 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710}
17711
17712/*
17713 * "visualmode()" function
17714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017716f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017717 typval_T *argvars UNUSED;
17718 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719{
17720#ifdef FEAT_VISUAL
17721 char_u str[2];
17722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017723 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 str[0] = curbuf->b_visual_mode_eval;
17725 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017726 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727
17728 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017729 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731#endif
17732}
17733
17734/*
17735 * "winbufnr(nr)" function
17736 */
17737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017739 typval_T *argvars;
17740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741{
17742 win_T *wp;
17743
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017744 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017746 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017748 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749}
17750
17751/*
17752 * "wincol()" function
17753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017755f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758{
17759 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017760 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761}
17762
17763/*
17764 * "winheight(nr)" function
17765 */
17766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017767f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017768 typval_T *argvars;
17769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770{
17771 win_T *wp;
17772
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017773 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017775 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017777 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778}
17779
17780/*
17781 * "winline()" function
17782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017784f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017785 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787{
17788 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017789 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790}
17791
17792/*
17793 * "winnr()" function
17794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017796f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017797 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799{
17800 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017801
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017803 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017805 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806}
17807
17808/*
17809 * "winrestcmd()" function
17810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017812f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017813 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815{
17816#ifdef FEAT_WINDOWS
17817 win_T *wp;
17818 int winnr = 1;
17819 garray_T ga;
17820 char_u buf[50];
17821
17822 ga_init2(&ga, (int)sizeof(char), 70);
17823 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17824 {
17825 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17826 ga_concat(&ga, buf);
17827# ifdef FEAT_VERTSPLIT
17828 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17829 ga_concat(&ga, buf);
17830# endif
17831 ++winnr;
17832 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017833 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017835 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840}
17841
17842/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017843 * "winrestview()" function
17844 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017845 static void
17846f_winrestview(argvars, rettv)
17847 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017848 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017849{
17850 dict_T *dict;
17851
17852 if (argvars[0].v_type != VAR_DICT
17853 || (dict = argvars[0].vval.v_dict) == NULL)
17854 EMSG(_(e_invarg));
17855 else
17856 {
17857 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17858 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17859#ifdef FEAT_VIRTUALEDIT
17860 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17861#endif
17862 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017863 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017864
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017865 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017866#ifdef FEAT_DIFF
17867 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17868#endif
17869 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17870 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17871
17872 check_cursor();
17873 changed_cline_bef_curs();
17874 invalidate_botline();
17875 redraw_later(VALID);
17876
17877 if (curwin->w_topline == 0)
17878 curwin->w_topline = 1;
17879 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17880 curwin->w_topline = curbuf->b_ml.ml_line_count;
17881#ifdef FEAT_DIFF
17882 check_topfill(curwin, TRUE);
17883#endif
17884 }
17885}
17886
17887/*
17888 * "winsaveview()" function
17889 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017890 static void
17891f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017892 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017893 typval_T *rettv;
17894{
17895 dict_T *dict;
17896
17897 dict = dict_alloc();
17898 if (dict == NULL)
17899 return;
17900 rettv->v_type = VAR_DICT;
17901 rettv->vval.v_dict = dict;
17902 ++dict->dv_refcount;
17903
17904 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17905 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17906#ifdef FEAT_VIRTUALEDIT
17907 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17908#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017909 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017910 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17911
17912 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17913#ifdef FEAT_DIFF
17914 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17915#endif
17916 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17917 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17918}
17919
17920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 * "winwidth(nr)" function
17922 */
17923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017924f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017925 typval_T *argvars;
17926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927{
17928 win_T *wp;
17929
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017930 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017932 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933 else
17934#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017935 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017937 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938#endif
17939}
17940
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017942 * "writefile()" function
17943 */
17944 static void
17945f_writefile(argvars, rettv)
17946 typval_T *argvars;
17947 typval_T *rettv;
17948{
17949 int binary = FALSE;
17950 char_u *fname;
17951 FILE *fd;
17952 listitem_T *li;
17953 char_u *s;
17954 int ret = 0;
17955 int c;
17956
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017957 if (check_restricted() || check_secure())
17958 return;
17959
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017960 if (argvars[0].v_type != VAR_LIST)
17961 {
17962 EMSG2(_(e_listarg), "writefile()");
17963 return;
17964 }
17965 if (argvars[0].vval.v_list == NULL)
17966 return;
17967
17968 if (argvars[2].v_type != VAR_UNKNOWN
17969 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17970 binary = TRUE;
17971
17972 /* Always open the file in binary mode, library functions have a mind of
17973 * their own about CR-LF conversion. */
17974 fname = get_tv_string(&argvars[1]);
17975 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17976 {
17977 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17978 ret = -1;
17979 }
17980 else
17981 {
17982 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17983 li = li->li_next)
17984 {
17985 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17986 {
17987 if (*s == '\n')
17988 c = putc(NUL, fd);
17989 else
17990 c = putc(*s, fd);
17991 if (c == EOF)
17992 {
17993 ret = -1;
17994 break;
17995 }
17996 }
17997 if (!binary || li->li_next != NULL)
17998 if (putc('\n', fd) == EOF)
17999 {
18000 ret = -1;
18001 break;
18002 }
18003 if (ret < 0)
18004 {
18005 EMSG(_(e_write));
18006 break;
18007 }
18008 }
18009 fclose(fd);
18010 }
18011
18012 rettv->vval.v_number = ret;
18013}
18014
18015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018017 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 */
18019 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018020var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018021 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018022 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018023 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018025 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018027 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028
Bram Moolenaara5525202006-03-02 22:52:09 +000018029 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018030 if (varp->v_type == VAR_LIST)
18031 {
18032 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018033 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018034 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018035 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018036
18037 l = varp->vval.v_list;
18038 if (l == NULL)
18039 return NULL;
18040
18041 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018042 pos.lnum = list_find_nr(l, 0L, &error);
18043 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018044 return NULL; /* invalid line number */
18045
18046 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018047 pos.col = list_find_nr(l, 1L, &error);
18048 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018049 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018050 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018051
18052 /* We accept "$" for the column number: last column. */
18053 li = list_find(l, 1L);
18054 if (li != NULL && li->li_tv.v_type == VAR_STRING
18055 && li->li_tv.vval.v_string != NULL
18056 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18057 pos.col = len + 1;
18058
Bram Moolenaara5525202006-03-02 22:52:09 +000018059 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018060 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018061 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018062 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018063
Bram Moolenaara5525202006-03-02 22:52:09 +000018064#ifdef FEAT_VIRTUALEDIT
18065 /* Get the virtual offset. Defaults to zero. */
18066 pos.coladd = list_find_nr(l, 2L, &error);
18067 if (error)
18068 pos.coladd = 0;
18069#endif
18070
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018071 return &pos;
18072 }
18073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018074 name = get_tv_string_chk(varp);
18075 if (name == NULL)
18076 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018077 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018079#ifdef FEAT_VISUAL
18080 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18081 {
18082 if (VIsual_active)
18083 return &VIsual;
18084 return &curwin->w_cursor;
18085 }
18086#endif
18087 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018089 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18091 return NULL;
18092 return pp;
18093 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018094
18095#ifdef FEAT_VIRTUALEDIT
18096 pos.coladd = 0;
18097#endif
18098
Bram Moolenaar477933c2007-07-17 14:32:23 +000018099 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018100 {
18101 pos.col = 0;
18102 if (name[1] == '0') /* "w0": first visible line */
18103 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018104 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018105 pos.lnum = curwin->w_topline;
18106 return &pos;
18107 }
18108 else if (name[1] == '$') /* "w$": last visible line */
18109 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018110 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018111 pos.lnum = curwin->w_botline - 1;
18112 return &pos;
18113 }
18114 }
18115 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018117 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 {
18119 pos.lnum = curbuf->b_ml.ml_line_count;
18120 pos.col = 0;
18121 }
18122 else
18123 {
18124 pos.lnum = curwin->w_cursor.lnum;
18125 pos.col = (colnr_T)STRLEN(ml_get_curline());
18126 }
18127 return &pos;
18128 }
18129 return NULL;
18130}
18131
18132/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018133 * Convert list in "arg" into a position and optional file number.
18134 * When "fnump" is NULL there is no file number, only 3 items.
18135 * Note that the column is passed on as-is, the caller may want to decrement
18136 * it to use 1 for the first column.
18137 * Return FAIL when conversion is not possible, doesn't check the position for
18138 * validity.
18139 */
18140 static int
18141list2fpos(arg, posp, fnump)
18142 typval_T *arg;
18143 pos_T *posp;
18144 int *fnump;
18145{
18146 list_T *l = arg->vval.v_list;
18147 long i = 0;
18148 long n;
18149
Bram Moolenaarbde35262006-07-23 20:12:24 +000018150 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18151 * when "fnump" isn't NULL and "coladd" is optional. */
18152 if (arg->v_type != VAR_LIST
18153 || l == NULL
18154 || l->lv_len < (fnump == NULL ? 2 : 3)
18155 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018156 return FAIL;
18157
18158 if (fnump != NULL)
18159 {
18160 n = list_find_nr(l, i++, NULL); /* fnum */
18161 if (n < 0)
18162 return FAIL;
18163 if (n == 0)
18164 n = curbuf->b_fnum; /* current buffer */
18165 *fnump = n;
18166 }
18167
18168 n = list_find_nr(l, i++, NULL); /* lnum */
18169 if (n < 0)
18170 return FAIL;
18171 posp->lnum = n;
18172
18173 n = list_find_nr(l, i++, NULL); /* col */
18174 if (n < 0)
18175 return FAIL;
18176 posp->col = n;
18177
18178#ifdef FEAT_VIRTUALEDIT
18179 n = list_find_nr(l, i, NULL);
18180 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018181 posp->coladd = 0;
18182 else
18183 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018184#endif
18185
18186 return OK;
18187}
18188
18189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 * Get the length of an environment variable name.
18191 * Advance "arg" to the first character after the name.
18192 * Return 0 for error.
18193 */
18194 static int
18195get_env_len(arg)
18196 char_u **arg;
18197{
18198 char_u *p;
18199 int len;
18200
18201 for (p = *arg; vim_isIDc(*p); ++p)
18202 ;
18203 if (p == *arg) /* no name found */
18204 return 0;
18205
18206 len = (int)(p - *arg);
18207 *arg = p;
18208 return len;
18209}
18210
18211/*
18212 * Get the length of the name of a function or internal variable.
18213 * "arg" is advanced to the first non-white character after the name.
18214 * Return 0 if something is wrong.
18215 */
18216 static int
18217get_id_len(arg)
18218 char_u **arg;
18219{
18220 char_u *p;
18221 int len;
18222
18223 /* Find the end of the name. */
18224 for (p = *arg; eval_isnamec(*p); ++p)
18225 ;
18226 if (p == *arg) /* no name found */
18227 return 0;
18228
18229 len = (int)(p - *arg);
18230 *arg = skipwhite(p);
18231
18232 return len;
18233}
18234
18235/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018236 * Get the length of the name of a variable or function.
18237 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018239 * Return -1 if curly braces expansion failed.
18240 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 * If the name contains 'magic' {}'s, expand them and return the
18242 * expanded name in an allocated string via 'alias' - caller must free.
18243 */
18244 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018245get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 char_u **arg;
18247 char_u **alias;
18248 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018249 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250{
18251 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 char_u *p;
18253 char_u *expr_start;
18254 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255
18256 *alias = NULL; /* default to no alias */
18257
18258 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18259 && (*arg)[2] == (int)KE_SNR)
18260 {
18261 /* hard coded <SNR>, already translated */
18262 *arg += 3;
18263 return get_id_len(arg) + 3;
18264 }
18265 len = eval_fname_script(*arg);
18266 if (len > 0)
18267 {
18268 /* literal "<SID>", "s:" or "<SNR>" */
18269 *arg += len;
18270 }
18271
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018273 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018275 p = find_name_end(*arg, &expr_start, &expr_end,
18276 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277 if (expr_start != NULL)
18278 {
18279 char_u *temp_string;
18280
18281 if (!evaluate)
18282 {
18283 len += (int)(p - *arg);
18284 *arg = skipwhite(p);
18285 return len;
18286 }
18287
18288 /*
18289 * Include any <SID> etc in the expanded string:
18290 * Thus the -len here.
18291 */
18292 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18293 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018294 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 *alias = temp_string;
18296 *arg = skipwhite(p);
18297 return (int)STRLEN(temp_string);
18298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299
18300 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018301 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 EMSG2(_(e_invexpr2), *arg);
18303
18304 return len;
18305}
18306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018307/*
18308 * Find the end of a variable or function name, taking care of magic braces.
18309 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18310 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018311 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018312 * Return a pointer to just after the name. Equal to "arg" if there is no
18313 * valid name.
18314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018316find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 char_u *arg;
18318 char_u **expr_start;
18319 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018320 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018322 int mb_nest = 0;
18323 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324 char_u *p;
18325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018326 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018328 *expr_start = NULL;
18329 *expr_end = NULL;
18330 }
18331
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018332 /* Quick check for valid starting character. */
18333 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18334 return arg;
18335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018336 for (p = arg; *p != NUL
18337 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018338 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018339 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018341 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018342 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018343 if (*p == '\'')
18344 {
18345 /* skip over 'string' to avoid counting [ and ] inside it. */
18346 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18347 ;
18348 if (*p == NUL)
18349 break;
18350 }
18351 else if (*p == '"')
18352 {
18353 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18354 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18355 if (*p == '\\' && p[1] != NUL)
18356 ++p;
18357 if (*p == NUL)
18358 break;
18359 }
18360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018361 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018363 if (*p == '[')
18364 ++br_nest;
18365 else if (*p == ']')
18366 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018369 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018371 if (*p == '{')
18372 {
18373 mb_nest++;
18374 if (expr_start != NULL && *expr_start == NULL)
18375 *expr_start = p;
18376 }
18377 else if (*p == '}')
18378 {
18379 mb_nest--;
18380 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18381 *expr_end = p;
18382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 }
18385
18386 return p;
18387}
18388
18389/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018390 * Expands out the 'magic' {}'s in a variable/function name.
18391 * Note that this can call itself recursively, to deal with
18392 * constructs like foo{bar}{baz}{bam}
18393 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18394 * "in_start" ^
18395 * "expr_start" ^
18396 * "expr_end" ^
18397 * "in_end" ^
18398 *
18399 * Returns a new allocated string, which the caller must free.
18400 * Returns NULL for failure.
18401 */
18402 static char_u *
18403make_expanded_name(in_start, expr_start, expr_end, in_end)
18404 char_u *in_start;
18405 char_u *expr_start;
18406 char_u *expr_end;
18407 char_u *in_end;
18408{
18409 char_u c1;
18410 char_u *retval = NULL;
18411 char_u *temp_result;
18412 char_u *nextcmd = NULL;
18413
18414 if (expr_end == NULL || in_end == NULL)
18415 return NULL;
18416 *expr_start = NUL;
18417 *expr_end = NUL;
18418 c1 = *in_end;
18419 *in_end = NUL;
18420
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018421 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018422 if (temp_result != NULL && nextcmd == NULL)
18423 {
18424 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18425 + (in_end - expr_end) + 1));
18426 if (retval != NULL)
18427 {
18428 STRCPY(retval, in_start);
18429 STRCAT(retval, temp_result);
18430 STRCAT(retval, expr_end + 1);
18431 }
18432 }
18433 vim_free(temp_result);
18434
18435 *in_end = c1; /* put char back for error messages */
18436 *expr_start = '{';
18437 *expr_end = '}';
18438
18439 if (retval != NULL)
18440 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018441 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018442 if (expr_start != NULL)
18443 {
18444 /* Further expansion! */
18445 temp_result = make_expanded_name(retval, expr_start,
18446 expr_end, temp_result);
18447 vim_free(retval);
18448 retval = temp_result;
18449 }
18450 }
18451
18452 return retval;
18453}
18454
18455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018457 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 */
18459 static int
18460eval_isnamec(c)
18461 int c;
18462{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018463 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18464}
18465
18466/*
18467 * Return TRUE if character "c" can be used as the first character in a
18468 * variable or function name (excluding '{' and '}').
18469 */
18470 static int
18471eval_isnamec1(c)
18472 int c;
18473{
18474 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475}
18476
18477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 * Set number v: variable to "val".
18479 */
18480 void
18481set_vim_var_nr(idx, val)
18482 int idx;
18483 long val;
18484{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018485 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486}
18487
18488/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018489 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490 */
18491 long
18492get_vim_var_nr(idx)
18493 int idx;
18494{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018495 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496}
18497
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018498/*
18499 * Get string v: variable value. Uses a static buffer, can only be used once.
18500 */
18501 char_u *
18502get_vim_var_str(idx)
18503 int idx;
18504{
18505 return get_tv_string(&vimvars[idx].vv_tv);
18506}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018507
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018509 * Get List v: variable value. Caller must take care of reference count when
18510 * needed.
18511 */
18512 list_T *
18513get_vim_var_list(idx)
18514 int idx;
18515{
18516 return vimvars[idx].vv_list;
18517}
18518
18519/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018520 * Set v:char to character "c".
18521 */
18522 void
18523set_vim_var_char(c)
18524 int c;
18525{
18526#ifdef FEAT_MBYTE
18527 char_u buf[MB_MAXBYTES];
18528#else
18529 char_u buf[2];
18530#endif
18531
18532#ifdef FEAT_MBYTE
18533 if (has_mbyte)
18534 buf[(*mb_char2bytes)(c, buf)] = NUL;
18535 else
18536#endif
18537 {
18538 buf[0] = c;
18539 buf[1] = NUL;
18540 }
18541 set_vim_var_string(VV_CHAR, buf, -1);
18542}
18543
18544/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018545 * Set v:count to "count" and v:count1 to "count1".
18546 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 */
18548 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018549set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 long count;
18551 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018552 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018554 if (set_prevcount)
18555 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018556 vimvars[VV_COUNT].vv_nr = count;
18557 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558}
18559
18560/*
18561 * Set string v: variable to a copy of "val".
18562 */
18563 void
18564set_vim_var_string(idx, val, len)
18565 int idx;
18566 char_u *val;
18567 int len; /* length of "val" to use or -1 (whole string) */
18568{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018569 /* Need to do this (at least) once, since we can't initialize a union.
18570 * Will always be invoked when "v:progname" is set. */
18571 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18572
Bram Moolenaare9a41262005-01-15 22:18:47 +000018573 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018575 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018577 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018579 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580}
18581
18582/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018583 * Set List v: variable to "val".
18584 */
18585 void
18586set_vim_var_list(idx, val)
18587 int idx;
18588 list_T *val;
18589{
18590 list_unref(vimvars[idx].vv_list);
18591 vimvars[idx].vv_list = val;
18592 if (val != NULL)
18593 ++val->lv_refcount;
18594}
18595
18596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 * Set v:register if needed.
18598 */
18599 void
18600set_reg_var(c)
18601 int c;
18602{
18603 char_u regname;
18604
18605 if (c == 0 || c == ' ')
18606 regname = '"';
18607 else
18608 regname = c;
18609 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018610 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 set_vim_var_string(VV_REG, &regname, 1);
18612}
18613
18614/*
18615 * Get or set v:exception. If "oldval" == NULL, return the current value.
18616 * Otherwise, restore the value to "oldval" and return NULL.
18617 * Must always be called in pairs to save and restore v:exception! Does not
18618 * take care of memory allocations.
18619 */
18620 char_u *
18621v_exception(oldval)
18622 char_u *oldval;
18623{
18624 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018625 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626
Bram Moolenaare9a41262005-01-15 22:18:47 +000018627 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 return NULL;
18629}
18630
18631/*
18632 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18633 * Otherwise, restore the value to "oldval" and return NULL.
18634 * Must always be called in pairs to save and restore v:throwpoint! Does not
18635 * take care of memory allocations.
18636 */
18637 char_u *
18638v_throwpoint(oldval)
18639 char_u *oldval;
18640{
18641 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018642 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643
Bram Moolenaare9a41262005-01-15 22:18:47 +000018644 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 return NULL;
18646}
18647
18648#if defined(FEAT_AUTOCMD) || defined(PROTO)
18649/*
18650 * Set v:cmdarg.
18651 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18652 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18653 * Must always be called in pairs!
18654 */
18655 char_u *
18656set_cmdarg(eap, oldarg)
18657 exarg_T *eap;
18658 char_u *oldarg;
18659{
18660 char_u *oldval;
18661 char_u *newval;
18662 unsigned len;
18663
Bram Moolenaare9a41262005-01-15 22:18:47 +000018664 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018665 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018667 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018668 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018669 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 }
18671
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018672 if (eap->force_bin == FORCE_BIN)
18673 len = 6;
18674 else if (eap->force_bin == FORCE_NOBIN)
18675 len = 8;
18676 else
18677 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018678
18679 if (eap->read_edit)
18680 len += 7;
18681
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018682 if (eap->force_ff != 0)
18683 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18684# ifdef FEAT_MBYTE
18685 if (eap->force_enc != 0)
18686 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018687 if (eap->bad_char != 0)
18688 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018689# endif
18690
18691 newval = alloc(len + 1);
18692 if (newval == NULL)
18693 return NULL;
18694
18695 if (eap->force_bin == FORCE_BIN)
18696 sprintf((char *)newval, " ++bin");
18697 else if (eap->force_bin == FORCE_NOBIN)
18698 sprintf((char *)newval, " ++nobin");
18699 else
18700 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018701
18702 if (eap->read_edit)
18703 STRCAT(newval, " ++edit");
18704
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018705 if (eap->force_ff != 0)
18706 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18707 eap->cmd + eap->force_ff);
18708# ifdef FEAT_MBYTE
18709 if (eap->force_enc != 0)
18710 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18711 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018712 if (eap->bad_char == BAD_KEEP)
18713 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18714 else if (eap->bad_char == BAD_DROP)
18715 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18716 else if (eap->bad_char != 0)
18717 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018718# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018719 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018720 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721}
18722#endif
18723
18724/*
18725 * Get the value of internal variable "name".
18726 * Return OK or FAIL.
18727 */
18728 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018729get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 char_u *name;
18731 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018732 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018733 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734{
18735 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018736 typval_T *tv = NULL;
18737 typval_T atv;
18738 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740
18741 /* truncate the name, so that we can use strcmp() */
18742 cc = name[len];
18743 name[len] = NUL;
18744
18745 /*
18746 * Check for "b:changedtick".
18747 */
18748 if (STRCMP(name, "b:changedtick") == 0)
18749 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018750 atv.v_type = VAR_NUMBER;
18751 atv.vval.v_number = curbuf->b_changedtick;
18752 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 }
18754
18755 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 * Check for user-defined variables.
18757 */
18758 else
18759 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018760 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018762 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 }
18764
Bram Moolenaare9a41262005-01-15 22:18:47 +000018765 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018767 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018768 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 ret = FAIL;
18770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018771 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018772 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773
18774 name[len] = cc;
18775
18776 return ret;
18777}
18778
18779/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018780 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18781 * Also handle function call with Funcref variable: func(expr)
18782 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18783 */
18784 static int
18785handle_subscript(arg, rettv, evaluate, verbose)
18786 char_u **arg;
18787 typval_T *rettv;
18788 int evaluate; /* do more than finding the end */
18789 int verbose; /* give error messages */
18790{
18791 int ret = OK;
18792 dict_T *selfdict = NULL;
18793 char_u *s;
18794 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018795 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018796
18797 while (ret == OK
18798 && (**arg == '['
18799 || (**arg == '.' && rettv->v_type == VAR_DICT)
18800 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18801 && !vim_iswhite(*(*arg - 1)))
18802 {
18803 if (**arg == '(')
18804 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018805 /* need to copy the funcref so that we can clear rettv */
18806 functv = *rettv;
18807 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018808
18809 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018810 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018811 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018812 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18813 &len, evaluate, selfdict);
18814
18815 /* Clear the funcref afterwards, so that deleting it while
18816 * evaluating the arguments is possible (see test55). */
18817 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018818
18819 /* Stop the expression evaluation when immediately aborting on
18820 * error, or when an interrupt occurred or an exception was thrown
18821 * but not caught. */
18822 if (aborting())
18823 {
18824 if (ret == OK)
18825 clear_tv(rettv);
18826 ret = FAIL;
18827 }
18828 dict_unref(selfdict);
18829 selfdict = NULL;
18830 }
18831 else /* **arg == '[' || **arg == '.' */
18832 {
18833 dict_unref(selfdict);
18834 if (rettv->v_type == VAR_DICT)
18835 {
18836 selfdict = rettv->vval.v_dict;
18837 if (selfdict != NULL)
18838 ++selfdict->dv_refcount;
18839 }
18840 else
18841 selfdict = NULL;
18842 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18843 {
18844 clear_tv(rettv);
18845 ret = FAIL;
18846 }
18847 }
18848 }
18849 dict_unref(selfdict);
18850 return ret;
18851}
18852
18853/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018854 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018855 * value).
18856 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018857 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018858alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018859{
Bram Moolenaar33570922005-01-25 22:26:29 +000018860 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018861}
18862
18863/*
18864 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 * The string "s" must have been allocated, it is consumed.
18866 * Return NULL for out of memory, the variable otherwise.
18867 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018868 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018869alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 char_u *s;
18871{
Bram Moolenaar33570922005-01-25 22:26:29 +000018872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018874 rettv = alloc_tv();
18875 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018877 rettv->v_type = VAR_STRING;
18878 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 }
18880 else
18881 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018882 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883}
18884
18885/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018886 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018888 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018889free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018890 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891{
18892 if (varp != NULL)
18893 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018894 switch (varp->v_type)
18895 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018896 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018897 func_unref(varp->vval.v_string);
18898 /*FALLTHROUGH*/
18899 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018900 vim_free(varp->vval.v_string);
18901 break;
18902 case VAR_LIST:
18903 list_unref(varp->vval.v_list);
18904 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018905 case VAR_DICT:
18906 dict_unref(varp->vval.v_dict);
18907 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018908 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018909#ifdef FEAT_FLOAT
18910 case VAR_FLOAT:
18911#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018912 case VAR_UNKNOWN:
18913 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018914 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018915 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018916 break;
18917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 vim_free(varp);
18919 }
18920}
18921
18922/*
18923 * Free the memory for a variable value and set the value to NULL or 0.
18924 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018925 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018926clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018927 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928{
18929 if (varp != NULL)
18930 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018931 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018933 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018934 func_unref(varp->vval.v_string);
18935 /*FALLTHROUGH*/
18936 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018937 vim_free(varp->vval.v_string);
18938 varp->vval.v_string = NULL;
18939 break;
18940 case VAR_LIST:
18941 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018942 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018943 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018944 case VAR_DICT:
18945 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018946 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018947 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018948 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018949 varp->vval.v_number = 0;
18950 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018951#ifdef FEAT_FLOAT
18952 case VAR_FLOAT:
18953 varp->vval.v_float = 0.0;
18954 break;
18955#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018956 case VAR_UNKNOWN:
18957 break;
18958 default:
18959 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018961 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 }
18963}
18964
18965/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018966 * Set the value of a variable to NULL without freeing items.
18967 */
18968 static void
18969init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018970 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018971{
18972 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018973 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018974}
18975
18976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 * Get the number value of a variable.
18978 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018979 * For incompatible types, return 0.
18980 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18981 * caller of incompatible types: it sets *denote to TRUE if "denote"
18982 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983 */
18984 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018985get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018986 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018988 int error = FALSE;
18989
18990 return get_tv_number_chk(varp, &error); /* return 0L on error */
18991}
18992
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018993 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018994get_tv_number_chk(varp, denote)
18995 typval_T *varp;
18996 int *denote;
18997{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018998 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019000 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019002 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019003 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019004#ifdef FEAT_FLOAT
19005 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019006 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019007 break;
19008#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019009 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019010 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019011 break;
19012 case VAR_STRING:
19013 if (varp->vval.v_string != NULL)
19014 vim_str2nr(varp->vval.v_string, NULL, NULL,
19015 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019016 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019017 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019018 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019019 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019020 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019021 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019022 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019023 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019024 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019025 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019027 if (denote == NULL) /* useful for values that must be unsigned */
19028 n = -1;
19029 else
19030 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019031 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032}
19033
19034/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019035 * Get the lnum from the first argument.
19036 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019037 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 */
19039 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019040get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019041 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042{
Bram Moolenaar33570922005-01-25 22:26:29 +000019043 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 linenr_T lnum;
19045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019046 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 if (lnum == 0) /* no valid number, try using line() */
19048 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019049 rettv.v_type = VAR_NUMBER;
19050 f_line(argvars, &rettv);
19051 lnum = rettv.vval.v_number;
19052 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053 }
19054 return lnum;
19055}
19056
19057/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019058 * Get the lnum from the first argument.
19059 * Also accepts "$", then "buf" is used.
19060 * Returns 0 on error.
19061 */
19062 static linenr_T
19063get_tv_lnum_buf(argvars, buf)
19064 typval_T *argvars;
19065 buf_T *buf;
19066{
19067 if (argvars[0].v_type == VAR_STRING
19068 && argvars[0].vval.v_string != NULL
19069 && argvars[0].vval.v_string[0] == '$'
19070 && buf != NULL)
19071 return buf->b_ml.ml_line_count;
19072 return get_tv_number_chk(&argvars[0], NULL);
19073}
19074
19075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 * Get the string value of a variable.
19077 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019078 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19079 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 * If the String variable has never been set, return an empty string.
19081 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019082 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19083 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 */
19085 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019086get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019087 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088{
19089 static char_u mybuf[NUMBUFLEN];
19090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019091 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092}
19093
19094 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019096 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019097 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019099 char_u *res = get_tv_string_buf_chk(varp, buf);
19100
19101 return res != NULL ? res : (char_u *)"";
19102}
19103
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019104 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019105get_tv_string_chk(varp)
19106 typval_T *varp;
19107{
19108 static char_u mybuf[NUMBUFLEN];
19109
19110 return get_tv_string_buf_chk(varp, mybuf);
19111}
19112
19113 static char_u *
19114get_tv_string_buf_chk(varp, buf)
19115 typval_T *varp;
19116 char_u *buf;
19117{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019118 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019120 case VAR_NUMBER:
19121 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19122 return buf;
19123 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019124 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019125 break;
19126 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019127 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019128 break;
19129 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019130 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019131 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019132#ifdef FEAT_FLOAT
19133 case VAR_FLOAT:
19134 EMSG(_("E806: using Float as a String"));
19135 break;
19136#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019137 case VAR_STRING:
19138 if (varp->vval.v_string != NULL)
19139 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019140 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019141 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019142 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019145 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146}
19147
19148/*
19149 * Find variable "name" in the list of variables.
19150 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019151 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019152 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019153 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019155 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019156find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019158 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019161 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162
Bram Moolenaara7043832005-01-21 11:56:39 +000019163 ht = find_var_ht(name, &varname);
19164 if (htp != NULL)
19165 *htp = ht;
19166 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019167 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019168 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169}
19170
19171/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019172 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019173 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019175 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019176find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019177 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019178 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019179 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019180{
Bram Moolenaar33570922005-01-25 22:26:29 +000019181 hashitem_T *hi;
19182
19183 if (*varname == NUL)
19184 {
19185 /* Must be something like "s:", otherwise "ht" would be NULL. */
19186 switch (varname[-2])
19187 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019188 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019189 case 'g': return &globvars_var;
19190 case 'v': return &vimvars_var;
19191 case 'b': return &curbuf->b_bufvar;
19192 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019193#ifdef FEAT_WINDOWS
19194 case 't': return &curtab->tp_winvar;
19195#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019196 case 'l': return current_funccal == NULL
19197 ? NULL : &current_funccal->l_vars_var;
19198 case 'a': return current_funccal == NULL
19199 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019200 }
19201 return NULL;
19202 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019203
19204 hi = hash_find(ht, varname);
19205 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019206 {
19207 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019208 * worked find the variable again. Don't auto-load a script if it was
19209 * loaded already, otherwise it would be loaded every time when
19210 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019211 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019212 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019213 hi = hash_find(ht, varname);
19214 if (HASHITEM_EMPTY(hi))
19215 return NULL;
19216 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019217 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019218}
19219
19220/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019221 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019222 * Set "varname" to the start of name without ':'.
19223 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019224 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019225find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 char_u *name;
19227 char_u **varname;
19228{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019229 hashitem_T *hi;
19230
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 if (name[1] != ':')
19232 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019233 /* The name must not start with a colon or #. */
19234 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 return NULL;
19236 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019237
19238 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019239 hi = hash_find(&compat_hashtab, name);
19240 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019241 return &compat_hashtab;
19242
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019244 return &globvarht; /* global variable */
19245 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 }
19247 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019248 if (*name == 'g') /* global variable */
19249 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019250 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19251 */
19252 if (vim_strchr(name + 2, ':') != NULL
19253 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019254 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019256 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019258 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019259#ifdef FEAT_WINDOWS
19260 if (*name == 't') /* tab page variable */
19261 return &curtab->tp_vars.dv_hashtab;
19262#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019263 if (*name == 'v') /* v: variable */
19264 return &vimvarht;
19265 if (*name == 'a' && current_funccal != NULL) /* function argument */
19266 return &current_funccal->l_avars.dv_hashtab;
19267 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19268 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 if (*name == 's' /* script variable */
19270 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19271 return &SCRIPT_VARS(current_SID);
19272 return NULL;
19273}
19274
19275/*
19276 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019277 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 * Returns NULL when it doesn't exist.
19279 */
19280 char_u *
19281get_var_value(name)
19282 char_u *name;
19283{
Bram Moolenaar33570922005-01-25 22:26:29 +000019284 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285
Bram Moolenaara7043832005-01-21 11:56:39 +000019286 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 if (v == NULL)
19288 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019289 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290}
19291
19292/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019293 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 * sourcing this script and when executing functions defined in the script.
19295 */
19296 void
19297new_script_vars(id)
19298 scid_T id;
19299{
Bram Moolenaara7043832005-01-21 11:56:39 +000019300 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019301 hashtab_T *ht;
19302 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019303
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19305 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019306 /* Re-allocating ga_data means that an ht_array pointing to
19307 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019309 for (i = 1; i <= ga_scripts.ga_len; ++i)
19310 {
19311 ht = &SCRIPT_VARS(i);
19312 if (ht->ht_mask == HT_INIT_SIZE - 1)
19313 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019314 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019315 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019316 }
19317
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318 while (ga_scripts.ga_len < id)
19319 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019320 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019321 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019322 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 }
19325 }
19326}
19327
19328/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019329 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19330 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331 */
19332 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019333init_var_dict(dict, dict_var)
19334 dict_T *dict;
19335 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336{
Bram Moolenaar33570922005-01-25 22:26:29 +000019337 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019338 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019339 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019340 dict_var->di_tv.vval.v_dict = dict;
19341 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019342 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019343 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19344 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345}
19346
19347/*
19348 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019349 * Frees all allocated variables and the value they contain.
19350 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 */
19352 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019353vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019354 hashtab_T *ht;
19355{
19356 vars_clear_ext(ht, TRUE);
19357}
19358
19359/*
19360 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19361 */
19362 static void
19363vars_clear_ext(ht, free_val)
19364 hashtab_T *ht;
19365 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366{
Bram Moolenaara7043832005-01-21 11:56:39 +000019367 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019368 hashitem_T *hi;
19369 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370
Bram Moolenaar33570922005-01-25 22:26:29 +000019371 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019372 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019373 for (hi = ht->ht_array; todo > 0; ++hi)
19374 {
19375 if (!HASHITEM_EMPTY(hi))
19376 {
19377 --todo;
19378
Bram Moolenaar33570922005-01-25 22:26:29 +000019379 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019380 * ht_array might change then. hash_clear() takes care of it
19381 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019382 v = HI2DI(hi);
19383 if (free_val)
19384 clear_tv(&v->di_tv);
19385 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19386 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019387 }
19388 }
19389 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019390 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391}
19392
Bram Moolenaara7043832005-01-21 11:56:39 +000019393/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019394 * Delete a variable from hashtab "ht" at item "hi".
19395 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019398delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019399 hashtab_T *ht;
19400 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401{
Bram Moolenaar33570922005-01-25 22:26:29 +000019402 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019403
19404 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019405 clear_tv(&di->di_tv);
19406 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407}
19408
19409/*
19410 * List the value of one internal variable.
19411 */
19412 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019413list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019414 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019416 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019418 char_u *tofree;
19419 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019420 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019421
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019422 current_copyID += COPYID_INC;
19423 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019424 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019425 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019426 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427}
19428
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019430list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 char_u *prefix;
19432 char_u *name;
19433 int type;
19434 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019435 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436{
Bram Moolenaar31859182007-08-14 20:41:13 +000019437 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19438 msg_start();
19439 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 if (name != NULL) /* "a:" vars don't have a name stored */
19441 msg_puts(name);
19442 msg_putchar(' ');
19443 msg_advance(22);
19444 if (type == VAR_NUMBER)
19445 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019446 else if (type == VAR_FUNC)
19447 msg_putchar('*');
19448 else if (type == VAR_LIST)
19449 {
19450 msg_putchar('[');
19451 if (*string == '[')
19452 ++string;
19453 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019454 else if (type == VAR_DICT)
19455 {
19456 msg_putchar('{');
19457 if (*string == '{')
19458 ++string;
19459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 else
19461 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019462
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019464
19465 if (type == VAR_FUNC)
19466 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019467 if (*first)
19468 {
19469 msg_clr_eos();
19470 *first = FALSE;
19471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472}
19473
19474/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019475 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 * If the variable already exists, the value is updated.
19477 * Otherwise the variable is created.
19478 */
19479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019480set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019482 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019483 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484{
Bram Moolenaar33570922005-01-25 22:26:29 +000019485 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019487 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019488 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019490 ht = find_var_ht(name, &varname);
19491 if (ht == NULL || *varname == NUL)
19492 {
19493 EMSG2(_(e_illvar), name);
19494 return;
19495 }
19496 v = find_var_in_ht(ht, varname, TRUE);
19497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019498 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019499 {
19500 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19501 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19502 ? name[2] : name[0]))
19503 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019504 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019505 return;
19506 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019507 /* Don't allow hiding a function. When "v" is not NULL we migth be
19508 * assigning another function to the same var, the type is checked
19509 * below. */
19510 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019511 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019512 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019513 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019514 return;
19515 }
19516 }
19517
Bram Moolenaar33570922005-01-25 22:26:29 +000019518 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019520 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019521 if (var_check_ro(v->di_flags, name)
19522 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019523 return;
19524 if (v->di_tv.v_type != tv->v_type
19525 && !((v->di_tv.v_type == VAR_STRING
19526 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019527 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019528 || tv->v_type == VAR_NUMBER))
19529#ifdef FEAT_FLOAT
19530 && !((v->di_tv.v_type == VAR_NUMBER
19531 || v->di_tv.v_type == VAR_FLOAT)
19532 && (tv->v_type == VAR_NUMBER
19533 || tv->v_type == VAR_FLOAT))
19534#endif
19535 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019536 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019537 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019538 return;
19539 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019540
19541 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019542 * Handle setting internal v: variables separately: we don't change
19543 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019544 */
19545 if (ht == &vimvarht)
19546 {
19547 if (v->di_tv.v_type == VAR_STRING)
19548 {
19549 vim_free(v->di_tv.vval.v_string);
19550 if (copy || tv->v_type != VAR_STRING)
19551 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19552 else
19553 {
19554 /* Take over the string to avoid an extra alloc/free. */
19555 v->di_tv.vval.v_string = tv->vval.v_string;
19556 tv->vval.v_string = NULL;
19557 }
19558 }
19559 else if (v->di_tv.v_type != VAR_NUMBER)
19560 EMSG2(_(e_intern2), "set_var()");
19561 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019562 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019563 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019564 if (STRCMP(varname, "searchforward") == 0)
19565 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19566 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019567 return;
19568 }
19569
19570 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 }
19572 else /* add a new variable */
19573 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019574 /* Can't add "v:" variable. */
19575 if (ht == &vimvarht)
19576 {
19577 EMSG2(_(e_illvar), name);
19578 return;
19579 }
19580
Bram Moolenaar92124a32005-06-17 22:03:40 +000019581 /* Make sure the variable name is valid. */
19582 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019583 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19584 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019585 {
19586 EMSG2(_(e_illvar), varname);
19587 return;
19588 }
19589
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019590 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19591 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019592 if (v == NULL)
19593 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019594 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019597 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 return;
19599 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019600 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019602
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019603 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019604 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019605 else
19606 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019607 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019608 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611}
19612
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019613/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019614 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 * Also give an error message.
19616 */
19617 static int
19618var_check_ro(flags, name)
19619 int flags;
19620 char_u *name;
19621{
19622 if (flags & DI_FLAGS_RO)
19623 {
19624 EMSG2(_(e_readonlyvar), name);
19625 return TRUE;
19626 }
19627 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19628 {
19629 EMSG2(_(e_readonlysbx), name);
19630 return TRUE;
19631 }
19632 return FALSE;
19633}
19634
19635/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019636 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19637 * Also give an error message.
19638 */
19639 static int
19640var_check_fixed(flags, name)
19641 int flags;
19642 char_u *name;
19643{
19644 if (flags & DI_FLAGS_FIX)
19645 {
19646 EMSG2(_("E795: Cannot delete variable %s"), name);
19647 return TRUE;
19648 }
19649 return FALSE;
19650}
19651
19652/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019653 * Return TRUE if typeval "tv" is set to be locked (immutable).
19654 * Also give an error message, using "name".
19655 */
19656 static int
19657tv_check_lock(lock, name)
19658 int lock;
19659 char_u *name;
19660{
19661 if (lock & VAR_LOCKED)
19662 {
19663 EMSG2(_("E741: Value is locked: %s"),
19664 name == NULL ? (char_u *)_("Unknown") : name);
19665 return TRUE;
19666 }
19667 if (lock & VAR_FIXED)
19668 {
19669 EMSG2(_("E742: Cannot change value of %s"),
19670 name == NULL ? (char_u *)_("Unknown") : name);
19671 return TRUE;
19672 }
19673 return FALSE;
19674}
19675
19676/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019678 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019679 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019680 * It is OK for "from" and "to" to point to the same item. This is used to
19681 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019682 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019683 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019684copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019685 typval_T *from;
19686 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019688 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019689 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019690 switch (from->v_type)
19691 {
19692 case VAR_NUMBER:
19693 to->vval.v_number = from->vval.v_number;
19694 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019695#ifdef FEAT_FLOAT
19696 case VAR_FLOAT:
19697 to->vval.v_float = from->vval.v_float;
19698 break;
19699#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019700 case VAR_STRING:
19701 case VAR_FUNC:
19702 if (from->vval.v_string == NULL)
19703 to->vval.v_string = NULL;
19704 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019705 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019706 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019707 if (from->v_type == VAR_FUNC)
19708 func_ref(to->vval.v_string);
19709 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019710 break;
19711 case VAR_LIST:
19712 if (from->vval.v_list == NULL)
19713 to->vval.v_list = NULL;
19714 else
19715 {
19716 to->vval.v_list = from->vval.v_list;
19717 ++to->vval.v_list->lv_refcount;
19718 }
19719 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019720 case VAR_DICT:
19721 if (from->vval.v_dict == NULL)
19722 to->vval.v_dict = NULL;
19723 else
19724 {
19725 to->vval.v_dict = from->vval.v_dict;
19726 ++to->vval.v_dict->dv_refcount;
19727 }
19728 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019729 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019730 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019731 break;
19732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733}
19734
19735/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019736 * Make a copy of an item.
19737 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019738 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19739 * reference to an already copied list/dict can be used.
19740 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019741 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019742 static int
19743item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019744 typval_T *from;
19745 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019746 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019747 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019748{
19749 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019750 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019751
Bram Moolenaar33570922005-01-25 22:26:29 +000019752 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019753 {
19754 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019755 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019756 }
19757 ++recurse;
19758
19759 switch (from->v_type)
19760 {
19761 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019762#ifdef FEAT_FLOAT
19763 case VAR_FLOAT:
19764#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019765 case VAR_STRING:
19766 case VAR_FUNC:
19767 copy_tv(from, to);
19768 break;
19769 case VAR_LIST:
19770 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019771 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019772 if (from->vval.v_list == NULL)
19773 to->vval.v_list = NULL;
19774 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19775 {
19776 /* use the copy made earlier */
19777 to->vval.v_list = from->vval.v_list->lv_copylist;
19778 ++to->vval.v_list->lv_refcount;
19779 }
19780 else
19781 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19782 if (to->vval.v_list == NULL)
19783 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019784 break;
19785 case VAR_DICT:
19786 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019787 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019788 if (from->vval.v_dict == NULL)
19789 to->vval.v_dict = NULL;
19790 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19791 {
19792 /* use the copy made earlier */
19793 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19794 ++to->vval.v_dict->dv_refcount;
19795 }
19796 else
19797 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19798 if (to->vval.v_dict == NULL)
19799 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019800 break;
19801 default:
19802 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019803 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019804 }
19805 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019806 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019807}
19808
19809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 * ":echo expr1 ..." print each argument separated with a space, add a
19811 * newline at the end.
19812 * ":echon expr1 ..." print each argument plain.
19813 */
19814 void
19815ex_echo(eap)
19816 exarg_T *eap;
19817{
19818 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019819 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019820 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 char_u *p;
19822 int needclr = TRUE;
19823 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019824 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825
19826 if (eap->skip)
19827 ++emsg_skip;
19828 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019830 /* If eval1() causes an error message the text from the command may
19831 * still need to be cleared. E.g., "echo 22,44". */
19832 need_clr_eos = needclr;
19833
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 {
19837 /*
19838 * Report the invalid expression unless the expression evaluation
19839 * has been cancelled due to an aborting error, an interrupt, or an
19840 * exception.
19841 */
19842 if (!aborting())
19843 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019844 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 break;
19846 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019847 need_clr_eos = FALSE;
19848
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 if (!eap->skip)
19850 {
19851 if (atstart)
19852 {
19853 atstart = FALSE;
19854 /* Call msg_start() after eval1(), evaluating the expression
19855 * may cause a message to appear. */
19856 if (eap->cmdidx == CMD_echo)
19857 msg_start();
19858 }
19859 else if (eap->cmdidx == CMD_echo)
19860 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019861 current_copyID += COPYID_INC;
19862 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019863 if (p != NULL)
19864 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019866 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019868 if (*p != TAB && needclr)
19869 {
19870 /* remove any text still there from the command */
19871 msg_clr_eos();
19872 needclr = FALSE;
19873 }
19874 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 }
19876 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019877 {
19878#ifdef FEAT_MBYTE
19879 if (has_mbyte)
19880 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019881 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019882
19883 (void)msg_outtrans_len_attr(p, i, echo_attr);
19884 p += i - 1;
19885 }
19886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019888 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019891 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 arg = skipwhite(arg);
19895 }
19896 eap->nextcmd = check_nextcmd(arg);
19897
19898 if (eap->skip)
19899 --emsg_skip;
19900 else
19901 {
19902 /* remove text that may still be there from the command */
19903 if (needclr)
19904 msg_clr_eos();
19905 if (eap->cmdidx == CMD_echo)
19906 msg_end();
19907 }
19908}
19909
19910/*
19911 * ":echohl {name}".
19912 */
19913 void
19914ex_echohl(eap)
19915 exarg_T *eap;
19916{
19917 int id;
19918
19919 id = syn_name2id(eap->arg);
19920 if (id == 0)
19921 echo_attr = 0;
19922 else
19923 echo_attr = syn_id2attr(id);
19924}
19925
19926/*
19927 * ":execute expr1 ..." execute the result of an expression.
19928 * ":echomsg expr1 ..." Print a message
19929 * ":echoerr expr1 ..." Print an error
19930 * Each gets spaces around each argument and a newline at the end for
19931 * echo commands
19932 */
19933 void
19934ex_execute(eap)
19935 exarg_T *eap;
19936{
19937 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019938 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 int ret = OK;
19940 char_u *p;
19941 garray_T ga;
19942 int len;
19943 int save_did_emsg;
19944
19945 ga_init2(&ga, 1, 80);
19946
19947 if (eap->skip)
19948 ++emsg_skip;
19949 while (*arg != NUL && *arg != '|' && *arg != '\n')
19950 {
19951 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019952 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 {
19954 /*
19955 * Report the invalid expression unless the expression evaluation
19956 * has been cancelled due to an aborting error, an interrupt, or an
19957 * exception.
19958 */
19959 if (!aborting())
19960 EMSG2(_(e_invexpr2), p);
19961 ret = FAIL;
19962 break;
19963 }
19964
19965 if (!eap->skip)
19966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019967 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 len = (int)STRLEN(p);
19969 if (ga_grow(&ga, len + 2) == FAIL)
19970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 ret = FAIL;
19973 break;
19974 }
19975 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 ga.ga_len += len;
19979 }
19980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019981 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 arg = skipwhite(arg);
19983 }
19984
19985 if (ret != FAIL && ga.ga_data != NULL)
19986 {
19987 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019990 out_flush();
19991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 else if (eap->cmdidx == CMD_echoerr)
19993 {
19994 /* We don't want to abort following commands, restore did_emsg. */
19995 save_did_emsg = did_emsg;
19996 EMSG((char_u *)ga.ga_data);
19997 if (!force_abort)
19998 did_emsg = save_did_emsg;
19999 }
20000 else if (eap->cmdidx == CMD_execute)
20001 do_cmdline((char_u *)ga.ga_data,
20002 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20003 }
20004
20005 ga_clear(&ga);
20006
20007 if (eap->skip)
20008 --emsg_skip;
20009
20010 eap->nextcmd = check_nextcmd(arg);
20011}
20012
20013/*
20014 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20015 * "arg" points to the "&" or '+' when called, to "option" when returning.
20016 * Returns NULL when no option name found. Otherwise pointer to the char
20017 * after the option name.
20018 */
20019 static char_u *
20020find_option_end(arg, opt_flags)
20021 char_u **arg;
20022 int *opt_flags;
20023{
20024 char_u *p = *arg;
20025
20026 ++p;
20027 if (*p == 'g' && p[1] == ':')
20028 {
20029 *opt_flags = OPT_GLOBAL;
20030 p += 2;
20031 }
20032 else if (*p == 'l' && p[1] == ':')
20033 {
20034 *opt_flags = OPT_LOCAL;
20035 p += 2;
20036 }
20037 else
20038 *opt_flags = 0;
20039
20040 if (!ASCII_ISALPHA(*p))
20041 return NULL;
20042 *arg = p;
20043
20044 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20045 p += 4; /* termcap option */
20046 else
20047 while (ASCII_ISALPHA(*p))
20048 ++p;
20049 return p;
20050}
20051
20052/*
20053 * ":function"
20054 */
20055 void
20056ex_function(eap)
20057 exarg_T *eap;
20058{
20059 char_u *theline;
20060 int j;
20061 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 char_u *name = NULL;
20064 char_u *p;
20065 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020066 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 garray_T newargs;
20068 garray_T newlines;
20069 int varargs = FALSE;
20070 int mustend = FALSE;
20071 int flags = 0;
20072 ufunc_T *fp;
20073 int indent;
20074 int nesting;
20075 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020076 dictitem_T *v;
20077 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020078 static int func_nr = 0; /* number for nameless function */
20079 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020080 hashtab_T *ht;
20081 int todo;
20082 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020083 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084
20085 /*
20086 * ":function" without argument: list functions.
20087 */
20088 if (ends_excmd(*eap->arg))
20089 {
20090 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020091 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020092 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020093 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020094 {
20095 if (!HASHITEM_EMPTY(hi))
20096 {
20097 --todo;
20098 fp = HI2UF(hi);
20099 if (!isdigit(*fp->uf_name))
20100 list_func_head(fp, FALSE);
20101 }
20102 }
20103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 eap->nextcmd = check_nextcmd(eap->arg);
20105 return;
20106 }
20107
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020108 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020109 * ":function /pat": list functions matching pattern.
20110 */
20111 if (*eap->arg == '/')
20112 {
20113 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20114 if (!eap->skip)
20115 {
20116 regmatch_T regmatch;
20117
20118 c = *p;
20119 *p = NUL;
20120 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20121 *p = c;
20122 if (regmatch.regprog != NULL)
20123 {
20124 regmatch.rm_ic = p_ic;
20125
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020126 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020127 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20128 {
20129 if (!HASHITEM_EMPTY(hi))
20130 {
20131 --todo;
20132 fp = HI2UF(hi);
20133 if (!isdigit(*fp->uf_name)
20134 && vim_regexec(&regmatch, fp->uf_name, 0))
20135 list_func_head(fp, FALSE);
20136 }
20137 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020138 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020139 }
20140 }
20141 if (*p == '/')
20142 ++p;
20143 eap->nextcmd = check_nextcmd(p);
20144 return;
20145 }
20146
20147 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020148 * Get the function name. There are these situations:
20149 * func normal function name
20150 * "name" == func, "fudi.fd_dict" == NULL
20151 * dict.func new dictionary entry
20152 * "name" == NULL, "fudi.fd_dict" set,
20153 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20154 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020155 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020156 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20157 * dict.func existing dict entry that's not a Funcref
20158 * "name" == NULL, "fudi.fd_dict" set,
20159 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020162 name = trans_function_name(&p, eap->skip, 0, &fudi);
20163 paren = (vim_strchr(p, '(') != NULL);
20164 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 {
20166 /*
20167 * Return on an invalid expression in braces, unless the expression
20168 * evaluation has been cancelled due to an aborting error, an
20169 * interrupt, or an exception.
20170 */
20171 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020172 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020173 if (!eap->skip && fudi.fd_newkey != NULL)
20174 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020175 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 else
20179 eap->skip = TRUE;
20180 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020181
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 /* An error in a function call during evaluation of an expression in magic
20183 * braces should not cause the function not to be defined. */
20184 saved_did_emsg = did_emsg;
20185 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186
20187 /*
20188 * ":function func" with only function name: list function.
20189 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020190 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 {
20192 if (!ends_excmd(*skipwhite(p)))
20193 {
20194 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020195 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 }
20197 eap->nextcmd = check_nextcmd(p);
20198 if (eap->nextcmd != NULL)
20199 *p = NUL;
20200 if (!eap->skip && !got_int)
20201 {
20202 fp = find_func(name);
20203 if (fp != NULL)
20204 {
20205 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020206 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020208 if (FUNCLINE(fp, j) == NULL)
20209 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 msg_putchar('\n');
20211 msg_outnum((long)(j + 1));
20212 if (j < 9)
20213 msg_putchar(' ');
20214 if (j < 99)
20215 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020216 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 out_flush(); /* show a line at a time */
20218 ui_breakcheck();
20219 }
20220 if (!got_int)
20221 {
20222 msg_putchar('\n');
20223 msg_puts((char_u *)" endfunction");
20224 }
20225 }
20226 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020227 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020229 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 }
20231
20232 /*
20233 * ":function name(arg1, arg2)" Define function.
20234 */
20235 p = skipwhite(p);
20236 if (*p != '(')
20237 {
20238 if (!eap->skip)
20239 {
20240 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020241 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 }
20243 /* attempt to continue by skipping some text */
20244 if (vim_strchr(p, '(') != NULL)
20245 p = vim_strchr(p, '(');
20246 }
20247 p = skipwhite(p + 1);
20248
20249 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20250 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20251
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020252 if (!eap->skip)
20253 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020254 /* Check the name of the function. Unless it's a dictionary function
20255 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020256 if (name != NULL)
20257 arg = name;
20258 else
20259 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020260 if (arg != NULL && (fudi.fd_di == NULL
20261 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020262 {
20263 if (*arg == K_SPECIAL)
20264 j = 3;
20265 else
20266 j = 0;
20267 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20268 : eval_isnamec(arg[j])))
20269 ++j;
20270 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020271 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020272 }
20273 }
20274
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 /*
20276 * Isolate the arguments: "arg1, arg2, ...)"
20277 */
20278 while (*p != ')')
20279 {
20280 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20281 {
20282 varargs = TRUE;
20283 p += 3;
20284 mustend = TRUE;
20285 }
20286 else
20287 {
20288 arg = p;
20289 while (ASCII_ISALNUM(*p) || *p == '_')
20290 ++p;
20291 if (arg == p || isdigit(*arg)
20292 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20293 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20294 {
20295 if (!eap->skip)
20296 EMSG2(_("E125: Illegal argument: %s"), arg);
20297 break;
20298 }
20299 if (ga_grow(&newargs, 1) == FAIL)
20300 goto erret;
20301 c = *p;
20302 *p = NUL;
20303 arg = vim_strsave(arg);
20304 if (arg == NULL)
20305 goto erret;
20306 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20307 *p = c;
20308 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 if (*p == ',')
20310 ++p;
20311 else
20312 mustend = TRUE;
20313 }
20314 p = skipwhite(p);
20315 if (mustend && *p != ')')
20316 {
20317 if (!eap->skip)
20318 EMSG2(_(e_invarg2), eap->arg);
20319 break;
20320 }
20321 }
20322 ++p; /* skip the ')' */
20323
Bram Moolenaare9a41262005-01-15 22:18:47 +000020324 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 for (;;)
20326 {
20327 p = skipwhite(p);
20328 if (STRNCMP(p, "range", 5) == 0)
20329 {
20330 flags |= FC_RANGE;
20331 p += 5;
20332 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020333 else if (STRNCMP(p, "dict", 4) == 0)
20334 {
20335 flags |= FC_DICT;
20336 p += 4;
20337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 else if (STRNCMP(p, "abort", 5) == 0)
20339 {
20340 flags |= FC_ABORT;
20341 p += 5;
20342 }
20343 else
20344 break;
20345 }
20346
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020347 /* When there is a line break use what follows for the function body.
20348 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20349 if (*p == '\n')
20350 line_arg = p + 1;
20351 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 EMSG(_(e_trailing));
20353
20354 /*
20355 * Read the body of the function, until ":endfunction" is found.
20356 */
20357 if (KeyTyped)
20358 {
20359 /* Check if the function already exists, don't let the user type the
20360 * whole function before telling him it doesn't work! For a script we
20361 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020362 if (!eap->skip && !eap->forceit)
20363 {
20364 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20365 EMSG(_(e_funcdict));
20366 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020367 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020370 if (!eap->skip && did_emsg)
20371 goto erret;
20372
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 msg_putchar('\n'); /* don't overwrite the function name */
20374 cmdline_row = msg_row;
20375 }
20376
20377 indent = 2;
20378 nesting = 0;
20379 for (;;)
20380 {
20381 msg_scroll = TRUE;
20382 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020383 sourcing_lnum_off = sourcing_lnum;
20384
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020385 if (line_arg != NULL)
20386 {
20387 /* Use eap->arg, split up in parts by line breaks. */
20388 theline = line_arg;
20389 p = vim_strchr(theline, '\n');
20390 if (p == NULL)
20391 line_arg += STRLEN(line_arg);
20392 else
20393 {
20394 *p = NUL;
20395 line_arg = p + 1;
20396 }
20397 }
20398 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 theline = getcmdline(':', 0L, indent);
20400 else
20401 theline = eap->getline(':', eap->cookie, indent);
20402 if (KeyTyped)
20403 lines_left = Rows - 1;
20404 if (theline == NULL)
20405 {
20406 EMSG(_("E126: Missing :endfunction"));
20407 goto erret;
20408 }
20409
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020410 /* Detect line continuation: sourcing_lnum increased more than one. */
20411 if (sourcing_lnum > sourcing_lnum_off + 1)
20412 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20413 else
20414 sourcing_lnum_off = 0;
20415
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 if (skip_until != NULL)
20417 {
20418 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20419 * don't check for ":endfunc". */
20420 if (STRCMP(theline, skip_until) == 0)
20421 {
20422 vim_free(skip_until);
20423 skip_until = NULL;
20424 }
20425 }
20426 else
20427 {
20428 /* skip ':' and blanks*/
20429 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20430 ;
20431
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020432 /* Check for "endfunction". */
20433 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020435 if (line_arg == NULL)
20436 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 break;
20438 }
20439
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020440 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 * at "end". */
20442 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20443 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020444 else if (STRNCMP(p, "if", 2) == 0
20445 || STRNCMP(p, "wh", 2) == 0
20446 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 || STRNCMP(p, "try", 3) == 0)
20448 indent += 2;
20449
20450 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020451 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020453 if (*p == '!')
20454 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 p += eval_fname_script(p);
20456 if (ASCII_ISALPHA(*p))
20457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020458 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 if (*skipwhite(p) == '(')
20460 {
20461 ++nesting;
20462 indent += 2;
20463 }
20464 }
20465 }
20466
20467 /* Check for ":append" or ":insert". */
20468 p = skip_range(p, NULL);
20469 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20470 || (p[0] == 'i'
20471 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20472 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20473 skip_until = vim_strsave((char_u *)".");
20474
20475 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20476 arg = skipwhite(skiptowhite(p));
20477 if (arg[0] == '<' && arg[1] =='<'
20478 && ((p[0] == 'p' && p[1] == 'y'
20479 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20480 || (p[0] == 'p' && p[1] == 'e'
20481 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20482 || (p[0] == 't' && p[1] == 'c'
20483 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20484 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20485 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020486 || (p[0] == 'm' && p[1] == 'z'
20487 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 ))
20489 {
20490 /* ":python <<" continues until a dot, like ":append" */
20491 p = skipwhite(arg + 2);
20492 if (*p == NUL)
20493 skip_until = vim_strsave((char_u *)".");
20494 else
20495 skip_until = vim_strsave(p);
20496 }
20497 }
20498
20499 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020500 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020501 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020502 if (line_arg == NULL)
20503 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020505 }
20506
20507 /* Copy the line to newly allocated memory. get_one_sourceline()
20508 * allocates 250 bytes per line, this saves 80% on average. The cost
20509 * is an extra alloc/free. */
20510 p = vim_strsave(theline);
20511 if (p != NULL)
20512 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020513 if (line_arg == NULL)
20514 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020515 theline = p;
20516 }
20517
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020518 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20519
20520 /* Add NULL lines for continuation lines, so that the line count is
20521 * equal to the index in the growarray. */
20522 while (sourcing_lnum_off-- > 0)
20523 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020524
20525 /* Check for end of eap->arg. */
20526 if (line_arg != NULL && *line_arg == NUL)
20527 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 }
20529
20530 /* Don't define the function when skipping commands or when an error was
20531 * detected. */
20532 if (eap->skip || did_emsg)
20533 goto erret;
20534
20535 /*
20536 * If there are no errors, add the function
20537 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020538 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020539 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020540 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020541 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020542 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020543 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020544 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020545 goto erret;
20546 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020547
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020548 fp = find_func(name);
20549 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020551 if (!eap->forceit)
20552 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020553 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020554 goto erret;
20555 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020556 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020557 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020558 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020559 name);
20560 goto erret;
20561 }
20562 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020563 ga_clear_strings(&(fp->uf_args));
20564 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020565 vim_free(name);
20566 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 }
20569 else
20570 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020571 char numbuf[20];
20572
20573 fp = NULL;
20574 if (fudi.fd_newkey == NULL && !eap->forceit)
20575 {
20576 EMSG(_(e_funcdict));
20577 goto erret;
20578 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020579 if (fudi.fd_di == NULL)
20580 {
20581 /* Can't add a function to a locked dictionary */
20582 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20583 goto erret;
20584 }
20585 /* Can't change an existing function if it is locked */
20586 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20587 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020588
20589 /* Give the function a sequential number. Can only be used with a
20590 * Funcref! */
20591 vim_free(name);
20592 sprintf(numbuf, "%d", ++func_nr);
20593 name = vim_strsave((char_u *)numbuf);
20594 if (name == NULL)
20595 goto erret;
20596 }
20597
20598 if (fp == NULL)
20599 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020600 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020601 {
20602 int slen, plen;
20603 char_u *scriptname;
20604
20605 /* Check that the autoload name matches the script name. */
20606 j = FAIL;
20607 if (sourcing_name != NULL)
20608 {
20609 scriptname = autoload_name(name);
20610 if (scriptname != NULL)
20611 {
20612 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020613 plen = (int)STRLEN(p);
20614 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020615 if (slen > plen && fnamecmp(p,
20616 sourcing_name + slen - plen) == 0)
20617 j = OK;
20618 vim_free(scriptname);
20619 }
20620 }
20621 if (j == FAIL)
20622 {
20623 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20624 goto erret;
20625 }
20626 }
20627
20628 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 if (fp == NULL)
20630 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020631
20632 if (fudi.fd_dict != NULL)
20633 {
20634 if (fudi.fd_di == NULL)
20635 {
20636 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020637 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020638 if (fudi.fd_di == NULL)
20639 {
20640 vim_free(fp);
20641 goto erret;
20642 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020643 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20644 {
20645 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020646 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020647 goto erret;
20648 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020649 }
20650 else
20651 /* overwrite existing dict entry */
20652 clear_tv(&fudi.fd_di->di_tv);
20653 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020654 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020655 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020656 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020657
20658 /* behave like "dict" was used */
20659 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020660 }
20661
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020663 STRCPY(fp->uf_name, name);
20664 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020666 fp->uf_args = newargs;
20667 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020668#ifdef FEAT_PROFILE
20669 fp->uf_tml_count = NULL;
20670 fp->uf_tml_total = NULL;
20671 fp->uf_tml_self = NULL;
20672 fp->uf_profiling = FALSE;
20673 if (prof_def_func())
20674 func_do_profile(fp);
20675#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020676 fp->uf_varargs = varargs;
20677 fp->uf_flags = flags;
20678 fp->uf_calls = 0;
20679 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020680 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681
20682erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 ga_clear_strings(&newargs);
20684 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020685ret_free:
20686 vim_free(skip_until);
20687 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690}
20691
20692/*
20693 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020694 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020696 * flags:
20697 * TFN_INT: internal function name OK
20698 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 * Advances "pp" to just after the function name (if no error).
20700 */
20701 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020702trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020703 char_u **pp;
20704 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020705 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020706 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020708 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 char_u *start;
20710 char_u *end;
20711 int lead;
20712 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020714 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020715
20716 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020717 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020719
20720 /* Check for hard coded <SNR>: already translated function ID (from a user
20721 * command). */
20722 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20723 && (*pp)[2] == (int)KE_SNR)
20724 {
20725 *pp += 3;
20726 len = get_id_len(pp) + 3;
20727 return vim_strnsave(start, len);
20728 }
20729
20730 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20731 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020733 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020735
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020736 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20737 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020738 if (end == start)
20739 {
20740 if (!skip)
20741 EMSG(_("E129: Function name required"));
20742 goto theend;
20743 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020744 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020745 {
20746 /*
20747 * Report an invalid expression in braces, unless the expression
20748 * evaluation has been cancelled due to an aborting error, an
20749 * interrupt, or an exception.
20750 */
20751 if (!aborting())
20752 {
20753 if (end != NULL)
20754 EMSG2(_(e_invarg2), start);
20755 }
20756 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020757 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020758 goto theend;
20759 }
20760
20761 if (lv.ll_tv != NULL)
20762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020763 if (fdp != NULL)
20764 {
20765 fdp->fd_dict = lv.ll_dict;
20766 fdp->fd_newkey = lv.ll_newkey;
20767 lv.ll_newkey = NULL;
20768 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020769 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020770 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20771 {
20772 name = vim_strsave(lv.ll_tv->vval.v_string);
20773 *pp = end;
20774 }
20775 else
20776 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020777 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20778 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020779 EMSG(_(e_funcref));
20780 else
20781 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020782 name = NULL;
20783 }
20784 goto theend;
20785 }
20786
20787 if (lv.ll_name == NULL)
20788 {
20789 /* Error found, but continue after the function name. */
20790 *pp = end;
20791 goto theend;
20792 }
20793
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020794 /* Check if the name is a Funcref. If so, use the value. */
20795 if (lv.ll_exp_name != NULL)
20796 {
20797 len = (int)STRLEN(lv.ll_exp_name);
20798 name = deref_func_name(lv.ll_exp_name, &len);
20799 if (name == lv.ll_exp_name)
20800 name = NULL;
20801 }
20802 else
20803 {
20804 len = (int)(end - *pp);
20805 name = deref_func_name(*pp, &len);
20806 if (name == *pp)
20807 name = NULL;
20808 }
20809 if (name != NULL)
20810 {
20811 name = vim_strsave(name);
20812 *pp = end;
20813 goto theend;
20814 }
20815
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020816 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020817 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020818 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020819 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20820 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20821 {
20822 /* When there was "s:" already or the name expanded to get a
20823 * leading "s:" then remove it. */
20824 lv.ll_name += 2;
20825 len -= 2;
20826 lead = 2;
20827 }
20828 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020829 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020830 {
20831 if (lead == 2) /* skip over "s:" */
20832 lv.ll_name += 2;
20833 len = (int)(end - lv.ll_name);
20834 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020835
20836 /*
20837 * Copy the function name to allocated memory.
20838 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20839 * Accept <SNR>123_name() outside a script.
20840 */
20841 if (skip)
20842 lead = 0; /* do nothing */
20843 else if (lead > 0)
20844 {
20845 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020846 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20847 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020848 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020849 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020850 if (current_SID <= 0)
20851 {
20852 EMSG(_(e_usingsid));
20853 goto theend;
20854 }
20855 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20856 lead += (int)STRLEN(sid_buf);
20857 }
20858 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020859 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020860 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020861 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020862 goto theend;
20863 }
20864 name = alloc((unsigned)(len + lead + 1));
20865 if (name != NULL)
20866 {
20867 if (lead > 0)
20868 {
20869 name[0] = K_SPECIAL;
20870 name[1] = KS_EXTRA;
20871 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020872 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020873 STRCPY(name + 3, sid_buf);
20874 }
20875 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20876 name[len + lead] = NUL;
20877 }
20878 *pp = end;
20879
20880theend:
20881 clear_lval(&lv);
20882 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883}
20884
20885/*
20886 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20887 * Return 2 if "p" starts with "s:".
20888 * Return 0 otherwise.
20889 */
20890 static int
20891eval_fname_script(p)
20892 char_u *p;
20893{
20894 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20895 || STRNICMP(p + 1, "SNR>", 4) == 0))
20896 return 5;
20897 if (p[0] == 's' && p[1] == ':')
20898 return 2;
20899 return 0;
20900}
20901
20902/*
20903 * Return TRUE if "p" starts with "<SID>" or "s:".
20904 * Only works if eval_fname_script() returned non-zero for "p"!
20905 */
20906 static int
20907eval_fname_sid(p)
20908 char_u *p;
20909{
20910 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20911}
20912
20913/*
20914 * List the head of the function: "name(arg1, arg2)".
20915 */
20916 static void
20917list_func_head(fp, indent)
20918 ufunc_T *fp;
20919 int indent;
20920{
20921 int j;
20922
20923 msg_start();
20924 if (indent)
20925 MSG_PUTS(" ");
20926 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020927 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 {
20929 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020930 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 }
20932 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020933 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020935 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 {
20937 if (j)
20938 MSG_PUTS(", ");
20939 msg_puts(FUNCARG(fp, j));
20940 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020941 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 {
20943 if (j)
20944 MSG_PUTS(", ");
20945 MSG_PUTS("...");
20946 }
20947 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020948 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020949 if (p_verbose > 0)
20950 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951}
20952
20953/*
20954 * Find a function by name, return pointer to it in ufuncs.
20955 * Return NULL for unknown function.
20956 */
20957 static ufunc_T *
20958find_func(name)
20959 char_u *name;
20960{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020961 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020963 hi = hash_find(&func_hashtab, name);
20964 if (!HASHITEM_EMPTY(hi))
20965 return HI2UF(hi);
20966 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967}
20968
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020969#if defined(EXITFREE) || defined(PROTO)
20970 void
20971free_all_functions()
20972{
20973 hashitem_T *hi;
20974
20975 /* Need to start all over every time, because func_free() may change the
20976 * hash table. */
20977 while (func_hashtab.ht_used > 0)
20978 for (hi = func_hashtab.ht_array; ; ++hi)
20979 if (!HASHITEM_EMPTY(hi))
20980 {
20981 func_free(HI2UF(hi));
20982 break;
20983 }
20984}
20985#endif
20986
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020987/*
20988 * Return TRUE if a function "name" exists.
20989 */
20990 static int
20991function_exists(name)
20992 char_u *name;
20993{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020994 char_u *nm = name;
20995 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020996 int n = FALSE;
20997
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020998 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020999 nm = skipwhite(nm);
21000
21001 /* Only accept "funcname", "funcname ", "funcname (..." and
21002 * "funcname(...", not "funcname!...". */
21003 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021004 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021005 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021006 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021007 else
21008 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021009 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021010 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021011 return n;
21012}
21013
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021014/*
21015 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021016 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021017 */
21018 static int
21019builtin_function(name)
21020 char_u *name;
21021{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021022 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21023 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021024}
21025
Bram Moolenaar05159a02005-02-26 23:04:13 +000021026#if defined(FEAT_PROFILE) || defined(PROTO)
21027/*
21028 * Start profiling function "fp".
21029 */
21030 static void
21031func_do_profile(fp)
21032 ufunc_T *fp;
21033{
21034 fp->uf_tm_count = 0;
21035 profile_zero(&fp->uf_tm_self);
21036 profile_zero(&fp->uf_tm_total);
21037 if (fp->uf_tml_count == NULL)
21038 fp->uf_tml_count = (int *)alloc_clear((unsigned)
21039 (sizeof(int) * fp->uf_lines.ga_len));
21040 if (fp->uf_tml_total == NULL)
21041 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
21042 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21043 if (fp->uf_tml_self == NULL)
21044 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
21045 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21046 fp->uf_tml_idx = -1;
21047 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21048 || fp->uf_tml_self == NULL)
21049 return; /* out of memory */
21050
21051 fp->uf_profiling = TRUE;
21052}
21053
21054/*
21055 * Dump the profiling results for all functions in file "fd".
21056 */
21057 void
21058func_dump_profile(fd)
21059 FILE *fd;
21060{
21061 hashitem_T *hi;
21062 int todo;
21063 ufunc_T *fp;
21064 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021065 ufunc_T **sorttab;
21066 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021068 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021069 if (todo == 0)
21070 return; /* nothing to dump */
21071
Bram Moolenaar73830342005-02-28 22:48:19 +000021072 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21073
Bram Moolenaar05159a02005-02-26 23:04:13 +000021074 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21075 {
21076 if (!HASHITEM_EMPTY(hi))
21077 {
21078 --todo;
21079 fp = HI2UF(hi);
21080 if (fp->uf_profiling)
21081 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021082 if (sorttab != NULL)
21083 sorttab[st_len++] = fp;
21084
Bram Moolenaar05159a02005-02-26 23:04:13 +000021085 if (fp->uf_name[0] == K_SPECIAL)
21086 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21087 else
21088 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21089 if (fp->uf_tm_count == 1)
21090 fprintf(fd, "Called 1 time\n");
21091 else
21092 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21093 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21094 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21095 fprintf(fd, "\n");
21096 fprintf(fd, "count total (s) self (s)\n");
21097
21098 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21099 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021100 if (FUNCLINE(fp, i) == NULL)
21101 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021102 prof_func_line(fd, fp->uf_tml_count[i],
21103 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021104 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21105 }
21106 fprintf(fd, "\n");
21107 }
21108 }
21109 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021110
21111 if (sorttab != NULL && st_len > 0)
21112 {
21113 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21114 prof_total_cmp);
21115 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21116 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21117 prof_self_cmp);
21118 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21119 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021120
21121 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021122}
Bram Moolenaar73830342005-02-28 22:48:19 +000021123
21124 static void
21125prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21126 FILE *fd;
21127 ufunc_T **sorttab;
21128 int st_len;
21129 char *title;
21130 int prefer_self; /* when equal print only self time */
21131{
21132 int i;
21133 ufunc_T *fp;
21134
21135 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21136 fprintf(fd, "count total (s) self (s) function\n");
21137 for (i = 0; i < 20 && i < st_len; ++i)
21138 {
21139 fp = sorttab[i];
21140 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21141 prefer_self);
21142 if (fp->uf_name[0] == K_SPECIAL)
21143 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21144 else
21145 fprintf(fd, " %s()\n", fp->uf_name);
21146 }
21147 fprintf(fd, "\n");
21148}
21149
21150/*
21151 * Print the count and times for one function or function line.
21152 */
21153 static void
21154prof_func_line(fd, count, total, self, prefer_self)
21155 FILE *fd;
21156 int count;
21157 proftime_T *total;
21158 proftime_T *self;
21159 int prefer_self; /* when equal print only self time */
21160{
21161 if (count > 0)
21162 {
21163 fprintf(fd, "%5d ", count);
21164 if (prefer_self && profile_equal(total, self))
21165 fprintf(fd, " ");
21166 else
21167 fprintf(fd, "%s ", profile_msg(total));
21168 if (!prefer_self && profile_equal(total, self))
21169 fprintf(fd, " ");
21170 else
21171 fprintf(fd, "%s ", profile_msg(self));
21172 }
21173 else
21174 fprintf(fd, " ");
21175}
21176
21177/*
21178 * Compare function for total time sorting.
21179 */
21180 static int
21181#ifdef __BORLANDC__
21182_RTLENTRYF
21183#endif
21184prof_total_cmp(s1, s2)
21185 const void *s1;
21186 const void *s2;
21187{
21188 ufunc_T *p1, *p2;
21189
21190 p1 = *(ufunc_T **)s1;
21191 p2 = *(ufunc_T **)s2;
21192 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21193}
21194
21195/*
21196 * Compare function for self time sorting.
21197 */
21198 static int
21199#ifdef __BORLANDC__
21200_RTLENTRYF
21201#endif
21202prof_self_cmp(s1, s2)
21203 const void *s1;
21204 const void *s2;
21205{
21206 ufunc_T *p1, *p2;
21207
21208 p1 = *(ufunc_T **)s1;
21209 p2 = *(ufunc_T **)s2;
21210 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21211}
21212
Bram Moolenaar05159a02005-02-26 23:04:13 +000021213#endif
21214
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021215/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021216 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021217 * Return TRUE if a package was loaded.
21218 */
21219 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021220script_autoload(name, reload)
21221 char_u *name;
21222 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021223{
21224 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021225 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021226 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021227 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021228
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021229 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021230 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021231 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021232 return FALSE;
21233
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021234 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021235
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021236 /* Find the name in the list of previously loaded package names. Skip
21237 * "autoload/", it's always the same. */
21238 for (i = 0; i < ga_loaded.ga_len; ++i)
21239 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21240 break;
21241 if (!reload && i < ga_loaded.ga_len)
21242 ret = FALSE; /* was loaded already */
21243 else
21244 {
21245 /* Remember the name if it wasn't loaded already. */
21246 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21247 {
21248 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21249 tofree = NULL;
21250 }
21251
21252 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021253 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021254 ret = TRUE;
21255 }
21256
21257 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021258 return ret;
21259}
21260
21261/*
21262 * Return the autoload script name for a function or variable name.
21263 * Returns NULL when out of memory.
21264 */
21265 static char_u *
21266autoload_name(name)
21267 char_u *name;
21268{
21269 char_u *p;
21270 char_u *scriptname;
21271
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021272 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021273 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21274 if (scriptname == NULL)
21275 return FALSE;
21276 STRCPY(scriptname, "autoload/");
21277 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021278 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021279 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021280 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021281 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021282 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021283}
21284
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21286
21287/*
21288 * Function given to ExpandGeneric() to obtain the list of user defined
21289 * function names.
21290 */
21291 char_u *
21292get_user_func_name(xp, idx)
21293 expand_T *xp;
21294 int idx;
21295{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021296 static long_u done;
21297 static hashitem_T *hi;
21298 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299
21300 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021302 done = 0;
21303 hi = func_hashtab.ht_array;
21304 }
21305 if (done < func_hashtab.ht_used)
21306 {
21307 if (done++ > 0)
21308 ++hi;
21309 while (HASHITEM_EMPTY(hi))
21310 ++hi;
21311 fp = HI2UF(hi);
21312
21313 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21314 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315
21316 cat_func_name(IObuff, fp);
21317 if (xp->xp_context != EXPAND_USER_FUNC)
21318 {
21319 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021320 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 STRCAT(IObuff, ")");
21322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 return IObuff;
21324 }
21325 return NULL;
21326}
21327
21328#endif /* FEAT_CMDL_COMPL */
21329
21330/*
21331 * Copy the function name of "fp" to buffer "buf".
21332 * "buf" must be able to hold the function name plus three bytes.
21333 * Takes care of script-local function names.
21334 */
21335 static void
21336cat_func_name(buf, fp)
21337 char_u *buf;
21338 ufunc_T *fp;
21339{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021340 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 {
21342 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021343 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 }
21345 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021346 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347}
21348
21349/*
21350 * ":delfunction {name}"
21351 */
21352 void
21353ex_delfunction(eap)
21354 exarg_T *eap;
21355{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021356 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 char_u *p;
21358 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021359 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360
21361 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021362 name = trans_function_name(&p, eap->skip, 0, &fudi);
21363 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021365 {
21366 if (fudi.fd_dict != NULL && !eap->skip)
21367 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 if (!ends_excmd(*skipwhite(p)))
21371 {
21372 vim_free(name);
21373 EMSG(_(e_trailing));
21374 return;
21375 }
21376 eap->nextcmd = check_nextcmd(p);
21377 if (eap->nextcmd != NULL)
21378 *p = NUL;
21379
21380 if (!eap->skip)
21381 fp = find_func(name);
21382 vim_free(name);
21383
21384 if (!eap->skip)
21385 {
21386 if (fp == NULL)
21387 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021388 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 return;
21390 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021391 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 {
21393 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21394 return;
21395 }
21396
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021397 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021399 /* Delete the dict item that refers to the function, it will
21400 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021401 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021403 else
21404 func_free(fp);
21405 }
21406}
21407
21408/*
21409 * Free a function and remove it from the list of functions.
21410 */
21411 static void
21412func_free(fp)
21413 ufunc_T *fp;
21414{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021415 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021416
21417 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021418 ga_clear_strings(&(fp->uf_args));
21419 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021420#ifdef FEAT_PROFILE
21421 vim_free(fp->uf_tml_count);
21422 vim_free(fp->uf_tml_total);
21423 vim_free(fp->uf_tml_self);
21424#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021425
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021426 /* remove the function from the function hashtable */
21427 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21428 if (HASHITEM_EMPTY(hi))
21429 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021430 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021431 hash_remove(&func_hashtab, hi);
21432
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021433 vim_free(fp);
21434}
21435
21436/*
21437 * Unreference a Function: decrement the reference count and free it when it
21438 * becomes zero. Only for numbered functions.
21439 */
21440 static void
21441func_unref(name)
21442 char_u *name;
21443{
21444 ufunc_T *fp;
21445
21446 if (name != NULL && isdigit(*name))
21447 {
21448 fp = find_func(name);
21449 if (fp == NULL)
21450 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021451 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021452 {
21453 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021454 * when "uf_calls" becomes zero. */
21455 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021456 func_free(fp);
21457 }
21458 }
21459}
21460
21461/*
21462 * Count a reference to a Function.
21463 */
21464 static void
21465func_ref(name)
21466 char_u *name;
21467{
21468 ufunc_T *fp;
21469
21470 if (name != NULL && isdigit(*name))
21471 {
21472 fp = find_func(name);
21473 if (fp == NULL)
21474 EMSG2(_(e_intern2), "func_ref()");
21475 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021476 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 }
21478}
21479
21480/*
21481 * Call a user function.
21482 */
21483 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021484call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 ufunc_T *fp; /* pointer to function */
21486 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021487 typval_T *argvars; /* arguments */
21488 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 linenr_T firstline; /* first line of range */
21490 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021491 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492{
Bram Moolenaar33570922005-01-25 22:26:29 +000021493 char_u *save_sourcing_name;
21494 linenr_T save_sourcing_lnum;
21495 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021496 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021497 int save_did_emsg;
21498 static int depth = 0;
21499 dictitem_T *v;
21500 int fixvar_idx = 0; /* index in fixvar[] */
21501 int i;
21502 int ai;
21503 char_u numbuf[NUMBUFLEN];
21504 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021505#ifdef FEAT_PROFILE
21506 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021507 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509
21510 /* If depth of calling is getting too high, don't execute the function */
21511 if (depth >= p_mfd)
21512 {
21513 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021514 rettv->v_type = VAR_NUMBER;
21515 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 return;
21517 }
21518 ++depth;
21519
21520 line_breakcheck(); /* check for CTRL-C hit */
21521
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021522 fc = (funccall_T *)alloc(sizeof(funccall_T));
21523 fc->caller = current_funccal;
21524 current_funccal = fc;
21525 fc->func = fp;
21526 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021527 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021528 fc->linenr = 0;
21529 fc->returned = FALSE;
21530 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021532 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21533 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534
Bram Moolenaar33570922005-01-25 22:26:29 +000021535 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021536 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021537 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21538 * each argument variable and saves a lot of time.
21539 */
21540 /*
21541 * Init l: variables.
21542 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021543 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021544 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021545 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021546 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21547 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021548 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021549 name = v->di_key;
21550 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021551 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021552 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021554 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021555 v->di_tv.vval.v_dict = selfdict;
21556 ++selfdict->dv_refcount;
21557 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021558
Bram Moolenaar33570922005-01-25 22:26:29 +000021559 /*
21560 * Init a: variables.
21561 * Set a:0 to "argcount".
21562 * Set a:000 to a list with room for the "..." arguments.
21563 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021564 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21565 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021566 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021567 /* Use "name" to avoid a warning from some compiler that checks the
21568 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021569 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021570 name = v->di_key;
21571 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021572 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021573 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021574 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021575 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021576 v->di_tv.vval.v_list = &fc->l_varlist;
21577 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21578 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21579 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021580
21581 /*
21582 * Set a:firstline to "firstline" and a:lastline to "lastline".
21583 * Set a:name to named arguments.
21584 * Set a:N to the "..." arguments.
21585 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021586 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021587 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021588 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021589 (varnumber_T)lastline);
21590 for (i = 0; i < argcount; ++i)
21591 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021592 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021593 if (ai < 0)
21594 /* named argument a:name */
21595 name = FUNCARG(fp, i);
21596 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021597 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021598 /* "..." argument a:1, a:2, etc. */
21599 sprintf((char *)numbuf, "%d", ai + 1);
21600 name = numbuf;
21601 }
21602 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21603 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021604 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021605 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21606 }
21607 else
21608 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021609 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21610 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021611 if (v == NULL)
21612 break;
21613 v->di_flags = DI_FLAGS_RO;
21614 }
21615 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021616 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021617
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021618 /* Note: the values are copied directly to avoid alloc/free.
21619 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021620 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021621 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021622
21623 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21624 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021625 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21626 fc->l_listitems[ai].li_tv = argvars[i];
21627 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021628 }
21629 }
21630
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 /* Don't redraw while executing the function. */
21632 ++RedrawingDisabled;
21633 save_sourcing_name = sourcing_name;
21634 save_sourcing_lnum = sourcing_lnum;
21635 sourcing_lnum = 1;
21636 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021637 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 if (sourcing_name != NULL)
21639 {
21640 if (save_sourcing_name != NULL
21641 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21642 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21643 else
21644 STRCPY(sourcing_name, "function ");
21645 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21646
21647 if (p_verbose >= 12)
21648 {
21649 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021650 verbose_enter_scroll();
21651
Bram Moolenaar555b2802005-05-19 21:08:39 +000021652 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 if (p_verbose >= 14)
21654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021656 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021657 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021658 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659
21660 msg_puts((char_u *)"(");
21661 for (i = 0; i < argcount; ++i)
21662 {
21663 if (i > 0)
21664 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021665 if (argvars[i].v_type == VAR_NUMBER)
21666 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 else
21668 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021669 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21670 if (s != NULL)
21671 {
21672 trunc_string(s, buf, MSG_BUF_CLEN);
21673 msg_puts(buf);
21674 vim_free(tofree);
21675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 }
21677 }
21678 msg_puts((char_u *)")");
21679 }
21680 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021681
21682 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 --no_wait_return;
21684 }
21685 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021686#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021687 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021688 {
21689 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21690 func_do_profile(fp);
21691 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021692 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021693 {
21694 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021695 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021696 profile_zero(&fp->uf_tm_children);
21697 }
21698 script_prof_save(&wait_start);
21699 }
21700#endif
21701
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021703 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 save_did_emsg = did_emsg;
21705 did_emsg = FALSE;
21706
21707 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021708 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21710
21711 --RedrawingDisabled;
21712
21713 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021714 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021716 clear_tv(rettv);
21717 rettv->v_type = VAR_NUMBER;
21718 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 }
21720
Bram Moolenaar05159a02005-02-26 23:04:13 +000021721#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021722 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021723 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021724 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021725 profile_end(&call_start);
21726 profile_sub_wait(&wait_start, &call_start);
21727 profile_add(&fp->uf_tm_total, &call_start);
21728 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021729 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021730 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021731 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21732 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021733 }
21734 }
21735#endif
21736
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 /* when being verbose, mention the return value */
21738 if (p_verbose >= 12)
21739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021741 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021744 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021745 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021746 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021747 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021748 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021750 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021751 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021752 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021753 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021754
Bram Moolenaar555b2802005-05-19 21:08:39 +000021755 /* The value may be very long. Skip the middle part, so that we
21756 * have some idea how it starts and ends. smsg() would always
21757 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021758 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021759 if (s != NULL)
21760 {
21761 trunc_string(s, buf, MSG_BUF_CLEN);
21762 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21763 vim_free(tofree);
21764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 }
21766 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021767
21768 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 --no_wait_return;
21770 }
21771
21772 vim_free(sourcing_name);
21773 sourcing_name = save_sourcing_name;
21774 sourcing_lnum = save_sourcing_lnum;
21775 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021776#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021777 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021778 script_prof_restore(&wait_start);
21779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780
21781 if (p_verbose >= 12 && sourcing_name != NULL)
21782 {
21783 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021784 verbose_enter_scroll();
21785
Bram Moolenaar555b2802005-05-19 21:08:39 +000021786 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021788
21789 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 --no_wait_return;
21791 }
21792
21793 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021794 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021796
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021797 /* If the a:000 list and the l: and a: dicts are not referenced we can
21798 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021799 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21800 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21801 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21802 {
21803 free_funccal(fc, FALSE);
21804 }
21805 else
21806 {
21807 hashitem_T *hi;
21808 listitem_T *li;
21809 int todo;
21810
21811 /* "fc" is still in use. This can happen when returning "a:000" or
21812 * assigning "l:" to a global variable.
21813 * Link "fc" in the list for garbage collection later. */
21814 fc->caller = previous_funccal;
21815 previous_funccal = fc;
21816
21817 /* Make a copy of the a: variables, since we didn't do that above. */
21818 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21819 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21820 {
21821 if (!HASHITEM_EMPTY(hi))
21822 {
21823 --todo;
21824 v = HI2DI(hi);
21825 copy_tv(&v->di_tv, &v->di_tv);
21826 }
21827 }
21828
21829 /* Make a copy of the a:000 items, since we didn't do that above. */
21830 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21831 copy_tv(&li->li_tv, &li->li_tv);
21832 }
21833}
21834
21835/*
21836 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021837 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021838 */
21839 static int
21840can_free_funccal(fc, copyID)
21841 funccall_T *fc;
21842 int copyID;
21843{
21844 return (fc->l_varlist.lv_copyID != copyID
21845 && fc->l_vars.dv_copyID != copyID
21846 && fc->l_avars.dv_copyID != copyID);
21847}
21848
21849/*
21850 * Free "fc" and what it contains.
21851 */
21852 static void
21853free_funccal(fc, free_val)
21854 funccall_T *fc;
21855 int free_val; /* a: vars were allocated */
21856{
21857 listitem_T *li;
21858
21859 /* The a: variables typevals may not have been allocated, only free the
21860 * allocated variables. */
21861 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21862
21863 /* free all l: variables */
21864 vars_clear(&fc->l_vars.dv_hashtab);
21865
21866 /* Free the a:000 variables if they were allocated. */
21867 if (free_val)
21868 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21869 clear_tv(&li->li_tv);
21870
21871 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872}
21873
21874/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 * Add a number variable "name" to dict "dp" with value "nr".
21876 */
21877 static void
21878add_nr_var(dp, v, name, nr)
21879 dict_T *dp;
21880 dictitem_T *v;
21881 char *name;
21882 varnumber_T nr;
21883{
21884 STRCPY(v->di_key, name);
21885 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21886 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21887 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021888 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021889 v->di_tv.vval.v_number = nr;
21890}
21891
21892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 * ":return [expr]"
21894 */
21895 void
21896ex_return(eap)
21897 exarg_T *eap;
21898{
21899 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021900 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 int returning = FALSE;
21902
21903 if (current_funccal == NULL)
21904 {
21905 EMSG(_("E133: :return not inside a function"));
21906 return;
21907 }
21908
21909 if (eap->skip)
21910 ++emsg_skip;
21911
21912 eap->nextcmd = NULL;
21913 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021914 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915 {
21916 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021917 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021919 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 }
21921 /* It's safer to return also on error. */
21922 else if (!eap->skip)
21923 {
21924 /*
21925 * Return unless the expression evaluation has been cancelled due to an
21926 * aborting error, an interrupt, or an exception.
21927 */
21928 if (!aborting())
21929 returning = do_return(eap, FALSE, TRUE, NULL);
21930 }
21931
21932 /* When skipping or the return gets pending, advance to the next command
21933 * in this line (!returning). Otherwise, ignore the rest of the line.
21934 * Following lines will be ignored by get_func_line(). */
21935 if (returning)
21936 eap->nextcmd = NULL;
21937 else if (eap->nextcmd == NULL) /* no argument */
21938 eap->nextcmd = check_nextcmd(arg);
21939
21940 if (eap->skip)
21941 --emsg_skip;
21942}
21943
21944/*
21945 * Return from a function. Possibly makes the return pending. Also called
21946 * for a pending return at the ":endtry" or after returning from an extra
21947 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021948 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021949 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 * FALSE when the return gets pending.
21951 */
21952 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021953do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954 exarg_T *eap;
21955 int reanimate;
21956 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021957 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958{
21959 int idx;
21960 struct condstack *cstack = eap->cstack;
21961
21962 if (reanimate)
21963 /* Undo the return. */
21964 current_funccal->returned = FALSE;
21965
21966 /*
21967 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21968 * not in its finally clause (which then is to be executed next) is found.
21969 * In this case, make the ":return" pending for execution at the ":endtry".
21970 * Otherwise, return normally.
21971 */
21972 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21973 if (idx >= 0)
21974 {
21975 cstack->cs_pending[idx] = CSTP_RETURN;
21976
21977 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021978 /* A pending return again gets pending. "rettv" points to an
21979 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021981 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 else
21983 {
21984 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021985 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021987 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021989 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 {
21991 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021992 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021993 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 else
21995 EMSG(_(e_outofmem));
21996 }
21997 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021998 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999
22000 if (reanimate)
22001 {
22002 /* The pending return value could be overwritten by a ":return"
22003 * without argument in a finally clause; reset the default
22004 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022005 current_funccal->rettv->v_type = VAR_NUMBER;
22006 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 }
22008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022009 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 }
22011 else
22012 {
22013 current_funccal->returned = TRUE;
22014
22015 /* If the return is carried out now, store the return value. For
22016 * a return immediately after reanimation, the value is already
22017 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022018 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022020 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022021 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022023 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 }
22025 }
22026
22027 return idx < 0;
22028}
22029
22030/*
22031 * Free the variable with a pending return value.
22032 */
22033 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022034discard_pending_return(rettv)
22035 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036{
Bram Moolenaar33570922005-01-25 22:26:29 +000022037 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038}
22039
22040/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022041 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042 * is an allocated string. Used by report_pending() for verbose messages.
22043 */
22044 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022045get_return_cmd(rettv)
22046 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022048 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022049 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022050 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022052 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022053 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022054 if (s == NULL)
22055 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022056
22057 STRCPY(IObuff, ":return ");
22058 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22059 if (STRLEN(s) + 8 >= IOSIZE)
22060 STRCPY(IObuff + IOSIZE - 4, "...");
22061 vim_free(tofree);
22062 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063}
22064
22065/*
22066 * Get next function line.
22067 * Called by do_cmdline() to get the next line.
22068 * Returns allocated string, or NULL for end of function.
22069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 char_u *
22071get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022072 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022074 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075{
Bram Moolenaar33570922005-01-25 22:26:29 +000022076 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022077 ufunc_T *fp = fcp->func;
22078 char_u *retval;
22079 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080
22081 /* If breakpoints have been added/deleted need to check for it. */
22082 if (fcp->dbg_tick != debug_tick)
22083 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022084 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 sourcing_lnum);
22086 fcp->dbg_tick = debug_tick;
22087 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022088#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022089 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022090 func_line_end(cookie);
22091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092
Bram Moolenaar05159a02005-02-26 23:04:13 +000022093 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022094 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22095 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 retval = NULL;
22097 else
22098 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022099 /* Skip NULL lines (continuation lines). */
22100 while (fcp->linenr < gap->ga_len
22101 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22102 ++fcp->linenr;
22103 if (fcp->linenr >= gap->ga_len)
22104 retval = NULL;
22105 else
22106 {
22107 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22108 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022109#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022110 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022111 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022112#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 }
22115
22116 /* Did we encounter a breakpoint? */
22117 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22118 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022119 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022121 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 sourcing_lnum);
22123 fcp->dbg_tick = debug_tick;
22124 }
22125
22126 return retval;
22127}
22128
Bram Moolenaar05159a02005-02-26 23:04:13 +000022129#if defined(FEAT_PROFILE) || defined(PROTO)
22130/*
22131 * Called when starting to read a function line.
22132 * "sourcing_lnum" must be correct!
22133 * When skipping lines it may not actually be executed, but we won't find out
22134 * until later and we need to store the time now.
22135 */
22136 void
22137func_line_start(cookie)
22138 void *cookie;
22139{
22140 funccall_T *fcp = (funccall_T *)cookie;
22141 ufunc_T *fp = fcp->func;
22142
22143 if (fp->uf_profiling && sourcing_lnum >= 1
22144 && sourcing_lnum <= fp->uf_lines.ga_len)
22145 {
22146 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022147 /* Skip continuation lines. */
22148 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22149 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022150 fp->uf_tml_execed = FALSE;
22151 profile_start(&fp->uf_tml_start);
22152 profile_zero(&fp->uf_tml_children);
22153 profile_get_wait(&fp->uf_tml_wait);
22154 }
22155}
22156
22157/*
22158 * Called when actually executing a function line.
22159 */
22160 void
22161func_line_exec(cookie)
22162 void *cookie;
22163{
22164 funccall_T *fcp = (funccall_T *)cookie;
22165 ufunc_T *fp = fcp->func;
22166
22167 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22168 fp->uf_tml_execed = TRUE;
22169}
22170
22171/*
22172 * Called when done with a function line.
22173 */
22174 void
22175func_line_end(cookie)
22176 void *cookie;
22177{
22178 funccall_T *fcp = (funccall_T *)cookie;
22179 ufunc_T *fp = fcp->func;
22180
22181 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22182 {
22183 if (fp->uf_tml_execed)
22184 {
22185 ++fp->uf_tml_count[fp->uf_tml_idx];
22186 profile_end(&fp->uf_tml_start);
22187 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022188 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022189 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22190 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022191 }
22192 fp->uf_tml_idx = -1;
22193 }
22194}
22195#endif
22196
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197/*
22198 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022199 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 */
22201 int
22202func_has_ended(cookie)
22203 void *cookie;
22204{
Bram Moolenaar33570922005-01-25 22:26:29 +000022205 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206
22207 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22208 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022209 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 || fcp->returned);
22211}
22212
22213/*
22214 * return TRUE if cookie indicates a function which "abort"s on errors.
22215 */
22216 int
22217func_has_abort(cookie)
22218 void *cookie;
22219{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022220 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221}
22222
22223#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22224typedef enum
22225{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022226 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22227 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22228 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229} var_flavour_T;
22230
22231static var_flavour_T var_flavour __ARGS((char_u *varname));
22232
22233 static var_flavour_T
22234var_flavour(varname)
22235 char_u *varname;
22236{
22237 char_u *p = varname;
22238
22239 if (ASCII_ISUPPER(*p))
22240 {
22241 while (*(++p))
22242 if (ASCII_ISLOWER(*p))
22243 return VAR_FLAVOUR_SESSION;
22244 return VAR_FLAVOUR_VIMINFO;
22245 }
22246 else
22247 return VAR_FLAVOUR_DEFAULT;
22248}
22249#endif
22250
22251#if defined(FEAT_VIMINFO) || defined(PROTO)
22252/*
22253 * Restore global vars that start with a capital from the viminfo file
22254 */
22255 int
22256read_viminfo_varlist(virp, writing)
22257 vir_T *virp;
22258 int writing;
22259{
22260 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022261 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022262 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263
22264 if (!writing && (find_viminfo_parameter('!') != NULL))
22265 {
22266 tab = vim_strchr(virp->vir_line + 1, '\t');
22267 if (tab != NULL)
22268 {
22269 *tab++ = '\0'; /* isolate the variable name */
22270 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022271 type = VAR_STRING;
22272#ifdef FEAT_FLOAT
22273 else if (*tab == 'F')
22274 type = VAR_FLOAT;
22275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276
22277 tab = vim_strchr(tab, '\t');
22278 if (tab != NULL)
22279 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022280 tv.v_type = type;
22281 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022282 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022284#ifdef FEAT_FLOAT
22285 else if (type == VAR_FLOAT)
22286 (void)string2float(tab + 1, &tv.vval.v_float);
22287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022289 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022290 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022291 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022292 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 }
22294 }
22295 }
22296
22297 return viminfo_readline(virp);
22298}
22299
22300/*
22301 * Write global vars that start with a capital to the viminfo file
22302 */
22303 void
22304write_viminfo_varlist(fp)
22305 FILE *fp;
22306{
Bram Moolenaar33570922005-01-25 22:26:29 +000022307 hashitem_T *hi;
22308 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022309 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022310 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022311 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022312 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022313 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314
22315 if (find_viminfo_parameter('!') == NULL)
22316 return;
22317
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022318 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022319
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022320 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022321 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022323 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022325 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022326 this_var = HI2DI(hi);
22327 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022328 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022329 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022330 {
22331 case VAR_STRING: s = "STR"; break;
22332 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022333#ifdef FEAT_FLOAT
22334 case VAR_FLOAT: s = "FLO"; break;
22335#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022336 default: continue;
22337 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022338 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022339 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022340 if (p != NULL)
22341 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022342 vim_free(tofree);
22343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344 }
22345 }
22346}
22347#endif
22348
22349#if defined(FEAT_SESSION) || defined(PROTO)
22350 int
22351store_session_globals(fd)
22352 FILE *fd;
22353{
Bram Moolenaar33570922005-01-25 22:26:29 +000022354 hashitem_T *hi;
22355 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022356 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357 char_u *p, *t;
22358
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022359 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022360 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022362 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022364 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022365 this_var = HI2DI(hi);
22366 if ((this_var->di_tv.v_type == VAR_NUMBER
22367 || this_var->di_tv.v_type == VAR_STRING)
22368 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022369 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022370 /* Escape special characters with a backslash. Turn a LF and
22371 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022372 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022373 (char_u *)"\\\"\n\r");
22374 if (p == NULL) /* out of memory */
22375 break;
22376 for (t = p; *t != NUL; ++t)
22377 if (*t == '\n')
22378 *t = 'n';
22379 else if (*t == '\r')
22380 *t = 'r';
22381 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022382 this_var->di_key,
22383 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22384 : ' ',
22385 p,
22386 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22387 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022388 || put_eol(fd) == FAIL)
22389 {
22390 vim_free(p);
22391 return FAIL;
22392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 vim_free(p);
22394 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022395#ifdef FEAT_FLOAT
22396 else if (this_var->di_tv.v_type == VAR_FLOAT
22397 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22398 {
22399 float_T f = this_var->di_tv.vval.v_float;
22400 int sign = ' ';
22401
22402 if (f < 0)
22403 {
22404 f = -f;
22405 sign = '-';
22406 }
22407 if ((fprintf(fd, "let %s = %c&%f",
22408 this_var->di_key, sign, f) < 0)
22409 || put_eol(fd) == FAIL)
22410 return FAIL;
22411 }
22412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 }
22414 }
22415 return OK;
22416}
22417#endif
22418
Bram Moolenaar661b1822005-07-28 22:36:45 +000022419/*
22420 * Display script name where an item was last set.
22421 * Should only be invoked when 'verbose' is non-zero.
22422 */
22423 void
22424last_set_msg(scriptID)
22425 scid_T scriptID;
22426{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022427 char_u *p;
22428
Bram Moolenaar661b1822005-07-28 22:36:45 +000022429 if (scriptID != 0)
22430 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022431 p = home_replace_save(NULL, get_scriptname(scriptID));
22432 if (p != NULL)
22433 {
22434 verbose_enter();
22435 MSG_PUTS(_("\n\tLast set from "));
22436 MSG_PUTS(p);
22437 vim_free(p);
22438 verbose_leave();
22439 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022440 }
22441}
22442
Bram Moolenaard812df62008-11-09 12:46:09 +000022443/*
22444 * List v:oldfiles in a nice way.
22445 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022446 void
22447ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022448 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022449{
22450 list_T *l = vimvars[VV_OLDFILES].vv_list;
22451 listitem_T *li;
22452 int nr = 0;
22453
22454 if (l == NULL)
22455 msg((char_u *)_("No old files"));
22456 else
22457 {
22458 msg_start();
22459 msg_scroll = TRUE;
22460 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22461 {
22462 msg_outnum((long)++nr);
22463 MSG_PUTS(": ");
22464 msg_outtrans(get_tv_string(&li->li_tv));
22465 msg_putchar('\n');
22466 out_flush(); /* output one line at a time */
22467 ui_breakcheck();
22468 }
22469 /* Assume "got_int" was set to truncate the listing. */
22470 got_int = FALSE;
22471
22472#ifdef FEAT_BROWSE_CMD
22473 if (cmdmod.browse)
22474 {
22475 quit_more = FALSE;
22476 nr = prompt_for_number(FALSE);
22477 msg_starthere();
22478 if (nr > 0)
22479 {
22480 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22481 (long)nr);
22482
22483 if (p != NULL)
22484 {
22485 p = expand_env_save(p);
22486 eap->arg = p;
22487 eap->cmdidx = CMD_edit;
22488 cmdmod.browse = FALSE;
22489 do_exedit(eap, NULL);
22490 vim_free(p);
22491 }
22492 }
22493 }
22494#endif
22495 }
22496}
22497
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498#endif /* FEAT_EVAL */
22499
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022501#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502
22503#ifdef WIN3264
22504/*
22505 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22506 */
22507static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22508static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22509static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22510
22511/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022512 * Get the short path (8.3) for the filename in "fnamep".
22513 * Only works for a valid file name.
22514 * When the path gets longer "fnamep" is changed and the allocated buffer
22515 * is put in "bufp".
22516 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22517 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 */
22519 static int
22520get_short_pathname(fnamep, bufp, fnamelen)
22521 char_u **fnamep;
22522 char_u **bufp;
22523 int *fnamelen;
22524{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022525 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 char_u *newbuf;
22527
22528 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 l = GetShortPathName(*fnamep, *fnamep, len);
22530 if (l > len - 1)
22531 {
22532 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022533 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 newbuf = vim_strnsave(*fnamep, l);
22535 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022536 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537
22538 vim_free(*bufp);
22539 *fnamep = *bufp = newbuf;
22540
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022541 /* Really should always succeed, as the buffer is big enough. */
22542 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 }
22544
22545 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022546 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547}
22548
22549/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022550 * Get the short path (8.3) for the filename in "fname". The converted
22551 * path is returned in "bufp".
22552 *
22553 * Some of the directories specified in "fname" may not exist. This function
22554 * will shorten the existing directories at the beginning of the path and then
22555 * append the remaining non-existing path.
22556 *
22557 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022558 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022559 * bufp - Pointer to an allocated buffer for the filename.
22560 * fnamelen - Length of the filename pointed to by fname
22561 *
22562 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 */
22564 static int
22565shortpath_for_invalid_fname(fname, bufp, fnamelen)
22566 char_u **fname;
22567 char_u **bufp;
22568 int *fnamelen;
22569{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022570 char_u *short_fname, *save_fname, *pbuf_unused;
22571 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022573 int old_len, len;
22574 int new_len, sfx_len;
22575 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576
22577 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022578 old_len = *fnamelen;
22579 save_fname = vim_strnsave(*fname, old_len);
22580 pbuf_unused = NULL;
22581 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022583 endp = save_fname + old_len - 1; /* Find the end of the copy */
22584 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022586 /*
22587 * Try shortening the supplied path till it succeeds by removing one
22588 * directory at a time from the tail of the path.
22589 */
22590 len = 0;
22591 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022593 /* go back one path-separator */
22594 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22595 --endp;
22596 if (endp <= save_fname)
22597 break; /* processed the complete path */
22598
22599 /*
22600 * Replace the path separator with a NUL and try to shorten the
22601 * resulting path.
22602 */
22603 ch = *endp;
22604 *endp = 0;
22605 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022606 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022607 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22608 {
22609 retval = FAIL;
22610 goto theend;
22611 }
22612 *endp = ch; /* preserve the string */
22613
22614 if (len > 0)
22615 break; /* successfully shortened the path */
22616
22617 /* failed to shorten the path. Skip the path separator */
22618 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 }
22620
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022621 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022623 /*
22624 * Succeeded in shortening the path. Now concatenate the shortened
22625 * path with the remaining path at the tail.
22626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022628 /* Compute the length of the new path. */
22629 sfx_len = (int)(save_endp - endp) + 1;
22630 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022632 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022634 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022636 /* There is not enough space in the currently allocated string,
22637 * copy it to a buffer big enough. */
22638 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022640 {
22641 retval = FAIL;
22642 goto theend;
22643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 }
22645 else
22646 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022647 /* Transfer short_fname to the main buffer (it's big enough),
22648 * unless get_short_pathname() did its work in-place. */
22649 *fname = *bufp = save_fname;
22650 if (short_fname != save_fname)
22651 vim_strncpy(save_fname, short_fname, len);
22652 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022654
22655 /* concat the not-shortened part of the path */
22656 vim_strncpy(*fname + len, endp, sfx_len);
22657 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022659
22660theend:
22661 vim_free(pbuf_unused);
22662 vim_free(save_fname);
22663
22664 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665}
22666
22667/*
22668 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022669 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 */
22671 static int
22672shortpath_for_partial(fnamep, bufp, fnamelen)
22673 char_u **fnamep;
22674 char_u **bufp;
22675 int *fnamelen;
22676{
22677 int sepcount, len, tflen;
22678 char_u *p;
22679 char_u *pbuf, *tfname;
22680 int hasTilde;
22681
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022682 /* Count up the path separators from the RHS.. so we know which part
22683 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022685 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 if (vim_ispathsep(*p))
22687 ++sepcount;
22688
22689 /* Need full path first (use expand_env() to remove a "~/") */
22690 hasTilde = (**fnamep == '~');
22691 if (hasTilde)
22692 pbuf = tfname = expand_env_save(*fnamep);
22693 else
22694 pbuf = tfname = FullName_save(*fnamep, FALSE);
22695
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022696 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022698 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22699 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700
22701 if (len == 0)
22702 {
22703 /* Don't have a valid filename, so shorten the rest of the
22704 * path if we can. This CAN give us invalid 8.3 filenames, but
22705 * there's not a lot of point in guessing what it might be.
22706 */
22707 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022708 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22709 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 }
22711
22712 /* Count the paths backward to find the beginning of the desired string. */
22713 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022714 {
22715#ifdef FEAT_MBYTE
22716 if (has_mbyte)
22717 p -= mb_head_off(tfname, p);
22718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 if (vim_ispathsep(*p))
22720 {
22721 if (sepcount == 0 || (hasTilde && sepcount == 1))
22722 break;
22723 else
22724 sepcount --;
22725 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 if (hasTilde)
22728 {
22729 --p;
22730 if (p >= tfname)
22731 *p = '~';
22732 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022733 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 }
22735 else
22736 ++p;
22737
22738 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22739 vim_free(*bufp);
22740 *fnamelen = (int)STRLEN(p);
22741 *bufp = pbuf;
22742 *fnamep = p;
22743
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022744 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745}
22746#endif /* WIN3264 */
22747
22748/*
22749 * Adjust a filename, according to a string of modifiers.
22750 * *fnamep must be NUL terminated when called. When returning, the length is
22751 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022752 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 * When there is an error, *fnamep is set to NULL.
22754 */
22755 int
22756modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22757 char_u *src; /* string with modifiers */
22758 int *usedlen; /* characters after src that are used */
22759 char_u **fnamep; /* file name so far */
22760 char_u **bufp; /* buffer for allocated file name or NULL */
22761 int *fnamelen; /* length of fnamep */
22762{
22763 int valid = 0;
22764 char_u *tail;
22765 char_u *s, *p, *pbuf;
22766 char_u dirname[MAXPATHL];
22767 int c;
22768 int has_fullname = 0;
22769#ifdef WIN3264
22770 int has_shortname = 0;
22771#endif
22772
22773repeat:
22774 /* ":p" - full path/file_name */
22775 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22776 {
22777 has_fullname = 1;
22778
22779 valid |= VALID_PATH;
22780 *usedlen += 2;
22781
22782 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22783 if ((*fnamep)[0] == '~'
22784#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22785 && ((*fnamep)[1] == '/'
22786# ifdef BACKSLASH_IN_FILENAME
22787 || (*fnamep)[1] == '\\'
22788# endif
22789 || (*fnamep)[1] == NUL)
22790
22791#endif
22792 )
22793 {
22794 *fnamep = expand_env_save(*fnamep);
22795 vim_free(*bufp); /* free any allocated file name */
22796 *bufp = *fnamep;
22797 if (*fnamep == NULL)
22798 return -1;
22799 }
22800
22801 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022802 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 {
22804 if (vim_ispathsep(*p)
22805 && p[1] == '.'
22806 && (p[2] == NUL
22807 || vim_ispathsep(p[2])
22808 || (p[2] == '.'
22809 && (p[3] == NUL || vim_ispathsep(p[3])))))
22810 break;
22811 }
22812
22813 /* FullName_save() is slow, don't use it when not needed. */
22814 if (*p != NUL || !vim_isAbsName(*fnamep))
22815 {
22816 *fnamep = FullName_save(*fnamep, *p != NUL);
22817 vim_free(*bufp); /* free any allocated file name */
22818 *bufp = *fnamep;
22819 if (*fnamep == NULL)
22820 return -1;
22821 }
22822
22823 /* Append a path separator to a directory. */
22824 if (mch_isdir(*fnamep))
22825 {
22826 /* Make room for one or two extra characters. */
22827 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22828 vim_free(*bufp); /* free any allocated file name */
22829 *bufp = *fnamep;
22830 if (*fnamep == NULL)
22831 return -1;
22832 add_pathsep(*fnamep);
22833 }
22834 }
22835
22836 /* ":." - path relative to the current directory */
22837 /* ":~" - path relative to the home directory */
22838 /* ":8" - shortname path - postponed till after */
22839 while (src[*usedlen] == ':'
22840 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22841 {
22842 *usedlen += 2;
22843 if (c == '8')
22844 {
22845#ifdef WIN3264
22846 has_shortname = 1; /* Postpone this. */
22847#endif
22848 continue;
22849 }
22850 pbuf = NULL;
22851 /* Need full path first (use expand_env() to remove a "~/") */
22852 if (!has_fullname)
22853 {
22854 if (c == '.' && **fnamep == '~')
22855 p = pbuf = expand_env_save(*fnamep);
22856 else
22857 p = pbuf = FullName_save(*fnamep, FALSE);
22858 }
22859 else
22860 p = *fnamep;
22861
22862 has_fullname = 0;
22863
22864 if (p != NULL)
22865 {
22866 if (c == '.')
22867 {
22868 mch_dirname(dirname, MAXPATHL);
22869 s = shorten_fname(p, dirname);
22870 if (s != NULL)
22871 {
22872 *fnamep = s;
22873 if (pbuf != NULL)
22874 {
22875 vim_free(*bufp); /* free any allocated file name */
22876 *bufp = pbuf;
22877 pbuf = NULL;
22878 }
22879 }
22880 }
22881 else
22882 {
22883 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22884 /* Only replace it when it starts with '~' */
22885 if (*dirname == '~')
22886 {
22887 s = vim_strsave(dirname);
22888 if (s != NULL)
22889 {
22890 *fnamep = s;
22891 vim_free(*bufp);
22892 *bufp = s;
22893 }
22894 }
22895 }
22896 vim_free(pbuf);
22897 }
22898 }
22899
22900 tail = gettail(*fnamep);
22901 *fnamelen = (int)STRLEN(*fnamep);
22902
22903 /* ":h" - head, remove "/file_name", can be repeated */
22904 /* Don't remove the first "/" or "c:\" */
22905 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22906 {
22907 valid |= VALID_HEAD;
22908 *usedlen += 2;
22909 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022910 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022911 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 *fnamelen = (int)(tail - *fnamep);
22913#ifdef VMS
22914 if (*fnamelen > 0)
22915 *fnamelen += 1; /* the path separator is part of the path */
22916#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022917 if (*fnamelen == 0)
22918 {
22919 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22920 p = vim_strsave((char_u *)".");
22921 if (p == NULL)
22922 return -1;
22923 vim_free(*bufp);
22924 *bufp = *fnamep = tail = p;
22925 *fnamelen = 1;
22926 }
22927 else
22928 {
22929 while (tail > s && !after_pathsep(s, tail))
22930 mb_ptr_back(*fnamep, tail);
22931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 }
22933
22934 /* ":8" - shortname */
22935 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22936 {
22937 *usedlen += 2;
22938#ifdef WIN3264
22939 has_shortname = 1;
22940#endif
22941 }
22942
22943#ifdef WIN3264
22944 /* Check shortname after we have done 'heads' and before we do 'tails'
22945 */
22946 if (has_shortname)
22947 {
22948 pbuf = NULL;
22949 /* Copy the string if it is shortened by :h */
22950 if (*fnamelen < (int)STRLEN(*fnamep))
22951 {
22952 p = vim_strnsave(*fnamep, *fnamelen);
22953 if (p == 0)
22954 return -1;
22955 vim_free(*bufp);
22956 *bufp = *fnamep = p;
22957 }
22958
22959 /* Split into two implementations - makes it easier. First is where
22960 * there isn't a full name already, second is where there is.
22961 */
22962 if (!has_fullname && !vim_isAbsName(*fnamep))
22963 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022964 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 return -1;
22966 }
22967 else
22968 {
22969 int l;
22970
22971 /* Simple case, already have the full-name
22972 * Nearly always shorter, so try first time. */
22973 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022974 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 return -1;
22976
22977 if (l == 0)
22978 {
22979 /* Couldn't find the filename.. search the paths.
22980 */
22981 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022982 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 return -1;
22984 }
22985 *fnamelen = l;
22986 }
22987 }
22988#endif /* WIN3264 */
22989
22990 /* ":t" - tail, just the basename */
22991 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22992 {
22993 *usedlen += 2;
22994 *fnamelen -= (int)(tail - *fnamep);
22995 *fnamep = tail;
22996 }
22997
22998 /* ":e" - extension, can be repeated */
22999 /* ":r" - root, without extension, can be repeated */
23000 while (src[*usedlen] == ':'
23001 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23002 {
23003 /* find a '.' in the tail:
23004 * - for second :e: before the current fname
23005 * - otherwise: The last '.'
23006 */
23007 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23008 s = *fnamep - 2;
23009 else
23010 s = *fnamep + *fnamelen - 1;
23011 for ( ; s > tail; --s)
23012 if (s[0] == '.')
23013 break;
23014 if (src[*usedlen + 1] == 'e') /* :e */
23015 {
23016 if (s > tail)
23017 {
23018 *fnamelen += (int)(*fnamep - (s + 1));
23019 *fnamep = s + 1;
23020#ifdef VMS
23021 /* cut version from the extension */
23022 s = *fnamep + *fnamelen - 1;
23023 for ( ; s > *fnamep; --s)
23024 if (s[0] == ';')
23025 break;
23026 if (s > *fnamep)
23027 *fnamelen = s - *fnamep;
23028#endif
23029 }
23030 else if (*fnamep <= tail)
23031 *fnamelen = 0;
23032 }
23033 else /* :r */
23034 {
23035 if (s > tail) /* remove one extension */
23036 *fnamelen = (int)(s - *fnamep);
23037 }
23038 *usedlen += 2;
23039 }
23040
23041 /* ":s?pat?foo?" - substitute */
23042 /* ":gs?pat?foo?" - global substitute */
23043 if (src[*usedlen] == ':'
23044 && (src[*usedlen + 1] == 's'
23045 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23046 {
23047 char_u *str;
23048 char_u *pat;
23049 char_u *sub;
23050 int sep;
23051 char_u *flags;
23052 int didit = FALSE;
23053
23054 flags = (char_u *)"";
23055 s = src + *usedlen + 2;
23056 if (src[*usedlen + 1] == 'g')
23057 {
23058 flags = (char_u *)"g";
23059 ++s;
23060 }
23061
23062 sep = *s++;
23063 if (sep)
23064 {
23065 /* find end of pattern */
23066 p = vim_strchr(s, sep);
23067 if (p != NULL)
23068 {
23069 pat = vim_strnsave(s, (int)(p - s));
23070 if (pat != NULL)
23071 {
23072 s = p + 1;
23073 /* find end of substitution */
23074 p = vim_strchr(s, sep);
23075 if (p != NULL)
23076 {
23077 sub = vim_strnsave(s, (int)(p - s));
23078 str = vim_strnsave(*fnamep, *fnamelen);
23079 if (sub != NULL && str != NULL)
23080 {
23081 *usedlen = (int)(p + 1 - src);
23082 s = do_string_sub(str, pat, sub, flags);
23083 if (s != NULL)
23084 {
23085 *fnamep = s;
23086 *fnamelen = (int)STRLEN(s);
23087 vim_free(*bufp);
23088 *bufp = s;
23089 didit = TRUE;
23090 }
23091 }
23092 vim_free(sub);
23093 vim_free(str);
23094 }
23095 vim_free(pat);
23096 }
23097 }
23098 /* after using ":s", repeat all the modifiers */
23099 if (didit)
23100 goto repeat;
23101 }
23102 }
23103
23104 return valid;
23105}
23106
23107/*
23108 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23109 * "flags" can be "g" to do a global substitute.
23110 * Returns an allocated string, NULL for error.
23111 */
23112 char_u *
23113do_string_sub(str, pat, sub, flags)
23114 char_u *str;
23115 char_u *pat;
23116 char_u *sub;
23117 char_u *flags;
23118{
23119 int sublen;
23120 regmatch_T regmatch;
23121 int i;
23122 int do_all;
23123 char_u *tail;
23124 garray_T ga;
23125 char_u *ret;
23126 char_u *save_cpo;
23127
23128 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23129 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023130 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131
23132 ga_init2(&ga, 1, 200);
23133
23134 do_all = (flags[0] == 'g');
23135
23136 regmatch.rm_ic = p_ic;
23137 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23138 if (regmatch.regprog != NULL)
23139 {
23140 tail = str;
23141 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23142 {
23143 /*
23144 * Get some space for a temporary buffer to do the substitution
23145 * into. It will contain:
23146 * - The text up to where the match is.
23147 * - The substituted text.
23148 * - The text after the match.
23149 */
23150 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23151 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23152 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23153 {
23154 ga_clear(&ga);
23155 break;
23156 }
23157
23158 /* copy the text up to where the match is */
23159 i = (int)(regmatch.startp[0] - tail);
23160 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23161 /* add the substituted text */
23162 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23163 + ga.ga_len + i, TRUE, TRUE, FALSE);
23164 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 /* avoid getting stuck on a match with an empty string */
23166 if (tail == regmatch.endp[0])
23167 {
23168 if (*tail == NUL)
23169 break;
23170 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23171 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 }
23173 else
23174 {
23175 tail = regmatch.endp[0];
23176 if (*tail == NUL)
23177 break;
23178 }
23179 if (!do_all)
23180 break;
23181 }
23182
23183 if (ga.ga_data != NULL)
23184 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23185
23186 vim_free(regmatch.regprog);
23187 }
23188
23189 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23190 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023191 if (p_cpo == empty_option)
23192 p_cpo = save_cpo;
23193 else
23194 /* Darn, evaluating {sub} expression changed the value. */
23195 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196
23197 return ret;
23198}
23199
23200#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */