blob: 865dc3a7dabe1f9a77e984fbda949c048915e25c [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));
454static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000455static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
458static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static int string2float __ARGS((char_u *text, float_T *value));
462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
464static int find_internal_func __ARGS((char_u *name));
465static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
466static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +0200467static 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 +0000468static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000469static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
472static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200481static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
497static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
498#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000499static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000502static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000504#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000505static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000506static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
512static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200513static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
518static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200529#ifdef FEAT_FLOAT
530static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
531#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000534static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
541static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200543static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000545static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000554static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000556static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000562static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000570static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000571static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000572static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000573static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200576static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000577static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000585static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000599static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000605static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000617#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200618static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000619static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000626static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000627static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000629static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000633#ifdef vim_mkdir
634static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100637#ifdef FEAT_MZSCHEME
638static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000642static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000643#ifdef FEAT_FLOAT
644static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
645#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000647static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000648static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000650static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000651static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#ifdef FEAT_FLOAT
664static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000667static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000669static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000676static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000677static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000678static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000679static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200681static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000682static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000684static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#ifdef FEAT_FLOAT
687static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200688static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000691static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000692static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
698#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000699static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700#ifdef HAVE_STRFTIME
701static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
702#endif
703static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000714static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000716static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000717static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000718static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000719static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000720static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000722static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200723#ifdef FEAT_FLOAT
724static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
726#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
731static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200734static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000744static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000747static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000749static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000750static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static int get_env_len __ARGS((char_u **arg));
752static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000753static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000754static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
755#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
756#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
757 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000758static 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 +0000759static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000760static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000761static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
762static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static typval_T *alloc_tv __ARGS((void));
764static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void init_tv __ARGS((typval_T *varp));
766static long get_tv_number __ARGS((typval_T *varp));
767static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000768static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static char_u *get_tv_string __ARGS((typval_T *varp));
770static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000771static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000773static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
775static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
776static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000777static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
778static 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 +0000779static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
780static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000781static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000782static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000783static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
785static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
786static int eval_fname_script __ARGS((char_u *p));
787static int eval_fname_sid __ARGS((char_u *p));
788static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static ufunc_T *find_func __ARGS((char_u *name));
790static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000791static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792#ifdef FEAT_PROFILE
793static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000794static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
795static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
796static int
797# ifdef __BORLANDC__
798 _RTLENTRYF
799# endif
800 prof_total_cmp __ARGS((const void *s1, const void *s2));
801static int
802# ifdef __BORLANDC__
803 _RTLENTRYF
804# endif
805 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000807static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000808static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000809static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static void func_free __ARGS((ufunc_T *fp));
811static void func_unref __ARGS((char_u *name));
812static void func_ref __ARGS((char_u *name));
813static 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 +0000814static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
815static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000817static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
818static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000819static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000820static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000821static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000823/* Character used as separated in autoload function/variable names. */
824#define AUTOLOAD_CHAR '#'
825
Bram Moolenaar33570922005-01-25 22:26:29 +0000826/*
827 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000828 */
829 void
830eval_init()
831{
Bram Moolenaar33570922005-01-25 22:26:29 +0000832 int i;
833 struct vimvar *p;
834
835 init_var_dict(&globvardict, &globvars_var);
836 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000837 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000838 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000839
840 for (i = 0; i < VV_LEN; ++i)
841 {
842 p = &vimvars[i];
843 STRCPY(p->vv_di.di_key, p->vv_name);
844 if (p->vv_flags & VV_RO)
845 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
846 else if (p->vv_flags & VV_RO_SBX)
847 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
848 else
849 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000850
851 /* add to v: scope dict, unless the value is not always available */
852 if (p->vv_type != VAR_UNKNOWN)
853 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000854 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000855 /* add to compat scope dict */
856 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000857 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000858 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000859}
860
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000861#if defined(EXITFREE) || defined(PROTO)
862 void
863eval_clear()
864{
865 int i;
866 struct vimvar *p;
867
868 for (i = 0; i < VV_LEN; ++i)
869 {
870 p = &vimvars[i];
871 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000872 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000873 vim_free(p->vv_str);
874 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000875 }
876 else if (p->vv_di.di_tv.v_type == VAR_LIST)
877 {
878 list_unref(p->vv_list);
879 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000880 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000881 }
882 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000883 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000884 hash_clear(&compat_hashtab);
885
Bram Moolenaard9fba312005-06-26 22:34:35 +0000886 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000887
888 /* global variables */
889 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000890
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000891 /* autoloaded script names */
892 ga_clear_strings(&ga_loaded);
893
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200894 /* script-local variables */
895 for (i = 1; i <= ga_scripts.ga_len; ++i)
896 {
897 vars_clear(&SCRIPT_VARS(i));
898 vim_free(SCRIPT_SV(i));
899 }
900 ga_clear(&ga_scripts);
901
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000902 /* unreferenced lists and dicts */
903 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000904
905 /* functions */
906 free_all_functions();
907 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908}
909#endif
910
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 * Return the name of the executed function.
913 */
914 char_u *
915func_name(cookie)
916 void *cookie;
917{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000918 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919}
920
921/*
922 * Return the address holding the next breakpoint line for a funccall cookie.
923 */
924 linenr_T *
925func_breakpoint(cookie)
926 void *cookie;
927{
Bram Moolenaar33570922005-01-25 22:26:29 +0000928 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929}
930
931/*
932 * Return the address holding the debug tick for a funccall cookie.
933 */
934 int *
935func_dbg_tick(cookie)
936 void *cookie;
937{
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939}
940
941/*
942 * Return the nesting level for a funccall cookie.
943 */
944 int
945func_level(cookie)
946 void *cookie;
947{
Bram Moolenaar33570922005-01-25 22:26:29 +0000948 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949}
950
951/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000952funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000954/* pointer to list of previously used funccal, still around because some
955 * item in it is still being used. */
956funccall_T *previous_funccal = NULL;
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958/*
959 * Return TRUE when a function was ended by a ":return" command.
960 */
961 int
962current_func_returned()
963{
964 return current_funccal->returned;
965}
966
967
968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 * Set an internal variable to a string value. Creates the variable if it does
970 * not already exist.
971 */
972 void
973set_internal_string_var(name, value)
974 char_u *name;
975 char_u *value;
976{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000977 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000978 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 val = vim_strsave(value);
981 if (val != NULL)
982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000983 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000984 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000986 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000987 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989 }
990}
991
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000992static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000993static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994static char_u *redir_endp = NULL;
995static char_u *redir_varname = NULL;
996
997/*
998 * Start recording command output to a variable
999 * Returns OK if successfully completed the setup. FAIL otherwise.
1000 */
1001 int
1002var_redir_start(name, append)
1003 char_u *name;
1004 int append; /* append to an existing variable */
1005{
1006 int save_emsg;
1007 int err;
1008 typval_T tv;
1009
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001010 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001011 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012 {
1013 EMSG(_(e_invarg));
1014 return FAIL;
1015 }
1016
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001017 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018 redir_varname = vim_strsave(name);
1019 if (redir_varname == NULL)
1020 return FAIL;
1021
1022 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1023 if (redir_lval == NULL)
1024 {
1025 var_redir_stop();
1026 return FAIL;
1027 }
1028
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001029 /* The output is stored in growarray "redir_ga" until redirection ends. */
1030 ga_init2(&redir_ga, (int)sizeof(char), 500);
1031
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001033 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1034 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001035 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1036 {
1037 if (redir_endp != NULL && *redir_endp != NUL)
1038 /* Trailing characters are present after the variable name */
1039 EMSG(_(e_trailing));
1040 else
1041 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001042 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 var_redir_stop();
1044 return FAIL;
1045 }
1046
1047 /* check if we can write to the variable: set it to or append an empty
1048 * string */
1049 save_emsg = did_emsg;
1050 did_emsg = FALSE;
1051 tv.v_type = VAR_STRING;
1052 tv.vval.v_string = (char_u *)"";
1053 if (append)
1054 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1055 else
1056 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1057 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001058 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (err)
1060 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001061 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 var_redir_stop();
1063 return FAIL;
1064 }
1065 if (redir_lval->ll_newkey != NULL)
1066 {
1067 /* Dictionary item was created, don't do it again. */
1068 vim_free(redir_lval->ll_newkey);
1069 redir_lval->ll_newkey = NULL;
1070 }
1071
1072 return OK;
1073}
1074
1075/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001076 * Append "value[value_len]" to the variable set by var_redir_start().
1077 * The actual appending is postponed until redirection ends, because the value
1078 * appended may in fact be the string we write to, changing it may cause freed
1079 * memory to be used:
1080 * :redir => foo
1081 * :let foo
1082 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 */
1084 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001085var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001087 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001089 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090
1091 if (redir_lval == NULL)
1092 return;
1093
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001095 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001097 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001099 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100 {
1101 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001102 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103 }
1104 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106}
1107
1108/*
1109 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001110 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 */
1112 void
1113var_redir_stop()
1114{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 typval_T tv;
1116
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 if (redir_lval != NULL)
1118 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001119 /* If there was no error: assign the text to the variable. */
1120 if (redir_endp != NULL)
1121 {
1122 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1123 tv.v_type = VAR_STRING;
1124 tv.vval.v_string = redir_ga.ga_data;
1125 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1126 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001128 /* free the collected output */
1129 vim_free(redir_ga.ga_data);
1130 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 clear_lval(redir_lval);
1133 vim_free(redir_lval);
1134 redir_lval = NULL;
1135 }
1136 vim_free(redir_varname);
1137 redir_varname = NULL;
1138}
1139
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140# if defined(FEAT_MBYTE) || defined(PROTO)
1141 int
1142eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1143 char_u *enc_from;
1144 char_u *enc_to;
1145 char_u *fname_from;
1146 char_u *fname_to;
1147{
1148 int err = FALSE;
1149
1150 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1151 set_vim_var_string(VV_CC_TO, enc_to, -1);
1152 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1153 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1154 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1155 err = TRUE;
1156 set_vim_var_string(VV_CC_FROM, NULL, -1);
1157 set_vim_var_string(VV_CC_TO, NULL, -1);
1158 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1159 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1160
1161 if (err)
1162 return FAIL;
1163 return OK;
1164}
1165# endif
1166
1167# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1168 int
1169eval_printexpr(fname, args)
1170 char_u *fname;
1171 char_u *args;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_FNAME_IN, fname, -1);
1176 set_vim_var_string(VV_CMDARG, args, -1);
1177 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1178 err = TRUE;
1179 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1180 set_vim_var_string(VV_CMDARG, NULL, -1);
1181
1182 if (err)
1183 {
1184 mch_remove(fname);
1185 return FAIL;
1186 }
1187 return OK;
1188}
1189# endif
1190
1191# if defined(FEAT_DIFF) || defined(PROTO)
1192 void
1193eval_diff(origfile, newfile, outfile)
1194 char_u *origfile;
1195 char_u *newfile;
1196 char_u *outfile;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1201 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1202 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1203 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1206 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1207}
1208
1209 void
1210eval_patch(origfile, difffile, outfile)
1211 char_u *origfile;
1212 char_u *difffile;
1213 char_u *outfile;
1214{
1215 int err;
1216
1217 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1218 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1219 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1220 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1221 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1222 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1223 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1224}
1225# endif
1226
1227/*
1228 * Top level evaluation function, returning a boolean.
1229 * Sets "error" to TRUE if there was an error.
1230 * Return TRUE or FALSE.
1231 */
1232 int
1233eval_to_bool(arg, error, nextcmd, skip)
1234 char_u *arg;
1235 int *error;
1236 char_u **nextcmd;
1237 int skip; /* only parse, don't execute */
1238{
Bram Moolenaar33570922005-01-25 22:26:29 +00001239 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 int retval = FALSE;
1241
1242 if (skip)
1243 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001244 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 else
1247 {
1248 *error = FALSE;
1249 if (!skip)
1250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001251 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001252 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 }
1255 if (skip)
1256 --emsg_skip;
1257
1258 return retval;
1259}
1260
1261/*
1262 * Top level evaluation function, returning a string. If "skip" is TRUE,
1263 * only parsing to "nextcmd" is done, without reporting errors. Return
1264 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1265 */
1266 char_u *
1267eval_to_string_skip(arg, nextcmd, skip)
1268 char_u *arg;
1269 char_u **nextcmd;
1270 int skip; /* only parse, don't execute */
1271{
Bram Moolenaar33570922005-01-25 22:26:29 +00001272 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 char_u *retval;
1274
1275 if (skip)
1276 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 retval = NULL;
1279 else
1280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 retval = vim_strsave(get_tv_string(&tv));
1282 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 if (skip)
1285 --emsg_skip;
1286
1287 return retval;
1288}
1289
1290/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001291 * Skip over an expression at "*pp".
1292 * Return FAIL for an error, OK otherwise.
1293 */
1294 int
1295skip_expr(pp)
1296 char_u **pp;
1297{
Bram Moolenaar33570922005-01-25 22:26:29 +00001298 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001299
1300 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001302}
1303
1304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001306 * When "convert" is TRUE convert a List into a sequence of lines and convert
1307 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 * Return pointer to allocated memory, or NULL for failure.
1309 */
1310 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001311eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 char_u *arg;
1313 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001314 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
Bram Moolenaar33570922005-01-25 22:26:29 +00001316 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001318 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001319#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001320 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001323 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 retval = NULL;
1325 else
1326 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001327 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001328 {
1329 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001330 if (tv.vval.v_list != NULL)
1331 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001332 ga_append(&ga, NUL);
1333 retval = (char_u *)ga.ga_data;
1334 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335#ifdef FEAT_FLOAT
1336 else if (convert && tv.v_type == VAR_FLOAT)
1337 {
1338 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1339 retval = vim_strsave(numbuf);
1340 }
1341#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001342 else
1343 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346
1347 return retval;
1348}
1349
1350/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001351 * Call eval_to_string() without using current local variables and using
1352 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 */
1354 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001355eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 char_u *arg;
1357 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001358 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
1360 char_u *retval;
1361 void *save_funccalp;
1362
1363 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001364 if (use_sandbox)
1365 ++sandbox;
1366 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001367 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001368 if (use_sandbox)
1369 --sandbox;
1370 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 restore_funccal(save_funccalp);
1372 return retval;
1373}
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375/*
1376 * Top level evaluation function, returning a number.
1377 * Evaluates "expr" silently.
1378 * Returns -1 for an error.
1379 */
1380 int
1381eval_to_number(expr)
1382 char_u *expr;
1383{
Bram Moolenaar33570922005-01-25 22:26:29 +00001384 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001386 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388 ++emsg_off;
1389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 retval = -1;
1392 else
1393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001394 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397 --emsg_off;
1398
1399 return retval;
1400}
1401
Bram Moolenaara40058a2005-07-11 22:42:07 +00001402/*
1403 * Prepare v: variable "idx" to be used.
1404 * Save the current typeval in "save_tv".
1405 * When not used yet add the variable to the v: hashtable.
1406 */
1407 static void
1408prepare_vimvar(idx, save_tv)
1409 int idx;
1410 typval_T *save_tv;
1411{
1412 *save_tv = vimvars[idx].vv_tv;
1413 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1414 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1415}
1416
1417/*
1418 * Restore v: variable "idx" to typeval "save_tv".
1419 * When no longer defined, remove the variable from the v: hashtable.
1420 */
1421 static void
1422restore_vimvar(idx, save_tv)
1423 int idx;
1424 typval_T *save_tv;
1425{
1426 hashitem_T *hi;
1427
Bram Moolenaara40058a2005-07-11 22:42:07 +00001428 vimvars[idx].vv_tv = *save_tv;
1429 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1430 {
1431 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1432 if (HASHITEM_EMPTY(hi))
1433 EMSG2(_(e_intern2), "restore_vimvar()");
1434 else
1435 hash_remove(&vimvarht, hi);
1436 }
1437}
1438
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001439#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001440/*
1441 * Evaluate an expression to a list with suggestions.
1442 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001443 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001444 */
1445 list_T *
1446eval_spell_expr(badword, expr)
1447 char_u *badword;
1448 char_u *expr;
1449{
1450 typval_T save_val;
1451 typval_T rettv;
1452 list_T *list = NULL;
1453 char_u *p = skipwhite(expr);
1454
1455 /* Set "v:val" to the bad word. */
1456 prepare_vimvar(VV_VAL, &save_val);
1457 vimvars[VV_VAL].vv_type = VAR_STRING;
1458 vimvars[VV_VAL].vv_str = badword;
1459 if (p_verbose == 0)
1460 ++emsg_off;
1461
1462 if (eval1(&p, &rettv, TRUE) == OK)
1463 {
1464 if (rettv.v_type != VAR_LIST)
1465 clear_tv(&rettv);
1466 else
1467 list = rettv.vval.v_list;
1468 }
1469
1470 if (p_verbose == 0)
1471 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001472 restore_vimvar(VV_VAL, &save_val);
1473
1474 return list;
1475}
1476
1477/*
1478 * "list" is supposed to contain two items: a word and a number. Return the
1479 * word in "pp" and the number as the return value.
1480 * Return -1 if anything isn't right.
1481 * Used to get the good word and score from the eval_spell_expr() result.
1482 */
1483 int
1484get_spellword(list, pp)
1485 list_T *list;
1486 char_u **pp;
1487{
1488 listitem_T *li;
1489
1490 li = list->lv_first;
1491 if (li == NULL)
1492 return -1;
1493 *pp = get_tv_string(&li->li_tv);
1494
1495 li = li->li_next;
1496 if (li == NULL)
1497 return -1;
1498 return get_tv_number(&li->li_tv);
1499}
1500#endif
1501
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001502/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001503 * Top level evaluation function.
1504 * Returns an allocated typval_T with the result.
1505 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001506 */
1507 typval_T *
1508eval_expr(arg, nextcmd)
1509 char_u *arg;
1510 char_u **nextcmd;
1511{
1512 typval_T *tv;
1513
1514 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001515 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001516 {
1517 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001518 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001519 }
1520
1521 return tv;
1522}
1523
1524
Bram Moolenaar4f688582007-07-24 12:34:30 +00001525#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1526 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001528 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001529 * Uses argv[argc] for the function arguments. Only Number and String
1530 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001531 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001533 static int
1534call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 char_u *func;
1536 int argc;
1537 char_u **argv;
1538 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
Bram Moolenaar33570922005-01-25 22:26:29 +00001541 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 long n;
1543 int len;
1544 int i;
1545 int doesrange;
1546 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001547 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001549 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001551 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
1553 for (i = 0; i < argc; i++)
1554 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001555 /* Pass a NULL or empty argument as an empty string */
1556 if (argv[i] == NULL || *argv[i] == NUL)
1557 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001558 argvars[i].v_type = VAR_STRING;
1559 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001560 continue;
1561 }
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 /* Recognize a number argument, the others must be strings. */
1564 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1565 if (len != 0 && len == (int)STRLEN(argv[i]))
1566 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001567 argvars[i].v_type = VAR_NUMBER;
1568 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
1570 else
1571 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001572 argvars[i].v_type = VAR_STRING;
1573 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
1575 }
1576
1577 if (safe)
1578 {
1579 save_funccalp = save_funccal();
1580 ++sandbox;
1581 }
1582
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001583 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1584 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (safe)
1588 {
1589 --sandbox;
1590 restore_funccal(save_funccalp);
1591 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 vim_free(argvars);
1593
1594 if (ret == FAIL)
1595 clear_tv(rettv);
1596
1597 return ret;
1598}
1599
Bram Moolenaar4f688582007-07-24 12:34:30 +00001600# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001602 * Call vimL function "func" and return the result as a string.
1603 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001604 * Uses argv[argc] for the function arguments.
1605 */
1606 void *
1607call_func_retstr(func, argc, argv, safe)
1608 char_u *func;
1609 int argc;
1610 char_u **argv;
1611 int safe; /* use the sandbox */
1612{
1613 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001614 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615
1616 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1617 return NULL;
1618
1619 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 return retval;
1622}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001623# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624
Bram Moolenaar4f688582007-07-24 12:34:30 +00001625# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001627 * Call vimL function "func" and return the result as a number.
1628 * Returns -1 when calling the function fails.
1629 * Uses argv[argc] for the function arguments.
1630 */
1631 long
1632call_func_retnr(func, argc, argv, safe)
1633 char_u *func;
1634 int argc;
1635 char_u **argv;
1636 int safe; /* use the sandbox */
1637{
1638 typval_T rettv;
1639 long retval;
1640
1641 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1642 return -1;
1643
1644 retval = get_tv_number_chk(&rettv, NULL);
1645 clear_tv(&rettv);
1646 return retval;
1647}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001648# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001649
1650/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001651 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001653 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654 */
1655 void *
1656call_func_retlist(func, argc, argv, safe)
1657 char_u *func;
1658 int argc;
1659 char_u **argv;
1660 int safe; /* use the sandbox */
1661{
1662 typval_T rettv;
1663
1664 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1665 return NULL;
1666
1667 if (rettv.v_type != VAR_LIST)
1668 {
1669 clear_tv(&rettv);
1670 return NULL;
1671 }
1672
1673 return rettv.vval.v_list;
1674}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675#endif
1676
Bram Moolenaar4f688582007-07-24 12:34:30 +00001677
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678/*
1679 * Save the current function call pointer, and set it to NULL.
1680 * Used when executing autocommands and for ":source".
1681 */
1682 void *
1683save_funccal()
1684{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001685 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 current_funccal = NULL;
1688 return (void *)fc;
1689}
1690
1691 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001692restore_funccal(vfc)
1693 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001695 funccall_T *fc = (funccall_T *)vfc;
1696
1697 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698}
1699
Bram Moolenaar05159a02005-02-26 23:04:13 +00001700#if defined(FEAT_PROFILE) || defined(PROTO)
1701/*
1702 * Prepare profiling for entering a child or something else that is not
1703 * counted for the script/function itself.
1704 * Should always be called in pair with prof_child_exit().
1705 */
1706 void
1707prof_child_enter(tm)
1708 proftime_T *tm; /* place to store waittime */
1709{
1710 funccall_T *fc = current_funccal;
1711
1712 if (fc != NULL && fc->func->uf_profiling)
1713 profile_start(&fc->prof_child);
1714 script_prof_save(tm);
1715}
1716
1717/*
1718 * Take care of time spent in a child.
1719 * Should always be called after prof_child_enter().
1720 */
1721 void
1722prof_child_exit(tm)
1723 proftime_T *tm; /* where waittime was stored */
1724{
1725 funccall_T *fc = current_funccal;
1726
1727 if (fc != NULL && fc->func->uf_profiling)
1728 {
1729 profile_end(&fc->prof_child);
1730 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1731 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1732 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1733 }
1734 script_prof_restore(tm);
1735}
1736#endif
1737
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739#ifdef FEAT_FOLDING
1740/*
1741 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1742 * it in "*cp". Doesn't give error messages.
1743 */
1744 int
1745eval_foldexpr(arg, cp)
1746 char_u *arg;
1747 int *cp;
1748{
Bram Moolenaar33570922005-01-25 22:26:29 +00001749 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 int retval;
1751 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001752 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1753 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001756 if (use_sandbox)
1757 ++sandbox;
1758 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 retval = 0;
1762 else
1763 {
1764 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 if (tv.v_type == VAR_NUMBER)
1766 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001767 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 retval = 0;
1769 else
1770 {
1771 /* If the result is a string, check if there is a non-digit before
1772 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 if (!VIM_ISDIGIT(*s) && *s != '-')
1775 *cp = *s++;
1776 retval = atol((char *)s);
1777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 }
1780 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001781 if (use_sandbox)
1782 --sandbox;
1783 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785 return retval;
1786}
1787#endif
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790 * ":let" list all variable values
1791 * ":let var1 var2" list variable values
1792 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 * ":let var += expr" assignment command.
1794 * ":let var -= expr" assignment command.
1795 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 */
1798 void
1799ex_let(eap)
1800 exarg_T *eap;
1801{
1802 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001804 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 int var_count = 0;
1807 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001808 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001809 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001810 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
Bram Moolenaardb552d602006-03-23 22:59:57 +00001812 argend = skip_var_list(arg, &var_count, &semicolon);
1813 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001815 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1816 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001817 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001820 /*
1821 * ":let" without "=": list variables
1822 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 if (*arg == '[')
1824 EMSG(_(e_invarg));
1825 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001827 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001831 list_glob_vars(&first);
1832 list_buf_vars(&first);
1833 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001834#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001835 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001836#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001837 list_script_vars(&first);
1838 list_func_vars(&first);
1839 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 eap->nextcmd = check_nextcmd(arg);
1842 }
1843 else
1844 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 op[0] = '=';
1846 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001847 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001848 {
1849 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1850 op[0] = expr[-1]; /* +=, -= or .= */
1851 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (eap->skip)
1855 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if (eap->skip)
1858 {
1859 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 --emsg_skip;
1862 }
1863 else if (i != FAIL)
1864 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001866 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869 }
1870}
1871
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872/*
1873 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1874 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875 * When "nextchars" is not NULL it points to a string with characters that
1876 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1877 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 * Returns OK or FAIL;
1879 */
1880 static int
1881ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1882 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001883 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 int copy; /* copy values from "tv", don't move */
1885 int semicolon; /* from skip_var_list() */
1886 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001887 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888{
1889 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001890 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001892 listitem_T *item;
1893 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894
1895 if (*arg != '[')
1896 {
1897 /*
1898 * ":let var = expr" or ":for var in list"
1899 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 return FAIL;
1902 return OK;
1903 }
1904
1905 /*
1906 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1907 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001908 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 {
1910 EMSG(_(e_listreq));
1911 return FAIL;
1912 }
1913
1914 i = list_len(l);
1915 if (semicolon == 0 && var_count < i)
1916 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001917 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 return FAIL;
1919 }
1920 if (var_count - semicolon > i)
1921 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001922 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 return FAIL;
1924 }
1925
1926 item = l->lv_first;
1927 while (*arg != ']')
1928 {
1929 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001930 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 item = item->li_next;
1932 if (arg == NULL)
1933 return FAIL;
1934
1935 arg = skipwhite(arg);
1936 if (*arg == ';')
1937 {
1938 /* Put the rest of the list (may be empty) in the var after ';'.
1939 * Create a new list for this. */
1940 l = list_alloc();
1941 if (l == NULL)
1942 return FAIL;
1943 while (item != NULL)
1944 {
1945 list_append_tv(l, &item->li_tv);
1946 item = item->li_next;
1947 }
1948
1949 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001950 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 ltv.vval.v_list = l;
1952 l->lv_refcount = 1;
1953
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001954 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1955 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 clear_tv(&ltv);
1957 if (arg == NULL)
1958 return FAIL;
1959 break;
1960 }
1961 else if (*arg != ',' && *arg != ']')
1962 {
1963 EMSG2(_(e_intern2), "ex_let_vars()");
1964 return FAIL;
1965 }
1966 }
1967
1968 return OK;
1969}
1970
1971/*
1972 * Skip over assignable variable "var" or list of variables "[var, var]".
1973 * Used for ":let varvar = expr" and ":for varvar in expr".
1974 * For "[var, var]" increment "*var_count" for each variable.
1975 * for "[var, var; var]" set "semicolon".
1976 * Return NULL for an error.
1977 */
1978 static char_u *
1979skip_var_list(arg, var_count, semicolon)
1980 char_u *arg;
1981 int *var_count;
1982 int *semicolon;
1983{
1984 char_u *p, *s;
1985
1986 if (*arg == '[')
1987 {
1988 /* "[var, var]": find the matching ']'. */
1989 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001990 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 {
1992 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1993 s = skip_var_one(p);
1994 if (s == p)
1995 {
1996 EMSG2(_(e_invarg2), p);
1997 return NULL;
1998 }
1999 ++*var_count;
2000
2001 p = skipwhite(s);
2002 if (*p == ']')
2003 break;
2004 else if (*p == ';')
2005 {
2006 if (*semicolon == 1)
2007 {
2008 EMSG(_("Double ; in list of variables"));
2009 return NULL;
2010 }
2011 *semicolon = 1;
2012 }
2013 else if (*p != ',')
2014 {
2015 EMSG2(_(e_invarg2), p);
2016 return NULL;
2017 }
2018 }
2019 return p + 1;
2020 }
2021 else
2022 return skip_var_one(arg);
2023}
2024
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002025/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002026 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002027 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002028 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 static char_u *
2030skip_var_one(arg)
2031 char_u *arg;
2032{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002033 if (*arg == '@' && arg[1] != NUL)
2034 return arg + 2;
2035 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2036 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037}
2038
Bram Moolenaara7043832005-01-21 11:56:39 +00002039/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002040 * List variables for hashtab "ht" with prefix "prefix".
2041 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002042 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002044list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002045 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002046 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002047 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002048 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002049{
Bram Moolenaar33570922005-01-25 22:26:29 +00002050 hashitem_T *hi;
2051 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002052 int todo;
2053
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002054 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002055 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2056 {
2057 if (!HASHITEM_EMPTY(hi))
2058 {
2059 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002060 di = HI2DI(hi);
2061 if (empty || di->di_tv.v_type != VAR_STRING
2062 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002063 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002064 }
2065 }
2066}
2067
2068/*
2069 * List global variables.
2070 */
2071 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002072list_glob_vars(first)
2073 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002075 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002076}
2077
2078/*
2079 * List buffer variables.
2080 */
2081 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082list_buf_vars(first)
2083 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 char_u numbuf[NUMBUFLEN];
2086
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002087 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2088 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002089
2090 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2092 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002093}
2094
2095/*
2096 * List window variables.
2097 */
2098 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002099list_win_vars(first)
2100 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002101{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002102 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2103 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104}
2105
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002106#ifdef FEAT_WINDOWS
2107/*
2108 * List tab page variables.
2109 */
2110 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111list_tab_vars(first)
2112 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002113{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2115 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002116}
2117#endif
2118
Bram Moolenaara7043832005-01-21 11:56:39 +00002119/*
2120 * List Vim variables.
2121 */
2122 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123list_vim_vars(first)
2124 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127}
2128
2129/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002130 * List script-local variables, if there is a script.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_script_vars(first)
2134 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002135{
2136 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2138 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002139}
2140
2141/*
2142 * List function variables, if there is a function.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_func_vars(first)
2146 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002147{
2148 if (current_funccal != NULL)
2149 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002151}
2152
2153/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154 * List variables in "arg".
2155 */
2156 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158 exarg_T *eap;
2159 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161{
2162 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002165 char_u *name_start;
2166 char_u *arg_subsc;
2167 char_u *tofree;
2168 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002169
2170 while (!ends_excmd(*arg) && !got_int)
2171 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002174 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2176 {
2177 emsg_severe = TRUE;
2178 EMSG(_(e_trailing));
2179 break;
2180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002184 /* get_name_len() takes care of expanding curly braces */
2185 name_start = name = arg;
2186 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2187 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002189 /* This is mainly to keep test 49 working: when expanding
2190 * curly braces fails overrule the exception error message. */
2191 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 emsg_severe = TRUE;
2194 EMSG2(_(e_invarg2), arg);
2195 break;
2196 }
2197 error = TRUE;
2198 }
2199 else
2200 {
2201 if (tofree != NULL)
2202 name = tofree;
2203 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 else
2206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 /* handle d.key, l[idx], f(expr) */
2208 arg_subsc = arg;
2209 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002210 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002214 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002216 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 case 'g': list_glob_vars(first); break;
2218 case 'b': list_buf_vars(first); break;
2219 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002220#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002222#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 case 'v': list_vim_vars(first); break;
2224 case 's': list_script_vars(first); break;
2225 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 default:
2227 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002228 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002229 }
2230 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 {
2232 char_u numbuf[NUMBUFLEN];
2233 char_u *tf;
2234 int c;
2235 char_u *s;
2236
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002237 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 c = *arg;
2239 *arg = NUL;
2240 list_one_var_a((char_u *)"",
2241 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 tv.v_type,
2243 s == NULL ? (char_u *)"" : s,
2244 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 *arg = c;
2246 vim_free(tf);
2247 }
2248 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 }
2251 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252
2253 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255
2256 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 }
2258
2259 return arg;
2260}
2261
2262/*
2263 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2264 * Returns a pointer to the char just after the var name.
2265 * Returns NULL if there is an error.
2266 */
2267 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002270 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002272 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002273 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274{
2275 int c1;
2276 char_u *name;
2277 char_u *p;
2278 char_u *arg_end = NULL;
2279 int len;
2280 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002281 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282
2283 /*
2284 * ":let $VAR = expr": Set environment variable.
2285 */
2286 if (*arg == '$')
2287 {
2288 /* Find the end of the name. */
2289 ++arg;
2290 name = arg;
2291 len = get_env_len(&arg);
2292 if (len == 0)
2293 EMSG2(_(e_invarg2), name - 1);
2294 else
2295 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296 if (op != NULL && (*op == '+' || *op == '-'))
2297 EMSG2(_(e_letwrong), op);
2298 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002299 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 EMSG(_(e_letunexp));
2301 else
2302 {
2303 c1 = name[len];
2304 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002305 p = get_tv_string_chk(tv);
2306 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 {
2308 int mustfree = FALSE;
2309 char_u *s = vim_getenv(name, &mustfree);
2310
2311 if (s != NULL)
2312 {
2313 p = tofree = concat_str(s, p);
2314 if (mustfree)
2315 vim_free(s);
2316 }
2317 }
2318 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002319 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002321 if (STRICMP(name, "HOME") == 0)
2322 init_homedir();
2323 else if (didset_vim && STRICMP(name, "VIM") == 0)
2324 didset_vim = FALSE;
2325 else if (didset_vimruntime
2326 && STRICMP(name, "VIMRUNTIME") == 0)
2327 didset_vimruntime = FALSE;
2328 arg_end = arg;
2329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 }
2333 }
2334 }
2335
2336 /*
2337 * ":let &option = expr": Set option value.
2338 * ":let &l:option = expr": Set local option value.
2339 * ":let &g:option = expr": Set global option value.
2340 */
2341 else if (*arg == '&')
2342 {
2343 /* Find the end of the name. */
2344 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345 if (p == NULL || (endchars != NULL
2346 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 EMSG(_(e_letunexp));
2348 else
2349 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 long n;
2351 int opt_type;
2352 long numval;
2353 char_u *stringval = NULL;
2354 char_u *s;
2355
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 c1 = *p;
2357 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358
2359 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 s = get_tv_string_chk(tv); /* != NULL if number or string */
2361 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 {
2363 opt_type = get_option_value(arg, &numval,
2364 &stringval, opt_flags);
2365 if ((opt_type == 1 && *op == '.')
2366 || (opt_type == 0 && *op != '.'))
2367 EMSG2(_(e_letwrong), op);
2368 else
2369 {
2370 if (opt_type == 1) /* number */
2371 {
2372 if (*op == '+')
2373 n = numval + n;
2374 else
2375 n = numval - n;
2376 }
2377 else if (opt_type == 0 && stringval != NULL) /* string */
2378 {
2379 s = concat_str(stringval, s);
2380 vim_free(stringval);
2381 stringval = s;
2382 }
2383 }
2384 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002385 if (s != NULL)
2386 {
2387 set_option_value(arg, n, s, opt_flags);
2388 arg_end = p;
2389 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 }
2393 }
2394
2395 /*
2396 * ":let @r = expr": Set register contents.
2397 */
2398 else if (*arg == '@')
2399 {
2400 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 if (op != NULL && (*op == '+' || *op == '-'))
2402 EMSG2(_(e_letwrong), op);
2403 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002404 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 EMSG(_(e_letunexp));
2406 else
2407 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002408 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 char_u *s;
2410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002411 p = get_tv_string_chk(tv);
2412 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002414 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (s != NULL)
2416 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002417 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 vim_free(s);
2419 }
2420 }
2421 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 arg_end = arg + 1;
2425 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002426 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 }
2428 }
2429
2430 /*
2431 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002434 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002436 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002438 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2442 EMSG(_(e_letunexp));
2443 else
2444 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 arg_end = p;
2447 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 }
2451
2452 else
2453 EMSG2(_(e_invarg2), arg);
2454
2455 return arg_end;
2456}
2457
2458/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2460 */
2461 static int
2462check_changedtick(arg)
2463 char_u *arg;
2464{
2465 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2466 {
2467 EMSG2(_(e_readonlyvar), arg);
2468 return TRUE;
2469 }
2470 return FALSE;
2471}
2472
2473/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 * Get an lval: variable, Dict item or List item that can be assigned a value
2475 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2476 * "name.key", "name.key[expr]" etc.
2477 * Indexing only works if "name" is an existing List or Dictionary.
2478 * "name" points to the start of the name.
2479 * If "rettv" is not NULL it points to the value to be assigned.
2480 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2481 * wrong; must end in space or cmd separator.
2482 *
2483 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002484 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 */
2487 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002488get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002490 typval_T *rettv;
2491 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 int unlet;
2493 int skip;
2494 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002495 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 char_u *expr_start, *expr_end;
2499 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002500 dictitem_T *v;
2501 typval_T var1;
2502 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002504 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002510 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511
2512 if (skip)
2513 {
2514 /* When skipping just find the end of the name. */
2515 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 }
2518
2519 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002520 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (expr_start != NULL)
2522 {
2523 /* Don't expand the name when we already know there is an error. */
2524 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2525 && *p != '[' && *p != '.')
2526 {
2527 EMSG(_(e_trailing));
2528 return NULL;
2529 }
2530
2531 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2532 if (lp->ll_exp_name == NULL)
2533 {
2534 /* Report an invalid expression in braces, unless the
2535 * expression evaluation has been cancelled due to an
2536 * aborting error, an interrupt, or an exception. */
2537 if (!aborting() && !quiet)
2538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002539 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 EMSG2(_(e_invarg2), name);
2541 return NULL;
2542 }
2543 }
2544 lp->ll_name = lp->ll_exp_name;
2545 }
2546 else
2547 lp->ll_name = name;
2548
2549 /* Without [idx] or .key we are done. */
2550 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2551 return p;
2552
2553 cc = *p;
2554 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002555 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (v == NULL && !quiet)
2557 EMSG2(_(e_undefvar), lp->ll_name);
2558 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 if (v == NULL)
2560 return NULL;
2561
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 /*
2563 * Loop until no more [idx] or .key is following.
2564 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2569 && !(lp->ll_tv->v_type == VAR_DICT
2570 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 if (!quiet)
2573 EMSG(_("E689: Can only index a List or Dictionary"));
2574 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002575 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 if (!quiet)
2579 EMSG(_("E708: [:] must come last"));
2580 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002582
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 len = -1;
2584 if (*p == '.')
2585 {
2586 key = p + 1;
2587 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2588 ;
2589 if (len == 0)
2590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (!quiet)
2592 EMSG(_(e_emptykey));
2593 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 }
2595 p = key + len;
2596 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002597 else
2598 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002600 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 if (*p == ':')
2602 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002603 else
2604 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 empty1 = FALSE;
2606 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002608 if (get_tv_string_chk(&var1) == NULL)
2609 {
2610 /* not a number or string */
2611 clear_tv(&var1);
2612 return NULL;
2613 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 }
2615
2616 /* Optionally get the second index [ :expr]. */
2617 if (*p == ':')
2618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002622 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623 if (!empty1)
2624 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (rettv != NULL && (rettv->v_type != VAR_LIST
2628 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!quiet)
2631 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 if (!empty1)
2633 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 }
2636 p = skipwhite(p + 1);
2637 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 else
2640 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 if (!empty1)
2645 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002648 if (get_tv_string_chk(&var2) == NULL)
2649 {
2650 /* not a number or string */
2651 if (!empty1)
2652 clear_tv(&var1);
2653 clear_tv(&var2);
2654 return NULL;
2655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 }
2672
2673 /* Skip to past ']'. */
2674 ++p;
2675 }
2676
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 {
2679 if (len == -1)
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 if (*key == NUL)
2684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
2690 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002691 lp->ll_list = NULL;
2692 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002693 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002696 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 if (len == -1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (len == -1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 p = NULL;
2713 break;
2714 }
2715 if (len == -1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 else
2720 {
2721 /*
2722 * Get the number and item for the only or first index of the List.
2723 */
2724 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 else
2727 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 clear_tv(&var1);
2730 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002731 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_list = lp->ll_tv->vval.v_list;
2733 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2734 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002736 if (lp->ll_n1 < 0)
2737 {
2738 lp->ll_n1 = 0;
2739 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2740 }
2741 }
2742 if (lp->ll_li == NULL)
2743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 }
2748
2749 /*
2750 * May need to find the item or absolute index for the second
2751 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 * When no index given: "lp->ll_empty2" is TRUE.
2753 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 }
2766
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2768 if (lp->ll_n1 < 0)
2769 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2770 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002772 }
2773
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002776 }
2777
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 return p;
2779}
2780
2781/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002782 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 */
2784 static void
2785clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002786 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787{
2788 vim_free(lp->ll_exp_name);
2789 vim_free(lp->ll_newkey);
2790}
2791
2792/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002793 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 */
2797 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002798set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002799 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002801 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804{
2805 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002806 listitem_T *ri;
2807 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808
2809 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002810 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 cc = *endp;
2814 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002815 if (op != NULL && *op != '=')
2816 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002817 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002819 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002820 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002821 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002822 {
2823 if (tv_op(&tv, rettv, op) == OK)
2824 set_var(lp->ll_name, &tv, FALSE);
2825 clear_tv(&tv);
2826 }
2827 }
2828 else
2829 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002831 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002833 else if (tv_check_lock(lp->ll_newkey == NULL
2834 ? lp->ll_tv->v_lock
2835 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2836 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 else if (lp->ll_range)
2838 {
2839 /*
2840 * Assign the List values to the list items.
2841 */
2842 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002843 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002844 if (op != NULL && *op != '=')
2845 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2846 else
2847 {
2848 clear_tv(&lp->ll_li->li_tv);
2849 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2850 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 ri = ri->li_next;
2852 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2853 break;
2854 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002855 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002857 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002858 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 ri = NULL;
2860 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002861 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002862 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_li = lp->ll_li->li_next;
2864 ++lp->ll_n1;
2865 }
2866 if (ri != NULL)
2867 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002868 else if (lp->ll_empty2
2869 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 : lp->ll_n1 != lp->ll_n2)
2871 EMSG(_("E711: List value has not enough items"));
2872 }
2873 else
2874 {
2875 /*
2876 * Assign to a List or Dictionary item.
2877 */
2878 if (lp->ll_newkey != NULL)
2879 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880 if (op != NULL && *op != '=')
2881 {
2882 EMSG2(_(e_letwrong), op);
2883 return;
2884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002887 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 if (di == NULL)
2889 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002890 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2891 {
2892 vim_free(di);
2893 return;
2894 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002896 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897 else if (op != NULL && *op != '=')
2898 {
2899 tv_op(lp->ll_tv, rettv, op);
2900 return;
2901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002902 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002904
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 /*
2906 * Assign the value to the variable or list item.
2907 */
2908 if (copy)
2909 copy_tv(rettv, lp->ll_tv);
2910 else
2911 {
2912 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002913 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 }
2916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917}
2918
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2921 * Returns OK or FAIL.
2922 */
2923 static int
2924tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002925 typval_T *tv1;
2926 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927 char_u *op;
2928{
2929 long n;
2930 char_u numbuf[NUMBUFLEN];
2931 char_u *s;
2932
2933 /* Can't do anything with a Funcref or a Dict on the right. */
2934 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2935 {
2936 switch (tv1->v_type)
2937 {
2938 case VAR_DICT:
2939 case VAR_FUNC:
2940 break;
2941
2942 case VAR_LIST:
2943 if (*op != '+' || tv2->v_type != VAR_LIST)
2944 break;
2945 /* List += List */
2946 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2947 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2948 return OK;
2949
2950 case VAR_NUMBER:
2951 case VAR_STRING:
2952 if (tv2->v_type == VAR_LIST)
2953 break;
2954 if (*op == '+' || *op == '-')
2955 {
2956 /* nr += nr or nr -= nr*/
2957 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002958#ifdef FEAT_FLOAT
2959 if (tv2->v_type == VAR_FLOAT)
2960 {
2961 float_T f = n;
2962
2963 if (*op == '+')
2964 f += tv2->vval.v_float;
2965 else
2966 f -= tv2->vval.v_float;
2967 clear_tv(tv1);
2968 tv1->v_type = VAR_FLOAT;
2969 tv1->vval.v_float = f;
2970 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002971 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002972#endif
2973 {
2974 if (*op == '+')
2975 n += get_tv_number(tv2);
2976 else
2977 n -= get_tv_number(tv2);
2978 clear_tv(tv1);
2979 tv1->v_type = VAR_NUMBER;
2980 tv1->vval.v_number = n;
2981 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 }
2983 else
2984 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985 if (tv2->v_type == VAR_FLOAT)
2986 break;
2987
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002988 /* str .= str */
2989 s = get_tv_string(tv1);
2990 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2991 clear_tv(tv1);
2992 tv1->v_type = VAR_STRING;
2993 tv1->vval.v_string = s;
2994 }
2995 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002996
2997#ifdef FEAT_FLOAT
2998 case VAR_FLOAT:
2999 {
3000 float_T f;
3001
3002 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3003 && tv2->v_type != VAR_NUMBER
3004 && tv2->v_type != VAR_STRING))
3005 break;
3006 if (tv2->v_type == VAR_FLOAT)
3007 f = tv2->vval.v_float;
3008 else
3009 f = get_tv_number(tv2);
3010 if (*op == '+')
3011 tv1->vval.v_float += f;
3012 else
3013 tv1->vval.v_float -= f;
3014 }
3015 return OK;
3016#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 }
3018 }
3019
3020 EMSG2(_(e_letwrong), op);
3021 return FAIL;
3022}
3023
3024/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003025 * Add a watcher to a list.
3026 */
3027 static void
3028list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003029 list_T *l;
3030 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003031{
3032 lw->lw_next = l->lv_watch;
3033 l->lv_watch = lw;
3034}
3035
3036/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003037 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003038 * No warning when it isn't found...
3039 */
3040 static void
3041list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003042 list_T *l;
3043 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044{
Bram Moolenaar33570922005-01-25 22:26:29 +00003045 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003046
3047 lwp = &l->lv_watch;
3048 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3049 {
3050 if (lw == lwrem)
3051 {
3052 *lwp = lw->lw_next;
3053 break;
3054 }
3055 lwp = &lw->lw_next;
3056 }
3057}
3058
3059/*
3060 * Just before removing an item from a list: advance watchers to the next
3061 * item.
3062 */
3063 static void
3064list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003065 list_T *l;
3066 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003067{
Bram Moolenaar33570922005-01-25 22:26:29 +00003068 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003069
3070 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3071 if (lw->lw_item == item)
3072 lw->lw_item = item->li_next;
3073}
3074
3075/*
3076 * Evaluate the expression used in a ":for var in expr" command.
3077 * "arg" points to "var".
3078 * Set "*errp" to TRUE for an error, FALSE otherwise;
3079 * Return a pointer that holds the info. Null when there is an error.
3080 */
3081 void *
3082eval_for_line(arg, errp, nextcmdp, skip)
3083 char_u *arg;
3084 int *errp;
3085 char_u **nextcmdp;
3086 int skip;
3087{
Bram Moolenaar33570922005-01-25 22:26:29 +00003088 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003089 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 typval_T tv;
3091 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092
3093 *errp = TRUE; /* default: there is an error */
3094
Bram Moolenaar33570922005-01-25 22:26:29 +00003095 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096 if (fi == NULL)
3097 return NULL;
3098
3099 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3100 if (expr == NULL)
3101 return fi;
3102
3103 expr = skipwhite(expr);
3104 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3105 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003106 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107 return fi;
3108 }
3109
3110 if (skip)
3111 ++emsg_skip;
3112 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3113 {
3114 *errp = FALSE;
3115 if (!skip)
3116 {
3117 l = tv.vval.v_list;
3118 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003119 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003121 clear_tv(&tv);
3122 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123 else
3124 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003125 /* No need to increment the refcount, it's already set for the
3126 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127 fi->fi_list = l;
3128 list_add_watch(l, &fi->fi_lw);
3129 fi->fi_lw.lw_item = l->lv_first;
3130 }
3131 }
3132 }
3133 if (skip)
3134 --emsg_skip;
3135
3136 return fi;
3137}
3138
3139/*
3140 * Use the first item in a ":for" list. Advance to the next.
3141 * Assign the values to the variable (list). "arg" points to the first one.
3142 * Return TRUE when a valid item was found, FALSE when at end of list or
3143 * something wrong.
3144 */
3145 int
3146next_for_item(fi_void, arg)
3147 void *fi_void;
3148 char_u *arg;
3149{
Bram Moolenaar33570922005-01-25 22:26:29 +00003150 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153
3154 item = fi->fi_lw.lw_item;
3155 if (item == NULL)
3156 result = FALSE;
3157 else
3158 {
3159 fi->fi_lw.lw_item = item->li_next;
3160 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3161 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3162 }
3163 return result;
3164}
3165
3166/*
3167 * Free the structure used to store info used by ":for".
3168 */
3169 void
3170free_for_info(fi_void)
3171 void *fi_void;
3172{
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003175 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003176 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003178 list_unref(fi->fi_list);
3179 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180 vim_free(fi);
3181}
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3184
3185 void
3186set_context_for_expression(xp, arg, cmdidx)
3187 expand_T *xp;
3188 char_u *arg;
3189 cmdidx_T cmdidx;
3190{
3191 int got_eq = FALSE;
3192 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 if (cmdidx == CMD_let)
3196 {
3197 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003198 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 {
3200 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003201 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 {
3203 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 if (vim_iswhite(*p))
3206 break;
3207 }
3208 return;
3209 }
3210 }
3211 else
3212 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3213 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 while ((xp->xp_pattern = vim_strpbrk(arg,
3215 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3216 {
3217 c = *xp->xp_pattern;
3218 if (c == '&')
3219 {
3220 c = xp->xp_pattern[1];
3221 if (c == '&')
3222 {
3223 ++xp->xp_pattern;
3224 xp->xp_context = cmdidx != CMD_let || got_eq
3225 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3226 }
3227 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003230 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3231 xp->xp_pattern += 2;
3232
3233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 }
3235 else if (c == '$')
3236 {
3237 /* environment variable */
3238 xp->xp_context = EXPAND_ENV_VARS;
3239 }
3240 else if (c == '=')
3241 {
3242 got_eq = TRUE;
3243 xp->xp_context = EXPAND_EXPRESSION;
3244 }
3245 else if (c == '<'
3246 && xp->xp_context == EXPAND_FUNCTIONS
3247 && vim_strchr(xp->xp_pattern, '(') == NULL)
3248 {
3249 /* Function name can start with "<SNR>" */
3250 break;
3251 }
3252 else if (cmdidx != CMD_let || got_eq)
3253 {
3254 if (c == '"') /* string */
3255 {
3256 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3257 if (c == '\\' && xp->xp_pattern[1] != NUL)
3258 ++xp->xp_pattern;
3259 xp->xp_context = EXPAND_NOTHING;
3260 }
3261 else if (c == '\'') /* literal string */
3262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3265 /* skip */ ;
3266 xp->xp_context = EXPAND_NOTHING;
3267 }
3268 else if (c == '|')
3269 {
3270 if (xp->xp_pattern[1] == '|')
3271 {
3272 ++xp->xp_pattern;
3273 xp->xp_context = EXPAND_EXPRESSION;
3274 }
3275 else
3276 xp->xp_context = EXPAND_COMMANDS;
3277 }
3278 else
3279 xp->xp_context = EXPAND_EXPRESSION;
3280 }
3281 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 /* Doesn't look like something valid, expand as an expression
3283 * anyway. */
3284 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 arg = xp->xp_pattern;
3286 if (*arg != NUL)
3287 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3288 /* skip */ ;
3289 }
3290 xp->xp_pattern = arg;
3291}
3292
3293#endif /* FEAT_CMDL_COMPL */
3294
3295/*
3296 * ":1,25call func(arg1, arg2)" function call.
3297 */
3298 void
3299ex_call(eap)
3300 exarg_T *eap;
3301{
3302 char_u *arg = eap->arg;
3303 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003305 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003307 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 linenr_T lnum;
3309 int doesrange;
3310 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003311 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003313 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003314 if (fudi.fd_newkey != NULL)
3315 {
3316 /* Still need to give an error message for missing key. */
3317 EMSG2(_(e_dictkey), fudi.fd_newkey);
3318 vim_free(fudi.fd_newkey);
3319 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003320 if (tofree == NULL)
3321 return;
3322
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003323 /* Increase refcount on dictionary, it could get deleted when evaluating
3324 * the arguments. */
3325 if (fudi.fd_dict != NULL)
3326 ++fudi.fd_dict->dv_refcount;
3327
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003328 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003329 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003330 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar532c7802005-01-27 14:44:31 +00003332 /* Skip white space to allow ":call func ()". Not good, but required for
3333 * backward compatibility. */
3334 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003335 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
3337 if (*startarg != '(')
3338 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003339 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 goto end;
3341 }
3342
3343 /*
3344 * When skipping, evaluate the function once, to find the end of the
3345 * arguments.
3346 * When the function takes a range, this is discovered after the first
3347 * call, and the loop is broken.
3348 */
3349 if (eap->skip)
3350 {
3351 ++emsg_skip;
3352 lnum = eap->line2; /* do it once, also with an invalid range */
3353 }
3354 else
3355 lnum = eap->line1;
3356 for ( ; lnum <= eap->line2; ++lnum)
3357 {
3358 if (!eap->skip && eap->addr_count > 0)
3359 {
3360 curwin->w_cursor.lnum = lnum;
3361 curwin->w_cursor.col = 0;
3362 }
3363 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003364 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003365 eap->line1, eap->line2, &doesrange,
3366 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 failed = TRUE;
3369 break;
3370 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003371
3372 /* Handle a function returning a Funcref, Dictionary or List. */
3373 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3374 {
3375 failed = TRUE;
3376 break;
3377 }
3378
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003379 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 if (doesrange || eap->skip)
3381 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003382
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003384 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003385 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003386 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 if (aborting())
3388 break;
3389 }
3390 if (eap->skip)
3391 --emsg_skip;
3392
3393 if (!failed)
3394 {
3395 /* Check for trailing illegal characters and a following command. */
3396 if (!ends_excmd(*arg))
3397 {
3398 emsg_severe = TRUE;
3399 EMSG(_(e_trailing));
3400 }
3401 else
3402 eap->nextcmd = check_nextcmd(arg);
3403 }
3404
3405end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003406 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003407 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408}
3409
3410/*
3411 * ":unlet[!] var1 ... " command.
3412 */
3413 void
3414ex_unlet(eap)
3415 exarg_T *eap;
3416{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003417 ex_unletlock(eap, eap->arg, 0);
3418}
3419
3420/*
3421 * ":lockvar" and ":unlockvar" commands
3422 */
3423 void
3424ex_lockvar(eap)
3425 exarg_T *eap;
3426{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003428 int deep = 2;
3429
3430 if (eap->forceit)
3431 deep = -1;
3432 else if (vim_isdigit(*arg))
3433 {
3434 deep = getdigits(&arg);
3435 arg = skipwhite(arg);
3436 }
3437
3438 ex_unletlock(eap, arg, deep);
3439}
3440
3441/*
3442 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3443 */
3444 static void
3445ex_unletlock(eap, argstart, deep)
3446 exarg_T *eap;
3447 char_u *argstart;
3448 int deep;
3449{
3450 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003453 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 do
3456 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003457 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003458 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3459 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003460 if (lv.ll_name == NULL)
3461 error = TRUE; /* error but continue parsing */
3462 if (name_end == NULL || (!vim_iswhite(*name_end)
3463 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003465 if (name_end != NULL)
3466 {
3467 emsg_severe = TRUE;
3468 EMSG(_(e_trailing));
3469 }
3470 if (!(eap->skip || error))
3471 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 break;
3473 }
3474
3475 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003476 {
3477 if (eap->cmdidx == CMD_unlet)
3478 {
3479 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3480 error = TRUE;
3481 }
3482 else
3483 {
3484 if (do_lock_var(&lv, name_end, deep,
3485 eap->cmdidx == CMD_lockvar) == FAIL)
3486 error = TRUE;
3487 }
3488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003490 if (!eap->skip)
3491 clear_lval(&lv);
3492
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 arg = skipwhite(name_end);
3494 } while (!ends_excmd(*arg));
3495
3496 eap->nextcmd = check_nextcmd(arg);
3497}
3498
Bram Moolenaar8c711452005-01-14 21:53:12 +00003499 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003500do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003502 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003503 int forceit;
3504{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003505 int ret = OK;
3506 int cc;
3507
3508 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003510 cc = *name_end;
3511 *name_end = NUL;
3512
3513 /* Normal name or expanded name. */
3514 if (check_changedtick(lp->ll_name))
3515 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003516 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003517 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003518 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003520 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3521 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003522 else if (lp->ll_range)
3523 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003524 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003525
3526 /* Delete a range of List items. */
3527 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3528 {
3529 li = lp->ll_li->li_next;
3530 listitem_remove(lp->ll_list, lp->ll_li);
3531 lp->ll_li = li;
3532 ++lp->ll_n1;
3533 }
3534 }
3535 else
3536 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003537 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538 /* unlet a List item. */
3539 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003541 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003542 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003543 }
3544
3545 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003546}
3547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548/*
3549 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 */
3552 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003553do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003555 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 hashtab_T *ht;
3558 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003559 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003560 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
Bram Moolenaar33570922005-01-25 22:26:29 +00003562 ht = find_var_ht(name, &varname);
3563 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003565 hi = hash_find(ht, varname);
3566 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003567 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003568 di = HI2DI(hi);
3569 if (var_check_fixed(di->di_flags, name)
3570 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571 return FAIL;
3572 delete_var(ht, hi);
3573 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 if (forceit)
3577 return OK;
3578 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 return FAIL;
3580}
3581
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582/*
3583 * Lock or unlock variable indicated by "lp".
3584 * "deep" is the levels to go (-1 for unlimited);
3585 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3586 */
3587 static int
3588do_lock_var(lp, name_end, deep, lock)
3589 lval_T *lp;
3590 char_u *name_end;
3591 int deep;
3592 int lock;
3593{
3594 int ret = OK;
3595 int cc;
3596 dictitem_T *di;
3597
3598 if (deep == 0) /* nothing to do */
3599 return OK;
3600
3601 if (lp->ll_tv == NULL)
3602 {
3603 cc = *name_end;
3604 *name_end = NUL;
3605
3606 /* Normal name or expanded name. */
3607 if (check_changedtick(lp->ll_name))
3608 ret = FAIL;
3609 else
3610 {
3611 di = find_var(lp->ll_name, NULL);
3612 if (di == NULL)
3613 ret = FAIL;
3614 else
3615 {
3616 if (lock)
3617 di->di_flags |= DI_FLAGS_LOCK;
3618 else
3619 di->di_flags &= ~DI_FLAGS_LOCK;
3620 item_lock(&di->di_tv, deep, lock);
3621 }
3622 }
3623 *name_end = cc;
3624 }
3625 else if (lp->ll_range)
3626 {
3627 listitem_T *li = lp->ll_li;
3628
3629 /* (un)lock a range of List items. */
3630 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3631 {
3632 item_lock(&li->li_tv, deep, lock);
3633 li = li->li_next;
3634 ++lp->ll_n1;
3635 }
3636 }
3637 else if (lp->ll_list != NULL)
3638 /* (un)lock a List item. */
3639 item_lock(&lp->ll_li->li_tv, deep, lock);
3640 else
3641 /* un(lock) a Dictionary item. */
3642 item_lock(&lp->ll_di->di_tv, deep, lock);
3643
3644 return ret;
3645}
3646
3647/*
3648 * Lock or unlock an item. "deep" is nr of levels to go.
3649 */
3650 static void
3651item_lock(tv, deep, lock)
3652 typval_T *tv;
3653 int deep;
3654 int lock;
3655{
3656 static int recurse = 0;
3657 list_T *l;
3658 listitem_T *li;
3659 dict_T *d;
3660 hashitem_T *hi;
3661 int todo;
3662
3663 if (recurse >= DICT_MAXNEST)
3664 {
3665 EMSG(_("E743: variable nested too deep for (un)lock"));
3666 return;
3667 }
3668 if (deep == 0)
3669 return;
3670 ++recurse;
3671
3672 /* lock/unlock the item itself */
3673 if (lock)
3674 tv->v_lock |= VAR_LOCKED;
3675 else
3676 tv->v_lock &= ~VAR_LOCKED;
3677
3678 switch (tv->v_type)
3679 {
3680 case VAR_LIST:
3681 if ((l = tv->vval.v_list) != NULL)
3682 {
3683 if (lock)
3684 l->lv_lock |= VAR_LOCKED;
3685 else
3686 l->lv_lock &= ~VAR_LOCKED;
3687 if (deep < 0 || deep > 1)
3688 /* recursive: lock/unlock the items the List contains */
3689 for (li = l->lv_first; li != NULL; li = li->li_next)
3690 item_lock(&li->li_tv, deep - 1, lock);
3691 }
3692 break;
3693 case VAR_DICT:
3694 if ((d = tv->vval.v_dict) != NULL)
3695 {
3696 if (lock)
3697 d->dv_lock |= VAR_LOCKED;
3698 else
3699 d->dv_lock &= ~VAR_LOCKED;
3700 if (deep < 0 || deep > 1)
3701 {
3702 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003703 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003704 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3705 {
3706 if (!HASHITEM_EMPTY(hi))
3707 {
3708 --todo;
3709 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3710 }
3711 }
3712 }
3713 }
3714 }
3715 --recurse;
3716}
3717
Bram Moolenaara40058a2005-07-11 22:42:07 +00003718/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003719 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3720 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003721 */
3722 static int
3723tv_islocked(tv)
3724 typval_T *tv;
3725{
3726 return (tv->v_lock & VAR_LOCKED)
3727 || (tv->v_type == VAR_LIST
3728 && tv->vval.v_list != NULL
3729 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3730 || (tv->v_type == VAR_DICT
3731 && tv->vval.v_dict != NULL
3732 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3733}
3734
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3736/*
3737 * Delete all "menutrans_" variables.
3738 */
3739 void
3740del_menutrans_vars()
3741{
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003743 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
Bram Moolenaar33570922005-01-25 22:26:29 +00003745 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003746 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003747 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003748 {
3749 if (!HASHITEM_EMPTY(hi))
3750 {
3751 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003752 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3753 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003754 }
3755 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757}
3758#endif
3759
3760#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3761
3762/*
3763 * Local string buffer for the next two functions to store a variable name
3764 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3765 * get_user_var_name().
3766 */
3767
3768static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3769
3770static char_u *varnamebuf = NULL;
3771static int varnamebuflen = 0;
3772
3773/*
3774 * Function to concatenate a prefix and a variable name.
3775 */
3776 static char_u *
3777cat_prefix_varname(prefix, name)
3778 int prefix;
3779 char_u *name;
3780{
3781 int len;
3782
3783 len = (int)STRLEN(name) + 3;
3784 if (len > varnamebuflen)
3785 {
3786 vim_free(varnamebuf);
3787 len += 10; /* some additional space */
3788 varnamebuf = alloc(len);
3789 if (varnamebuf == NULL)
3790 {
3791 varnamebuflen = 0;
3792 return NULL;
3793 }
3794 varnamebuflen = len;
3795 }
3796 *varnamebuf = prefix;
3797 varnamebuf[1] = ':';
3798 STRCPY(varnamebuf + 2, name);
3799 return varnamebuf;
3800}
3801
3802/*
3803 * Function given to ExpandGeneric() to obtain the list of user defined
3804 * (global/buffer/window/built-in) variable names.
3805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 char_u *
3807get_user_var_name(xp, idx)
3808 expand_T *xp;
3809 int idx;
3810{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003811 static long_u gdone;
3812 static long_u bdone;
3813 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003814#ifdef FEAT_WINDOWS
3815 static long_u tdone;
3816#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003817 static int vidx;
3818 static hashitem_T *hi;
3819 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820
3821 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003822 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003823 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003824#ifdef FEAT_WINDOWS
3825 tdone = 0;
3826#endif
3827 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003828
3829 /* Global variables */
3830 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003832 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003833 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003834 else
3835 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003836 while (HASHITEM_EMPTY(hi))
3837 ++hi;
3838 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3839 return cat_prefix_varname('g', hi->hi_key);
3840 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003842
3843 /* b: variables */
3844 ht = &curbuf->b_vars.dv_hashtab;
3845 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003847 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003849 else
3850 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 while (HASHITEM_EMPTY(hi))
3852 ++hi;
3853 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 return (char_u *)"b:changedtick";
3859 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003860
3861 /* w: variables */
3862 ht = &curwin->w_vars.dv_hashtab;
3863 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003865 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003867 else
3868 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003869 while (HASHITEM_EMPTY(hi))
3870 ++hi;
3871 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003873
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003874#ifdef FEAT_WINDOWS
3875 /* t: variables */
3876 ht = &curtab->tp_vars.dv_hashtab;
3877 if (tdone < ht->ht_used)
3878 {
3879 if (tdone++ == 0)
3880 hi = ht->ht_array;
3881 else
3882 ++hi;
3883 while (HASHITEM_EMPTY(hi))
3884 ++hi;
3885 return cat_prefix_varname('t', hi->hi_key);
3886 }
3887#endif
3888
Bram Moolenaar33570922005-01-25 22:26:29 +00003889 /* v: variables */
3890 if (vidx < VV_LEN)
3891 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
3893 vim_free(varnamebuf);
3894 varnamebuf = NULL;
3895 varnamebuflen = 0;
3896 return NULL;
3897}
3898
3899#endif /* FEAT_CMDL_COMPL */
3900
3901/*
3902 * types for expressions.
3903 */
3904typedef enum
3905{
3906 TYPE_UNKNOWN = 0
3907 , TYPE_EQUAL /* == */
3908 , TYPE_NEQUAL /* != */
3909 , TYPE_GREATER /* > */
3910 , TYPE_GEQUAL /* >= */
3911 , TYPE_SMALLER /* < */
3912 , TYPE_SEQUAL /* <= */
3913 , TYPE_MATCH /* =~ */
3914 , TYPE_NOMATCH /* !~ */
3915} exptype_T;
3916
3917/*
3918 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003919 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3921 */
3922
3923/*
3924 * Handle zero level expression.
3925 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003927 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 * Return OK or FAIL.
3929 */
3930 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003931eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 char_u **nextcmd;
3935 int evaluate;
3936{
3937 int ret;
3938 char_u *p;
3939
3940 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003941 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (ret == FAIL || !ends_excmd(*p))
3943 {
3944 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003945 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 /*
3947 * Report the invalid expression unless the expression evaluation has
3948 * been cancelled due to an aborting error, an interrupt, or an
3949 * exception.
3950 */
3951 if (!aborting())
3952 EMSG2(_(e_invexpr2), arg);
3953 ret = FAIL;
3954 }
3955 if (nextcmd != NULL)
3956 *nextcmd = check_nextcmd(p);
3957
3958 return ret;
3959}
3960
3961/*
3962 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003963 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 *
3965 * "arg" must point to the first non-white of the expression.
3966 * "arg" is advanced to the next non-white after the recognized expression.
3967 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003968 * Note: "rettv.v_lock" is not set.
3969 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 * Return OK or FAIL.
3971 */
3972 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003973eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 int evaluate;
3977{
3978 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003979 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981 /*
3982 * Get the first variable.
3983 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003984 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return FAIL;
3986
3987 if ((*arg)[0] == '?')
3988 {
3989 result = FALSE;
3990 if (evaluate)
3991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003992 int error = FALSE;
3993
3994 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003997 if (error)
3998 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000
4001 /*
4002 * Get the second variable.
4003 */
4004 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 return FAIL;
4007
4008 /*
4009 * Check for the ":".
4010 */
4011 if ((*arg)[0] != ':')
4012 {
4013 EMSG(_("E109: Missing ':' after '?'"));
4014 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 return FAIL;
4017 }
4018
4019 /*
4020 * Get the third variable.
4021 */
4022 *arg = skipwhite(*arg + 1);
4023 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4024 {
4025 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return FAIL;
4028 }
4029 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
4032
4033 return OK;
4034}
4035
4036/*
4037 * Handle first level expression:
4038 * expr2 || expr2 || expr2 logical OR
4039 *
4040 * "arg" must point to the first non-white of the expression.
4041 * "arg" is advanced to the next non-white after the recognized expression.
4042 *
4043 * Return OK or FAIL.
4044 */
4045 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 int evaluate;
4050{
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 long result;
4053 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004054 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056 /*
4057 * Get the first variable.
4058 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 return FAIL;
4061
4062 /*
4063 * Repeat until there is no following "||".
4064 */
4065 first = TRUE;
4066 result = FALSE;
4067 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4068 {
4069 if (evaluate && first)
4070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004071 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004074 if (error)
4075 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 first = FALSE;
4077 }
4078
4079 /*
4080 * Get the second variable.
4081 */
4082 *arg = skipwhite(*arg + 2);
4083 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4084 return FAIL;
4085
4086 /*
4087 * Compute the result.
4088 */
4089 if (evaluate && !result)
4090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004091 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 if (error)
4095 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 if (evaluate)
4098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 rettv->v_type = VAR_NUMBER;
4100 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102 }
4103
4104 return OK;
4105}
4106
4107/*
4108 * Handle second level expression:
4109 * expr3 && expr3 && expr3 logical AND
4110 *
4111 * "arg" must point to the first non-white of the expression.
4112 * "arg" is advanced to the next non-white after the recognized expression.
4113 *
4114 * Return OK or FAIL.
4115 */
4116 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 int evaluate;
4121{
Bram Moolenaar33570922005-01-25 22:26:29 +00004122 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 long result;
4124 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004125 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127 /*
4128 * Get the first variable.
4129 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 return FAIL;
4132
4133 /*
4134 * Repeat until there is no following "&&".
4135 */
4136 first = TRUE;
4137 result = TRUE;
4138 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4139 {
4140 if (evaluate && first)
4141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004142 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004145 if (error)
4146 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 first = FALSE;
4148 }
4149
4150 /*
4151 * Get the second variable.
4152 */
4153 *arg = skipwhite(*arg + 2);
4154 if (eval4(arg, &var2, evaluate && result) == FAIL)
4155 return FAIL;
4156
4157 /*
4158 * Compute the result.
4159 */
4160 if (evaluate && result)
4161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 if (error)
4166 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 if (evaluate)
4169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 rettv->v_type = VAR_NUMBER;
4171 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 }
4174
4175 return OK;
4176}
4177
4178/*
4179 * Handle third level expression:
4180 * var1 == var2
4181 * var1 =~ var2
4182 * var1 != var2
4183 * var1 !~ var2
4184 * var1 > var2
4185 * var1 >= var2
4186 * var1 < var2
4187 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004188 * var1 is var2
4189 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 *
4191 * "arg" must point to the first non-white of the expression.
4192 * "arg" is advanced to the next non-white after the recognized expression.
4193 *
4194 * Return OK or FAIL.
4195 */
4196 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 int evaluate;
4201{
Bram Moolenaar33570922005-01-25 22:26:29 +00004202 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 char_u *p;
4204 int i;
4205 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004206 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 int len = 2;
4208 long n1, n2;
4209 char_u *s1, *s2;
4210 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4211 regmatch_T regmatch;
4212 int ic;
4213 char_u *save_cpo;
4214
4215 /*
4216 * Get the first variable.
4217 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return FAIL;
4220
4221 p = *arg;
4222 switch (p[0])
4223 {
4224 case '=': if (p[1] == '=')
4225 type = TYPE_EQUAL;
4226 else if (p[1] == '~')
4227 type = TYPE_MATCH;
4228 break;
4229 case '!': if (p[1] == '=')
4230 type = TYPE_NEQUAL;
4231 else if (p[1] == '~')
4232 type = TYPE_NOMATCH;
4233 break;
4234 case '>': if (p[1] != '=')
4235 {
4236 type = TYPE_GREATER;
4237 len = 1;
4238 }
4239 else
4240 type = TYPE_GEQUAL;
4241 break;
4242 case '<': if (p[1] != '=')
4243 {
4244 type = TYPE_SMALLER;
4245 len = 1;
4246 }
4247 else
4248 type = TYPE_SEQUAL;
4249 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004250 case 'i': if (p[1] == 's')
4251 {
4252 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4253 len = 5;
4254 if (!vim_isIDc(p[len]))
4255 {
4256 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4257 type_is = TRUE;
4258 }
4259 }
4260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262
4263 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004264 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 */
4266 if (type != TYPE_UNKNOWN)
4267 {
4268 /* extra question mark appended: ignore case */
4269 if (p[len] == '?')
4270 {
4271 ic = TRUE;
4272 ++len;
4273 }
4274 /* extra '#' appended: match case */
4275 else if (p[len] == '#')
4276 {
4277 ic = FALSE;
4278 ++len;
4279 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004280 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 else
4282 ic = p_ic;
4283
4284 /*
4285 * Get the second variable.
4286 */
4287 *arg = skipwhite(p + len);
4288 if (eval5(arg, &var2, evaluate) == FAIL)
4289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 return FAIL;
4292 }
4293
4294 if (evaluate)
4295 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004296 if (type_is && rettv->v_type != var2.v_type)
4297 {
4298 /* For "is" a different type always means FALSE, for "notis"
4299 * it means TRUE. */
4300 n1 = (type == TYPE_NEQUAL);
4301 }
4302 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4303 {
4304 if (type_is)
4305 {
4306 n1 = (rettv->v_type == var2.v_type
4307 && rettv->vval.v_list == var2.vval.v_list);
4308 if (type == TYPE_NEQUAL)
4309 n1 = !n1;
4310 }
4311 else if (rettv->v_type != var2.v_type
4312 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4313 {
4314 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004315 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004316 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004317 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004318 clear_tv(rettv);
4319 clear_tv(&var2);
4320 return FAIL;
4321 }
4322 else
4323 {
4324 /* Compare two Lists for being equal or unequal. */
4325 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4326 if (type == TYPE_NEQUAL)
4327 n1 = !n1;
4328 }
4329 }
4330
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004331 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4332 {
4333 if (type_is)
4334 {
4335 n1 = (rettv->v_type == var2.v_type
4336 && rettv->vval.v_dict == var2.vval.v_dict);
4337 if (type == TYPE_NEQUAL)
4338 n1 = !n1;
4339 }
4340 else if (rettv->v_type != var2.v_type
4341 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4342 {
4343 if (rettv->v_type != var2.v_type)
4344 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4345 else
4346 EMSG(_("E736: Invalid operation for Dictionary"));
4347 clear_tv(rettv);
4348 clear_tv(&var2);
4349 return FAIL;
4350 }
4351 else
4352 {
4353 /* Compare two Dictionaries for being equal or unequal. */
4354 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4355 if (type == TYPE_NEQUAL)
4356 n1 = !n1;
4357 }
4358 }
4359
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004360 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4361 {
4362 if (rettv->v_type != var2.v_type
4363 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4364 {
4365 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004366 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004368 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004369 clear_tv(rettv);
4370 clear_tv(&var2);
4371 return FAIL;
4372 }
4373 else
4374 {
4375 /* Compare two Funcrefs for being equal or unequal. */
4376 if (rettv->vval.v_string == NULL
4377 || var2.vval.v_string == NULL)
4378 n1 = FALSE;
4379 else
4380 n1 = STRCMP(rettv->vval.v_string,
4381 var2.vval.v_string) == 0;
4382 if (type == TYPE_NEQUAL)
4383 n1 = !n1;
4384 }
4385 }
4386
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004387#ifdef FEAT_FLOAT
4388 /*
4389 * If one of the two variables is a float, compare as a float.
4390 * When using "=~" or "!~", always compare as string.
4391 */
4392 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4393 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4394 {
4395 float_T f1, f2;
4396
4397 if (rettv->v_type == VAR_FLOAT)
4398 f1 = rettv->vval.v_float;
4399 else
4400 f1 = get_tv_number(rettv);
4401 if (var2.v_type == VAR_FLOAT)
4402 f2 = var2.vval.v_float;
4403 else
4404 f2 = get_tv_number(&var2);
4405 n1 = FALSE;
4406 switch (type)
4407 {
4408 case TYPE_EQUAL: n1 = (f1 == f2); break;
4409 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4410 case TYPE_GREATER: n1 = (f1 > f2); break;
4411 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4412 case TYPE_SMALLER: n1 = (f1 < f2); break;
4413 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4414 case TYPE_UNKNOWN:
4415 case TYPE_MATCH:
4416 case TYPE_NOMATCH: break; /* avoid gcc warning */
4417 }
4418 }
4419#endif
4420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 /*
4422 * If one of the two variables is a number, compare as a number.
4423 * When using "=~" or "!~", always compare as string.
4424 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004425 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004428 n1 = get_tv_number(rettv);
4429 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 switch (type)
4431 {
4432 case TYPE_EQUAL: n1 = (n1 == n2); break;
4433 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4434 case TYPE_GREATER: n1 = (n1 > n2); break;
4435 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4436 case TYPE_SMALLER: n1 = (n1 < n2); break;
4437 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4438 case TYPE_UNKNOWN:
4439 case TYPE_MATCH:
4440 case TYPE_NOMATCH: break; /* avoid gcc warning */
4441 }
4442 }
4443 else
4444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004445 s1 = get_tv_string_buf(rettv, buf1);
4446 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4448 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4449 else
4450 i = 0;
4451 n1 = FALSE;
4452 switch (type)
4453 {
4454 case TYPE_EQUAL: n1 = (i == 0); break;
4455 case TYPE_NEQUAL: n1 = (i != 0); break;
4456 case TYPE_GREATER: n1 = (i > 0); break;
4457 case TYPE_GEQUAL: n1 = (i >= 0); break;
4458 case TYPE_SMALLER: n1 = (i < 0); break;
4459 case TYPE_SEQUAL: n1 = (i <= 0); break;
4460
4461 case TYPE_MATCH:
4462 case TYPE_NOMATCH:
4463 /* avoid 'l' flag in 'cpoptions' */
4464 save_cpo = p_cpo;
4465 p_cpo = (char_u *)"";
4466 regmatch.regprog = vim_regcomp(s2,
4467 RE_MAGIC + RE_STRING);
4468 regmatch.rm_ic = ic;
4469 if (regmatch.regprog != NULL)
4470 {
4471 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4472 vim_free(regmatch.regprog);
4473 if (type == TYPE_NOMATCH)
4474 n1 = !n1;
4475 }
4476 p_cpo = save_cpo;
4477 break;
4478
4479 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4480 }
4481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 clear_tv(rettv);
4483 clear_tv(&var2);
4484 rettv->v_type = VAR_NUMBER;
4485 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 }
4488
4489 return OK;
4490}
4491
4492/*
4493 * Handle fourth level expression:
4494 * + number addition
4495 * - number subtraction
4496 * . string concatenation
4497 *
4498 * "arg" must point to the first non-white of the expression.
4499 * "arg" is advanced to the next non-white after the recognized expression.
4500 *
4501 * Return OK or FAIL.
4502 */
4503 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004504eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 int evaluate;
4508{
Bram Moolenaar33570922005-01-25 22:26:29 +00004509 typval_T var2;
4510 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 int op;
4512 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004513#ifdef FEAT_FLOAT
4514 float_T f1 = 0, f2 = 0;
4515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 char_u *s1, *s2;
4517 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4518 char_u *p;
4519
4520 /*
4521 * Get the first variable.
4522 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004523 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 return FAIL;
4525
4526 /*
4527 * Repeat computing, until no '+', '-' or '.' is following.
4528 */
4529 for (;;)
4530 {
4531 op = **arg;
4532 if (op != '+' && op != '-' && op != '.')
4533 break;
4534
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004535 if ((op != '+' || rettv->v_type != VAR_LIST)
4536#ifdef FEAT_FLOAT
4537 && (op == '.' || rettv->v_type != VAR_FLOAT)
4538#endif
4539 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004540 {
4541 /* For "list + ...", an illegal use of the first operand as
4542 * a number cannot be determined before evaluating the 2nd
4543 * operand: if this is also a list, all is ok.
4544 * For "something . ...", "something - ..." or "non-list + ...",
4545 * we know that the first operand needs to be a string or number
4546 * without evaluating the 2nd operand. So check before to avoid
4547 * side effects after an error. */
4548 if (evaluate && get_tv_string_chk(rettv) == NULL)
4549 {
4550 clear_tv(rettv);
4551 return FAIL;
4552 }
4553 }
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 /*
4556 * Get the second variable.
4557 */
4558 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004559 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 return FAIL;
4563 }
4564
4565 if (evaluate)
4566 {
4567 /*
4568 * Compute the result.
4569 */
4570 if (op == '.')
4571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004572 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4573 s2 = get_tv_string_buf_chk(&var2, buf2);
4574 if (s2 == NULL) /* type error ? */
4575 {
4576 clear_tv(rettv);
4577 clear_tv(&var2);
4578 return FAIL;
4579 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004580 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 clear_tv(rettv);
4582 rettv->v_type = VAR_STRING;
4583 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004585 else if (op == '+' && rettv->v_type == VAR_LIST
4586 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004587 {
4588 /* concatenate Lists */
4589 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4590 &var3) == FAIL)
4591 {
4592 clear_tv(rettv);
4593 clear_tv(&var2);
4594 return FAIL;
4595 }
4596 clear_tv(rettv);
4597 *rettv = var3;
4598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 else
4600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004601 int error = FALSE;
4602
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004603#ifdef FEAT_FLOAT
4604 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004605 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004606 f1 = rettv->vval.v_float;
4607 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004609 else
4610#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004611 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004612 n1 = get_tv_number_chk(rettv, &error);
4613 if (error)
4614 {
4615 /* This can only happen for "list + non-list". For
4616 * "non-list + ..." or "something - ...", we returned
4617 * before evaluating the 2nd operand. */
4618 clear_tv(rettv);
4619 return FAIL;
4620 }
4621#ifdef FEAT_FLOAT
4622 if (var2.v_type == VAR_FLOAT)
4623 f1 = n1;
4624#endif
4625 }
4626#ifdef FEAT_FLOAT
4627 if (var2.v_type == VAR_FLOAT)
4628 {
4629 f2 = var2.vval.v_float;
4630 n2 = 0;
4631 }
4632 else
4633#endif
4634 {
4635 n2 = get_tv_number_chk(&var2, &error);
4636 if (error)
4637 {
4638 clear_tv(rettv);
4639 clear_tv(&var2);
4640 return FAIL;
4641 }
4642#ifdef FEAT_FLOAT
4643 if (rettv->v_type == VAR_FLOAT)
4644 f2 = n2;
4645#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004646 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004647 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004648
4649#ifdef FEAT_FLOAT
4650 /* If there is a float on either side the result is a float. */
4651 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4652 {
4653 if (op == '+')
4654 f1 = f1 + f2;
4655 else
4656 f1 = f1 - f2;
4657 rettv->v_type = VAR_FLOAT;
4658 rettv->vval.v_float = f1;
4659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004661#endif
4662 {
4663 if (op == '+')
4664 n1 = n1 + n2;
4665 else
4666 n1 = n1 - n2;
4667 rettv->v_type = VAR_NUMBER;
4668 rettv->vval.v_number = n1;
4669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 }
4674 return OK;
4675}
4676
4677/*
4678 * Handle fifth level expression:
4679 * * number multiplication
4680 * / number division
4681 * % number modulo
4682 *
4683 * "arg" must point to the first non-white of the expression.
4684 * "arg" is advanced to the next non-white after the recognized expression.
4685 *
4686 * Return OK or FAIL.
4687 */
4688 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004689eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004693 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
Bram Moolenaar33570922005-01-25 22:26:29 +00004695 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 int op;
4697 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004698#ifdef FEAT_FLOAT
4699 int use_float = FALSE;
4700 float_T f1 = 0, f2;
4701#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703
4704 /*
4705 * Get the first variable.
4706 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004707 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return FAIL;
4709
4710 /*
4711 * Repeat computing, until no '*', '/' or '%' is following.
4712 */
4713 for (;;)
4714 {
4715 op = **arg;
4716 if (op != '*' && op != '/' && op != '%')
4717 break;
4718
4719 if (evaluate)
4720 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721#ifdef FEAT_FLOAT
4722 if (rettv->v_type == VAR_FLOAT)
4723 {
4724 f1 = rettv->vval.v_float;
4725 use_float = TRUE;
4726 n1 = 0;
4727 }
4728 else
4729#endif
4730 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 if (error)
4733 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 else
4736 n1 = 0;
4737
4738 /*
4739 * Get the second variable.
4740 */
4741 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return FAIL;
4744
4745 if (evaluate)
4746 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004747#ifdef FEAT_FLOAT
4748 if (var2.v_type == VAR_FLOAT)
4749 {
4750 if (!use_float)
4751 {
4752 f1 = n1;
4753 use_float = TRUE;
4754 }
4755 f2 = var2.vval.v_float;
4756 n2 = 0;
4757 }
4758 else
4759#endif
4760 {
4761 n2 = get_tv_number_chk(&var2, &error);
4762 clear_tv(&var2);
4763 if (error)
4764 return FAIL;
4765#ifdef FEAT_FLOAT
4766 if (use_float)
4767 f2 = n2;
4768#endif
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
4771 /*
4772 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004773 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004775#ifdef FEAT_FLOAT
4776 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004778 if (op == '*')
4779 f1 = f1 * f2;
4780 else if (op == '/')
4781 {
4782 /* We rely on the floating point library to handle divide
4783 * by zero to result in "inf" and not a crash. */
4784 f1 = f1 / f2;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004788 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 return FAIL;
4790 }
4791 rettv->v_type = VAR_FLOAT;
4792 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
4794 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797 if (op == '*')
4798 n1 = n1 * n2;
4799 else if (op == '/')
4800 {
4801 if (n2 == 0) /* give an error message? */
4802 {
4803 if (n1 == 0)
4804 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4805 else if (n1 < 0)
4806 n1 = -0x7fffffffL;
4807 else
4808 n1 = 0x7fffffffL;
4809 }
4810 else
4811 n1 = n1 / n2;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 {
4815 if (n2 == 0) /* give an error message? */
4816 n1 = 0;
4817 else
4818 n1 = n1 % n2;
4819 }
4820 rettv->v_type = VAR_NUMBER;
4821 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
4824 }
4825
4826 return OK;
4827}
4828
4829/*
4830 * Handle sixth level expression:
4831 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004832 * "string" string constant
4833 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 * &option-name option value
4835 * @r register contents
4836 * identifier variable value
4837 * function() function call
4838 * $VAR environment variable
4839 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004840 * [expr, expr] List
4841 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 *
4843 * Also handle:
4844 * ! in front logical NOT
4845 * - in front unary minus
4846 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 * trailing [] subscript in String or List
4848 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 *
4850 * "arg" must point to the first non-white of the expression.
4851 * "arg" is advanced to the next non-white after the recognized expression.
4852 *
4853 * Return OK or FAIL.
4854 */
4855 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004856eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004860 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 long n;
4863 int len;
4864 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 char_u *start_leader, *end_leader;
4866 int ret = OK;
4867 char_u *alias;
4868
4869 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 /*
4876 * Skip '!' and '-' characters. They are handled later.
4877 */
4878 start_leader = *arg;
4879 while (**arg == '!' || **arg == '-' || **arg == '+')
4880 *arg = skipwhite(*arg + 1);
4881 end_leader = *arg;
4882
4883 switch (**arg)
4884 {
4885 /*
4886 * Number constant.
4887 */
4888 case '0':
4889 case '1':
4890 case '2':
4891 case '3':
4892 case '4':
4893 case '5':
4894 case '6':
4895 case '7':
4896 case '8':
4897 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 {
4899#ifdef FEAT_FLOAT
4900 char_u *p = skipdigits(*arg + 1);
4901 int get_float = FALSE;
4902
4903 /* We accept a float when the format matches
4904 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004905 * strict to avoid backwards compatibility problems.
4906 * Don't look for a float after the "." operator, so that
4907 * ":let vers = 1.2.3" doesn't fail. */
4908 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 get_float = TRUE;
4911 p = skipdigits(p + 2);
4912 if (*p == 'e' || *p == 'E')
4913 {
4914 ++p;
4915 if (*p == '-' || *p == '+')
4916 ++p;
4917 if (!vim_isdigit(*p))
4918 get_float = FALSE;
4919 else
4920 p = skipdigits(p + 1);
4921 }
4922 if (ASCII_ISALPHA(*p) || *p == '.')
4923 get_float = FALSE;
4924 }
4925 if (get_float)
4926 {
4927 float_T f;
4928
4929 *arg += string2float(*arg, &f);
4930 if (evaluate)
4931 {
4932 rettv->v_type = VAR_FLOAT;
4933 rettv->vval.v_float = f;
4934 }
4935 }
4936 else
4937#endif
4938 {
4939 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4940 *arg += len;
4941 if (evaluate)
4942 {
4943 rettv->v_type = VAR_NUMBER;
4944 rettv->vval.v_number = n;
4945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 /*
4951 * String constant: "string".
4952 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004953 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 break;
4955
4956 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004959 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 break;
4961
4962 /*
4963 * List: [expr, expr]
4964 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 break;
4967
4968 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 * Dictionary: {key: val, key: val}
4970 */
4971 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4972 break;
4973
4974 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004975 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004977 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 break;
4979
4980 /*
4981 * Environment variable: $VAR.
4982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 break;
4985
4986 /*
4987 * Register contents: @r.
4988 */
4989 case '@': ++*arg;
4990 if (evaluate)
4991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004992 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004993 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995 if (**arg != NUL)
4996 ++*arg;
4997 break;
4998
4999 /*
5000 * nested expression: (expression).
5001 */
5002 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005003 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 if (**arg == ')')
5005 ++*arg;
5006 else if (ret == OK)
5007 {
5008 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005009 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 ret = FAIL;
5011 }
5012 break;
5013
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 break;
5016 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017
5018 if (ret == NOTDONE)
5019 {
5020 /*
5021 * Must be a variable or function name.
5022 * Can also be a curly-braces kind of name: {expr}.
5023 */
5024 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005025 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 if (alias != NULL)
5027 s = alias;
5028
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005029 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 ret = FAIL;
5031 else
5032 {
5033 if (**arg == '(') /* recursive! */
5034 {
5035 /* If "s" is the name of a variable of type VAR_FUNC
5036 * use its contents. */
5037 s = deref_func_name(s, &len);
5038
5039 /* Invoke the function. */
5040 ret = get_func_tv(s, len, rettv, arg,
5041 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005042 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 /* Stop the expression evaluation when immediately
5044 * aborting on error, or when an interrupt occurred or
5045 * an exception was thrown but not caught. */
5046 if (aborting())
5047 {
5048 if (ret == OK)
5049 clear_tv(rettv);
5050 ret = FAIL;
5051 }
5052 }
5053 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005054 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005055 else
5056 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 }
5058
5059 if (alias != NULL)
5060 vim_free(alias);
5061 }
5062
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *arg = skipwhite(*arg);
5064
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005065 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5066 * expr(expr). */
5067 if (ret == OK)
5068 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
5071 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5072 */
5073 if (ret == OK && evaluate && end_leader > start_leader)
5074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005075 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005076 int val = 0;
5077#ifdef FEAT_FLOAT
5078 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005079
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005080 if (rettv->v_type == VAR_FLOAT)
5081 f = rettv->vval.v_float;
5082 else
5083#endif
5084 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005085 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005087 clear_tv(rettv);
5088 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005090 else
5091 {
5092 while (end_leader > start_leader)
5093 {
5094 --end_leader;
5095 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 {
5097#ifdef FEAT_FLOAT
5098 if (rettv->v_type == VAR_FLOAT)
5099 f = !f;
5100 else
5101#endif
5102 val = !val;
5103 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005104 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 {
5106#ifdef FEAT_FLOAT
5107 if (rettv->v_type == VAR_FLOAT)
5108 f = -f;
5109 else
5110#endif
5111 val = -val;
5112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005113 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005114#ifdef FEAT_FLOAT
5115 if (rettv->v_type == VAR_FLOAT)
5116 {
5117 clear_tv(rettv);
5118 rettv->vval.v_float = f;
5119 }
5120 else
5121#endif
5122 {
5123 clear_tv(rettv);
5124 rettv->v_type = VAR_NUMBER;
5125 rettv->vval.v_number = val;
5126 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129
5130 return ret;
5131}
5132
5133/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005134 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5135 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005136 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5137 */
5138 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005139eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005141 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005143 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144{
5145 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005146 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 long len = -1;
5149 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005153 if (rettv->v_type == VAR_FUNC
5154#ifdef FEAT_FLOAT
5155 || rettv->v_type == VAR_FLOAT
5156#endif
5157 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005159 if (verbose)
5160 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161 return FAIL;
5162 }
5163
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 /*
5167 * dict.name
5168 */
5169 key = *arg + 1;
5170 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5171 ;
5172 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 }
5176 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 /*
5179 * something[idx]
5180 *
5181 * Get the (first) variable from inside the [].
5182 */
5183 *arg = skipwhite(*arg + 1);
5184 if (**arg == ':')
5185 empty1 = TRUE;
5186 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5187 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005188 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5189 {
5190 /* not a number or string */
5191 clear_tv(&var1);
5192 return FAIL;
5193 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194
5195 /*
5196 * Get the second variable from inside the [:].
5197 */
5198 if (**arg == ':')
5199 {
5200 range = TRUE;
5201 *arg = skipwhite(*arg + 1);
5202 if (**arg == ']')
5203 empty2 = TRUE;
5204 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 if (!empty1)
5207 clear_tv(&var1);
5208 return FAIL;
5209 }
5210 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5211 {
5212 /* not a number or string */
5213 if (!empty1)
5214 clear_tv(&var1);
5215 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 return FAIL;
5217 }
5218 }
5219
5220 /* Check for the ']'. */
5221 if (**arg != ']')
5222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005223 if (verbose)
5224 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 clear_tv(&var1);
5226 if (range)
5227 clear_tv(&var2);
5228 return FAIL;
5229 }
5230 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231 }
5232
5233 if (evaluate)
5234 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 n1 = 0;
5236 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238 n1 = get_tv_number(&var1);
5239 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 }
5241 if (range)
5242 {
5243 if (empty2)
5244 n2 = -1;
5245 else
5246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005247 n2 = get_tv_number(&var2);
5248 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 }
5250 }
5251
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 {
5254 case VAR_NUMBER:
5255 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 len = (long)STRLEN(s);
5258 if (range)
5259 {
5260 /* The resulting variable is a substring. If the indexes
5261 * are out of range the result is empty. */
5262 if (n1 < 0)
5263 {
5264 n1 = len + n1;
5265 if (n1 < 0)
5266 n1 = 0;
5267 }
5268 if (n2 < 0)
5269 n2 = len + n2;
5270 else if (n2 >= len)
5271 n2 = len;
5272 if (n1 >= len || n2 < 0 || n1 > n2)
5273 s = NULL;
5274 else
5275 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5276 }
5277 else
5278 {
5279 /* The resulting variable is a string of a single
5280 * character. If the index is too big or negative the
5281 * result is empty. */
5282 if (n1 >= len || n1 < 0)
5283 s = NULL;
5284 else
5285 s = vim_strnsave(s + n1, 1);
5286 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005287 clear_tv(rettv);
5288 rettv->v_type = VAR_STRING;
5289 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 break;
5291
5292 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005293 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 if (n1 < 0)
5295 n1 = len + n1;
5296 if (!empty1 && (n1 < 0 || n1 >= len))
5297 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005298 /* For a range we allow invalid values and return an empty
5299 * list. A list index out of range is an error. */
5300 if (!range)
5301 {
5302 if (verbose)
5303 EMSGN(_(e_listidx), n1);
5304 return FAIL;
5305 }
5306 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 }
5308 if (range)
5309 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005310 list_T *l;
5311 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312
5313 if (n2 < 0)
5314 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005315 else if (n2 >= len)
5316 n2 = len - 1;
5317 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005318 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 l = list_alloc();
5320 if (l == NULL)
5321 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005322 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323 n1 <= n2; ++n1)
5324 {
5325 if (list_append_tv(l, &item->li_tv) == FAIL)
5326 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005327 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 return FAIL;
5329 }
5330 item = item->li_next;
5331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005332 clear_tv(rettv);
5333 rettv->v_type = VAR_LIST;
5334 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005335 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 }
5337 else
5338 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005339 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005340 clear_tv(rettv);
5341 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 }
5343 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344
5345 case VAR_DICT:
5346 if (range)
5347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 if (verbose)
5349 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 if (len == -1)
5351 clear_tv(&var1);
5352 return FAIL;
5353 }
5354 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005355 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356
5357 if (len == -1)
5358 {
5359 key = get_tv_string(&var1);
5360 if (*key == NUL)
5361 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362 if (verbose)
5363 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 clear_tv(&var1);
5365 return FAIL;
5366 }
5367 }
5368
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005369 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005371 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005372 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373 if (len == -1)
5374 clear_tv(&var1);
5375 if (item == NULL)
5376 return FAIL;
5377
5378 copy_tv(&item->di_tv, &var1);
5379 clear_tv(rettv);
5380 *rettv = var1;
5381 }
5382 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 }
5384 }
5385
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 return OK;
5387}
5388
5389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 * Get an option value.
5391 * "arg" points to the '&' or '+' before the option name.
5392 * "arg" is advanced to character after the option name.
5393 * Return OK or FAIL.
5394 */
5395 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005398 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 int evaluate;
5400{
5401 char_u *option_end;
5402 long numval;
5403 char_u *stringval;
5404 int opt_type;
5405 int c;
5406 int working = (**arg == '+'); /* has("+option") */
5407 int ret = OK;
5408 int opt_flags;
5409
5410 /*
5411 * Isolate the option name and find its value.
5412 */
5413 option_end = find_option_end(arg, &opt_flags);
5414 if (option_end == NULL)
5415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005416 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 EMSG2(_("E112: Option name missing: %s"), *arg);
5418 return FAIL;
5419 }
5420
5421 if (!evaluate)
5422 {
5423 *arg = option_end;
5424 return OK;
5425 }
5426
5427 c = *option_end;
5428 *option_end = NUL;
5429 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
5432 if (opt_type == -3) /* invalid name */
5433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 EMSG2(_("E113: Unknown option: %s"), *arg);
5436 ret = FAIL;
5437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
5440 if (opt_type == -2) /* hidden string option */
5441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005442 rettv->v_type = VAR_STRING;
5443 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445 else if (opt_type == -1) /* hidden number option */
5446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 rettv->v_type = VAR_NUMBER;
5448 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
5450 else if (opt_type == 1) /* number option */
5451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 rettv->v_type = VAR_NUMBER;
5453 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 }
5455 else /* string option */
5456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 rettv->v_type = VAR_STRING;
5458 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460 }
5461 else if (working && (opt_type == -2 || opt_type == -1))
5462 ret = FAIL;
5463
5464 *option_end = c; /* put back for error messages */
5465 *arg = option_end;
5466
5467 return ret;
5468}
5469
5470/*
5471 * Allocate a variable for a string constant.
5472 * Return OK or FAIL.
5473 */
5474 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 int evaluate;
5479{
5480 char_u *p;
5481 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 int extra = 0;
5483
5484 /*
5485 * Find the end of the string, skipping backslashed characters.
5486 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005487 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
5489 if (*p == '\\' && p[1] != NUL)
5490 {
5491 ++p;
5492 /* A "\<x>" form occupies at least 4 characters, and produces up
5493 * to 6 characters: reserve space for 2 extra */
5494 if (*p == '<')
5495 extra += 2;
5496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
5498
5499 if (*p != '"')
5500 {
5501 EMSG2(_("E114: Missing quote: %s"), *arg);
5502 return FAIL;
5503 }
5504
5505 /* If only parsing, set *arg and return here */
5506 if (!evaluate)
5507 {
5508 *arg = p + 1;
5509 return OK;
5510 }
5511
5512 /*
5513 * Copy the string into allocated memory, handling backslashed
5514 * characters.
5515 */
5516 name = alloc((unsigned)(p - *arg + extra));
5517 if (name == NULL)
5518 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005519 rettv->v_type = VAR_STRING;
5520 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
5524 if (*p == '\\')
5525 {
5526 switch (*++p)
5527 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005528 case 'b': *name++ = BS; ++p; break;
5529 case 'e': *name++ = ESC; ++p; break;
5530 case 'f': *name++ = FF; ++p; break;
5531 case 'n': *name++ = NL; ++p; break;
5532 case 'r': *name++ = CAR; ++p; break;
5533 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
5535 case 'X': /* hex: "\x1", "\x12" */
5536 case 'x':
5537 case 'u': /* Unicode: "\u0023" */
5538 case 'U':
5539 if (vim_isxdigit(p[1]))
5540 {
5541 int n, nr;
5542 int c = toupper(*p);
5543
5544 if (c == 'X')
5545 n = 2;
5546 else
5547 n = 4;
5548 nr = 0;
5549 while (--n >= 0 && vim_isxdigit(p[1]))
5550 {
5551 ++p;
5552 nr = (nr << 4) + hex2nr(*p);
5553 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005554 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555#ifdef FEAT_MBYTE
5556 /* For "\u" store the number according to
5557 * 'encoding'. */
5558 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 else
5561#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 break;
5565
5566 /* octal: "\1", "\12", "\123" */
5567 case '0':
5568 case '1':
5569 case '2':
5570 case '3':
5571 case '4':
5572 case '5':
5573 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 case '7': *name = *p++ - '0';
5575 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 *name = (*name << 3) + *p++ - '0';
5578 if (*p >= '0' && *p <= '7')
5579 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 break;
5583
5584 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 if (extra != 0)
5587 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 break;
5590 }
5591 /* FALLTHROUGH */
5592
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 break;
5595 }
5596 }
5597 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 *arg = p + 1;
5603
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 return OK;
5605}
5606
5607/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 * Return OK or FAIL.
5610 */
5611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 int evaluate;
5616{
5617 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005618 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005619 int reduce = 0;
5620
5621 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005623 */
5624 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005628 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005629 break;
5630 ++reduce;
5631 ++p;
5632 }
5633 }
5634
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005636 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005638 return FAIL;
5639 }
5640
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005642 if (!evaluate)
5643 {
5644 *arg = p + 1;
5645 return OK;
5646 }
5647
5648 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005650 */
5651 str = alloc((unsigned)((p - *arg) - reduce));
5652 if (str == NULL)
5653 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 rettv->v_type = VAR_STRING;
5655 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005656
Bram Moolenaar8c711452005-01-14 21:53:12 +00005657 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005658 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005661 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005662 break;
5663 ++p;
5664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005666 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005668 *arg = p + 1;
5669
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005670 return OK;
5671}
5672
5673/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 * Allocate a variable for a List and fill it from "*arg".
5675 * Return OK or FAIL.
5676 */
5677 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005680 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681 int evaluate;
5682{
Bram Moolenaar33570922005-01-25 22:26:29 +00005683 list_T *l = NULL;
5684 typval_T tv;
5685 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686
5687 if (evaluate)
5688 {
5689 l = list_alloc();
5690 if (l == NULL)
5691 return FAIL;
5692 }
5693
5694 *arg = skipwhite(*arg + 1);
5695 while (**arg != ']' && **arg != NUL)
5696 {
5697 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5698 goto failret;
5699 if (evaluate)
5700 {
5701 item = listitem_alloc();
5702 if (item != NULL)
5703 {
5704 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005705 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706 list_append(l, item);
5707 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005708 else
5709 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005710 }
5711
5712 if (**arg == ']')
5713 break;
5714 if (**arg != ',')
5715 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 goto failret;
5718 }
5719 *arg = skipwhite(*arg + 1);
5720 }
5721
5722 if (**arg != ']')
5723 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725failret:
5726 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005727 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 return FAIL;
5729 }
5730
5731 *arg = skipwhite(*arg + 1);
5732 if (evaluate)
5733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005734 rettv->v_type = VAR_LIST;
5735 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005736 ++l->lv_refcount;
5737 }
5738
5739 return OK;
5740}
5741
5742/*
5743 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005744 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005746 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747list_alloc()
5748{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005749 list_T *l;
5750
5751 l = (list_T *)alloc_clear(sizeof(list_T));
5752 if (l != NULL)
5753 {
5754 /* Prepend the list to the list of lists for garbage collection. */
5755 if (first_list != NULL)
5756 first_list->lv_used_prev = l;
5757 l->lv_used_prev = NULL;
5758 l->lv_used_next = first_list;
5759 first_list = l;
5760 }
5761 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005762}
5763
5764/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005765 * Allocate an empty list for a return value.
5766 * Returns OK or FAIL.
5767 */
5768 static int
5769rettv_list_alloc(rettv)
5770 typval_T *rettv;
5771{
5772 list_T *l = list_alloc();
5773
5774 if (l == NULL)
5775 return FAIL;
5776
5777 rettv->vval.v_list = l;
5778 rettv->v_type = VAR_LIST;
5779 ++l->lv_refcount;
5780 return OK;
5781}
5782
5783/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784 * Unreference a list: decrement the reference count and free it when it
5785 * becomes zero.
5786 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005787 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005788list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005789 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005791 if (l != NULL && --l->lv_refcount <= 0)
5792 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793}
5794
5795/*
5796 * Free a list, including all items it points to.
5797 * Ignores the reference count.
5798 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005799 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005800list_free(l, recurse)
5801 list_T *l;
5802 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803{
Bram Moolenaar33570922005-01-25 22:26:29 +00005804 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005806 /* Remove the list from the list of lists for garbage collection. */
5807 if (l->lv_used_prev == NULL)
5808 first_list = l->lv_used_next;
5809 else
5810 l->lv_used_prev->lv_used_next = l->lv_used_next;
5811 if (l->lv_used_next != NULL)
5812 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5813
Bram Moolenaard9fba312005-06-26 22:34:35 +00005814 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005815 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005816 /* Remove the item before deleting it. */
5817 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005818 if (recurse || (item->li_tv.v_type != VAR_LIST
5819 && item->li_tv.v_type != VAR_DICT))
5820 clear_tv(&item->li_tv);
5821 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 }
5823 vim_free(l);
5824}
5825
5826/*
5827 * Allocate a list item.
5828 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005829 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830listitem_alloc()
5831{
Bram Moolenaar33570922005-01-25 22:26:29 +00005832 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833}
5834
5835/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005836 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 */
5838 static void
5839listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005840 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005842 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 vim_free(item);
5844}
5845
5846/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005847 * Remove a list item from a List and free it. Also clears the value.
5848 */
5849 static void
5850listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005851 list_T *l;
5852 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005853{
5854 list_remove(l, item, item);
5855 listitem_free(item);
5856}
5857
5858/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 * Get the number of items in a list.
5860 */
5861 static long
5862list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005863 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 if (l == NULL)
5866 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005867 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868}
5869
5870/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005871 * Return TRUE when two lists have exactly the same values.
5872 */
5873 static int
5874list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 list_T *l1;
5876 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005877 int ic; /* ignore case for strings */
5878{
Bram Moolenaar33570922005-01-25 22:26:29 +00005879 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005880
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005881 if (l1 == NULL || l2 == NULL)
5882 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005883 if (l1 == l2)
5884 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005885 if (list_len(l1) != list_len(l2))
5886 return FALSE;
5887
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005888 for (item1 = l1->lv_first, item2 = l2->lv_first;
5889 item1 != NULL && item2 != NULL;
5890 item1 = item1->li_next, item2 = item2->li_next)
5891 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5892 return FALSE;
5893 return item1 == NULL && item2 == NULL;
5894}
5895
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01005896#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5897 || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005898/*
5899 * Return the dictitem that an entry in a hashtable points to.
5900 */
5901 dictitem_T *
5902dict_lookup(hi)
5903 hashitem_T *hi;
5904{
5905 return HI2DI(hi);
5906}
5907#endif
5908
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005909/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005910 * Return TRUE when two dictionaries have exactly the same key/values.
5911 */
5912 static int
5913dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 dict_T *d1;
5915 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005916 int ic; /* ignore case for strings */
5917{
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 hashitem_T *hi;
5919 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005920 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005921
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005922 if (d1 == NULL || d2 == NULL)
5923 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005924 if (d1 == d2)
5925 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005926 if (dict_len(d1) != dict_len(d2))
5927 return FALSE;
5928
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005929 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005931 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005932 if (!HASHITEM_EMPTY(hi))
5933 {
5934 item2 = dict_find(d2, hi->hi_key, -1);
5935 if (item2 == NULL)
5936 return FALSE;
5937 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5938 return FALSE;
5939 --todo;
5940 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005941 }
5942 return TRUE;
5943}
5944
5945/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005946 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005947 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005948 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005949 */
5950 static int
5951tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 typval_T *tv1;
5953 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005954 int ic; /* ignore case */
5955{
5956 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005957 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005958 static int recursive = 0; /* cach recursive loops */
5959 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005960
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005961 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005962 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005963 /* Catch lists and dicts that have an endless loop by limiting
5964 * recursiveness to 1000. We guess they are equal then. */
5965 if (recursive >= 1000)
5966 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005967
5968 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005969 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005970 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005971 ++recursive;
5972 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5973 --recursive;
5974 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005975
5976 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005977 ++recursive;
5978 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5979 --recursive;
5980 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005981
5982 case VAR_FUNC:
5983 return (tv1->vval.v_string != NULL
5984 && tv2->vval.v_string != NULL
5985 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5986
5987 case VAR_NUMBER:
5988 return tv1->vval.v_number == tv2->vval.v_number;
5989
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005990#ifdef FEAT_FLOAT
5991 case VAR_FLOAT:
5992 return tv1->vval.v_float == tv2->vval.v_float;
5993#endif
5994
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005995 case VAR_STRING:
5996 s1 = get_tv_string_buf(tv1, buf1);
5997 s2 = get_tv_string_buf(tv2, buf2);
5998 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006000
6001 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002 return TRUE;
6003}
6004
6005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Locate item with index "n" in list "l" and return it.
6007 * A negative index is counted from the end; -1 is the last item.
6008 * Returns NULL when "n" is out of range.
6009 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 long n;
6014{
Bram Moolenaar33570922005-01-25 22:26:29 +00006015 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 long idx;
6017
6018 if (l == NULL)
6019 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006020
6021 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006023 n = l->lv_len + n;
6024
6025 /* Check for index out of range. */
6026 if (n < 0 || n >= l->lv_len)
6027 return NULL;
6028
6029 /* When there is a cached index may start search from there. */
6030 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006032 if (n < l->lv_idx / 2)
6033 {
6034 /* closest to the start of the list */
6035 item = l->lv_first;
6036 idx = 0;
6037 }
6038 else if (n > (l->lv_idx + l->lv_len) / 2)
6039 {
6040 /* closest to the end of the list */
6041 item = l->lv_last;
6042 idx = l->lv_len - 1;
6043 }
6044 else
6045 {
6046 /* closest to the cached index */
6047 item = l->lv_idx_item;
6048 idx = l->lv_idx;
6049 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 }
6051 else
6052 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006053 if (n < l->lv_len / 2)
6054 {
6055 /* closest to the start of the list */
6056 item = l->lv_first;
6057 idx = 0;
6058 }
6059 else
6060 {
6061 /* closest to the end of the list */
6062 item = l->lv_last;
6063 idx = l->lv_len - 1;
6064 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006066
6067 while (n > idx)
6068 {
6069 /* search forward */
6070 item = item->li_next;
6071 ++idx;
6072 }
6073 while (n < idx)
6074 {
6075 /* search backward */
6076 item = item->li_prev;
6077 --idx;
6078 }
6079
6080 /* cache the used index */
6081 l->lv_idx = idx;
6082 l->lv_idx_item = item;
6083
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 return item;
6085}
6086
6087/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006088 * Get list item "l[idx]" as a number.
6089 */
6090 static long
6091list_find_nr(l, idx, errorp)
6092 list_T *l;
6093 long idx;
6094 int *errorp; /* set to TRUE when something wrong */
6095{
6096 listitem_T *li;
6097
6098 li = list_find(l, idx);
6099 if (li == NULL)
6100 {
6101 if (errorp != NULL)
6102 *errorp = TRUE;
6103 return -1L;
6104 }
6105 return get_tv_number_chk(&li->li_tv, errorp);
6106}
6107
6108/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006109 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6110 */
6111 char_u *
6112list_find_str(l, idx)
6113 list_T *l;
6114 long idx;
6115{
6116 listitem_T *li;
6117
6118 li = list_find(l, idx - 1);
6119 if (li == NULL)
6120 {
6121 EMSGN(_(e_listidx), idx);
6122 return NULL;
6123 }
6124 return get_tv_string(&li->li_tv);
6125}
6126
6127/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006128 * Locate "item" list "l" and return its index.
6129 * Returns -1 when "item" is not in the list.
6130 */
6131 static long
6132list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 list_T *l;
6134 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006135{
6136 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006138
6139 if (l == NULL)
6140 return -1;
6141 idx = 0;
6142 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6143 ++idx;
6144 if (li == NULL)
6145 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006146 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006147}
6148
6149/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 * Append item "item" to the end of list "l".
6151 */
6152 static void
6153list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 list_T *l;
6155 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156{
6157 if (l->lv_last == NULL)
6158 {
6159 /* empty list */
6160 l->lv_first = item;
6161 l->lv_last = item;
6162 item->li_prev = NULL;
6163 }
6164 else
6165 {
6166 l->lv_last->li_next = item;
6167 item->li_prev = l->lv_last;
6168 l->lv_last = item;
6169 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006170 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171 item->li_next = NULL;
6172}
6173
6174/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006178 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006180 list_T *l;
6181 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006183 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184
Bram Moolenaar05159a02005-02-26 23:04:13 +00006185 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006186 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006187 copy_tv(tv, &li->li_tv);
6188 list_append(l, li);
6189 return OK;
6190}
6191
6192/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006193 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006194 * Return FAIL when out of memory.
6195 */
6196 int
6197list_append_dict(list, dict)
6198 list_T *list;
6199 dict_T *dict;
6200{
6201 listitem_T *li = listitem_alloc();
6202
6203 if (li == NULL)
6204 return FAIL;
6205 li->li_tv.v_type = VAR_DICT;
6206 li->li_tv.v_lock = 0;
6207 li->li_tv.vval.v_dict = dict;
6208 list_append(list, li);
6209 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006210 return OK;
6211}
6212
6213/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006214 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006215 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006216 * Returns FAIL when out of memory.
6217 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006218 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006219list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006220 list_T *l;
6221 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006222 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006223{
6224 listitem_T *li = listitem_alloc();
6225
6226 if (li == NULL)
6227 return FAIL;
6228 list_append(l, li);
6229 li->li_tv.v_type = VAR_STRING;
6230 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006231 if (str == NULL)
6232 li->li_tv.vval.v_string = NULL;
6233 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006234 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006235 return FAIL;
6236 return OK;
6237}
6238
6239/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006240 * Append "n" to list "l".
6241 * Returns FAIL when out of memory.
6242 */
6243 static int
6244list_append_number(l, n)
6245 list_T *l;
6246 varnumber_T n;
6247{
6248 listitem_T *li;
6249
6250 li = listitem_alloc();
6251 if (li == NULL)
6252 return FAIL;
6253 li->li_tv.v_type = VAR_NUMBER;
6254 li->li_tv.v_lock = 0;
6255 li->li_tv.vval.v_number = n;
6256 list_append(l, li);
6257 return OK;
6258}
6259
6260/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262 * If "item" is NULL append at the end.
6263 * Return FAIL when out of memory.
6264 */
6265 static int
6266list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006267 list_T *l;
6268 typval_T *tv;
6269 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006270{
Bram Moolenaar33570922005-01-25 22:26:29 +00006271 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006272
6273 if (ni == NULL)
6274 return FAIL;
6275 copy_tv(tv, &ni->li_tv);
6276 if (item == NULL)
6277 /* Append new item at end of list. */
6278 list_append(l, ni);
6279 else
6280 {
6281 /* Insert new item before existing item. */
6282 ni->li_prev = item->li_prev;
6283 ni->li_next = item;
6284 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006285 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006286 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006287 ++l->lv_idx;
6288 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006289 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006290 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006291 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006292 l->lv_idx_item = NULL;
6293 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006294 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006295 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006296 }
6297 return OK;
6298}
6299
6300/*
6301 * Extend "l1" with "l2".
6302 * If "bef" is NULL append at the end, otherwise insert before this item.
6303 * Returns FAIL when out of memory.
6304 */
6305 static int
6306list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006307 list_T *l1;
6308 list_T *l2;
6309 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006310{
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006312 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006313
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006314 /* We also quit the loop when we have inserted the original item count of
6315 * the list, avoid a hang when we extend a list with itself. */
6316 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006317 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6318 return FAIL;
6319 return OK;
6320}
6321
6322/*
6323 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6324 * Return FAIL when out of memory.
6325 */
6326 static int
6327list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006328 list_T *l1;
6329 list_T *l2;
6330 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331{
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006333
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006334 if (l1 == NULL || l2 == NULL)
6335 return FAIL;
6336
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006338 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006339 if (l == NULL)
6340 return FAIL;
6341 tv->v_type = VAR_LIST;
6342 tv->vval.v_list = l;
6343
6344 /* append all items from the second list */
6345 return list_extend(l, l2, NULL);
6346}
6347
6348/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006349 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006351 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006352 * Returns NULL when out of memory.
6353 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006355list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006358 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359{
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 list_T *copy;
6361 listitem_T *item;
6362 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363
6364 if (orig == NULL)
6365 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006366
6367 copy = list_alloc();
6368 if (copy != NULL)
6369 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006370 if (copyID != 0)
6371 {
6372 /* Do this before adding the items, because one of the items may
6373 * refer back to this list. */
6374 orig->lv_copyID = copyID;
6375 orig->lv_copylist = copy;
6376 }
6377 for (item = orig->lv_first; item != NULL && !got_int;
6378 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379 {
6380 ni = listitem_alloc();
6381 if (ni == NULL)
6382 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006384 {
6385 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6386 {
6387 vim_free(ni);
6388 break;
6389 }
6390 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006392 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 list_append(copy, ni);
6394 }
6395 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006396 if (item != NULL)
6397 {
6398 list_unref(copy);
6399 copy = NULL;
6400 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401 }
6402
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403 return copy;
6404}
6405
6406/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006408 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006411list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 list_T *l;
6413 listitem_T *item;
6414 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006415{
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418 /* notify watchers */
6419 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006421 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 list_fix_watch(l, ip);
6423 if (ip == item2)
6424 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426
6427 if (item2->li_next == NULL)
6428 l->lv_last = item->li_prev;
6429 else
6430 item2->li_next->li_prev = item->li_prev;
6431 if (item->li_prev == NULL)
6432 l->lv_first = item2->li_next;
6433 else
6434 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436}
6437
6438/*
6439 * Return an allocated string with the string representation of a list.
6440 * May return NULL.
6441 */
6442 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006443list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006445 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446{
6447 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006448
6449 if (tv->vval.v_list == NULL)
6450 return NULL;
6451 ga_init2(&ga, (int)sizeof(char), 80);
6452 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006453 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006454 {
6455 vim_free(ga.ga_data);
6456 return NULL;
6457 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458 ga_append(&ga, ']');
6459 ga_append(&ga, NUL);
6460 return (char_u *)ga.ga_data;
6461}
6462
6463/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006464 * Join list "l" into a string in "*gap", using separator "sep".
6465 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006466 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006467 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006468 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006469list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006470 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006472 char_u *sep;
6473 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006474 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006475{
6476 int first = TRUE;
6477 char_u *tofree;
6478 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006480 char_u *s;
6481
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006482 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006483 {
6484 if (first)
6485 first = FALSE;
6486 else
6487 ga_concat(gap, sep);
6488
6489 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006490 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006491 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006492 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493 if (s != NULL)
6494 ga_concat(gap, s);
6495 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 if (s == NULL)
6497 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006498 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006499 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006501}
6502
6503/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006504 * Garbage collection for lists and dictionaries.
6505 *
6506 * We use reference counts to be able to free most items right away when they
6507 * are no longer used. But for composite items it's possible that it becomes
6508 * unused while the reference count is > 0: When there is a recursive
6509 * reference. Example:
6510 * :let l = [1, 2, 3]
6511 * :let d = {9: l}
6512 * :let l[1] = d
6513 *
6514 * Since this is quite unusual we handle this with garbage collection: every
6515 * once in a while find out which lists and dicts are not referenced from any
6516 * variable.
6517 *
6518 * Here is a good reference text about garbage collection (refers to Python
6519 * but it applies to all reference-counting mechanisms):
6520 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006521 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006522
6523/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006524 * Do garbage collection for lists and dicts.
6525 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006526 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006527 int
6528garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006529{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006530 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006531 buf_T *buf;
6532 win_T *wp;
6533 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006534 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006535 int did_free;
6536 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006537#ifdef FEAT_WINDOWS
6538 tabpage_T *tp;
6539#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006540
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006541 /* Only do this once. */
6542 want_garbage_collect = FALSE;
6543 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006544 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006545
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006546 /* We advance by two because we add one for items referenced through
6547 * previous_funccal. */
6548 current_copyID += COPYID_INC;
6549 copyID = current_copyID;
6550
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006551 /*
6552 * 1. Go through all accessible variables and mark all lists and dicts
6553 * with copyID.
6554 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006555
6556 /* Don't free variables in the previous_funccal list unless they are only
6557 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006558 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006559 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6560 {
6561 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6562 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6563 }
6564
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006565 /* script-local variables */
6566 for (i = 1; i <= ga_scripts.ga_len; ++i)
6567 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6568
6569 /* buffer-local variables */
6570 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6571 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6572
6573 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006574 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006575 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6576
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006577#ifdef FEAT_WINDOWS
6578 /* tabpage-local variables */
6579 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6580 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6581#endif
6582
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006583 /* global variables */
6584 set_ref_in_ht(&globvarht, copyID);
6585
6586 /* function-local variables */
6587 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6588 {
6589 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6590 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6591 }
6592
Bram Moolenaard812df62008-11-09 12:46:09 +00006593 /* v: vars */
6594 set_ref_in_ht(&vimvarht, copyID);
6595
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006596 /*
6597 * 2. Free lists and dictionaries that are not referenced.
6598 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006599 did_free = free_unref_items(copyID);
6600
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006601 /*
6602 * 3. Check if any funccal can be freed now.
6603 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006604 for (pfc = &previous_funccal; *pfc != NULL; )
6605 {
6606 if (can_free_funccal(*pfc, copyID))
6607 {
6608 fc = *pfc;
6609 *pfc = fc->caller;
6610 free_funccal(fc, TRUE);
6611 did_free = TRUE;
6612 did_free_funccal = TRUE;
6613 }
6614 else
6615 pfc = &(*pfc)->caller;
6616 }
6617 if (did_free_funccal)
6618 /* When a funccal was freed some more items might be garbage
6619 * collected, so run again. */
6620 (void)garbage_collect();
6621
6622 return did_free;
6623}
6624
6625/*
6626 * Free lists and dictionaries that are no longer referenced.
6627 */
6628 static int
6629free_unref_items(copyID)
6630 int copyID;
6631{
6632 dict_T *dd;
6633 list_T *ll;
6634 int did_free = FALSE;
6635
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006636 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006637 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006638 */
6639 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006640 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006641 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006642 /* Free the Dictionary and ordinary items it contains, but don't
6643 * recurse into Lists and Dictionaries, they will be in the list
6644 * of dicts or list of lists. */
6645 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006646 did_free = TRUE;
6647
6648 /* restart, next dict may also have been freed */
6649 dd = first_dict;
6650 }
6651 else
6652 dd = dd->dv_used_next;
6653
6654 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006655 * Go through the list of lists and free items without the copyID.
6656 * But don't free a list that has a watcher (used in a for loop), these
6657 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006658 */
6659 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006660 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6661 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006662 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006663 /* Free the List and ordinary items it contains, but don't recurse
6664 * into Lists and Dictionaries, they will be in the list of dicts
6665 * or list of lists. */
6666 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006667 did_free = TRUE;
6668
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006669 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006670 ll = first_list;
6671 }
6672 else
6673 ll = ll->lv_used_next;
6674
6675 return did_free;
6676}
6677
6678/*
6679 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6680 */
6681 static void
6682set_ref_in_ht(ht, copyID)
6683 hashtab_T *ht;
6684 int copyID;
6685{
6686 int todo;
6687 hashitem_T *hi;
6688
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006689 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006690 for (hi = ht->ht_array; todo > 0; ++hi)
6691 if (!HASHITEM_EMPTY(hi))
6692 {
6693 --todo;
6694 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6695 }
6696}
6697
6698/*
6699 * Mark all lists and dicts referenced through list "l" with "copyID".
6700 */
6701 static void
6702set_ref_in_list(l, copyID)
6703 list_T *l;
6704 int copyID;
6705{
6706 listitem_T *li;
6707
6708 for (li = l->lv_first; li != NULL; li = li->li_next)
6709 set_ref_in_item(&li->li_tv, copyID);
6710}
6711
6712/*
6713 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6714 */
6715 static void
6716set_ref_in_item(tv, copyID)
6717 typval_T *tv;
6718 int copyID;
6719{
6720 dict_T *dd;
6721 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006722
6723 switch (tv->v_type)
6724 {
6725 case VAR_DICT:
6726 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006727 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006728 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006729 /* Didn't see this dict yet. */
6730 dd->dv_copyID = copyID;
6731 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006732 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006733 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734
6735 case VAR_LIST:
6736 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006737 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006738 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 /* Didn't see this list yet. */
6740 ll->lv_copyID = copyID;
6741 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006742 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006744 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006745 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006746}
6747
6748/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749 * Allocate an empty header for a dictionary.
6750 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006751 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752dict_alloc()
6753{
Bram Moolenaar33570922005-01-25 22:26:29 +00006754 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006755
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006757 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006758 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006759 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 if (first_dict != NULL)
6761 first_dict->dv_used_prev = d;
6762 d->dv_used_next = first_dict;
6763 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006764 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006765
Bram Moolenaar33570922005-01-25 22:26:29 +00006766 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006767 d->dv_lock = 0;
6768 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006769 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006770 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006771 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006772}
6773
6774/*
6775 * Unreference a Dictionary: decrement the reference count and free it when it
6776 * becomes zero.
6777 */
6778 static void
6779dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006781{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006782 if (d != NULL && --d->dv_refcount <= 0)
6783 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006784}
6785
6786/*
6787 * Free a Dictionary, including all items it contains.
6788 * Ignores the reference count.
6789 */
6790 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006791dict_free(d, recurse)
6792 dict_T *d;
6793 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006794{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006795 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006796 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006797 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006798
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006799 /* Remove the dict from the list of dicts for garbage collection. */
6800 if (d->dv_used_prev == NULL)
6801 first_dict = d->dv_used_next;
6802 else
6803 d->dv_used_prev->dv_used_next = d->dv_used_next;
6804 if (d->dv_used_next != NULL)
6805 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6806
6807 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006808 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006809 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006810 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006811 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006812 if (!HASHITEM_EMPTY(hi))
6813 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006814 /* Remove the item before deleting it, just in case there is
6815 * something recursive causing trouble. */
6816 di = HI2DI(hi);
6817 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006818 if (recurse || (di->di_tv.v_type != VAR_LIST
6819 && di->di_tv.v_type != VAR_DICT))
6820 clear_tv(&di->di_tv);
6821 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006822 --todo;
6823 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006825 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006826 vim_free(d);
6827}
6828
6829/*
6830 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006831 * The "key" is copied to the new item.
6832 * Note that the value of the item "di_tv" still needs to be initialized!
6833 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006834 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006835 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006836dictitem_alloc(key)
6837 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838{
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006840
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006841 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006842 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006843 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006844 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006845 di->di_flags = 0;
6846 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006847 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006848}
6849
6850/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851 * Make a copy of a Dictionary item.
6852 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006853 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006854dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006855 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006856{
Bram Moolenaar33570922005-01-25 22:26:29 +00006857 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006858
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006859 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6860 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006861 if (di != NULL)
6862 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006863 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006864 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006865 copy_tv(&org->di_tv, &di->di_tv);
6866 }
6867 return di;
6868}
6869
6870/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006871 * Remove item "item" from Dictionary "dict" and free it.
6872 */
6873 static void
6874dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006875 dict_T *dict;
6876 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006877{
Bram Moolenaar33570922005-01-25 22:26:29 +00006878 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006879
Bram Moolenaar33570922005-01-25 22:26:29 +00006880 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006881 if (HASHITEM_EMPTY(hi))
6882 EMSG2(_(e_intern2), "dictitem_remove()");
6883 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006884 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006885 dictitem_free(item);
6886}
6887
6888/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006889 * Free a dict item. Also clears the value.
6890 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006891 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006892dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006893 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006894{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006895 clear_tv(&item->di_tv);
6896 vim_free(item);
6897}
6898
6899/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6901 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006902 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006903 * Returns NULL when out of memory.
6904 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006905 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006906dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006907 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006908 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006909 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006910{
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 dict_T *copy;
6912 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006913 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006914 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006915
6916 if (orig == NULL)
6917 return NULL;
6918
6919 copy = dict_alloc();
6920 if (copy != NULL)
6921 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006922 if (copyID != 0)
6923 {
6924 orig->dv_copyID = copyID;
6925 orig->dv_copydict = copy;
6926 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006927 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006928 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006930 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006931 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006932 --todo;
6933
6934 di = dictitem_alloc(hi->hi_key);
6935 if (di == NULL)
6936 break;
6937 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006938 {
6939 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6940 copyID) == FAIL)
6941 {
6942 vim_free(di);
6943 break;
6944 }
6945 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006946 else
6947 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6948 if (dict_add(copy, di) == FAIL)
6949 {
6950 dictitem_free(di);
6951 break;
6952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006953 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006954 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006955
Bram Moolenaare9a41262005-01-15 22:18:47 +00006956 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006957 if (todo > 0)
6958 {
6959 dict_unref(copy);
6960 copy = NULL;
6961 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962 }
6963
6964 return copy;
6965}
6966
6967/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006969 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006971 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 dict_T *d;
6974 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006975{
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006977}
6978
Bram Moolenaar8c711452005-01-14 21:53:12 +00006979/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006980 * Add a number or string entry to dictionary "d".
6981 * When "str" is NULL use number "nr", otherwise use "str".
6982 * Returns FAIL when out of memory and when key already exists.
6983 */
6984 int
6985dict_add_nr_str(d, key, nr, str)
6986 dict_T *d;
6987 char *key;
6988 long nr;
6989 char_u *str;
6990{
6991 dictitem_T *item;
6992
6993 item = dictitem_alloc((char_u *)key);
6994 if (item == NULL)
6995 return FAIL;
6996 item->di_tv.v_lock = 0;
6997 if (str == NULL)
6998 {
6999 item->di_tv.v_type = VAR_NUMBER;
7000 item->di_tv.vval.v_number = nr;
7001 }
7002 else
7003 {
7004 item->di_tv.v_type = VAR_STRING;
7005 item->di_tv.vval.v_string = vim_strsave(str);
7006 }
7007 if (dict_add(d, item) == FAIL)
7008 {
7009 dictitem_free(item);
7010 return FAIL;
7011 }
7012 return OK;
7013}
7014
7015/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007016 * Get the number of items in a Dictionary.
7017 */
7018 static long
7019dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007021{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007022 if (d == NULL)
7023 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007024 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007025}
7026
7027/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028 * Find item "key[len]" in Dictionary "d".
7029 * If "len" is negative use strlen(key).
7030 * Returns NULL when not found.
7031 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035 char_u *key;
7036 int len;
7037{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007038#define AKEYLEN 200
7039 char_u buf[AKEYLEN];
7040 char_u *akey;
7041 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007042 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007043
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007044 if (len < 0)
7045 akey = key;
7046 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007048 tofree = akey = vim_strnsave(key, len);
7049 if (akey == NULL)
7050 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007051 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007052 else
7053 {
7054 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007055 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007056 akey = buf;
7057 }
7058
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007060 vim_free(tofree);
7061 if (HASHITEM_EMPTY(hi))
7062 return NULL;
7063 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064}
7065
7066/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007067 * Get a string item from a dictionary.
7068 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007069 * Returns NULL if the entry doesn't exist or out of memory.
7070 */
7071 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007072get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007073 dict_T *d;
7074 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007075 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007076{
7077 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007078 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007079
7080 di = dict_find(d, key, -1);
7081 if (di == NULL)
7082 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007083 s = get_tv_string(&di->di_tv);
7084 if (save && s != NULL)
7085 s = vim_strsave(s);
7086 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007087}
7088
7089/*
7090 * Get a number item from a dictionary.
7091 * Returns 0 if the entry doesn't exist or out of memory.
7092 */
7093 long
7094get_dict_number(d, key)
7095 dict_T *d;
7096 char_u *key;
7097{
7098 dictitem_T *di;
7099
7100 di = dict_find(d, key, -1);
7101 if (di == NULL)
7102 return 0;
7103 return get_tv_number(&di->di_tv);
7104}
7105
7106/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107 * Return an allocated string with the string representation of a Dictionary.
7108 * May return NULL.
7109 */
7110 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007111dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007112 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007113 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007114{
7115 garray_T ga;
7116 int first = TRUE;
7117 char_u *tofree;
7118 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007123
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125 return NULL;
7126 ga_init2(&ga, (int)sizeof(char), 80);
7127 ga_append(&ga, '{');
7128
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007129 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007130 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007134 --todo;
7135
7136 if (first)
7137 first = FALSE;
7138 else
7139 ga_concat(&ga, (char_u *)", ");
7140
7141 tofree = string_quote(hi->hi_key, FALSE);
7142 if (tofree != NULL)
7143 {
7144 ga_concat(&ga, tofree);
7145 vim_free(tofree);
7146 }
7147 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007148 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007149 if (s != NULL)
7150 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007151 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007152 if (s == NULL)
7153 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007156 if (todo > 0)
7157 {
7158 vim_free(ga.ga_data);
7159 return NULL;
7160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007161
7162 ga_append(&ga, '}');
7163 ga_append(&ga, NUL);
7164 return (char_u *)ga.ga_data;
7165}
7166
7167/*
7168 * Allocate a variable for a Dictionary and fill it from "*arg".
7169 * Return OK or FAIL. Returns NOTDONE for {expr}.
7170 */
7171 static int
7172get_dict_tv(arg, rettv, evaluate)
7173 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 int evaluate;
7176{
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dict_T *d = NULL;
7178 typval_T tvkey;
7179 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007180 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184
7185 /*
7186 * First check if it's not a curly-braces thing: {expr}.
7187 * Must do this without evaluating, otherwise a function may be called
7188 * twice. Unfortunately this means we need to call eval1() twice for the
7189 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192 if (*start != '}')
7193 {
7194 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7195 return FAIL;
7196 if (*start == '}')
7197 return NOTDONE;
7198 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199
7200 if (evaluate)
7201 {
7202 d = dict_alloc();
7203 if (d == NULL)
7204 return FAIL;
7205 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007206 tvkey.v_type = VAR_UNKNOWN;
7207 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208
7209 *arg = skipwhite(*arg + 1);
7210 while (**arg != '}' && **arg != NUL)
7211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 goto failret;
7214 if (**arg != ':')
7215 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007216 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 goto failret;
7219 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007220 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007222 key = get_tv_string_buf_chk(&tvkey, buf);
7223 if (key == NULL || *key == NUL)
7224 {
7225 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7226 if (key != NULL)
7227 EMSG(_(e_emptykey));
7228 clear_tv(&tvkey);
7229 goto failret;
7230 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232
7233 *arg = skipwhite(*arg + 1);
7234 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7235 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007236 if (evaluate)
7237 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 goto failret;
7239 }
7240 if (evaluate)
7241 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007242 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 if (item != NULL)
7244 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007245 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 clear_tv(&tv);
7248 goto failret;
7249 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007250 item = dictitem_alloc(key);
7251 clear_tv(&tvkey);
7252 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007255 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 if (dict_add(d, item) == FAIL)
7257 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 }
7259 }
7260
7261 if (**arg == '}')
7262 break;
7263 if (**arg != ',')
7264 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007265 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 goto failret;
7267 }
7268 *arg = skipwhite(*arg + 1);
7269 }
7270
7271 if (**arg != '}')
7272 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007273 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274failret:
7275 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007276 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 return FAIL;
7278 }
7279
7280 *arg = skipwhite(*arg + 1);
7281 if (evaluate)
7282 {
7283 rettv->v_type = VAR_DICT;
7284 rettv->vval.v_dict = d;
7285 ++d->dv_refcount;
7286 }
7287
7288 return OK;
7289}
7290
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007292 * Return a string with the string representation of a variable.
7293 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007294 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007295 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007296 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007297 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007298 */
7299 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007300echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007302 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007303 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007304 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007305{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 static int recurse = 0;
7307 char_u *r = NULL;
7308
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007311 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007312 *tofree = NULL;
7313 return NULL;
7314 }
7315 ++recurse;
7316
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007317 switch (tv->v_type)
7318 {
7319 case VAR_FUNC:
7320 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321 r = tv->vval.v_string;
7322 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007323
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007324 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007325 if (tv->vval.v_list == NULL)
7326 {
7327 *tofree = NULL;
7328 r = NULL;
7329 }
7330 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7331 {
7332 *tofree = NULL;
7333 r = (char_u *)"[...]";
7334 }
7335 else
7336 {
7337 tv->vval.v_list->lv_copyID = copyID;
7338 *tofree = list2string(tv, copyID);
7339 r = *tofree;
7340 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007341 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007342
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007344 if (tv->vval.v_dict == NULL)
7345 {
7346 *tofree = NULL;
7347 r = NULL;
7348 }
7349 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7350 {
7351 *tofree = NULL;
7352 r = (char_u *)"{...}";
7353 }
7354 else
7355 {
7356 tv->vval.v_dict->dv_copyID = copyID;
7357 *tofree = dict2string(tv, copyID);
7358 r = *tofree;
7359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007361
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007362 case VAR_STRING:
7363 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007364 *tofree = NULL;
7365 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007366 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007367
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007368#ifdef FEAT_FLOAT
7369 case VAR_FLOAT:
7370 *tofree = NULL;
7371 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7372 r = numbuf;
7373 break;
7374#endif
7375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007377 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007379 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380
7381 --recurse;
7382 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007383}
7384
7385/*
7386 * Return a string with the string representation of a variable.
7387 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7388 * "numbuf" is used for a number.
7389 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007390 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007391 */
7392 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007393tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007395 char_u **tofree;
7396 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007397 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007398{
7399 switch (tv->v_type)
7400 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007401 case VAR_FUNC:
7402 *tofree = string_quote(tv->vval.v_string, TRUE);
7403 return *tofree;
7404 case VAR_STRING:
7405 *tofree = string_quote(tv->vval.v_string, FALSE);
7406 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007407#ifdef FEAT_FLOAT
7408 case VAR_FLOAT:
7409 *tofree = NULL;
7410 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7411 return numbuf;
7412#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007414 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007417 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007418 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007419 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007420 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007421}
7422
7423/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 * Return string "str" in ' quotes, doubling ' characters.
7425 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007427 */
7428 static char_u *
7429string_quote(str, function)
7430 char_u *str;
7431 int function;
7432{
Bram Moolenaar33570922005-01-25 22:26:29 +00007433 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007434 char_u *p, *r, *s;
7435
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 len = (function ? 13 : 3);
7437 if (str != NULL)
7438 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007439 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007440 for (p = str; *p != NUL; mb_ptr_adv(p))
7441 if (*p == '\'')
7442 ++len;
7443 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007444 s = r = alloc(len);
7445 if (r != NULL)
7446 {
7447 if (function)
7448 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007450 r += 10;
7451 }
7452 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 if (str != NULL)
7455 for (p = str; *p != NUL; )
7456 {
7457 if (*p == '\'')
7458 *r++ = '\'';
7459 MB_COPY_CHAR(p, r);
7460 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007462 if (function)
7463 *r++ = ')';
7464 *r++ = NUL;
7465 }
7466 return s;
7467}
7468
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007469#ifdef FEAT_FLOAT
7470/*
7471 * Convert the string "text" to a floating point number.
7472 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7473 * this always uses a decimal point.
7474 * Returns the length of the text that was consumed.
7475 */
7476 static int
7477string2float(text, value)
7478 char_u *text;
7479 float_T *value; /* result stored here */
7480{
7481 char *s = (char *)text;
7482 float_T f;
7483
7484 f = strtod(s, &s);
7485 *value = f;
7486 return (int)((char_u *)s - text);
7487}
7488#endif
7489
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 * Get the value of an environment variable.
7492 * "arg" is pointing to the '$'. It is advanced to after the name.
7493 * If the environment variable was not set, silently assume it is empty.
7494 * Always return OK.
7495 */
7496 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007497get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 int evaluate;
7501{
7502 char_u *string = NULL;
7503 int len;
7504 int cc;
7505 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007506 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507
7508 ++*arg;
7509 name = *arg;
7510 len = get_env_len(arg);
7511 if (evaluate)
7512 {
7513 if (len != 0)
7514 {
7515 cc = name[len];
7516 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007517 /* first try vim_getenv(), fast for normal environment vars */
7518 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007520 {
7521 if (!mustfree)
7522 string = vim_strsave(string);
7523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 else
7525 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007526 if (mustfree)
7527 vim_free(string);
7528
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 /* next try expanding things like $VIM and ${HOME} */
7530 string = expand_env_save(name - 1);
7531 if (string != NULL && *string == '$')
7532 {
7533 vim_free(string);
7534 string = NULL;
7535 }
7536 }
7537 name[len] = cc;
7538 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007539 rettv->v_type = VAR_STRING;
7540 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 }
7542
7543 return OK;
7544}
7545
7546/*
7547 * Array with names and number of arguments of all internal functions
7548 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7549 */
7550static struct fst
7551{
7552 char *f_name; /* function name */
7553 char f_min_argc; /* minimal number of arguments */
7554 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007555 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007556 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557} functions[] =
7558{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007559#ifdef FEAT_FLOAT
7560 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007561 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007562#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007563 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 {"append", 2, 2, f_append},
7565 {"argc", 0, 0, f_argc},
7566 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007567 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007568#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007569 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007570 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007571 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007574 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {"bufexists", 1, 1, f_bufexists},
7576 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7577 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7578 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7579 {"buflisted", 1, 1, f_buflisted},
7580 {"bufloaded", 1, 1, f_bufloaded},
7581 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007582 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 {"bufwinnr", 1, 1, f_bufwinnr},
7584 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007585 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007587#ifdef FEAT_FLOAT
7588 {"ceil", 1, 1, f_ceil},
7589#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007590 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {"char2nr", 1, 1, f_char2nr},
7592 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007593 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007595#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007596 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007597 {"complete_add", 1, 1, f_complete_add},
7598 {"complete_check", 0, 0, f_complete_check},
7599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007602#ifdef FEAT_FLOAT
7603 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007604 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007605#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007606 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007608 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {"delete", 1, 1, f_delete},
7611 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007612 {"diff_filler", 1, 1, f_diff_filler},
7613 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007614 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007616 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 {"eventhandler", 0, 0, f_eventhandler},
7618 {"executable", 1, 1, f_executable},
7619 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007620#ifdef FEAT_FLOAT
7621 {"exp", 1, 1, f_exp},
7622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007624 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007625 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7627 {"filereadable", 1, 1, f_filereadable},
7628 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007629 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007630 {"finddir", 1, 3, f_finddir},
7631 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007632#ifdef FEAT_FLOAT
7633 {"float2nr", 1, 1, f_float2nr},
7634 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007635 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007636#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007637 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {"fnamemodify", 2, 2, f_fnamemodify},
7639 {"foldclosed", 1, 1, f_foldclosed},
7640 {"foldclosedend", 1, 1, f_foldclosedend},
7641 {"foldlevel", 1, 1, f_foldlevel},
7642 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007643 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007645 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007646 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007647 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007648 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 {"getbufvar", 2, 2, f_getbufvar},
7650 {"getchar", 0, 1, f_getchar},
7651 {"getcharmod", 0, 0, f_getcharmod},
7652 {"getcmdline", 0, 0, f_getcmdline},
7653 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007654 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007656 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007657 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {"getfsize", 1, 1, f_getfsize},
7659 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007660 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007661 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007662 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007663 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007664 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007665 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007666 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007667 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007669 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007670 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {"getwinposx", 0, 0, f_getwinposx},
7672 {"getwinposy", 0, 0, f_getwinposy},
7673 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007674 {"glob", 1, 2, f_glob},
7675 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007678 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007679 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7681 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7682 {"histadd", 2, 2, f_histadd},
7683 {"histdel", 1, 2, f_histdel},
7684 {"histget", 1, 2, f_histget},
7685 {"histnr", 1, 1, f_histnr},
7686 {"hlID", 1, 1, f_hlID},
7687 {"hlexists", 1, 1, f_hlexists},
7688 {"hostname", 0, 0, f_hostname},
7689 {"iconv", 3, 3, f_iconv},
7690 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007691 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007692 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007694 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {"inputrestore", 0, 0, f_inputrestore},
7696 {"inputsave", 0, 0, f_inputsave},
7697 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007700 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007702 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007705 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {"libcall", 3, 3, f_libcall},
7707 {"libcallnr", 3, 3, f_libcallnr},
7708 {"line", 1, 1, f_line},
7709 {"line2byte", 1, 1, f_line2byte},
7710 {"lispindent", 1, 1, f_lispindent},
7711 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007712#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007713 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007714 {"log10", 1, 1, f_log10},
7715#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007716 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007717 {"maparg", 1, 3, f_maparg},
7718 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007719 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007720 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007721 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007722 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007723 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007724 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007725 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007726 {"max", 1, 1, f_max},
7727 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007728#ifdef vim_mkdir
7729 {"mkdir", 1, 3, f_mkdir},
7730#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007731 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007732#ifdef FEAT_MZSCHEME
7733 {"mzeval", 1, 1, f_mzeval},
7734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {"nextnonblank", 1, 1, f_nextnonblank},
7736 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007737 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007738#ifdef FEAT_FLOAT
7739 {"pow", 2, 2, f_pow},
7740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007742 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007743 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007745 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007746 {"reltime", 0, 2, f_reltime},
7747 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {"remote_expr", 2, 3, f_remote_expr},
7749 {"remote_foreground", 1, 1, f_remote_foreground},
7750 {"remote_peek", 1, 2, f_remote_peek},
7751 {"remote_read", 1, 1, f_remote_read},
7752 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007753 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007755 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007757 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007758#ifdef FEAT_FLOAT
7759 {"round", 1, 1, f_round},
7760#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007761 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007762 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007763 {"searchpair", 3, 7, f_searchpair},
7764 {"searchpairpos", 3, 7, f_searchpairpos},
7765 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {"server2client", 2, 2, f_server2client},
7767 {"serverlist", 0, 0, f_serverlist},
7768 {"setbufvar", 3, 3, f_setbufvar},
7769 {"setcmdpos", 1, 1, f_setcmdpos},
7770 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007771 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007772 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007773 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007774 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007776 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007777 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007779 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007781#ifdef FEAT_FLOAT
7782 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007783 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007784#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007785 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007786 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007787 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007788 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007789 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007790#ifdef FEAT_FLOAT
7791 {"sqrt", 1, 1, f_sqrt},
7792 {"str2float", 1, 1, f_str2float},
7793#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007794 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795#ifdef HAVE_STRFTIME
7796 {"strftime", 1, 2, f_strftime},
7797#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 {"strlen", 1, 1, f_strlen},
7801 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007802 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {"strtrans", 1, 1, f_strtrans},
7804 {"submatch", 1, 1, f_submatch},
7805 {"substitute", 4, 4, f_substitute},
7806 {"synID", 3, 3, f_synID},
7807 {"synIDattr", 2, 3, f_synIDattr},
7808 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007809 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007810 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007811 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007812 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007813 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007814 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007815 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007816#ifdef FEAT_FLOAT
7817 {"tan", 1, 1, f_tan},
7818 {"tanh", 1, 1, f_tanh},
7819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007821 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {"tolower", 1, 1, f_tolower},
7823 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007824 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007825#ifdef FEAT_FLOAT
7826 {"trunc", 1, 1, f_trunc},
7827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007829 {"undofile", 1, 1, f_undofile},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007830 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {"virtcol", 1, 1, f_virtcol},
7832 {"visualmode", 0, 1, f_visualmode},
7833 {"winbufnr", 1, 1, f_winbufnr},
7834 {"wincol", 0, 0, f_wincol},
7835 {"winheight", 1, 1, f_winheight},
7836 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007837 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007839 {"winrestview", 1, 1, f_winrestview},
7840 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007842 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843};
7844
7845#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7846
7847/*
7848 * Function given to ExpandGeneric() to obtain the list of internal
7849 * or user defined function names.
7850 */
7851 char_u *
7852get_function_name(xp, idx)
7853 expand_T *xp;
7854 int idx;
7855{
7856 static int intidx = -1;
7857 char_u *name;
7858
7859 if (idx == 0)
7860 intidx = -1;
7861 if (intidx < 0)
7862 {
7863 name = get_user_func_name(xp, idx);
7864 if (name != NULL)
7865 return name;
7866 }
7867 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7868 {
7869 STRCPY(IObuff, functions[intidx].f_name);
7870 STRCAT(IObuff, "(");
7871 if (functions[intidx].f_max_argc == 0)
7872 STRCAT(IObuff, ")");
7873 return IObuff;
7874 }
7875
7876 return NULL;
7877}
7878
7879/*
7880 * Function given to ExpandGeneric() to obtain the list of internal or
7881 * user defined variable or function names.
7882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 char_u *
7884get_expr_name(xp, idx)
7885 expand_T *xp;
7886 int idx;
7887{
7888 static int intidx = -1;
7889 char_u *name;
7890
7891 if (idx == 0)
7892 intidx = -1;
7893 if (intidx < 0)
7894 {
7895 name = get_function_name(xp, idx);
7896 if (name != NULL)
7897 return name;
7898 }
7899 return get_user_var_name(xp, ++intidx);
7900}
7901
7902#endif /* FEAT_CMDL_COMPL */
7903
7904/*
7905 * Find internal function in table above.
7906 * Return index, or -1 if not found
7907 */
7908 static int
7909find_internal_func(name)
7910 char_u *name; /* name of the function */
7911{
7912 int first = 0;
7913 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7914 int cmp;
7915 int x;
7916
7917 /*
7918 * Find the function name in the table. Binary search.
7919 */
7920 while (first <= last)
7921 {
7922 x = first + ((unsigned)(last - first) >> 1);
7923 cmp = STRCMP(name, functions[x].f_name);
7924 if (cmp < 0)
7925 last = x - 1;
7926 else if (cmp > 0)
7927 first = x + 1;
7928 else
7929 return x;
7930 }
7931 return -1;
7932}
7933
7934/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7936 * name it contains, otherwise return "name".
7937 */
7938 static char_u *
7939deref_func_name(name, lenp)
7940 char_u *name;
7941 int *lenp;
7942{
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944 int cc;
7945
7946 cc = name[*lenp];
7947 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007948 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007950 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007953 {
7954 *lenp = 0;
7955 return (char_u *)""; /* just in case */
7956 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007957 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007959 }
7960
7961 return name;
7962}
7963
7964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 * Allocate a variable for the result of a function.
7966 * Return OK or FAIL.
7967 */
7968 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007969get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7970 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 char_u *name; /* name of the function */
7972 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 char_u **arg; /* argument, pointing to the '(' */
7975 linenr_T firstline; /* first line of range */
7976 linenr_T lastline; /* last line of range */
7977 int *doesrange; /* return: function handled range */
7978 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 char_u *argp;
7982 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007983 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 int argcount = 0; /* number of arguments found */
7985
7986 /*
7987 * Get the arguments.
7988 */
7989 argp = *arg;
7990 while (argcount < MAX_FUNC_ARGS)
7991 {
7992 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7993 if (*argp == ')' || *argp == ',' || *argp == NUL)
7994 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7996 {
7997 ret = FAIL;
7998 break;
7999 }
8000 ++argcount;
8001 if (*argp != ',')
8002 break;
8003 }
8004 if (*argp == ')')
8005 ++argp;
8006 else
8007 ret = FAIL;
8008
8009 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008011 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008013 {
8014 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008015 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008017 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008021 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022
8023 *arg = skipwhite(argp);
8024 return ret;
8025}
8026
8027
8028/*
8029 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008030 * Return OK when the function can't be called, FAIL otherwise.
8031 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 */
8033 static int
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008034call_func(func_name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008035 doesrange, evaluate, selfdict)
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008036 char_u *func_name; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008040 typval_T *argvars; /* vars for arguments, must have "argcount"
8041 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 linenr_T firstline; /* first line of range */
8043 linenr_T lastline; /* last line of range */
8044 int *doesrange; /* return: function handled range */
8045 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008046 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049#define ERROR_UNKNOWN 0
8050#define ERROR_TOOMANY 1
8051#define ERROR_TOOFEW 2
8052#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053#define ERROR_DICT 4
8054#define ERROR_NONE 5
8055#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 int error = ERROR_NONE;
8057 int i;
8058 int llen;
8059 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060#define FLEN_FIXED 40
8061 char_u fname_buf[FLEN_FIXED + 1];
8062 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008063 char_u *name;
8064
8065 /* Make a copy of the name, if it comes from a funcref variable it could
8066 * be changed or deleted in the called function. */
8067 name = vim_strnsave(func_name, len);
8068 if (name == NULL)
8069 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070
8071 /*
8072 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8073 * Change <SNR>123_name() to K_SNR 123_name().
8074 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 llen = eval_fname_script(name);
8077 if (llen > 0)
8078 {
8079 fname_buf[0] = K_SPECIAL;
8080 fname_buf[1] = KS_EXTRA;
8081 fname_buf[2] = (int)KE_SNR;
8082 i = 3;
8083 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8084 {
8085 if (current_SID <= 0)
8086 error = ERROR_SCRIPT;
8087 else
8088 {
8089 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8090 i = (int)STRLEN(fname_buf);
8091 }
8092 }
8093 if (i + STRLEN(name + llen) < FLEN_FIXED)
8094 {
8095 STRCPY(fname_buf + i, name + llen);
8096 fname = fname_buf;
8097 }
8098 else
8099 {
8100 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8101 if (fname == NULL)
8102 error = ERROR_OTHER;
8103 else
8104 {
8105 mch_memmove(fname, fname_buf, (size_t)i);
8106 STRCPY(fname + i, name + llen);
8107 }
8108 }
8109 }
8110 else
8111 fname = name;
8112
8113 *doesrange = FALSE;
8114
8115
8116 /* execute the function if no errors detected and executing */
8117 if (evaluate && error == ERROR_NONE)
8118 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008119 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8120 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 error = ERROR_UNKNOWN;
8122
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008123 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {
8125 /*
8126 * User defined function.
8127 */
8128 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008129
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008131 /* Trigger FuncUndefined event, may load the function. */
8132 if (fp == NULL
8133 && apply_autocmds(EVENT_FUNCUNDEFINED,
8134 fname, fname, TRUE, NULL)
8135 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008137 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 fp = find_func(fname);
8139 }
8140#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008141 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008142 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008143 {
8144 /* loaded a package, search for the function again */
8145 fp = find_func(fname);
8146 }
8147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 if (fp != NULL)
8149 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008150 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008152 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008154 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008156 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008157 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 else
8159 {
8160 /*
8161 * Call the user function.
8162 * Save and restore search patterns, script variables and
8163 * redo buffer.
8164 */
8165 save_search_patterns();
8166 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008167 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008168 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008169 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008170 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8171 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8172 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008173 /* Function was unreferenced while being used, free it
8174 * now. */
8175 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 restoreRedobuff();
8177 restore_search_patterns();
8178 error = ERROR_NONE;
8179 }
8180 }
8181 }
8182 else
8183 {
8184 /*
8185 * Find the function name in the table, call its implementation.
8186 */
8187 i = find_internal_func(fname);
8188 if (i >= 0)
8189 {
8190 if (argcount < functions[i].f_min_argc)
8191 error = ERROR_TOOFEW;
8192 else if (argcount > functions[i].f_max_argc)
8193 error = ERROR_TOOMANY;
8194 else
8195 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008196 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008197 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 error = ERROR_NONE;
8199 }
8200 }
8201 }
8202 /*
8203 * The function call (or "FuncUndefined" autocommand sequence) might
8204 * have been aborted by an error, an interrupt, or an explicitly thrown
8205 * exception that has not been caught so far. This situation can be
8206 * tested for by calling aborting(). For an error in an internal
8207 * function or for the "E132" error in call_user_func(), however, the
8208 * throw point at which the "force_abort" flag (temporarily reset by
8209 * emsg()) is normally updated has not been reached yet. We need to
8210 * update that flag first to make aborting() reliable.
8211 */
8212 update_force_abort();
8213 }
8214 if (error == ERROR_NONE)
8215 ret = OK;
8216
8217 /*
8218 * Report an error unless the argument evaluation or function call has been
8219 * cancelled due to an aborting error, an interrupt, or an exception.
8220 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008221 if (!aborting())
8222 {
8223 switch (error)
8224 {
8225 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008226 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008227 break;
8228 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008229 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008230 break;
8231 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008232 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008233 name);
8234 break;
8235 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008236 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008237 name);
8238 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008239 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008240 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008241 name);
8242 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008243 }
8244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 if (fname != name && fname != fname_buf)
8247 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008248 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249
8250 return ret;
8251}
8252
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008253/*
8254 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008255 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008256 */
8257 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008258emsg_funcname(ermsg, name)
8259 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008260 char_u *name;
8261{
8262 char_u *p;
8263
8264 if (*name == K_SPECIAL)
8265 p = concat_str((char_u *)"<SNR>", name + 3);
8266 else
8267 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008268 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008269 if (p != name)
8270 vim_free(p);
8271}
8272
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008273/*
8274 * Return TRUE for a non-zero Number and a non-empty String.
8275 */
8276 static int
8277non_zero_arg(argvars)
8278 typval_T *argvars;
8279{
8280 return ((argvars[0].v_type == VAR_NUMBER
8281 && argvars[0].vval.v_number != 0)
8282 || (argvars[0].v_type == VAR_STRING
8283 && argvars[0].vval.v_string != NULL
8284 && *argvars[0].vval.v_string != NUL));
8285}
8286
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287/*********************************************
8288 * Implementation of the built-in functions
8289 */
8290
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008291#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008292static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8293
8294/*
8295 * Get the float value of "argvars[0]" into "f".
8296 * Returns FAIL when the argument is not a Number or Float.
8297 */
8298 static int
8299get_float_arg(argvars, f)
8300 typval_T *argvars;
8301 float_T *f;
8302{
8303 if (argvars[0].v_type == VAR_FLOAT)
8304 {
8305 *f = argvars[0].vval.v_float;
8306 return OK;
8307 }
8308 if (argvars[0].v_type == VAR_NUMBER)
8309 {
8310 *f = (float_T)argvars[0].vval.v_number;
8311 return OK;
8312 }
8313 EMSG(_("E808: Number or Float required"));
8314 return FAIL;
8315}
8316
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008317/*
8318 * "abs(expr)" function
8319 */
8320 static void
8321f_abs(argvars, rettv)
8322 typval_T *argvars;
8323 typval_T *rettv;
8324{
8325 if (argvars[0].v_type == VAR_FLOAT)
8326 {
8327 rettv->v_type = VAR_FLOAT;
8328 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8329 }
8330 else
8331 {
8332 varnumber_T n;
8333 int error = FALSE;
8334
8335 n = get_tv_number_chk(&argvars[0], &error);
8336 if (error)
8337 rettv->vval.v_number = -1;
8338 else if (n > 0)
8339 rettv->vval.v_number = n;
8340 else
8341 rettv->vval.v_number = -n;
8342 }
8343}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008344
8345/*
8346 * "acos()" function
8347 */
8348 static void
8349f_acos(argvars, rettv)
8350 typval_T *argvars;
8351 typval_T *rettv;
8352{
8353 float_T f;
8354
8355 rettv->v_type = VAR_FLOAT;
8356 if (get_float_arg(argvars, &f) == OK)
8357 rettv->vval.v_float = acos(f);
8358 else
8359 rettv->vval.v_float = 0.0;
8360}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361#endif
8362
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008364 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 */
8366 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008367f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 typval_T *argvars;
8369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
Bram Moolenaar33570922005-01-25 22:26:29 +00008371 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008374 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008376 if ((l = argvars[0].vval.v_list) != NULL
8377 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8378 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008380 }
8381 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008382 EMSG(_(e_listreq));
8383}
8384
8385/*
8386 * "append(lnum, string/list)" function
8387 */
8388 static void
8389f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008390 typval_T *argvars;
8391 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008392{
8393 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008394 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008395 list_T *l = NULL;
8396 listitem_T *li = NULL;
8397 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008398 long added = 0;
8399
Bram Moolenaar0d660222005-01-07 21:51:51 +00008400 lnum = get_tv_lnum(argvars);
8401 if (lnum >= 0
8402 && lnum <= curbuf->b_ml.ml_line_count
8403 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008404 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008405 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008406 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008407 l = argvars[1].vval.v_list;
8408 if (l == NULL)
8409 return;
8410 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008411 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008412 for (;;)
8413 {
8414 if (l == NULL)
8415 tv = &argvars[1]; /* append a string */
8416 else if (li == NULL)
8417 break; /* end of list */
8418 else
8419 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008420 line = get_tv_string_chk(tv);
8421 if (line == NULL) /* type error */
8422 {
8423 rettv->vval.v_number = 1; /* Failed */
8424 break;
8425 }
8426 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008427 ++added;
8428 if (l == NULL)
8429 break;
8430 li = li->li_next;
8431 }
8432
8433 appended_lines_mark(lnum, added);
8434 if (curwin->w_cursor.lnum > lnum)
8435 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008437 else
8438 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439}
8440
8441/*
8442 * "argc()" function
8443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008445f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008446 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008449 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450}
8451
8452/*
8453 * "argidx()" function
8454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008456f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008457 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008460 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461}
8462
8463/*
8464 * "argv(nr)" function
8465 */
8466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008468 typval_T *argvars;
8469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 int idx;
8472
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008473 if (argvars[0].v_type != VAR_UNKNOWN)
8474 {
8475 idx = get_tv_number_chk(&argvars[0], NULL);
8476 if (idx >= 0 && idx < ARGCOUNT)
8477 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8478 else
8479 rettv->vval.v_string = NULL;
8480 rettv->v_type = VAR_STRING;
8481 }
8482 else if (rettv_list_alloc(rettv) == OK)
8483 for (idx = 0; idx < ARGCOUNT; ++idx)
8484 list_append_string(rettv->vval.v_list,
8485 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486}
8487
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008488#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008489/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008490 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008491 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008492 static void
8493f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008494 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008495 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008496{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008497 float_T f;
8498
8499 rettv->v_type = VAR_FLOAT;
8500 if (get_float_arg(argvars, &f) == OK)
8501 rettv->vval.v_float = asin(f);
8502 else
8503 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008504}
8505
8506/*
8507 * "atan()" function
8508 */
8509 static void
8510f_atan(argvars, rettv)
8511 typval_T *argvars;
8512 typval_T *rettv;
8513{
8514 float_T f;
8515
8516 rettv->v_type = VAR_FLOAT;
8517 if (get_float_arg(argvars, &f) == OK)
8518 rettv->vval.v_float = atan(f);
8519 else
8520 rettv->vval.v_float = 0.0;
8521}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008522
8523/*
8524 * "atan2()" function
8525 */
8526 static void
8527f_atan2(argvars, rettv)
8528 typval_T *argvars;
8529 typval_T *rettv;
8530{
8531 float_T fx, fy;
8532
8533 rettv->v_type = VAR_FLOAT;
8534 if (get_float_arg(argvars, &fx) == OK
8535 && get_float_arg(&argvars[1], &fy) == OK)
8536 rettv->vval.v_float = atan2(fx, fy);
8537 else
8538 rettv->vval.v_float = 0.0;
8539}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008540#endif
8541
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542/*
8543 * "browse(save, title, initdir, default)" function
8544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008547 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549{
8550#ifdef FEAT_BROWSE
8551 int save;
8552 char_u *title;
8553 char_u *initdir;
8554 char_u *defname;
8555 char_u buf[NUMBUFLEN];
8556 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008557 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008559 save = get_tv_number_chk(&argvars[0], &error);
8560 title = get_tv_string_chk(&argvars[1]);
8561 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8562 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008564 if (error || title == NULL || initdir == NULL || defname == NULL)
8565 rettv->vval.v_string = NULL;
8566 else
8567 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008568 do_browse(save ? BROWSE_SAVE : 0,
8569 title, defname, NULL, initdir, NULL, curbuf);
8570#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008572#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008574}
8575
8576/*
8577 * "browsedir(title, initdir)" function
8578 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008581 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008583{
8584#ifdef FEAT_BROWSE
8585 char_u *title;
8586 char_u *initdir;
8587 char_u buf[NUMBUFLEN];
8588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008589 title = get_tv_string_chk(&argvars[0]);
8590 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008592 if (title == NULL || initdir == NULL)
8593 rettv->vval.v_string = NULL;
8594 else
8595 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008596 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601}
8602
Bram Moolenaar33570922005-01-25 22:26:29 +00008603static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008604
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605/*
8606 * Find a buffer by number or exact name.
8607 */
8608 static buf_T *
8609find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
8612 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008614 if (avar->v_type == VAR_NUMBER)
8615 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008616 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008618 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008619 if (buf == NULL)
8620 {
8621 /* No full path name match, try a match with a URL or a "nofile"
8622 * buffer, these don't use the full path. */
8623 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8624 if (buf->b_fname != NULL
8625 && (path_with_url(buf->b_fname)
8626#ifdef FEAT_QUICKFIX
8627 || bt_nofile(buf)
8628#endif
8629 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008630 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008631 break;
8632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 }
8634 return buf;
8635}
8636
8637/*
8638 * "bufexists(expr)" function
8639 */
8640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008642 typval_T *argvars;
8643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646}
8647
8648/*
8649 * "buflisted(expr)" function
8650 */
8651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008652f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 typval_T *argvars;
8654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 buf_T *buf;
8657
8658 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008659 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660}
8661
8662/*
8663 * "bufloaded(expr)" function
8664 */
8665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008666f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008667 typval_T *argvars;
8668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669{
8670 buf_T *buf;
8671
8672 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674}
8675
Bram Moolenaar33570922005-01-25 22:26:29 +00008676static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008677
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678/*
8679 * Get buffer by number or pattern.
8680 */
8681 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 int save_magic;
8687 char_u *save_cpo;
8688 buf_T *buf;
8689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008690 if (tv->v_type == VAR_NUMBER)
8691 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008692 if (tv->v_type != VAR_STRING)
8693 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (name == NULL || *name == NUL)
8695 return curbuf;
8696 if (name[0] == '$' && name[1] == NUL)
8697 return lastbuf;
8698
8699 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8700 save_magic = p_magic;
8701 p_magic = TRUE;
8702 save_cpo = p_cpo;
8703 p_cpo = (char_u *)"";
8704
8705 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8706 TRUE, FALSE));
8707
8708 p_magic = save_magic;
8709 p_cpo = save_cpo;
8710
8711 /* If not found, try expanding the name, like done for bufexists(). */
8712 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
8715 return buf;
8716}
8717
8718/*
8719 * "bufname(expr)" function
8720 */
8721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
8726 buf_T *buf;
8727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 buf = get_buf_tv(&argvars[0]);
8731 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008733 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 --emsg_off;
8737}
8738
8739/*
8740 * "bufnr(expr)" function
8741 */
8742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008744 typval_T *argvars;
8745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
8747 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008748 int error = FALSE;
8749 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008751 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008754 --emsg_off;
8755
8756 /* If the buffer isn't found and the second argument is not zero create a
8757 * new buffer. */
8758 if (buf == NULL
8759 && argvars[1].v_type != VAR_UNKNOWN
8760 && get_tv_number_chk(&argvars[1], &error) != 0
8761 && !error
8762 && (name = get_tv_string_chk(&argvars[0])) != NULL
8763 && !error)
8764 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8765
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770}
8771
8772/*
8773 * "bufwinnr(nr)" function
8774 */
8775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 typval_T *argvars;
8778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
8780#ifdef FEAT_WINDOWS
8781 win_T *wp;
8782 int winnr = 0;
8783#endif
8784 buf_T *buf;
8785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008786 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789#ifdef FEAT_WINDOWS
8790 for (wp = firstwin; wp; wp = wp->w_next)
8791 {
8792 ++winnr;
8793 if (wp->w_buffer == buf)
8794 break;
8795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799#endif
8800 --emsg_off;
8801}
8802
8803/*
8804 * "byte2line(byte)" function
8805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008808 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810{
8811#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813#else
8814 long boff = 0;
8815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008816 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008818 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 (linenr_T)0, &boff);
8822#endif
8823}
8824
8825/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008826 * "byteidx()" function
8827 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008830 typval_T *argvars;
8831 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008832{
8833#ifdef FEAT_MBYTE
8834 char_u *t;
8835#endif
8836 char_u *str;
8837 long idx;
8838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 str = get_tv_string_chk(&argvars[0]);
8840 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008842 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008843 return;
8844
8845#ifdef FEAT_MBYTE
8846 t = str;
8847 for ( ; idx > 0; idx--)
8848 {
8849 if (*t == NUL) /* EOL reached */
8850 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008851 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008852 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008853 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008854#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008855 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008857#endif
8858}
8859
8860/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008861 * "call(func, arglist)" function
8862 */
8863 static void
8864f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008865 typval_T *argvars;
8866 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008867{
8868 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008869 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008870 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008871 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008872 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008873 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008874
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008875 if (argvars[1].v_type != VAR_LIST)
8876 {
8877 EMSG(_(e_listreq));
8878 return;
8879 }
8880 if (argvars[1].vval.v_list == NULL)
8881 return;
8882
8883 if (argvars[0].v_type == VAR_FUNC)
8884 func = argvars[0].vval.v_string;
8885 else
8886 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 if (*func == NUL)
8888 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008889
Bram Moolenaare9a41262005-01-15 22:18:47 +00008890 if (argvars[2].v_type != VAR_UNKNOWN)
8891 {
8892 if (argvars[2].v_type != VAR_DICT)
8893 {
8894 EMSG(_(e_dictreq));
8895 return;
8896 }
8897 selfdict = argvars[2].vval.v_dict;
8898 }
8899
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008900 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8901 item = item->li_next)
8902 {
8903 if (argc == MAX_FUNC_ARGS)
8904 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008905 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008906 break;
8907 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008908 /* Make a copy of each argument. This is needed to be able to set
8909 * v_lock to VAR_FIXED in the copy without changing the original list.
8910 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008911 copy_tv(&item->li_tv, &argv[argc++]);
8912 }
8913
8914 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008915 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008916 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8917 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008918
8919 /* Free the arguments. */
8920 while (argc > 0)
8921 clear_tv(&argv[--argc]);
8922}
8923
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924#ifdef FEAT_FLOAT
8925/*
8926 * "ceil({float})" function
8927 */
8928 static void
8929f_ceil(argvars, rettv)
8930 typval_T *argvars;
8931 typval_T *rettv;
8932{
8933 float_T f;
8934
8935 rettv->v_type = VAR_FLOAT;
8936 if (get_float_arg(argvars, &f) == OK)
8937 rettv->vval.v_float = ceil(f);
8938 else
8939 rettv->vval.v_float = 0.0;
8940}
8941#endif
8942
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008943/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008944 * "changenr()" function
8945 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008946 static void
8947f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008948 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008949 typval_T *rettv;
8950{
8951 rettv->vval.v_number = curbuf->b_u_seq_cur;
8952}
8953
8954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 * "char2nr(string)" function
8956 */
8957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 typval_T *argvars;
8960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962#ifdef FEAT_MBYTE
8963 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 else
8966#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968}
8969
8970/*
8971 * "cindent(lnum)" function
8972 */
8973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008975 typval_T *argvars;
8976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978#ifdef FEAT_CINDENT
8979 pos_T pos;
8980 linenr_T lnum;
8981
8982 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8985 {
8986 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 curwin->w_cursor = pos;
8989 }
8990 else
8991#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008992 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993}
8994
8995/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008996 * "clearmatches()" function
8997 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008998 static void
8999f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009000 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009001 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009002{
9003#ifdef FEAT_SEARCH_EXTRA
9004 clear_matches(curwin);
9005#endif
9006}
9007
9008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 * "col(string)" function
9010 */
9011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 typval_T *argvars;
9014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015{
9016 colnr_T col = 0;
9017 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009018 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009020 fp = var2fpos(&argvars[0], FALSE, &fnum);
9021 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 {
9023 if (fp->col == MAXCOL)
9024 {
9025 /* '> can be MAXCOL, get the length of the line then */
9026 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009027 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 else
9029 col = MAXCOL;
9030 }
9031 else
9032 {
9033 col = fp->col + 1;
9034#ifdef FEAT_VIRTUALEDIT
9035 /* col(".") when the cursor is on the NUL at the end of the line
9036 * because of "coladd" can be seen as an extra column. */
9037 if (virtual_active() && fp == &curwin->w_cursor)
9038 {
9039 char_u *p = ml_get_cursor();
9040
9041 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9042 curwin->w_virtcol - curwin->w_cursor.coladd))
9043 {
9044# ifdef FEAT_MBYTE
9045 int l;
9046
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009047 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 col += l;
9049# else
9050 if (*p != NUL && p[1] == NUL)
9051 ++col;
9052# endif
9053 }
9054 }
9055#endif
9056 }
9057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059}
9060
Bram Moolenaar572cb562005-08-05 21:35:02 +00009061#if defined(FEAT_INS_EXPAND)
9062/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009063 * "complete()" function
9064 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009065 static void
9066f_complete(argvars, rettv)
9067 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009068 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009069{
9070 int startcol;
9071
9072 if ((State & INSERT) == 0)
9073 {
9074 EMSG(_("E785: complete() can only be used in Insert mode"));
9075 return;
9076 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009077
9078 /* Check for undo allowed here, because if something was already inserted
9079 * the line was already saved for undo and this check isn't done. */
9080 if (!undo_allowed())
9081 return;
9082
Bram Moolenaarade00832006-03-10 21:46:58 +00009083 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9084 {
9085 EMSG(_(e_invarg));
9086 return;
9087 }
9088
9089 startcol = get_tv_number_chk(&argvars[0], NULL);
9090 if (startcol <= 0)
9091 return;
9092
9093 set_completion(startcol - 1, argvars[1].vval.v_list);
9094}
9095
9096/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009097 * "complete_add()" function
9098 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009099 static void
9100f_complete_add(argvars, rettv)
9101 typval_T *argvars;
9102 typval_T *rettv;
9103{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009104 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009105}
9106
9107/*
9108 * "complete_check()" function
9109 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009110 static void
9111f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009112 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009113 typval_T *rettv;
9114{
9115 int saved = RedrawingDisabled;
9116
9117 RedrawingDisabled = 0;
9118 ins_compl_check_keys(0);
9119 rettv->vval.v_number = compl_interrupted;
9120 RedrawingDisabled = saved;
9121}
9122#endif
9123
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124/*
9125 * "confirm(message, buttons[, default [, type]])" function
9126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009128f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009129 typval_T *argvars UNUSED;
9130 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
9132#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9133 char_u *message;
9134 char_u *buttons = NULL;
9135 char_u buf[NUMBUFLEN];
9136 char_u buf2[NUMBUFLEN];
9137 int def = 1;
9138 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009139 char_u *typestr;
9140 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 message = get_tv_string_chk(&argvars[0]);
9143 if (message == NULL)
9144 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009145 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009147 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9148 if (buttons == NULL)
9149 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009150 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009152 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009153 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009155 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9156 if (typestr == NULL)
9157 error = TRUE;
9158 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009160 switch (TOUPPER_ASC(*typestr))
9161 {
9162 case 'E': type = VIM_ERROR; break;
9163 case 'Q': type = VIM_QUESTION; break;
9164 case 'I': type = VIM_INFO; break;
9165 case 'W': type = VIM_WARNING; break;
9166 case 'G': type = VIM_GENERIC; break;
9167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 }
9169 }
9170 }
9171 }
9172
9173 if (buttons == NULL || *buttons == NUL)
9174 buttons = (char_u *)_("&Ok");
9175
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009176 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009177 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179#endif
9180}
9181
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009182/*
9183 * "copy()" function
9184 */
9185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009187 typval_T *argvars;
9188 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009189{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009190 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009193#ifdef FEAT_FLOAT
9194/*
9195 * "cos()" function
9196 */
9197 static void
9198f_cos(argvars, rettv)
9199 typval_T *argvars;
9200 typval_T *rettv;
9201{
9202 float_T f;
9203
9204 rettv->v_type = VAR_FLOAT;
9205 if (get_float_arg(argvars, &f) == OK)
9206 rettv->vval.v_float = cos(f);
9207 else
9208 rettv->vval.v_float = 0.0;
9209}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009210
9211/*
9212 * "cosh()" function
9213 */
9214 static void
9215f_cosh(argvars, rettv)
9216 typval_T *argvars;
9217 typval_T *rettv;
9218{
9219 float_T f;
9220
9221 rettv->v_type = VAR_FLOAT;
9222 if (get_float_arg(argvars, &f) == OK)
9223 rettv->vval.v_float = cosh(f);
9224 else
9225 rettv->vval.v_float = 0.0;
9226}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009227#endif
9228
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009230 * "count()" function
9231 */
9232 static void
9233f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009234 typval_T *argvars;
9235 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009236{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009237 long n = 0;
9238 int ic = FALSE;
9239
Bram Moolenaare9a41262005-01-15 22:18:47 +00009240 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009241 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009242 listitem_T *li;
9243 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009244 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009245
Bram Moolenaare9a41262005-01-15 22:18:47 +00009246 if ((l = argvars[0].vval.v_list) != NULL)
9247 {
9248 li = l->lv_first;
9249 if (argvars[2].v_type != VAR_UNKNOWN)
9250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009251 int error = FALSE;
9252
9253 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009254 if (argvars[3].v_type != VAR_UNKNOWN)
9255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 idx = get_tv_number_chk(&argvars[3], &error);
9257 if (!error)
9258 {
9259 li = list_find(l, idx);
9260 if (li == NULL)
9261 EMSGN(_(e_listidx), idx);
9262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009263 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 if (error)
9265 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009266 }
9267
9268 for ( ; li != NULL; li = li->li_next)
9269 if (tv_equal(&li->li_tv, &argvars[1], ic))
9270 ++n;
9271 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009272 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009273 else if (argvars[0].v_type == VAR_DICT)
9274 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009275 int todo;
9276 dict_T *d;
9277 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009278
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009279 if ((d = argvars[0].vval.v_dict) != NULL)
9280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009281 int error = FALSE;
9282
Bram Moolenaare9a41262005-01-15 22:18:47 +00009283 if (argvars[2].v_type != VAR_UNKNOWN)
9284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009285 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009286 if (argvars[3].v_type != VAR_UNKNOWN)
9287 EMSG(_(e_invarg));
9288 }
9289
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009290 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009291 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009292 {
9293 if (!HASHITEM_EMPTY(hi))
9294 {
9295 --todo;
9296 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9297 ++n;
9298 }
9299 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009300 }
9301 }
9302 else
9303 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009304 rettv->vval.v_number = n;
9305}
9306
9307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9309 *
9310 * Checks the existence of a cscope connection.
9311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009313f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009314 typval_T *argvars UNUSED;
9315 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316{
9317#ifdef FEAT_CSCOPE
9318 int num = 0;
9319 char_u *dbpath = NULL;
9320 char_u *prepend = NULL;
9321 char_u buf[NUMBUFLEN];
9322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009323 if (argvars[0].v_type != VAR_UNKNOWN
9324 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 num = (int)get_tv_number(&argvars[0]);
9327 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009328 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009329 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 }
9331
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333#endif
9334}
9335
9336/*
9337 * "cursor(lnum, col)" function
9338 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009339 * Moves the cursor to the specified line and column.
9340 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009344 typval_T *argvars;
9345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009348#ifdef FEAT_VIRTUALEDIT
9349 long coladd = 0;
9350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009352 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009353 if (argvars[1].v_type == VAR_UNKNOWN)
9354 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009355 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009356
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009357 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009358 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009359 line = pos.lnum;
9360 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009361#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009362 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009363#endif
9364 }
9365 else
9366 {
9367 line = get_tv_lnum(argvars);
9368 col = get_tv_number_chk(&argvars[1], NULL);
9369#ifdef FEAT_VIRTUALEDIT
9370 if (argvars[2].v_type != VAR_UNKNOWN)
9371 coladd = get_tv_number_chk(&argvars[2], NULL);
9372#endif
9373 }
9374 if (line < 0 || col < 0
9375#ifdef FEAT_VIRTUALEDIT
9376 || coladd < 0
9377#endif
9378 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 if (line > 0)
9381 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 if (col > 0)
9383 curwin->w_cursor.col = col - 1;
9384#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009385 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386#endif
9387
9388 /* Make sure the cursor is in a valid position. */
9389 check_cursor();
9390#ifdef FEAT_MBYTE
9391 /* Correct cursor for multi-byte character. */
9392 if (has_mbyte)
9393 mb_adjust_cursor();
9394#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009395
9396 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009397 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398}
9399
9400/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009401 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 */
9403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009405 typval_T *argvars;
9406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009408 int noref = 0;
9409
9410 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009412 if (noref < 0 || noref > 1)
9413 EMSG(_(e_invarg));
9414 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009415 {
9416 current_copyID += COPYID_INC;
9417 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419}
9420
9421/*
9422 * "delete()" function
9423 */
9424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009426 typval_T *argvars;
9427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428{
9429 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433}
9434
9435/*
9436 * "did_filetype()" function
9437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009440 typval_T *argvars UNUSED;
9441 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
9443#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445#endif
9446}
9447
9448/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009449 * "diff_filler()" function
9450 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009453 typval_T *argvars UNUSED;
9454 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009455{
9456#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009458#endif
9459}
9460
9461/*
9462 * "diff_hlID()" function
9463 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009466 typval_T *argvars UNUSED;
9467 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009468{
9469#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009471 static linenr_T prev_lnum = 0;
9472 static int changedtick = 0;
9473 static int fnum = 0;
9474 static int change_start = 0;
9475 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009476 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009477 int filler_lines;
9478 int col;
9479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 if (lnum < 0) /* ignore type error in {lnum} arg */
9481 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009482 if (lnum != prev_lnum
9483 || changedtick != curbuf->b_changedtick
9484 || fnum != curbuf->b_fnum)
9485 {
9486 /* New line, buffer, change: need to get the values. */
9487 filler_lines = diff_check(curwin, lnum);
9488 if (filler_lines < 0)
9489 {
9490 if (filler_lines == -1)
9491 {
9492 change_start = MAXCOL;
9493 change_end = -1;
9494 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9495 hlID = HLF_ADD; /* added line */
9496 else
9497 hlID = HLF_CHD; /* changed line */
9498 }
9499 else
9500 hlID = HLF_ADD; /* added line */
9501 }
9502 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009503 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009504 prev_lnum = lnum;
9505 changedtick = curbuf->b_changedtick;
9506 fnum = curbuf->b_fnum;
9507 }
9508
9509 if (hlID == HLF_CHD || hlID == HLF_TXD)
9510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009511 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009512 if (col >= change_start && col <= change_end)
9513 hlID = HLF_TXD; /* changed text */
9514 else
9515 hlID = HLF_CHD; /* changed line */
9516 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009517 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009518#endif
9519}
9520
9521/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009522 * "empty({expr})" function
9523 */
9524 static void
9525f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009528{
9529 int n;
9530
9531 switch (argvars[0].v_type)
9532 {
9533 case VAR_STRING:
9534 case VAR_FUNC:
9535 n = argvars[0].vval.v_string == NULL
9536 || *argvars[0].vval.v_string == NUL;
9537 break;
9538 case VAR_NUMBER:
9539 n = argvars[0].vval.v_number == 0;
9540 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009541#ifdef FEAT_FLOAT
9542 case VAR_FLOAT:
9543 n = argvars[0].vval.v_float == 0.0;
9544 break;
9545#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009546 case VAR_LIST:
9547 n = argvars[0].vval.v_list == NULL
9548 || argvars[0].vval.v_list->lv_first == NULL;
9549 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009550 case VAR_DICT:
9551 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009552 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009553 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009554 default:
9555 EMSG2(_(e_intern2), "f_empty()");
9556 n = 0;
9557 }
9558
9559 rettv->vval.v_number = n;
9560}
9561
9562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 * "escape({string}, {chars})" function
9564 */
9565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009567 typval_T *argvars;
9568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
9570 char_u buf[NUMBUFLEN];
9571
Bram Moolenaar758711c2005-02-02 23:11:38 +00009572 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9573 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575}
9576
9577/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009578 * "eval()" function
9579 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009580 static void
9581f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009582 typval_T *argvars;
9583 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009584{
9585 char_u *s;
9586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 s = get_tv_string_chk(&argvars[0]);
9588 if (s != NULL)
9589 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009591 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9592 {
9593 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009594 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009595 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009596 else if (*s != NUL)
9597 EMSG(_(e_trailing));
9598}
9599
9600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 * "eventhandler()" function
9602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009605 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609}
9610
9611/*
9612 * "executable()" function
9613 */
9614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009616 typval_T *argvars;
9617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620}
9621
9622/*
9623 * "exists()" function
9624 */
9625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 typval_T *argvars;
9628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629{
9630 char_u *p;
9631 char_u *name;
9632 int n = FALSE;
9633 int len = 0;
9634
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 if (*p == '$') /* environment variable */
9637 {
9638 /* first try "normal" environment variables (fast) */
9639 if (mch_getenv(p + 1) != NULL)
9640 n = TRUE;
9641 else
9642 {
9643 /* try expanding things like $VIM and ${HOME} */
9644 p = expand_env_save(p);
9645 if (p != NULL && *p != '$')
9646 n = TRUE;
9647 vim_free(p);
9648 }
9649 }
9650 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009653 if (*skipwhite(p) != NUL)
9654 n = FALSE; /* trailing garbage */
9655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 else if (*p == '*') /* internal or user defined function */
9657 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009658 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 }
9660 else if (*p == ':')
9661 {
9662 n = cmd_exists(p + 1);
9663 }
9664 else if (*p == '#')
9665 {
9666#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009667 if (p[1] == '#')
9668 n = autocmd_supported(p + 2);
9669 else
9670 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671#endif
9672 }
9673 else /* internal variable */
9674 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009675 char_u *tofree;
9676 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009678 /* get_name_len() takes care of expanding curly braces */
9679 name = p;
9680 len = get_name_len(&p, &tofree, TRUE, FALSE);
9681 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009683 if (tofree != NULL)
9684 name = tofree;
9685 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9686 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009688 /* handle d.key, l[idx], f(expr) */
9689 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9690 if (n)
9691 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 }
9693 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009694 if (*p != NUL)
9695 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009697 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 }
9699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
9702
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009703#ifdef FEAT_FLOAT
9704/*
9705 * "exp()" function
9706 */
9707 static void
9708f_exp(argvars, rettv)
9709 typval_T *argvars;
9710 typval_T *rettv;
9711{
9712 float_T f;
9713
9714 rettv->v_type = VAR_FLOAT;
9715 if (get_float_arg(argvars, &f) == OK)
9716 rettv->vval.v_float = exp(f);
9717 else
9718 rettv->vval.v_float = 0.0;
9719}
9720#endif
9721
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722/*
9723 * "expand()" function
9724 */
9725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009727 typval_T *argvars;
9728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729{
9730 char_u *s;
9731 int len;
9732 char_u *errormsg;
9733 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9734 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009735 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737 rettv->v_type = VAR_STRING;
9738 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 if (*s == '%' || *s == '#' || *s == '<')
9740 {
9741 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009742 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 --emsg_off;
9744 }
9745 else
9746 {
9747 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009748 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009749 if (argvars[1].v_type != VAR_UNKNOWN
9750 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009752 if (!error)
9753 {
9754 ExpandInit(&xpc);
9755 xpc.xp_context = EXPAND_FILES;
9756 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009757 }
9758 else
9759 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 }
9761}
9762
9763/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009764 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009765 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009766 */
9767 static void
9768f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009769 typval_T *argvars;
9770 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009771{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009772 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009773 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009774 list_T *l1, *l2;
9775 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009776 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009778
Bram Moolenaare9a41262005-01-15 22:18:47 +00009779 l1 = argvars[0].vval.v_list;
9780 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009781 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9782 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009783 {
9784 if (argvars[2].v_type != VAR_UNKNOWN)
9785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009786 before = get_tv_number_chk(&argvars[2], &error);
9787 if (error)
9788 return; /* type error; errmsg already given */
9789
Bram Moolenaar758711c2005-02-02 23:11:38 +00009790 if (before == l1->lv_len)
9791 item = NULL;
9792 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009793 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009794 item = list_find(l1, before);
9795 if (item == NULL)
9796 {
9797 EMSGN(_(e_listidx), before);
9798 return;
9799 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009800 }
9801 }
9802 else
9803 item = NULL;
9804 list_extend(l1, l2, item);
9805
Bram Moolenaare9a41262005-01-15 22:18:47 +00009806 copy_tv(&argvars[0], rettv);
9807 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009809 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9810 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009811 dict_T *d1, *d2;
9812 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009813 char_u *action;
9814 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009815 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009816 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009817
9818 d1 = argvars[0].vval.v_dict;
9819 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009820 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9821 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009822 {
9823 /* Check the third argument. */
9824 if (argvars[2].v_type != VAR_UNKNOWN)
9825 {
9826 static char *(av[]) = {"keep", "force", "error"};
9827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009828 action = get_tv_string_chk(&argvars[2]);
9829 if (action == NULL)
9830 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009831 for (i = 0; i < 3; ++i)
9832 if (STRCMP(action, av[i]) == 0)
9833 break;
9834 if (i == 3)
9835 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009836 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009837 return;
9838 }
9839 }
9840 else
9841 action = (char_u *)"force";
9842
9843 /* Go over all entries in the second dict and add them to the
9844 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009845 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009846 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009847 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009848 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009849 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009850 --todo;
9851 di1 = dict_find(d1, hi2->hi_key, -1);
9852 if (di1 == NULL)
9853 {
9854 di1 = dictitem_copy(HI2DI(hi2));
9855 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9856 dictitem_free(di1);
9857 }
9858 else if (*action == 'e')
9859 {
9860 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9861 break;
9862 }
9863 else if (*action == 'f')
9864 {
9865 clear_tv(&di1->di_tv);
9866 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009868 }
9869 }
9870
Bram Moolenaare9a41262005-01-15 22:18:47 +00009871 copy_tv(&argvars[0], rettv);
9872 }
9873 }
9874 else
9875 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009876}
9877
9878/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009879 * "feedkeys()" function
9880 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009881 static void
9882f_feedkeys(argvars, rettv)
9883 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009884 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009885{
9886 int remap = TRUE;
9887 char_u *keys, *flags;
9888 char_u nbuf[NUMBUFLEN];
9889 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009890 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009891
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009892 /* This is not allowed in the sandbox. If the commands would still be
9893 * executed in the sandbox it would be OK, but it probably happens later,
9894 * when "sandbox" is no longer set. */
9895 if (check_secure())
9896 return;
9897
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009898 keys = get_tv_string(&argvars[0]);
9899 if (*keys != NUL)
9900 {
9901 if (argvars[1].v_type != VAR_UNKNOWN)
9902 {
9903 flags = get_tv_string_buf(&argvars[1], nbuf);
9904 for ( ; *flags != NUL; ++flags)
9905 {
9906 switch (*flags)
9907 {
9908 case 'n': remap = FALSE; break;
9909 case 'm': remap = TRUE; break;
9910 case 't': typed = TRUE; break;
9911 }
9912 }
9913 }
9914
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009915 /* Need to escape K_SPECIAL and CSI before putting the string in the
9916 * typeahead buffer. */
9917 keys_esc = vim_strsave_escape_csi(keys);
9918 if (keys_esc != NULL)
9919 {
9920 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009921 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009922 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009923 if (vgetc_busy)
9924 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009925 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009926 }
9927}
9928
9929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 * "filereadable()" function
9931 */
9932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009934 typval_T *argvars;
9935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009937 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 char_u *p;
9939 int n;
9940
Bram Moolenaarc236c162008-07-13 17:41:49 +00009941#ifndef O_NONBLOCK
9942# define O_NONBLOCK 0
9943#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009945 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9946 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 {
9948 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009949 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 }
9951 else
9952 n = FALSE;
9953
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009954 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955}
9956
9957/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009958 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 * rights to write into.
9960 */
9961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009962f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 typval_T *argvars;
9964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009966 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967}
9968
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009969static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009970
9971 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009972findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009973 typval_T *argvars;
9974 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009975 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009976{
9977#ifdef FEAT_SEARCHPATH
9978 char_u *fname;
9979 char_u *fresult = NULL;
9980 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9981 char_u *p;
9982 char_u pathbuf[NUMBUFLEN];
9983 int count = 1;
9984 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009985 int error = FALSE;
9986#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009987
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009988 rettv->vval.v_string = NULL;
9989 rettv->v_type = VAR_STRING;
9990
9991#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009993
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009994 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009996 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9997 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009998 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009999 else
10000 {
10001 if (*p != NUL)
10002 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010004 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010005 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010006 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010007 }
10008
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010009 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10010 error = TRUE;
10011
10012 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010014 do
10015 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010016 if (rettv->v_type == VAR_STRING)
10017 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 fresult = find_file_in_path_option(first ? fname : NULL,
10019 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010020 0, first, path,
10021 find_what,
10022 curbuf->b_ffname,
10023 find_what == FINDFILE_DIR
10024 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010026
10027 if (fresult != NULL && rettv->v_type == VAR_LIST)
10028 list_append_string(rettv->vval.v_list, fresult, -1);
10029
10030 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010031 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010032
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010033 if (rettv->v_type == VAR_STRING)
10034 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010035#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010036}
10037
Bram Moolenaar33570922005-01-25 22:26:29 +000010038static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10039static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010040
10041/*
10042 * Implementation of map() and filter().
10043 */
10044 static void
10045filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010046 typval_T *argvars;
10047 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010048 int map;
10049{
10050 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010051 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010052 listitem_T *li, *nli;
10053 list_T *l = NULL;
10054 dictitem_T *di;
10055 hashtab_T *ht;
10056 hashitem_T *hi;
10057 dict_T *d = NULL;
10058 typval_T save_val;
10059 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010060 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010061 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010062 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010063 int save_did_emsg;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010064 int index = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010065
Bram Moolenaare9a41262005-01-15 22:18:47 +000010066 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010067 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010068 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010069 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010070 return;
10071 }
10072 else if (argvars[0].v_type == VAR_DICT)
10073 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010074 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010075 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010076 return;
10077 }
10078 else
10079 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010080 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010081 return;
10082 }
10083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 expr = get_tv_string_buf_chk(&argvars[1], buf);
10085 /* On type errors, the preceding call has already displayed an error
10086 * message. Avoid a misleading error message for an empty string that
10087 * was not passed as argument. */
10088 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 prepare_vimvar(VV_VAL, &save_val);
10091 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010092
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010093 /* We reset "did_emsg" to be able to detect whether an error
10094 * occurred during evaluation of the expression. */
10095 save_did_emsg = did_emsg;
10096 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010097
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010098 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010099 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 vimvars[VV_KEY].vv_type = VAR_STRING;
10102
10103 ht = &d->dv_hashtab;
10104 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010105 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010108 if (!HASHITEM_EMPTY(hi))
10109 {
10110 --todo;
10111 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010112 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 break;
10114 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010115 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010116 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117 break;
10118 if (!map && rem)
10119 dictitem_remove(d, di);
10120 clear_tv(&vimvars[VV_KEY].vv_tv);
10121 }
10122 }
10123 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 }
10125 else
10126 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010127 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010129 for (li = l->lv_first; li != NULL; li = nli)
10130 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010131 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010132 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 nli = li->li_next;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010134 vimvars[VV_KEY].vv_nr = index;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010135 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010136 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010137 break;
10138 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010139 listitem_remove(l, li);
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010140 ++index;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010141 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010142 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010143
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010144 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010146
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010147 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010148 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010149
10150 copy_tv(&argvars[0], rettv);
10151}
10152
10153 static int
10154filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010155 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010156 char_u *expr;
10157 int map;
10158 int *remp;
10159{
Bram Moolenaar33570922005-01-25 22:26:29 +000010160 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010161 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010162 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010163
Bram Moolenaar33570922005-01-25 22:26:29 +000010164 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010165 s = expr;
10166 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010167 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010168 if (*s != NUL) /* check for trailing chars after expr */
10169 {
10170 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010171 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010172 }
10173 if (map)
10174 {
10175 /* map(): replace the list item value */
10176 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010177 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 *tv = rettv;
10179 }
10180 else
10181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 int error = FALSE;
10183
Bram Moolenaare9a41262005-01-15 22:18:47 +000010184 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010186 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 /* On type error, nothing has been removed; return FAIL to stop the
10188 * loop. The error message was given by get_tv_number_chk(). */
10189 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010190 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010191 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010192 retval = OK;
10193theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010194 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010195 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010196}
10197
10198/*
10199 * "filter()" function
10200 */
10201 static void
10202f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010203 typval_T *argvars;
10204 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010205{
10206 filter_map(argvars, rettv, FALSE);
10207}
10208
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010209/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010210 * "finddir({fname}[, {path}[, {count}]])" function
10211 */
10212 static void
10213f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010214 typval_T *argvars;
10215 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010216{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010217 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010218}
10219
10220/*
10221 * "findfile({fname}[, {path}[, {count}]])" function
10222 */
10223 static void
10224f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010225 typval_T *argvars;
10226 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010227{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010228 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010229}
10230
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010231#ifdef FEAT_FLOAT
10232/*
10233 * "float2nr({float})" function
10234 */
10235 static void
10236f_float2nr(argvars, rettv)
10237 typval_T *argvars;
10238 typval_T *rettv;
10239{
10240 float_T f;
10241
10242 if (get_float_arg(argvars, &f) == OK)
10243 {
10244 if (f < -0x7fffffff)
10245 rettv->vval.v_number = -0x7fffffff;
10246 else if (f > 0x7fffffff)
10247 rettv->vval.v_number = 0x7fffffff;
10248 else
10249 rettv->vval.v_number = (varnumber_T)f;
10250 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010251}
10252
10253/*
10254 * "floor({float})" function
10255 */
10256 static void
10257f_floor(argvars, rettv)
10258 typval_T *argvars;
10259 typval_T *rettv;
10260{
10261 float_T f;
10262
10263 rettv->v_type = VAR_FLOAT;
10264 if (get_float_arg(argvars, &f) == OK)
10265 rettv->vval.v_float = floor(f);
10266 else
10267 rettv->vval.v_float = 0.0;
10268}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010269
10270/*
10271 * "fmod()" function
10272 */
10273 static void
10274f_fmod(argvars, rettv)
10275 typval_T *argvars;
10276 typval_T *rettv;
10277{
10278 float_T fx, fy;
10279
10280 rettv->v_type = VAR_FLOAT;
10281 if (get_float_arg(argvars, &fx) == OK
10282 && get_float_arg(&argvars[1], &fy) == OK)
10283 rettv->vval.v_float = fmod(fx, fy);
10284 else
10285 rettv->vval.v_float = 0.0;
10286}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010287#endif
10288
Bram Moolenaar0d660222005-01-07 21:51:51 +000010289/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010290 * "fnameescape({string})" function
10291 */
10292 static void
10293f_fnameescape(argvars, rettv)
10294 typval_T *argvars;
10295 typval_T *rettv;
10296{
10297 rettv->vval.v_string = vim_strsave_fnameescape(
10298 get_tv_string(&argvars[0]), FALSE);
10299 rettv->v_type = VAR_STRING;
10300}
10301
10302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 * "fnamemodify({fname}, {mods})" function
10304 */
10305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010306f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010307 typval_T *argvars;
10308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309{
10310 char_u *fname;
10311 char_u *mods;
10312 int usedlen = 0;
10313 int len;
10314 char_u *fbuf = NULL;
10315 char_u buf[NUMBUFLEN];
10316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010317 fname = get_tv_string_chk(&argvars[0]);
10318 mods = get_tv_string_buf_chk(&argvars[1], buf);
10319 if (fname == NULL || mods == NULL)
10320 fname = NULL;
10321 else
10322 {
10323 len = (int)STRLEN(fname);
10324 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010327 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 vim_free(fbuf);
10333}
10334
Bram Moolenaar33570922005-01-25 22:26:29 +000010335static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336
10337/*
10338 * "foldclosed()" function
10339 */
10340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010342 typval_T *argvars;
10343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 int end;
10345{
10346#ifdef FEAT_FOLDING
10347 linenr_T lnum;
10348 linenr_T first, last;
10349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10352 {
10353 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10354 {
10355 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010356 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010358 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 return;
10360 }
10361 }
10362#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010363 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364}
10365
10366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010367 * "foldclosed()" function
10368 */
10369 static void
10370f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010371 typval_T *argvars;
10372 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010373{
10374 foldclosed_both(argvars, rettv, FALSE);
10375}
10376
10377/*
10378 * "foldclosedend()" function
10379 */
10380 static void
10381f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010382 typval_T *argvars;
10383 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010384{
10385 foldclosed_both(argvars, rettv, TRUE);
10386}
10387
10388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 * "foldlevel()" function
10390 */
10391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010392f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010393 typval_T *argvars;
10394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395{
10396#ifdef FEAT_FOLDING
10397 linenr_T lnum;
10398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010399 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403}
10404
10405/*
10406 * "foldtext()" function
10407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010409f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010410 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412{
10413#ifdef FEAT_FOLDING
10414 linenr_T lnum;
10415 char_u *s;
10416 char_u *r;
10417 int len;
10418 char *txt;
10419#endif
10420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010421 rettv->v_type = VAR_STRING;
10422 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10425 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10426 <= curbuf->b_ml.ml_line_count
10427 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 {
10429 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010430 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10431 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 {
10433 if (!linewhite(lnum))
10434 break;
10435 ++lnum;
10436 }
10437
10438 /* Find interesting text in this line. */
10439 s = skipwhite(ml_get(lnum));
10440 /* skip C comment-start */
10441 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010444 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010445 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010446 {
10447 s = skipwhite(ml_get(lnum + 1));
10448 if (*s == '*')
10449 s = skipwhite(s + 1);
10450 }
10451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 txt = _("+-%s%3ld lines: ");
10453 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 + 20 /* for %3ld */
10456 + STRLEN(s))); /* concatenated */
10457 if (r != NULL)
10458 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010459 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10460 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10461 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 len = (int)STRLEN(r);
10463 STRCAT(r, s);
10464 /* remove 'foldmarker' and 'commentstring' */
10465 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 }
10468 }
10469#endif
10470}
10471
10472/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010473 * "foldtextresult(lnum)" function
10474 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010477 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010479{
10480#ifdef FEAT_FOLDING
10481 linenr_T lnum;
10482 char_u *text;
10483 char_u buf[51];
10484 foldinfo_T foldinfo;
10485 int fold_count;
10486#endif
10487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010488 rettv->v_type = VAR_STRING;
10489 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010490#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010492 /* treat illegal types and illegal string values for {lnum} the same */
10493 if (lnum < 0)
10494 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010495 fold_count = foldedCount(curwin, lnum, &foldinfo);
10496 if (fold_count > 0)
10497 {
10498 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10499 &foldinfo, buf);
10500 if (text == buf)
10501 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010502 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010503 }
10504#endif
10505}
10506
10507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 * "foreground()" function
10509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010512 typval_T *argvars UNUSED;
10513 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515#ifdef FEAT_GUI
10516 if (gui.in_use)
10517 gui_mch_set_foreground();
10518#else
10519# ifdef WIN32
10520 win32_set_foreground();
10521# endif
10522#endif
10523}
10524
10525/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010526 * "function()" function
10527 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010530 typval_T *argvars;
10531 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010532{
10533 char_u *s;
10534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010536 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010537 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010538 /* Don't check an autoload name for existence here. */
10539 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010540 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010541 else
10542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 rettv->vval.v_string = vim_strsave(s);
10544 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010545 }
10546}
10547
10548/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010549 * "garbagecollect()" function
10550 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010551 static void
10552f_garbagecollect(argvars, rettv)
10553 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010554 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010555{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010556 /* This is postponed until we are back at the toplevel, because we may be
10557 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10558 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010559
10560 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10561 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010562}
10563
10564/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010565 * "get()" function
10566 */
10567 static void
10568f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010569 typval_T *argvars;
10570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010571{
Bram Moolenaar33570922005-01-25 22:26:29 +000010572 listitem_T *li;
10573 list_T *l;
10574 dictitem_T *di;
10575 dict_T *d;
10576 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010577
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010579 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010582 int error = FALSE;
10583
10584 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10585 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010587 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010588 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010589 else if (argvars[0].v_type == VAR_DICT)
10590 {
10591 if ((d = argvars[0].vval.v_dict) != NULL)
10592 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010593 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594 if (di != NULL)
10595 tv = &di->di_tv;
10596 }
10597 }
10598 else
10599 EMSG2(_(e_listdictarg), "get()");
10600
10601 if (tv == NULL)
10602 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010603 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010604 copy_tv(&argvars[2], rettv);
10605 }
10606 else
10607 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010608}
10609
Bram Moolenaar342337a2005-07-21 21:11:17 +000010610static 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 +000010611
10612/*
10613 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010614 * Return a range (from start to end) of lines in rettv from the specified
10615 * buffer.
10616 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010617 */
10618 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010619get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010620 buf_T *buf;
10621 linenr_T start;
10622 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010623 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010624 typval_T *rettv;
10625{
10626 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010627
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010628 if (retlist && rettv_list_alloc(rettv) == FAIL)
10629 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010630
10631 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10632 return;
10633
10634 if (!retlist)
10635 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010636 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10637 p = ml_get_buf(buf, start, FALSE);
10638 else
10639 p = (char_u *)"";
10640
10641 rettv->v_type = VAR_STRING;
10642 rettv->vval.v_string = vim_strsave(p);
10643 }
10644 else
10645 {
10646 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010647 return;
10648
10649 if (start < 1)
10650 start = 1;
10651 if (end > buf->b_ml.ml_line_count)
10652 end = buf->b_ml.ml_line_count;
10653 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010654 if (list_append_string(rettv->vval.v_list,
10655 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010656 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010657 }
10658}
10659
10660/*
10661 * "getbufline()" function
10662 */
10663 static void
10664f_getbufline(argvars, rettv)
10665 typval_T *argvars;
10666 typval_T *rettv;
10667{
10668 linenr_T lnum;
10669 linenr_T end;
10670 buf_T *buf;
10671
10672 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10673 ++emsg_off;
10674 buf = get_buf_tv(&argvars[0]);
10675 --emsg_off;
10676
Bram Moolenaar661b1822005-07-28 22:36:45 +000010677 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010678 if (argvars[2].v_type == VAR_UNKNOWN)
10679 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010680 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010681 end = get_tv_lnum_buf(&argvars[2], buf);
10682
Bram Moolenaar342337a2005-07-21 21:11:17 +000010683 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010684}
10685
Bram Moolenaar0d660222005-01-07 21:51:51 +000010686/*
10687 * "getbufvar()" function
10688 */
10689 static void
10690f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 typval_T *argvars;
10692 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010693{
10694 buf_T *buf;
10695 buf_T *save_curbuf;
10696 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010697 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10700 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010701 ++emsg_off;
10702 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010703
10704 rettv->v_type = VAR_STRING;
10705 rettv->vval.v_string = NULL;
10706
10707 if (buf != NULL && varname != NULL)
10708 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010709 /* set curbuf to be our buf, temporarily */
10710 save_curbuf = curbuf;
10711 curbuf = buf;
10712
Bram Moolenaar0d660222005-01-07 21:51:51 +000010713 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010714 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010715 else
10716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010717 if (*varname == NUL)
10718 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10719 * scope prefix before the NUL byte is required by
10720 * find_var_in_ht(). */
10721 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010722 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010723 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010724 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010725 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010726 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010727
10728 /* restore previous notion of curbuf */
10729 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010730 }
10731
10732 --emsg_off;
10733}
10734
10735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 * "getchar()" function
10737 */
10738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010740 typval_T *argvars;
10741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742{
10743 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010746 /* Position the cursor. Needed after a message that ends in a space. */
10747 windgoto(msg_row, msg_col);
10748
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 ++no_mapping;
10750 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010751 for (;;)
10752 {
10753 if (argvars[0].v_type == VAR_UNKNOWN)
10754 /* getchar(): blocking wait. */
10755 n = safe_vgetc();
10756 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10757 /* getchar(1): only check if char avail */
10758 n = vpeekc();
10759 else if (error || vpeekc() == NUL)
10760 /* illegal argument or getchar(0) and no char avail: return zero */
10761 n = 0;
10762 else
10763 /* getchar(0) and char avail: return char */
10764 n = safe_vgetc();
10765 if (n == K_IGNORE)
10766 continue;
10767 break;
10768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 --no_mapping;
10770 --allow_keys;
10771
Bram Moolenaar219b8702006-11-01 14:32:36 +000010772 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10773 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10774 vimvars[VV_MOUSE_COL].vv_nr = 0;
10775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 if (IS_SPECIAL(n) || mod_mask != 0)
10778 {
10779 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10780 int i = 0;
10781
10782 /* Turn a special key into three bytes, plus modifier. */
10783 if (mod_mask != 0)
10784 {
10785 temp[i++] = K_SPECIAL;
10786 temp[i++] = KS_MODIFIER;
10787 temp[i++] = mod_mask;
10788 }
10789 if (IS_SPECIAL(n))
10790 {
10791 temp[i++] = K_SPECIAL;
10792 temp[i++] = K_SECOND(n);
10793 temp[i++] = K_THIRD(n);
10794 }
10795#ifdef FEAT_MBYTE
10796 else if (has_mbyte)
10797 i += (*mb_char2bytes)(n, temp + i);
10798#endif
10799 else
10800 temp[i++] = n;
10801 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->v_type = VAR_STRING;
10803 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010804
10805#ifdef FEAT_MOUSE
10806 if (n == K_LEFTMOUSE
10807 || n == K_LEFTMOUSE_NM
10808 || n == K_LEFTDRAG
10809 || n == K_LEFTRELEASE
10810 || n == K_LEFTRELEASE_NM
10811 || n == K_MIDDLEMOUSE
10812 || n == K_MIDDLEDRAG
10813 || n == K_MIDDLERELEASE
10814 || n == K_RIGHTMOUSE
10815 || n == K_RIGHTDRAG
10816 || n == K_RIGHTRELEASE
10817 || n == K_X1MOUSE
10818 || n == K_X1DRAG
10819 || n == K_X1RELEASE
10820 || n == K_X2MOUSE
10821 || n == K_X2DRAG
10822 || n == K_X2RELEASE
10823 || n == K_MOUSEDOWN
10824 || n == K_MOUSEUP)
10825 {
10826 int row = mouse_row;
10827 int col = mouse_col;
10828 win_T *win;
10829 linenr_T lnum;
10830# ifdef FEAT_WINDOWS
10831 win_T *wp;
10832# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010833 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010834
10835 if (row >= 0 && col >= 0)
10836 {
10837 /* Find the window at the mouse coordinates and compute the
10838 * text position. */
10839 win = mouse_find_win(&row, &col);
10840 (void)mouse_comp_pos(win, &row, &col, &lnum);
10841# ifdef FEAT_WINDOWS
10842 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010843 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010844# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010845 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010846 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10847 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10848 }
10849 }
10850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 }
10852}
10853
10854/*
10855 * "getcharmod()" function
10856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010859 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863}
10864
10865/*
10866 * "getcmdline()" function
10867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010870 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 rettv->v_type = VAR_STRING;
10874 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875}
10876
10877/*
10878 * "getcmdpos()" function
10879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010882 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886}
10887
10888/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010889 * "getcmdtype()" function
10890 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010891 static void
10892f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010893 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010894 typval_T *rettv;
10895{
10896 rettv->v_type = VAR_STRING;
10897 rettv->vval.v_string = alloc(2);
10898 if (rettv->vval.v_string != NULL)
10899 {
10900 rettv->vval.v_string[0] = get_cmdline_type();
10901 rettv->vval.v_string[1] = NUL;
10902 }
10903}
10904
10905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 * "getcwd()" function
10907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010910 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
10913 char_u cwd[MAXPATHL];
10914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 else
10919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010920 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010922 if (rettv->vval.v_string != NULL)
10923 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924#endif
10925 }
10926}
10927
10928/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010929 * "getfontname()" function
10930 */
10931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010933 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010934 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010935{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010936 rettv->v_type = VAR_STRING;
10937 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010938#ifdef FEAT_GUI
10939 if (gui.in_use)
10940 {
10941 GuiFont font;
10942 char_u *name = NULL;
10943
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010944 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010945 {
10946 /* Get the "Normal" font. Either the name saved by
10947 * hl_set_font_name() or from the font ID. */
10948 font = gui.norm_font;
10949 name = hl_get_font_name();
10950 }
10951 else
10952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010954 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10955 return;
10956 font = gui_mch_get_font(name, FALSE);
10957 if (font == NOFONT)
10958 return; /* Invalid font name, return empty string. */
10959 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010961 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010962 gui_mch_free_font(font);
10963 }
10964#endif
10965}
10966
10967/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010968 * "getfperm({fname})" function
10969 */
10970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010972 typval_T *argvars;
10973 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010974{
10975 char_u *fname;
10976 struct stat st;
10977 char_u *perm = NULL;
10978 char_u flags[] = "rwx";
10979 int i;
10980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010981 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010983 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010984 if (mch_stat((char *)fname, &st) >= 0)
10985 {
10986 perm = vim_strsave((char_u *)"---------");
10987 if (perm != NULL)
10988 {
10989 for (i = 0; i < 9; i++)
10990 {
10991 if (st.st_mode & (1 << (8 - i)))
10992 perm[i] = flags[i % 3];
10993 }
10994 }
10995 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010997}
10998
10999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 * "getfsize({fname})" function
11001 */
11002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011003f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011004 typval_T *argvars;
11005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006{
11007 char_u *fname;
11008 struct stat st;
11009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013
11014 if (mch_stat((char *)fname, &st) >= 0)
11015 {
11016 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011020 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011021
11022 /* non-perfect check for overflow */
11023 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11024 rettv->vval.v_number = -2;
11025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 }
11027 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029}
11030
11031/*
11032 * "getftime({fname})" function
11033 */
11034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011036 typval_T *argvars;
11037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038{
11039 char_u *fname;
11040 struct stat st;
11041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043
11044 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048}
11049
11050/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011051 * "getftype({fname})" function
11052 */
11053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011057{
11058 char_u *fname;
11059 struct stat st;
11060 char_u *type = NULL;
11061 char *t;
11062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011066 if (mch_lstat((char *)fname, &st) >= 0)
11067 {
11068#ifdef S_ISREG
11069 if (S_ISREG(st.st_mode))
11070 t = "file";
11071 else if (S_ISDIR(st.st_mode))
11072 t = "dir";
11073# ifdef S_ISLNK
11074 else if (S_ISLNK(st.st_mode))
11075 t = "link";
11076# endif
11077# ifdef S_ISBLK
11078 else if (S_ISBLK(st.st_mode))
11079 t = "bdev";
11080# endif
11081# ifdef S_ISCHR
11082 else if (S_ISCHR(st.st_mode))
11083 t = "cdev";
11084# endif
11085# ifdef S_ISFIFO
11086 else if (S_ISFIFO(st.st_mode))
11087 t = "fifo";
11088# endif
11089# ifdef S_ISSOCK
11090 else if (S_ISSOCK(st.st_mode))
11091 t = "fifo";
11092# endif
11093 else
11094 t = "other";
11095#else
11096# ifdef S_IFMT
11097 switch (st.st_mode & S_IFMT)
11098 {
11099 case S_IFREG: t = "file"; break;
11100 case S_IFDIR: t = "dir"; break;
11101# ifdef S_IFLNK
11102 case S_IFLNK: t = "link"; break;
11103# endif
11104# ifdef S_IFBLK
11105 case S_IFBLK: t = "bdev"; break;
11106# endif
11107# ifdef S_IFCHR
11108 case S_IFCHR: t = "cdev"; break;
11109# endif
11110# ifdef S_IFIFO
11111 case S_IFIFO: t = "fifo"; break;
11112# endif
11113# ifdef S_IFSOCK
11114 case S_IFSOCK: t = "socket"; break;
11115# endif
11116 default: t = "other";
11117 }
11118# else
11119 if (mch_isdir(fname))
11120 t = "dir";
11121 else
11122 t = "file";
11123# endif
11124#endif
11125 type = vim_strsave((char_u *)t);
11126 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011128}
11129
11130/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011131 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011132 */
11133 static void
11134f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011137{
11138 linenr_T lnum;
11139 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011140 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011141
11142 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011143 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011144 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011145 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011146 retlist = FALSE;
11147 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011148 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011149 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011151 retlist = TRUE;
11152 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011153
Bram Moolenaar342337a2005-07-21 21:11:17 +000011154 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011155}
11156
11157/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011158 * "getmatches()" function
11159 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011160 static void
11161f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011162 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011163 typval_T *rettv;
11164{
11165#ifdef FEAT_SEARCH_EXTRA
11166 dict_T *dict;
11167 matchitem_T *cur = curwin->w_match_head;
11168
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011169 if (rettv_list_alloc(rettv) == OK)
11170 {
11171 while (cur != NULL)
11172 {
11173 dict = dict_alloc();
11174 if (dict == NULL)
11175 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011176 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11177 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11178 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11179 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11180 list_append_dict(rettv->vval.v_list, dict);
11181 cur = cur->next;
11182 }
11183 }
11184#endif
11185}
11186
11187/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011188 * "getpid()" function
11189 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011190 static void
11191f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011192 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011193 typval_T *rettv;
11194{
11195 rettv->vval.v_number = mch_get_pid();
11196}
11197
11198/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011199 * "getpos(string)" function
11200 */
11201 static void
11202f_getpos(argvars, rettv)
11203 typval_T *argvars;
11204 typval_T *rettv;
11205{
11206 pos_T *fp;
11207 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011208 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011209
11210 if (rettv_list_alloc(rettv) == OK)
11211 {
11212 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011213 fp = var2fpos(&argvars[0], TRUE, &fnum);
11214 if (fnum != -1)
11215 list_append_number(l, (varnumber_T)fnum);
11216 else
11217 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011218 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11219 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011220 list_append_number(l, (fp != NULL)
11221 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011222 : (varnumber_T)0);
11223 list_append_number(l,
11224#ifdef FEAT_VIRTUALEDIT
11225 (fp != NULL) ? (varnumber_T)fp->coladd :
11226#endif
11227 (varnumber_T)0);
11228 }
11229 else
11230 rettv->vval.v_number = FALSE;
11231}
11232
11233/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011234 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011235 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011236 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011237f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011238 typval_T *argvars UNUSED;
11239 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011240{
11241#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011242 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011243#endif
11244
Bram Moolenaar2641f772005-03-25 21:58:17 +000011245#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011246 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011247 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011248 wp = NULL;
11249 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11250 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011251 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011252 if (wp == NULL)
11253 return;
11254 }
11255
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011256 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011257 }
11258#endif
11259}
11260
11261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 * "getreg()" function
11263 */
11264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011266 typval_T *argvars;
11267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268{
11269 char_u *strregname;
11270 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011271 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011272 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011274 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276 strregname = get_tv_string_chk(&argvars[0]);
11277 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011278 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011282 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 regname = (strregname == NULL ? '"' : *strregname);
11284 if (regname == 0)
11285 regname = '"';
11286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011288 rettv->vval.v_string = error ? NULL :
11289 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290}
11291
11292/*
11293 * "getregtype()" function
11294 */
11295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011297 typval_T *argvars;
11298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299{
11300 char_u *strregname;
11301 int regname;
11302 char_u buf[NUMBUFLEN + 2];
11303 long reglen = 0;
11304
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011305 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306 {
11307 strregname = get_tv_string_chk(&argvars[0]);
11308 if (strregname == NULL) /* type error; errmsg already given */
11309 {
11310 rettv->v_type = VAR_STRING;
11311 rettv->vval.v_string = NULL;
11312 return;
11313 }
11314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 else
11316 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011317 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318
11319 regname = (strregname == NULL ? '"' : *strregname);
11320 if (regname == 0)
11321 regname = '"';
11322
11323 buf[0] = NUL;
11324 buf[1] = NUL;
11325 switch (get_reg_type(regname, &reglen))
11326 {
11327 case MLINE: buf[0] = 'V'; break;
11328 case MCHAR: buf[0] = 'v'; break;
11329#ifdef FEAT_VISUAL
11330 case MBLOCK:
11331 buf[0] = Ctrl_V;
11332 sprintf((char *)buf + 1, "%ld", reglen + 1);
11333 break;
11334#endif
11335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->v_type = VAR_STRING;
11337 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338}
11339
11340/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011341 * "gettabvar()" function
11342 */
11343 static void
11344f_gettabvar(argvars, rettv)
11345 typval_T *argvars;
11346 typval_T *rettv;
11347{
11348 tabpage_T *tp;
11349 dictitem_T *v;
11350 char_u *varname;
11351
11352 rettv->v_type = VAR_STRING;
11353 rettv->vval.v_string = NULL;
11354
11355 varname = get_tv_string_chk(&argvars[1]);
11356 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11357 if (tp != NULL && varname != NULL)
11358 {
11359 /* look up the variable */
11360 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11361 if (v != NULL)
11362 copy_tv(&v->di_tv, rettv);
11363 }
11364}
11365
11366/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011367 * "gettabwinvar()" function
11368 */
11369 static void
11370f_gettabwinvar(argvars, rettv)
11371 typval_T *argvars;
11372 typval_T *rettv;
11373{
11374 getwinvar(argvars, rettv, 1);
11375}
11376
11377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 * "getwinposx()" function
11379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011382 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386#ifdef FEAT_GUI
11387 if (gui.in_use)
11388 {
11389 int x, y;
11390
11391 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 }
11394#endif
11395}
11396
11397/*
11398 * "getwinposy()" function
11399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011402 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011405 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406#ifdef FEAT_GUI
11407 if (gui.in_use)
11408 {
11409 int x, y;
11410
11411 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 }
11414#endif
11415}
11416
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011417/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011418 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011419 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011420 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011421find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011422 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011423 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011424{
11425#ifdef FEAT_WINDOWS
11426 win_T *wp;
11427#endif
11428 int nr;
11429
11430 nr = get_tv_number_chk(vp, NULL);
11431
11432#ifdef FEAT_WINDOWS
11433 if (nr < 0)
11434 return NULL;
11435 if (nr == 0)
11436 return curwin;
11437
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011438 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11439 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011440 if (--nr <= 0)
11441 break;
11442 return wp;
11443#else
11444 if (nr == 0 || nr == 1)
11445 return curwin;
11446 return NULL;
11447#endif
11448}
11449
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450/*
11451 * "getwinvar()" function
11452 */
11453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011455 typval_T *argvars;
11456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011458 getwinvar(argvars, rettv, 0);
11459}
11460
11461/*
11462 * getwinvar() and gettabwinvar()
11463 */
11464 static void
11465getwinvar(argvars, rettv, off)
11466 typval_T *argvars;
11467 typval_T *rettv;
11468 int off; /* 1 for gettabwinvar() */
11469{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 win_T *win, *oldcurwin;
11471 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011473 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011475#ifdef FEAT_WINDOWS
11476 if (off == 1)
11477 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11478 else
11479 tp = curtab;
11480#endif
11481 win = find_win_by_nr(&argvars[off], tp);
11482 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011483 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->v_type = VAR_STRING;
11486 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487
11488 if (win != NULL && varname != NULL)
11489 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011490 /* Set curwin to be our win, temporarily. Also set curbuf, so
11491 * that we can get buffer-local options. */
11492 oldcurwin = curwin;
11493 curwin = win;
11494 curbuf = win->w_buffer;
11495
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498 else
11499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 if (*varname == NUL)
11501 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11502 * scope prefix before the NUL byte is required by
11503 * find_var_in_ht(). */
11504 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011506 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011508 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011510
11511 /* restore previous notion of curwin */
11512 curwin = oldcurwin;
11513 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 }
11515
11516 --emsg_off;
11517}
11518
11519/*
11520 * "glob()" function
11521 */
11522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011524 typval_T *argvars;
11525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011527 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011529 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011531 /* When the optional second argument is non-zero, don't remove matches
11532 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11533 if (argvars[1].v_type != VAR_UNKNOWN
11534 && get_tv_number_chk(&argvars[1], &error))
11535 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011537 if (!error)
11538 {
11539 ExpandInit(&xpc);
11540 xpc.xp_context = EXPAND_FILES;
11541 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11542 NULL, flags, WILD_ALL);
11543 }
11544 else
11545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546}
11547
11548/*
11549 * "globpath()" function
11550 */
11551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011553 typval_T *argvars;
11554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011556 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011558 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011559 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011561 /* When the optional second argument is non-zero, don't remove matches
11562 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11563 if (argvars[2].v_type != VAR_UNKNOWN
11564 && get_tv_number_chk(&argvars[2], &error))
11565 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011567 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 rettv->vval.v_string = NULL;
11569 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011570 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11571 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572}
11573
11574/*
11575 * "has()" function
11576 */
11577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011579 typval_T *argvars;
11580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581{
11582 int i;
11583 char_u *name;
11584 int n = FALSE;
11585 static char *(has_list[]) =
11586 {
11587#ifdef AMIGA
11588 "amiga",
11589# ifdef FEAT_ARP
11590 "arp",
11591# endif
11592#endif
11593#ifdef __BEOS__
11594 "beos",
11595#endif
11596#ifdef MSDOS
11597# ifdef DJGPP
11598 "dos32",
11599# else
11600 "dos16",
11601# endif
11602#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011603#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 "mac",
11605#endif
11606#if defined(MACOS_X_UNIX)
11607 "macunix",
11608#endif
11609#ifdef OS2
11610 "os2",
11611#endif
11612#ifdef __QNX__
11613 "qnx",
11614#endif
11615#ifdef RISCOS
11616 "riscos",
11617#endif
11618#ifdef UNIX
11619 "unix",
11620#endif
11621#ifdef VMS
11622 "vms",
11623#endif
11624#ifdef WIN16
11625 "win16",
11626#endif
11627#ifdef WIN32
11628 "win32",
11629#endif
11630#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11631 "win32unix",
11632#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011633#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 "win64",
11635#endif
11636#ifdef EBCDIC
11637 "ebcdic",
11638#endif
11639#ifndef CASE_INSENSITIVE_FILENAME
11640 "fname_case",
11641#endif
11642#ifdef FEAT_ARABIC
11643 "arabic",
11644#endif
11645#ifdef FEAT_AUTOCMD
11646 "autocmd",
11647#endif
11648#ifdef FEAT_BEVAL
11649 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011650# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11651 "balloon_multiline",
11652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653#endif
11654#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11655 "builtin_terms",
11656# ifdef ALL_BUILTIN_TCAPS
11657 "all_builtin_terms",
11658# endif
11659#endif
11660#ifdef FEAT_BYTEOFF
11661 "byte_offset",
11662#endif
11663#ifdef FEAT_CINDENT
11664 "cindent",
11665#endif
11666#ifdef FEAT_CLIENTSERVER
11667 "clientserver",
11668#endif
11669#ifdef FEAT_CLIPBOARD
11670 "clipboard",
11671#endif
11672#ifdef FEAT_CMDL_COMPL
11673 "cmdline_compl",
11674#endif
11675#ifdef FEAT_CMDHIST
11676 "cmdline_hist",
11677#endif
11678#ifdef FEAT_COMMENTS
11679 "comments",
11680#endif
11681#ifdef FEAT_CRYPT
11682 "cryptv",
11683#endif
11684#ifdef FEAT_CSCOPE
11685 "cscope",
11686#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011687#ifdef CURSOR_SHAPE
11688 "cursorshape",
11689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690#ifdef DEBUG
11691 "debug",
11692#endif
11693#ifdef FEAT_CON_DIALOG
11694 "dialog_con",
11695#endif
11696#ifdef FEAT_GUI_DIALOG
11697 "dialog_gui",
11698#endif
11699#ifdef FEAT_DIFF
11700 "diff",
11701#endif
11702#ifdef FEAT_DIGRAPHS
11703 "digraphs",
11704#endif
11705#ifdef FEAT_DND
11706 "dnd",
11707#endif
11708#ifdef FEAT_EMACS_TAGS
11709 "emacs_tags",
11710#endif
11711 "eval", /* always present, of course! */
11712#ifdef FEAT_EX_EXTRA
11713 "ex_extra",
11714#endif
11715#ifdef FEAT_SEARCH_EXTRA
11716 "extra_search",
11717#endif
11718#ifdef FEAT_FKMAP
11719 "farsi",
11720#endif
11721#ifdef FEAT_SEARCHPATH
11722 "file_in_path",
11723#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011724#if defined(UNIX) && !defined(USE_SYSTEM)
11725 "filterpipe",
11726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727#ifdef FEAT_FIND_ID
11728 "find_in_path",
11729#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011730#ifdef FEAT_FLOAT
11731 "float",
11732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733#ifdef FEAT_FOLDING
11734 "folding",
11735#endif
11736#ifdef FEAT_FOOTER
11737 "footer",
11738#endif
11739#if !defined(USE_SYSTEM) && defined(UNIX)
11740 "fork",
11741#endif
11742#ifdef FEAT_GETTEXT
11743 "gettext",
11744#endif
11745#ifdef FEAT_GUI
11746 "gui",
11747#endif
11748#ifdef FEAT_GUI_ATHENA
11749# ifdef FEAT_GUI_NEXTAW
11750 "gui_neXtaw",
11751# else
11752 "gui_athena",
11753# endif
11754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755#ifdef FEAT_GUI_GTK
11756 "gui_gtk",
11757# ifdef HAVE_GTK2
11758 "gui_gtk2",
11759# endif
11760#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011761#ifdef FEAT_GUI_GNOME
11762 "gui_gnome",
11763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764#ifdef FEAT_GUI_MAC
11765 "gui_mac",
11766#endif
11767#ifdef FEAT_GUI_MOTIF
11768 "gui_motif",
11769#endif
11770#ifdef FEAT_GUI_PHOTON
11771 "gui_photon",
11772#endif
11773#ifdef FEAT_GUI_W16
11774 "gui_win16",
11775#endif
11776#ifdef FEAT_GUI_W32
11777 "gui_win32",
11778#endif
11779#ifdef FEAT_HANGULIN
11780 "hangul_input",
11781#endif
11782#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11783 "iconv",
11784#endif
11785#ifdef FEAT_INS_EXPAND
11786 "insert_expand",
11787#endif
11788#ifdef FEAT_JUMPLIST
11789 "jumplist",
11790#endif
11791#ifdef FEAT_KEYMAP
11792 "keymap",
11793#endif
11794#ifdef FEAT_LANGMAP
11795 "langmap",
11796#endif
11797#ifdef FEAT_LIBCALL
11798 "libcall",
11799#endif
11800#ifdef FEAT_LINEBREAK
11801 "linebreak",
11802#endif
11803#ifdef FEAT_LISP
11804 "lispindent",
11805#endif
11806#ifdef FEAT_LISTCMDS
11807 "listcmds",
11808#endif
11809#ifdef FEAT_LOCALMAP
11810 "localmap",
11811#endif
11812#ifdef FEAT_MENU
11813 "menu",
11814#endif
11815#ifdef FEAT_SESSION
11816 "mksession",
11817#endif
11818#ifdef FEAT_MODIFY_FNAME
11819 "modify_fname",
11820#endif
11821#ifdef FEAT_MOUSE
11822 "mouse",
11823#endif
11824#ifdef FEAT_MOUSESHAPE
11825 "mouseshape",
11826#endif
11827#if defined(UNIX) || defined(VMS)
11828# ifdef FEAT_MOUSE_DEC
11829 "mouse_dec",
11830# endif
11831# ifdef FEAT_MOUSE_GPM
11832 "mouse_gpm",
11833# endif
11834# ifdef FEAT_MOUSE_JSB
11835 "mouse_jsbterm",
11836# endif
11837# ifdef FEAT_MOUSE_NET
11838 "mouse_netterm",
11839# endif
11840# ifdef FEAT_MOUSE_PTERM
11841 "mouse_pterm",
11842# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011843# ifdef FEAT_SYSMOUSE
11844 "mouse_sysmouse",
11845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846# ifdef FEAT_MOUSE_XTERM
11847 "mouse_xterm",
11848# endif
11849#endif
11850#ifdef FEAT_MBYTE
11851 "multi_byte",
11852#endif
11853#ifdef FEAT_MBYTE_IME
11854 "multi_byte_ime",
11855#endif
11856#ifdef FEAT_MULTI_LANG
11857 "multi_lang",
11858#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011859#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011860#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011861 "mzscheme",
11862#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864#ifdef FEAT_OLE
11865 "ole",
11866#endif
11867#ifdef FEAT_OSFILETYPE
11868 "osfiletype",
11869#endif
11870#ifdef FEAT_PATH_EXTRA
11871 "path_extra",
11872#endif
11873#ifdef FEAT_PERL
11874#ifndef DYNAMIC_PERL
11875 "perl",
11876#endif
11877#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011878#ifdef FEAT_PERSISTENT_UNDO
11879 "persistent_undo",
11880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881#ifdef FEAT_PYTHON
11882#ifndef DYNAMIC_PYTHON
11883 "python",
11884#endif
11885#endif
11886#ifdef FEAT_POSTSCRIPT
11887 "postscript",
11888#endif
11889#ifdef FEAT_PRINTER
11890 "printer",
11891#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011892#ifdef FEAT_PROFILE
11893 "profile",
11894#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011895#ifdef FEAT_RELTIME
11896 "reltime",
11897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898#ifdef FEAT_QUICKFIX
11899 "quickfix",
11900#endif
11901#ifdef FEAT_RIGHTLEFT
11902 "rightleft",
11903#endif
11904#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11905 "ruby",
11906#endif
11907#ifdef FEAT_SCROLLBIND
11908 "scrollbind",
11909#endif
11910#ifdef FEAT_CMDL_INFO
11911 "showcmd",
11912 "cmdline_info",
11913#endif
11914#ifdef FEAT_SIGNS
11915 "signs",
11916#endif
11917#ifdef FEAT_SMARTINDENT
11918 "smartindent",
11919#endif
11920#ifdef FEAT_SNIFF
11921 "sniff",
11922#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000011923#ifdef STARTUPTIME
11924 "startuptime",
11925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926#ifdef FEAT_STL_OPT
11927 "statusline",
11928#endif
11929#ifdef FEAT_SUN_WORKSHOP
11930 "sun_workshop",
11931#endif
11932#ifdef FEAT_NETBEANS_INTG
11933 "netbeans_intg",
11934#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011935#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011936 "spell",
11937#endif
11938#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 "syntax",
11940#endif
11941#if defined(USE_SYSTEM) || !defined(UNIX)
11942 "system",
11943#endif
11944#ifdef FEAT_TAG_BINS
11945 "tag_binary",
11946#endif
11947#ifdef FEAT_TAG_OLDSTATIC
11948 "tag_old_static",
11949#endif
11950#ifdef FEAT_TAG_ANYWHITE
11951 "tag_any_white",
11952#endif
11953#ifdef FEAT_TCL
11954# ifndef DYNAMIC_TCL
11955 "tcl",
11956# endif
11957#endif
11958#ifdef TERMINFO
11959 "terminfo",
11960#endif
11961#ifdef FEAT_TERMRESPONSE
11962 "termresponse",
11963#endif
11964#ifdef FEAT_TEXTOBJ
11965 "textobjects",
11966#endif
11967#ifdef HAVE_TGETENT
11968 "tgetent",
11969#endif
11970#ifdef FEAT_TITLE
11971 "title",
11972#endif
11973#ifdef FEAT_TOOLBAR
11974 "toolbar",
11975#endif
11976#ifdef FEAT_USR_CMDS
11977 "user-commands", /* was accidentally included in 5.4 */
11978 "user_commands",
11979#endif
11980#ifdef FEAT_VIMINFO
11981 "viminfo",
11982#endif
11983#ifdef FEAT_VERTSPLIT
11984 "vertsplit",
11985#endif
11986#ifdef FEAT_VIRTUALEDIT
11987 "virtualedit",
11988#endif
11989#ifdef FEAT_VISUAL
11990 "visual",
11991#endif
11992#ifdef FEAT_VISUALEXTRA
11993 "visualextra",
11994#endif
11995#ifdef FEAT_VREPLACE
11996 "vreplace",
11997#endif
11998#ifdef FEAT_WILDIGN
11999 "wildignore",
12000#endif
12001#ifdef FEAT_WILDMENU
12002 "wildmenu",
12003#endif
12004#ifdef FEAT_WINDOWS
12005 "windows",
12006#endif
12007#ifdef FEAT_WAK
12008 "winaltkeys",
12009#endif
12010#ifdef FEAT_WRITEBACKUP
12011 "writebackup",
12012#endif
12013#ifdef FEAT_XIM
12014 "xim",
12015#endif
12016#ifdef FEAT_XFONTSET
12017 "xfontset",
12018#endif
12019#ifdef USE_XSMP
12020 "xsmp",
12021#endif
12022#ifdef USE_XSMP_INTERACT
12023 "xsmp_interact",
12024#endif
12025#ifdef FEAT_XCLIPBOARD
12026 "xterm_clipboard",
12027#endif
12028#ifdef FEAT_XTERM_SAVE
12029 "xterm_save",
12030#endif
12031#if defined(UNIX) && defined(FEAT_X11)
12032 "X11",
12033#endif
12034 NULL
12035 };
12036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 for (i = 0; has_list[i] != NULL; ++i)
12039 if (STRICMP(name, has_list[i]) == 0)
12040 {
12041 n = TRUE;
12042 break;
12043 }
12044
12045 if (n == FALSE)
12046 {
12047 if (STRNICMP(name, "patch", 5) == 0)
12048 n = has_patch(atoi((char *)name + 5));
12049 else if (STRICMP(name, "vim_starting") == 0)
12050 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012051#ifdef FEAT_MBYTE
12052 else if (STRICMP(name, "multi_byte_encoding") == 0)
12053 n = has_mbyte;
12054#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012055#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12056 else if (STRICMP(name, "balloon_multiline") == 0)
12057 n = multiline_balloon_available();
12058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059#ifdef DYNAMIC_TCL
12060 else if (STRICMP(name, "tcl") == 0)
12061 n = tcl_enabled(FALSE);
12062#endif
12063#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12064 else if (STRICMP(name, "iconv") == 0)
12065 n = iconv_enabled(FALSE);
12066#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012067#ifdef DYNAMIC_MZSCHEME
12068 else if (STRICMP(name, "mzscheme") == 0)
12069 n = mzscheme_enabled(FALSE);
12070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071#ifdef DYNAMIC_RUBY
12072 else if (STRICMP(name, "ruby") == 0)
12073 n = ruby_enabled(FALSE);
12074#endif
12075#ifdef DYNAMIC_PYTHON
12076 else if (STRICMP(name, "python") == 0)
12077 n = python_enabled(FALSE);
12078#endif
12079#ifdef DYNAMIC_PERL
12080 else if (STRICMP(name, "perl") == 0)
12081 n = perl_enabled(FALSE);
12082#endif
12083#ifdef FEAT_GUI
12084 else if (STRICMP(name, "gui_running") == 0)
12085 n = (gui.in_use || gui.starting);
12086# ifdef FEAT_GUI_W32
12087 else if (STRICMP(name, "gui_win32s") == 0)
12088 n = gui_is_win32s();
12089# endif
12090# ifdef FEAT_BROWSE
12091 else if (STRICMP(name, "browse") == 0)
12092 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12093# endif
12094#endif
12095#ifdef FEAT_SYN_HL
12096 else if (STRICMP(name, "syntax_items") == 0)
12097 n = syntax_present(curbuf);
12098#endif
12099#if defined(WIN3264)
12100 else if (STRICMP(name, "win95") == 0)
12101 n = mch_windows95();
12102#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012103#ifdef FEAT_NETBEANS_INTG
12104 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012105 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 }
12108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012109 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110}
12111
12112/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012113 * "has_key()" function
12114 */
12115 static void
12116f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012117 typval_T *argvars;
12118 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012119{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012120 if (argvars[0].v_type != VAR_DICT)
12121 {
12122 EMSG(_(e_dictreq));
12123 return;
12124 }
12125 if (argvars[0].vval.v_dict == NULL)
12126 return;
12127
12128 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012129 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012130}
12131
12132/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012133 * "haslocaldir()" function
12134 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012135 static void
12136f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012137 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012138 typval_T *rettv;
12139{
12140 rettv->vval.v_number = (curwin->w_localdir != NULL);
12141}
12142
12143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 * "hasmapto()" function
12145 */
12146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012148 typval_T *argvars;
12149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150{
12151 char_u *name;
12152 char_u *mode;
12153 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012154 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012157 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 mode = (char_u *)"nvo";
12159 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012161 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012162 if (argvars[2].v_type != VAR_UNKNOWN)
12163 abbr = get_tv_number(&argvars[2]);
12164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165
Bram Moolenaar2c932302006-03-18 21:42:09 +000012166 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170}
12171
12172/*
12173 * "histadd()" function
12174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012177 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179{
12180#ifdef FEAT_CMDHIST
12181 int histype;
12182 char_u *str;
12183 char_u buf[NUMBUFLEN];
12184#endif
12185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 if (check_restricted() || check_secure())
12188 return;
12189#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012190 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12191 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 if (histype >= 0)
12193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 if (*str != NUL)
12196 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012197 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012199 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 return;
12201 }
12202 }
12203#endif
12204}
12205
12206/*
12207 * "histdel()" function
12208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012210f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012211 typval_T *argvars UNUSED;
12212 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213{
12214#ifdef FEAT_CMDHIST
12215 int n;
12216 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012217 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012219 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12220 if (str == NULL)
12221 n = 0;
12222 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012224 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012225 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012227 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012228 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 else
12230 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012231 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012232 get_tv_string_buf(&argvars[1], buf));
12233 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#endif
12235}
12236
12237/*
12238 * "histget()" function
12239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012241f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012242 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244{
12245#ifdef FEAT_CMDHIST
12246 int type;
12247 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012248 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012250 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12251 if (str == NULL)
12252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012254 {
12255 type = get_histtype(str);
12256 if (argvars[1].v_type == VAR_UNKNOWN)
12257 idx = get_history_idx(type);
12258 else
12259 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12260 /* -1 on type error */
12261 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012266 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267}
12268
12269/*
12270 * "histnr()" function
12271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012274 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276{
12277 int i;
12278
12279#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012280 char_u *history = get_tv_string_chk(&argvars[0]);
12281
12282 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 if (i >= HIST_CMD && i < HIST_COUNT)
12284 i = get_history_idx(i);
12285 else
12286#endif
12287 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289}
12290
12291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 * "highlightID(name)" function
12293 */
12294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012295f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012296 typval_T *argvars;
12297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300}
12301
12302/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012303 * "highlight_exists()" function
12304 */
12305 static void
12306f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012307 typval_T *argvars;
12308 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012309{
12310 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12311}
12312
12313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 * "hostname()" function
12315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012318 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320{
12321 char_u hostname[256];
12322
12323 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 rettv->v_type = VAR_STRING;
12325 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326}
12327
12328/*
12329 * iconv() function
12330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012332f_iconv(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#ifdef FEAT_MBYTE
12337 char_u buf1[NUMBUFLEN];
12338 char_u buf2[NUMBUFLEN];
12339 char_u *from, *to, *str;
12340 vimconv_T vimconv;
12341#endif
12342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->v_type = VAR_STRING;
12344 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345
12346#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347 str = get_tv_string(&argvars[0]);
12348 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12349 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 vimconv.vc_type = CONV_NONE;
12351 convert_setup(&vimconv, from, to);
12352
12353 /* If the encodings are equal, no conversion needed. */
12354 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012355 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358
12359 convert_setup(&vimconv, NULL, NULL);
12360 vim_free(from);
12361 vim_free(to);
12362#endif
12363}
12364
12365/*
12366 * "indent()" function
12367 */
12368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012370 typval_T *argvars;
12371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372{
12373 linenr_T lnum;
12374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012375 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380}
12381
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012382/*
12383 * "index()" function
12384 */
12385 static void
12386f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012387 typval_T *argvars;
12388 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012389{
Bram Moolenaar33570922005-01-25 22:26:29 +000012390 list_T *l;
12391 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012392 long idx = 0;
12393 int ic = FALSE;
12394
12395 rettv->vval.v_number = -1;
12396 if (argvars[0].v_type != VAR_LIST)
12397 {
12398 EMSG(_(e_listreq));
12399 return;
12400 }
12401 l = argvars[0].vval.v_list;
12402 if (l != NULL)
12403 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012404 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012405 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 int error = FALSE;
12408
Bram Moolenaar758711c2005-02-02 23:11:38 +000012409 /* Start at specified item. Use the cached index that list_find()
12410 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012411 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012412 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012413 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012414 ic = get_tv_number_chk(&argvars[3], &error);
12415 if (error)
12416 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012417 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012418
Bram Moolenaar758711c2005-02-02 23:11:38 +000012419 for ( ; item != NULL; item = item->li_next, ++idx)
12420 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012421 {
12422 rettv->vval.v_number = idx;
12423 break;
12424 }
12425 }
12426}
12427
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428static int inputsecret_flag = 0;
12429
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012430static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12431
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012433 * This function is used by f_input() and f_inputdialog() functions. The third
12434 * argument to f_input() specifies the type of completion to use at the
12435 * prompt. The third argument to f_inputdialog() specifies the value to return
12436 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 */
12438 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012439get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012440 typval_T *argvars;
12441 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012442 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012444 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 char_u *p = NULL;
12446 int c;
12447 char_u buf[NUMBUFLEN];
12448 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012449 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012450 int xp_type = EXPAND_NOTHING;
12451 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012453 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012454 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455
12456#ifdef NO_CONSOLE_INPUT
12457 /* While starting up, there is no place to enter text. */
12458 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460#endif
12461
12462 cmd_silent = FALSE; /* Want to see the prompt. */
12463 if (prompt != NULL)
12464 {
12465 /* Only the part of the message after the last NL is considered as
12466 * prompt for the command line */
12467 p = vim_strrchr(prompt, '\n');
12468 if (p == NULL)
12469 p = prompt;
12470 else
12471 {
12472 ++p;
12473 c = *p;
12474 *p = NUL;
12475 msg_start();
12476 msg_clr_eos();
12477 msg_puts_attr(prompt, echo_attr);
12478 msg_didout = FALSE;
12479 msg_starthere();
12480 *p = c;
12481 }
12482 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 if (argvars[1].v_type != VAR_UNKNOWN)
12485 {
12486 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12487 if (defstr != NULL)
12488 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012490 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012491 {
12492 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012493 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012494 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012495
Bram Moolenaar4463f292005-09-25 22:20:24 +000012496 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012497
Bram Moolenaar4463f292005-09-25 22:20:24 +000012498 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12499 if (xp_name == NULL)
12500 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012501
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012502 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012503
Bram Moolenaar4463f292005-09-25 22:20:24 +000012504 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12505 &xp_arg) == FAIL)
12506 return;
12507 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012508 }
12509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 if (defstr != NULL)
12511 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012512 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12513 xp_type, xp_arg);
12514
12515 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 /* since the user typed this, no need to wait for return */
12518 need_wait_return = FALSE;
12519 msg_didout = FALSE;
12520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 cmd_silent = cmd_silent_save;
12522}
12523
12524/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012525 * "input()" function
12526 * Also handles inputsecret() when inputsecret is set.
12527 */
12528 static void
12529f_input(argvars, rettv)
12530 typval_T *argvars;
12531 typval_T *rettv;
12532{
12533 get_user_input(argvars, rettv, FALSE);
12534}
12535
12536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 * "inputdialog()" function
12538 */
12539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012540f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012541 typval_T *argvars;
12542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543{
12544#if defined(FEAT_GUI_TEXTDIALOG)
12545 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12546 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12547 {
12548 char_u *message;
12549 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012550 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012552 message = get_tv_string_chk(&argvars[0]);
12553 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012554 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012555 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 else
12557 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012558 if (message != NULL && defstr != NULL
12559 && do_dialog(VIM_QUESTION, NULL, message,
12560 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 else
12563 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012564 if (message != NULL && defstr != NULL
12565 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012566 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012567 rettv->vval.v_string = vim_strsave(
12568 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573 }
12574 else
12575#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012576 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577}
12578
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012579/*
12580 * "inputlist()" function
12581 */
12582 static void
12583f_inputlist(argvars, rettv)
12584 typval_T *argvars;
12585 typval_T *rettv;
12586{
12587 listitem_T *li;
12588 int selected;
12589 int mouse_used;
12590
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012591#ifdef NO_CONSOLE_INPUT
12592 /* While starting up, there is no place to enter text. */
12593 if (no_console_input())
12594 return;
12595#endif
12596 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12597 {
12598 EMSG2(_(e_listarg), "inputlist()");
12599 return;
12600 }
12601
12602 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012603 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012604 lines_left = Rows; /* avoid more prompt */
12605 msg_scroll = TRUE;
12606 msg_clr_eos();
12607
12608 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12609 {
12610 msg_puts(get_tv_string(&li->li_tv));
12611 msg_putchar('\n');
12612 }
12613
12614 /* Ask for choice. */
12615 selected = prompt_for_number(&mouse_used);
12616 if (mouse_used)
12617 selected -= lines_left;
12618
12619 rettv->vval.v_number = selected;
12620}
12621
12622
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12624
12625/*
12626 * "inputrestore()" function
12627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012629f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012630 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632{
12633 if (ga_userinput.ga_len > 0)
12634 {
12635 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12637 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012638 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639 }
12640 else if (p_verbose > 1)
12641 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012642 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 }
12645}
12646
12647/*
12648 * "inputsave()" function
12649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012652 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012655 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 if (ga_grow(&ga_userinput, 1) == OK)
12657 {
12658 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12659 + ga_userinput.ga_len);
12660 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012661 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 }
12663 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012664 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665}
12666
12667/*
12668 * "inputsecret()" function
12669 */
12670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012672 typval_T *argvars;
12673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674{
12675 ++cmdline_star;
12676 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 --cmdline_star;
12679 --inputsecret_flag;
12680}
12681
12682/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012683 * "insert()" function
12684 */
12685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 typval_T *argvars;
12688 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012689{
12690 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012691 listitem_T *item;
12692 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012693 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012694
12695 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012696 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012697 else if ((l = argvars[0].vval.v_list) != NULL
12698 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012699 {
12700 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012701 before = get_tv_number_chk(&argvars[2], &error);
12702 if (error)
12703 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012704
Bram Moolenaar758711c2005-02-02 23:11:38 +000012705 if (before == l->lv_len)
12706 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012707 else
12708 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012709 item = list_find(l, before);
12710 if (item == NULL)
12711 {
12712 EMSGN(_(e_listidx), before);
12713 l = NULL;
12714 }
12715 }
12716 if (l != NULL)
12717 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012718 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012719 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012720 }
12721 }
12722}
12723
12724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725 * "isdirectory()" function
12726 */
12727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012728f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012729 typval_T *argvars;
12730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733}
12734
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012735/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012736 * "islocked()" function
12737 */
12738 static void
12739f_islocked(argvars, rettv)
12740 typval_T *argvars;
12741 typval_T *rettv;
12742{
12743 lval_T lv;
12744 char_u *end;
12745 dictitem_T *di;
12746
12747 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012748 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12749 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012750 if (end != NULL && lv.ll_name != NULL)
12751 {
12752 if (*end != NUL)
12753 EMSG(_(e_trailing));
12754 else
12755 {
12756 if (lv.ll_tv == NULL)
12757 {
12758 if (check_changedtick(lv.ll_name))
12759 rettv->vval.v_number = 1; /* always locked */
12760 else
12761 {
12762 di = find_var(lv.ll_name, NULL);
12763 if (di != NULL)
12764 {
12765 /* Consider a variable locked when:
12766 * 1. the variable itself is locked
12767 * 2. the value of the variable is locked.
12768 * 3. the List or Dict value is locked.
12769 */
12770 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12771 || tv_islocked(&di->di_tv));
12772 }
12773 }
12774 }
12775 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012776 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012777 else if (lv.ll_newkey != NULL)
12778 EMSG2(_(e_dictkey), lv.ll_newkey);
12779 else if (lv.ll_list != NULL)
12780 /* List item. */
12781 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12782 else
12783 /* Dictionary item. */
12784 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12785 }
12786 }
12787
12788 clear_lval(&lv);
12789}
12790
Bram Moolenaar33570922005-01-25 22:26:29 +000012791static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012792
12793/*
12794 * Turn a dict into a list:
12795 * "what" == 0: list of keys
12796 * "what" == 1: list of values
12797 * "what" == 2: list of items
12798 */
12799 static void
12800dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012801 typval_T *argvars;
12802 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012803 int what;
12804{
Bram Moolenaar33570922005-01-25 22:26:29 +000012805 list_T *l2;
12806 dictitem_T *di;
12807 hashitem_T *hi;
12808 listitem_T *li;
12809 listitem_T *li2;
12810 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012811 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012812
Bram Moolenaar8c711452005-01-14 21:53:12 +000012813 if (argvars[0].v_type != VAR_DICT)
12814 {
12815 EMSG(_(e_dictreq));
12816 return;
12817 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012818 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012819 return;
12820
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012821 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012822 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012823
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012824 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012826 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012827 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012828 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012829 --todo;
12830 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012831
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012832 li = listitem_alloc();
12833 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012834 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012835 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012836
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012837 if (what == 0)
12838 {
12839 /* keys() */
12840 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012841 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012842 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12843 }
12844 else if (what == 1)
12845 {
12846 /* values() */
12847 copy_tv(&di->di_tv, &li->li_tv);
12848 }
12849 else
12850 {
12851 /* items() */
12852 l2 = list_alloc();
12853 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012854 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012855 li->li_tv.vval.v_list = l2;
12856 if (l2 == NULL)
12857 break;
12858 ++l2->lv_refcount;
12859
12860 li2 = listitem_alloc();
12861 if (li2 == NULL)
12862 break;
12863 list_append(l2, li2);
12864 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012865 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012866 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12867
12868 li2 = listitem_alloc();
12869 if (li2 == NULL)
12870 break;
12871 list_append(l2, li2);
12872 copy_tv(&di->di_tv, &li2->li_tv);
12873 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012874 }
12875 }
12876}
12877
12878/*
12879 * "items(dict)" function
12880 */
12881 static void
12882f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012883 typval_T *argvars;
12884 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012885{
12886 dict_list(argvars, rettv, 2);
12887}
12888
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012890 * "join()" function
12891 */
12892 static void
12893f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012894 typval_T *argvars;
12895 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012896{
12897 garray_T ga;
12898 char_u *sep;
12899
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012900 if (argvars[0].v_type != VAR_LIST)
12901 {
12902 EMSG(_(e_listreq));
12903 return;
12904 }
12905 if (argvars[0].vval.v_list == NULL)
12906 return;
12907 if (argvars[1].v_type == VAR_UNKNOWN)
12908 sep = (char_u *)" ";
12909 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012910 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012911
12912 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012913
12914 if (sep != NULL)
12915 {
12916 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012917 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012918 ga_append(&ga, NUL);
12919 rettv->vval.v_string = (char_u *)ga.ga_data;
12920 }
12921 else
12922 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012923}
12924
12925/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012926 * "keys()" function
12927 */
12928 static void
12929f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 typval_T *argvars;
12931 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012932{
12933 dict_list(argvars, rettv, 0);
12934}
12935
12936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 * "last_buffer_nr()" function.
12938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012941 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943{
12944 int n = 0;
12945 buf_T *buf;
12946
12947 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12948 if (n < buf->b_fnum)
12949 n = buf->b_fnum;
12950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012952}
12953
12954/*
12955 * "len()" function
12956 */
12957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012958f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012959 typval_T *argvars;
12960 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012961{
12962 switch (argvars[0].v_type)
12963 {
12964 case VAR_STRING:
12965 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012966 rettv->vval.v_number = (varnumber_T)STRLEN(
12967 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012968 break;
12969 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012970 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012971 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012972 case VAR_DICT:
12973 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12974 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012975 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012976 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012977 break;
12978 }
12979}
12980
Bram Moolenaar33570922005-01-25 22:26:29 +000012981static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012982
12983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012984libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012985 typval_T *argvars;
12986 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012987 int type;
12988{
12989#ifdef FEAT_LIBCALL
12990 char_u *string_in;
12991 char_u **string_result;
12992 int nr_result;
12993#endif
12994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012996 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012997 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012998
12999 if (check_restricted() || check_secure())
13000 return;
13001
13002#ifdef FEAT_LIBCALL
13003 /* The first two args must be strings, otherwise its meaningless */
13004 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13005 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013006 string_in = NULL;
13007 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013008 string_in = argvars[2].vval.v_string;
13009 if (type == VAR_NUMBER)
13010 string_result = NULL;
13011 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013013 if (mch_libcall(argvars[0].vval.v_string,
13014 argvars[1].vval.v_string,
13015 string_in,
13016 argvars[2].vval.v_number,
13017 string_result,
13018 &nr_result) == OK
13019 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013020 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013021 }
13022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023}
13024
13025/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013026 * "libcall()" function
13027 */
13028 static void
13029f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013030 typval_T *argvars;
13031 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013032{
13033 libcall_common(argvars, rettv, VAR_STRING);
13034}
13035
13036/*
13037 * "libcallnr()" function
13038 */
13039 static void
13040f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013041 typval_T *argvars;
13042 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013043{
13044 libcall_common(argvars, rettv, VAR_NUMBER);
13045}
13046
13047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 * "line(string)" function
13049 */
13050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013051f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013052 typval_T *argvars;
13053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054{
13055 linenr_T lnum = 0;
13056 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013057 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013059 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060 if (fp != NULL)
13061 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063}
13064
13065/*
13066 * "line2byte(lnum)" function
13067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013069f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013070 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072{
13073#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075#else
13076 linenr_T lnum;
13077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013078 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13083 if (rettv->vval.v_number >= 0)
13084 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085#endif
13086}
13087
13088/*
13089 * "lispindent(lnum)" function
13090 */
13091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013092f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013093 typval_T *argvars;
13094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095{
13096#ifdef FEAT_LISP
13097 pos_T pos;
13098 linenr_T lnum;
13099
13100 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13103 {
13104 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013105 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 curwin->w_cursor = pos;
13107 }
13108 else
13109#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013110 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111}
13112
13113/*
13114 * "localtime()" function
13115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013118 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122}
13123
Bram Moolenaar33570922005-01-25 22:26:29 +000013124static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125
13126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013127get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013128 typval_T *argvars;
13129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130 int exact;
13131{
13132 char_u *keys;
13133 char_u *which;
13134 char_u buf[NUMBUFLEN];
13135 char_u *keys_buf = NULL;
13136 char_u *rhs;
13137 int mode;
13138 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013139 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140
13141 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013142 rettv->v_type = VAR_STRING;
13143 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 if (*keys == NUL)
13147 return;
13148
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013149 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013151 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013152 if (argvars[2].v_type != VAR_UNKNOWN)
13153 abbr = get_tv_number(&argvars[2]);
13154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 else
13156 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013157 if (which == NULL)
13158 return;
13159
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 mode = get_map_mode(&which, 0);
13161
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013162 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013163 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 vim_free(keys_buf);
13165 if (rhs != NULL)
13166 {
13167 ga_init(&ga);
13168 ga.ga_itemsize = 1;
13169 ga.ga_growsize = 40;
13170
13171 while (*rhs != NUL)
13172 ga_concat(&ga, str2special(&rhs, FALSE));
13173
13174 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013175 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 }
13177}
13178
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013179#ifdef FEAT_FLOAT
13180/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013181 * "log()" function
13182 */
13183 static void
13184f_log(argvars, rettv)
13185 typval_T *argvars;
13186 typval_T *rettv;
13187{
13188 float_T f;
13189
13190 rettv->v_type = VAR_FLOAT;
13191 if (get_float_arg(argvars, &f) == OK)
13192 rettv->vval.v_float = log(f);
13193 else
13194 rettv->vval.v_float = 0.0;
13195}
13196
13197/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013198 * "log10()" function
13199 */
13200 static void
13201f_log10(argvars, rettv)
13202 typval_T *argvars;
13203 typval_T *rettv;
13204{
13205 float_T f;
13206
13207 rettv->v_type = VAR_FLOAT;
13208 if (get_float_arg(argvars, &f) == OK)
13209 rettv->vval.v_float = log10(f);
13210 else
13211 rettv->vval.v_float = 0.0;
13212}
13213#endif
13214
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013216 * "map()" function
13217 */
13218 static void
13219f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013220 typval_T *argvars;
13221 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013222{
13223 filter_map(argvars, rettv, TRUE);
13224}
13225
13226/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013227 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 */
13229 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 typval_T *argvars;
13232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013234 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235}
13236
13237/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013238 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 */
13240 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013241f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 typval_T *argvars;
13243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013245 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246}
13247
Bram Moolenaar33570922005-01-25 22:26:29 +000013248static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249
13250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013252 typval_T *argvars;
13253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 int type;
13255{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013256 char_u *str = NULL;
13257 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 char_u *pat;
13259 regmatch_T regmatch;
13260 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013261 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 char_u *save_cpo;
13263 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013264 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013265 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013266 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013267 list_T *l = NULL;
13268 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013269 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013270 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271
13272 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13273 save_cpo = p_cpo;
13274 p_cpo = (char_u *)"";
13275
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013276 rettv->vval.v_number = -1;
13277 if (type == 3)
13278 {
13279 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013280 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013281 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013282 }
13283 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013285 rettv->v_type = VAR_STRING;
13286 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013289 if (argvars[0].v_type == VAR_LIST)
13290 {
13291 if ((l = argvars[0].vval.v_list) == NULL)
13292 goto theend;
13293 li = l->lv_first;
13294 }
13295 else
13296 expr = str = get_tv_string(&argvars[0]);
13297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013298 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13299 if (pat == NULL)
13300 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013301
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013302 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013304 int error = FALSE;
13305
13306 start = get_tv_number_chk(&argvars[2], &error);
13307 if (error)
13308 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013309 if (l != NULL)
13310 {
13311 li = list_find(l, start);
13312 if (li == NULL)
13313 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013314 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013315 }
13316 else
13317 {
13318 if (start < 0)
13319 start = 0;
13320 if (start > (long)STRLEN(str))
13321 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013322 /* When "count" argument is there ignore matches before "start",
13323 * otherwise skip part of the string. Differs when pattern is "^"
13324 * or "\<". */
13325 if (argvars[3].v_type != VAR_UNKNOWN)
13326 startcol = start;
13327 else
13328 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013329 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013330
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013331 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013332 nth = get_tv_number_chk(&argvars[3], &error);
13333 if (error)
13334 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 }
13336
13337 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13338 if (regmatch.regprog != NULL)
13339 {
13340 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013341
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013342 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013343 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013344 if (l != NULL)
13345 {
13346 if (li == NULL)
13347 {
13348 match = FALSE;
13349 break;
13350 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013351 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013352 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013353 if (str == NULL)
13354 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013355 }
13356
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013357 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013358
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013359 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013360 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013361 if (l == NULL && !match)
13362 break;
13363
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013364 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013365 if (l != NULL)
13366 {
13367 li = li->li_next;
13368 ++idx;
13369 }
13370 else
13371 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013372#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013373 startcol = (colnr_T)(regmatch.startp[0]
13374 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013375#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013376 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013377#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013378 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013379 }
13380
13381 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013383 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013384 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013385 int i;
13386
13387 /* return list with matched string and submatches */
13388 for (i = 0; i < NSUBEXP; ++i)
13389 {
13390 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013391 {
13392 if (list_append_string(rettv->vval.v_list,
13393 (char_u *)"", 0) == FAIL)
13394 break;
13395 }
13396 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013397 regmatch.startp[i],
13398 (int)(regmatch.endp[i] - regmatch.startp[i]))
13399 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013400 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013401 }
13402 }
13403 else if (type == 2)
13404 {
13405 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013406 if (l != NULL)
13407 copy_tv(&li->li_tv, rettv);
13408 else
13409 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013411 }
13412 else if (l != NULL)
13413 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 else
13415 {
13416 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 (varnumber_T)(regmatch.startp[0] - str);
13419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013420 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013422 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 }
13424 }
13425 vim_free(regmatch.regprog);
13426 }
13427
13428theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013429 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 p_cpo = save_cpo;
13431}
13432
13433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013434 * "match()" function
13435 */
13436 static void
13437f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013438 typval_T *argvars;
13439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013440{
13441 find_some_match(argvars, rettv, 1);
13442}
13443
13444/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013445 * "matchadd()" function
13446 */
13447 static void
13448f_matchadd(argvars, rettv)
13449 typval_T *argvars;
13450 typval_T *rettv;
13451{
13452#ifdef FEAT_SEARCH_EXTRA
13453 char_u buf[NUMBUFLEN];
13454 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13455 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13456 int prio = 10; /* default priority */
13457 int id = -1;
13458 int error = FALSE;
13459
13460 rettv->vval.v_number = -1;
13461
13462 if (grp == NULL || pat == NULL)
13463 return;
13464 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013465 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013466 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013467 if (argvars[3].v_type != VAR_UNKNOWN)
13468 id = get_tv_number_chk(&argvars[3], &error);
13469 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013470 if (error == TRUE)
13471 return;
13472 if (id >= 1 && id <= 3)
13473 {
13474 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13475 return;
13476 }
13477
13478 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13479#endif
13480}
13481
13482/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013483 * "matcharg()" function
13484 */
13485 static void
13486f_matcharg(argvars, rettv)
13487 typval_T *argvars;
13488 typval_T *rettv;
13489{
13490 if (rettv_list_alloc(rettv) == OK)
13491 {
13492#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013493 int id = get_tv_number(&argvars[0]);
13494 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013495
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013496 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013497 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013498 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13499 {
13500 list_append_string(rettv->vval.v_list,
13501 syn_id2name(m->hlg_id), -1);
13502 list_append_string(rettv->vval.v_list, m->pattern, -1);
13503 }
13504 else
13505 {
13506 list_append_string(rettv->vval.v_list, NUL, -1);
13507 list_append_string(rettv->vval.v_list, NUL, -1);
13508 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013509 }
13510#endif
13511 }
13512}
13513
13514/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013515 * "matchdelete()" function
13516 */
13517 static void
13518f_matchdelete(argvars, rettv)
13519 typval_T *argvars;
13520 typval_T *rettv;
13521{
13522#ifdef FEAT_SEARCH_EXTRA
13523 rettv->vval.v_number = match_delete(curwin,
13524 (int)get_tv_number(&argvars[0]), TRUE);
13525#endif
13526}
13527
13528/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013529 * "matchend()" function
13530 */
13531 static void
13532f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013533 typval_T *argvars;
13534 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013535{
13536 find_some_match(argvars, rettv, 0);
13537}
13538
13539/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013540 * "matchlist()" function
13541 */
13542 static void
13543f_matchlist(argvars, rettv)
13544 typval_T *argvars;
13545 typval_T *rettv;
13546{
13547 find_some_match(argvars, rettv, 3);
13548}
13549
13550/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013551 * "matchstr()" function
13552 */
13553 static void
13554f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013555 typval_T *argvars;
13556 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013557{
13558 find_some_match(argvars, rettv, 2);
13559}
13560
Bram Moolenaar33570922005-01-25 22:26:29 +000013561static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013562
13563 static void
13564max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013565 typval_T *argvars;
13566 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013567 int domax;
13568{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013569 long n = 0;
13570 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013571 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013572
13573 if (argvars[0].v_type == VAR_LIST)
13574 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013575 list_T *l;
13576 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013577
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013578 l = argvars[0].vval.v_list;
13579 if (l != NULL)
13580 {
13581 li = l->lv_first;
13582 if (li != NULL)
13583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013584 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013585 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013586 {
13587 li = li->li_next;
13588 if (li == NULL)
13589 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013591 if (domax ? i > n : i < n)
13592 n = i;
13593 }
13594 }
13595 }
13596 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013597 else if (argvars[0].v_type == VAR_DICT)
13598 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013599 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013600 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013602 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013603
13604 d = argvars[0].vval.v_dict;
13605 if (d != NULL)
13606 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013607 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013608 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013610 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013612 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013614 if (first)
13615 {
13616 n = i;
13617 first = FALSE;
13618 }
13619 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013620 n = i;
13621 }
13622 }
13623 }
13624 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013625 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013626 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013628}
13629
13630/*
13631 * "max()" function
13632 */
13633 static void
13634f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *argvars;
13636 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013637{
13638 max_min(argvars, rettv, TRUE);
13639}
13640
13641/*
13642 * "min()" function
13643 */
13644 static void
13645f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 typval_T *argvars;
13647 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013648{
13649 max_min(argvars, rettv, FALSE);
13650}
13651
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013652static int mkdir_recurse __ARGS((char_u *dir, int prot));
13653
13654/*
13655 * Create the directory in which "dir" is located, and higher levels when
13656 * needed.
13657 */
13658 static int
13659mkdir_recurse(dir, prot)
13660 char_u *dir;
13661 int prot;
13662{
13663 char_u *p;
13664 char_u *updir;
13665 int r = FAIL;
13666
13667 /* Get end of directory name in "dir".
13668 * We're done when it's "/" or "c:/". */
13669 p = gettail_sep(dir);
13670 if (p <= get_past_head(dir))
13671 return OK;
13672
13673 /* If the directory exists we're done. Otherwise: create it.*/
13674 updir = vim_strnsave(dir, (int)(p - dir));
13675 if (updir == NULL)
13676 return FAIL;
13677 if (mch_isdir(updir))
13678 r = OK;
13679 else if (mkdir_recurse(updir, prot) == OK)
13680 r = vim_mkdir_emsg(updir, prot);
13681 vim_free(updir);
13682 return r;
13683}
13684
13685#ifdef vim_mkdir
13686/*
13687 * "mkdir()" function
13688 */
13689 static void
13690f_mkdir(argvars, rettv)
13691 typval_T *argvars;
13692 typval_T *rettv;
13693{
13694 char_u *dir;
13695 char_u buf[NUMBUFLEN];
13696 int prot = 0755;
13697
13698 rettv->vval.v_number = FAIL;
13699 if (check_restricted() || check_secure())
13700 return;
13701
13702 dir = get_tv_string_buf(&argvars[0], buf);
13703 if (argvars[1].v_type != VAR_UNKNOWN)
13704 {
13705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 prot = get_tv_number_chk(&argvars[2], NULL);
13707 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013708 mkdir_recurse(dir, prot);
13709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013710 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013711}
13712#endif
13713
Bram Moolenaar0d660222005-01-07 21:51:51 +000013714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 * "mode()" function
13716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013719 typval_T *argvars;
13720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013722 char_u buf[3];
13723
13724 buf[1] = NUL;
13725 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726
13727#ifdef FEAT_VISUAL
13728 if (VIsual_active)
13729 {
13730 if (VIsual_select)
13731 buf[0] = VIsual_mode + 's' - 'v';
13732 else
13733 buf[0] = VIsual_mode;
13734 }
13735 else
13736#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013737 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13738 || State == CONFIRM)
13739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013741 if (State == ASKMORE)
13742 buf[1] = 'm';
13743 else if (State == CONFIRM)
13744 buf[1] = '?';
13745 }
13746 else if (State == EXTERNCMD)
13747 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 else if (State & INSERT)
13749 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013750#ifdef FEAT_VREPLACE
13751 if (State & VREPLACE_FLAG)
13752 {
13753 buf[0] = 'R';
13754 buf[1] = 'v';
13755 }
13756 else
13757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 if (State & REPLACE_FLAG)
13759 buf[0] = 'R';
13760 else
13761 buf[0] = 'i';
13762 }
13763 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013766 if (exmode_active)
13767 buf[1] = 'v';
13768 }
13769 else if (exmode_active)
13770 {
13771 buf[0] = 'c';
13772 buf[1] = 'e';
13773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013777 if (finish_op)
13778 buf[1] = 'o';
13779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013781 /* Clear out the minor mode when the argument is not a non-zero number or
13782 * non-empty string. */
13783 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013784 buf[1] = NUL;
13785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013786 rettv->vval.v_string = vim_strsave(buf);
13787 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788}
13789
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013790#ifdef FEAT_MZSCHEME
13791/*
13792 * "mzeval()" function
13793 */
13794 static void
13795f_mzeval(argvars, rettv)
13796 typval_T *argvars;
13797 typval_T *rettv;
13798{
13799 char_u *str;
13800 char_u buf[NUMBUFLEN];
13801
13802 str = get_tv_string_buf(&argvars[0], buf);
13803 do_mzeval(str, rettv);
13804}
13805#endif
13806
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013808 * "nextnonblank()" function
13809 */
13810 static void
13811f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013812 typval_T *argvars;
13813 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013814{
13815 linenr_T lnum;
13816
13817 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013819 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013820 {
13821 lnum = 0;
13822 break;
13823 }
13824 if (*skipwhite(ml_get(lnum)) != NUL)
13825 break;
13826 }
13827 rettv->vval.v_number = lnum;
13828}
13829
13830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 * "nr2char()" function
13832 */
13833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013835 typval_T *argvars;
13836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837{
13838 char_u buf[NUMBUFLEN];
13839
13840#ifdef FEAT_MBYTE
13841 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013842 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 else
13844#endif
13845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013846 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847 buf[1] = NUL;
13848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013849 rettv->v_type = VAR_STRING;
13850 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013851}
13852
13853/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013854 * "pathshorten()" function
13855 */
13856 static void
13857f_pathshorten(argvars, rettv)
13858 typval_T *argvars;
13859 typval_T *rettv;
13860{
13861 char_u *p;
13862
13863 rettv->v_type = VAR_STRING;
13864 p = get_tv_string_chk(&argvars[0]);
13865 if (p == NULL)
13866 rettv->vval.v_string = NULL;
13867 else
13868 {
13869 p = vim_strsave(p);
13870 rettv->vval.v_string = p;
13871 if (p != NULL)
13872 shorten_dir(p);
13873 }
13874}
13875
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013876#ifdef FEAT_FLOAT
13877/*
13878 * "pow()" function
13879 */
13880 static void
13881f_pow(argvars, rettv)
13882 typval_T *argvars;
13883 typval_T *rettv;
13884{
13885 float_T fx, fy;
13886
13887 rettv->v_type = VAR_FLOAT;
13888 if (get_float_arg(argvars, &fx) == OK
13889 && get_float_arg(&argvars[1], &fy) == OK)
13890 rettv->vval.v_float = pow(fx, fy);
13891 else
13892 rettv->vval.v_float = 0.0;
13893}
13894#endif
13895
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013896/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013897 * "prevnonblank()" function
13898 */
13899 static void
13900f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013901 typval_T *argvars;
13902 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013903{
13904 linenr_T lnum;
13905
13906 lnum = get_tv_lnum(argvars);
13907 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13908 lnum = 0;
13909 else
13910 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13911 --lnum;
13912 rettv->vval.v_number = lnum;
13913}
13914
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013915#ifdef HAVE_STDARG_H
13916/* This dummy va_list is here because:
13917 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13918 * - locally in the function results in a "used before set" warning
13919 * - using va_start() to initialize it gives "function with fixed args" error */
13920static va_list ap;
13921#endif
13922
Bram Moolenaar8c711452005-01-14 21:53:12 +000013923/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013924 * "printf()" function
13925 */
13926 static void
13927f_printf(argvars, rettv)
13928 typval_T *argvars;
13929 typval_T *rettv;
13930{
13931 rettv->v_type = VAR_STRING;
13932 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013933#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013934 {
13935 char_u buf[NUMBUFLEN];
13936 int len;
13937 char_u *s;
13938 int saved_did_emsg = did_emsg;
13939 char *fmt;
13940
13941 /* Get the required length, allocate the buffer and do it for real. */
13942 did_emsg = FALSE;
13943 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013944 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013945 if (!did_emsg)
13946 {
13947 s = alloc(len + 1);
13948 if (s != NULL)
13949 {
13950 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013951 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013952 }
13953 }
13954 did_emsg |= saved_did_emsg;
13955 }
13956#endif
13957}
13958
13959/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013960 * "pumvisible()" function
13961 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013962 static void
13963f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013964 typval_T *argvars UNUSED;
13965 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013966{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013967#ifdef FEAT_INS_EXPAND
13968 if (pum_visible())
13969 rettv->vval.v_number = 1;
13970#endif
13971}
13972
13973/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013974 * "range()" function
13975 */
13976 static void
13977f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013978 typval_T *argvars;
13979 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013980{
13981 long start;
13982 long end;
13983 long stride = 1;
13984 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013988 if (argvars[1].v_type == VAR_UNKNOWN)
13989 {
13990 end = start - 1;
13991 start = 0;
13992 }
13993 else
13994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013998 }
13999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014000 if (error)
14001 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014002 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014003 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014004 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014005 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014006 else
14007 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014008 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014009 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014010 if (list_append_number(rettv->vval.v_list,
14011 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014012 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014013 }
14014}
14015
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014016/*
14017 * "readfile()" function
14018 */
14019 static void
14020f_readfile(argvars, rettv)
14021 typval_T *argvars;
14022 typval_T *rettv;
14023{
14024 int binary = FALSE;
14025 char_u *fname;
14026 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014027 listitem_T *li;
14028#define FREAD_SIZE 200 /* optimized for text lines */
14029 char_u buf[FREAD_SIZE];
14030 int readlen; /* size of last fread() */
14031 int buflen; /* nr of valid chars in buf[] */
14032 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14033 int tolist; /* first byte in buf[] still to be put in list */
14034 int chop; /* how many CR to chop off */
14035 char_u *prev = NULL; /* previously read bytes, if any */
14036 int prevlen = 0; /* length of "prev" if not NULL */
14037 char_u *s;
14038 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014039 long maxline = MAXLNUM;
14040 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014041
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014042 if (argvars[1].v_type != VAR_UNKNOWN)
14043 {
14044 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14045 binary = TRUE;
14046 if (argvars[2].v_type != VAR_UNKNOWN)
14047 maxline = get_tv_number(&argvars[2]);
14048 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014049
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014050 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014051 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014052
14053 /* Always open the file in binary mode, library functions have a mind of
14054 * their own about CR-LF conversion. */
14055 fname = get_tv_string(&argvars[0]);
14056 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14057 {
14058 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14059 return;
14060 }
14061
14062 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014063 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014064 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014065 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014066 buflen = filtd + readlen;
14067 tolist = 0;
14068 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14069 {
14070 if (buf[filtd] == '\n' || readlen <= 0)
14071 {
14072 /* Only when in binary mode add an empty list item when the
14073 * last line ends in a '\n'. */
14074 if (!binary && readlen == 0 && filtd == 0)
14075 break;
14076
14077 /* Found end-of-line or end-of-file: add a text line to the
14078 * list. */
14079 chop = 0;
14080 if (!binary)
14081 while (filtd - chop - 1 >= tolist
14082 && buf[filtd - chop - 1] == '\r')
14083 ++chop;
14084 len = filtd - tolist - chop;
14085 if (prev == NULL)
14086 s = vim_strnsave(buf + tolist, len);
14087 else
14088 {
14089 s = alloc((unsigned)(prevlen + len + 1));
14090 if (s != NULL)
14091 {
14092 mch_memmove(s, prev, prevlen);
14093 vim_free(prev);
14094 prev = NULL;
14095 mch_memmove(s + prevlen, buf + tolist, len);
14096 s[prevlen + len] = NUL;
14097 }
14098 }
14099 tolist = filtd + 1;
14100
14101 li = listitem_alloc();
14102 if (li == NULL)
14103 {
14104 vim_free(s);
14105 break;
14106 }
14107 li->li_tv.v_type = VAR_STRING;
14108 li->li_tv.v_lock = 0;
14109 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014110 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014111
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014112 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014113 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014114 if (readlen <= 0)
14115 break;
14116 }
14117 else if (buf[filtd] == NUL)
14118 buf[filtd] = '\n';
14119 }
14120 if (readlen <= 0)
14121 break;
14122
14123 if (tolist == 0)
14124 {
14125 /* "buf" is full, need to move text to an allocated buffer */
14126 if (prev == NULL)
14127 {
14128 prev = vim_strnsave(buf, buflen);
14129 prevlen = buflen;
14130 }
14131 else
14132 {
14133 s = alloc((unsigned)(prevlen + buflen));
14134 if (s != NULL)
14135 {
14136 mch_memmove(s, prev, prevlen);
14137 mch_memmove(s + prevlen, buf, buflen);
14138 vim_free(prev);
14139 prev = s;
14140 prevlen += buflen;
14141 }
14142 }
14143 filtd = 0;
14144 }
14145 else
14146 {
14147 mch_memmove(buf, buf + tolist, buflen - tolist);
14148 filtd -= tolist;
14149 }
14150 }
14151
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014152 /*
14153 * For a negative line count use only the lines at the end of the file,
14154 * free the rest.
14155 */
14156 if (maxline < 0)
14157 while (cnt > -maxline)
14158 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014159 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014160 --cnt;
14161 }
14162
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014163 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014164 fclose(fd);
14165}
14166
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014167#if defined(FEAT_RELTIME)
14168static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14169
14170/*
14171 * Convert a List to proftime_T.
14172 * Return FAIL when there is something wrong.
14173 */
14174 static int
14175list2proftime(arg, tm)
14176 typval_T *arg;
14177 proftime_T *tm;
14178{
14179 long n1, n2;
14180 int error = FALSE;
14181
14182 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14183 || arg->vval.v_list->lv_len != 2)
14184 return FAIL;
14185 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14186 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14187# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014188 tm->HighPart = n1;
14189 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014190# else
14191 tm->tv_sec = n1;
14192 tm->tv_usec = n2;
14193# endif
14194 return error ? FAIL : OK;
14195}
14196#endif /* FEAT_RELTIME */
14197
14198/*
14199 * "reltime()" function
14200 */
14201 static void
14202f_reltime(argvars, rettv)
14203 typval_T *argvars;
14204 typval_T *rettv;
14205{
14206#ifdef FEAT_RELTIME
14207 proftime_T res;
14208 proftime_T start;
14209
14210 if (argvars[0].v_type == VAR_UNKNOWN)
14211 {
14212 /* No arguments: get current time. */
14213 profile_start(&res);
14214 }
14215 else if (argvars[1].v_type == VAR_UNKNOWN)
14216 {
14217 if (list2proftime(&argvars[0], &res) == FAIL)
14218 return;
14219 profile_end(&res);
14220 }
14221 else
14222 {
14223 /* Two arguments: compute the difference. */
14224 if (list2proftime(&argvars[0], &start) == FAIL
14225 || list2proftime(&argvars[1], &res) == FAIL)
14226 return;
14227 profile_sub(&res, &start);
14228 }
14229
14230 if (rettv_list_alloc(rettv) == OK)
14231 {
14232 long n1, n2;
14233
14234# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014235 n1 = res.HighPart;
14236 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014237# else
14238 n1 = res.tv_sec;
14239 n2 = res.tv_usec;
14240# endif
14241 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14242 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14243 }
14244#endif
14245}
14246
14247/*
14248 * "reltimestr()" function
14249 */
14250 static void
14251f_reltimestr(argvars, rettv)
14252 typval_T *argvars;
14253 typval_T *rettv;
14254{
14255#ifdef FEAT_RELTIME
14256 proftime_T tm;
14257#endif
14258
14259 rettv->v_type = VAR_STRING;
14260 rettv->vval.v_string = NULL;
14261#ifdef FEAT_RELTIME
14262 if (list2proftime(&argvars[0], &tm) == OK)
14263 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14264#endif
14265}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014266
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14268static void make_connection __ARGS((void));
14269static int check_connection __ARGS((void));
14270
14271 static void
14272make_connection()
14273{
14274 if (X_DISPLAY == NULL
14275# ifdef FEAT_GUI
14276 && !gui.in_use
14277# endif
14278 )
14279 {
14280 x_force_connect = TRUE;
14281 setup_term_clip();
14282 x_force_connect = FALSE;
14283 }
14284}
14285
14286 static int
14287check_connection()
14288{
14289 make_connection();
14290 if (X_DISPLAY == NULL)
14291 {
14292 EMSG(_("E240: No connection to Vim server"));
14293 return FAIL;
14294 }
14295 return OK;
14296}
14297#endif
14298
14299#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014300static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014301
14302 static void
14303remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014304 typval_T *argvars;
14305 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014306 int expr;
14307{
14308 char_u *server_name;
14309 char_u *keys;
14310 char_u *r = NULL;
14311 char_u buf[NUMBUFLEN];
14312# ifdef WIN32
14313 HWND w;
14314# else
14315 Window w;
14316# endif
14317
14318 if (check_restricted() || check_secure())
14319 return;
14320
14321# ifdef FEAT_X11
14322 if (check_connection() == FAIL)
14323 return;
14324# endif
14325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014326 server_name = get_tv_string_chk(&argvars[0]);
14327 if (server_name == NULL)
14328 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014329 keys = get_tv_string_buf(&argvars[1], buf);
14330# ifdef WIN32
14331 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14332# else
14333 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14334 < 0)
14335# endif
14336 {
14337 if (r != NULL)
14338 EMSG(r); /* sending worked but evaluation failed */
14339 else
14340 EMSG2(_("E241: Unable to send to %s"), server_name);
14341 return;
14342 }
14343
14344 rettv->vval.v_string = r;
14345
14346 if (argvars[2].v_type != VAR_UNKNOWN)
14347 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014348 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014349 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014351
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014352 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014353 v.di_tv.v_type = VAR_STRING;
14354 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 idvar = get_tv_string_chk(&argvars[2]);
14356 if (idvar != NULL)
14357 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014358 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014359 }
14360}
14361#endif
14362
14363/*
14364 * "remote_expr()" function
14365 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014366 static void
14367f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014368 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014369 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014370{
14371 rettv->v_type = VAR_STRING;
14372 rettv->vval.v_string = NULL;
14373#ifdef FEAT_CLIENTSERVER
14374 remote_common(argvars, rettv, TRUE);
14375#endif
14376}
14377
14378/*
14379 * "remote_foreground()" function
14380 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014381 static void
14382f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014383 typval_T *argvars UNUSED;
14384 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014386#ifdef FEAT_CLIENTSERVER
14387# ifdef WIN32
14388 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 {
14390 char_u *server_name = get_tv_string_chk(&argvars[0]);
14391
14392 if (server_name != NULL)
14393 serverForeground(server_name);
14394 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014395# else
14396 /* Send a foreground() expression to the server. */
14397 argvars[1].v_type = VAR_STRING;
14398 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14399 argvars[2].v_type = VAR_UNKNOWN;
14400 remote_common(argvars, rettv, TRUE);
14401 vim_free(argvars[1].vval.v_string);
14402# endif
14403#endif
14404}
14405
Bram Moolenaar0d660222005-01-07 21:51:51 +000014406 static void
14407f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014409 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014410{
14411#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014412 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014413 char_u *s = NULL;
14414# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014415 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014416# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014418
14419 if (check_restricted() || check_secure())
14420 {
14421 rettv->vval.v_number = -1;
14422 return;
14423 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424 serverid = get_tv_string_chk(&argvars[0]);
14425 if (serverid == NULL)
14426 {
14427 rettv->vval.v_number = -1;
14428 return; /* type error; errmsg already given */
14429 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014431 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014432 if (n == 0)
14433 rettv->vval.v_number = -1;
14434 else
14435 {
14436 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14437 rettv->vval.v_number = (s != NULL);
14438 }
14439# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014440 if (check_connection() == FAIL)
14441 return;
14442
14443 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445# endif
14446
14447 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 char_u *retvar;
14450
Bram Moolenaar33570922005-01-25 22:26:29 +000014451 v.di_tv.v_type = VAR_STRING;
14452 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 retvar = get_tv_string_chk(&argvars[1]);
14454 if (retvar != NULL)
14455 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014456 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014457 }
14458#else
14459 rettv->vval.v_number = -1;
14460#endif
14461}
14462
Bram Moolenaar0d660222005-01-07 21:51:51 +000014463 static void
14464f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014465 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014466 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014467{
14468 char_u *r = NULL;
14469
14470#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014471 char_u *serverid = get_tv_string_chk(&argvars[0]);
14472
14473 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014474 {
14475# ifdef WIN32
14476 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014477 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014478
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014479 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480 if (n != 0)
14481 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14482 if (r == NULL)
14483# else
14484 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014486# endif
14487 EMSG(_("E277: Unable to read a server reply"));
14488 }
14489#endif
14490 rettv->v_type = VAR_STRING;
14491 rettv->vval.v_string = r;
14492}
14493
14494/*
14495 * "remote_send()" function
14496 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014497 static void
14498f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014499 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014501{
14502 rettv->v_type = VAR_STRING;
14503 rettv->vval.v_string = NULL;
14504#ifdef FEAT_CLIENTSERVER
14505 remote_common(argvars, rettv, FALSE);
14506#endif
14507}
14508
14509/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014510 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014511 */
14512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014513f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014514 typval_T *argvars;
14515 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014516{
Bram Moolenaar33570922005-01-25 22:26:29 +000014517 list_T *l;
14518 listitem_T *item, *item2;
14519 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014520 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014521 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014522 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014523 dict_T *d;
14524 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014525
Bram Moolenaar8c711452005-01-14 21:53:12 +000014526 if (argvars[0].v_type == VAR_DICT)
14527 {
14528 if (argvars[2].v_type != VAR_UNKNOWN)
14529 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014530 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014531 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 key = get_tv_string_chk(&argvars[1]);
14534 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014536 di = dict_find(d, key, -1);
14537 if (di == NULL)
14538 EMSG2(_(e_dictkey), key);
14539 else
14540 {
14541 *rettv = di->di_tv;
14542 init_tv(&di->di_tv);
14543 dictitem_remove(d, di);
14544 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014545 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014546 }
14547 }
14548 else if (argvars[0].v_type != VAR_LIST)
14549 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014550 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014551 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014553 int error = FALSE;
14554
14555 idx = get_tv_number_chk(&argvars[1], &error);
14556 if (error)
14557 ; /* type error: do nothing, errmsg already given */
14558 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014559 EMSGN(_(e_listidx), idx);
14560 else
14561 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014562 if (argvars[2].v_type == VAR_UNKNOWN)
14563 {
14564 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014565 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014566 *rettv = item->li_tv;
14567 vim_free(item);
14568 }
14569 else
14570 {
14571 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014572 end = get_tv_number_chk(&argvars[2], &error);
14573 if (error)
14574 ; /* type error: do nothing */
14575 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014576 EMSGN(_(e_listidx), end);
14577 else
14578 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014579 int cnt = 0;
14580
14581 for (li = item; li != NULL; li = li->li_next)
14582 {
14583 ++cnt;
14584 if (li == item2)
14585 break;
14586 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014587 if (li == NULL) /* didn't find "item2" after "item" */
14588 EMSG(_(e_invrange));
14589 else
14590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014591 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014592 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014593 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014594 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014595 l->lv_first = item;
14596 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014597 item->li_prev = NULL;
14598 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014599 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014600 }
14601 }
14602 }
14603 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014604 }
14605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606}
14607
14608/*
14609 * "rename({from}, {to})" function
14610 */
14611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014613 typval_T *argvars;
14614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615{
14616 char_u buf[NUMBUFLEN];
14617
14618 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14622 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623}
14624
14625/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014626 * "repeat()" function
14627 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014628 static void
14629f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014630 typval_T *argvars;
14631 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014632{
14633 char_u *p;
14634 int n;
14635 int slen;
14636 int len;
14637 char_u *r;
14638 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014639
14640 n = get_tv_number(&argvars[1]);
14641 if (argvars[0].v_type == VAR_LIST)
14642 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014643 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014644 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014645 if (list_extend(rettv->vval.v_list,
14646 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014647 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014648 }
14649 else
14650 {
14651 p = get_tv_string(&argvars[0]);
14652 rettv->v_type = VAR_STRING;
14653 rettv->vval.v_string = NULL;
14654
14655 slen = (int)STRLEN(p);
14656 len = slen * n;
14657 if (len <= 0)
14658 return;
14659
14660 r = alloc(len + 1);
14661 if (r != NULL)
14662 {
14663 for (i = 0; i < n; i++)
14664 mch_memmove(r + i * slen, p, (size_t)slen);
14665 r[len] = NUL;
14666 }
14667
14668 rettv->vval.v_string = r;
14669 }
14670}
14671
14672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 * "resolve()" function
14674 */
14675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014677 typval_T *argvars;
14678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679{
14680 char_u *p;
14681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683#ifdef FEAT_SHORTCUT
14684 {
14685 char_u *v = NULL;
14686
14687 v = mch_resolve_shortcut(p);
14688 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014691 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692 }
14693#else
14694# ifdef HAVE_READLINK
14695 {
14696 char_u buf[MAXPATHL + 1];
14697 char_u *cpy;
14698 int len;
14699 char_u *remain = NULL;
14700 char_u *q;
14701 int is_relative_to_current = FALSE;
14702 int has_trailing_pathsep = FALSE;
14703 int limit = 100;
14704
14705 p = vim_strsave(p);
14706
14707 if (p[0] == '.' && (vim_ispathsep(p[1])
14708 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14709 is_relative_to_current = TRUE;
14710
14711 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014712 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 has_trailing_pathsep = TRUE;
14714
14715 q = getnextcomp(p);
14716 if (*q != NUL)
14717 {
14718 /* Separate the first path component in "p", and keep the
14719 * remainder (beginning with the path separator). */
14720 remain = vim_strsave(q - 1);
14721 q[-1] = NUL;
14722 }
14723
14724 for (;;)
14725 {
14726 for (;;)
14727 {
14728 len = readlink((char *)p, (char *)buf, MAXPATHL);
14729 if (len <= 0)
14730 break;
14731 buf[len] = NUL;
14732
14733 if (limit-- == 0)
14734 {
14735 vim_free(p);
14736 vim_free(remain);
14737 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014738 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739 goto fail;
14740 }
14741
14742 /* Ensure that the result will have a trailing path separator
14743 * if the argument has one. */
14744 if (remain == NULL && has_trailing_pathsep)
14745 add_pathsep(buf);
14746
14747 /* Separate the first path component in the link value and
14748 * concatenate the remainders. */
14749 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14750 if (*q != NUL)
14751 {
14752 if (remain == NULL)
14753 remain = vim_strsave(q - 1);
14754 else
14755 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014756 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 if (cpy != NULL)
14758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 vim_free(remain);
14760 remain = cpy;
14761 }
14762 }
14763 q[-1] = NUL;
14764 }
14765
14766 q = gettail(p);
14767 if (q > p && *q == NUL)
14768 {
14769 /* Ignore trailing path separator. */
14770 q[-1] = NUL;
14771 q = gettail(p);
14772 }
14773 if (q > p && !mch_isFullName(buf))
14774 {
14775 /* symlink is relative to directory of argument */
14776 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14777 if (cpy != NULL)
14778 {
14779 STRCPY(cpy, p);
14780 STRCPY(gettail(cpy), buf);
14781 vim_free(p);
14782 p = cpy;
14783 }
14784 }
14785 else
14786 {
14787 vim_free(p);
14788 p = vim_strsave(buf);
14789 }
14790 }
14791
14792 if (remain == NULL)
14793 break;
14794
14795 /* Append the first path component of "remain" to "p". */
14796 q = getnextcomp(remain + 1);
14797 len = q - remain - (*q != NUL);
14798 cpy = vim_strnsave(p, STRLEN(p) + len);
14799 if (cpy != NULL)
14800 {
14801 STRNCAT(cpy, remain, len);
14802 vim_free(p);
14803 p = cpy;
14804 }
14805 /* Shorten "remain". */
14806 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014807 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808 else
14809 {
14810 vim_free(remain);
14811 remain = NULL;
14812 }
14813 }
14814
14815 /* If the result is a relative path name, make it explicitly relative to
14816 * the current directory if and only if the argument had this form. */
14817 if (!vim_ispathsep(*p))
14818 {
14819 if (is_relative_to_current
14820 && *p != NUL
14821 && !(p[0] == '.'
14822 && (p[1] == NUL
14823 || vim_ispathsep(p[1])
14824 || (p[1] == '.'
14825 && (p[2] == NUL
14826 || vim_ispathsep(p[2]))))))
14827 {
14828 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014829 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830 if (cpy != NULL)
14831 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832 vim_free(p);
14833 p = cpy;
14834 }
14835 }
14836 else if (!is_relative_to_current)
14837 {
14838 /* Strip leading "./". */
14839 q = p;
14840 while (q[0] == '.' && vim_ispathsep(q[1]))
14841 q += 2;
14842 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014843 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 }
14845 }
14846
14847 /* Ensure that the result will have no trailing path separator
14848 * if the argument had none. But keep "/" or "//". */
14849 if (!has_trailing_pathsep)
14850 {
14851 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014852 if (after_pathsep(p, q))
14853 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 }
14855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014856 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857 }
14858# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014859 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860# endif
14861#endif
14862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014863 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864
14865#ifdef HAVE_READLINK
14866fail:
14867#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869}
14870
14871/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014872 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 */
14874 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014875f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014876 typval_T *argvars;
14877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878{
Bram Moolenaar33570922005-01-25 22:26:29 +000014879 list_T *l;
14880 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881
Bram Moolenaar0d660222005-01-07 21:51:51 +000014882 if (argvars[0].v_type != VAR_LIST)
14883 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014884 else if ((l = argvars[0].vval.v_list) != NULL
14885 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014886 {
14887 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014888 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014889 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014890 while (li != NULL)
14891 {
14892 ni = li->li_prev;
14893 list_append(l, li);
14894 li = ni;
14895 }
14896 rettv->vval.v_list = l;
14897 rettv->v_type = VAR_LIST;
14898 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014899 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901}
14902
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014903#define SP_NOMOVE 0x01 /* don't move cursor */
14904#define SP_REPEAT 0x02 /* repeat to find outer pair */
14905#define SP_RETCOUNT 0x04 /* return matchcount */
14906#define SP_SETPCMARK 0x08 /* set previous context mark */
14907#define SP_START 0x10 /* accept match at start position */
14908#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14909#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014910
Bram Moolenaar33570922005-01-25 22:26:29 +000014911static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014912
14913/*
14914 * Get flags for a search function.
14915 * Possibly sets "p_ws".
14916 * Returns BACKWARD, FORWARD or zero (for an error).
14917 */
14918 static int
14919get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014920 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014921 int *flagsp;
14922{
14923 int dir = FORWARD;
14924 char_u *flags;
14925 char_u nbuf[NUMBUFLEN];
14926 int mask;
14927
14928 if (varp->v_type != VAR_UNKNOWN)
14929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014930 flags = get_tv_string_buf_chk(varp, nbuf);
14931 if (flags == NULL)
14932 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014933 while (*flags != NUL)
14934 {
14935 switch (*flags)
14936 {
14937 case 'b': dir = BACKWARD; break;
14938 case 'w': p_ws = TRUE; break;
14939 case 'W': p_ws = FALSE; break;
14940 default: mask = 0;
14941 if (flagsp != NULL)
14942 switch (*flags)
14943 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014944 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014945 case 'e': mask = SP_END; break;
14946 case 'm': mask = SP_RETCOUNT; break;
14947 case 'n': mask = SP_NOMOVE; break;
14948 case 'p': mask = SP_SUBPAT; break;
14949 case 'r': mask = SP_REPEAT; break;
14950 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014951 }
14952 if (mask == 0)
14953 {
14954 EMSG2(_(e_invarg2), flags);
14955 dir = 0;
14956 }
14957 else
14958 *flagsp |= mask;
14959 }
14960 if (dir == 0)
14961 break;
14962 ++flags;
14963 }
14964 }
14965 return dir;
14966}
14967
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014969 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014971 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014972search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014973 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014974 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014975 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014977 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978 char_u *pat;
14979 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014980 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981 int save_p_ws = p_ws;
14982 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014983 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014984 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014985 proftime_T tm;
14986#ifdef FEAT_RELTIME
14987 long time_limit = 0;
14988#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014989 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014990 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014992 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014993 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014994 if (dir == 0)
14995 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014996 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014997 if (flags & SP_START)
14998 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014999 if (flags & SP_END)
15000 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015001
Bram Moolenaar76929292008-01-06 19:07:36 +000015002 /* Optional arguments: line number to stop searching and timeout. */
15003 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015004 {
15005 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15006 if (lnum_stop < 0)
15007 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015008#ifdef FEAT_RELTIME
15009 if (argvars[3].v_type != VAR_UNKNOWN)
15010 {
15011 time_limit = get_tv_number_chk(&argvars[3], NULL);
15012 if (time_limit < 0)
15013 goto theend;
15014 }
15015#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015016 }
15017
Bram Moolenaar76929292008-01-06 19:07:36 +000015018#ifdef FEAT_RELTIME
15019 /* Set the time limit, if there is one. */
15020 profile_setlimit(time_limit, &tm);
15021#endif
15022
Bram Moolenaar231334e2005-07-25 20:46:57 +000015023 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015024 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015025 * Check to make sure only those flags are set.
15026 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15027 * flags cannot be set. Check for that condition also.
15028 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015029 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015030 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015033 goto theend;
15034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015036 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015037 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015038 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015039 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015041 if (flags & SP_SUBPAT)
15042 retval = subpatnum;
15043 else
15044 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015045 if (flags & SP_SETPCMARK)
15046 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015048 if (match_pos != NULL)
15049 {
15050 /* Store the match cursor position */
15051 match_pos->lnum = pos.lnum;
15052 match_pos->col = pos.col + 1;
15053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 /* "/$" will put the cursor after the end of the line, may need to
15055 * correct that here */
15056 check_cursor();
15057 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015058
15059 /* If 'n' flag is used: restore cursor position. */
15060 if (flags & SP_NOMOVE)
15061 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015062 else
15063 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015064theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015066
15067 return retval;
15068}
15069
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015070#ifdef FEAT_FLOAT
15071/*
15072 * "round({float})" function
15073 */
15074 static void
15075f_round(argvars, rettv)
15076 typval_T *argvars;
15077 typval_T *rettv;
15078{
15079 float_T f;
15080
15081 rettv->v_type = VAR_FLOAT;
15082 if (get_float_arg(argvars, &f) == OK)
15083 /* round() is not in C90, use ceil() or floor() instead. */
15084 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15085 else
15086 rettv->vval.v_float = 0.0;
15087}
15088#endif
15089
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015090/*
15091 * "search()" function
15092 */
15093 static void
15094f_search(argvars, rettv)
15095 typval_T *argvars;
15096 typval_T *rettv;
15097{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015098 int flags = 0;
15099
15100 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101}
15102
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015104 * "searchdecl()" function
15105 */
15106 static void
15107f_searchdecl(argvars, rettv)
15108 typval_T *argvars;
15109 typval_T *rettv;
15110{
15111 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015112 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015113 int error = FALSE;
15114 char_u *name;
15115
15116 rettv->vval.v_number = 1; /* default: FAIL */
15117
15118 name = get_tv_string_chk(&argvars[0]);
15119 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015120 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015121 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015122 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15123 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15124 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015125 if (!error && name != NULL)
15126 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015127 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015128}
15129
15130/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015131 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015133 static int
15134searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015136 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137{
15138 char_u *spat, *mpat, *epat;
15139 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 int dir;
15142 int flags = 0;
15143 char_u nbuf1[NUMBUFLEN];
15144 char_u nbuf2[NUMBUFLEN];
15145 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015146 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015147 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015148 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015151 spat = get_tv_string_chk(&argvars[0]);
15152 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15153 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15154 if (spat == NULL || mpat == NULL || epat == NULL)
15155 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 /* Handle the optional fourth argument: flags */
15158 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015159 if (dir == 0)
15160 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015161
15162 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015163 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15164 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015165 if ((flags & (SP_END | SP_SUBPAT)) != 0
15166 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015167 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015168 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015169 goto theend;
15170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015172 /* Using 'r' implies 'W', otherwise it doesn't work. */
15173 if (flags & SP_REPEAT)
15174 p_ws = FALSE;
15175
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015176 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015177 if (argvars[3].v_type == VAR_UNKNOWN
15178 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 skip = (char_u *)"";
15180 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015182 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015183 if (argvars[5].v_type != VAR_UNKNOWN)
15184 {
15185 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15186 if (lnum_stop < 0)
15187 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015188#ifdef FEAT_RELTIME
15189 if (argvars[6].v_type != VAR_UNKNOWN)
15190 {
15191 time_limit = get_tv_number_chk(&argvars[6], NULL);
15192 if (time_limit < 0)
15193 goto theend;
15194 }
15195#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015196 }
15197 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015198 if (skip == NULL)
15199 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015201 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015202 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015203
15204theend:
15205 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015206
15207 return retval;
15208}
15209
15210/*
15211 * "searchpair()" function
15212 */
15213 static void
15214f_searchpair(argvars, rettv)
15215 typval_T *argvars;
15216 typval_T *rettv;
15217{
15218 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15219}
15220
15221/*
15222 * "searchpairpos()" function
15223 */
15224 static void
15225f_searchpairpos(argvars, rettv)
15226 typval_T *argvars;
15227 typval_T *rettv;
15228{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015229 pos_T match_pos;
15230 int lnum = 0;
15231 int col = 0;
15232
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015233 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015234 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015235
15236 if (searchpair_cmn(argvars, &match_pos) > 0)
15237 {
15238 lnum = match_pos.lnum;
15239 col = match_pos.col;
15240 }
15241
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015242 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15243 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015244}
15245
15246/*
15247 * Search for a start/middle/end thing.
15248 * Used by searchpair(), see its documentation for the details.
15249 * Returns 0 or -1 for no match,
15250 */
15251 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015252do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15253 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015254 char_u *spat; /* start pattern */
15255 char_u *mpat; /* middle pattern */
15256 char_u *epat; /* end pattern */
15257 int dir; /* BACKWARD or FORWARD */
15258 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015259 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015260 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015261 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015262 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015263{
15264 char_u *save_cpo;
15265 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15266 long retval = 0;
15267 pos_T pos;
15268 pos_T firstpos;
15269 pos_T foundpos;
15270 pos_T save_cursor;
15271 pos_T save_pos;
15272 int n;
15273 int r;
15274 int nest = 1;
15275 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015276 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015277 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015278
15279 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15280 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015281 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015282
Bram Moolenaar76929292008-01-06 19:07:36 +000015283#ifdef FEAT_RELTIME
15284 /* Set the time limit, if there is one. */
15285 profile_setlimit(time_limit, &tm);
15286#endif
15287
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015288 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15289 * start/middle/end (pat3, for the top pair). */
15290 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15291 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15292 if (pat2 == NULL || pat3 == NULL)
15293 goto theend;
15294 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15295 if (*mpat == NUL)
15296 STRCPY(pat3, pat2);
15297 else
15298 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15299 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015300 if (flags & SP_START)
15301 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015302
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 save_cursor = curwin->w_cursor;
15304 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015305 clearpos(&firstpos);
15306 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307 pat = pat3;
15308 for (;;)
15309 {
15310 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015311 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15313 /* didn't find it or found the first match again: FAIL */
15314 break;
15315
15316 if (firstpos.lnum == 0)
15317 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015318 if (equalpos(pos, foundpos))
15319 {
15320 /* Found the same position again. Can happen with a pattern that
15321 * has "\zs" at the end and searching backwards. Advance one
15322 * character and try again. */
15323 if (dir == BACKWARD)
15324 decl(&pos);
15325 else
15326 incl(&pos);
15327 }
15328 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015330 /* clear the start flag to avoid getting stuck here */
15331 options &= ~SEARCH_START;
15332
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 /* If the skip pattern matches, ignore this match. */
15334 if (*skip != NUL)
15335 {
15336 save_pos = curwin->w_cursor;
15337 curwin->w_cursor = pos;
15338 r = eval_to_bool(skip, &err, NULL, FALSE);
15339 curwin->w_cursor = save_pos;
15340 if (err)
15341 {
15342 /* Evaluating {skip} caused an error, break here. */
15343 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015344 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 break;
15346 }
15347 if (r)
15348 continue;
15349 }
15350
15351 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15352 {
15353 /* Found end when searching backwards or start when searching
15354 * forward: nested pair. */
15355 ++nest;
15356 pat = pat2; /* nested, don't search for middle */
15357 }
15358 else
15359 {
15360 /* Found end when searching forward or start when searching
15361 * backward: end of (nested) pair; or found middle in outer pair. */
15362 if (--nest == 1)
15363 pat = pat3; /* outer level, search for middle */
15364 }
15365
15366 if (nest == 0)
15367 {
15368 /* Found the match: return matchcount or line number. */
15369 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015370 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015372 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015373 if (flags & SP_SETPCMARK)
15374 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 curwin->w_cursor = pos;
15376 if (!(flags & SP_REPEAT))
15377 break;
15378 nest = 1; /* search for next unmatched */
15379 }
15380 }
15381
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015382 if (match_pos != NULL)
15383 {
15384 /* Store the match cursor position */
15385 match_pos->lnum = curwin->w_cursor.lnum;
15386 match_pos->col = curwin->w_cursor.col + 1;
15387 }
15388
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015390 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 curwin->w_cursor = save_cursor;
15392
15393theend:
15394 vim_free(pat2);
15395 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015396 if (p_cpo == empty_option)
15397 p_cpo = save_cpo;
15398 else
15399 /* Darn, evaluating the {skip} expression changed the value. */
15400 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015401
15402 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403}
15404
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015405/*
15406 * "searchpos()" function
15407 */
15408 static void
15409f_searchpos(argvars, rettv)
15410 typval_T *argvars;
15411 typval_T *rettv;
15412{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015413 pos_T match_pos;
15414 int lnum = 0;
15415 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015416 int n;
15417 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015418
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015419 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015420 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015421
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015422 n = search_cmn(argvars, &match_pos, &flags);
15423 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015424 {
15425 lnum = match_pos.lnum;
15426 col = match_pos.col;
15427 }
15428
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015429 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15430 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015431 if (flags & SP_SUBPAT)
15432 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015433}
15434
15435
Bram Moolenaar0d660222005-01-07 21:51:51 +000015436 static void
15437f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015438 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015441#ifdef FEAT_CLIENTSERVER
15442 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015443 char_u *server = get_tv_string_chk(&argvars[0]);
15444 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445
Bram Moolenaar0d660222005-01-07 21:51:51 +000015446 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015447 if (server == NULL || reply == NULL)
15448 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015449 if (check_restricted() || check_secure())
15450 return;
15451# ifdef FEAT_X11
15452 if (check_connection() == FAIL)
15453 return;
15454# endif
15455
15456 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015458 EMSG(_("E258: Unable to send to client"));
15459 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015461 rettv->vval.v_number = 0;
15462#else
15463 rettv->vval.v_number = -1;
15464#endif
15465}
15466
Bram Moolenaar0d660222005-01-07 21:51:51 +000015467 static void
15468f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015469 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015470 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015471{
15472 char_u *r = NULL;
15473
15474#ifdef FEAT_CLIENTSERVER
15475# ifdef WIN32
15476 r = serverGetVimNames();
15477# else
15478 make_connection();
15479 if (X_DISPLAY != NULL)
15480 r = serverGetVimNames(X_DISPLAY);
15481# endif
15482#endif
15483 rettv->v_type = VAR_STRING;
15484 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485}
15486
15487/*
15488 * "setbufvar()" function
15489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015492 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015493 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494{
15495 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015498 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499 char_u nbuf[NUMBUFLEN];
15500
15501 if (check_restricted() || check_secure())
15502 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015503 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15504 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015505 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 varp = &argvars[2];
15507
15508 if (buf != NULL && varname != NULL && varp != NULL)
15509 {
15510 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512
15513 if (*varname == '&')
15514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015515 long numval;
15516 char_u *strval;
15517 int error = FALSE;
15518
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015520 numval = get_tv_number_chk(varp, &error);
15521 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015522 if (!error && strval != NULL)
15523 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 }
15525 else
15526 {
15527 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15528 if (bufvarname != NULL)
15529 {
15530 STRCPY(bufvarname, "b:");
15531 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015532 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 vim_free(bufvarname);
15534 }
15535 }
15536
15537 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540}
15541
15542/*
15543 * "setcmdpos()" function
15544 */
15545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015546f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015547 typval_T *argvars;
15548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015550 int pos = (int)get_tv_number(&argvars[0]) - 1;
15551
15552 if (pos >= 0)
15553 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554}
15555
15556/*
15557 * "setline()" function
15558 */
15559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015561 typval_T *argvars;
15562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563{
15564 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015565 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015566 list_T *l = NULL;
15567 listitem_T *li = NULL;
15568 long added = 0;
15569 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015571 lnum = get_tv_lnum(&argvars[0]);
15572 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015574 l = argvars[1].vval.v_list;
15575 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015577 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015578 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015579
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015580 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015581 for (;;)
15582 {
15583 if (l != NULL)
15584 {
15585 /* list argument, get next string */
15586 if (li == NULL)
15587 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015589 li = li->li_next;
15590 }
15591
15592 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015593 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015594 break;
15595 if (lnum <= curbuf->b_ml.ml_line_count)
15596 {
15597 /* existing line, replace it */
15598 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15599 {
15600 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015601 if (lnum == curwin->w_cursor.lnum)
15602 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015603 rettv->vval.v_number = 0; /* OK */
15604 }
15605 }
15606 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15607 {
15608 /* lnum is one past the last line, append the line */
15609 ++added;
15610 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15611 rettv->vval.v_number = 0; /* OK */
15612 }
15613
15614 if (l == NULL) /* only one string argument */
15615 break;
15616 ++lnum;
15617 }
15618
15619 if (added > 0)
15620 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621}
15622
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015623static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15624
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015626 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015627 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015628 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015629set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015630 win_T *wp UNUSED;
15631 typval_T *list_arg UNUSED;
15632 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015633 typval_T *rettv;
15634{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015635#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015636 char_u *act;
15637 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015638#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015639
Bram Moolenaar2641f772005-03-25 21:58:17 +000015640 rettv->vval.v_number = -1;
15641
15642#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015643 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015644 EMSG(_(e_listreq));
15645 else
15646 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015647 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015648
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015649 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015650 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015651 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015652 if (act == NULL)
15653 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015654 if (*act == 'a' || *act == 'r')
15655 action = *act;
15656 }
15657
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015658 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015659 rettv->vval.v_number = 0;
15660 }
15661#endif
15662}
15663
15664/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015665 * "setloclist()" function
15666 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015667 static void
15668f_setloclist(argvars, rettv)
15669 typval_T *argvars;
15670 typval_T *rettv;
15671{
15672 win_T *win;
15673
15674 rettv->vval.v_number = -1;
15675
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015676 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015677 if (win != NULL)
15678 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15679}
15680
15681/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015682 * "setmatches()" function
15683 */
15684 static void
15685f_setmatches(argvars, rettv)
15686 typval_T *argvars;
15687 typval_T *rettv;
15688{
15689#ifdef FEAT_SEARCH_EXTRA
15690 list_T *l;
15691 listitem_T *li;
15692 dict_T *d;
15693
15694 rettv->vval.v_number = -1;
15695 if (argvars[0].v_type != VAR_LIST)
15696 {
15697 EMSG(_(e_listreq));
15698 return;
15699 }
15700 if ((l = argvars[0].vval.v_list) != NULL)
15701 {
15702
15703 /* To some extent make sure that we are dealing with a list from
15704 * "getmatches()". */
15705 li = l->lv_first;
15706 while (li != NULL)
15707 {
15708 if (li->li_tv.v_type != VAR_DICT
15709 || (d = li->li_tv.vval.v_dict) == NULL)
15710 {
15711 EMSG(_(e_invarg));
15712 return;
15713 }
15714 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15715 && dict_find(d, (char_u *)"pattern", -1) != NULL
15716 && dict_find(d, (char_u *)"priority", -1) != NULL
15717 && dict_find(d, (char_u *)"id", -1) != NULL))
15718 {
15719 EMSG(_(e_invarg));
15720 return;
15721 }
15722 li = li->li_next;
15723 }
15724
15725 clear_matches(curwin);
15726 li = l->lv_first;
15727 while (li != NULL)
15728 {
15729 d = li->li_tv.vval.v_dict;
15730 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15731 get_dict_string(d, (char_u *)"pattern", FALSE),
15732 (int)get_dict_number(d, (char_u *)"priority"),
15733 (int)get_dict_number(d, (char_u *)"id"));
15734 li = li->li_next;
15735 }
15736 rettv->vval.v_number = 0;
15737 }
15738#endif
15739}
15740
15741/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015742 * "setpos()" function
15743 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015744 static void
15745f_setpos(argvars, rettv)
15746 typval_T *argvars;
15747 typval_T *rettv;
15748{
15749 pos_T pos;
15750 int fnum;
15751 char_u *name;
15752
Bram Moolenaar08250432008-02-13 11:42:46 +000015753 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015754 name = get_tv_string_chk(argvars);
15755 if (name != NULL)
15756 {
15757 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15758 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015759 if (--pos.col < 0)
15760 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015761 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015762 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015763 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015764 if (fnum == curbuf->b_fnum)
15765 {
15766 curwin->w_cursor = pos;
15767 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015768 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015769 }
15770 else
15771 EMSG(_(e_invarg));
15772 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015773 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15774 {
15775 /* set mark */
15776 if (setmark_pos(name[1], &pos, fnum) == OK)
15777 rettv->vval.v_number = 0;
15778 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015779 else
15780 EMSG(_(e_invarg));
15781 }
15782 }
15783}
15784
15785/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015786 * "setqflist()" function
15787 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015788 static void
15789f_setqflist(argvars, rettv)
15790 typval_T *argvars;
15791 typval_T *rettv;
15792{
15793 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15794}
15795
15796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797 * "setreg()" function
15798 */
15799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015800f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015801 typval_T *argvars;
15802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803{
15804 int regname;
15805 char_u *strregname;
15806 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015807 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808 int append;
15809 char_u yank_type;
15810 long block_len;
15811
15812 block_len = -1;
15813 yank_type = MAUTO;
15814 append = FALSE;
15815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015816 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015817 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015819 if (strregname == NULL)
15820 return; /* type error; errmsg already given */
15821 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822 if (regname == 0 || regname == '@')
15823 regname = '"';
15824 else if (regname == '=')
15825 return;
15826
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015829 stropt = get_tv_string_chk(&argvars[2]);
15830 if (stropt == NULL)
15831 return; /* type error */
15832 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 switch (*stropt)
15834 {
15835 case 'a': case 'A': /* append */
15836 append = TRUE;
15837 break;
15838 case 'v': case 'c': /* character-wise selection */
15839 yank_type = MCHAR;
15840 break;
15841 case 'V': case 'l': /* line-wise selection */
15842 yank_type = MLINE;
15843 break;
15844#ifdef FEAT_VISUAL
15845 case 'b': case Ctrl_V: /* block-wise selection */
15846 yank_type = MBLOCK;
15847 if (VIM_ISDIGIT(stropt[1]))
15848 {
15849 ++stropt;
15850 block_len = getdigits(&stropt) - 1;
15851 --stropt;
15852 }
15853 break;
15854#endif
15855 }
15856 }
15857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015858 strval = get_tv_string_chk(&argvars[1]);
15859 if (strval != NULL)
15860 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015862 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863}
15864
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015865/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015866 * "settabvar()" function
15867 */
15868 static void
15869f_settabvar(argvars, rettv)
15870 typval_T *argvars;
15871 typval_T *rettv;
15872{
15873 tabpage_T *save_curtab;
15874 char_u *varname, *tabvarname;
15875 typval_T *varp;
15876 tabpage_T *tp;
15877
15878 rettv->vval.v_number = 0;
15879
15880 if (check_restricted() || check_secure())
15881 return;
15882
15883 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15884 varname = get_tv_string_chk(&argvars[1]);
15885 varp = &argvars[2];
15886
15887 if (tp != NULL && varname != NULL && varp != NULL)
15888 {
15889 save_curtab = curtab;
15890 goto_tabpage_tp(tp);
15891
15892 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
15893 if (tabvarname != NULL)
15894 {
15895 STRCPY(tabvarname, "t:");
15896 STRCPY(tabvarname + 2, varname);
15897 set_var(tabvarname, varp, TRUE);
15898 vim_free(tabvarname);
15899 }
15900
15901 /* Restore current tabpage */
15902 if (valid_tabpage(save_curtab))
15903 goto_tabpage_tp(save_curtab);
15904 }
15905}
15906
15907/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015908 * "settabwinvar()" function
15909 */
15910 static void
15911f_settabwinvar(argvars, rettv)
15912 typval_T *argvars;
15913 typval_T *rettv;
15914{
15915 setwinvar(argvars, rettv, 1);
15916}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917
15918/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015919 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015922f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015923 typval_T *argvars;
15924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015926 setwinvar(argvars, rettv, 0);
15927}
15928
15929/*
15930 * "setwinvar()" and "settabwinvar()" functions
15931 */
15932 static void
15933setwinvar(argvars, rettv, off)
15934 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015935 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015936 int off;
15937{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 win_T *win;
15939#ifdef FEAT_WINDOWS
15940 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015941 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942#endif
15943 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015944 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015946 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947
15948 if (check_restricted() || check_secure())
15949 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015950
15951#ifdef FEAT_WINDOWS
15952 if (off == 1)
15953 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15954 else
15955 tp = curtab;
15956#endif
15957 win = find_win_by_nr(&argvars[off], tp);
15958 varname = get_tv_string_chk(&argvars[off + 1]);
15959 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960
15961 if (win != NULL && varname != NULL && varp != NULL)
15962 {
15963#ifdef FEAT_WINDOWS
15964 /* set curwin to be our win, temporarily */
15965 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015966 save_curtab = curtab;
15967 goto_tabpage_tp(tp);
15968 if (!win_valid(win))
15969 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 curwin = win;
15971 curbuf = curwin->w_buffer;
15972#endif
15973
15974 if (*varname == '&')
15975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015976 long numval;
15977 char_u *strval;
15978 int error = FALSE;
15979
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015981 numval = get_tv_number_chk(varp, &error);
15982 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 if (!error && strval != NULL)
15984 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985 }
15986 else
15987 {
15988 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15989 if (winvarname != NULL)
15990 {
15991 STRCPY(winvarname, "w:");
15992 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015993 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 vim_free(winvarname);
15995 }
15996 }
15997
15998#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015999 /* Restore current tabpage and window, if still valid (autocomands can
16000 * make them invalid). */
16001 if (valid_tabpage(save_curtab))
16002 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 if (win_valid(save_curwin))
16004 {
16005 curwin = save_curwin;
16006 curbuf = curwin->w_buffer;
16007 }
16008#endif
16009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010}
16011
16012/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016013 * "shellescape({string})" function
16014 */
16015 static void
16016f_shellescape(argvars, rettv)
16017 typval_T *argvars;
16018 typval_T *rettv;
16019{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016020 rettv->vval.v_string = vim_strsave_shellescape(
16021 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016022 rettv->v_type = VAR_STRING;
16023}
16024
16025/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016026 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027 */
16028 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016029f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016030 typval_T *argvars;
16031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016033 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034
Bram Moolenaar0d660222005-01-07 21:51:51 +000016035 p = get_tv_string(&argvars[0]);
16036 rettv->vval.v_string = vim_strsave(p);
16037 simplify_filename(rettv->vval.v_string); /* simplify in place */
16038 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039}
16040
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016041#ifdef FEAT_FLOAT
16042/*
16043 * "sin()" function
16044 */
16045 static void
16046f_sin(argvars, rettv)
16047 typval_T *argvars;
16048 typval_T *rettv;
16049{
16050 float_T f;
16051
16052 rettv->v_type = VAR_FLOAT;
16053 if (get_float_arg(argvars, &f) == OK)
16054 rettv->vval.v_float = sin(f);
16055 else
16056 rettv->vval.v_float = 0.0;
16057}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016058
16059/*
16060 * "sinh()" function
16061 */
16062 static void
16063f_sinh(argvars, rettv)
16064 typval_T *argvars;
16065 typval_T *rettv;
16066{
16067 float_T f;
16068
16069 rettv->v_type = VAR_FLOAT;
16070 if (get_float_arg(argvars, &f) == OK)
16071 rettv->vval.v_float = sinh(f);
16072 else
16073 rettv->vval.v_float = 0.0;
16074}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016075#endif
16076
Bram Moolenaar0d660222005-01-07 21:51:51 +000016077static int
16078#ifdef __BORLANDC__
16079 _RTLENTRYF
16080#endif
16081 item_compare __ARGS((const void *s1, const void *s2));
16082static int
16083#ifdef __BORLANDC__
16084 _RTLENTRYF
16085#endif
16086 item_compare2 __ARGS((const void *s1, const void *s2));
16087
16088static int item_compare_ic;
16089static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016090static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016091#define ITEM_COMPARE_FAIL 999
16092
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016094 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016096 static int
16097#ifdef __BORLANDC__
16098_RTLENTRYF
16099#endif
16100item_compare(s1, s2)
16101 const void *s1;
16102 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016104 char_u *p1, *p2;
16105 char_u *tofree1, *tofree2;
16106 int res;
16107 char_u numbuf1[NUMBUFLEN];
16108 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016110 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16111 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016112 if (p1 == NULL)
16113 p1 = (char_u *)"";
16114 if (p2 == NULL)
16115 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016116 if (item_compare_ic)
16117 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016119 res = STRCMP(p1, p2);
16120 vim_free(tofree1);
16121 vim_free(tofree2);
16122 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123}
16124
16125 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016126#ifdef __BORLANDC__
16127_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129item_compare2(s1, s2)
16130 const void *s1;
16131 const void *s2;
16132{
16133 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016134 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016135 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016138 /* shortcut after failure in previous call; compare all items equal */
16139 if (item_compare_func_err)
16140 return 0;
16141
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016142 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16143 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016144 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16145 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016146
16147 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016148 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016149 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016150 clear_tv(&argv[0]);
16151 clear_tv(&argv[1]);
16152
16153 if (res == FAIL)
16154 res = ITEM_COMPARE_FAIL;
16155 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016156 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16157 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016158 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016159 clear_tv(&rettv);
16160 return res;
16161}
16162
16163/*
16164 * "sort({list})" function
16165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 typval_T *argvars;
16169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170{
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 list_T *l;
16172 listitem_T *li;
16173 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016174 long len;
16175 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177 if (argvars[0].v_type != VAR_LIST)
16178 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 else
16180 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016181 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016182 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016183 return;
16184 rettv->vval.v_list = l;
16185 rettv->v_type = VAR_LIST;
16186 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187
Bram Moolenaar0d660222005-01-07 21:51:51 +000016188 len = list_len(l);
16189 if (len <= 1)
16190 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191
Bram Moolenaar0d660222005-01-07 21:51:51 +000016192 item_compare_ic = FALSE;
16193 item_compare_func = NULL;
16194 if (argvars[1].v_type != VAR_UNKNOWN)
16195 {
16196 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016197 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198 else
16199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016200 int error = FALSE;
16201
16202 i = get_tv_number_chk(&argvars[1], &error);
16203 if (error)
16204 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205 if (i == 1)
16206 item_compare_ic = TRUE;
16207 else
16208 item_compare_func = get_tv_string(&argvars[1]);
16209 }
16210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016214 if (ptrs == NULL)
16215 return;
16216 i = 0;
16217 for (li = l->lv_first; li != NULL; li = li->li_next)
16218 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016220 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016221 /* test the compare function */
16222 if (item_compare_func != NULL
16223 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16224 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016225 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227 {
16228 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016229 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016230 item_compare_func == NULL ? item_compare : item_compare2);
16231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016232 if (!item_compare_func_err)
16233 {
16234 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016235 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016236 l->lv_len = 0;
16237 for (i = 0; i < len; ++i)
16238 list_append(l, ptrs[i]);
16239 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016240 }
16241
16242 vim_free(ptrs);
16243 }
16244}
16245
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016246/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016247 * "soundfold({word})" function
16248 */
16249 static void
16250f_soundfold(argvars, rettv)
16251 typval_T *argvars;
16252 typval_T *rettv;
16253{
16254 char_u *s;
16255
16256 rettv->v_type = VAR_STRING;
16257 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016258#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016259 rettv->vval.v_string = eval_soundfold(s);
16260#else
16261 rettv->vval.v_string = vim_strsave(s);
16262#endif
16263}
16264
16265/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016266 * "spellbadword()" function
16267 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016268 static void
16269f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016270 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016271 typval_T *rettv;
16272{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016273 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016274 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016275 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016276
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016277 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016278 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016279
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016280#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016281 if (argvars[0].v_type == VAR_UNKNOWN)
16282 {
16283 /* Find the start and length of the badly spelled word. */
16284 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16285 if (len != 0)
16286 word = ml_get_cursor();
16287 }
16288 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16289 {
16290 char_u *str = get_tv_string_chk(&argvars[0]);
16291 int capcol = -1;
16292
16293 if (str != NULL)
16294 {
16295 /* Check the argument for spelling. */
16296 while (*str != NUL)
16297 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016298 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016299 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016300 {
16301 word = str;
16302 break;
16303 }
16304 str += len;
16305 }
16306 }
16307 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016308#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016309
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016310 list_append_string(rettv->vval.v_list, word, len);
16311 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016312 attr == HLF_SPB ? "bad" :
16313 attr == HLF_SPR ? "rare" :
16314 attr == HLF_SPL ? "local" :
16315 attr == HLF_SPC ? "caps" :
16316 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016317}
16318
16319/*
16320 * "spellsuggest()" function
16321 */
16322 static void
16323f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016324 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016325 typval_T *rettv;
16326{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016327#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016328 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016329 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016330 int maxcount;
16331 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016332 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016333 listitem_T *li;
16334 int need_capital = FALSE;
16335#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016336
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016337 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016338 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016339
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016340#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016341 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16342 {
16343 str = get_tv_string(&argvars[0]);
16344 if (argvars[1].v_type != VAR_UNKNOWN)
16345 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016346 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016347 if (maxcount <= 0)
16348 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016349 if (argvars[2].v_type != VAR_UNKNOWN)
16350 {
16351 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16352 if (typeerr)
16353 return;
16354 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016355 }
16356 else
16357 maxcount = 25;
16358
Bram Moolenaar4770d092006-01-12 23:22:24 +000016359 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016360
16361 for (i = 0; i < ga.ga_len; ++i)
16362 {
16363 str = ((char_u **)ga.ga_data)[i];
16364
16365 li = listitem_alloc();
16366 if (li == NULL)
16367 vim_free(str);
16368 else
16369 {
16370 li->li_tv.v_type = VAR_STRING;
16371 li->li_tv.v_lock = 0;
16372 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016373 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016374 }
16375 }
16376 ga_clear(&ga);
16377 }
16378#endif
16379}
16380
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016382f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016383 typval_T *argvars;
16384 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016385{
16386 char_u *str;
16387 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016388 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389 regmatch_T regmatch;
16390 char_u patbuf[NUMBUFLEN];
16391 char_u *save_cpo;
16392 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016393 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016394 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016395 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016396
16397 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16398 save_cpo = p_cpo;
16399 p_cpo = (char_u *)"";
16400
16401 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016402 if (argvars[1].v_type != VAR_UNKNOWN)
16403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16405 if (pat == NULL)
16406 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016407 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016408 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016409 }
16410 if (pat == NULL || *pat == NUL)
16411 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016413 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016415 if (typeerr)
16416 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417
Bram Moolenaar0d660222005-01-07 21:51:51 +000016418 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16419 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016421 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016422 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016423 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016424 if (*str == NUL)
16425 match = FALSE; /* empty item at the end */
16426 else
16427 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016428 if (match)
16429 end = regmatch.startp[0];
16430 else
16431 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016432 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16433 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016434 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016435 if (list_append_string(rettv->vval.v_list, str,
16436 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016438 }
16439 if (!match)
16440 break;
16441 /* Advance to just after the match. */
16442 if (regmatch.endp[0] > str)
16443 col = 0;
16444 else
16445 {
16446 /* Don't get stuck at the same match. */
16447#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016448 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449#else
16450 col = 1;
16451#endif
16452 }
16453 str = regmatch.endp[0];
16454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455
Bram Moolenaar0d660222005-01-07 21:51:51 +000016456 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460}
16461
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016462#ifdef FEAT_FLOAT
16463/*
16464 * "sqrt()" function
16465 */
16466 static void
16467f_sqrt(argvars, rettv)
16468 typval_T *argvars;
16469 typval_T *rettv;
16470{
16471 float_T f;
16472
16473 rettv->v_type = VAR_FLOAT;
16474 if (get_float_arg(argvars, &f) == OK)
16475 rettv->vval.v_float = sqrt(f);
16476 else
16477 rettv->vval.v_float = 0.0;
16478}
16479
16480/*
16481 * "str2float()" function
16482 */
16483 static void
16484f_str2float(argvars, rettv)
16485 typval_T *argvars;
16486 typval_T *rettv;
16487{
16488 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16489
16490 if (*p == '+')
16491 p = skipwhite(p + 1);
16492 (void)string2float(p, &rettv->vval.v_float);
16493 rettv->v_type = VAR_FLOAT;
16494}
16495#endif
16496
Bram Moolenaar2c932302006-03-18 21:42:09 +000016497/*
16498 * "str2nr()" function
16499 */
16500 static void
16501f_str2nr(argvars, rettv)
16502 typval_T *argvars;
16503 typval_T *rettv;
16504{
16505 int base = 10;
16506 char_u *p;
16507 long n;
16508
16509 if (argvars[1].v_type != VAR_UNKNOWN)
16510 {
16511 base = get_tv_number(&argvars[1]);
16512 if (base != 8 && base != 10 && base != 16)
16513 {
16514 EMSG(_(e_invarg));
16515 return;
16516 }
16517 }
16518
16519 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016520 if (*p == '+')
16521 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016522 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16523 rettv->vval.v_number = n;
16524}
16525
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526#ifdef HAVE_STRFTIME
16527/*
16528 * "strftime({format}[, {time}])" function
16529 */
16530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016531f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016532 typval_T *argvars;
16533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534{
16535 char_u result_buf[256];
16536 struct tm *curtime;
16537 time_t seconds;
16538 char_u *p;
16539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016540 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016543 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 seconds = time(NULL);
16545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016546 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 curtime = localtime(&seconds);
16548 /* MSVC returns NULL for an invalid value of seconds. */
16549 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016550 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 else
16552 {
16553# ifdef FEAT_MBYTE
16554 vimconv_T conv;
16555 char_u *enc;
16556
16557 conv.vc_type = CONV_NONE;
16558 enc = enc_locale();
16559 convert_setup(&conv, p_enc, enc);
16560 if (conv.vc_type != CONV_NONE)
16561 p = string_convert(&conv, p, NULL);
16562# endif
16563 if (p != NULL)
16564 (void)strftime((char *)result_buf, sizeof(result_buf),
16565 (char *)p, curtime);
16566 else
16567 result_buf[0] = NUL;
16568
16569# ifdef FEAT_MBYTE
16570 if (conv.vc_type != CONV_NONE)
16571 vim_free(p);
16572 convert_setup(&conv, enc, p_enc);
16573 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016574 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 else
16576# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016577 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578
16579# ifdef FEAT_MBYTE
16580 /* Release conversion descriptors */
16581 convert_setup(&conv, NULL, NULL);
16582 vim_free(enc);
16583# endif
16584 }
16585}
16586#endif
16587
16588/*
16589 * "stridx()" function
16590 */
16591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016592f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016593 typval_T *argvars;
16594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595{
16596 char_u buf[NUMBUFLEN];
16597 char_u *needle;
16598 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016599 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016601 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016603 needle = get_tv_string_chk(&argvars[1]);
16604 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016605 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016606 if (needle == NULL || haystack == NULL)
16607 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608
Bram Moolenaar33570922005-01-25 22:26:29 +000016609 if (argvars[2].v_type != VAR_UNKNOWN)
16610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016611 int error = FALSE;
16612
16613 start_idx = get_tv_number_chk(&argvars[2], &error);
16614 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016615 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016616 if (start_idx >= 0)
16617 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016618 }
16619
16620 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16621 if (pos != NULL)
16622 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623}
16624
16625/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016626 * "string()" function
16627 */
16628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016629f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016630 typval_T *argvars;
16631 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016632{
16633 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016634 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016637 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016638 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016639 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016640 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641}
16642
16643/*
16644 * "strlen()" function
16645 */
16646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016647f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016648 typval_T *argvars;
16649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651 rettv->vval.v_number = (varnumber_T)(STRLEN(
16652 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653}
16654
16655/*
16656 * "strpart()" function
16657 */
16658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016659f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016660 typval_T *argvars;
16661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662{
16663 char_u *p;
16664 int n;
16665 int len;
16666 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016667 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016669 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 slen = (int)STRLEN(p);
16671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016672 n = get_tv_number_chk(&argvars[1], &error);
16673 if (error)
16674 len = 0;
16675 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016676 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 else
16678 len = slen - n; /* default len: all bytes that are available. */
16679
16680 /*
16681 * Only return the overlap between the specified part and the actual
16682 * string.
16683 */
16684 if (n < 0)
16685 {
16686 len += n;
16687 n = 0;
16688 }
16689 else if (n > slen)
16690 n = slen;
16691 if (len < 0)
16692 len = 0;
16693 else if (n + len > slen)
16694 len = slen - n;
16695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016696 rettv->v_type = VAR_STRING;
16697 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698}
16699
16700/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016701 * "strridx()" function
16702 */
16703 static void
16704f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016705 typval_T *argvars;
16706 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016707{
16708 char_u buf[NUMBUFLEN];
16709 char_u *needle;
16710 char_u *haystack;
16711 char_u *rest;
16712 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016713 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016715 needle = get_tv_string_chk(&argvars[1]);
16716 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016717
16718 rettv->vval.v_number = -1;
16719 if (needle == NULL || haystack == NULL)
16720 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016721
16722 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016723 if (argvars[2].v_type != VAR_UNKNOWN)
16724 {
16725 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016726 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016727 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016728 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016729 }
16730 else
16731 end_idx = haystack_len;
16732
Bram Moolenaar0d660222005-01-07 21:51:51 +000016733 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016734 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016735 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016736 lastmatch = haystack + end_idx;
16737 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016738 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016739 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016740 for (rest = haystack; *rest != '\0'; ++rest)
16741 {
16742 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016743 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016744 break;
16745 lastmatch = rest;
16746 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016747 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016748
16749 if (lastmatch == NULL)
16750 rettv->vval.v_number = -1;
16751 else
16752 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16753}
16754
16755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 * "strtrans()" function
16757 */
16758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016759f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016760 typval_T *argvars;
16761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016763 rettv->v_type = VAR_STRING;
16764 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765}
16766
16767/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016768 * "submatch()" function
16769 */
16770 static void
16771f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016772 typval_T *argvars;
16773 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016774{
16775 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016776 rettv->vval.v_string =
16777 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016778}
16779
16780/*
16781 * "substitute()" function
16782 */
16783 static void
16784f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 typval_T *argvars;
16786 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016787{
16788 char_u patbuf[NUMBUFLEN];
16789 char_u subbuf[NUMBUFLEN];
16790 char_u flagsbuf[NUMBUFLEN];
16791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016792 char_u *str = get_tv_string_chk(&argvars[0]);
16793 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16794 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16795 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16796
Bram Moolenaar0d660222005-01-07 21:51:51 +000016797 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016798 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16799 rettv->vval.v_string = NULL;
16800 else
16801 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016802}
16803
16804/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016805 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016808f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016809 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811{
16812 int id = 0;
16813#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016814 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 long col;
16816 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016817 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016819 lnum = get_tv_lnum(argvars); /* -1 on type error */
16820 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16821 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016823 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016824 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016825 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826#endif
16827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016828 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829}
16830
16831/*
16832 * "synIDattr(id, what [, mode])" function
16833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016835f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016836 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838{
16839 char_u *p = NULL;
16840#ifdef FEAT_SYN_HL
16841 int id;
16842 char_u *what;
16843 char_u *mode;
16844 char_u modebuf[NUMBUFLEN];
16845 int modec;
16846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016847 id = get_tv_number(&argvars[0]);
16848 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016849 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016851 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 modec = TOLOWER_ASC(mode[0]);
16853 if (modec != 't' && modec != 'c'
16854#ifdef FEAT_GUI
16855 && modec != 'g'
16856#endif
16857 )
16858 modec = 0; /* replace invalid with current */
16859 }
16860 else
16861 {
16862#ifdef FEAT_GUI
16863 if (gui.in_use)
16864 modec = 'g';
16865 else
16866#endif
16867 if (t_colors > 1)
16868 modec = 'c';
16869 else
16870 modec = 't';
16871 }
16872
16873
16874 switch (TOLOWER_ASC(what[0]))
16875 {
16876 case 'b':
16877 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16878 p = highlight_color(id, what, modec);
16879 else /* bold */
16880 p = highlight_has_attr(id, HL_BOLD, modec);
16881 break;
16882
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016883 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 p = highlight_color(id, what, modec);
16885 break;
16886
16887 case 'i':
16888 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16889 p = highlight_has_attr(id, HL_INVERSE, modec);
16890 else /* italic */
16891 p = highlight_has_attr(id, HL_ITALIC, modec);
16892 break;
16893
16894 case 'n': /* name */
16895 p = get_highlight_name(NULL, id - 1);
16896 break;
16897
16898 case 'r': /* reverse */
16899 p = highlight_has_attr(id, HL_INVERSE, modec);
16900 break;
16901
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016902 case 's':
16903 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16904 p = highlight_color(id, what, modec);
16905 else /* standout */
16906 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 break;
16908
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016909 case 'u':
16910 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16911 /* underline */
16912 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16913 else
16914 /* undercurl */
16915 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 break;
16917 }
16918
16919 if (p != NULL)
16920 p = vim_strsave(p);
16921#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016922 rettv->v_type = VAR_STRING;
16923 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924}
16925
16926/*
16927 * "synIDtrans(id)" function
16928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016930f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016931 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933{
16934 int id;
16935
16936#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016937 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938
16939 if (id > 0)
16940 id = syn_get_final_id(id);
16941 else
16942#endif
16943 id = 0;
16944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016945 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946}
16947
16948/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016949 * "synstack(lnum, col)" function
16950 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016951 static void
16952f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016953 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016954 typval_T *rettv;
16955{
16956#ifdef FEAT_SYN_HL
16957 long lnum;
16958 long col;
16959 int i;
16960 int id;
16961#endif
16962
16963 rettv->v_type = VAR_LIST;
16964 rettv->vval.v_list = NULL;
16965
16966#ifdef FEAT_SYN_HL
16967 lnum = get_tv_lnum(argvars); /* -1 on type error */
16968 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16969
16970 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016971 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016972 && rettv_list_alloc(rettv) != FAIL)
16973 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016974 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016975 for (i = 0; ; ++i)
16976 {
16977 id = syn_get_stack_item(i);
16978 if (id < 0)
16979 break;
16980 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16981 break;
16982 }
16983 }
16984#endif
16985}
16986
16987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 * "system()" function
16989 */
16990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016991f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016992 typval_T *argvars;
16993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016995 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016997 char_u *infile = NULL;
16998 char_u buf[NUMBUFLEN];
16999 int err = FALSE;
17000 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017002 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017003 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017004
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017005 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017006 {
17007 /*
17008 * Write the string to a temp file, to be used for input of the shell
17009 * command.
17010 */
17011 if ((infile = vim_tempname('i')) == NULL)
17012 {
17013 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017014 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017015 }
17016
17017 fd = mch_fopen((char *)infile, WRITEBIN);
17018 if (fd == NULL)
17019 {
17020 EMSG2(_(e_notopen), infile);
17021 goto done;
17022 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017023 p = get_tv_string_buf_chk(&argvars[1], buf);
17024 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017025 {
17026 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017027 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017028 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017029 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17030 err = TRUE;
17031 if (fclose(fd) != 0)
17032 err = TRUE;
17033 if (err)
17034 {
17035 EMSG(_("E677: Error writing temp file"));
17036 goto done;
17037 }
17038 }
17039
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017040 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17041 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017042
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043#ifdef USE_CR
17044 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017045 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046 {
17047 char_u *s;
17048
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017049 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050 {
17051 if (*s == CAR)
17052 *s = NL;
17053 }
17054 }
17055#else
17056# ifdef USE_CRNL
17057 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017058 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 {
17060 char_u *s, *d;
17061
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017062 d = res;
17063 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 {
17065 if (s[0] == CAR && s[1] == NL)
17066 ++s;
17067 *d++ = *s;
17068 }
17069 *d = NUL;
17070 }
17071# endif
17072#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017073
17074done:
17075 if (infile != NULL)
17076 {
17077 mch_remove(infile);
17078 vim_free(infile);
17079 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017080 rettv->v_type = VAR_STRING;
17081 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082}
17083
17084/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017085 * "tabpagebuflist()" function
17086 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017087 static void
17088f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017089 typval_T *argvars UNUSED;
17090 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017091{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017092#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017093 tabpage_T *tp;
17094 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017095
17096 if (argvars[0].v_type == VAR_UNKNOWN)
17097 wp = firstwin;
17098 else
17099 {
17100 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17101 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017102 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017103 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017104 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017105 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017106 for (; wp != NULL; wp = wp->w_next)
17107 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017108 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017109 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017110 }
17111#endif
17112}
17113
17114
17115/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017116 * "tabpagenr()" function
17117 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017118 static void
17119f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017120 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017121 typval_T *rettv;
17122{
17123 int nr = 1;
17124#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017125 char_u *arg;
17126
17127 if (argvars[0].v_type != VAR_UNKNOWN)
17128 {
17129 arg = get_tv_string_chk(&argvars[0]);
17130 nr = 0;
17131 if (arg != NULL)
17132 {
17133 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017134 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017135 else
17136 EMSG2(_(e_invexpr2), arg);
17137 }
17138 }
17139 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017140 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017141#endif
17142 rettv->vval.v_number = nr;
17143}
17144
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017145
17146#ifdef FEAT_WINDOWS
17147static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17148
17149/*
17150 * Common code for tabpagewinnr() and winnr().
17151 */
17152 static int
17153get_winnr(tp, argvar)
17154 tabpage_T *tp;
17155 typval_T *argvar;
17156{
17157 win_T *twin;
17158 int nr = 1;
17159 win_T *wp;
17160 char_u *arg;
17161
17162 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17163 if (argvar->v_type != VAR_UNKNOWN)
17164 {
17165 arg = get_tv_string_chk(argvar);
17166 if (arg == NULL)
17167 nr = 0; /* type error; errmsg already given */
17168 else if (STRCMP(arg, "$") == 0)
17169 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17170 else if (STRCMP(arg, "#") == 0)
17171 {
17172 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17173 if (twin == NULL)
17174 nr = 0;
17175 }
17176 else
17177 {
17178 EMSG2(_(e_invexpr2), arg);
17179 nr = 0;
17180 }
17181 }
17182
17183 if (nr > 0)
17184 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17185 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017186 {
17187 if (wp == NULL)
17188 {
17189 /* didn't find it in this tabpage */
17190 nr = 0;
17191 break;
17192 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017193 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017194 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017195 return nr;
17196}
17197#endif
17198
17199/*
17200 * "tabpagewinnr()" function
17201 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017202 static void
17203f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017204 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017205 typval_T *rettv;
17206{
17207 int nr = 1;
17208#ifdef FEAT_WINDOWS
17209 tabpage_T *tp;
17210
17211 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17212 if (tp == NULL)
17213 nr = 0;
17214 else
17215 nr = get_winnr(tp, &argvars[1]);
17216#endif
17217 rettv->vval.v_number = nr;
17218}
17219
17220
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017221/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017222 * "tagfiles()" function
17223 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017224 static void
17225f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017226 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017227 typval_T *rettv;
17228{
17229 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017230 tagname_T tn;
17231 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017232
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017233 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017234 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017235
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017236 for (first = TRUE; ; first = FALSE)
17237 if (get_tagfname(&tn, first, fname) == FAIL
17238 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017239 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017240 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017241}
17242
17243/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017244 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017245 */
17246 static void
17247f_taglist(argvars, rettv)
17248 typval_T *argvars;
17249 typval_T *rettv;
17250{
17251 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017252
17253 tag_pattern = get_tv_string(&argvars[0]);
17254
17255 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017256 if (*tag_pattern == NUL)
17257 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017258
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017259 if (rettv_list_alloc(rettv) == OK)
17260 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017261}
17262
17263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 * "tempname()" function
17265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017267f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017268 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270{
17271 static int x = 'A';
17272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017273 rettv->v_type = VAR_STRING;
17274 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275
17276 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17277 * names. Skip 'I' and 'O', they are used for shell redirection. */
17278 do
17279 {
17280 if (x == 'Z')
17281 x = '0';
17282 else if (x == '9')
17283 x = 'A';
17284 else
17285 {
17286#ifdef EBCDIC
17287 if (x == 'I')
17288 x = 'J';
17289 else if (x == 'R')
17290 x = 'S';
17291 else
17292#endif
17293 ++x;
17294 }
17295 } while (x == 'I' || x == 'O');
17296}
17297
17298/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017299 * "test(list)" function: Just checking the walls...
17300 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017301 static void
17302f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017303 typval_T *argvars UNUSED;
17304 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017305{
17306 /* Used for unit testing. Change the code below to your liking. */
17307#if 0
17308 listitem_T *li;
17309 list_T *l;
17310 char_u *bad, *good;
17311
17312 if (argvars[0].v_type != VAR_LIST)
17313 return;
17314 l = argvars[0].vval.v_list;
17315 if (l == NULL)
17316 return;
17317 li = l->lv_first;
17318 if (li == NULL)
17319 return;
17320 bad = get_tv_string(&li->li_tv);
17321 li = li->li_next;
17322 if (li == NULL)
17323 return;
17324 good = get_tv_string(&li->li_tv);
17325 rettv->vval.v_number = test_edit_score(bad, good);
17326#endif
17327}
17328
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017329#ifdef FEAT_FLOAT
17330/*
17331 * "tan()" function
17332 */
17333 static void
17334f_tan(argvars, rettv)
17335 typval_T *argvars;
17336 typval_T *rettv;
17337{
17338 float_T f;
17339
17340 rettv->v_type = VAR_FLOAT;
17341 if (get_float_arg(argvars, &f) == OK)
17342 rettv->vval.v_float = tan(f);
17343 else
17344 rettv->vval.v_float = 0.0;
17345}
17346
17347/*
17348 * "tanh()" function
17349 */
17350 static void
17351f_tanh(argvars, rettv)
17352 typval_T *argvars;
17353 typval_T *rettv;
17354{
17355 float_T f;
17356
17357 rettv->v_type = VAR_FLOAT;
17358 if (get_float_arg(argvars, &f) == OK)
17359 rettv->vval.v_float = tanh(f);
17360 else
17361 rettv->vval.v_float = 0.0;
17362}
17363#endif
17364
Bram Moolenaard52d9742005-08-21 22:20:28 +000017365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 * "tolower(string)" function
17367 */
17368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 typval_T *argvars;
17371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372{
17373 char_u *p;
17374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375 p = vim_strsave(get_tv_string(&argvars[0]));
17376 rettv->v_type = VAR_STRING;
17377 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378
17379 if (p != NULL)
17380 while (*p != NUL)
17381 {
17382#ifdef FEAT_MBYTE
17383 int l;
17384
17385 if (enc_utf8)
17386 {
17387 int c, lc;
17388
17389 c = utf_ptr2char(p);
17390 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017391 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 /* TODO: reallocate string when byte count changes. */
17393 if (utf_char2len(lc) == l)
17394 utf_char2bytes(lc, p);
17395 p += l;
17396 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017397 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 p += l; /* skip multi-byte character */
17399 else
17400#endif
17401 {
17402 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17403 ++p;
17404 }
17405 }
17406}
17407
17408/*
17409 * "toupper(string)" function
17410 */
17411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017412f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017413 typval_T *argvars;
17414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017416 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017417 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418}
17419
17420/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017421 * "tr(string, fromstr, tostr)" function
17422 */
17423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017425 typval_T *argvars;
17426 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017427{
17428 char_u *instr;
17429 char_u *fromstr;
17430 char_u *tostr;
17431 char_u *p;
17432#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017433 int inlen;
17434 int fromlen;
17435 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017436 int idx;
17437 char_u *cpstr;
17438 int cplen;
17439 int first = TRUE;
17440#endif
17441 char_u buf[NUMBUFLEN];
17442 char_u buf2[NUMBUFLEN];
17443 garray_T ga;
17444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017445 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017446 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17447 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017448
17449 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017450 rettv->v_type = VAR_STRING;
17451 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017452 if (fromstr == NULL || tostr == NULL)
17453 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017454 ga_init2(&ga, (int)sizeof(char), 80);
17455
17456#ifdef FEAT_MBYTE
17457 if (!has_mbyte)
17458#endif
17459 /* not multi-byte: fromstr and tostr must be the same length */
17460 if (STRLEN(fromstr) != STRLEN(tostr))
17461 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017462#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017463error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017464#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017465 EMSG2(_(e_invarg2), fromstr);
17466 ga_clear(&ga);
17467 return;
17468 }
17469
17470 /* fromstr and tostr have to contain the same number of chars */
17471 while (*instr != NUL)
17472 {
17473#ifdef FEAT_MBYTE
17474 if (has_mbyte)
17475 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017476 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017477 cpstr = instr;
17478 cplen = inlen;
17479 idx = 0;
17480 for (p = fromstr; *p != NUL; p += fromlen)
17481 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017482 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017483 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17484 {
17485 for (p = tostr; *p != NUL; p += tolen)
17486 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017487 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017488 if (idx-- == 0)
17489 {
17490 cplen = tolen;
17491 cpstr = p;
17492 break;
17493 }
17494 }
17495 if (*p == NUL) /* tostr is shorter than fromstr */
17496 goto error;
17497 break;
17498 }
17499 ++idx;
17500 }
17501
17502 if (first && cpstr == instr)
17503 {
17504 /* Check that fromstr and tostr have the same number of
17505 * (multi-byte) characters. Done only once when a character
17506 * of instr doesn't appear in fromstr. */
17507 first = FALSE;
17508 for (p = tostr; *p != NUL; p += tolen)
17509 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017510 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017511 --idx;
17512 }
17513 if (idx != 0)
17514 goto error;
17515 }
17516
17517 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017518 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017519 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017520
17521 instr += inlen;
17522 }
17523 else
17524#endif
17525 {
17526 /* When not using multi-byte chars we can do it faster. */
17527 p = vim_strchr(fromstr, *instr);
17528 if (p != NULL)
17529 ga_append(&ga, tostr[p - fromstr]);
17530 else
17531 ga_append(&ga, *instr);
17532 ++instr;
17533 }
17534 }
17535
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017536 /* add a terminating NUL */
17537 ga_grow(&ga, 1);
17538 ga_append(&ga, NUL);
17539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017540 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017541}
17542
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017543#ifdef FEAT_FLOAT
17544/*
17545 * "trunc({float})" function
17546 */
17547 static void
17548f_trunc(argvars, rettv)
17549 typval_T *argvars;
17550 typval_T *rettv;
17551{
17552 float_T f;
17553
17554 rettv->v_type = VAR_FLOAT;
17555 if (get_float_arg(argvars, &f) == OK)
17556 /* trunc() is not in C90, use floor() or ceil() instead. */
17557 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17558 else
17559 rettv->vval.v_float = 0.0;
17560}
17561#endif
17562
Bram Moolenaar8299df92004-07-10 09:47:34 +000017563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 * "type(expr)" function
17565 */
17566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017567f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017568 typval_T *argvars;
17569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017571 int n;
17572
17573 switch (argvars[0].v_type)
17574 {
17575 case VAR_NUMBER: n = 0; break;
17576 case VAR_STRING: n = 1; break;
17577 case VAR_FUNC: n = 2; break;
17578 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017579 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017580#ifdef FEAT_FLOAT
17581 case VAR_FLOAT: n = 5; break;
17582#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017583 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17584 }
17585 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586}
17587
17588/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017589 * "undofile(name)" function
17590 */
17591 static void
17592f_undofile(argvars, rettv)
17593 typval_T *argvars;
17594 typval_T *rettv;
17595{
17596 rettv->v_type = VAR_STRING;
17597#ifdef FEAT_PERSISTENT_UNDO
17598 rettv->vval.v_string = u_get_undo_file_name(get_tv_string(&argvars[0]),
17599 FALSE);
17600#else
17601 rettv->vval.v_string = NULL;
17602#endif
17603}
17604
17605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017606 * "values(dict)" function
17607 */
17608 static void
17609f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017610 typval_T *argvars;
17611 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017612{
17613 dict_list(argvars, rettv, 1);
17614}
17615
17616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 * "virtcol(string)" function
17618 */
17619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017620f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017621 typval_T *argvars;
17622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623{
17624 colnr_T vcol = 0;
17625 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017626 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017628 fp = var2fpos(&argvars[0], FALSE, &fnum);
17629 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17630 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 {
17632 getvvcol(curwin, fp, NULL, NULL, &vcol);
17633 ++vcol;
17634 }
17635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017636 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637}
17638
17639/*
17640 * "visualmode()" function
17641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017643f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017644 typval_T *argvars UNUSED;
17645 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646{
17647#ifdef FEAT_VISUAL
17648 char_u str[2];
17649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017650 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 str[0] = curbuf->b_visual_mode_eval;
17652 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017653 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654
17655 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017656 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658#endif
17659}
17660
17661/*
17662 * "winbufnr(nr)" function
17663 */
17664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017665f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017666 typval_T *argvars;
17667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668{
17669 win_T *wp;
17670
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017671 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017675 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676}
17677
17678/*
17679 * "wincol()" function
17680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017682f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685{
17686 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017687 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688}
17689
17690/*
17691 * "winheight(nr)" function
17692 */
17693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017694f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017695 typval_T *argvars;
17696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697{
17698 win_T *wp;
17699
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017700 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017704 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705}
17706
17707/*
17708 * "winline()" function
17709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017711f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017712 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714{
17715 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017716 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717}
17718
17719/*
17720 * "winnr()" function
17721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017723f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017724 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726{
17727 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017728
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017730 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017732 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733}
17734
17735/*
17736 * "winrestcmd()" function
17737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017739f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017740 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742{
17743#ifdef FEAT_WINDOWS
17744 win_T *wp;
17745 int winnr = 1;
17746 garray_T ga;
17747 char_u buf[50];
17748
17749 ga_init2(&ga, (int)sizeof(char), 70);
17750 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17751 {
17752 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17753 ga_concat(&ga, buf);
17754# ifdef FEAT_VERTSPLIT
17755 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17756 ga_concat(&ga, buf);
17757# endif
17758 ++winnr;
17759 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017760 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017762 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017766 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767}
17768
17769/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017770 * "winrestview()" function
17771 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017772 static void
17773f_winrestview(argvars, rettv)
17774 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017775 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017776{
17777 dict_T *dict;
17778
17779 if (argvars[0].v_type != VAR_DICT
17780 || (dict = argvars[0].vval.v_dict) == NULL)
17781 EMSG(_(e_invarg));
17782 else
17783 {
17784 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17785 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17786#ifdef FEAT_VIRTUALEDIT
17787 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17788#endif
17789 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017790 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017791
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017792 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017793#ifdef FEAT_DIFF
17794 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17795#endif
17796 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17797 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17798
17799 check_cursor();
17800 changed_cline_bef_curs();
17801 invalidate_botline();
17802 redraw_later(VALID);
17803
17804 if (curwin->w_topline == 0)
17805 curwin->w_topline = 1;
17806 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17807 curwin->w_topline = curbuf->b_ml.ml_line_count;
17808#ifdef FEAT_DIFF
17809 check_topfill(curwin, TRUE);
17810#endif
17811 }
17812}
17813
17814/*
17815 * "winsaveview()" function
17816 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017817 static void
17818f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017819 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017820 typval_T *rettv;
17821{
17822 dict_T *dict;
17823
17824 dict = dict_alloc();
17825 if (dict == NULL)
17826 return;
17827 rettv->v_type = VAR_DICT;
17828 rettv->vval.v_dict = dict;
17829 ++dict->dv_refcount;
17830
17831 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17832 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17833#ifdef FEAT_VIRTUALEDIT
17834 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17835#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017836 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017837 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17838
17839 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17840#ifdef FEAT_DIFF
17841 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17842#endif
17843 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17844 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17845}
17846
17847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 * "winwidth(nr)" function
17849 */
17850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017851f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017852 typval_T *argvars;
17853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854{
17855 win_T *wp;
17856
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017857 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017859 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 else
17861#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017862 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017864 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865#endif
17866}
17867
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017869 * "writefile()" function
17870 */
17871 static void
17872f_writefile(argvars, rettv)
17873 typval_T *argvars;
17874 typval_T *rettv;
17875{
17876 int binary = FALSE;
17877 char_u *fname;
17878 FILE *fd;
17879 listitem_T *li;
17880 char_u *s;
17881 int ret = 0;
17882 int c;
17883
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017884 if (check_restricted() || check_secure())
17885 return;
17886
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017887 if (argvars[0].v_type != VAR_LIST)
17888 {
17889 EMSG2(_(e_listarg), "writefile()");
17890 return;
17891 }
17892 if (argvars[0].vval.v_list == NULL)
17893 return;
17894
17895 if (argvars[2].v_type != VAR_UNKNOWN
17896 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17897 binary = TRUE;
17898
17899 /* Always open the file in binary mode, library functions have a mind of
17900 * their own about CR-LF conversion. */
17901 fname = get_tv_string(&argvars[1]);
17902 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17903 {
17904 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17905 ret = -1;
17906 }
17907 else
17908 {
17909 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17910 li = li->li_next)
17911 {
17912 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17913 {
17914 if (*s == '\n')
17915 c = putc(NUL, fd);
17916 else
17917 c = putc(*s, fd);
17918 if (c == EOF)
17919 {
17920 ret = -1;
17921 break;
17922 }
17923 }
17924 if (!binary || li->li_next != NULL)
17925 if (putc('\n', fd) == EOF)
17926 {
17927 ret = -1;
17928 break;
17929 }
17930 if (ret < 0)
17931 {
17932 EMSG(_(e_write));
17933 break;
17934 }
17935 }
17936 fclose(fd);
17937 }
17938
17939 rettv->vval.v_number = ret;
17940}
17941
17942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017944 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 */
17946 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017947var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017948 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017949 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017950 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017952 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017954 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955
Bram Moolenaara5525202006-03-02 22:52:09 +000017956 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017957 if (varp->v_type == VAR_LIST)
17958 {
17959 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017960 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017961 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017962 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017963
17964 l = varp->vval.v_list;
17965 if (l == NULL)
17966 return NULL;
17967
17968 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017969 pos.lnum = list_find_nr(l, 0L, &error);
17970 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017971 return NULL; /* invalid line number */
17972
17973 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017974 pos.col = list_find_nr(l, 1L, &error);
17975 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017976 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017977 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017978
17979 /* We accept "$" for the column number: last column. */
17980 li = list_find(l, 1L);
17981 if (li != NULL && li->li_tv.v_type == VAR_STRING
17982 && li->li_tv.vval.v_string != NULL
17983 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17984 pos.col = len + 1;
17985
Bram Moolenaara5525202006-03-02 22:52:09 +000017986 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017987 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017988 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017989 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017990
Bram Moolenaara5525202006-03-02 22:52:09 +000017991#ifdef FEAT_VIRTUALEDIT
17992 /* Get the virtual offset. Defaults to zero. */
17993 pos.coladd = list_find_nr(l, 2L, &error);
17994 if (error)
17995 pos.coladd = 0;
17996#endif
17997
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017998 return &pos;
17999 }
18000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018001 name = get_tv_string_chk(varp);
18002 if (name == NULL)
18003 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018004 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018006#ifdef FEAT_VISUAL
18007 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18008 {
18009 if (VIsual_active)
18010 return &VIsual;
18011 return &curwin->w_cursor;
18012 }
18013#endif
18014 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018016 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18018 return NULL;
18019 return pp;
18020 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018021
18022#ifdef FEAT_VIRTUALEDIT
18023 pos.coladd = 0;
18024#endif
18025
Bram Moolenaar477933c2007-07-17 14:32:23 +000018026 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018027 {
18028 pos.col = 0;
18029 if (name[1] == '0') /* "w0": first visible line */
18030 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018031 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018032 pos.lnum = curwin->w_topline;
18033 return &pos;
18034 }
18035 else if (name[1] == '$') /* "w$": last visible line */
18036 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018037 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018038 pos.lnum = curwin->w_botline - 1;
18039 return &pos;
18040 }
18041 }
18042 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018044 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 {
18046 pos.lnum = curbuf->b_ml.ml_line_count;
18047 pos.col = 0;
18048 }
18049 else
18050 {
18051 pos.lnum = curwin->w_cursor.lnum;
18052 pos.col = (colnr_T)STRLEN(ml_get_curline());
18053 }
18054 return &pos;
18055 }
18056 return NULL;
18057}
18058
18059/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018060 * Convert list in "arg" into a position and optional file number.
18061 * When "fnump" is NULL there is no file number, only 3 items.
18062 * Note that the column is passed on as-is, the caller may want to decrement
18063 * it to use 1 for the first column.
18064 * Return FAIL when conversion is not possible, doesn't check the position for
18065 * validity.
18066 */
18067 static int
18068list2fpos(arg, posp, fnump)
18069 typval_T *arg;
18070 pos_T *posp;
18071 int *fnump;
18072{
18073 list_T *l = arg->vval.v_list;
18074 long i = 0;
18075 long n;
18076
Bram Moolenaarbde35262006-07-23 20:12:24 +000018077 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18078 * when "fnump" isn't NULL and "coladd" is optional. */
18079 if (arg->v_type != VAR_LIST
18080 || l == NULL
18081 || l->lv_len < (fnump == NULL ? 2 : 3)
18082 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018083 return FAIL;
18084
18085 if (fnump != NULL)
18086 {
18087 n = list_find_nr(l, i++, NULL); /* fnum */
18088 if (n < 0)
18089 return FAIL;
18090 if (n == 0)
18091 n = curbuf->b_fnum; /* current buffer */
18092 *fnump = n;
18093 }
18094
18095 n = list_find_nr(l, i++, NULL); /* lnum */
18096 if (n < 0)
18097 return FAIL;
18098 posp->lnum = n;
18099
18100 n = list_find_nr(l, i++, NULL); /* col */
18101 if (n < 0)
18102 return FAIL;
18103 posp->col = n;
18104
18105#ifdef FEAT_VIRTUALEDIT
18106 n = list_find_nr(l, i, NULL);
18107 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018108 posp->coladd = 0;
18109 else
18110 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018111#endif
18112
18113 return OK;
18114}
18115
18116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 * Get the length of an environment variable name.
18118 * Advance "arg" to the first character after the name.
18119 * Return 0 for error.
18120 */
18121 static int
18122get_env_len(arg)
18123 char_u **arg;
18124{
18125 char_u *p;
18126 int len;
18127
18128 for (p = *arg; vim_isIDc(*p); ++p)
18129 ;
18130 if (p == *arg) /* no name found */
18131 return 0;
18132
18133 len = (int)(p - *arg);
18134 *arg = p;
18135 return len;
18136}
18137
18138/*
18139 * Get the length of the name of a function or internal variable.
18140 * "arg" is advanced to the first non-white character after the name.
18141 * Return 0 if something is wrong.
18142 */
18143 static int
18144get_id_len(arg)
18145 char_u **arg;
18146{
18147 char_u *p;
18148 int len;
18149
18150 /* Find the end of the name. */
18151 for (p = *arg; eval_isnamec(*p); ++p)
18152 ;
18153 if (p == *arg) /* no name found */
18154 return 0;
18155
18156 len = (int)(p - *arg);
18157 *arg = skipwhite(p);
18158
18159 return len;
18160}
18161
18162/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018163 * Get the length of the name of a variable or function.
18164 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018166 * Return -1 if curly braces expansion failed.
18167 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 * If the name contains 'magic' {}'s, expand them and return the
18169 * expanded name in an allocated string via 'alias' - caller must free.
18170 */
18171 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018172get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 char_u **arg;
18174 char_u **alias;
18175 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018176 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177{
18178 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 char_u *p;
18180 char_u *expr_start;
18181 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182
18183 *alias = NULL; /* default to no alias */
18184
18185 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18186 && (*arg)[2] == (int)KE_SNR)
18187 {
18188 /* hard coded <SNR>, already translated */
18189 *arg += 3;
18190 return get_id_len(arg) + 3;
18191 }
18192 len = eval_fname_script(*arg);
18193 if (len > 0)
18194 {
18195 /* literal "<SID>", "s:" or "<SNR>" */
18196 *arg += len;
18197 }
18198
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018202 p = find_name_end(*arg, &expr_start, &expr_end,
18203 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 if (expr_start != NULL)
18205 {
18206 char_u *temp_string;
18207
18208 if (!evaluate)
18209 {
18210 len += (int)(p - *arg);
18211 *arg = skipwhite(p);
18212 return len;
18213 }
18214
18215 /*
18216 * Include any <SID> etc in the expanded string:
18217 * Thus the -len here.
18218 */
18219 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18220 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018221 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 *alias = temp_string;
18223 *arg = skipwhite(p);
18224 return (int)STRLEN(temp_string);
18225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226
18227 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018228 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 EMSG2(_(e_invexpr2), *arg);
18230
18231 return len;
18232}
18233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018234/*
18235 * Find the end of a variable or function name, taking care of magic braces.
18236 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18237 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018238 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018239 * Return a pointer to just after the name. Equal to "arg" if there is no
18240 * valid name.
18241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018243find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 char_u *arg;
18245 char_u **expr_start;
18246 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018247 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018249 int mb_nest = 0;
18250 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 char_u *p;
18252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018253 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018255 *expr_start = NULL;
18256 *expr_end = NULL;
18257 }
18258
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018259 /* Quick check for valid starting character. */
18260 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18261 return arg;
18262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018263 for (p = arg; *p != NUL
18264 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018265 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018266 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018267 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018268 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018269 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018270 if (*p == '\'')
18271 {
18272 /* skip over 'string' to avoid counting [ and ] inside it. */
18273 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18274 ;
18275 if (*p == NUL)
18276 break;
18277 }
18278 else if (*p == '"')
18279 {
18280 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18281 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18282 if (*p == '\\' && p[1] != NUL)
18283 ++p;
18284 if (*p == NUL)
18285 break;
18286 }
18287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018288 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018290 if (*p == '[')
18291 ++br_nest;
18292 else if (*p == ']')
18293 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018296 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018298 if (*p == '{')
18299 {
18300 mb_nest++;
18301 if (expr_start != NULL && *expr_start == NULL)
18302 *expr_start = p;
18303 }
18304 else if (*p == '}')
18305 {
18306 mb_nest--;
18307 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18308 *expr_end = p;
18309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 }
18312
18313 return p;
18314}
18315
18316/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018317 * Expands out the 'magic' {}'s in a variable/function name.
18318 * Note that this can call itself recursively, to deal with
18319 * constructs like foo{bar}{baz}{bam}
18320 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18321 * "in_start" ^
18322 * "expr_start" ^
18323 * "expr_end" ^
18324 * "in_end" ^
18325 *
18326 * Returns a new allocated string, which the caller must free.
18327 * Returns NULL for failure.
18328 */
18329 static char_u *
18330make_expanded_name(in_start, expr_start, expr_end, in_end)
18331 char_u *in_start;
18332 char_u *expr_start;
18333 char_u *expr_end;
18334 char_u *in_end;
18335{
18336 char_u c1;
18337 char_u *retval = NULL;
18338 char_u *temp_result;
18339 char_u *nextcmd = NULL;
18340
18341 if (expr_end == NULL || in_end == NULL)
18342 return NULL;
18343 *expr_start = NUL;
18344 *expr_end = NUL;
18345 c1 = *in_end;
18346 *in_end = NUL;
18347
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018348 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018349 if (temp_result != NULL && nextcmd == NULL)
18350 {
18351 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18352 + (in_end - expr_end) + 1));
18353 if (retval != NULL)
18354 {
18355 STRCPY(retval, in_start);
18356 STRCAT(retval, temp_result);
18357 STRCAT(retval, expr_end + 1);
18358 }
18359 }
18360 vim_free(temp_result);
18361
18362 *in_end = c1; /* put char back for error messages */
18363 *expr_start = '{';
18364 *expr_end = '}';
18365
18366 if (retval != NULL)
18367 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018368 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018369 if (expr_start != NULL)
18370 {
18371 /* Further expansion! */
18372 temp_result = make_expanded_name(retval, expr_start,
18373 expr_end, temp_result);
18374 vim_free(retval);
18375 retval = temp_result;
18376 }
18377 }
18378
18379 return retval;
18380}
18381
18382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018384 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 */
18386 static int
18387eval_isnamec(c)
18388 int c;
18389{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018390 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18391}
18392
18393/*
18394 * Return TRUE if character "c" can be used as the first character in a
18395 * variable or function name (excluding '{' and '}').
18396 */
18397 static int
18398eval_isnamec1(c)
18399 int c;
18400{
18401 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402}
18403
18404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405 * Set number v: variable to "val".
18406 */
18407 void
18408set_vim_var_nr(idx, val)
18409 int idx;
18410 long val;
18411{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018412 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413}
18414
18415/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018416 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 */
18418 long
18419get_vim_var_nr(idx)
18420 int idx;
18421{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018422 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423}
18424
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018425/*
18426 * Get string v: variable value. Uses a static buffer, can only be used once.
18427 */
18428 char_u *
18429get_vim_var_str(idx)
18430 int idx;
18431{
18432 return get_tv_string(&vimvars[idx].vv_tv);
18433}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018434
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018436 * Get List v: variable value. Caller must take care of reference count when
18437 * needed.
18438 */
18439 list_T *
18440get_vim_var_list(idx)
18441 int idx;
18442{
18443 return vimvars[idx].vv_list;
18444}
18445
18446/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018447 * Set v:char to character "c".
18448 */
18449 void
18450set_vim_var_char(c)
18451 int c;
18452{
18453#ifdef FEAT_MBYTE
18454 char_u buf[MB_MAXBYTES];
18455#else
18456 char_u buf[2];
18457#endif
18458
18459#ifdef FEAT_MBYTE
18460 if (has_mbyte)
18461 buf[(*mb_char2bytes)(c, buf)] = NUL;
18462 else
18463#endif
18464 {
18465 buf[0] = c;
18466 buf[1] = NUL;
18467 }
18468 set_vim_var_string(VV_CHAR, buf, -1);
18469}
18470
18471/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018472 * Set v:count to "count" and v:count1 to "count1".
18473 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 */
18475 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018476set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 long count;
18478 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018479 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018481 if (set_prevcount)
18482 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018483 vimvars[VV_COUNT].vv_nr = count;
18484 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485}
18486
18487/*
18488 * Set string v: variable to a copy of "val".
18489 */
18490 void
18491set_vim_var_string(idx, val, len)
18492 int idx;
18493 char_u *val;
18494 int len; /* length of "val" to use or -1 (whole string) */
18495{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018496 /* Need to do this (at least) once, since we can't initialize a union.
18497 * Will always be invoked when "v:progname" is set. */
18498 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18499
Bram Moolenaare9a41262005-01-15 22:18:47 +000018500 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018502 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018504 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018506 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507}
18508
18509/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018510 * Set List v: variable to "val".
18511 */
18512 void
18513set_vim_var_list(idx, val)
18514 int idx;
18515 list_T *val;
18516{
18517 list_unref(vimvars[idx].vv_list);
18518 vimvars[idx].vv_list = val;
18519 if (val != NULL)
18520 ++val->lv_refcount;
18521}
18522
18523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 * Set v:register if needed.
18525 */
18526 void
18527set_reg_var(c)
18528 int c;
18529{
18530 char_u regname;
18531
18532 if (c == 0 || c == ' ')
18533 regname = '"';
18534 else
18535 regname = c;
18536 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018537 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 set_vim_var_string(VV_REG, &regname, 1);
18539}
18540
18541/*
18542 * Get or set v:exception. If "oldval" == NULL, return the current value.
18543 * Otherwise, restore the value to "oldval" and return NULL.
18544 * Must always be called in pairs to save and restore v:exception! Does not
18545 * take care of memory allocations.
18546 */
18547 char_u *
18548v_exception(oldval)
18549 char_u *oldval;
18550{
18551 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018552 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553
Bram Moolenaare9a41262005-01-15 22:18:47 +000018554 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 return NULL;
18556}
18557
18558/*
18559 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18560 * Otherwise, restore the value to "oldval" and return NULL.
18561 * Must always be called in pairs to save and restore v:throwpoint! Does not
18562 * take care of memory allocations.
18563 */
18564 char_u *
18565v_throwpoint(oldval)
18566 char_u *oldval;
18567{
18568 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018569 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570
Bram Moolenaare9a41262005-01-15 22:18:47 +000018571 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 return NULL;
18573}
18574
18575#if defined(FEAT_AUTOCMD) || defined(PROTO)
18576/*
18577 * Set v:cmdarg.
18578 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18579 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18580 * Must always be called in pairs!
18581 */
18582 char_u *
18583set_cmdarg(eap, oldarg)
18584 exarg_T *eap;
18585 char_u *oldarg;
18586{
18587 char_u *oldval;
18588 char_u *newval;
18589 unsigned len;
18590
Bram Moolenaare9a41262005-01-15 22:18:47 +000018591 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018592 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018594 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018595 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018596 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 }
18598
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018599 if (eap->force_bin == FORCE_BIN)
18600 len = 6;
18601 else if (eap->force_bin == FORCE_NOBIN)
18602 len = 8;
18603 else
18604 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018605
18606 if (eap->read_edit)
18607 len += 7;
18608
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018609 if (eap->force_ff != 0)
18610 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18611# ifdef FEAT_MBYTE
18612 if (eap->force_enc != 0)
18613 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018614 if (eap->bad_char != 0)
18615 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018616# endif
18617
18618 newval = alloc(len + 1);
18619 if (newval == NULL)
18620 return NULL;
18621
18622 if (eap->force_bin == FORCE_BIN)
18623 sprintf((char *)newval, " ++bin");
18624 else if (eap->force_bin == FORCE_NOBIN)
18625 sprintf((char *)newval, " ++nobin");
18626 else
18627 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018628
18629 if (eap->read_edit)
18630 STRCAT(newval, " ++edit");
18631
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018632 if (eap->force_ff != 0)
18633 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18634 eap->cmd + eap->force_ff);
18635# ifdef FEAT_MBYTE
18636 if (eap->force_enc != 0)
18637 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18638 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018639 if (eap->bad_char == BAD_KEEP)
18640 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18641 else if (eap->bad_char == BAD_DROP)
18642 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18643 else if (eap->bad_char != 0)
18644 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018645# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018646 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018647 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648}
18649#endif
18650
18651/*
18652 * Get the value of internal variable "name".
18653 * Return OK or FAIL.
18654 */
18655 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018656get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 char_u *name;
18658 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018659 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018660 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661{
18662 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018663 typval_T *tv = NULL;
18664 typval_T atv;
18665 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667
18668 /* truncate the name, so that we can use strcmp() */
18669 cc = name[len];
18670 name[len] = NUL;
18671
18672 /*
18673 * Check for "b:changedtick".
18674 */
18675 if (STRCMP(name, "b:changedtick") == 0)
18676 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018677 atv.v_type = VAR_NUMBER;
18678 atv.vval.v_number = curbuf->b_changedtick;
18679 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 }
18681
18682 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 * Check for user-defined variables.
18684 */
18685 else
18686 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018687 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018689 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 }
18691
Bram Moolenaare9a41262005-01-15 22:18:47 +000018692 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018694 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018695 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 ret = FAIL;
18697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018699 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700
18701 name[len] = cc;
18702
18703 return ret;
18704}
18705
18706/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018707 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18708 * Also handle function call with Funcref variable: func(expr)
18709 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18710 */
18711 static int
18712handle_subscript(arg, rettv, evaluate, verbose)
18713 char_u **arg;
18714 typval_T *rettv;
18715 int evaluate; /* do more than finding the end */
18716 int verbose; /* give error messages */
18717{
18718 int ret = OK;
18719 dict_T *selfdict = NULL;
18720 char_u *s;
18721 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018722 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018723
18724 while (ret == OK
18725 && (**arg == '['
18726 || (**arg == '.' && rettv->v_type == VAR_DICT)
18727 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18728 && !vim_iswhite(*(*arg - 1)))
18729 {
18730 if (**arg == '(')
18731 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018732 /* need to copy the funcref so that we can clear rettv */
18733 functv = *rettv;
18734 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018735
18736 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018737 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018738 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018739 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18740 &len, evaluate, selfdict);
18741
18742 /* Clear the funcref afterwards, so that deleting it while
18743 * evaluating the arguments is possible (see test55). */
18744 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018745
18746 /* Stop the expression evaluation when immediately aborting on
18747 * error, or when an interrupt occurred or an exception was thrown
18748 * but not caught. */
18749 if (aborting())
18750 {
18751 if (ret == OK)
18752 clear_tv(rettv);
18753 ret = FAIL;
18754 }
18755 dict_unref(selfdict);
18756 selfdict = NULL;
18757 }
18758 else /* **arg == '[' || **arg == '.' */
18759 {
18760 dict_unref(selfdict);
18761 if (rettv->v_type == VAR_DICT)
18762 {
18763 selfdict = rettv->vval.v_dict;
18764 if (selfdict != NULL)
18765 ++selfdict->dv_refcount;
18766 }
18767 else
18768 selfdict = NULL;
18769 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18770 {
18771 clear_tv(rettv);
18772 ret = FAIL;
18773 }
18774 }
18775 }
18776 dict_unref(selfdict);
18777 return ret;
18778}
18779
18780/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018781 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018782 * value).
18783 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018784 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018785alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018786{
Bram Moolenaar33570922005-01-25 22:26:29 +000018787 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018788}
18789
18790/*
18791 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 * The string "s" must have been allocated, it is consumed.
18793 * Return NULL for out of memory, the variable otherwise.
18794 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018795 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018796alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 char_u *s;
18798{
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018801 rettv = alloc_tv();
18802 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018804 rettv->v_type = VAR_STRING;
18805 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 }
18807 else
18808 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018809 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810}
18811
18812/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018813 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018815 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018816free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018817 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818{
18819 if (varp != NULL)
18820 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018821 switch (varp->v_type)
18822 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018823 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018824 func_unref(varp->vval.v_string);
18825 /*FALLTHROUGH*/
18826 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018827 vim_free(varp->vval.v_string);
18828 break;
18829 case VAR_LIST:
18830 list_unref(varp->vval.v_list);
18831 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018832 case VAR_DICT:
18833 dict_unref(varp->vval.v_dict);
18834 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018835 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018836#ifdef FEAT_FLOAT
18837 case VAR_FLOAT:
18838#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018839 case VAR_UNKNOWN:
18840 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018841 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018842 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018843 break;
18844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 vim_free(varp);
18846 }
18847}
18848
18849/*
18850 * Free the memory for a variable value and set the value to NULL or 0.
18851 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018852 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018853clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018854 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855{
18856 if (varp != NULL)
18857 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018858 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018860 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018861 func_unref(varp->vval.v_string);
18862 /*FALLTHROUGH*/
18863 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018864 vim_free(varp->vval.v_string);
18865 varp->vval.v_string = NULL;
18866 break;
18867 case VAR_LIST:
18868 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018869 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018870 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018871 case VAR_DICT:
18872 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018873 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018874 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018876 varp->vval.v_number = 0;
18877 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018878#ifdef FEAT_FLOAT
18879 case VAR_FLOAT:
18880 varp->vval.v_float = 0.0;
18881 break;
18882#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018883 case VAR_UNKNOWN:
18884 break;
18885 default:
18886 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018888 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889 }
18890}
18891
18892/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018893 * Set the value of a variable to NULL without freeing items.
18894 */
18895 static void
18896init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018897 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018898{
18899 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018900 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018901}
18902
18903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 * Get the number value of a variable.
18905 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018906 * For incompatible types, return 0.
18907 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18908 * caller of incompatible types: it sets *denote to TRUE if "denote"
18909 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 */
18911 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018912get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018913 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018915 int error = FALSE;
18916
18917 return get_tv_number_chk(varp, &error); /* return 0L on error */
18918}
18919
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018920 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018921get_tv_number_chk(varp, denote)
18922 typval_T *varp;
18923 int *denote;
18924{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018925 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018927 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018929 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018930 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018931#ifdef FEAT_FLOAT
18932 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018933 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018934 break;
18935#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018936 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018937 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018938 break;
18939 case VAR_STRING:
18940 if (varp->vval.v_string != NULL)
18941 vim_str2nr(varp->vval.v_string, NULL, NULL,
18942 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018943 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018944 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018945 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018946 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018947 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018948 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018949 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018950 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018951 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018954 if (denote == NULL) /* useful for values that must be unsigned */
18955 n = -1;
18956 else
18957 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018958 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959}
18960
18961/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018962 * Get the lnum from the first argument.
18963 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018964 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 */
18966 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018967get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018968 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969{
Bram Moolenaar33570922005-01-25 22:26:29 +000018970 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 linenr_T lnum;
18972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018973 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 if (lnum == 0) /* no valid number, try using line() */
18975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018976 rettv.v_type = VAR_NUMBER;
18977 f_line(argvars, &rettv);
18978 lnum = rettv.vval.v_number;
18979 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 }
18981 return lnum;
18982}
18983
18984/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018985 * Get the lnum from the first argument.
18986 * Also accepts "$", then "buf" is used.
18987 * Returns 0 on error.
18988 */
18989 static linenr_T
18990get_tv_lnum_buf(argvars, buf)
18991 typval_T *argvars;
18992 buf_T *buf;
18993{
18994 if (argvars[0].v_type == VAR_STRING
18995 && argvars[0].vval.v_string != NULL
18996 && argvars[0].vval.v_string[0] == '$'
18997 && buf != NULL)
18998 return buf->b_ml.ml_line_count;
18999 return get_tv_number_chk(&argvars[0], NULL);
19000}
19001
19002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 * Get the string value of a variable.
19004 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019005 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19006 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 * If the String variable has never been set, return an empty string.
19008 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019009 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19010 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 */
19012 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019013get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019014 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015{
19016 static char_u mybuf[NUMBUFLEN];
19017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019018 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019}
19020
19021 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019022get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019023 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019024 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019026 char_u *res = get_tv_string_buf_chk(varp, buf);
19027
19028 return res != NULL ? res : (char_u *)"";
19029}
19030
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019031 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019032get_tv_string_chk(varp)
19033 typval_T *varp;
19034{
19035 static char_u mybuf[NUMBUFLEN];
19036
19037 return get_tv_string_buf_chk(varp, mybuf);
19038}
19039
19040 static char_u *
19041get_tv_string_buf_chk(varp, buf)
19042 typval_T *varp;
19043 char_u *buf;
19044{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019045 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019047 case VAR_NUMBER:
19048 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19049 return buf;
19050 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019051 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019052 break;
19053 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019054 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019055 break;
19056 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019057 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019058 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019059#ifdef FEAT_FLOAT
19060 case VAR_FLOAT:
19061 EMSG(_("E806: using Float as a String"));
19062 break;
19063#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019064 case VAR_STRING:
19065 if (varp->vval.v_string != NULL)
19066 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019067 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019068 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019069 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019072 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073}
19074
19075/*
19076 * Find variable "name" in the list of variables.
19077 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019078 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019079 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019080 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019083find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019085 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019088 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089
Bram Moolenaara7043832005-01-21 11:56:39 +000019090 ht = find_var_ht(name, &varname);
19091 if (htp != NULL)
19092 *htp = ht;
19093 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019095 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096}
19097
19098/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019099 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019100 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019102 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019103find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019104 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019105 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019106 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019107{
Bram Moolenaar33570922005-01-25 22:26:29 +000019108 hashitem_T *hi;
19109
19110 if (*varname == NUL)
19111 {
19112 /* Must be something like "s:", otherwise "ht" would be NULL. */
19113 switch (varname[-2])
19114 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019115 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019116 case 'g': return &globvars_var;
19117 case 'v': return &vimvars_var;
19118 case 'b': return &curbuf->b_bufvar;
19119 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019120#ifdef FEAT_WINDOWS
19121 case 't': return &curtab->tp_winvar;
19122#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019123 case 'l': return current_funccal == NULL
19124 ? NULL : &current_funccal->l_vars_var;
19125 case 'a': return current_funccal == NULL
19126 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019127 }
19128 return NULL;
19129 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019130
19131 hi = hash_find(ht, varname);
19132 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019133 {
19134 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019135 * worked find the variable again. Don't auto-load a script if it was
19136 * loaded already, otherwise it would be loaded every time when
19137 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019138 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019139 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019140 hi = hash_find(ht, varname);
19141 if (HASHITEM_EMPTY(hi))
19142 return NULL;
19143 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019144 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019145}
19146
19147/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019148 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019149 * Set "varname" to the start of name without ':'.
19150 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019151 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019152find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 char_u *name;
19154 char_u **varname;
19155{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019156 hashitem_T *hi;
19157
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 if (name[1] != ':')
19159 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019160 /* The name must not start with a colon or #. */
19161 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 return NULL;
19163 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019164
19165 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019166 hi = hash_find(&compat_hashtab, name);
19167 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019168 return &compat_hashtab;
19169
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 return &globvarht; /* global variable */
19172 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 }
19174 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019175 if (*name == 'g') /* global variable */
19176 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019177 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19178 */
19179 if (vim_strchr(name + 2, ':') != NULL
19180 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019181 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019183 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019185 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019186#ifdef FEAT_WINDOWS
19187 if (*name == 't') /* tab page variable */
19188 return &curtab->tp_vars.dv_hashtab;
19189#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019190 if (*name == 'v') /* v: variable */
19191 return &vimvarht;
19192 if (*name == 'a' && current_funccal != NULL) /* function argument */
19193 return &current_funccal->l_avars.dv_hashtab;
19194 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19195 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 if (*name == 's' /* script variable */
19197 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19198 return &SCRIPT_VARS(current_SID);
19199 return NULL;
19200}
19201
19202/*
19203 * Get the string value of a (global/local) variable.
19204 * Returns NULL when it doesn't exist.
19205 */
19206 char_u *
19207get_var_value(name)
19208 char_u *name;
19209{
Bram Moolenaar33570922005-01-25 22:26:29 +000019210 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211
Bram Moolenaara7043832005-01-21 11:56:39 +000019212 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213 if (v == NULL)
19214 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019215 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216}
19217
19218/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019219 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 * sourcing this script and when executing functions defined in the script.
19221 */
19222 void
19223new_script_vars(id)
19224 scid_T id;
19225{
Bram Moolenaara7043832005-01-21 11:56:39 +000019226 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 hashtab_T *ht;
19228 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019229
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19231 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019232 /* Re-allocating ga_data means that an ht_array pointing to
19233 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019234 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019235 for (i = 1; i <= ga_scripts.ga_len; ++i)
19236 {
19237 ht = &SCRIPT_VARS(i);
19238 if (ht->ht_mask == HT_INIT_SIZE - 1)
19239 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019240 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019241 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019242 }
19243
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 while (ga_scripts.ga_len < id)
19245 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019246 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
19247 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019248 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 }
19251 }
19252}
19253
19254/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019255 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19256 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 */
19258 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019259init_var_dict(dict, dict_var)
19260 dict_T *dict;
19261 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262{
Bram Moolenaar33570922005-01-25 22:26:29 +000019263 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019264 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019265 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019266 dict_var->di_tv.vval.v_dict = dict;
19267 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019268 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019269 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19270 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271}
19272
19273/*
19274 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019275 * Frees all allocated variables and the value they contain.
19276 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 */
19278 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019279vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019280 hashtab_T *ht;
19281{
19282 vars_clear_ext(ht, TRUE);
19283}
19284
19285/*
19286 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19287 */
19288 static void
19289vars_clear_ext(ht, free_val)
19290 hashtab_T *ht;
19291 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292{
Bram Moolenaara7043832005-01-21 11:56:39 +000019293 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019294 hashitem_T *hi;
19295 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296
Bram Moolenaar33570922005-01-25 22:26:29 +000019297 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019298 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019299 for (hi = ht->ht_array; todo > 0; ++hi)
19300 {
19301 if (!HASHITEM_EMPTY(hi))
19302 {
19303 --todo;
19304
Bram Moolenaar33570922005-01-25 22:26:29 +000019305 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019306 * ht_array might change then. hash_clear() takes care of it
19307 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 v = HI2DI(hi);
19309 if (free_val)
19310 clear_tv(&v->di_tv);
19311 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19312 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019313 }
19314 }
19315 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019316 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317}
19318
Bram Moolenaara7043832005-01-21 11:56:39 +000019319/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019320 * Delete a variable from hashtab "ht" at item "hi".
19321 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019324delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019325 hashtab_T *ht;
19326 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327{
Bram Moolenaar33570922005-01-25 22:26:29 +000019328 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019329
19330 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019331 clear_tv(&di->di_tv);
19332 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333}
19334
19335/*
19336 * List the value of one internal variable.
19337 */
19338 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019339list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019340 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019342 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019344 char_u *tofree;
19345 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019346 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019347
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019348 current_copyID += COPYID_INC;
19349 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019350 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019351 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019352 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353}
19354
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019356list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357 char_u *prefix;
19358 char_u *name;
19359 int type;
19360 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019361 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362{
Bram Moolenaar31859182007-08-14 20:41:13 +000019363 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19364 msg_start();
19365 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 if (name != NULL) /* "a:" vars don't have a name stored */
19367 msg_puts(name);
19368 msg_putchar(' ');
19369 msg_advance(22);
19370 if (type == VAR_NUMBER)
19371 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019372 else if (type == VAR_FUNC)
19373 msg_putchar('*');
19374 else if (type == VAR_LIST)
19375 {
19376 msg_putchar('[');
19377 if (*string == '[')
19378 ++string;
19379 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019380 else if (type == VAR_DICT)
19381 {
19382 msg_putchar('{');
19383 if (*string == '{')
19384 ++string;
19385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 else
19387 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019388
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019390
19391 if (type == VAR_FUNC)
19392 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019393 if (*first)
19394 {
19395 msg_clr_eos();
19396 *first = FALSE;
19397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398}
19399
19400/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019401 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 * If the variable already exists, the value is updated.
19403 * Otherwise the variable is created.
19404 */
19405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019406set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019408 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019409 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410{
Bram Moolenaar33570922005-01-25 22:26:29 +000019411 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019413 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019414 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019416 ht = find_var_ht(name, &varname);
19417 if (ht == NULL || *varname == NUL)
19418 {
19419 EMSG2(_(e_illvar), name);
19420 return;
19421 }
19422 v = find_var_in_ht(ht, varname, TRUE);
19423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019424 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019425 {
19426 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19427 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19428 ? name[2] : name[0]))
19429 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019430 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019431 return;
19432 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019433 /* Don't allow hiding a function. When "v" is not NULL we migth be
19434 * assigning another function to the same var, the type is checked
19435 * below. */
19436 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019437 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019438 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019439 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019440 return;
19441 }
19442 }
19443
Bram Moolenaar33570922005-01-25 22:26:29 +000019444 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019446 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019447 if (var_check_ro(v->di_flags, name)
19448 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019449 return;
19450 if (v->di_tv.v_type != tv->v_type
19451 && !((v->di_tv.v_type == VAR_STRING
19452 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019453 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019454 || tv->v_type == VAR_NUMBER))
19455#ifdef FEAT_FLOAT
19456 && !((v->di_tv.v_type == VAR_NUMBER
19457 || v->di_tv.v_type == VAR_FLOAT)
19458 && (tv->v_type == VAR_NUMBER
19459 || tv->v_type == VAR_FLOAT))
19460#endif
19461 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019462 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019463 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019464 return;
19465 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019466
19467 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019468 * Handle setting internal v: variables separately: we don't change
19469 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019470 */
19471 if (ht == &vimvarht)
19472 {
19473 if (v->di_tv.v_type == VAR_STRING)
19474 {
19475 vim_free(v->di_tv.vval.v_string);
19476 if (copy || tv->v_type != VAR_STRING)
19477 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19478 else
19479 {
19480 /* Take over the string to avoid an extra alloc/free. */
19481 v->di_tv.vval.v_string = tv->vval.v_string;
19482 tv->vval.v_string = NULL;
19483 }
19484 }
19485 else if (v->di_tv.v_type != VAR_NUMBER)
19486 EMSG2(_(e_intern2), "set_var()");
19487 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019488 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019489 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019490 if (STRCMP(varname, "searchforward") == 0)
19491 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19492 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019493 return;
19494 }
19495
19496 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 }
19498 else /* add a new variable */
19499 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019500 /* Can't add "v:" variable. */
19501 if (ht == &vimvarht)
19502 {
19503 EMSG2(_(e_illvar), name);
19504 return;
19505 }
19506
Bram Moolenaar92124a32005-06-17 22:03:40 +000019507 /* Make sure the variable name is valid. */
19508 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019509 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19510 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019511 {
19512 EMSG2(_(e_illvar), varname);
19513 return;
19514 }
19515
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019516 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19517 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019518 if (v == NULL)
19519 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019520 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019521 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019523 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 return;
19525 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019526 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019528
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019529 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019530 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019531 else
19532 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019533 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019534 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019535 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537}
19538
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019539/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019540 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019541 * Also give an error message.
19542 */
19543 static int
19544var_check_ro(flags, name)
19545 int flags;
19546 char_u *name;
19547{
19548 if (flags & DI_FLAGS_RO)
19549 {
19550 EMSG2(_(e_readonlyvar), name);
19551 return TRUE;
19552 }
19553 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19554 {
19555 EMSG2(_(e_readonlysbx), name);
19556 return TRUE;
19557 }
19558 return FALSE;
19559}
19560
19561/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019562 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19563 * Also give an error message.
19564 */
19565 static int
19566var_check_fixed(flags, name)
19567 int flags;
19568 char_u *name;
19569{
19570 if (flags & DI_FLAGS_FIX)
19571 {
19572 EMSG2(_("E795: Cannot delete variable %s"), name);
19573 return TRUE;
19574 }
19575 return FALSE;
19576}
19577
19578/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019579 * Return TRUE if typeval "tv" is set to be locked (immutable).
19580 * Also give an error message, using "name".
19581 */
19582 static int
19583tv_check_lock(lock, name)
19584 int lock;
19585 char_u *name;
19586{
19587 if (lock & VAR_LOCKED)
19588 {
19589 EMSG2(_("E741: Value is locked: %s"),
19590 name == NULL ? (char_u *)_("Unknown") : name);
19591 return TRUE;
19592 }
19593 if (lock & VAR_FIXED)
19594 {
19595 EMSG2(_("E742: Cannot change value of %s"),
19596 name == NULL ? (char_u *)_("Unknown") : name);
19597 return TRUE;
19598 }
19599 return FALSE;
19600}
19601
19602/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019604 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019605 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019606 * It is OK for "from" and "to" to point to the same item. This is used to
19607 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019608 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019609 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019610copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019611 typval_T *from;
19612 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019614 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019615 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019616 switch (from->v_type)
19617 {
19618 case VAR_NUMBER:
19619 to->vval.v_number = from->vval.v_number;
19620 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019621#ifdef FEAT_FLOAT
19622 case VAR_FLOAT:
19623 to->vval.v_float = from->vval.v_float;
19624 break;
19625#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019626 case VAR_STRING:
19627 case VAR_FUNC:
19628 if (from->vval.v_string == NULL)
19629 to->vval.v_string = NULL;
19630 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019631 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019632 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019633 if (from->v_type == VAR_FUNC)
19634 func_ref(to->vval.v_string);
19635 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019636 break;
19637 case VAR_LIST:
19638 if (from->vval.v_list == NULL)
19639 to->vval.v_list = NULL;
19640 else
19641 {
19642 to->vval.v_list = from->vval.v_list;
19643 ++to->vval.v_list->lv_refcount;
19644 }
19645 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019646 case VAR_DICT:
19647 if (from->vval.v_dict == NULL)
19648 to->vval.v_dict = NULL;
19649 else
19650 {
19651 to->vval.v_dict = from->vval.v_dict;
19652 ++to->vval.v_dict->dv_refcount;
19653 }
19654 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019655 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019656 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019657 break;
19658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659}
19660
19661/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019662 * Make a copy of an item.
19663 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019664 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19665 * reference to an already copied list/dict can be used.
19666 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019667 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019668 static int
19669item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 typval_T *from;
19671 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019672 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019673 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019674{
19675 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019676 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019677
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019679 {
19680 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019681 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019682 }
19683 ++recurse;
19684
19685 switch (from->v_type)
19686 {
19687 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019688#ifdef FEAT_FLOAT
19689 case VAR_FLOAT:
19690#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019691 case VAR_STRING:
19692 case VAR_FUNC:
19693 copy_tv(from, to);
19694 break;
19695 case VAR_LIST:
19696 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019697 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019698 if (from->vval.v_list == NULL)
19699 to->vval.v_list = NULL;
19700 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19701 {
19702 /* use the copy made earlier */
19703 to->vval.v_list = from->vval.v_list->lv_copylist;
19704 ++to->vval.v_list->lv_refcount;
19705 }
19706 else
19707 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19708 if (to->vval.v_list == NULL)
19709 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019710 break;
19711 case VAR_DICT:
19712 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019713 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019714 if (from->vval.v_dict == NULL)
19715 to->vval.v_dict = NULL;
19716 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19717 {
19718 /* use the copy made earlier */
19719 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19720 ++to->vval.v_dict->dv_refcount;
19721 }
19722 else
19723 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19724 if (to->vval.v_dict == NULL)
19725 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019726 break;
19727 default:
19728 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019729 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019730 }
19731 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019732 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019733}
19734
19735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 * ":echo expr1 ..." print each argument separated with a space, add a
19737 * newline at the end.
19738 * ":echon expr1 ..." print each argument plain.
19739 */
19740 void
19741ex_echo(eap)
19742 exarg_T *eap;
19743{
19744 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019745 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019746 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 char_u *p;
19748 int needclr = TRUE;
19749 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019750 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751
19752 if (eap->skip)
19753 ++emsg_skip;
19754 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19755 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019756 /* If eval1() causes an error message the text from the command may
19757 * still need to be cleared. E.g., "echo 22,44". */
19758 need_clr_eos = needclr;
19759
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019761 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 {
19763 /*
19764 * Report the invalid expression unless the expression evaluation
19765 * has been cancelled due to an aborting error, an interrupt, or an
19766 * exception.
19767 */
19768 if (!aborting())
19769 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019770 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 break;
19772 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019773 need_clr_eos = FALSE;
19774
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 if (!eap->skip)
19776 {
19777 if (atstart)
19778 {
19779 atstart = FALSE;
19780 /* Call msg_start() after eval1(), evaluating the expression
19781 * may cause a message to appear. */
19782 if (eap->cmdidx == CMD_echo)
19783 msg_start();
19784 }
19785 else if (eap->cmdidx == CMD_echo)
19786 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019787 current_copyID += COPYID_INC;
19788 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019789 if (p != NULL)
19790 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019792 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019794 if (*p != TAB && needclr)
19795 {
19796 /* remove any text still there from the command */
19797 msg_clr_eos();
19798 needclr = FALSE;
19799 }
19800 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 }
19802 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019803 {
19804#ifdef FEAT_MBYTE
19805 if (has_mbyte)
19806 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019807 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019808
19809 (void)msg_outtrans_len_attr(p, i, echo_attr);
19810 p += i - 1;
19811 }
19812 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019814 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019817 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 arg = skipwhite(arg);
19821 }
19822 eap->nextcmd = check_nextcmd(arg);
19823
19824 if (eap->skip)
19825 --emsg_skip;
19826 else
19827 {
19828 /* remove text that may still be there from the command */
19829 if (needclr)
19830 msg_clr_eos();
19831 if (eap->cmdidx == CMD_echo)
19832 msg_end();
19833 }
19834}
19835
19836/*
19837 * ":echohl {name}".
19838 */
19839 void
19840ex_echohl(eap)
19841 exarg_T *eap;
19842{
19843 int id;
19844
19845 id = syn_name2id(eap->arg);
19846 if (id == 0)
19847 echo_attr = 0;
19848 else
19849 echo_attr = syn_id2attr(id);
19850}
19851
19852/*
19853 * ":execute expr1 ..." execute the result of an expression.
19854 * ":echomsg expr1 ..." Print a message
19855 * ":echoerr expr1 ..." Print an error
19856 * Each gets spaces around each argument and a newline at the end for
19857 * echo commands
19858 */
19859 void
19860ex_execute(eap)
19861 exarg_T *eap;
19862{
19863 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 int ret = OK;
19866 char_u *p;
19867 garray_T ga;
19868 int len;
19869 int save_did_emsg;
19870
19871 ga_init2(&ga, 1, 80);
19872
19873 if (eap->skip)
19874 ++emsg_skip;
19875 while (*arg != NUL && *arg != '|' && *arg != '\n')
19876 {
19877 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019878 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 {
19880 /*
19881 * Report the invalid expression unless the expression evaluation
19882 * has been cancelled due to an aborting error, an interrupt, or an
19883 * exception.
19884 */
19885 if (!aborting())
19886 EMSG2(_(e_invexpr2), p);
19887 ret = FAIL;
19888 break;
19889 }
19890
19891 if (!eap->skip)
19892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 len = (int)STRLEN(p);
19895 if (ga_grow(&ga, len + 2) == FAIL)
19896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019897 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 ret = FAIL;
19899 break;
19900 }
19901 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 ga.ga_len += len;
19905 }
19906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 arg = skipwhite(arg);
19909 }
19910
19911 if (ret != FAIL && ga.ga_data != NULL)
19912 {
19913 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019916 out_flush();
19917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 else if (eap->cmdidx == CMD_echoerr)
19919 {
19920 /* We don't want to abort following commands, restore did_emsg. */
19921 save_did_emsg = did_emsg;
19922 EMSG((char_u *)ga.ga_data);
19923 if (!force_abort)
19924 did_emsg = save_did_emsg;
19925 }
19926 else if (eap->cmdidx == CMD_execute)
19927 do_cmdline((char_u *)ga.ga_data,
19928 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19929 }
19930
19931 ga_clear(&ga);
19932
19933 if (eap->skip)
19934 --emsg_skip;
19935
19936 eap->nextcmd = check_nextcmd(arg);
19937}
19938
19939/*
19940 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19941 * "arg" points to the "&" or '+' when called, to "option" when returning.
19942 * Returns NULL when no option name found. Otherwise pointer to the char
19943 * after the option name.
19944 */
19945 static char_u *
19946find_option_end(arg, opt_flags)
19947 char_u **arg;
19948 int *opt_flags;
19949{
19950 char_u *p = *arg;
19951
19952 ++p;
19953 if (*p == 'g' && p[1] == ':')
19954 {
19955 *opt_flags = OPT_GLOBAL;
19956 p += 2;
19957 }
19958 else if (*p == 'l' && p[1] == ':')
19959 {
19960 *opt_flags = OPT_LOCAL;
19961 p += 2;
19962 }
19963 else
19964 *opt_flags = 0;
19965
19966 if (!ASCII_ISALPHA(*p))
19967 return NULL;
19968 *arg = p;
19969
19970 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19971 p += 4; /* termcap option */
19972 else
19973 while (ASCII_ISALPHA(*p))
19974 ++p;
19975 return p;
19976}
19977
19978/*
19979 * ":function"
19980 */
19981 void
19982ex_function(eap)
19983 exarg_T *eap;
19984{
19985 char_u *theline;
19986 int j;
19987 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 char_u *name = NULL;
19990 char_u *p;
19991 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019992 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 garray_T newargs;
19994 garray_T newlines;
19995 int varargs = FALSE;
19996 int mustend = FALSE;
19997 int flags = 0;
19998 ufunc_T *fp;
19999 int indent;
20000 int nesting;
20001 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020002 dictitem_T *v;
20003 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020004 static int func_nr = 0; /* number for nameless function */
20005 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020006 hashtab_T *ht;
20007 int todo;
20008 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020009 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010
20011 /*
20012 * ":function" without argument: list functions.
20013 */
20014 if (ends_excmd(*eap->arg))
20015 {
20016 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020017 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020018 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020019 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020020 {
20021 if (!HASHITEM_EMPTY(hi))
20022 {
20023 --todo;
20024 fp = HI2UF(hi);
20025 if (!isdigit(*fp->uf_name))
20026 list_func_head(fp, FALSE);
20027 }
20028 }
20029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 eap->nextcmd = check_nextcmd(eap->arg);
20031 return;
20032 }
20033
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020034 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020035 * ":function /pat": list functions matching pattern.
20036 */
20037 if (*eap->arg == '/')
20038 {
20039 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20040 if (!eap->skip)
20041 {
20042 regmatch_T regmatch;
20043
20044 c = *p;
20045 *p = NUL;
20046 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20047 *p = c;
20048 if (regmatch.regprog != NULL)
20049 {
20050 regmatch.rm_ic = p_ic;
20051
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020052 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020053 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20054 {
20055 if (!HASHITEM_EMPTY(hi))
20056 {
20057 --todo;
20058 fp = HI2UF(hi);
20059 if (!isdigit(*fp->uf_name)
20060 && vim_regexec(&regmatch, fp->uf_name, 0))
20061 list_func_head(fp, FALSE);
20062 }
20063 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020064 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020065 }
20066 }
20067 if (*p == '/')
20068 ++p;
20069 eap->nextcmd = check_nextcmd(p);
20070 return;
20071 }
20072
20073 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020074 * Get the function name. There are these situations:
20075 * func normal function name
20076 * "name" == func, "fudi.fd_dict" == NULL
20077 * dict.func new dictionary entry
20078 * "name" == NULL, "fudi.fd_dict" set,
20079 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20080 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020081 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020082 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20083 * dict.func existing dict entry that's not a Funcref
20084 * "name" == NULL, "fudi.fd_dict" set,
20085 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020088 name = trans_function_name(&p, eap->skip, 0, &fudi);
20089 paren = (vim_strchr(p, '(') != NULL);
20090 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 {
20092 /*
20093 * Return on an invalid expression in braces, unless the expression
20094 * evaluation has been cancelled due to an aborting error, an
20095 * interrupt, or an exception.
20096 */
20097 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020099 if (!eap->skip && fudi.fd_newkey != NULL)
20100 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020101 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 else
20105 eap->skip = TRUE;
20106 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020107
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 /* An error in a function call during evaluation of an expression in magic
20109 * braces should not cause the function not to be defined. */
20110 saved_did_emsg = did_emsg;
20111 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112
20113 /*
20114 * ":function func" with only function name: list function.
20115 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020116 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 {
20118 if (!ends_excmd(*skipwhite(p)))
20119 {
20120 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020121 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 }
20123 eap->nextcmd = check_nextcmd(p);
20124 if (eap->nextcmd != NULL)
20125 *p = NUL;
20126 if (!eap->skip && !got_int)
20127 {
20128 fp = find_func(name);
20129 if (fp != NULL)
20130 {
20131 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020132 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020134 if (FUNCLINE(fp, j) == NULL)
20135 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 msg_putchar('\n');
20137 msg_outnum((long)(j + 1));
20138 if (j < 9)
20139 msg_putchar(' ');
20140 if (j < 99)
20141 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020142 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 out_flush(); /* show a line at a time */
20144 ui_breakcheck();
20145 }
20146 if (!got_int)
20147 {
20148 msg_putchar('\n');
20149 msg_puts((char_u *)" endfunction");
20150 }
20151 }
20152 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020153 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020155 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156 }
20157
20158 /*
20159 * ":function name(arg1, arg2)" Define function.
20160 */
20161 p = skipwhite(p);
20162 if (*p != '(')
20163 {
20164 if (!eap->skip)
20165 {
20166 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020167 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 }
20169 /* attempt to continue by skipping some text */
20170 if (vim_strchr(p, '(') != NULL)
20171 p = vim_strchr(p, '(');
20172 }
20173 p = skipwhite(p + 1);
20174
20175 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20176 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20177
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020178 if (!eap->skip)
20179 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020180 /* Check the name of the function. Unless it's a dictionary function
20181 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020182 if (name != NULL)
20183 arg = name;
20184 else
20185 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020186 if (arg != NULL && (fudi.fd_di == NULL
20187 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020188 {
20189 if (*arg == K_SPECIAL)
20190 j = 3;
20191 else
20192 j = 0;
20193 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20194 : eval_isnamec(arg[j])))
20195 ++j;
20196 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020197 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020198 }
20199 }
20200
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 /*
20202 * Isolate the arguments: "arg1, arg2, ...)"
20203 */
20204 while (*p != ')')
20205 {
20206 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20207 {
20208 varargs = TRUE;
20209 p += 3;
20210 mustend = TRUE;
20211 }
20212 else
20213 {
20214 arg = p;
20215 while (ASCII_ISALNUM(*p) || *p == '_')
20216 ++p;
20217 if (arg == p || isdigit(*arg)
20218 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20219 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20220 {
20221 if (!eap->skip)
20222 EMSG2(_("E125: Illegal argument: %s"), arg);
20223 break;
20224 }
20225 if (ga_grow(&newargs, 1) == FAIL)
20226 goto erret;
20227 c = *p;
20228 *p = NUL;
20229 arg = vim_strsave(arg);
20230 if (arg == NULL)
20231 goto erret;
20232 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20233 *p = c;
20234 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 if (*p == ',')
20236 ++p;
20237 else
20238 mustend = TRUE;
20239 }
20240 p = skipwhite(p);
20241 if (mustend && *p != ')')
20242 {
20243 if (!eap->skip)
20244 EMSG2(_(e_invarg2), eap->arg);
20245 break;
20246 }
20247 }
20248 ++p; /* skip the ')' */
20249
Bram Moolenaare9a41262005-01-15 22:18:47 +000020250 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 for (;;)
20252 {
20253 p = skipwhite(p);
20254 if (STRNCMP(p, "range", 5) == 0)
20255 {
20256 flags |= FC_RANGE;
20257 p += 5;
20258 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020259 else if (STRNCMP(p, "dict", 4) == 0)
20260 {
20261 flags |= FC_DICT;
20262 p += 4;
20263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 else if (STRNCMP(p, "abort", 5) == 0)
20265 {
20266 flags |= FC_ABORT;
20267 p += 5;
20268 }
20269 else
20270 break;
20271 }
20272
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020273 /* When there is a line break use what follows for the function body.
20274 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20275 if (*p == '\n')
20276 line_arg = p + 1;
20277 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 EMSG(_(e_trailing));
20279
20280 /*
20281 * Read the body of the function, until ":endfunction" is found.
20282 */
20283 if (KeyTyped)
20284 {
20285 /* Check if the function already exists, don't let the user type the
20286 * whole function before telling him it doesn't work! For a script we
20287 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020288 if (!eap->skip && !eap->forceit)
20289 {
20290 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20291 EMSG(_(e_funcdict));
20292 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020293 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020296 if (!eap->skip && did_emsg)
20297 goto erret;
20298
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 msg_putchar('\n'); /* don't overwrite the function name */
20300 cmdline_row = msg_row;
20301 }
20302
20303 indent = 2;
20304 nesting = 0;
20305 for (;;)
20306 {
20307 msg_scroll = TRUE;
20308 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020309 sourcing_lnum_off = sourcing_lnum;
20310
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020311 if (line_arg != NULL)
20312 {
20313 /* Use eap->arg, split up in parts by line breaks. */
20314 theline = line_arg;
20315 p = vim_strchr(theline, '\n');
20316 if (p == NULL)
20317 line_arg += STRLEN(line_arg);
20318 else
20319 {
20320 *p = NUL;
20321 line_arg = p + 1;
20322 }
20323 }
20324 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 theline = getcmdline(':', 0L, indent);
20326 else
20327 theline = eap->getline(':', eap->cookie, indent);
20328 if (KeyTyped)
20329 lines_left = Rows - 1;
20330 if (theline == NULL)
20331 {
20332 EMSG(_("E126: Missing :endfunction"));
20333 goto erret;
20334 }
20335
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020336 /* Detect line continuation: sourcing_lnum increased more than one. */
20337 if (sourcing_lnum > sourcing_lnum_off + 1)
20338 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20339 else
20340 sourcing_lnum_off = 0;
20341
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 if (skip_until != NULL)
20343 {
20344 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20345 * don't check for ":endfunc". */
20346 if (STRCMP(theline, skip_until) == 0)
20347 {
20348 vim_free(skip_until);
20349 skip_until = NULL;
20350 }
20351 }
20352 else
20353 {
20354 /* skip ':' and blanks*/
20355 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20356 ;
20357
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020358 /* Check for "endfunction". */
20359 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020361 if (line_arg == NULL)
20362 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 break;
20364 }
20365
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020366 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 * at "end". */
20368 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20369 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020370 else if (STRNCMP(p, "if", 2) == 0
20371 || STRNCMP(p, "wh", 2) == 0
20372 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 || STRNCMP(p, "try", 3) == 0)
20374 indent += 2;
20375
20376 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020377 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020379 if (*p == '!')
20380 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 p += eval_fname_script(p);
20382 if (ASCII_ISALPHA(*p))
20383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020384 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 if (*skipwhite(p) == '(')
20386 {
20387 ++nesting;
20388 indent += 2;
20389 }
20390 }
20391 }
20392
20393 /* Check for ":append" or ":insert". */
20394 p = skip_range(p, NULL);
20395 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20396 || (p[0] == 'i'
20397 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20398 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20399 skip_until = vim_strsave((char_u *)".");
20400
20401 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20402 arg = skipwhite(skiptowhite(p));
20403 if (arg[0] == '<' && arg[1] =='<'
20404 && ((p[0] == 'p' && p[1] == 'y'
20405 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20406 || (p[0] == 'p' && p[1] == 'e'
20407 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20408 || (p[0] == 't' && p[1] == 'c'
20409 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20410 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20411 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020412 || (p[0] == 'm' && p[1] == 'z'
20413 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 ))
20415 {
20416 /* ":python <<" continues until a dot, like ":append" */
20417 p = skipwhite(arg + 2);
20418 if (*p == NUL)
20419 skip_until = vim_strsave((char_u *)".");
20420 else
20421 skip_until = vim_strsave(p);
20422 }
20423 }
20424
20425 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020426 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020427 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020428 if (line_arg == NULL)
20429 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020431 }
20432
20433 /* Copy the line to newly allocated memory. get_one_sourceline()
20434 * allocates 250 bytes per line, this saves 80% on average. The cost
20435 * is an extra alloc/free. */
20436 p = vim_strsave(theline);
20437 if (p != NULL)
20438 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020439 if (line_arg == NULL)
20440 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020441 theline = p;
20442 }
20443
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020444 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20445
20446 /* Add NULL lines for continuation lines, so that the line count is
20447 * equal to the index in the growarray. */
20448 while (sourcing_lnum_off-- > 0)
20449 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020450
20451 /* Check for end of eap->arg. */
20452 if (line_arg != NULL && *line_arg == NUL)
20453 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 }
20455
20456 /* Don't define the function when skipping commands or when an error was
20457 * detected. */
20458 if (eap->skip || did_emsg)
20459 goto erret;
20460
20461 /*
20462 * If there are no errors, add the function
20463 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020464 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020465 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020466 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020467 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020468 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020469 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020470 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020471 goto erret;
20472 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020473
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020474 fp = find_func(name);
20475 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020477 if (!eap->forceit)
20478 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020479 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020480 goto erret;
20481 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020482 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020483 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020484 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020485 name);
20486 goto erret;
20487 }
20488 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020489 ga_clear_strings(&(fp->uf_args));
20490 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020491 vim_free(name);
20492 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 }
20495 else
20496 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020497 char numbuf[20];
20498
20499 fp = NULL;
20500 if (fudi.fd_newkey == NULL && !eap->forceit)
20501 {
20502 EMSG(_(e_funcdict));
20503 goto erret;
20504 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020505 if (fudi.fd_di == NULL)
20506 {
20507 /* Can't add a function to a locked dictionary */
20508 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20509 goto erret;
20510 }
20511 /* Can't change an existing function if it is locked */
20512 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20513 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020514
20515 /* Give the function a sequential number. Can only be used with a
20516 * Funcref! */
20517 vim_free(name);
20518 sprintf(numbuf, "%d", ++func_nr);
20519 name = vim_strsave((char_u *)numbuf);
20520 if (name == NULL)
20521 goto erret;
20522 }
20523
20524 if (fp == NULL)
20525 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020526 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020527 {
20528 int slen, plen;
20529 char_u *scriptname;
20530
20531 /* Check that the autoload name matches the script name. */
20532 j = FAIL;
20533 if (sourcing_name != NULL)
20534 {
20535 scriptname = autoload_name(name);
20536 if (scriptname != NULL)
20537 {
20538 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020539 plen = (int)STRLEN(p);
20540 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020541 if (slen > plen && fnamecmp(p,
20542 sourcing_name + slen - plen) == 0)
20543 j = OK;
20544 vim_free(scriptname);
20545 }
20546 }
20547 if (j == FAIL)
20548 {
20549 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20550 goto erret;
20551 }
20552 }
20553
20554 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 if (fp == NULL)
20556 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020557
20558 if (fudi.fd_dict != NULL)
20559 {
20560 if (fudi.fd_di == NULL)
20561 {
20562 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020563 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020564 if (fudi.fd_di == NULL)
20565 {
20566 vim_free(fp);
20567 goto erret;
20568 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020569 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20570 {
20571 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020572 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020573 goto erret;
20574 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020575 }
20576 else
20577 /* overwrite existing dict entry */
20578 clear_tv(&fudi.fd_di->di_tv);
20579 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020580 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020581 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020582 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020583
20584 /* behave like "dict" was used */
20585 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020586 }
20587
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020589 STRCPY(fp->uf_name, name);
20590 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020592 fp->uf_args = newargs;
20593 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020594#ifdef FEAT_PROFILE
20595 fp->uf_tml_count = NULL;
20596 fp->uf_tml_total = NULL;
20597 fp->uf_tml_self = NULL;
20598 fp->uf_profiling = FALSE;
20599 if (prof_def_func())
20600 func_do_profile(fp);
20601#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020602 fp->uf_varargs = varargs;
20603 fp->uf_flags = flags;
20604 fp->uf_calls = 0;
20605 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020606 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607
20608erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 ga_clear_strings(&newargs);
20610 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020611ret_free:
20612 vim_free(skip_until);
20613 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616}
20617
20618/*
20619 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020620 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020622 * flags:
20623 * TFN_INT: internal function name OK
20624 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 * Advances "pp" to just after the function name (if no error).
20626 */
20627 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020628trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 char_u **pp;
20630 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020631 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020632 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020634 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 char_u *start;
20636 char_u *end;
20637 int lead;
20638 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020640 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020641
20642 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020643 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020645
20646 /* Check for hard coded <SNR>: already translated function ID (from a user
20647 * command). */
20648 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20649 && (*pp)[2] == (int)KE_SNR)
20650 {
20651 *pp += 3;
20652 len = get_id_len(pp) + 3;
20653 return vim_strnsave(start, len);
20654 }
20655
20656 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20657 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020659 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020661
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020662 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20663 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020664 if (end == start)
20665 {
20666 if (!skip)
20667 EMSG(_("E129: Function name required"));
20668 goto theend;
20669 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020670 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020671 {
20672 /*
20673 * Report an invalid expression in braces, unless the expression
20674 * evaluation has been cancelled due to an aborting error, an
20675 * interrupt, or an exception.
20676 */
20677 if (!aborting())
20678 {
20679 if (end != NULL)
20680 EMSG2(_(e_invarg2), start);
20681 }
20682 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020683 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020684 goto theend;
20685 }
20686
20687 if (lv.ll_tv != NULL)
20688 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020689 if (fdp != NULL)
20690 {
20691 fdp->fd_dict = lv.ll_dict;
20692 fdp->fd_newkey = lv.ll_newkey;
20693 lv.ll_newkey = NULL;
20694 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020695 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020696 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20697 {
20698 name = vim_strsave(lv.ll_tv->vval.v_string);
20699 *pp = end;
20700 }
20701 else
20702 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020703 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20704 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020705 EMSG(_(e_funcref));
20706 else
20707 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020708 name = NULL;
20709 }
20710 goto theend;
20711 }
20712
20713 if (lv.ll_name == NULL)
20714 {
20715 /* Error found, but continue after the function name. */
20716 *pp = end;
20717 goto theend;
20718 }
20719
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020720 /* Check if the name is a Funcref. If so, use the value. */
20721 if (lv.ll_exp_name != NULL)
20722 {
20723 len = (int)STRLEN(lv.ll_exp_name);
20724 name = deref_func_name(lv.ll_exp_name, &len);
20725 if (name == lv.ll_exp_name)
20726 name = NULL;
20727 }
20728 else
20729 {
20730 len = (int)(end - *pp);
20731 name = deref_func_name(*pp, &len);
20732 if (name == *pp)
20733 name = NULL;
20734 }
20735 if (name != NULL)
20736 {
20737 name = vim_strsave(name);
20738 *pp = end;
20739 goto theend;
20740 }
20741
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020742 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020743 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020744 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020745 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20746 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20747 {
20748 /* When there was "s:" already or the name expanded to get a
20749 * leading "s:" then remove it. */
20750 lv.ll_name += 2;
20751 len -= 2;
20752 lead = 2;
20753 }
20754 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020755 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020756 {
20757 if (lead == 2) /* skip over "s:" */
20758 lv.ll_name += 2;
20759 len = (int)(end - lv.ll_name);
20760 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020761
20762 /*
20763 * Copy the function name to allocated memory.
20764 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20765 * Accept <SNR>123_name() outside a script.
20766 */
20767 if (skip)
20768 lead = 0; /* do nothing */
20769 else if (lead > 0)
20770 {
20771 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020772 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20773 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020774 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020775 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020776 if (current_SID <= 0)
20777 {
20778 EMSG(_(e_usingsid));
20779 goto theend;
20780 }
20781 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20782 lead += (int)STRLEN(sid_buf);
20783 }
20784 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020785 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020786 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020787 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020788 goto theend;
20789 }
20790 name = alloc((unsigned)(len + lead + 1));
20791 if (name != NULL)
20792 {
20793 if (lead > 0)
20794 {
20795 name[0] = K_SPECIAL;
20796 name[1] = KS_EXTRA;
20797 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020798 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020799 STRCPY(name + 3, sid_buf);
20800 }
20801 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20802 name[len + lead] = NUL;
20803 }
20804 *pp = end;
20805
20806theend:
20807 clear_lval(&lv);
20808 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809}
20810
20811/*
20812 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20813 * Return 2 if "p" starts with "s:".
20814 * Return 0 otherwise.
20815 */
20816 static int
20817eval_fname_script(p)
20818 char_u *p;
20819{
20820 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20821 || STRNICMP(p + 1, "SNR>", 4) == 0))
20822 return 5;
20823 if (p[0] == 's' && p[1] == ':')
20824 return 2;
20825 return 0;
20826}
20827
20828/*
20829 * Return TRUE if "p" starts with "<SID>" or "s:".
20830 * Only works if eval_fname_script() returned non-zero for "p"!
20831 */
20832 static int
20833eval_fname_sid(p)
20834 char_u *p;
20835{
20836 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20837}
20838
20839/*
20840 * List the head of the function: "name(arg1, arg2)".
20841 */
20842 static void
20843list_func_head(fp, indent)
20844 ufunc_T *fp;
20845 int indent;
20846{
20847 int j;
20848
20849 msg_start();
20850 if (indent)
20851 MSG_PUTS(" ");
20852 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020853 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 {
20855 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020856 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 }
20858 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020859 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020861 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 {
20863 if (j)
20864 MSG_PUTS(", ");
20865 msg_puts(FUNCARG(fp, j));
20866 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020867 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 {
20869 if (j)
20870 MSG_PUTS(", ");
20871 MSG_PUTS("...");
20872 }
20873 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020874 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020875 if (p_verbose > 0)
20876 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877}
20878
20879/*
20880 * Find a function by name, return pointer to it in ufuncs.
20881 * Return NULL for unknown function.
20882 */
20883 static ufunc_T *
20884find_func(name)
20885 char_u *name;
20886{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020887 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020889 hi = hash_find(&func_hashtab, name);
20890 if (!HASHITEM_EMPTY(hi))
20891 return HI2UF(hi);
20892 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893}
20894
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020895#if defined(EXITFREE) || defined(PROTO)
20896 void
20897free_all_functions()
20898{
20899 hashitem_T *hi;
20900
20901 /* Need to start all over every time, because func_free() may change the
20902 * hash table. */
20903 while (func_hashtab.ht_used > 0)
20904 for (hi = func_hashtab.ht_array; ; ++hi)
20905 if (!HASHITEM_EMPTY(hi))
20906 {
20907 func_free(HI2UF(hi));
20908 break;
20909 }
20910}
20911#endif
20912
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020913/*
20914 * Return TRUE if a function "name" exists.
20915 */
20916 static int
20917function_exists(name)
20918 char_u *name;
20919{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020920 char_u *nm = name;
20921 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020922 int n = FALSE;
20923
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020924 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020925 nm = skipwhite(nm);
20926
20927 /* Only accept "funcname", "funcname ", "funcname (..." and
20928 * "funcname(...", not "funcname!...". */
20929 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020930 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020931 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020932 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020933 else
20934 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020935 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020936 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020937 return n;
20938}
20939
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020940/*
20941 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020942 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020943 */
20944 static int
20945builtin_function(name)
20946 char_u *name;
20947{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020948 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20949 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020950}
20951
Bram Moolenaar05159a02005-02-26 23:04:13 +000020952#if defined(FEAT_PROFILE) || defined(PROTO)
20953/*
20954 * Start profiling function "fp".
20955 */
20956 static void
20957func_do_profile(fp)
20958 ufunc_T *fp;
20959{
20960 fp->uf_tm_count = 0;
20961 profile_zero(&fp->uf_tm_self);
20962 profile_zero(&fp->uf_tm_total);
20963 if (fp->uf_tml_count == NULL)
20964 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20965 (sizeof(int) * fp->uf_lines.ga_len));
20966 if (fp->uf_tml_total == NULL)
20967 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20968 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20969 if (fp->uf_tml_self == NULL)
20970 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20971 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20972 fp->uf_tml_idx = -1;
20973 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20974 || fp->uf_tml_self == NULL)
20975 return; /* out of memory */
20976
20977 fp->uf_profiling = TRUE;
20978}
20979
20980/*
20981 * Dump the profiling results for all functions in file "fd".
20982 */
20983 void
20984func_dump_profile(fd)
20985 FILE *fd;
20986{
20987 hashitem_T *hi;
20988 int todo;
20989 ufunc_T *fp;
20990 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020991 ufunc_T **sorttab;
20992 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020993
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020994 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020995 if (todo == 0)
20996 return; /* nothing to dump */
20997
Bram Moolenaar73830342005-02-28 22:48:19 +000020998 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20999
Bram Moolenaar05159a02005-02-26 23:04:13 +000021000 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21001 {
21002 if (!HASHITEM_EMPTY(hi))
21003 {
21004 --todo;
21005 fp = HI2UF(hi);
21006 if (fp->uf_profiling)
21007 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021008 if (sorttab != NULL)
21009 sorttab[st_len++] = fp;
21010
Bram Moolenaar05159a02005-02-26 23:04:13 +000021011 if (fp->uf_name[0] == K_SPECIAL)
21012 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21013 else
21014 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21015 if (fp->uf_tm_count == 1)
21016 fprintf(fd, "Called 1 time\n");
21017 else
21018 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21019 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21020 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21021 fprintf(fd, "\n");
21022 fprintf(fd, "count total (s) self (s)\n");
21023
21024 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21025 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021026 if (FUNCLINE(fp, i) == NULL)
21027 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021028 prof_func_line(fd, fp->uf_tml_count[i],
21029 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021030 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21031 }
21032 fprintf(fd, "\n");
21033 }
21034 }
21035 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021036
21037 if (sorttab != NULL && st_len > 0)
21038 {
21039 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21040 prof_total_cmp);
21041 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21042 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21043 prof_self_cmp);
21044 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21045 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021046
21047 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021048}
Bram Moolenaar73830342005-02-28 22:48:19 +000021049
21050 static void
21051prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21052 FILE *fd;
21053 ufunc_T **sorttab;
21054 int st_len;
21055 char *title;
21056 int prefer_self; /* when equal print only self time */
21057{
21058 int i;
21059 ufunc_T *fp;
21060
21061 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21062 fprintf(fd, "count total (s) self (s) function\n");
21063 for (i = 0; i < 20 && i < st_len; ++i)
21064 {
21065 fp = sorttab[i];
21066 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21067 prefer_self);
21068 if (fp->uf_name[0] == K_SPECIAL)
21069 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21070 else
21071 fprintf(fd, " %s()\n", fp->uf_name);
21072 }
21073 fprintf(fd, "\n");
21074}
21075
21076/*
21077 * Print the count and times for one function or function line.
21078 */
21079 static void
21080prof_func_line(fd, count, total, self, prefer_self)
21081 FILE *fd;
21082 int count;
21083 proftime_T *total;
21084 proftime_T *self;
21085 int prefer_self; /* when equal print only self time */
21086{
21087 if (count > 0)
21088 {
21089 fprintf(fd, "%5d ", count);
21090 if (prefer_self && profile_equal(total, self))
21091 fprintf(fd, " ");
21092 else
21093 fprintf(fd, "%s ", profile_msg(total));
21094 if (!prefer_self && profile_equal(total, self))
21095 fprintf(fd, " ");
21096 else
21097 fprintf(fd, "%s ", profile_msg(self));
21098 }
21099 else
21100 fprintf(fd, " ");
21101}
21102
21103/*
21104 * Compare function for total time sorting.
21105 */
21106 static int
21107#ifdef __BORLANDC__
21108_RTLENTRYF
21109#endif
21110prof_total_cmp(s1, s2)
21111 const void *s1;
21112 const void *s2;
21113{
21114 ufunc_T *p1, *p2;
21115
21116 p1 = *(ufunc_T **)s1;
21117 p2 = *(ufunc_T **)s2;
21118 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21119}
21120
21121/*
21122 * Compare function for self time sorting.
21123 */
21124 static int
21125#ifdef __BORLANDC__
21126_RTLENTRYF
21127#endif
21128prof_self_cmp(s1, s2)
21129 const void *s1;
21130 const void *s2;
21131{
21132 ufunc_T *p1, *p2;
21133
21134 p1 = *(ufunc_T **)s1;
21135 p2 = *(ufunc_T **)s2;
21136 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21137}
21138
Bram Moolenaar05159a02005-02-26 23:04:13 +000021139#endif
21140
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021141/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021142 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021143 * Return TRUE if a package was loaded.
21144 */
21145 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021146script_autoload(name, reload)
21147 char_u *name;
21148 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021149{
21150 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021151 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021152 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021153 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021154
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021155 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021156 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021157 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021158 return FALSE;
21159
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021160 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021161
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021162 /* Find the name in the list of previously loaded package names. Skip
21163 * "autoload/", it's always the same. */
21164 for (i = 0; i < ga_loaded.ga_len; ++i)
21165 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21166 break;
21167 if (!reload && i < ga_loaded.ga_len)
21168 ret = FALSE; /* was loaded already */
21169 else
21170 {
21171 /* Remember the name if it wasn't loaded already. */
21172 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21173 {
21174 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21175 tofree = NULL;
21176 }
21177
21178 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021179 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021180 ret = TRUE;
21181 }
21182
21183 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021184 return ret;
21185}
21186
21187/*
21188 * Return the autoload script name for a function or variable name.
21189 * Returns NULL when out of memory.
21190 */
21191 static char_u *
21192autoload_name(name)
21193 char_u *name;
21194{
21195 char_u *p;
21196 char_u *scriptname;
21197
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021198 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021199 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21200 if (scriptname == NULL)
21201 return FALSE;
21202 STRCPY(scriptname, "autoload/");
21203 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021204 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021205 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021206 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021207 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021208 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021209}
21210
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21212
21213/*
21214 * Function given to ExpandGeneric() to obtain the list of user defined
21215 * function names.
21216 */
21217 char_u *
21218get_user_func_name(xp, idx)
21219 expand_T *xp;
21220 int idx;
21221{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021222 static long_u done;
21223 static hashitem_T *hi;
21224 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225
21226 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021228 done = 0;
21229 hi = func_hashtab.ht_array;
21230 }
21231 if (done < func_hashtab.ht_used)
21232 {
21233 if (done++ > 0)
21234 ++hi;
21235 while (HASHITEM_EMPTY(hi))
21236 ++hi;
21237 fp = HI2UF(hi);
21238
21239 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21240 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241
21242 cat_func_name(IObuff, fp);
21243 if (xp->xp_context != EXPAND_USER_FUNC)
21244 {
21245 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021246 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247 STRCAT(IObuff, ")");
21248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 return IObuff;
21250 }
21251 return NULL;
21252}
21253
21254#endif /* FEAT_CMDL_COMPL */
21255
21256/*
21257 * Copy the function name of "fp" to buffer "buf".
21258 * "buf" must be able to hold the function name plus three bytes.
21259 * Takes care of script-local function names.
21260 */
21261 static void
21262cat_func_name(buf, fp)
21263 char_u *buf;
21264 ufunc_T *fp;
21265{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021266 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 {
21268 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021269 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 }
21271 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021272 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273}
21274
21275/*
21276 * ":delfunction {name}"
21277 */
21278 void
21279ex_delfunction(eap)
21280 exarg_T *eap;
21281{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021282 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 char_u *p;
21284 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021285 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286
21287 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021288 name = trans_function_name(&p, eap->skip, 0, &fudi);
21289 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021291 {
21292 if (fudi.fd_dict != NULL && !eap->skip)
21293 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 if (!ends_excmd(*skipwhite(p)))
21297 {
21298 vim_free(name);
21299 EMSG(_(e_trailing));
21300 return;
21301 }
21302 eap->nextcmd = check_nextcmd(p);
21303 if (eap->nextcmd != NULL)
21304 *p = NUL;
21305
21306 if (!eap->skip)
21307 fp = find_func(name);
21308 vim_free(name);
21309
21310 if (!eap->skip)
21311 {
21312 if (fp == NULL)
21313 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021314 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 return;
21316 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021317 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 {
21319 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21320 return;
21321 }
21322
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021323 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021325 /* Delete the dict item that refers to the function, it will
21326 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021327 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021329 else
21330 func_free(fp);
21331 }
21332}
21333
21334/*
21335 * Free a function and remove it from the list of functions.
21336 */
21337 static void
21338func_free(fp)
21339 ufunc_T *fp;
21340{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021341 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021342
21343 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021344 ga_clear_strings(&(fp->uf_args));
21345 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021346#ifdef FEAT_PROFILE
21347 vim_free(fp->uf_tml_count);
21348 vim_free(fp->uf_tml_total);
21349 vim_free(fp->uf_tml_self);
21350#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021351
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021352 /* remove the function from the function hashtable */
21353 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21354 if (HASHITEM_EMPTY(hi))
21355 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021356 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021357 hash_remove(&func_hashtab, hi);
21358
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021359 vim_free(fp);
21360}
21361
21362/*
21363 * Unreference a Function: decrement the reference count and free it when it
21364 * becomes zero. Only for numbered functions.
21365 */
21366 static void
21367func_unref(name)
21368 char_u *name;
21369{
21370 ufunc_T *fp;
21371
21372 if (name != NULL && isdigit(*name))
21373 {
21374 fp = find_func(name);
21375 if (fp == NULL)
21376 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021377 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021378 {
21379 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021380 * when "uf_calls" becomes zero. */
21381 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021382 func_free(fp);
21383 }
21384 }
21385}
21386
21387/*
21388 * Count a reference to a Function.
21389 */
21390 static void
21391func_ref(name)
21392 char_u *name;
21393{
21394 ufunc_T *fp;
21395
21396 if (name != NULL && isdigit(*name))
21397 {
21398 fp = find_func(name);
21399 if (fp == NULL)
21400 EMSG2(_(e_intern2), "func_ref()");
21401 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021402 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 }
21404}
21405
21406/*
21407 * Call a user function.
21408 */
21409 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021410call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 ufunc_T *fp; /* pointer to function */
21412 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021413 typval_T *argvars; /* arguments */
21414 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 linenr_T firstline; /* first line of range */
21416 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021417 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418{
Bram Moolenaar33570922005-01-25 22:26:29 +000021419 char_u *save_sourcing_name;
21420 linenr_T save_sourcing_lnum;
21421 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021422 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021423 int save_did_emsg;
21424 static int depth = 0;
21425 dictitem_T *v;
21426 int fixvar_idx = 0; /* index in fixvar[] */
21427 int i;
21428 int ai;
21429 char_u numbuf[NUMBUFLEN];
21430 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021431#ifdef FEAT_PROFILE
21432 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021433 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435
21436 /* If depth of calling is getting too high, don't execute the function */
21437 if (depth >= p_mfd)
21438 {
21439 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021440 rettv->v_type = VAR_NUMBER;
21441 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 return;
21443 }
21444 ++depth;
21445
21446 line_breakcheck(); /* check for CTRL-C hit */
21447
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021448 fc = (funccall_T *)alloc(sizeof(funccall_T));
21449 fc->caller = current_funccal;
21450 current_funccal = fc;
21451 fc->func = fp;
21452 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021453 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021454 fc->linenr = 0;
21455 fc->returned = FALSE;
21456 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021458 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21459 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460
Bram Moolenaar33570922005-01-25 22:26:29 +000021461 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021462 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021463 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21464 * each argument variable and saves a lot of time.
21465 */
21466 /*
21467 * Init l: variables.
21468 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021469 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021470 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021471 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021472 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21473 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021474 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021475 name = v->di_key;
21476 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021477 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021478 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021479 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021480 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021481 v->di_tv.vval.v_dict = selfdict;
21482 ++selfdict->dv_refcount;
21483 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021484
Bram Moolenaar33570922005-01-25 22:26:29 +000021485 /*
21486 * Init a: variables.
21487 * Set a:0 to "argcount".
21488 * Set a:000 to a list with room for the "..." arguments.
21489 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021490 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21491 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021492 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021493 /* Use "name" to avoid a warning from some compiler that checks the
21494 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021495 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021496 name = v->di_key;
21497 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021498 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021499 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021500 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021501 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021502 v->di_tv.vval.v_list = &fc->l_varlist;
21503 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21504 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21505 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021506
21507 /*
21508 * Set a:firstline to "firstline" and a:lastline to "lastline".
21509 * Set a:name to named arguments.
21510 * Set a:N to the "..." arguments.
21511 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021512 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021513 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021514 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021515 (varnumber_T)lastline);
21516 for (i = 0; i < argcount; ++i)
21517 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021518 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021519 if (ai < 0)
21520 /* named argument a:name */
21521 name = FUNCARG(fp, i);
21522 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021523 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021524 /* "..." argument a:1, a:2, etc. */
21525 sprintf((char *)numbuf, "%d", ai + 1);
21526 name = numbuf;
21527 }
21528 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21529 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021530 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021531 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21532 }
21533 else
21534 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021535 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21536 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021537 if (v == NULL)
21538 break;
21539 v->di_flags = DI_FLAGS_RO;
21540 }
21541 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021542 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021543
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021544 /* Note: the values are copied directly to avoid alloc/free.
21545 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021546 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021547 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021548
21549 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21550 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021551 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21552 fc->l_listitems[ai].li_tv = argvars[i];
21553 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021554 }
21555 }
21556
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 /* Don't redraw while executing the function. */
21558 ++RedrawingDisabled;
21559 save_sourcing_name = sourcing_name;
21560 save_sourcing_lnum = sourcing_lnum;
21561 sourcing_lnum = 1;
21562 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021563 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 if (sourcing_name != NULL)
21565 {
21566 if (save_sourcing_name != NULL
21567 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21568 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21569 else
21570 STRCPY(sourcing_name, "function ");
21571 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21572
21573 if (p_verbose >= 12)
21574 {
21575 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021576 verbose_enter_scroll();
21577
Bram Moolenaar555b2802005-05-19 21:08:39 +000021578 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 if (p_verbose >= 14)
21580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021582 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021583 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021584 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585
21586 msg_puts((char_u *)"(");
21587 for (i = 0; i < argcount; ++i)
21588 {
21589 if (i > 0)
21590 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021591 if (argvars[i].v_type == VAR_NUMBER)
21592 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 else
21594 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021595 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21596 if (s != NULL)
21597 {
21598 trunc_string(s, buf, MSG_BUF_CLEN);
21599 msg_puts(buf);
21600 vim_free(tofree);
21601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 }
21603 }
21604 msg_puts((char_u *)")");
21605 }
21606 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021607
21608 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 --no_wait_return;
21610 }
21611 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021612#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021613 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021614 {
21615 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21616 func_do_profile(fp);
21617 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021618 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021619 {
21620 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021621 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021622 profile_zero(&fp->uf_tm_children);
21623 }
21624 script_prof_save(&wait_start);
21625 }
21626#endif
21627
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021629 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 save_did_emsg = did_emsg;
21631 did_emsg = FALSE;
21632
21633 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021634 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21636
21637 --RedrawingDisabled;
21638
21639 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021640 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021642 clear_tv(rettv);
21643 rettv->v_type = VAR_NUMBER;
21644 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 }
21646
Bram Moolenaar05159a02005-02-26 23:04:13 +000021647#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021648 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021649 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021650 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021651 profile_end(&call_start);
21652 profile_sub_wait(&wait_start, &call_start);
21653 profile_add(&fp->uf_tm_total, &call_start);
21654 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021655 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021656 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021657 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21658 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021659 }
21660 }
21661#endif
21662
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 /* when being verbose, mention the return value */
21664 if (p_verbose >= 12)
21665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021667 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021670 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021671 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021672 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021673 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021676 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021677 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021678 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021679 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021680
Bram Moolenaar555b2802005-05-19 21:08:39 +000021681 /* The value may be very long. Skip the middle part, so that we
21682 * have some idea how it starts and ends. smsg() would always
21683 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021684 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021685 if (s != NULL)
21686 {
21687 trunc_string(s, buf, MSG_BUF_CLEN);
21688 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21689 vim_free(tofree);
21690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 }
21692 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021693
21694 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 --no_wait_return;
21696 }
21697
21698 vim_free(sourcing_name);
21699 sourcing_name = save_sourcing_name;
21700 sourcing_lnum = save_sourcing_lnum;
21701 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021702#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021703 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021704 script_prof_restore(&wait_start);
21705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706
21707 if (p_verbose >= 12 && sourcing_name != NULL)
21708 {
21709 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021710 verbose_enter_scroll();
21711
Bram Moolenaar555b2802005-05-19 21:08:39 +000021712 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021714
21715 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 --no_wait_return;
21717 }
21718
21719 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021720 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021722
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021723 /* If the a:000 list and the l: and a: dicts are not referenced we can
21724 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021725 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21726 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21727 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21728 {
21729 free_funccal(fc, FALSE);
21730 }
21731 else
21732 {
21733 hashitem_T *hi;
21734 listitem_T *li;
21735 int todo;
21736
21737 /* "fc" is still in use. This can happen when returning "a:000" or
21738 * assigning "l:" to a global variable.
21739 * Link "fc" in the list for garbage collection later. */
21740 fc->caller = previous_funccal;
21741 previous_funccal = fc;
21742
21743 /* Make a copy of the a: variables, since we didn't do that above. */
21744 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21745 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21746 {
21747 if (!HASHITEM_EMPTY(hi))
21748 {
21749 --todo;
21750 v = HI2DI(hi);
21751 copy_tv(&v->di_tv, &v->di_tv);
21752 }
21753 }
21754
21755 /* Make a copy of the a:000 items, since we didn't do that above. */
21756 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21757 copy_tv(&li->li_tv, &li->li_tv);
21758 }
21759}
21760
21761/*
21762 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021763 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021764 */
21765 static int
21766can_free_funccal(fc, copyID)
21767 funccall_T *fc;
21768 int copyID;
21769{
21770 return (fc->l_varlist.lv_copyID != copyID
21771 && fc->l_vars.dv_copyID != copyID
21772 && fc->l_avars.dv_copyID != copyID);
21773}
21774
21775/*
21776 * Free "fc" and what it contains.
21777 */
21778 static void
21779free_funccal(fc, free_val)
21780 funccall_T *fc;
21781 int free_val; /* a: vars were allocated */
21782{
21783 listitem_T *li;
21784
21785 /* The a: variables typevals may not have been allocated, only free the
21786 * allocated variables. */
21787 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21788
21789 /* free all l: variables */
21790 vars_clear(&fc->l_vars.dv_hashtab);
21791
21792 /* Free the a:000 variables if they were allocated. */
21793 if (free_val)
21794 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21795 clear_tv(&li->li_tv);
21796
21797 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798}
21799
21800/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021801 * Add a number variable "name" to dict "dp" with value "nr".
21802 */
21803 static void
21804add_nr_var(dp, v, name, nr)
21805 dict_T *dp;
21806 dictitem_T *v;
21807 char *name;
21808 varnumber_T nr;
21809{
21810 STRCPY(v->di_key, name);
21811 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21812 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21813 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021814 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021815 v->di_tv.vval.v_number = nr;
21816}
21817
21818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 * ":return [expr]"
21820 */
21821 void
21822ex_return(eap)
21823 exarg_T *eap;
21824{
21825 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021826 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 int returning = FALSE;
21828
21829 if (current_funccal == NULL)
21830 {
21831 EMSG(_("E133: :return not inside a function"));
21832 return;
21833 }
21834
21835 if (eap->skip)
21836 ++emsg_skip;
21837
21838 eap->nextcmd = NULL;
21839 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021840 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 {
21842 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021843 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021845 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 }
21847 /* It's safer to return also on error. */
21848 else if (!eap->skip)
21849 {
21850 /*
21851 * Return unless the expression evaluation has been cancelled due to an
21852 * aborting error, an interrupt, or an exception.
21853 */
21854 if (!aborting())
21855 returning = do_return(eap, FALSE, TRUE, NULL);
21856 }
21857
21858 /* When skipping or the return gets pending, advance to the next command
21859 * in this line (!returning). Otherwise, ignore the rest of the line.
21860 * Following lines will be ignored by get_func_line(). */
21861 if (returning)
21862 eap->nextcmd = NULL;
21863 else if (eap->nextcmd == NULL) /* no argument */
21864 eap->nextcmd = check_nextcmd(arg);
21865
21866 if (eap->skip)
21867 --emsg_skip;
21868}
21869
21870/*
21871 * Return from a function. Possibly makes the return pending. Also called
21872 * for a pending return at the ":endtry" or after returning from an extra
21873 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021874 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021875 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 * FALSE when the return gets pending.
21877 */
21878 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021879do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 exarg_T *eap;
21881 int reanimate;
21882 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021883 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884{
21885 int idx;
21886 struct condstack *cstack = eap->cstack;
21887
21888 if (reanimate)
21889 /* Undo the return. */
21890 current_funccal->returned = FALSE;
21891
21892 /*
21893 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21894 * not in its finally clause (which then is to be executed next) is found.
21895 * In this case, make the ":return" pending for execution at the ":endtry".
21896 * Otherwise, return normally.
21897 */
21898 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21899 if (idx >= 0)
21900 {
21901 cstack->cs_pending[idx] = CSTP_RETURN;
21902
21903 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021904 /* A pending return again gets pending. "rettv" points to an
21905 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021907 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 else
21909 {
21910 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021911 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021913 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021915 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 {
21917 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021918 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021919 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 else
21921 EMSG(_(e_outofmem));
21922 }
21923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021924 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925
21926 if (reanimate)
21927 {
21928 /* The pending return value could be overwritten by a ":return"
21929 * without argument in a finally clause; reset the default
21930 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021931 current_funccal->rettv->v_type = VAR_NUMBER;
21932 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 }
21934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021935 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 }
21937 else
21938 {
21939 current_funccal->returned = TRUE;
21940
21941 /* If the return is carried out now, store the return value. For
21942 * a return immediately after reanimation, the value is already
21943 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021944 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021946 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021947 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021949 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 }
21951 }
21952
21953 return idx < 0;
21954}
21955
21956/*
21957 * Free the variable with a pending return value.
21958 */
21959 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021960discard_pending_return(rettv)
21961 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962{
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964}
21965
21966/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021967 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 * is an allocated string. Used by report_pending() for verbose messages.
21969 */
21970 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021971get_return_cmd(rettv)
21972 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021974 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021975 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021976 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021978 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021979 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021980 if (s == NULL)
21981 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021982
21983 STRCPY(IObuff, ":return ");
21984 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21985 if (STRLEN(s) + 8 >= IOSIZE)
21986 STRCPY(IObuff + IOSIZE - 4, "...");
21987 vim_free(tofree);
21988 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989}
21990
21991/*
21992 * Get next function line.
21993 * Called by do_cmdline() to get the next line.
21994 * Returns allocated string, or NULL for end of function.
21995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 char_u *
21997get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021998 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022000 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001{
Bram Moolenaar33570922005-01-25 22:26:29 +000022002 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022003 ufunc_T *fp = fcp->func;
22004 char_u *retval;
22005 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006
22007 /* If breakpoints have been added/deleted need to check for it. */
22008 if (fcp->dbg_tick != debug_tick)
22009 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022010 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 sourcing_lnum);
22012 fcp->dbg_tick = debug_tick;
22013 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022014#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022015 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022016 func_line_end(cookie);
22017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018
Bram Moolenaar05159a02005-02-26 23:04:13 +000022019 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022020 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22021 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 retval = NULL;
22023 else
22024 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022025 /* Skip NULL lines (continuation lines). */
22026 while (fcp->linenr < gap->ga_len
22027 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22028 ++fcp->linenr;
22029 if (fcp->linenr >= gap->ga_len)
22030 retval = NULL;
22031 else
22032 {
22033 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22034 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022035#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022036 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022037 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022038#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 }
22041
22042 /* Did we encounter a breakpoint? */
22043 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22044 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022045 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022047 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 sourcing_lnum);
22049 fcp->dbg_tick = debug_tick;
22050 }
22051
22052 return retval;
22053}
22054
Bram Moolenaar05159a02005-02-26 23:04:13 +000022055#if defined(FEAT_PROFILE) || defined(PROTO)
22056/*
22057 * Called when starting to read a function line.
22058 * "sourcing_lnum" must be correct!
22059 * When skipping lines it may not actually be executed, but we won't find out
22060 * until later and we need to store the time now.
22061 */
22062 void
22063func_line_start(cookie)
22064 void *cookie;
22065{
22066 funccall_T *fcp = (funccall_T *)cookie;
22067 ufunc_T *fp = fcp->func;
22068
22069 if (fp->uf_profiling && sourcing_lnum >= 1
22070 && sourcing_lnum <= fp->uf_lines.ga_len)
22071 {
22072 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022073 /* Skip continuation lines. */
22074 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22075 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022076 fp->uf_tml_execed = FALSE;
22077 profile_start(&fp->uf_tml_start);
22078 profile_zero(&fp->uf_tml_children);
22079 profile_get_wait(&fp->uf_tml_wait);
22080 }
22081}
22082
22083/*
22084 * Called when actually executing a function line.
22085 */
22086 void
22087func_line_exec(cookie)
22088 void *cookie;
22089{
22090 funccall_T *fcp = (funccall_T *)cookie;
22091 ufunc_T *fp = fcp->func;
22092
22093 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22094 fp->uf_tml_execed = TRUE;
22095}
22096
22097/*
22098 * Called when done with a function line.
22099 */
22100 void
22101func_line_end(cookie)
22102 void *cookie;
22103{
22104 funccall_T *fcp = (funccall_T *)cookie;
22105 ufunc_T *fp = fcp->func;
22106
22107 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22108 {
22109 if (fp->uf_tml_execed)
22110 {
22111 ++fp->uf_tml_count[fp->uf_tml_idx];
22112 profile_end(&fp->uf_tml_start);
22113 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022114 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022115 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22116 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022117 }
22118 fp->uf_tml_idx = -1;
22119 }
22120}
22121#endif
22122
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123/*
22124 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022125 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126 */
22127 int
22128func_has_ended(cookie)
22129 void *cookie;
22130{
Bram Moolenaar33570922005-01-25 22:26:29 +000022131 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132
22133 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22134 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022135 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 || fcp->returned);
22137}
22138
22139/*
22140 * return TRUE if cookie indicates a function which "abort"s on errors.
22141 */
22142 int
22143func_has_abort(cookie)
22144 void *cookie;
22145{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022146 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147}
22148
22149#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22150typedef enum
22151{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022152 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22153 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22154 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155} var_flavour_T;
22156
22157static var_flavour_T var_flavour __ARGS((char_u *varname));
22158
22159 static var_flavour_T
22160var_flavour(varname)
22161 char_u *varname;
22162{
22163 char_u *p = varname;
22164
22165 if (ASCII_ISUPPER(*p))
22166 {
22167 while (*(++p))
22168 if (ASCII_ISLOWER(*p))
22169 return VAR_FLAVOUR_SESSION;
22170 return VAR_FLAVOUR_VIMINFO;
22171 }
22172 else
22173 return VAR_FLAVOUR_DEFAULT;
22174}
22175#endif
22176
22177#if defined(FEAT_VIMINFO) || defined(PROTO)
22178/*
22179 * Restore global vars that start with a capital from the viminfo file
22180 */
22181 int
22182read_viminfo_varlist(virp, writing)
22183 vir_T *virp;
22184 int writing;
22185{
22186 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022187 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022188 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189
22190 if (!writing && (find_viminfo_parameter('!') != NULL))
22191 {
22192 tab = vim_strchr(virp->vir_line + 1, '\t');
22193 if (tab != NULL)
22194 {
22195 *tab++ = '\0'; /* isolate the variable name */
22196 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022197 type = VAR_STRING;
22198#ifdef FEAT_FLOAT
22199 else if (*tab == 'F')
22200 type = VAR_FLOAT;
22201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202
22203 tab = vim_strchr(tab, '\t');
22204 if (tab != NULL)
22205 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022206 tv.v_type = type;
22207 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022208 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022210#ifdef FEAT_FLOAT
22211 else if (type == VAR_FLOAT)
22212 (void)string2float(tab + 1, &tv.vval.v_float);
22213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022215 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022216 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022217 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022218 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 }
22220 }
22221 }
22222
22223 return viminfo_readline(virp);
22224}
22225
22226/*
22227 * Write global vars that start with a capital to the viminfo file
22228 */
22229 void
22230write_viminfo_varlist(fp)
22231 FILE *fp;
22232{
Bram Moolenaar33570922005-01-25 22:26:29 +000022233 hashitem_T *hi;
22234 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022235 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022236 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022237 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022238 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022239 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240
22241 if (find_viminfo_parameter('!') == NULL)
22242 return;
22243
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022244 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022245
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022246 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022247 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022249 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022251 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022252 this_var = HI2DI(hi);
22253 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022254 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022256 {
22257 case VAR_STRING: s = "STR"; break;
22258 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022259#ifdef FEAT_FLOAT
22260 case VAR_FLOAT: s = "FLO"; break;
22261#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022262 default: continue;
22263 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022264 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022265 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022266 if (p != NULL)
22267 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022268 vim_free(tofree);
22269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 }
22271 }
22272}
22273#endif
22274
22275#if defined(FEAT_SESSION) || defined(PROTO)
22276 int
22277store_session_globals(fd)
22278 FILE *fd;
22279{
Bram Moolenaar33570922005-01-25 22:26:29 +000022280 hashitem_T *hi;
22281 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022282 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 char_u *p, *t;
22284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022285 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022286 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022288 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022290 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022291 this_var = HI2DI(hi);
22292 if ((this_var->di_tv.v_type == VAR_NUMBER
22293 || this_var->di_tv.v_type == VAR_STRING)
22294 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022295 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022296 /* Escape special characters with a backslash. Turn a LF and
22297 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022298 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022299 (char_u *)"\\\"\n\r");
22300 if (p == NULL) /* out of memory */
22301 break;
22302 for (t = p; *t != NUL; ++t)
22303 if (*t == '\n')
22304 *t = 'n';
22305 else if (*t == '\r')
22306 *t = 'r';
22307 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 this_var->di_key,
22309 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22310 : ' ',
22311 p,
22312 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22313 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022314 || put_eol(fd) == FAIL)
22315 {
22316 vim_free(p);
22317 return FAIL;
22318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 vim_free(p);
22320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022321#ifdef FEAT_FLOAT
22322 else if (this_var->di_tv.v_type == VAR_FLOAT
22323 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22324 {
22325 float_T f = this_var->di_tv.vval.v_float;
22326 int sign = ' ';
22327
22328 if (f < 0)
22329 {
22330 f = -f;
22331 sign = '-';
22332 }
22333 if ((fprintf(fd, "let %s = %c&%f",
22334 this_var->di_key, sign, f) < 0)
22335 || put_eol(fd) == FAIL)
22336 return FAIL;
22337 }
22338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 }
22340 }
22341 return OK;
22342}
22343#endif
22344
Bram Moolenaar661b1822005-07-28 22:36:45 +000022345/*
22346 * Display script name where an item was last set.
22347 * Should only be invoked when 'verbose' is non-zero.
22348 */
22349 void
22350last_set_msg(scriptID)
22351 scid_T scriptID;
22352{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022353 char_u *p;
22354
Bram Moolenaar661b1822005-07-28 22:36:45 +000022355 if (scriptID != 0)
22356 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022357 p = home_replace_save(NULL, get_scriptname(scriptID));
22358 if (p != NULL)
22359 {
22360 verbose_enter();
22361 MSG_PUTS(_("\n\tLast set from "));
22362 MSG_PUTS(p);
22363 vim_free(p);
22364 verbose_leave();
22365 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022366 }
22367}
22368
Bram Moolenaard812df62008-11-09 12:46:09 +000022369/*
22370 * List v:oldfiles in a nice way.
22371 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022372 void
22373ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022374 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022375{
22376 list_T *l = vimvars[VV_OLDFILES].vv_list;
22377 listitem_T *li;
22378 int nr = 0;
22379
22380 if (l == NULL)
22381 msg((char_u *)_("No old files"));
22382 else
22383 {
22384 msg_start();
22385 msg_scroll = TRUE;
22386 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22387 {
22388 msg_outnum((long)++nr);
22389 MSG_PUTS(": ");
22390 msg_outtrans(get_tv_string(&li->li_tv));
22391 msg_putchar('\n');
22392 out_flush(); /* output one line at a time */
22393 ui_breakcheck();
22394 }
22395 /* Assume "got_int" was set to truncate the listing. */
22396 got_int = FALSE;
22397
22398#ifdef FEAT_BROWSE_CMD
22399 if (cmdmod.browse)
22400 {
22401 quit_more = FALSE;
22402 nr = prompt_for_number(FALSE);
22403 msg_starthere();
22404 if (nr > 0)
22405 {
22406 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22407 (long)nr);
22408
22409 if (p != NULL)
22410 {
22411 p = expand_env_save(p);
22412 eap->arg = p;
22413 eap->cmdidx = CMD_edit;
22414 cmdmod.browse = FALSE;
22415 do_exedit(eap, NULL);
22416 vim_free(p);
22417 }
22418 }
22419 }
22420#endif
22421 }
22422}
22423
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424#endif /* FEAT_EVAL */
22425
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022427#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428
22429#ifdef WIN3264
22430/*
22431 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22432 */
22433static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22434static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22435static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22436
22437/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022438 * Get the short path (8.3) for the filename in "fnamep".
22439 * Only works for a valid file name.
22440 * When the path gets longer "fnamep" is changed and the allocated buffer
22441 * is put in "bufp".
22442 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22443 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 */
22445 static int
22446get_short_pathname(fnamep, bufp, fnamelen)
22447 char_u **fnamep;
22448 char_u **bufp;
22449 int *fnamelen;
22450{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022451 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 char_u *newbuf;
22453
22454 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 l = GetShortPathName(*fnamep, *fnamep, len);
22456 if (l > len - 1)
22457 {
22458 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022459 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460 newbuf = vim_strnsave(*fnamep, l);
22461 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463
22464 vim_free(*bufp);
22465 *fnamep = *bufp = newbuf;
22466
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022467 /* Really should always succeed, as the buffer is big enough. */
22468 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 }
22470
22471 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022472 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473}
22474
22475/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022476 * Get the short path (8.3) for the filename in "fname". The converted
22477 * path is returned in "bufp".
22478 *
22479 * Some of the directories specified in "fname" may not exist. This function
22480 * will shorten the existing directories at the beginning of the path and then
22481 * append the remaining non-existing path.
22482 *
22483 * fname - Pointer to the filename to shorten. On return, contains the
22484 * pointer to the shortened pathname
22485 * bufp - Pointer to an allocated buffer for the filename.
22486 * fnamelen - Length of the filename pointed to by fname
22487 *
22488 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 */
22490 static int
22491shortpath_for_invalid_fname(fname, bufp, fnamelen)
22492 char_u **fname;
22493 char_u **bufp;
22494 int *fnamelen;
22495{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022496 char_u *short_fname, *save_fname, *pbuf_unused;
22497 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022499 int old_len, len;
22500 int new_len, sfx_len;
22501 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502
22503 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022504 old_len = *fnamelen;
22505 save_fname = vim_strnsave(*fname, old_len);
22506 pbuf_unused = NULL;
22507 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022509 endp = save_fname + old_len - 1; /* Find the end of the copy */
22510 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022512 /*
22513 * Try shortening the supplied path till it succeeds by removing one
22514 * directory at a time from the tail of the path.
22515 */
22516 len = 0;
22517 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022519 /* go back one path-separator */
22520 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22521 --endp;
22522 if (endp <= save_fname)
22523 break; /* processed the complete path */
22524
22525 /*
22526 * Replace the path separator with a NUL and try to shorten the
22527 * resulting path.
22528 */
22529 ch = *endp;
22530 *endp = 0;
22531 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022532 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022533 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22534 {
22535 retval = FAIL;
22536 goto theend;
22537 }
22538 *endp = ch; /* preserve the string */
22539
22540 if (len > 0)
22541 break; /* successfully shortened the path */
22542
22543 /* failed to shorten the path. Skip the path separator */
22544 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 }
22546
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022547 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022549 /*
22550 * Succeeded in shortening the path. Now concatenate the shortened
22551 * path with the remaining path at the tail.
22552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022554 /* Compute the length of the new path. */
22555 sfx_len = (int)(save_endp - endp) + 1;
22556 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022558 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022560 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022562 /* There is not enough space in the currently allocated string,
22563 * copy it to a buffer big enough. */
22564 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022566 {
22567 retval = FAIL;
22568 goto theend;
22569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 }
22571 else
22572 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022573 /* Transfer short_fname to the main buffer (it's big enough),
22574 * unless get_short_pathname() did its work in-place. */
22575 *fname = *bufp = save_fname;
22576 if (short_fname != save_fname)
22577 vim_strncpy(save_fname, short_fname, len);
22578 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022580
22581 /* concat the not-shortened part of the path */
22582 vim_strncpy(*fname + len, endp, sfx_len);
22583 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022585
22586theend:
22587 vim_free(pbuf_unused);
22588 vim_free(save_fname);
22589
22590 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591}
22592
22593/*
22594 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022595 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 */
22597 static int
22598shortpath_for_partial(fnamep, bufp, fnamelen)
22599 char_u **fnamep;
22600 char_u **bufp;
22601 int *fnamelen;
22602{
22603 int sepcount, len, tflen;
22604 char_u *p;
22605 char_u *pbuf, *tfname;
22606 int hasTilde;
22607
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022608 /* Count up the path separators from the RHS.. so we know which part
22609 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022611 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 if (vim_ispathsep(*p))
22613 ++sepcount;
22614
22615 /* Need full path first (use expand_env() to remove a "~/") */
22616 hasTilde = (**fnamep == '~');
22617 if (hasTilde)
22618 pbuf = tfname = expand_env_save(*fnamep);
22619 else
22620 pbuf = tfname = FullName_save(*fnamep, FALSE);
22621
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022622 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022624 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22625 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626
22627 if (len == 0)
22628 {
22629 /* Don't have a valid filename, so shorten the rest of the
22630 * path if we can. This CAN give us invalid 8.3 filenames, but
22631 * there's not a lot of point in guessing what it might be.
22632 */
22633 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022634 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 }
22637
22638 /* Count the paths backward to find the beginning of the desired string. */
22639 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022640 {
22641#ifdef FEAT_MBYTE
22642 if (has_mbyte)
22643 p -= mb_head_off(tfname, p);
22644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 if (vim_ispathsep(*p))
22646 {
22647 if (sepcount == 0 || (hasTilde && sepcount == 1))
22648 break;
22649 else
22650 sepcount --;
22651 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 if (hasTilde)
22654 {
22655 --p;
22656 if (p >= tfname)
22657 *p = '~';
22658 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 }
22661 else
22662 ++p;
22663
22664 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22665 vim_free(*bufp);
22666 *fnamelen = (int)STRLEN(p);
22667 *bufp = pbuf;
22668 *fnamep = p;
22669
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022670 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671}
22672#endif /* WIN3264 */
22673
22674/*
22675 * Adjust a filename, according to a string of modifiers.
22676 * *fnamep must be NUL terminated when called. When returning, the length is
22677 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022678 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 * When there is an error, *fnamep is set to NULL.
22680 */
22681 int
22682modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22683 char_u *src; /* string with modifiers */
22684 int *usedlen; /* characters after src that are used */
22685 char_u **fnamep; /* file name so far */
22686 char_u **bufp; /* buffer for allocated file name or NULL */
22687 int *fnamelen; /* length of fnamep */
22688{
22689 int valid = 0;
22690 char_u *tail;
22691 char_u *s, *p, *pbuf;
22692 char_u dirname[MAXPATHL];
22693 int c;
22694 int has_fullname = 0;
22695#ifdef WIN3264
22696 int has_shortname = 0;
22697#endif
22698
22699repeat:
22700 /* ":p" - full path/file_name */
22701 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22702 {
22703 has_fullname = 1;
22704
22705 valid |= VALID_PATH;
22706 *usedlen += 2;
22707
22708 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22709 if ((*fnamep)[0] == '~'
22710#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22711 && ((*fnamep)[1] == '/'
22712# ifdef BACKSLASH_IN_FILENAME
22713 || (*fnamep)[1] == '\\'
22714# endif
22715 || (*fnamep)[1] == NUL)
22716
22717#endif
22718 )
22719 {
22720 *fnamep = expand_env_save(*fnamep);
22721 vim_free(*bufp); /* free any allocated file name */
22722 *bufp = *fnamep;
22723 if (*fnamep == NULL)
22724 return -1;
22725 }
22726
22727 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022728 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 {
22730 if (vim_ispathsep(*p)
22731 && p[1] == '.'
22732 && (p[2] == NUL
22733 || vim_ispathsep(p[2])
22734 || (p[2] == '.'
22735 && (p[3] == NUL || vim_ispathsep(p[3])))))
22736 break;
22737 }
22738
22739 /* FullName_save() is slow, don't use it when not needed. */
22740 if (*p != NUL || !vim_isAbsName(*fnamep))
22741 {
22742 *fnamep = FullName_save(*fnamep, *p != NUL);
22743 vim_free(*bufp); /* free any allocated file name */
22744 *bufp = *fnamep;
22745 if (*fnamep == NULL)
22746 return -1;
22747 }
22748
22749 /* Append a path separator to a directory. */
22750 if (mch_isdir(*fnamep))
22751 {
22752 /* Make room for one or two extra characters. */
22753 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22754 vim_free(*bufp); /* free any allocated file name */
22755 *bufp = *fnamep;
22756 if (*fnamep == NULL)
22757 return -1;
22758 add_pathsep(*fnamep);
22759 }
22760 }
22761
22762 /* ":." - path relative to the current directory */
22763 /* ":~" - path relative to the home directory */
22764 /* ":8" - shortname path - postponed till after */
22765 while (src[*usedlen] == ':'
22766 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22767 {
22768 *usedlen += 2;
22769 if (c == '8')
22770 {
22771#ifdef WIN3264
22772 has_shortname = 1; /* Postpone this. */
22773#endif
22774 continue;
22775 }
22776 pbuf = NULL;
22777 /* Need full path first (use expand_env() to remove a "~/") */
22778 if (!has_fullname)
22779 {
22780 if (c == '.' && **fnamep == '~')
22781 p = pbuf = expand_env_save(*fnamep);
22782 else
22783 p = pbuf = FullName_save(*fnamep, FALSE);
22784 }
22785 else
22786 p = *fnamep;
22787
22788 has_fullname = 0;
22789
22790 if (p != NULL)
22791 {
22792 if (c == '.')
22793 {
22794 mch_dirname(dirname, MAXPATHL);
22795 s = shorten_fname(p, dirname);
22796 if (s != NULL)
22797 {
22798 *fnamep = s;
22799 if (pbuf != NULL)
22800 {
22801 vim_free(*bufp); /* free any allocated file name */
22802 *bufp = pbuf;
22803 pbuf = NULL;
22804 }
22805 }
22806 }
22807 else
22808 {
22809 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22810 /* Only replace it when it starts with '~' */
22811 if (*dirname == '~')
22812 {
22813 s = vim_strsave(dirname);
22814 if (s != NULL)
22815 {
22816 *fnamep = s;
22817 vim_free(*bufp);
22818 *bufp = s;
22819 }
22820 }
22821 }
22822 vim_free(pbuf);
22823 }
22824 }
22825
22826 tail = gettail(*fnamep);
22827 *fnamelen = (int)STRLEN(*fnamep);
22828
22829 /* ":h" - head, remove "/file_name", can be repeated */
22830 /* Don't remove the first "/" or "c:\" */
22831 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22832 {
22833 valid |= VALID_HEAD;
22834 *usedlen += 2;
22835 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022836 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022837 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 *fnamelen = (int)(tail - *fnamep);
22839#ifdef VMS
22840 if (*fnamelen > 0)
22841 *fnamelen += 1; /* the path separator is part of the path */
22842#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022843 if (*fnamelen == 0)
22844 {
22845 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22846 p = vim_strsave((char_u *)".");
22847 if (p == NULL)
22848 return -1;
22849 vim_free(*bufp);
22850 *bufp = *fnamep = tail = p;
22851 *fnamelen = 1;
22852 }
22853 else
22854 {
22855 while (tail > s && !after_pathsep(s, tail))
22856 mb_ptr_back(*fnamep, tail);
22857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 }
22859
22860 /* ":8" - shortname */
22861 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22862 {
22863 *usedlen += 2;
22864#ifdef WIN3264
22865 has_shortname = 1;
22866#endif
22867 }
22868
22869#ifdef WIN3264
22870 /* Check shortname after we have done 'heads' and before we do 'tails'
22871 */
22872 if (has_shortname)
22873 {
22874 pbuf = NULL;
22875 /* Copy the string if it is shortened by :h */
22876 if (*fnamelen < (int)STRLEN(*fnamep))
22877 {
22878 p = vim_strnsave(*fnamep, *fnamelen);
22879 if (p == 0)
22880 return -1;
22881 vim_free(*bufp);
22882 *bufp = *fnamep = p;
22883 }
22884
22885 /* Split into two implementations - makes it easier. First is where
22886 * there isn't a full name already, second is where there is.
22887 */
22888 if (!has_fullname && !vim_isAbsName(*fnamep))
22889 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022890 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 return -1;
22892 }
22893 else
22894 {
22895 int l;
22896
22897 /* Simple case, already have the full-name
22898 * Nearly always shorter, so try first time. */
22899 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022900 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 return -1;
22902
22903 if (l == 0)
22904 {
22905 /* Couldn't find the filename.. search the paths.
22906 */
22907 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022908 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 return -1;
22910 }
22911 *fnamelen = l;
22912 }
22913 }
22914#endif /* WIN3264 */
22915
22916 /* ":t" - tail, just the basename */
22917 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22918 {
22919 *usedlen += 2;
22920 *fnamelen -= (int)(tail - *fnamep);
22921 *fnamep = tail;
22922 }
22923
22924 /* ":e" - extension, can be repeated */
22925 /* ":r" - root, without extension, can be repeated */
22926 while (src[*usedlen] == ':'
22927 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22928 {
22929 /* find a '.' in the tail:
22930 * - for second :e: before the current fname
22931 * - otherwise: The last '.'
22932 */
22933 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22934 s = *fnamep - 2;
22935 else
22936 s = *fnamep + *fnamelen - 1;
22937 for ( ; s > tail; --s)
22938 if (s[0] == '.')
22939 break;
22940 if (src[*usedlen + 1] == 'e') /* :e */
22941 {
22942 if (s > tail)
22943 {
22944 *fnamelen += (int)(*fnamep - (s + 1));
22945 *fnamep = s + 1;
22946#ifdef VMS
22947 /* cut version from the extension */
22948 s = *fnamep + *fnamelen - 1;
22949 for ( ; s > *fnamep; --s)
22950 if (s[0] == ';')
22951 break;
22952 if (s > *fnamep)
22953 *fnamelen = s - *fnamep;
22954#endif
22955 }
22956 else if (*fnamep <= tail)
22957 *fnamelen = 0;
22958 }
22959 else /* :r */
22960 {
22961 if (s > tail) /* remove one extension */
22962 *fnamelen = (int)(s - *fnamep);
22963 }
22964 *usedlen += 2;
22965 }
22966
22967 /* ":s?pat?foo?" - substitute */
22968 /* ":gs?pat?foo?" - global substitute */
22969 if (src[*usedlen] == ':'
22970 && (src[*usedlen + 1] == 's'
22971 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22972 {
22973 char_u *str;
22974 char_u *pat;
22975 char_u *sub;
22976 int sep;
22977 char_u *flags;
22978 int didit = FALSE;
22979
22980 flags = (char_u *)"";
22981 s = src + *usedlen + 2;
22982 if (src[*usedlen + 1] == 'g')
22983 {
22984 flags = (char_u *)"g";
22985 ++s;
22986 }
22987
22988 sep = *s++;
22989 if (sep)
22990 {
22991 /* find end of pattern */
22992 p = vim_strchr(s, sep);
22993 if (p != NULL)
22994 {
22995 pat = vim_strnsave(s, (int)(p - s));
22996 if (pat != NULL)
22997 {
22998 s = p + 1;
22999 /* find end of substitution */
23000 p = vim_strchr(s, sep);
23001 if (p != NULL)
23002 {
23003 sub = vim_strnsave(s, (int)(p - s));
23004 str = vim_strnsave(*fnamep, *fnamelen);
23005 if (sub != NULL && str != NULL)
23006 {
23007 *usedlen = (int)(p + 1 - src);
23008 s = do_string_sub(str, pat, sub, flags);
23009 if (s != NULL)
23010 {
23011 *fnamep = s;
23012 *fnamelen = (int)STRLEN(s);
23013 vim_free(*bufp);
23014 *bufp = s;
23015 didit = TRUE;
23016 }
23017 }
23018 vim_free(sub);
23019 vim_free(str);
23020 }
23021 vim_free(pat);
23022 }
23023 }
23024 /* after using ":s", repeat all the modifiers */
23025 if (didit)
23026 goto repeat;
23027 }
23028 }
23029
23030 return valid;
23031}
23032
23033/*
23034 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23035 * "flags" can be "g" to do a global substitute.
23036 * Returns an allocated string, NULL for error.
23037 */
23038 char_u *
23039do_string_sub(str, pat, sub, flags)
23040 char_u *str;
23041 char_u *pat;
23042 char_u *sub;
23043 char_u *flags;
23044{
23045 int sublen;
23046 regmatch_T regmatch;
23047 int i;
23048 int do_all;
23049 char_u *tail;
23050 garray_T ga;
23051 char_u *ret;
23052 char_u *save_cpo;
23053
23054 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23055 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023056 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057
23058 ga_init2(&ga, 1, 200);
23059
23060 do_all = (flags[0] == 'g');
23061
23062 regmatch.rm_ic = p_ic;
23063 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23064 if (regmatch.regprog != NULL)
23065 {
23066 tail = str;
23067 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23068 {
23069 /*
23070 * Get some space for a temporary buffer to do the substitution
23071 * into. It will contain:
23072 * - The text up to where the match is.
23073 * - The substituted text.
23074 * - The text after the match.
23075 */
23076 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23077 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23078 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23079 {
23080 ga_clear(&ga);
23081 break;
23082 }
23083
23084 /* copy the text up to where the match is */
23085 i = (int)(regmatch.startp[0] - tail);
23086 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23087 /* add the substituted text */
23088 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23089 + ga.ga_len + i, TRUE, TRUE, FALSE);
23090 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 /* avoid getting stuck on a match with an empty string */
23092 if (tail == regmatch.endp[0])
23093 {
23094 if (*tail == NUL)
23095 break;
23096 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23097 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023098 }
23099 else
23100 {
23101 tail = regmatch.endp[0];
23102 if (*tail == NUL)
23103 break;
23104 }
23105 if (!do_all)
23106 break;
23107 }
23108
23109 if (ga.ga_data != NULL)
23110 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23111
23112 vim_free(regmatch.regprog);
23113 }
23114
23115 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23116 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023117 if (p_cpo == empty_option)
23118 p_cpo = save_cpo;
23119 else
23120 /* Darn, evaluating {sub} expression changed the value. */
23121 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122
23123 return ret;
23124}
23125
23126#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */