blob: 3c238fcbec50b7b59148da3609197905970247d0 [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));
467static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000468static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000469static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
472static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
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));
734static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000743static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000746static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000748static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000749static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static int get_env_len __ARGS((char_u **arg));
751static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000752static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000753static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
754#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
755#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
756 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000757static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000759static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
761static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static typval_T *alloc_tv __ARGS((void));
763static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000764static void init_tv __ARGS((typval_T *varp));
765static long get_tv_number __ARGS((typval_T *varp));
766static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000767static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static char_u *get_tv_string __ARGS((typval_T *varp));
769static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000770static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000772static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
774static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
775static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000776static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
777static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
779static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000780static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000781static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000782static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
784static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
785static int eval_fname_script __ARGS((char_u *p));
786static int eval_fname_sid __ARGS((char_u *p));
787static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static ufunc_T *find_func __ARGS((char_u *name));
789static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000790static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791#ifdef FEAT_PROFILE
792static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000793static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
794static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
795static int
796# ifdef __BORLANDC__
797 _RTLENTRYF
798# endif
799 prof_total_cmp __ARGS((const void *s1, const void *s2));
800static int
801# ifdef __BORLANDC__
802 _RTLENTRYF
803# endif
804 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000806static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000807static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000808static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static void func_free __ARGS((ufunc_T *fp));
810static void func_unref __ARGS((char_u *name));
811static void func_ref __ARGS((char_u *name));
812static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000813static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
814static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000815static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000816static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
817static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000818static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000819static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000820static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000822/* Character used as separated in autoload function/variable names. */
823#define AUTOLOAD_CHAR '#'
824
Bram Moolenaar33570922005-01-25 22:26:29 +0000825/*
826 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000827 */
828 void
829eval_init()
830{
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 int i;
832 struct vimvar *p;
833
834 init_var_dict(&globvardict, &globvars_var);
835 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000836 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000837 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000838
839 for (i = 0; i < VV_LEN; ++i)
840 {
841 p = &vimvars[i];
842 STRCPY(p->vv_di.di_key, p->vv_name);
843 if (p->vv_flags & VV_RO)
844 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
845 else if (p->vv_flags & VV_RO_SBX)
846 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
847 else
848 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000849
850 /* add to v: scope dict, unless the value is not always available */
851 if (p->vv_type != VAR_UNKNOWN)
852 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000854 /* add to compat scope dict */
855 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000857 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000858}
859
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000860#if defined(EXITFREE) || defined(PROTO)
861 void
862eval_clear()
863{
864 int i;
865 struct vimvar *p;
866
867 for (i = 0; i < VV_LEN; ++i)
868 {
869 p = &vimvars[i];
870 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000871 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000872 vim_free(p->vv_str);
873 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000874 }
875 else if (p->vv_di.di_tv.v_type == VAR_LIST)
876 {
877 list_unref(p->vv_list);
878 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000879 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000880 }
881 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000882 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000883 hash_clear(&compat_hashtab);
884
Bram Moolenaard9fba312005-06-26 22:34:35 +0000885 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000886
887 /* global variables */
888 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000889
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000890 /* autoloaded script names */
891 ga_clear_strings(&ga_loaded);
892
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200893 /* script-local variables */
894 for (i = 1; i <= ga_scripts.ga_len; ++i)
895 {
896 vars_clear(&SCRIPT_VARS(i));
897 vim_free(SCRIPT_SV(i));
898 }
899 ga_clear(&ga_scripts);
900
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000901 /* unreferenced lists and dicts */
902 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000903
904 /* functions */
905 free_all_functions();
906 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907}
908#endif
909
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 * Return the name of the executed function.
912 */
913 char_u *
914func_name(cookie)
915 void *cookie;
916{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918}
919
920/*
921 * Return the address holding the next breakpoint line for a funccall cookie.
922 */
923 linenr_T *
924func_breakpoint(cookie)
925 void *cookie;
926{
Bram Moolenaar33570922005-01-25 22:26:29 +0000927 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928}
929
930/*
931 * Return the address holding the debug tick for a funccall cookie.
932 */
933 int *
934func_dbg_tick(cookie)
935 void *cookie;
936{
Bram Moolenaar33570922005-01-25 22:26:29 +0000937 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938}
939
940/*
941 * Return the nesting level for a funccall cookie.
942 */
943 int
944func_level(cookie)
945 void *cookie;
946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000951funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000953/* pointer to list of previously used funccal, still around because some
954 * item in it is still being used. */
955funccall_T *previous_funccal = NULL;
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957/*
958 * Return TRUE when a function was ended by a ":return" command.
959 */
960 int
961current_func_returned()
962{
963 return current_funccal->returned;
964}
965
966
967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 * Set an internal variable to a string value. Creates the variable if it does
969 * not already exist.
970 */
971 void
972set_internal_string_var(name, value)
973 char_u *name;
974 char_u *value;
975{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000976 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000977 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 val = vim_strsave(value);
980 if (val != NULL)
981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000982 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000983 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000985 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000986 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988 }
989}
990
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000991static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000992static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000993static char_u *redir_endp = NULL;
994static char_u *redir_varname = NULL;
995
996/*
997 * Start recording command output to a variable
998 * Returns OK if successfully completed the setup. FAIL otherwise.
999 */
1000 int
1001var_redir_start(name, append)
1002 char_u *name;
1003 int append; /* append to an existing variable */
1004{
1005 int save_emsg;
1006 int err;
1007 typval_T tv;
1008
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001009 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001010 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011 {
1012 EMSG(_(e_invarg));
1013 return FAIL;
1014 }
1015
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001016 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017 redir_varname = vim_strsave(name);
1018 if (redir_varname == NULL)
1019 return FAIL;
1020
1021 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1022 if (redir_lval == NULL)
1023 {
1024 var_redir_stop();
1025 return FAIL;
1026 }
1027
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001028 /* The output is stored in growarray "redir_ga" until redirection ends. */
1029 ga_init2(&redir_ga, (int)sizeof(char), 500);
1030
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001032 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1033 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1035 {
1036 if (redir_endp != NULL && *redir_endp != NUL)
1037 /* Trailing characters are present after the variable name */
1038 EMSG(_(e_trailing));
1039 else
1040 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 var_redir_stop();
1043 return FAIL;
1044 }
1045
1046 /* check if we can write to the variable: set it to or append an empty
1047 * string */
1048 save_emsg = did_emsg;
1049 did_emsg = FALSE;
1050 tv.v_type = VAR_STRING;
1051 tv.vval.v_string = (char_u *)"";
1052 if (append)
1053 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1054 else
1055 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1056 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001057 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058 if (err)
1059 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001060 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 var_redir_stop();
1062 return FAIL;
1063 }
1064 if (redir_lval->ll_newkey != NULL)
1065 {
1066 /* Dictionary item was created, don't do it again. */
1067 vim_free(redir_lval->ll_newkey);
1068 redir_lval->ll_newkey = NULL;
1069 }
1070
1071 return OK;
1072}
1073
1074/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001075 * Append "value[value_len]" to the variable set by var_redir_start().
1076 * The actual appending is postponed until redirection ends, because the value
1077 * appended may in fact be the string we write to, changing it may cause freed
1078 * memory to be used:
1079 * :redir => foo
1080 * :let foo
1081 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 */
1083 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001084var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001086 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001088 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089
1090 if (redir_lval == NULL)
1091 return;
1092
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001093 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001094 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001096 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001098 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 {
1100 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001101 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102 }
1103 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105}
1106
1107/*
1108 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001109 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 */
1111 void
1112var_redir_stop()
1113{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 typval_T tv;
1115
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 if (redir_lval != NULL)
1117 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 /* If there was no error: assign the text to the variable. */
1119 if (redir_endp != NULL)
1120 {
1121 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1122 tv.v_type = VAR_STRING;
1123 tv.vval.v_string = redir_ga.ga_data;
1124 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1125 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001127 /* free the collected output */
1128 vim_free(redir_ga.ga_data);
1129 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 clear_lval(redir_lval);
1132 vim_free(redir_lval);
1133 redir_lval = NULL;
1134 }
1135 vim_free(redir_varname);
1136 redir_varname = NULL;
1137}
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139# if defined(FEAT_MBYTE) || defined(PROTO)
1140 int
1141eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1142 char_u *enc_from;
1143 char_u *enc_to;
1144 char_u *fname_from;
1145 char_u *fname_to;
1146{
1147 int err = FALSE;
1148
1149 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1150 set_vim_var_string(VV_CC_TO, enc_to, -1);
1151 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1152 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1153 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1154 err = TRUE;
1155 set_vim_var_string(VV_CC_FROM, NULL, -1);
1156 set_vim_var_string(VV_CC_TO, NULL, -1);
1157 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1158 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1159
1160 if (err)
1161 return FAIL;
1162 return OK;
1163}
1164# endif
1165
1166# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1167 int
1168eval_printexpr(fname, args)
1169 char_u *fname;
1170 char_u *args;
1171{
1172 int err = FALSE;
1173
1174 set_vim_var_string(VV_FNAME_IN, fname, -1);
1175 set_vim_var_string(VV_CMDARG, args, -1);
1176 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1177 err = TRUE;
1178 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1179 set_vim_var_string(VV_CMDARG, NULL, -1);
1180
1181 if (err)
1182 {
1183 mch_remove(fname);
1184 return FAIL;
1185 }
1186 return OK;
1187}
1188# endif
1189
1190# if defined(FEAT_DIFF) || defined(PROTO)
1191 void
1192eval_diff(origfile, newfile, outfile)
1193 char_u *origfile;
1194 char_u *newfile;
1195 char_u *outfile;
1196{
1197 int err = FALSE;
1198
1199 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1200 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1201 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1202 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1205 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1206}
1207
1208 void
1209eval_patch(origfile, difffile, outfile)
1210 char_u *origfile;
1211 char_u *difffile;
1212 char_u *outfile;
1213{
1214 int err;
1215
1216 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1217 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1218 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1219 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1220 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1221 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1222 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1223}
1224# endif
1225
1226/*
1227 * Top level evaluation function, returning a boolean.
1228 * Sets "error" to TRUE if there was an error.
1229 * Return TRUE or FALSE.
1230 */
1231 int
1232eval_to_bool(arg, error, nextcmd, skip)
1233 char_u *arg;
1234 int *error;
1235 char_u **nextcmd;
1236 int skip; /* only parse, don't execute */
1237{
Bram Moolenaar33570922005-01-25 22:26:29 +00001238 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 int retval = FALSE;
1240
1241 if (skip)
1242 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 else
1246 {
1247 *error = FALSE;
1248 if (!skip)
1249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001250 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001251 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253 }
1254 if (skip)
1255 --emsg_skip;
1256
1257 return retval;
1258}
1259
1260/*
1261 * Top level evaluation function, returning a string. If "skip" is TRUE,
1262 * only parsing to "nextcmd" is done, without reporting errors. Return
1263 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1264 */
1265 char_u *
1266eval_to_string_skip(arg, nextcmd, skip)
1267 char_u *arg;
1268 char_u **nextcmd;
1269 int skip; /* only parse, don't execute */
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 char_u *retval;
1273
1274 if (skip)
1275 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 retval = NULL;
1278 else
1279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 retval = vim_strsave(get_tv_string(&tv));
1281 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 if (skip)
1284 --emsg_skip;
1285
1286 return retval;
1287}
1288
1289/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001290 * Skip over an expression at "*pp".
1291 * Return FAIL for an error, OK otherwise.
1292 */
1293 int
1294skip_expr(pp)
1295 char_u **pp;
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001298
1299 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001301}
1302
1303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001305 * When "convert" is TRUE convert a List into a sequence of lines and convert
1306 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 * Return pointer to allocated memory, or NULL for failure.
1308 */
1309 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001310eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *arg;
1312 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001313 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
Bram Moolenaar33570922005-01-25 22:26:29 +00001315 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001317 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001318#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001319 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001326 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001327 {
1328 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001329 if (tv.vval.v_list != NULL)
1330 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001331 ga_append(&ga, NUL);
1332 retval = (char_u *)ga.ga_data;
1333 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334#ifdef FEAT_FLOAT
1335 else if (convert && tv.v_type == VAR_FLOAT)
1336 {
1337 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1338 retval = vim_strsave(numbuf);
1339 }
1340#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001341 else
1342 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345
1346 return retval;
1347}
1348
1349/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001350 * Call eval_to_string() without using current local variables and using
1351 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 */
1353 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001354eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *arg;
1356 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001357 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
1359 char_u *retval;
1360 void *save_funccalp;
1361
1362 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001363 if (use_sandbox)
1364 ++sandbox;
1365 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001367 if (use_sandbox)
1368 --sandbox;
1369 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 restore_funccal(save_funccalp);
1371 return retval;
1372}
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374/*
1375 * Top level evaluation function, returning a number.
1376 * Evaluates "expr" silently.
1377 * Returns -1 for an error.
1378 */
1379 int
1380eval_to_number(expr)
1381 char_u *expr;
1382{
Bram Moolenaar33570922005-01-25 22:26:29 +00001383 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001385 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 ++emsg_off;
1388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 retval = -1;
1391 else
1392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001393 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 --emsg_off;
1397
1398 return retval;
1399}
1400
Bram Moolenaara40058a2005-07-11 22:42:07 +00001401/*
1402 * Prepare v: variable "idx" to be used.
1403 * Save the current typeval in "save_tv".
1404 * When not used yet add the variable to the v: hashtable.
1405 */
1406 static void
1407prepare_vimvar(idx, save_tv)
1408 int idx;
1409 typval_T *save_tv;
1410{
1411 *save_tv = vimvars[idx].vv_tv;
1412 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1413 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1414}
1415
1416/*
1417 * Restore v: variable "idx" to typeval "save_tv".
1418 * When no longer defined, remove the variable from the v: hashtable.
1419 */
1420 static void
1421restore_vimvar(idx, save_tv)
1422 int idx;
1423 typval_T *save_tv;
1424{
1425 hashitem_T *hi;
1426
Bram Moolenaara40058a2005-07-11 22:42:07 +00001427 vimvars[idx].vv_tv = *save_tv;
1428 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1429 {
1430 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1431 if (HASHITEM_EMPTY(hi))
1432 EMSG2(_(e_intern2), "restore_vimvar()");
1433 else
1434 hash_remove(&vimvarht, hi);
1435 }
1436}
1437
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001438#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001439/*
1440 * Evaluate an expression to a list with suggestions.
1441 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001442 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001443 */
1444 list_T *
1445eval_spell_expr(badword, expr)
1446 char_u *badword;
1447 char_u *expr;
1448{
1449 typval_T save_val;
1450 typval_T rettv;
1451 list_T *list = NULL;
1452 char_u *p = skipwhite(expr);
1453
1454 /* Set "v:val" to the bad word. */
1455 prepare_vimvar(VV_VAL, &save_val);
1456 vimvars[VV_VAL].vv_type = VAR_STRING;
1457 vimvars[VV_VAL].vv_str = badword;
1458 if (p_verbose == 0)
1459 ++emsg_off;
1460
1461 if (eval1(&p, &rettv, TRUE) == OK)
1462 {
1463 if (rettv.v_type != VAR_LIST)
1464 clear_tv(&rettv);
1465 else
1466 list = rettv.vval.v_list;
1467 }
1468
1469 if (p_verbose == 0)
1470 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001471 restore_vimvar(VV_VAL, &save_val);
1472
1473 return list;
1474}
1475
1476/*
1477 * "list" is supposed to contain two items: a word and a number. Return the
1478 * word in "pp" and the number as the return value.
1479 * Return -1 if anything isn't right.
1480 * Used to get the good word and score from the eval_spell_expr() result.
1481 */
1482 int
1483get_spellword(list, pp)
1484 list_T *list;
1485 char_u **pp;
1486{
1487 listitem_T *li;
1488
1489 li = list->lv_first;
1490 if (li == NULL)
1491 return -1;
1492 *pp = get_tv_string(&li->li_tv);
1493
1494 li = li->li_next;
1495 if (li == NULL)
1496 return -1;
1497 return get_tv_number(&li->li_tv);
1498}
1499#endif
1500
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001501/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001502 * Top level evaluation function.
1503 * Returns an allocated typval_T with the result.
1504 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001505 */
1506 typval_T *
1507eval_expr(arg, nextcmd)
1508 char_u *arg;
1509 char_u **nextcmd;
1510{
1511 typval_T *tv;
1512
1513 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001514 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001515 {
1516 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001517 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001518 }
1519
1520 return tv;
1521}
1522
1523
Bram Moolenaar4f688582007-07-24 12:34:30 +00001524#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1525 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001527 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001528 * Uses argv[argc] for the function arguments. Only Number and String
1529 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001530 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001532 static int
1533call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 char_u *func;
1535 int argc;
1536 char_u **argv;
1537 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
Bram Moolenaar33570922005-01-25 22:26:29 +00001540 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 long n;
1542 int len;
1543 int i;
1544 int doesrange;
1545 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001546 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001548 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552 for (i = 0; i < argc; i++)
1553 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001554 /* Pass a NULL or empty argument as an empty string */
1555 if (argv[i] == NULL || *argv[i] == NUL)
1556 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001557 argvars[i].v_type = VAR_STRING;
1558 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001559 continue;
1560 }
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 /* Recognize a number argument, the others must be strings. */
1563 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1564 if (len != 0 && len == (int)STRLEN(argv[i]))
1565 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001566 argvars[i].v_type = VAR_NUMBER;
1567 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 else
1570 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001571 argvars[i].v_type = VAR_STRING;
1572 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
1574 }
1575
1576 if (safe)
1577 {
1578 save_funccalp = save_funccal();
1579 ++sandbox;
1580 }
1581
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1583 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (safe)
1587 {
1588 --sandbox;
1589 restore_funccal(save_funccalp);
1590 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 vim_free(argvars);
1592
1593 if (ret == FAIL)
1594 clear_tv(rettv);
1595
1596 return ret;
1597}
1598
Bram Moolenaar4f688582007-07-24 12:34:30 +00001599# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001600/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001601 * Call vimL function "func" and return the result as a string.
1602 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 * Uses argv[argc] for the function arguments.
1604 */
1605 void *
1606call_func_retstr(func, argc, argv, safe)
1607 char_u *func;
1608 int argc;
1609 char_u **argv;
1610 int safe; /* use the sandbox */
1611{
1612 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001613 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614
1615 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1616 return NULL;
1617
1618 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 return retval;
1621}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001622# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623
Bram Moolenaar4f688582007-07-24 12:34:30 +00001624# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001626 * Call vimL function "func" and return the result as a number.
1627 * Returns -1 when calling the function fails.
1628 * Uses argv[argc] for the function arguments.
1629 */
1630 long
1631call_func_retnr(func, argc, argv, safe)
1632 char_u *func;
1633 int argc;
1634 char_u **argv;
1635 int safe; /* use the sandbox */
1636{
1637 typval_T rettv;
1638 long retval;
1639
1640 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1641 return -1;
1642
1643 retval = get_tv_number_chk(&rettv, NULL);
1644 clear_tv(&rettv);
1645 return retval;
1646}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001647# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001648
1649/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001650 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001652 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 */
1654 void *
1655call_func_retlist(func, argc, argv, safe)
1656 char_u *func;
1657 int argc;
1658 char_u **argv;
1659 int safe; /* use the sandbox */
1660{
1661 typval_T rettv;
1662
1663 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1664 return NULL;
1665
1666 if (rettv.v_type != VAR_LIST)
1667 {
1668 clear_tv(&rettv);
1669 return NULL;
1670 }
1671
1672 return rettv.vval.v_list;
1673}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674#endif
1675
Bram Moolenaar4f688582007-07-24 12:34:30 +00001676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677/*
1678 * Save the current function call pointer, and set it to NULL.
1679 * Used when executing autocommands and for ":source".
1680 */
1681 void *
1682save_funccal()
1683{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001684 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 current_funccal = NULL;
1687 return (void *)fc;
1688}
1689
1690 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001691restore_funccal(vfc)
1692 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001694 funccall_T *fc = (funccall_T *)vfc;
1695
1696 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697}
1698
Bram Moolenaar05159a02005-02-26 23:04:13 +00001699#if defined(FEAT_PROFILE) || defined(PROTO)
1700/*
1701 * Prepare profiling for entering a child or something else that is not
1702 * counted for the script/function itself.
1703 * Should always be called in pair with prof_child_exit().
1704 */
1705 void
1706prof_child_enter(tm)
1707 proftime_T *tm; /* place to store waittime */
1708{
1709 funccall_T *fc = current_funccal;
1710
1711 if (fc != NULL && fc->func->uf_profiling)
1712 profile_start(&fc->prof_child);
1713 script_prof_save(tm);
1714}
1715
1716/*
1717 * Take care of time spent in a child.
1718 * Should always be called after prof_child_enter().
1719 */
1720 void
1721prof_child_exit(tm)
1722 proftime_T *tm; /* where waittime was stored */
1723{
1724 funccall_T *fc = current_funccal;
1725
1726 if (fc != NULL && fc->func->uf_profiling)
1727 {
1728 profile_end(&fc->prof_child);
1729 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1730 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1731 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1732 }
1733 script_prof_restore(tm);
1734}
1735#endif
1736
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#ifdef FEAT_FOLDING
1739/*
1740 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1741 * it in "*cp". Doesn't give error messages.
1742 */
1743 int
1744eval_foldexpr(arg, cp)
1745 char_u *arg;
1746 int *cp;
1747{
Bram Moolenaar33570922005-01-25 22:26:29 +00001748 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 int retval;
1750 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001751 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1752 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001755 if (use_sandbox)
1756 ++sandbox;
1757 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 retval = 0;
1761 else
1762 {
1763 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764 if (tv.v_type == VAR_NUMBER)
1765 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001766 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 retval = 0;
1768 else
1769 {
1770 /* If the result is a string, check if there is a non-digit before
1771 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 if (!VIM_ISDIGIT(*s) && *s != '-')
1774 *cp = *s++;
1775 retval = atol((char *)s);
1776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
1779 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001780 if (use_sandbox)
1781 --sandbox;
1782 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 return retval;
1785}
1786#endif
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 * ":let" list all variable values
1790 * ":let var1 var2" list variable values
1791 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001792 * ":let var += expr" assignment command.
1793 * ":let var -= expr" assignment command.
1794 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
1797 void
1798ex_let(eap)
1799 exarg_T *eap;
1800{
1801 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 int var_count = 0;
1806 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001807 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001808 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001809 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaardb552d602006-03-23 22:59:57 +00001811 argend = skip_var_list(arg, &var_count, &semicolon);
1812 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001814 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1815 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001816 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001819 /*
1820 * ":let" without "=": list variables
1821 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 if (*arg == '[')
1823 EMSG(_(e_invarg));
1824 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001826 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001830 list_glob_vars(&first);
1831 list_buf_vars(&first);
1832 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001833#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001834 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001835#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001836 list_script_vars(&first);
1837 list_func_vars(&first);
1838 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 eap->nextcmd = check_nextcmd(arg);
1841 }
1842 else
1843 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 op[0] = '=';
1845 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 {
1848 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1849 op[0] = expr[-1]; /* +=, -= or .= */
1850 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (eap->skip)
1854 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (eap->skip)
1857 {
1858 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 --emsg_skip;
1861 }
1862 else if (i != FAIL)
1863 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001865 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 }
1869}
1870
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871/*
1872 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1873 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 * When "nextchars" is not NULL it points to a string with characters that
1875 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1876 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 * Returns OK or FAIL;
1878 */
1879 static int
1880ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1881 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 int copy; /* copy values from "tv", don't move */
1884 int semicolon; /* from skip_var_list() */
1885 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887{
1888 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 listitem_T *item;
1892 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893
1894 if (*arg != '[')
1895 {
1896 /*
1897 * ":let var = expr" or ":for var in list"
1898 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 return FAIL;
1901 return OK;
1902 }
1903
1904 /*
1905 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1906 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001907 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908 {
1909 EMSG(_(e_listreq));
1910 return FAIL;
1911 }
1912
1913 i = list_len(l);
1914 if (semicolon == 0 && var_count < i)
1915 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001916 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 return FAIL;
1918 }
1919 if (var_count - semicolon > i)
1920 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001921 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 return FAIL;
1923 }
1924
1925 item = l->lv_first;
1926 while (*arg != ']')
1927 {
1928 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 item = item->li_next;
1931 if (arg == NULL)
1932 return FAIL;
1933
1934 arg = skipwhite(arg);
1935 if (*arg == ';')
1936 {
1937 /* Put the rest of the list (may be empty) in the var after ';'.
1938 * Create a new list for this. */
1939 l = list_alloc();
1940 if (l == NULL)
1941 return FAIL;
1942 while (item != NULL)
1943 {
1944 list_append_tv(l, &item->li_tv);
1945 item = item->li_next;
1946 }
1947
1948 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001949 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 ltv.vval.v_list = l;
1951 l->lv_refcount = 1;
1952
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1954 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 clear_tv(&ltv);
1956 if (arg == NULL)
1957 return FAIL;
1958 break;
1959 }
1960 else if (*arg != ',' && *arg != ']')
1961 {
1962 EMSG2(_(e_intern2), "ex_let_vars()");
1963 return FAIL;
1964 }
1965 }
1966
1967 return OK;
1968}
1969
1970/*
1971 * Skip over assignable variable "var" or list of variables "[var, var]".
1972 * Used for ":let varvar = expr" and ":for varvar in expr".
1973 * For "[var, var]" increment "*var_count" for each variable.
1974 * for "[var, var; var]" set "semicolon".
1975 * Return NULL for an error.
1976 */
1977 static char_u *
1978skip_var_list(arg, var_count, semicolon)
1979 char_u *arg;
1980 int *var_count;
1981 int *semicolon;
1982{
1983 char_u *p, *s;
1984
1985 if (*arg == '[')
1986 {
1987 /* "[var, var]": find the matching ']'. */
1988 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001989 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 {
1991 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1992 s = skip_var_one(p);
1993 if (s == p)
1994 {
1995 EMSG2(_(e_invarg2), p);
1996 return NULL;
1997 }
1998 ++*var_count;
1999
2000 p = skipwhite(s);
2001 if (*p == ']')
2002 break;
2003 else if (*p == ';')
2004 {
2005 if (*semicolon == 1)
2006 {
2007 EMSG(_("Double ; in list of variables"));
2008 return NULL;
2009 }
2010 *semicolon = 1;
2011 }
2012 else if (*p != ',')
2013 {
2014 EMSG2(_(e_invarg2), p);
2015 return NULL;
2016 }
2017 }
2018 return p + 1;
2019 }
2020 else
2021 return skip_var_one(arg);
2022}
2023
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002024/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002025 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002026 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002027 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 static char_u *
2029skip_var_one(arg)
2030 char_u *arg;
2031{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002032 if (*arg == '@' && arg[1] != NUL)
2033 return arg + 2;
2034 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2035 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002036}
2037
Bram Moolenaara7043832005-01-21 11:56:39 +00002038/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002039 * List variables for hashtab "ht" with prefix "prefix".
2040 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002041 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002043list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002044 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002045 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002046 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002047 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002048{
Bram Moolenaar33570922005-01-25 22:26:29 +00002049 hashitem_T *hi;
2050 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002051 int todo;
2052
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002053 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002054 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2055 {
2056 if (!HASHITEM_EMPTY(hi))
2057 {
2058 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002059 di = HI2DI(hi);
2060 if (empty || di->di_tv.v_type != VAR_STRING
2061 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002062 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002063 }
2064 }
2065}
2066
2067/*
2068 * List global variables.
2069 */
2070 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071list_glob_vars(first)
2072 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002073{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002074 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002075}
2076
2077/*
2078 * List buffer variables.
2079 */
2080 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081list_buf_vars(first)
2082 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 char_u numbuf[NUMBUFLEN];
2085
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002086 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2087 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002088
2089 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2091 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002092}
2093
2094/*
2095 * List window variables.
2096 */
2097 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098list_win_vars(first)
2099 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002100{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2102 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002103}
2104
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002105#ifdef FEAT_WINDOWS
2106/*
2107 * List tab page variables.
2108 */
2109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110list_tab_vars(first)
2111 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002112{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2114 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002115}
2116#endif
2117
Bram Moolenaara7043832005-01-21 11:56:39 +00002118/*
2119 * List Vim variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_vim_vars(first)
2123 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126}
2127
2128/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002129 * List script-local variables, if there is a script.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_script_vars(first)
2133 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002134{
2135 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2137 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002138}
2139
2140/*
2141 * List function variables, if there is a function.
2142 */
2143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144list_func_vars(first)
2145 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002146{
2147 if (current_funccal != NULL)
2148 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002150}
2151
2152/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 * List variables in "arg".
2154 */
2155 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 exarg_T *eap;
2158 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160{
2161 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002164 char_u *name_start;
2165 char_u *arg_subsc;
2166 char_u *tofree;
2167 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168
2169 while (!ends_excmd(*arg) && !got_int)
2170 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002171 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002173 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2175 {
2176 emsg_severe = TRUE;
2177 EMSG(_(e_trailing));
2178 break;
2179 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 /* get_name_len() takes care of expanding curly braces */
2184 name_start = name = arg;
2185 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2186 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 /* This is mainly to keep test 49 working: when expanding
2189 * curly braces fails overrule the exception error message. */
2190 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 emsg_severe = TRUE;
2193 EMSG2(_(e_invarg2), arg);
2194 break;
2195 }
2196 error = TRUE;
2197 }
2198 else
2199 {
2200 if (tofree != NULL)
2201 name = tofree;
2202 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 else
2205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 /* handle d.key, l[idx], f(expr) */
2207 arg_subsc = arg;
2208 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002209 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002213 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002215 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 case 'g': list_glob_vars(first); break;
2217 case 'b': list_buf_vars(first); break;
2218 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002221#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222 case 'v': list_vim_vars(first); break;
2223 case 's': list_script_vars(first); break;
2224 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 default:
2226 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002227 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002228 }
2229 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 {
2231 char_u numbuf[NUMBUFLEN];
2232 char_u *tf;
2233 int c;
2234 char_u *s;
2235
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002236 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 c = *arg;
2238 *arg = NUL;
2239 list_one_var_a((char_u *)"",
2240 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 tv.v_type,
2242 s == NULL ? (char_u *)"" : s,
2243 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 *arg = c;
2245 vim_free(tf);
2246 }
2247 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 }
2250 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251
2252 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254
2255 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 }
2257
2258 return arg;
2259}
2260
2261/*
2262 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2263 * Returns a pointer to the char just after the var name.
2264 * Returns NULL if there is an error.
2265 */
2266 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002269 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002271 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273{
2274 int c1;
2275 char_u *name;
2276 char_u *p;
2277 char_u *arg_end = NULL;
2278 int len;
2279 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281
2282 /*
2283 * ":let $VAR = expr": Set environment variable.
2284 */
2285 if (*arg == '$')
2286 {
2287 /* Find the end of the name. */
2288 ++arg;
2289 name = arg;
2290 len = get_env_len(&arg);
2291 if (len == 0)
2292 EMSG2(_(e_invarg2), name - 1);
2293 else
2294 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295 if (op != NULL && (*op == '+' || *op == '-'))
2296 EMSG2(_(e_letwrong), op);
2297 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002298 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 EMSG(_(e_letunexp));
2300 else
2301 {
2302 c1 = name[len];
2303 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002304 p = get_tv_string_chk(tv);
2305 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 {
2307 int mustfree = FALSE;
2308 char_u *s = vim_getenv(name, &mustfree);
2309
2310 if (s != NULL)
2311 {
2312 p = tofree = concat_str(s, p);
2313 if (mustfree)
2314 vim_free(s);
2315 }
2316 }
2317 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002318 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002320 if (STRICMP(name, "HOME") == 0)
2321 init_homedir();
2322 else if (didset_vim && STRICMP(name, "VIM") == 0)
2323 didset_vim = FALSE;
2324 else if (didset_vimruntime
2325 && STRICMP(name, "VIMRUNTIME") == 0)
2326 didset_vimruntime = FALSE;
2327 arg_end = arg;
2328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
2332 }
2333 }
2334
2335 /*
2336 * ":let &option = expr": Set option value.
2337 * ":let &l:option = expr": Set local option value.
2338 * ":let &g:option = expr": Set global option value.
2339 */
2340 else if (*arg == '&')
2341 {
2342 /* Find the end of the name. */
2343 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002344 if (p == NULL || (endchars != NULL
2345 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 EMSG(_(e_letunexp));
2347 else
2348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 long n;
2350 int opt_type;
2351 long numval;
2352 char_u *stringval = NULL;
2353 char_u *s;
2354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 c1 = *p;
2356 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357
2358 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002359 s = get_tv_string_chk(tv); /* != NULL if number or string */
2360 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 {
2362 opt_type = get_option_value(arg, &numval,
2363 &stringval, opt_flags);
2364 if ((opt_type == 1 && *op == '.')
2365 || (opt_type == 0 && *op != '.'))
2366 EMSG2(_(e_letwrong), op);
2367 else
2368 {
2369 if (opt_type == 1) /* number */
2370 {
2371 if (*op == '+')
2372 n = numval + n;
2373 else
2374 n = numval - n;
2375 }
2376 else if (opt_type == 0 && stringval != NULL) /* string */
2377 {
2378 s = concat_str(stringval, s);
2379 vim_free(stringval);
2380 stringval = s;
2381 }
2382 }
2383 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 if (s != NULL)
2385 {
2386 set_option_value(arg, n, s, opt_flags);
2387 arg_end = p;
2388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 }
2392 }
2393
2394 /*
2395 * ":let @r = expr": Set register contents.
2396 */
2397 else if (*arg == '@')
2398 {
2399 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 if (op != NULL && (*op == '+' || *op == '-'))
2401 EMSG2(_(e_letwrong), op);
2402 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002403 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002407 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 char_u *s;
2409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 p = get_tv_string_chk(tv);
2411 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002413 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 if (s != NULL)
2415 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002416 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 vim_free(s);
2418 }
2419 }
2420 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002421 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 arg_end = arg + 1;
2424 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002425 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 }
2427 }
2428
2429 /*
2430 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002433 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002435 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002437 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2441 EMSG(_(e_letunexp));
2442 else
2443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450
2451 else
2452 EMSG2(_(e_invarg2), arg);
2453
2454 return arg_end;
2455}
2456
2457/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2459 */
2460 static int
2461check_changedtick(arg)
2462 char_u *arg;
2463{
2464 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2465 {
2466 EMSG2(_(e_readonlyvar), arg);
2467 return TRUE;
2468 }
2469 return FALSE;
2470}
2471
2472/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 * Get an lval: variable, Dict item or List item that can be assigned a value
2474 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2475 * "name.key", "name.key[expr]" etc.
2476 * Indexing only works if "name" is an existing List or Dictionary.
2477 * "name" points to the start of the name.
2478 * If "rettv" is not NULL it points to the value to be assigned.
2479 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2480 * wrong; must end in space or cmd separator.
2481 *
2482 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002483 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 */
2486 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002487get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002489 typval_T *rettv;
2490 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 int unlet;
2492 int skip;
2493 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002494 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 char_u *expr_start, *expr_end;
2498 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002499 dictitem_T *v;
2500 typval_T var1;
2501 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002509 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510
2511 if (skip)
2512 {
2513 /* When skipping just find the end of the name. */
2514 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 }
2517
2518 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002519 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (expr_start != NULL)
2521 {
2522 /* Don't expand the name when we already know there is an error. */
2523 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2524 && *p != '[' && *p != '.')
2525 {
2526 EMSG(_(e_trailing));
2527 return NULL;
2528 }
2529
2530 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2531 if (lp->ll_exp_name == NULL)
2532 {
2533 /* Report an invalid expression in braces, unless the
2534 * expression evaluation has been cancelled due to an
2535 * aborting error, an interrupt, or an exception. */
2536 if (!aborting() && !quiet)
2537 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002538 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 EMSG2(_(e_invarg2), name);
2540 return NULL;
2541 }
2542 }
2543 lp->ll_name = lp->ll_exp_name;
2544 }
2545 else
2546 lp->ll_name = name;
2547
2548 /* Without [idx] or .key we are done. */
2549 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2550 return p;
2551
2552 cc = *p;
2553 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002554 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (v == NULL && !quiet)
2556 EMSG2(_(e_undefvar), lp->ll_name);
2557 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 if (v == NULL)
2559 return NULL;
2560
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 /*
2562 * Loop until no more [idx] or .key is following.
2563 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2568 && !(lp->ll_tv->v_type == VAR_DICT
2569 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 if (!quiet)
2572 EMSG(_("E689: Can only index a List or Dictionary"));
2573 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (!quiet)
2578 EMSG(_("E708: [:] must come last"));
2579 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002581
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 len = -1;
2583 if (*p == '.')
2584 {
2585 key = p + 1;
2586 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2587 ;
2588 if (len == 0)
2589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!quiet)
2591 EMSG(_(e_emptykey));
2592 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 }
2594 p = key + len;
2595 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596 else
2597 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002599 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 if (*p == ':')
2601 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002602 else
2603 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 empty1 = FALSE;
2605 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002607 if (get_tv_string_chk(&var1) == NULL)
2608 {
2609 /* not a number or string */
2610 clear_tv(&var1);
2611 return NULL;
2612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 }
2614
2615 /* Optionally get the second index [ :expr]. */
2616 if (*p == ':')
2617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 if (!empty1)
2623 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (rettv != NULL && (rettv->v_type != VAR_LIST
2627 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!quiet)
2630 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 if (!empty1)
2632 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 }
2635 p = skipwhite(p + 1);
2636 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 else
2639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 if (!empty1)
2644 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002647 if (get_tv_string_chk(&var2) == NULL)
2648 {
2649 /* not a number or string */
2650 if (!empty1)
2651 clear_tv(&var1);
2652 clear_tv(&var2);
2653 return NULL;
2654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (!empty1)
2666 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
2671
2672 /* Skip to past ']'. */
2673 ++p;
2674 }
2675
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 {
2678 if (len == -1)
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002681 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (*key == NUL)
2683 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (!quiet)
2685 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690 lp->ll_list = NULL;
2691 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002692 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002695 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002699 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (len == -1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (len == -1)
2709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 p = NULL;
2712 break;
2713 }
2714 if (len == -1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 else
2719 {
2720 /*
2721 * Get the number and item for the only or first index of the List.
2722 */
2723 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 else
2726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002727 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 clear_tv(&var1);
2729 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002730 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_list = lp->ll_tv->vval.v_list;
2732 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2733 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002735 if (lp->ll_n1 < 0)
2736 {
2737 lp->ll_n1 = 0;
2738 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2739 }
2740 }
2741 if (lp->ll_li == NULL)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747
2748 /*
2749 * May need to find the item or absolute index for the second
2750 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 * When no index given: "lp->ll_empty2" is TRUE.
2752 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2767 if (lp->ll_n1 < 0)
2768 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2769 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002771 }
2772
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002775 }
2776
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 return p;
2778}
2779
2780/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002781 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 */
2783 static void
2784clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002785 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786{
2787 vim_free(lp->ll_exp_name);
2788 vim_free(lp->ll_newkey);
2789}
2790
2791/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002792 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 */
2796 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002797set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803{
2804 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 listitem_T *ri;
2806 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807
2808 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002811 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 cc = *endp;
2813 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 if (op != NULL && *op != '=')
2815 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002818 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002819 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002820 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 {
2822 if (tv_op(&tv, rettv, op) == OK)
2823 set_var(lp->ll_name, &tv, FALSE);
2824 clear_tv(&tv);
2825 }
2826 }
2827 else
2828 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002830 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002832 else if (tv_check_lock(lp->ll_newkey == NULL
2833 ? lp->ll_tv->v_lock
2834 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2835 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 else if (lp->ll_range)
2837 {
2838 /*
2839 * Assign the List values to the list items.
2840 */
2841 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002842 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 if (op != NULL && *op != '=')
2844 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2845 else
2846 {
2847 clear_tv(&lp->ll_li->li_tv);
2848 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2849 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 ri = ri->li_next;
2851 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2852 break;
2853 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002856 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 ri = NULL;
2859 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002860 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_li = lp->ll_li->li_next;
2863 ++lp->ll_n1;
2864 }
2865 if (ri != NULL)
2866 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002867 else if (lp->ll_empty2
2868 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 : lp->ll_n1 != lp->ll_n2)
2870 EMSG(_("E711: List value has not enough items"));
2871 }
2872 else
2873 {
2874 /*
2875 * Assign to a List or Dictionary item.
2876 */
2877 if (lp->ll_newkey != NULL)
2878 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879 if (op != NULL && *op != '=')
2880 {
2881 EMSG2(_(e_letwrong), op);
2882 return;
2883 }
2884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002886 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 if (di == NULL)
2888 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002889 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2890 {
2891 vim_free(di);
2892 return;
2893 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002895 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 else if (op != NULL && *op != '=')
2897 {
2898 tv_op(lp->ll_tv, rettv, op);
2899 return;
2900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 /*
2905 * Assign the value to the variable or list item.
2906 */
2907 if (copy)
2908 copy_tv(rettv, lp->ll_tv);
2909 else
2910 {
2911 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002912 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 }
2915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002916}
2917
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2920 * Returns OK or FAIL.
2921 */
2922 static int
2923tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T *tv1;
2925 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 char_u *op;
2927{
2928 long n;
2929 char_u numbuf[NUMBUFLEN];
2930 char_u *s;
2931
2932 /* Can't do anything with a Funcref or a Dict on the right. */
2933 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2934 {
2935 switch (tv1->v_type)
2936 {
2937 case VAR_DICT:
2938 case VAR_FUNC:
2939 break;
2940
2941 case VAR_LIST:
2942 if (*op != '+' || tv2->v_type != VAR_LIST)
2943 break;
2944 /* List += List */
2945 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2946 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2947 return OK;
2948
2949 case VAR_NUMBER:
2950 case VAR_STRING:
2951 if (tv2->v_type == VAR_LIST)
2952 break;
2953 if (*op == '+' || *op == '-')
2954 {
2955 /* nr += nr or nr -= nr*/
2956 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002957#ifdef FEAT_FLOAT
2958 if (tv2->v_type == VAR_FLOAT)
2959 {
2960 float_T f = n;
2961
2962 if (*op == '+')
2963 f += tv2->vval.v_float;
2964 else
2965 f -= tv2->vval.v_float;
2966 clear_tv(tv1);
2967 tv1->v_type = VAR_FLOAT;
2968 tv1->vval.v_float = f;
2969 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002971#endif
2972 {
2973 if (*op == '+')
2974 n += get_tv_number(tv2);
2975 else
2976 n -= get_tv_number(tv2);
2977 clear_tv(tv1);
2978 tv1->v_type = VAR_NUMBER;
2979 tv1->vval.v_number = n;
2980 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 }
2982 else
2983 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002984 if (tv2->v_type == VAR_FLOAT)
2985 break;
2986
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 /* str .= str */
2988 s = get_tv_string(tv1);
2989 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2990 clear_tv(tv1);
2991 tv1->v_type = VAR_STRING;
2992 tv1->vval.v_string = s;
2993 }
2994 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002995
2996#ifdef FEAT_FLOAT
2997 case VAR_FLOAT:
2998 {
2999 float_T f;
3000
3001 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3002 && tv2->v_type != VAR_NUMBER
3003 && tv2->v_type != VAR_STRING))
3004 break;
3005 if (tv2->v_type == VAR_FLOAT)
3006 f = tv2->vval.v_float;
3007 else
3008 f = get_tv_number(tv2);
3009 if (*op == '+')
3010 tv1->vval.v_float += f;
3011 else
3012 tv1->vval.v_float -= f;
3013 }
3014 return OK;
3015#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 }
3017 }
3018
3019 EMSG2(_(e_letwrong), op);
3020 return FAIL;
3021}
3022
3023/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003024 * Add a watcher to a list.
3025 */
3026 static void
3027list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003028 list_T *l;
3029 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003030{
3031 lw->lw_next = l->lv_watch;
3032 l->lv_watch = lw;
3033}
3034
3035/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003036 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003037 * No warning when it isn't found...
3038 */
3039 static void
3040list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003041 list_T *l;
3042 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003043{
Bram Moolenaar33570922005-01-25 22:26:29 +00003044 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045
3046 lwp = &l->lv_watch;
3047 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3048 {
3049 if (lw == lwrem)
3050 {
3051 *lwp = lw->lw_next;
3052 break;
3053 }
3054 lwp = &lw->lw_next;
3055 }
3056}
3057
3058/*
3059 * Just before removing an item from a list: advance watchers to the next
3060 * item.
3061 */
3062 static void
3063list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 list_T *l;
3065 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066{
Bram Moolenaar33570922005-01-25 22:26:29 +00003067 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003068
3069 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3070 if (lw->lw_item == item)
3071 lw->lw_item = item->li_next;
3072}
3073
3074/*
3075 * Evaluate the expression used in a ":for var in expr" command.
3076 * "arg" points to "var".
3077 * Set "*errp" to TRUE for an error, FALSE otherwise;
3078 * Return a pointer that holds the info. Null when there is an error.
3079 */
3080 void *
3081eval_for_line(arg, errp, nextcmdp, skip)
3082 char_u *arg;
3083 int *errp;
3084 char_u **nextcmdp;
3085 int skip;
3086{
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 typval_T tv;
3090 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003091
3092 *errp = TRUE; /* default: there is an error */
3093
Bram Moolenaar33570922005-01-25 22:26:29 +00003094 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003095 if (fi == NULL)
3096 return NULL;
3097
3098 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3099 if (expr == NULL)
3100 return fi;
3101
3102 expr = skipwhite(expr);
3103 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3104 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003105 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106 return fi;
3107 }
3108
3109 if (skip)
3110 ++emsg_skip;
3111 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3112 {
3113 *errp = FALSE;
3114 if (!skip)
3115 {
3116 l = tv.vval.v_list;
3117 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003118 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003120 clear_tv(&tv);
3121 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122 else
3123 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003124 /* No need to increment the refcount, it's already set for the
3125 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126 fi->fi_list = l;
3127 list_add_watch(l, &fi->fi_lw);
3128 fi->fi_lw.lw_item = l->lv_first;
3129 }
3130 }
3131 }
3132 if (skip)
3133 --emsg_skip;
3134
3135 return fi;
3136}
3137
3138/*
3139 * Use the first item in a ":for" list. Advance to the next.
3140 * Assign the values to the variable (list). "arg" points to the first one.
3141 * Return TRUE when a valid item was found, FALSE when at end of list or
3142 * something wrong.
3143 */
3144 int
3145next_for_item(fi_void, arg)
3146 void *fi_void;
3147 char_u *arg;
3148{
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003152
3153 item = fi->fi_lw.lw_item;
3154 if (item == NULL)
3155 result = FALSE;
3156 else
3157 {
3158 fi->fi_lw.lw_item = item->li_next;
3159 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3160 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3161 }
3162 return result;
3163}
3164
3165/*
3166 * Free the structure used to store info used by ":for".
3167 */
3168 void
3169free_for_info(fi_void)
3170 void *fi_void;
3171{
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003174 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003175 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003177 list_unref(fi->fi_list);
3178 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003179 vim_free(fi);
3180}
3181
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3183
3184 void
3185set_context_for_expression(xp, arg, cmdidx)
3186 expand_T *xp;
3187 char_u *arg;
3188 cmdidx_T cmdidx;
3189{
3190 int got_eq = FALSE;
3191 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 if (cmdidx == CMD_let)
3195 {
3196 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003197 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 {
3199 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003200 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 {
3202 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 if (vim_iswhite(*p))
3205 break;
3206 }
3207 return;
3208 }
3209 }
3210 else
3211 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3212 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 while ((xp->xp_pattern = vim_strpbrk(arg,
3214 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3215 {
3216 c = *xp->xp_pattern;
3217 if (c == '&')
3218 {
3219 c = xp->xp_pattern[1];
3220 if (c == '&')
3221 {
3222 ++xp->xp_pattern;
3223 xp->xp_context = cmdidx != CMD_let || got_eq
3224 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3225 }
3226 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003229 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3230 xp->xp_pattern += 2;
3231
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234 else if (c == '$')
3235 {
3236 /* environment variable */
3237 xp->xp_context = EXPAND_ENV_VARS;
3238 }
3239 else if (c == '=')
3240 {
3241 got_eq = TRUE;
3242 xp->xp_context = EXPAND_EXPRESSION;
3243 }
3244 else if (c == '<'
3245 && xp->xp_context == EXPAND_FUNCTIONS
3246 && vim_strchr(xp->xp_pattern, '(') == NULL)
3247 {
3248 /* Function name can start with "<SNR>" */
3249 break;
3250 }
3251 else if (cmdidx != CMD_let || got_eq)
3252 {
3253 if (c == '"') /* string */
3254 {
3255 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3256 if (c == '\\' && xp->xp_pattern[1] != NUL)
3257 ++xp->xp_pattern;
3258 xp->xp_context = EXPAND_NOTHING;
3259 }
3260 else if (c == '\'') /* literal string */
3261 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003262 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3264 /* skip */ ;
3265 xp->xp_context = EXPAND_NOTHING;
3266 }
3267 else if (c == '|')
3268 {
3269 if (xp->xp_pattern[1] == '|')
3270 {
3271 ++xp->xp_pattern;
3272 xp->xp_context = EXPAND_EXPRESSION;
3273 }
3274 else
3275 xp->xp_context = EXPAND_COMMANDS;
3276 }
3277 else
3278 xp->xp_context = EXPAND_EXPRESSION;
3279 }
3280 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 /* Doesn't look like something valid, expand as an expression
3282 * anyway. */
3283 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 arg = xp->xp_pattern;
3285 if (*arg != NUL)
3286 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3287 /* skip */ ;
3288 }
3289 xp->xp_pattern = arg;
3290}
3291
3292#endif /* FEAT_CMDL_COMPL */
3293
3294/*
3295 * ":1,25call func(arg1, arg2)" function call.
3296 */
3297 void
3298ex_call(eap)
3299 exarg_T *eap;
3300{
3301 char_u *arg = eap->arg;
3302 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003304 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 linenr_T lnum;
3308 int doesrange;
3309 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003310 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003312 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003313 if (fudi.fd_newkey != NULL)
3314 {
3315 /* Still need to give an error message for missing key. */
3316 EMSG2(_(e_dictkey), fudi.fd_newkey);
3317 vim_free(fudi.fd_newkey);
3318 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003319 if (tofree == NULL)
3320 return;
3321
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003322 /* Increase refcount on dictionary, it could get deleted when evaluating
3323 * the arguments. */
3324 if (fudi.fd_dict != NULL)
3325 ++fudi.fd_dict->dv_refcount;
3326
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003327 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003328 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003329 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
Bram Moolenaar532c7802005-01-27 14:44:31 +00003331 /* Skip white space to allow ":call func ()". Not good, but required for
3332 * backward compatibility. */
3333 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003334 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 if (*startarg != '(')
3337 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003338 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 goto end;
3340 }
3341
3342 /*
3343 * When skipping, evaluate the function once, to find the end of the
3344 * arguments.
3345 * When the function takes a range, this is discovered after the first
3346 * call, and the loop is broken.
3347 */
3348 if (eap->skip)
3349 {
3350 ++emsg_skip;
3351 lnum = eap->line2; /* do it once, also with an invalid range */
3352 }
3353 else
3354 lnum = eap->line1;
3355 for ( ; lnum <= eap->line2; ++lnum)
3356 {
3357 if (!eap->skip && eap->addr_count > 0)
3358 {
3359 curwin->w_cursor.lnum = lnum;
3360 curwin->w_cursor.col = 0;
3361 }
3362 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003363 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003364 eap->line1, eap->line2, &doesrange,
3365 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 failed = TRUE;
3368 break;
3369 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003370
3371 /* Handle a function returning a Funcref, Dictionary or List. */
3372 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3373 {
3374 failed = TRUE;
3375 break;
3376 }
3377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003378 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 if (doesrange || eap->skip)
3380 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003383 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003384 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003385 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (aborting())
3387 break;
3388 }
3389 if (eap->skip)
3390 --emsg_skip;
3391
3392 if (!failed)
3393 {
3394 /* Check for trailing illegal characters and a following command. */
3395 if (!ends_excmd(*arg))
3396 {
3397 emsg_severe = TRUE;
3398 EMSG(_(e_trailing));
3399 }
3400 else
3401 eap->nextcmd = check_nextcmd(arg);
3402 }
3403
3404end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003405 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003406 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407}
3408
3409/*
3410 * ":unlet[!] var1 ... " command.
3411 */
3412 void
3413ex_unlet(eap)
3414 exarg_T *eap;
3415{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003416 ex_unletlock(eap, eap->arg, 0);
3417}
3418
3419/*
3420 * ":lockvar" and ":unlockvar" commands
3421 */
3422 void
3423ex_lockvar(eap)
3424 exarg_T *eap;
3425{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003427 int deep = 2;
3428
3429 if (eap->forceit)
3430 deep = -1;
3431 else if (vim_isdigit(*arg))
3432 {
3433 deep = getdigits(&arg);
3434 arg = skipwhite(arg);
3435 }
3436
3437 ex_unletlock(eap, arg, deep);
3438}
3439
3440/*
3441 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3442 */
3443 static void
3444ex_unletlock(eap, argstart, deep)
3445 exarg_T *eap;
3446 char_u *argstart;
3447 int deep;
3448{
3449 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003452 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 do
3455 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003456 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003457 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3458 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003459 if (lv.ll_name == NULL)
3460 error = TRUE; /* error but continue parsing */
3461 if (name_end == NULL || (!vim_iswhite(*name_end)
3462 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003464 if (name_end != NULL)
3465 {
3466 emsg_severe = TRUE;
3467 EMSG(_(e_trailing));
3468 }
3469 if (!(eap->skip || error))
3470 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 break;
3472 }
3473
3474 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003475 {
3476 if (eap->cmdidx == CMD_unlet)
3477 {
3478 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3479 error = TRUE;
3480 }
3481 else
3482 {
3483 if (do_lock_var(&lv, name_end, deep,
3484 eap->cmdidx == CMD_lockvar) == FAIL)
3485 error = TRUE;
3486 }
3487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003489 if (!eap->skip)
3490 clear_lval(&lv);
3491
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 arg = skipwhite(name_end);
3493 } while (!ends_excmd(*arg));
3494
3495 eap->nextcmd = check_nextcmd(arg);
3496}
3497
Bram Moolenaar8c711452005-01-14 21:53:12 +00003498 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003499do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003501 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003502 int forceit;
3503{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003504 int ret = OK;
3505 int cc;
3506
3507 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003509 cc = *name_end;
3510 *name_end = NUL;
3511
3512 /* Normal name or expanded name. */
3513 if (check_changedtick(lp->ll_name))
3514 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003515 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003516 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003517 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003518 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003519 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3520 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003521 else if (lp->ll_range)
3522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003523 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524
3525 /* Delete a range of List items. */
3526 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3527 {
3528 li = lp->ll_li->li_next;
3529 listitem_remove(lp->ll_list, lp->ll_li);
3530 lp->ll_li = li;
3531 ++lp->ll_n1;
3532 }
3533 }
3534 else
3535 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003537 /* unlet a List item. */
3538 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003541 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003542 }
3543
3544 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003545}
3546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547/*
3548 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 */
3551 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaar33570922005-01-25 22:26:29 +00003556 hashtab_T *ht;
3557 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003558 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003559 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
Bram Moolenaar33570922005-01-25 22:26:29 +00003561 ht = find_var_ht(name, &varname);
3562 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003564 hi = hash_find(ht, varname);
3565 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003566 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003567 di = HI2DI(hi);
3568 if (var_check_fixed(di->di_flags, name)
3569 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 return FAIL;
3571 delete_var(ht, hi);
3572 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575 if (forceit)
3576 return OK;
3577 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 return FAIL;
3579}
3580
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003581/*
3582 * Lock or unlock variable indicated by "lp".
3583 * "deep" is the levels to go (-1 for unlimited);
3584 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3585 */
3586 static int
3587do_lock_var(lp, name_end, deep, lock)
3588 lval_T *lp;
3589 char_u *name_end;
3590 int deep;
3591 int lock;
3592{
3593 int ret = OK;
3594 int cc;
3595 dictitem_T *di;
3596
3597 if (deep == 0) /* nothing to do */
3598 return OK;
3599
3600 if (lp->ll_tv == NULL)
3601 {
3602 cc = *name_end;
3603 *name_end = NUL;
3604
3605 /* Normal name or expanded name. */
3606 if (check_changedtick(lp->ll_name))
3607 ret = FAIL;
3608 else
3609 {
3610 di = find_var(lp->ll_name, NULL);
3611 if (di == NULL)
3612 ret = FAIL;
3613 else
3614 {
3615 if (lock)
3616 di->di_flags |= DI_FLAGS_LOCK;
3617 else
3618 di->di_flags &= ~DI_FLAGS_LOCK;
3619 item_lock(&di->di_tv, deep, lock);
3620 }
3621 }
3622 *name_end = cc;
3623 }
3624 else if (lp->ll_range)
3625 {
3626 listitem_T *li = lp->ll_li;
3627
3628 /* (un)lock a range of List items. */
3629 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3630 {
3631 item_lock(&li->li_tv, deep, lock);
3632 li = li->li_next;
3633 ++lp->ll_n1;
3634 }
3635 }
3636 else if (lp->ll_list != NULL)
3637 /* (un)lock a List item. */
3638 item_lock(&lp->ll_li->li_tv, deep, lock);
3639 else
3640 /* un(lock) a Dictionary item. */
3641 item_lock(&lp->ll_di->di_tv, deep, lock);
3642
3643 return ret;
3644}
3645
3646/*
3647 * Lock or unlock an item. "deep" is nr of levels to go.
3648 */
3649 static void
3650item_lock(tv, deep, lock)
3651 typval_T *tv;
3652 int deep;
3653 int lock;
3654{
3655 static int recurse = 0;
3656 list_T *l;
3657 listitem_T *li;
3658 dict_T *d;
3659 hashitem_T *hi;
3660 int todo;
3661
3662 if (recurse >= DICT_MAXNEST)
3663 {
3664 EMSG(_("E743: variable nested too deep for (un)lock"));
3665 return;
3666 }
3667 if (deep == 0)
3668 return;
3669 ++recurse;
3670
3671 /* lock/unlock the item itself */
3672 if (lock)
3673 tv->v_lock |= VAR_LOCKED;
3674 else
3675 tv->v_lock &= ~VAR_LOCKED;
3676
3677 switch (tv->v_type)
3678 {
3679 case VAR_LIST:
3680 if ((l = tv->vval.v_list) != NULL)
3681 {
3682 if (lock)
3683 l->lv_lock |= VAR_LOCKED;
3684 else
3685 l->lv_lock &= ~VAR_LOCKED;
3686 if (deep < 0 || deep > 1)
3687 /* recursive: lock/unlock the items the List contains */
3688 for (li = l->lv_first; li != NULL; li = li->li_next)
3689 item_lock(&li->li_tv, deep - 1, lock);
3690 }
3691 break;
3692 case VAR_DICT:
3693 if ((d = tv->vval.v_dict) != NULL)
3694 {
3695 if (lock)
3696 d->dv_lock |= VAR_LOCKED;
3697 else
3698 d->dv_lock &= ~VAR_LOCKED;
3699 if (deep < 0 || deep > 1)
3700 {
3701 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003702 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003703 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3704 {
3705 if (!HASHITEM_EMPTY(hi))
3706 {
3707 --todo;
3708 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3709 }
3710 }
3711 }
3712 }
3713 }
3714 --recurse;
3715}
3716
Bram Moolenaara40058a2005-07-11 22:42:07 +00003717/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003718 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3719 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003720 */
3721 static int
3722tv_islocked(tv)
3723 typval_T *tv;
3724{
3725 return (tv->v_lock & VAR_LOCKED)
3726 || (tv->v_type == VAR_LIST
3727 && tv->vval.v_list != NULL
3728 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3729 || (tv->v_type == VAR_DICT
3730 && tv->vval.v_dict != NULL
3731 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3732}
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3735/*
3736 * Delete all "menutrans_" variables.
3737 */
3738 void
3739del_menutrans_vars()
3740{
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003742 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003745 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003746 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003747 {
3748 if (!HASHITEM_EMPTY(hi))
3749 {
3750 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003751 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3752 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003753 }
3754 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003755 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756}
3757#endif
3758
3759#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3760
3761/*
3762 * Local string buffer for the next two functions to store a variable name
3763 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3764 * get_user_var_name().
3765 */
3766
3767static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3768
3769static char_u *varnamebuf = NULL;
3770static int varnamebuflen = 0;
3771
3772/*
3773 * Function to concatenate a prefix and a variable name.
3774 */
3775 static char_u *
3776cat_prefix_varname(prefix, name)
3777 int prefix;
3778 char_u *name;
3779{
3780 int len;
3781
3782 len = (int)STRLEN(name) + 3;
3783 if (len > varnamebuflen)
3784 {
3785 vim_free(varnamebuf);
3786 len += 10; /* some additional space */
3787 varnamebuf = alloc(len);
3788 if (varnamebuf == NULL)
3789 {
3790 varnamebuflen = 0;
3791 return NULL;
3792 }
3793 varnamebuflen = len;
3794 }
3795 *varnamebuf = prefix;
3796 varnamebuf[1] = ':';
3797 STRCPY(varnamebuf + 2, name);
3798 return varnamebuf;
3799}
3800
3801/*
3802 * Function given to ExpandGeneric() to obtain the list of user defined
3803 * (global/buffer/window/built-in) variable names.
3804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 char_u *
3806get_user_var_name(xp, idx)
3807 expand_T *xp;
3808 int idx;
3809{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003810 static long_u gdone;
3811 static long_u bdone;
3812 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003813#ifdef FEAT_WINDOWS
3814 static long_u tdone;
3815#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003816 static int vidx;
3817 static hashitem_T *hi;
3818 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003821 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003822 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003823#ifdef FEAT_WINDOWS
3824 tdone = 0;
3825#endif
3826 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003827
3828 /* Global variables */
3829 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003831 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003832 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003833 else
3834 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003835 while (HASHITEM_EMPTY(hi))
3836 ++hi;
3837 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3838 return cat_prefix_varname('g', hi->hi_key);
3839 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003841
3842 /* b: variables */
3843 ht = &curbuf->b_vars.dv_hashtab;
3844 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003846 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003848 else
3849 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 while (HASHITEM_EMPTY(hi))
3851 ++hi;
3852 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003854 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003856 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return (char_u *)"b:changedtick";
3858 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003859
3860 /* w: variables */
3861 ht = &curwin->w_vars.dv_hashtab;
3862 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003864 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003865 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003866 else
3867 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003868 while (HASHITEM_EMPTY(hi))
3869 ++hi;
3870 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003872
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003873#ifdef FEAT_WINDOWS
3874 /* t: variables */
3875 ht = &curtab->tp_vars.dv_hashtab;
3876 if (tdone < ht->ht_used)
3877 {
3878 if (tdone++ == 0)
3879 hi = ht->ht_array;
3880 else
3881 ++hi;
3882 while (HASHITEM_EMPTY(hi))
3883 ++hi;
3884 return cat_prefix_varname('t', hi->hi_key);
3885 }
3886#endif
3887
Bram Moolenaar33570922005-01-25 22:26:29 +00003888 /* v: variables */
3889 if (vidx < VV_LEN)
3890 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
3892 vim_free(varnamebuf);
3893 varnamebuf = NULL;
3894 varnamebuflen = 0;
3895 return NULL;
3896}
3897
3898#endif /* FEAT_CMDL_COMPL */
3899
3900/*
3901 * types for expressions.
3902 */
3903typedef enum
3904{
3905 TYPE_UNKNOWN = 0
3906 , TYPE_EQUAL /* == */
3907 , TYPE_NEQUAL /* != */
3908 , TYPE_GREATER /* > */
3909 , TYPE_GEQUAL /* >= */
3910 , TYPE_SMALLER /* < */
3911 , TYPE_SEQUAL /* <= */
3912 , TYPE_MATCH /* =~ */
3913 , TYPE_NOMATCH /* !~ */
3914} exptype_T;
3915
3916/*
3917 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3920 */
3921
3922/*
3923 * Handle zero level expression.
3924 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003926 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 * Return OK or FAIL.
3928 */
3929 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 char_u **nextcmd;
3934 int evaluate;
3935{
3936 int ret;
3937 char_u *p;
3938
3939 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003940 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 if (ret == FAIL || !ends_excmd(*p))
3942 {
3943 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 /*
3946 * Report the invalid expression unless the expression evaluation has
3947 * been cancelled due to an aborting error, an interrupt, or an
3948 * exception.
3949 */
3950 if (!aborting())
3951 EMSG2(_(e_invexpr2), arg);
3952 ret = FAIL;
3953 }
3954 if (nextcmd != NULL)
3955 *nextcmd = check_nextcmd(p);
3956
3957 return ret;
3958}
3959
3960/*
3961 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003962 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 *
3964 * "arg" must point to the first non-white of the expression.
3965 * "arg" is advanced to the next non-white after the recognized expression.
3966 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003967 * Note: "rettv.v_lock" is not set.
3968 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 * Return OK or FAIL.
3970 */
3971 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 int evaluate;
3976{
3977 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980 /*
3981 * Get the first variable.
3982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003983 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 return FAIL;
3985
3986 if ((*arg)[0] == '?')
3987 {
3988 result = FALSE;
3989 if (evaluate)
3990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 int error = FALSE;
3992
3993 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 if (error)
3997 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999
4000 /*
4001 * Get the second variable.
4002 */
4003 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 return FAIL;
4006
4007 /*
4008 * Check for the ":".
4009 */
4010 if ((*arg)[0] != ':')
4011 {
4012 EMSG(_("E109: Missing ':' after '?'"));
4013 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 return FAIL;
4016 }
4017
4018 /*
4019 * Get the third variable.
4020 */
4021 *arg = skipwhite(*arg + 1);
4022 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4023 {
4024 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004025 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return FAIL;
4027 }
4028 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031
4032 return OK;
4033}
4034
4035/*
4036 * Handle first level expression:
4037 * expr2 || expr2 || expr2 logical OR
4038 *
4039 * "arg" must point to the first non-white of the expression.
4040 * "arg" is advanced to the next non-white after the recognized expression.
4041 *
4042 * Return OK or FAIL.
4043 */
4044 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 int evaluate;
4049{
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 long result;
4052 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004053 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055 /*
4056 * Get the first variable.
4057 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return FAIL;
4060
4061 /*
4062 * Repeat until there is no following "||".
4063 */
4064 first = TRUE;
4065 result = FALSE;
4066 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4067 {
4068 if (evaluate && first)
4069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004070 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004073 if (error)
4074 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 first = FALSE;
4076 }
4077
4078 /*
4079 * Get the second variable.
4080 */
4081 *arg = skipwhite(*arg + 2);
4082 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4083 return FAIL;
4084
4085 /*
4086 * Compute the result.
4087 */
4088 if (evaluate && !result)
4089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004090 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 if (error)
4094 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096 if (evaluate)
4097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 rettv->v_type = VAR_NUMBER;
4099 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 }
4102
4103 return OK;
4104}
4105
4106/*
4107 * Handle second level expression:
4108 * expr3 && expr3 && expr3 logical AND
4109 *
4110 * "arg" must point to the first non-white of the expression.
4111 * "arg" is advanced to the next non-white after the recognized expression.
4112 *
4113 * Return OK or FAIL.
4114 */
4115 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 int evaluate;
4120{
Bram Moolenaar33570922005-01-25 22:26:29 +00004121 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 long result;
4123 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004124 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 /*
4127 * Get the first variable.
4128 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131
4132 /*
4133 * Repeat until there is no following "&&".
4134 */
4135 first = TRUE;
4136 result = TRUE;
4137 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4138 {
4139 if (evaluate && first)
4140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004141 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004144 if (error)
4145 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 first = FALSE;
4147 }
4148
4149 /*
4150 * Get the second variable.
4151 */
4152 *arg = skipwhite(*arg + 2);
4153 if (eval4(arg, &var2, evaluate && result) == FAIL)
4154 return FAIL;
4155
4156 /*
4157 * Compute the result.
4158 */
4159 if (evaluate && result)
4160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (error)
4165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167 if (evaluate)
4168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 rettv->v_type = VAR_NUMBER;
4170 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 }
4173
4174 return OK;
4175}
4176
4177/*
4178 * Handle third level expression:
4179 * var1 == var2
4180 * var1 =~ var2
4181 * var1 != var2
4182 * var1 !~ var2
4183 * var1 > var2
4184 * var1 >= var2
4185 * var1 < var2
4186 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004187 * var1 is var2
4188 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 *
4190 * "arg" must point to the first non-white of the expression.
4191 * "arg" is advanced to the next non-white after the recognized expression.
4192 *
4193 * Return OK or FAIL.
4194 */
4195 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 int evaluate;
4200{
Bram Moolenaar33570922005-01-25 22:26:29 +00004201 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 char_u *p;
4203 int i;
4204 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004205 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 int len = 2;
4207 long n1, n2;
4208 char_u *s1, *s2;
4209 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4210 regmatch_T regmatch;
4211 int ic;
4212 char_u *save_cpo;
4213
4214 /*
4215 * Get the first variable.
4216 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 return FAIL;
4219
4220 p = *arg;
4221 switch (p[0])
4222 {
4223 case '=': if (p[1] == '=')
4224 type = TYPE_EQUAL;
4225 else if (p[1] == '~')
4226 type = TYPE_MATCH;
4227 break;
4228 case '!': if (p[1] == '=')
4229 type = TYPE_NEQUAL;
4230 else if (p[1] == '~')
4231 type = TYPE_NOMATCH;
4232 break;
4233 case '>': if (p[1] != '=')
4234 {
4235 type = TYPE_GREATER;
4236 len = 1;
4237 }
4238 else
4239 type = TYPE_GEQUAL;
4240 break;
4241 case '<': if (p[1] != '=')
4242 {
4243 type = TYPE_SMALLER;
4244 len = 1;
4245 }
4246 else
4247 type = TYPE_SEQUAL;
4248 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004249 case 'i': if (p[1] == 's')
4250 {
4251 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4252 len = 5;
4253 if (!vim_isIDc(p[len]))
4254 {
4255 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4256 type_is = TRUE;
4257 }
4258 }
4259 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261
4262 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004263 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 */
4265 if (type != TYPE_UNKNOWN)
4266 {
4267 /* extra question mark appended: ignore case */
4268 if (p[len] == '?')
4269 {
4270 ic = TRUE;
4271 ++len;
4272 }
4273 /* extra '#' appended: match case */
4274 else if (p[len] == '#')
4275 {
4276 ic = FALSE;
4277 ++len;
4278 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004279 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 else
4281 ic = p_ic;
4282
4283 /*
4284 * Get the second variable.
4285 */
4286 *arg = skipwhite(p + len);
4287 if (eval5(arg, &var2, evaluate) == FAIL)
4288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 return FAIL;
4291 }
4292
4293 if (evaluate)
4294 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004295 if (type_is && rettv->v_type != var2.v_type)
4296 {
4297 /* For "is" a different type always means FALSE, for "notis"
4298 * it means TRUE. */
4299 n1 = (type == TYPE_NEQUAL);
4300 }
4301 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4302 {
4303 if (type_is)
4304 {
4305 n1 = (rettv->v_type == var2.v_type
4306 && rettv->vval.v_list == var2.vval.v_list);
4307 if (type == TYPE_NEQUAL)
4308 n1 = !n1;
4309 }
4310 else if (rettv->v_type != var2.v_type
4311 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4312 {
4313 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004314 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004315 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004316 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004317 clear_tv(rettv);
4318 clear_tv(&var2);
4319 return FAIL;
4320 }
4321 else
4322 {
4323 /* Compare two Lists for being equal or unequal. */
4324 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4325 if (type == TYPE_NEQUAL)
4326 n1 = !n1;
4327 }
4328 }
4329
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004330 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4331 {
4332 if (type_is)
4333 {
4334 n1 = (rettv->v_type == var2.v_type
4335 && rettv->vval.v_dict == var2.vval.v_dict);
4336 if (type == TYPE_NEQUAL)
4337 n1 = !n1;
4338 }
4339 else if (rettv->v_type != var2.v_type
4340 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4341 {
4342 if (rettv->v_type != var2.v_type)
4343 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4344 else
4345 EMSG(_("E736: Invalid operation for Dictionary"));
4346 clear_tv(rettv);
4347 clear_tv(&var2);
4348 return FAIL;
4349 }
4350 else
4351 {
4352 /* Compare two Dictionaries for being equal or unequal. */
4353 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4354 if (type == TYPE_NEQUAL)
4355 n1 = !n1;
4356 }
4357 }
4358
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004359 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4360 {
4361 if (rettv->v_type != var2.v_type
4362 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4363 {
4364 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004365 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004366 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004367 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004368 clear_tv(rettv);
4369 clear_tv(&var2);
4370 return FAIL;
4371 }
4372 else
4373 {
4374 /* Compare two Funcrefs for being equal or unequal. */
4375 if (rettv->vval.v_string == NULL
4376 || var2.vval.v_string == NULL)
4377 n1 = FALSE;
4378 else
4379 n1 = STRCMP(rettv->vval.v_string,
4380 var2.vval.v_string) == 0;
4381 if (type == TYPE_NEQUAL)
4382 n1 = !n1;
4383 }
4384 }
4385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004386#ifdef FEAT_FLOAT
4387 /*
4388 * If one of the two variables is a float, compare as a float.
4389 * When using "=~" or "!~", always compare as string.
4390 */
4391 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4392 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4393 {
4394 float_T f1, f2;
4395
4396 if (rettv->v_type == VAR_FLOAT)
4397 f1 = rettv->vval.v_float;
4398 else
4399 f1 = get_tv_number(rettv);
4400 if (var2.v_type == VAR_FLOAT)
4401 f2 = var2.vval.v_float;
4402 else
4403 f2 = get_tv_number(&var2);
4404 n1 = FALSE;
4405 switch (type)
4406 {
4407 case TYPE_EQUAL: n1 = (f1 == f2); break;
4408 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4409 case TYPE_GREATER: n1 = (f1 > f2); break;
4410 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4411 case TYPE_SMALLER: n1 = (f1 < f2); break;
4412 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4413 case TYPE_UNKNOWN:
4414 case TYPE_MATCH:
4415 case TYPE_NOMATCH: break; /* avoid gcc warning */
4416 }
4417 }
4418#endif
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 /*
4421 * If one of the two variables is a number, compare as a number.
4422 * When using "=~" or "!~", always compare as string.
4423 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004427 n1 = get_tv_number(rettv);
4428 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 switch (type)
4430 {
4431 case TYPE_EQUAL: n1 = (n1 == n2); break;
4432 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4433 case TYPE_GREATER: n1 = (n1 > n2); break;
4434 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4435 case TYPE_SMALLER: n1 = (n1 < n2); break;
4436 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4437 case TYPE_UNKNOWN:
4438 case TYPE_MATCH:
4439 case TYPE_NOMATCH: break; /* avoid gcc warning */
4440 }
4441 }
4442 else
4443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444 s1 = get_tv_string_buf(rettv, buf1);
4445 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4447 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4448 else
4449 i = 0;
4450 n1 = FALSE;
4451 switch (type)
4452 {
4453 case TYPE_EQUAL: n1 = (i == 0); break;
4454 case TYPE_NEQUAL: n1 = (i != 0); break;
4455 case TYPE_GREATER: n1 = (i > 0); break;
4456 case TYPE_GEQUAL: n1 = (i >= 0); break;
4457 case TYPE_SMALLER: n1 = (i < 0); break;
4458 case TYPE_SEQUAL: n1 = (i <= 0); break;
4459
4460 case TYPE_MATCH:
4461 case TYPE_NOMATCH:
4462 /* avoid 'l' flag in 'cpoptions' */
4463 save_cpo = p_cpo;
4464 p_cpo = (char_u *)"";
4465 regmatch.regprog = vim_regcomp(s2,
4466 RE_MAGIC + RE_STRING);
4467 regmatch.rm_ic = ic;
4468 if (regmatch.regprog != NULL)
4469 {
4470 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4471 vim_free(regmatch.regprog);
4472 if (type == TYPE_NOMATCH)
4473 n1 = !n1;
4474 }
4475 p_cpo = save_cpo;
4476 break;
4477
4478 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4479 }
4480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 clear_tv(rettv);
4482 clear_tv(&var2);
4483 rettv->v_type = VAR_NUMBER;
4484 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486 }
4487
4488 return OK;
4489}
4490
4491/*
4492 * Handle fourth level expression:
4493 * + number addition
4494 * - number subtraction
4495 * . string concatenation
4496 *
4497 * "arg" must point to the first non-white of the expression.
4498 * "arg" is advanced to the next non-white after the recognized expression.
4499 *
4500 * Return OK or FAIL.
4501 */
4502 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004503eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 int evaluate;
4507{
Bram Moolenaar33570922005-01-25 22:26:29 +00004508 typval_T var2;
4509 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 int op;
4511 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512#ifdef FEAT_FLOAT
4513 float_T f1 = 0, f2 = 0;
4514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 char_u *s1, *s2;
4516 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4517 char_u *p;
4518
4519 /*
4520 * Get the first variable.
4521 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004522 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 return FAIL;
4524
4525 /*
4526 * Repeat computing, until no '+', '-' or '.' is following.
4527 */
4528 for (;;)
4529 {
4530 op = **arg;
4531 if (op != '+' && op != '-' && op != '.')
4532 break;
4533
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004534 if ((op != '+' || rettv->v_type != VAR_LIST)
4535#ifdef FEAT_FLOAT
4536 && (op == '.' || rettv->v_type != VAR_FLOAT)
4537#endif
4538 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004539 {
4540 /* For "list + ...", an illegal use of the first operand as
4541 * a number cannot be determined before evaluating the 2nd
4542 * operand: if this is also a list, all is ok.
4543 * For "something . ...", "something - ..." or "non-list + ...",
4544 * we know that the first operand needs to be a string or number
4545 * without evaluating the 2nd operand. So check before to avoid
4546 * side effects after an error. */
4547 if (evaluate && get_tv_string_chk(rettv) == NULL)
4548 {
4549 clear_tv(rettv);
4550 return FAIL;
4551 }
4552 }
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 /*
4555 * Get the second variable.
4556 */
4557 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004558 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004560 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 return FAIL;
4562 }
4563
4564 if (evaluate)
4565 {
4566 /*
4567 * Compute the result.
4568 */
4569 if (op == '.')
4570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004571 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4572 s2 = get_tv_string_buf_chk(&var2, buf2);
4573 if (s2 == NULL) /* type error ? */
4574 {
4575 clear_tv(rettv);
4576 clear_tv(&var2);
4577 return FAIL;
4578 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004579 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 clear_tv(rettv);
4581 rettv->v_type = VAR_STRING;
4582 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004584 else if (op == '+' && rettv->v_type == VAR_LIST
4585 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004586 {
4587 /* concatenate Lists */
4588 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4589 &var3) == FAIL)
4590 {
4591 clear_tv(rettv);
4592 clear_tv(&var2);
4593 return FAIL;
4594 }
4595 clear_tv(rettv);
4596 *rettv = var3;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
4599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004600 int error = FALSE;
4601
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004602#ifdef FEAT_FLOAT
4603 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004604 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004605 f1 = rettv->vval.v_float;
4606 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004607 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004608 else
4609#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004610 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611 n1 = get_tv_number_chk(rettv, &error);
4612 if (error)
4613 {
4614 /* This can only happen for "list + non-list". For
4615 * "non-list + ..." or "something - ...", we returned
4616 * before evaluating the 2nd operand. */
4617 clear_tv(rettv);
4618 return FAIL;
4619 }
4620#ifdef FEAT_FLOAT
4621 if (var2.v_type == VAR_FLOAT)
4622 f1 = n1;
4623#endif
4624 }
4625#ifdef FEAT_FLOAT
4626 if (var2.v_type == VAR_FLOAT)
4627 {
4628 f2 = var2.vval.v_float;
4629 n2 = 0;
4630 }
4631 else
4632#endif
4633 {
4634 n2 = get_tv_number_chk(&var2, &error);
4635 if (error)
4636 {
4637 clear_tv(rettv);
4638 clear_tv(&var2);
4639 return FAIL;
4640 }
4641#ifdef FEAT_FLOAT
4642 if (rettv->v_type == VAR_FLOAT)
4643 f2 = n2;
4644#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004646 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004647
4648#ifdef FEAT_FLOAT
4649 /* If there is a float on either side the result is a float. */
4650 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4651 {
4652 if (op == '+')
4653 f1 = f1 + f2;
4654 else
4655 f1 = f1 - f2;
4656 rettv->v_type = VAR_FLOAT;
4657 rettv->vval.v_float = f1;
4658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004660#endif
4661 {
4662 if (op == '+')
4663 n1 = n1 + n2;
4664 else
4665 n1 = n1 - n2;
4666 rettv->v_type = VAR_NUMBER;
4667 rettv->vval.v_number = n1;
4668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672 }
4673 return OK;
4674}
4675
4676/*
4677 * Handle fifth level expression:
4678 * * number multiplication
4679 * / number division
4680 * % number modulo
4681 *
4682 * "arg" must point to the first non-white of the expression.
4683 * "arg" is advanced to the next non-white after the recognized expression.
4684 *
4685 * Return OK or FAIL.
4686 */
4687 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004688eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004692 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
Bram Moolenaar33570922005-01-25 22:26:29 +00004694 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 int op;
4696 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697#ifdef FEAT_FLOAT
4698 int use_float = FALSE;
4699 float_T f1 = 0, f2;
4700#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '*', '/' or '%' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '*' && op != '/' && op != '%')
4716 break;
4717
4718 if (evaluate)
4719 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720#ifdef FEAT_FLOAT
4721 if (rettv->v_type == VAR_FLOAT)
4722 {
4723 f1 = rettv->vval.v_float;
4724 use_float = TRUE;
4725 n1 = 0;
4726 }
4727 else
4728#endif
4729 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004731 if (error)
4732 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734 else
4735 n1 = 0;
4736
4737 /*
4738 * Get the second variable.
4739 */
4740 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004741 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return FAIL;
4743
4744 if (evaluate)
4745 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 if (var2.v_type == VAR_FLOAT)
4748 {
4749 if (!use_float)
4750 {
4751 f1 = n1;
4752 use_float = TRUE;
4753 }
4754 f2 = var2.vval.v_float;
4755 n2 = 0;
4756 }
4757 else
4758#endif
4759 {
4760 n2 = get_tv_number_chk(&var2, &error);
4761 clear_tv(&var2);
4762 if (error)
4763 return FAIL;
4764#ifdef FEAT_FLOAT
4765 if (use_float)
4766 f2 = n2;
4767#endif
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 /*
4771 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774#ifdef FEAT_FLOAT
4775 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777 if (op == '*')
4778 f1 = f1 * f2;
4779 else if (op == '/')
4780 {
4781 /* We rely on the floating point library to handle divide
4782 * by zero to result in "inf" and not a crash. */
4783 f1 = f1 / f2;
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004787 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 return FAIL;
4789 }
4790 rettv->v_type = VAR_FLOAT;
4791 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
4793 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 if (op == '*')
4797 n1 = n1 * n2;
4798 else if (op == '/')
4799 {
4800 if (n2 == 0) /* give an error message? */
4801 {
4802 if (n1 == 0)
4803 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4804 else if (n1 < 0)
4805 n1 = -0x7fffffffL;
4806 else
4807 n1 = 0x7fffffffL;
4808 }
4809 else
4810 n1 = n1 / n2;
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004813 {
4814 if (n2 == 0) /* give an error message? */
4815 n1 = 0;
4816 else
4817 n1 = n1 % n2;
4818 }
4819 rettv->v_type = VAR_NUMBER;
4820 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
4823 }
4824
4825 return OK;
4826}
4827
4828/*
4829 * Handle sixth level expression:
4830 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004831 * "string" string constant
4832 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 * &option-name option value
4834 * @r register contents
4835 * identifier variable value
4836 * function() function call
4837 * $VAR environment variable
4838 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004839 * [expr, expr] List
4840 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 *
4842 * Also handle:
4843 * ! in front logical NOT
4844 * - in front unary minus
4845 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004846 * trailing [] subscript in String or List
4847 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 *
4849 * "arg" must point to the first non-white of the expression.
4850 * "arg" is advanced to the next non-white after the recognized expression.
4851 *
4852 * Return OK or FAIL.
4853 */
4854 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004855eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004859 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 long n;
4862 int len;
4863 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 char_u *start_leader, *end_leader;
4865 int ret = OK;
4866 char_u *alias;
4867
4868 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004870 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004872 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 /*
4875 * Skip '!' and '-' characters. They are handled later.
4876 */
4877 start_leader = *arg;
4878 while (**arg == '!' || **arg == '-' || **arg == '+')
4879 *arg = skipwhite(*arg + 1);
4880 end_leader = *arg;
4881
4882 switch (**arg)
4883 {
4884 /*
4885 * Number constant.
4886 */
4887 case '0':
4888 case '1':
4889 case '2':
4890 case '3':
4891 case '4':
4892 case '5':
4893 case '6':
4894 case '7':
4895 case '8':
4896 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 {
4898#ifdef FEAT_FLOAT
4899 char_u *p = skipdigits(*arg + 1);
4900 int get_float = FALSE;
4901
4902 /* We accept a float when the format matches
4903 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004904 * strict to avoid backwards compatibility problems.
4905 * Don't look for a float after the "." operator, so that
4906 * ":let vers = 1.2.3" doesn't fail. */
4907 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 get_float = TRUE;
4910 p = skipdigits(p + 2);
4911 if (*p == 'e' || *p == 'E')
4912 {
4913 ++p;
4914 if (*p == '-' || *p == '+')
4915 ++p;
4916 if (!vim_isdigit(*p))
4917 get_float = FALSE;
4918 else
4919 p = skipdigits(p + 1);
4920 }
4921 if (ASCII_ISALPHA(*p) || *p == '.')
4922 get_float = FALSE;
4923 }
4924 if (get_float)
4925 {
4926 float_T f;
4927
4928 *arg += string2float(*arg, &f);
4929 if (evaluate)
4930 {
4931 rettv->v_type = VAR_FLOAT;
4932 rettv->vval.v_float = f;
4933 }
4934 }
4935 else
4936#endif
4937 {
4938 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4939 *arg += len;
4940 if (evaluate)
4941 {
4942 rettv->v_type = VAR_NUMBER;
4943 rettv->vval.v_number = n;
4944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 /*
4950 * String constant: "string".
4951 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004952 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 break;
4954
4955 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004958 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 break;
4960
4961 /*
4962 * List: [expr, expr]
4963 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 break;
4966
4967 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 * Dictionary: {key: val, key: val}
4969 */
4970 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4971 break;
4972
4973 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004974 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004976 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 break;
4978
4979 /*
4980 * Environment variable: $VAR.
4981 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 break;
4984
4985 /*
4986 * Register contents: @r.
4987 */
4988 case '@': ++*arg;
4989 if (evaluate)
4990 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004991 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004992 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994 if (**arg != NUL)
4995 ++*arg;
4996 break;
4997
4998 /*
4999 * nested expression: (expression).
5000 */
5001 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (**arg == ')')
5004 ++*arg;
5005 else if (ret == OK)
5006 {
5007 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005008 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 ret = FAIL;
5010 }
5011 break;
5012
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 break;
5015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016
5017 if (ret == NOTDONE)
5018 {
5019 /*
5020 * Must be a variable or function name.
5021 * Can also be a curly-braces kind of name: {expr}.
5022 */
5023 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005024 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 if (alias != NULL)
5026 s = alias;
5027
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005028 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 ret = FAIL;
5030 else
5031 {
5032 if (**arg == '(') /* recursive! */
5033 {
5034 /* If "s" is the name of a variable of type VAR_FUNC
5035 * use its contents. */
5036 s = deref_func_name(s, &len);
5037
5038 /* Invoke the function. */
5039 ret = get_func_tv(s, len, rettv, arg,
5040 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005041 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 /* Stop the expression evaluation when immediately
5043 * aborting on error, or when an interrupt occurred or
5044 * an exception was thrown but not caught. */
5045 if (aborting())
5046 {
5047 if (ret == OK)
5048 clear_tv(rettv);
5049 ret = FAIL;
5050 }
5051 }
5052 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005053 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005054 else
5055 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 }
5057
5058 if (alias != NULL)
5059 vim_free(alias);
5060 }
5061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 *arg = skipwhite(*arg);
5063
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005064 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5065 * expr(expr). */
5066 if (ret == OK)
5067 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069 /*
5070 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5071 */
5072 if (ret == OK && evaluate && end_leader > start_leader)
5073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005074 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005075 int val = 0;
5076#ifdef FEAT_FLOAT
5077 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005078
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 if (rettv->v_type == VAR_FLOAT)
5080 f = rettv->vval.v_float;
5081 else
5082#endif
5083 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005084 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005086 clear_tv(rettv);
5087 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005089 else
5090 {
5091 while (end_leader > start_leader)
5092 {
5093 --end_leader;
5094 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095 {
5096#ifdef FEAT_FLOAT
5097 if (rettv->v_type == VAR_FLOAT)
5098 f = !f;
5099 else
5100#endif
5101 val = !val;
5102 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005103 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005104 {
5105#ifdef FEAT_FLOAT
5106 if (rettv->v_type == VAR_FLOAT)
5107 f = -f;
5108 else
5109#endif
5110 val = -val;
5111 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005112 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005113#ifdef FEAT_FLOAT
5114 if (rettv->v_type == VAR_FLOAT)
5115 {
5116 clear_tv(rettv);
5117 rettv->vval.v_float = f;
5118 }
5119 else
5120#endif
5121 {
5122 clear_tv(rettv);
5123 rettv->v_type = VAR_NUMBER;
5124 rettv->vval.v_number = val;
5125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128
5129 return ret;
5130}
5131
5132/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005133 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5134 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5136 */
5137 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005138eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005140 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005142 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143{
5144 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005145 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 long len = -1;
5148 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005152 if (rettv->v_type == VAR_FUNC
5153#ifdef FEAT_FLOAT
5154 || rettv->v_type == VAR_FLOAT
5155#endif
5156 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005158 if (verbose)
5159 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 return FAIL;
5161 }
5162
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 /*
5166 * dict.name
5167 */
5168 key = *arg + 1;
5169 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5170 ;
5171 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 }
5175 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 /*
5178 * something[idx]
5179 *
5180 * Get the (first) variable from inside the [].
5181 */
5182 *arg = skipwhite(*arg + 1);
5183 if (**arg == ':')
5184 empty1 = TRUE;
5185 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5186 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005187 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5188 {
5189 /* not a number or string */
5190 clear_tv(&var1);
5191 return FAIL;
5192 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193
5194 /*
5195 * Get the second variable from inside the [:].
5196 */
5197 if (**arg == ':')
5198 {
5199 range = TRUE;
5200 *arg = skipwhite(*arg + 1);
5201 if (**arg == ']')
5202 empty2 = TRUE;
5203 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205 if (!empty1)
5206 clear_tv(&var1);
5207 return FAIL;
5208 }
5209 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5210 {
5211 /* not a number or string */
5212 if (!empty1)
5213 clear_tv(&var1);
5214 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 return FAIL;
5216 }
5217 }
5218
5219 /* Check for the ']'. */
5220 if (**arg != ']')
5221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005222 if (verbose)
5223 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 clear_tv(&var1);
5225 if (range)
5226 clear_tv(&var2);
5227 return FAIL;
5228 }
5229 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 }
5231
5232 if (evaluate)
5233 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 n1 = 0;
5235 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237 n1 = get_tv_number(&var1);
5238 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 }
5240 if (range)
5241 {
5242 if (empty2)
5243 n2 = -1;
5244 else
5245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005246 n2 = get_tv_number(&var2);
5247 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 }
5249 }
5250
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005251 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 {
5253 case VAR_NUMBER:
5254 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005255 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 len = (long)STRLEN(s);
5257 if (range)
5258 {
5259 /* The resulting variable is a substring. If the indexes
5260 * are out of range the result is empty. */
5261 if (n1 < 0)
5262 {
5263 n1 = len + n1;
5264 if (n1 < 0)
5265 n1 = 0;
5266 }
5267 if (n2 < 0)
5268 n2 = len + n2;
5269 else if (n2 >= len)
5270 n2 = len;
5271 if (n1 >= len || n2 < 0 || n1 > n2)
5272 s = NULL;
5273 else
5274 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5275 }
5276 else
5277 {
5278 /* The resulting variable is a string of a single
5279 * character. If the index is too big or negative the
5280 * result is empty. */
5281 if (n1 >= len || n1 < 0)
5282 s = NULL;
5283 else
5284 s = vim_strnsave(s + n1, 1);
5285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005286 clear_tv(rettv);
5287 rettv->v_type = VAR_STRING;
5288 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 break;
5290
5291 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005292 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 if (n1 < 0)
5294 n1 = len + n1;
5295 if (!empty1 && (n1 < 0 || n1 >= len))
5296 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005297 /* For a range we allow invalid values and return an empty
5298 * list. A list index out of range is an error. */
5299 if (!range)
5300 {
5301 if (verbose)
5302 EMSGN(_(e_listidx), n1);
5303 return FAIL;
5304 }
5305 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 }
5307 if (range)
5308 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 list_T *l;
5310 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311
5312 if (n2 < 0)
5313 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005314 else if (n2 >= len)
5315 n2 = len - 1;
5316 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005317 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 l = list_alloc();
5319 if (l == NULL)
5320 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005321 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 n1 <= n2; ++n1)
5323 {
5324 if (list_append_tv(l, &item->li_tv) == FAIL)
5325 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005326 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 return FAIL;
5328 }
5329 item = item->li_next;
5330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005331 clear_tv(rettv);
5332 rettv->v_type = VAR_LIST;
5333 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005334 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 }
5336 else
5337 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005338 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005339 clear_tv(rettv);
5340 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 }
5342 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343
5344 case VAR_DICT:
5345 if (range)
5346 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005347 if (verbose)
5348 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 if (len == -1)
5350 clear_tv(&var1);
5351 return FAIL;
5352 }
5353 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355
5356 if (len == -1)
5357 {
5358 key = get_tv_string(&var1);
5359 if (*key == NUL)
5360 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005361 if (verbose)
5362 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 clear_tv(&var1);
5364 return FAIL;
5365 }
5366 }
5367
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005368 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005371 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 if (len == -1)
5373 clear_tv(&var1);
5374 if (item == NULL)
5375 return FAIL;
5376
5377 copy_tv(&item->di_tv, &var1);
5378 clear_tv(rettv);
5379 *rettv = var1;
5380 }
5381 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 }
5383 }
5384
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 return OK;
5386}
5387
5388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 * Get an option value.
5390 * "arg" points to the '&' or '+' before the option name.
5391 * "arg" is advanced to character after the option name.
5392 * Return OK or FAIL.
5393 */
5394 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005395get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 int evaluate;
5399{
5400 char_u *option_end;
5401 long numval;
5402 char_u *stringval;
5403 int opt_type;
5404 int c;
5405 int working = (**arg == '+'); /* has("+option") */
5406 int ret = OK;
5407 int opt_flags;
5408
5409 /*
5410 * Isolate the option name and find its value.
5411 */
5412 option_end = find_option_end(arg, &opt_flags);
5413 if (option_end == NULL)
5414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 EMSG2(_("E112: Option name missing: %s"), *arg);
5417 return FAIL;
5418 }
5419
5420 if (!evaluate)
5421 {
5422 *arg = option_end;
5423 return OK;
5424 }
5425
5426 c = *option_end;
5427 *option_end = NUL;
5428 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
5431 if (opt_type == -3) /* invalid name */
5432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 EMSG2(_("E113: Unknown option: %s"), *arg);
5435 ret = FAIL;
5436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {
5439 if (opt_type == -2) /* hidden string option */
5440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441 rettv->v_type = VAR_STRING;
5442 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444 else if (opt_type == -1) /* hidden number option */
5445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 rettv->v_type = VAR_NUMBER;
5447 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
5449 else if (opt_type == 1) /* number option */
5450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005451 rettv->v_type = VAR_NUMBER;
5452 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
5454 else /* string option */
5455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 rettv->v_type = VAR_STRING;
5457 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 }
5460 else if (working && (opt_type == -2 || opt_type == -1))
5461 ret = FAIL;
5462
5463 *option_end = c; /* put back for error messages */
5464 *arg = option_end;
5465
5466 return ret;
5467}
5468
5469/*
5470 * Allocate a variable for a string constant.
5471 * Return OK or FAIL.
5472 */
5473 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 int evaluate;
5478{
5479 char_u *p;
5480 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 int extra = 0;
5482
5483 /*
5484 * Find the end of the string, skipping backslashed characters.
5485 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005486 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
5488 if (*p == '\\' && p[1] != NUL)
5489 {
5490 ++p;
5491 /* A "\<x>" form occupies at least 4 characters, and produces up
5492 * to 6 characters: reserve space for 2 extra */
5493 if (*p == '<')
5494 extra += 2;
5495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497
5498 if (*p != '"')
5499 {
5500 EMSG2(_("E114: Missing quote: %s"), *arg);
5501 return FAIL;
5502 }
5503
5504 /* If only parsing, set *arg and return here */
5505 if (!evaluate)
5506 {
5507 *arg = p + 1;
5508 return OK;
5509 }
5510
5511 /*
5512 * Copy the string into allocated memory, handling backslashed
5513 * characters.
5514 */
5515 name = alloc((unsigned)(p - *arg + extra));
5516 if (name == NULL)
5517 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 rettv->v_type = VAR_STRING;
5519 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Bram Moolenaar8c711452005-01-14 21:53:12 +00005521 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
5523 if (*p == '\\')
5524 {
5525 switch (*++p)
5526 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 case 'b': *name++ = BS; ++p; break;
5528 case 'e': *name++ = ESC; ++p; break;
5529 case 'f': *name++ = FF; ++p; break;
5530 case 'n': *name++ = NL; ++p; break;
5531 case 'r': *name++ = CAR; ++p; break;
5532 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
5534 case 'X': /* hex: "\x1", "\x12" */
5535 case 'x':
5536 case 'u': /* Unicode: "\u0023" */
5537 case 'U':
5538 if (vim_isxdigit(p[1]))
5539 {
5540 int n, nr;
5541 int c = toupper(*p);
5542
5543 if (c == 'X')
5544 n = 2;
5545 else
5546 n = 4;
5547 nr = 0;
5548 while (--n >= 0 && vim_isxdigit(p[1]))
5549 {
5550 ++p;
5551 nr = (nr << 4) + hex2nr(*p);
5552 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554#ifdef FEAT_MBYTE
5555 /* For "\u" store the number according to
5556 * 'encoding'. */
5557 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005558 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 else
5560#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 break;
5564
5565 /* octal: "\1", "\12", "\123" */
5566 case '0':
5567 case '1':
5568 case '2':
5569 case '3':
5570 case '4':
5571 case '5':
5572 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 case '7': *name = *p++ - '0';
5574 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 *name = (*name << 3) + *p++ - '0';
5577 if (*p >= '0' && *p <= '7')
5578 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 break;
5582
5583 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 if (extra != 0)
5586 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 break;
5589 }
5590 /* FALLTHROUGH */
5591
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 break;
5594 }
5595 }
5596 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 *arg = p + 1;
5602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 return OK;
5604}
5605
5606/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * Return OK or FAIL.
5609 */
5610 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 int evaluate;
5615{
5616 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005617 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005618 int reduce = 0;
5619
5620 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005622 */
5623 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005628 break;
5629 ++reduce;
5630 ++p;
5631 }
5632 }
5633
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005637 return FAIL;
5638 }
5639
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 if (!evaluate)
5642 {
5643 *arg = p + 1;
5644 return OK;
5645 }
5646
5647 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005649 */
5650 str = alloc((unsigned)((p - *arg) - reduce));
5651 if (str == NULL)
5652 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 rettv->v_type = VAR_STRING;
5654 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005655
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 break;
5662 ++p;
5663 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005667 *arg = p + 1;
5668
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005669 return OK;
5670}
5671
5672/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673 * Allocate a variable for a List and fill it from "*arg".
5674 * Return OK or FAIL.
5675 */
5676 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005679 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 int evaluate;
5681{
Bram Moolenaar33570922005-01-25 22:26:29 +00005682 list_T *l = NULL;
5683 typval_T tv;
5684 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685
5686 if (evaluate)
5687 {
5688 l = list_alloc();
5689 if (l == NULL)
5690 return FAIL;
5691 }
5692
5693 *arg = skipwhite(*arg + 1);
5694 while (**arg != ']' && **arg != NUL)
5695 {
5696 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5697 goto failret;
5698 if (evaluate)
5699 {
5700 item = listitem_alloc();
5701 if (item != NULL)
5702 {
5703 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005704 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705 list_append(l, item);
5706 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005707 else
5708 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 }
5710
5711 if (**arg == ']')
5712 break;
5713 if (**arg != ',')
5714 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 goto failret;
5717 }
5718 *arg = skipwhite(*arg + 1);
5719 }
5720
5721 if (**arg != ']')
5722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724failret:
5725 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005726 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727 return FAIL;
5728 }
5729
5730 *arg = skipwhite(*arg + 1);
5731 if (evaluate)
5732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005733 rettv->v_type = VAR_LIST;
5734 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735 ++l->lv_refcount;
5736 }
5737
5738 return OK;
5739}
5740
5741/*
5742 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005743 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005745 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746list_alloc()
5747{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005748 list_T *l;
5749
5750 l = (list_T *)alloc_clear(sizeof(list_T));
5751 if (l != NULL)
5752 {
5753 /* Prepend the list to the list of lists for garbage collection. */
5754 if (first_list != NULL)
5755 first_list->lv_used_prev = l;
5756 l->lv_used_prev = NULL;
5757 l->lv_used_next = first_list;
5758 first_list = l;
5759 }
5760 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761}
5762
5763/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005764 * Allocate an empty list for a return value.
5765 * Returns OK or FAIL.
5766 */
5767 static int
5768rettv_list_alloc(rettv)
5769 typval_T *rettv;
5770{
5771 list_T *l = list_alloc();
5772
5773 if (l == NULL)
5774 return FAIL;
5775
5776 rettv->vval.v_list = l;
5777 rettv->v_type = VAR_LIST;
5778 ++l->lv_refcount;
5779 return OK;
5780}
5781
5782/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783 * Unreference a list: decrement the reference count and free it when it
5784 * becomes zero.
5785 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005786 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005790 if (l != NULL && --l->lv_refcount <= 0)
5791 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792}
5793
5794/*
5795 * Free a list, including all items it points to.
5796 * Ignores the reference count.
5797 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005798 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005799list_free(l, recurse)
5800 list_T *l;
5801 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802{
Bram Moolenaar33570922005-01-25 22:26:29 +00005803 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005805 /* Remove the list from the list of lists for garbage collection. */
5806 if (l->lv_used_prev == NULL)
5807 first_list = l->lv_used_next;
5808 else
5809 l->lv_used_prev->lv_used_next = l->lv_used_next;
5810 if (l->lv_used_next != NULL)
5811 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5812
Bram Moolenaard9fba312005-06-26 22:34:35 +00005813 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005815 /* Remove the item before deleting it. */
5816 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005817 if (recurse || (item->li_tv.v_type != VAR_LIST
5818 && item->li_tv.v_type != VAR_DICT))
5819 clear_tv(&item->li_tv);
5820 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 }
5822 vim_free(l);
5823}
5824
5825/*
5826 * Allocate a list item.
5827 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005828 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829listitem_alloc()
5830{
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832}
5833
5834/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005835 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 */
5837 static void
5838listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005839 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005841 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 vim_free(item);
5843}
5844
5845/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005846 * Remove a list item from a List and free it. Also clears the value.
5847 */
5848 static void
5849listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 list_T *l;
5851 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005852{
5853 list_remove(l, item, item);
5854 listitem_free(item);
5855}
5856
5857/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 * Get the number of items in a list.
5859 */
5860 static long
5861list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 if (l == NULL)
5865 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005866 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867}
5868
5869/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005870 * Return TRUE when two lists have exactly the same values.
5871 */
5872 static int
5873list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 list_T *l1;
5875 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005876 int ic; /* ignore case for strings */
5877{
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005880 if (l1 == NULL || l2 == NULL)
5881 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005882 if (l1 == l2)
5883 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005884 if (list_len(l1) != list_len(l2))
5885 return FALSE;
5886
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 for (item1 = l1->lv_first, item2 = l2->lv_first;
5888 item1 != NULL && item2 != NULL;
5889 item1 = item1->li_next, item2 = item2->li_next)
5890 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5891 return FALSE;
5892 return item1 == NULL && item2 == NULL;
5893}
5894
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01005895#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5896 || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005897/*
5898 * Return the dictitem that an entry in a hashtable points to.
5899 */
5900 dictitem_T *
5901dict_lookup(hi)
5902 hashitem_T *hi;
5903{
5904 return HI2DI(hi);
5905}
5906#endif
5907
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005908/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005909 * Return TRUE when two dictionaries have exactly the same key/values.
5910 */
5911 static int
5912dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 dict_T *d1;
5914 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005915 int ic; /* ignore case for strings */
5916{
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 hashitem_T *hi;
5918 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005919 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005920
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005921 if (d1 == NULL || d2 == NULL)
5922 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005923 if (d1 == d2)
5924 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005925 if (dict_len(d1) != dict_len(d2))
5926 return FALSE;
5927
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005928 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005930 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005931 if (!HASHITEM_EMPTY(hi))
5932 {
5933 item2 = dict_find(d2, hi->hi_key, -1);
5934 if (item2 == NULL)
5935 return FALSE;
5936 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5937 return FALSE;
5938 --todo;
5939 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005940 }
5941 return TRUE;
5942}
5943
5944/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005945 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005946 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005947 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005948 */
5949 static int
5950tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 typval_T *tv1;
5952 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005953 int ic; /* ignore case */
5954{
5955 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005956 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005957 static int recursive = 0; /* cach recursive loops */
5958 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005959
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005960 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005961 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005962 /* Catch lists and dicts that have an endless loop by limiting
5963 * recursiveness to 1000. We guess they are equal then. */
5964 if (recursive >= 1000)
5965 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005966
5967 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005969 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005970 ++recursive;
5971 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5972 --recursive;
5973 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005974
5975 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005976 ++recursive;
5977 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5978 --recursive;
5979 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005980
5981 case VAR_FUNC:
5982 return (tv1->vval.v_string != NULL
5983 && tv2->vval.v_string != NULL
5984 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5985
5986 case VAR_NUMBER:
5987 return tv1->vval.v_number == tv2->vval.v_number;
5988
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005989#ifdef FEAT_FLOAT
5990 case VAR_FLOAT:
5991 return tv1->vval.v_float == tv2->vval.v_float;
5992#endif
5993
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005994 case VAR_STRING:
5995 s1 = get_tv_string_buf(tv1, buf1);
5996 s2 = get_tv_string_buf(tv2, buf2);
5997 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005999
6000 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001 return TRUE;
6002}
6003
6004/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 * Locate item with index "n" in list "l" and return it.
6006 * A negative index is counted from the end; -1 is the last item.
6007 * Returns NULL when "n" is out of range.
6008 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 long n;
6013{
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 long idx;
6016
6017 if (l == NULL)
6018 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006019
6020 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006022 n = l->lv_len + n;
6023
6024 /* Check for index out of range. */
6025 if (n < 0 || n >= l->lv_len)
6026 return NULL;
6027
6028 /* When there is a cached index may start search from there. */
6029 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006031 if (n < l->lv_idx / 2)
6032 {
6033 /* closest to the start of the list */
6034 item = l->lv_first;
6035 idx = 0;
6036 }
6037 else if (n > (l->lv_idx + l->lv_len) / 2)
6038 {
6039 /* closest to the end of the list */
6040 item = l->lv_last;
6041 idx = l->lv_len - 1;
6042 }
6043 else
6044 {
6045 /* closest to the cached index */
6046 item = l->lv_idx_item;
6047 idx = l->lv_idx;
6048 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 }
6050 else
6051 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006052 if (n < l->lv_len / 2)
6053 {
6054 /* closest to the start of the list */
6055 item = l->lv_first;
6056 idx = 0;
6057 }
6058 else
6059 {
6060 /* closest to the end of the list */
6061 item = l->lv_last;
6062 idx = l->lv_len - 1;
6063 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006065
6066 while (n > idx)
6067 {
6068 /* search forward */
6069 item = item->li_next;
6070 ++idx;
6071 }
6072 while (n < idx)
6073 {
6074 /* search backward */
6075 item = item->li_prev;
6076 --idx;
6077 }
6078
6079 /* cache the used index */
6080 l->lv_idx = idx;
6081 l->lv_idx_item = item;
6082
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 return item;
6084}
6085
6086/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006087 * Get list item "l[idx]" as a number.
6088 */
6089 static long
6090list_find_nr(l, idx, errorp)
6091 list_T *l;
6092 long idx;
6093 int *errorp; /* set to TRUE when something wrong */
6094{
6095 listitem_T *li;
6096
6097 li = list_find(l, idx);
6098 if (li == NULL)
6099 {
6100 if (errorp != NULL)
6101 *errorp = TRUE;
6102 return -1L;
6103 }
6104 return get_tv_number_chk(&li->li_tv, errorp);
6105}
6106
6107/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006108 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6109 */
6110 char_u *
6111list_find_str(l, idx)
6112 list_T *l;
6113 long idx;
6114{
6115 listitem_T *li;
6116
6117 li = list_find(l, idx - 1);
6118 if (li == NULL)
6119 {
6120 EMSGN(_(e_listidx), idx);
6121 return NULL;
6122 }
6123 return get_tv_string(&li->li_tv);
6124}
6125
6126/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006127 * Locate "item" list "l" and return its index.
6128 * Returns -1 when "item" is not in the list.
6129 */
6130 static long
6131list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 list_T *l;
6133 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006134{
6135 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006137
6138 if (l == NULL)
6139 return -1;
6140 idx = 0;
6141 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6142 ++idx;
6143 if (li == NULL)
6144 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006145 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006146}
6147
6148/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 * Append item "item" to the end of list "l".
6150 */
6151 static void
6152list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 list_T *l;
6154 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155{
6156 if (l->lv_last == NULL)
6157 {
6158 /* empty list */
6159 l->lv_first = item;
6160 l->lv_last = item;
6161 item->li_prev = NULL;
6162 }
6163 else
6164 {
6165 l->lv_last->li_next = item;
6166 item->li_prev = l->lv_last;
6167 l->lv_last = item;
6168 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006169 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006170 item->li_next = NULL;
6171}
6172
6173/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006177 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 list_T *l;
6180 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006182 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183
Bram Moolenaar05159a02005-02-26 23:04:13 +00006184 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006186 copy_tv(tv, &li->li_tv);
6187 list_append(l, li);
6188 return OK;
6189}
6190
6191/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006192 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006193 * Return FAIL when out of memory.
6194 */
6195 int
6196list_append_dict(list, dict)
6197 list_T *list;
6198 dict_T *dict;
6199{
6200 listitem_T *li = listitem_alloc();
6201
6202 if (li == NULL)
6203 return FAIL;
6204 li->li_tv.v_type = VAR_DICT;
6205 li->li_tv.v_lock = 0;
6206 li->li_tv.vval.v_dict = dict;
6207 list_append(list, li);
6208 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006209 return OK;
6210}
6211
6212/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006213 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006214 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006215 * Returns FAIL when out of memory.
6216 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006217 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006218list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006219 list_T *l;
6220 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006221 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006222{
6223 listitem_T *li = listitem_alloc();
6224
6225 if (li == NULL)
6226 return FAIL;
6227 list_append(l, li);
6228 li->li_tv.v_type = VAR_STRING;
6229 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006230 if (str == NULL)
6231 li->li_tv.vval.v_string = NULL;
6232 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006233 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006234 return FAIL;
6235 return OK;
6236}
6237
6238/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006239 * Append "n" to list "l".
6240 * Returns FAIL when out of memory.
6241 */
6242 static int
6243list_append_number(l, n)
6244 list_T *l;
6245 varnumber_T n;
6246{
6247 listitem_T *li;
6248
6249 li = listitem_alloc();
6250 if (li == NULL)
6251 return FAIL;
6252 li->li_tv.v_type = VAR_NUMBER;
6253 li->li_tv.v_lock = 0;
6254 li->li_tv.vval.v_number = n;
6255 list_append(l, li);
6256 return OK;
6257}
6258
6259/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006261 * If "item" is NULL append at the end.
6262 * Return FAIL when out of memory.
6263 */
6264 static int
6265list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 list_T *l;
6267 typval_T *tv;
6268 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269{
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006271
6272 if (ni == NULL)
6273 return FAIL;
6274 copy_tv(tv, &ni->li_tv);
6275 if (item == NULL)
6276 /* Append new item at end of list. */
6277 list_append(l, ni);
6278 else
6279 {
6280 /* Insert new item before existing item. */
6281 ni->li_prev = item->li_prev;
6282 ni->li_next = item;
6283 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006285 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006286 ++l->lv_idx;
6287 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006288 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006289 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006290 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006291 l->lv_idx_item = NULL;
6292 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006293 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006294 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006295 }
6296 return OK;
6297}
6298
6299/*
6300 * Extend "l1" with "l2".
6301 * If "bef" is NULL append at the end, otherwise insert before this item.
6302 * Returns FAIL when out of memory.
6303 */
6304 static int
6305list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 list_T *l1;
6307 list_T *l2;
6308 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309{
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006311 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006312
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006313 /* We also quit the loop when we have inserted the original item count of
6314 * the list, avoid a hang when we extend a list with itself. */
6315 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006316 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6317 return FAIL;
6318 return OK;
6319}
6320
6321/*
6322 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6323 * Return FAIL when out of memory.
6324 */
6325 static int
6326list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 list_T *l1;
6328 list_T *l2;
6329 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006330{
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006333 if (l1 == NULL || l2 == NULL)
6334 return FAIL;
6335
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006337 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 if (l == NULL)
6339 return FAIL;
6340 tv->v_type = VAR_LIST;
6341 tv->vval.v_list = l;
6342
6343 /* append all items from the second list */
6344 return list_extend(l, l2, NULL);
6345}
6346
6347/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006348 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006349 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006350 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 * Returns NULL when out of memory.
6352 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006354list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006355 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006357 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358{
Bram Moolenaar33570922005-01-25 22:26:29 +00006359 list_T *copy;
6360 listitem_T *item;
6361 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362
6363 if (orig == NULL)
6364 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006365
6366 copy = list_alloc();
6367 if (copy != NULL)
6368 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006369 if (copyID != 0)
6370 {
6371 /* Do this before adding the items, because one of the items may
6372 * refer back to this list. */
6373 orig->lv_copyID = copyID;
6374 orig->lv_copylist = copy;
6375 }
6376 for (item = orig->lv_first; item != NULL && !got_int;
6377 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 {
6379 ni = listitem_alloc();
6380 if (ni == NULL)
6381 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006382 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006383 {
6384 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6385 {
6386 vim_free(ni);
6387 break;
6388 }
6389 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006391 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 list_append(copy, ni);
6393 }
6394 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006395 if (item != NULL)
6396 {
6397 list_unref(copy);
6398 copy = NULL;
6399 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 }
6401
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402 return copy;
6403}
6404
6405/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006407 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006410list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 list_T *l;
6412 listitem_T *item;
6413 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414{
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 /* notify watchers */
6418 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006420 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 list_fix_watch(l, ip);
6422 if (ip == item2)
6423 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425
6426 if (item2->li_next == NULL)
6427 l->lv_last = item->li_prev;
6428 else
6429 item2->li_next->li_prev = item->li_prev;
6430 if (item->li_prev == NULL)
6431 l->lv_first = item2->li_next;
6432 else
6433 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006434 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435}
6436
6437/*
6438 * Return an allocated string with the string representation of a list.
6439 * May return NULL.
6440 */
6441 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006442list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006444 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445{
6446 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447
6448 if (tv->vval.v_list == NULL)
6449 return NULL;
6450 ga_init2(&ga, (int)sizeof(char), 80);
6451 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006452 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006453 {
6454 vim_free(ga.ga_data);
6455 return NULL;
6456 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 ga_append(&ga, ']');
6458 ga_append(&ga, NUL);
6459 return (char_u *)ga.ga_data;
6460}
6461
6462/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006463 * Join list "l" into a string in "*gap", using separator "sep".
6464 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006465 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006466 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006468list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006469 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006470 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006471 char_u *sep;
6472 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006473 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006474{
6475 int first = TRUE;
6476 char_u *tofree;
6477 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006479 char_u *s;
6480
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006482 {
6483 if (first)
6484 first = FALSE;
6485 else
6486 ga_concat(gap, sep);
6487
6488 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006489 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006490 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006491 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006492 if (s != NULL)
6493 ga_concat(gap, s);
6494 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495 if (s == NULL)
6496 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006497 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006498 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006500}
6501
6502/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006503 * Garbage collection for lists and dictionaries.
6504 *
6505 * We use reference counts to be able to free most items right away when they
6506 * are no longer used. But for composite items it's possible that it becomes
6507 * unused while the reference count is > 0: When there is a recursive
6508 * reference. Example:
6509 * :let l = [1, 2, 3]
6510 * :let d = {9: l}
6511 * :let l[1] = d
6512 *
6513 * Since this is quite unusual we handle this with garbage collection: every
6514 * once in a while find out which lists and dicts are not referenced from any
6515 * variable.
6516 *
6517 * Here is a good reference text about garbage collection (refers to Python
6518 * but it applies to all reference-counting mechanisms):
6519 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006520 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006521
6522/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006523 * Do garbage collection for lists and dicts.
6524 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006525 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006526 int
6527garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006528{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006529 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006530 buf_T *buf;
6531 win_T *wp;
6532 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006533 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006534 int did_free;
6535 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006536#ifdef FEAT_WINDOWS
6537 tabpage_T *tp;
6538#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006539
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006540 /* Only do this once. */
6541 want_garbage_collect = FALSE;
6542 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006543 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006544
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006545 /* We advance by two because we add one for items referenced through
6546 * previous_funccal. */
6547 current_copyID += COPYID_INC;
6548 copyID = current_copyID;
6549
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006550 /*
6551 * 1. Go through all accessible variables and mark all lists and dicts
6552 * with copyID.
6553 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006554
6555 /* Don't free variables in the previous_funccal list unless they are only
6556 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006557 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006558 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6559 {
6560 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6561 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6562 }
6563
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006564 /* script-local variables */
6565 for (i = 1; i <= ga_scripts.ga_len; ++i)
6566 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6567
6568 /* buffer-local variables */
6569 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6570 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6571
6572 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006573 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006574 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6575
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006576#ifdef FEAT_WINDOWS
6577 /* tabpage-local variables */
6578 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6579 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6580#endif
6581
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006582 /* global variables */
6583 set_ref_in_ht(&globvarht, copyID);
6584
6585 /* function-local variables */
6586 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6587 {
6588 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6589 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6590 }
6591
Bram Moolenaard812df62008-11-09 12:46:09 +00006592 /* v: vars */
6593 set_ref_in_ht(&vimvarht, copyID);
6594
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006595 /*
6596 * 2. Free lists and dictionaries that are not referenced.
6597 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006598 did_free = free_unref_items(copyID);
6599
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006600 /*
6601 * 3. Check if any funccal can be freed now.
6602 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006603 for (pfc = &previous_funccal; *pfc != NULL; )
6604 {
6605 if (can_free_funccal(*pfc, copyID))
6606 {
6607 fc = *pfc;
6608 *pfc = fc->caller;
6609 free_funccal(fc, TRUE);
6610 did_free = TRUE;
6611 did_free_funccal = TRUE;
6612 }
6613 else
6614 pfc = &(*pfc)->caller;
6615 }
6616 if (did_free_funccal)
6617 /* When a funccal was freed some more items might be garbage
6618 * collected, so run again. */
6619 (void)garbage_collect();
6620
6621 return did_free;
6622}
6623
6624/*
6625 * Free lists and dictionaries that are no longer referenced.
6626 */
6627 static int
6628free_unref_items(copyID)
6629 int copyID;
6630{
6631 dict_T *dd;
6632 list_T *ll;
6633 int did_free = FALSE;
6634
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006635 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006636 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006637 */
6638 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006639 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006640 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006641 /* Free the Dictionary and ordinary items it contains, but don't
6642 * recurse into Lists and Dictionaries, they will be in the list
6643 * of dicts or list of lists. */
6644 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006645 did_free = TRUE;
6646
6647 /* restart, next dict may also have been freed */
6648 dd = first_dict;
6649 }
6650 else
6651 dd = dd->dv_used_next;
6652
6653 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006654 * Go through the list of lists and free items without the copyID.
6655 * But don't free a list that has a watcher (used in a for loop), these
6656 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006657 */
6658 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006659 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6660 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006661 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006662 /* Free the List and ordinary items it contains, but don't recurse
6663 * into Lists and Dictionaries, they will be in the list of dicts
6664 * or list of lists. */
6665 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006666 did_free = TRUE;
6667
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006668 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006669 ll = first_list;
6670 }
6671 else
6672 ll = ll->lv_used_next;
6673
6674 return did_free;
6675}
6676
6677/*
6678 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6679 */
6680 static void
6681set_ref_in_ht(ht, copyID)
6682 hashtab_T *ht;
6683 int copyID;
6684{
6685 int todo;
6686 hashitem_T *hi;
6687
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006688 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006689 for (hi = ht->ht_array; todo > 0; ++hi)
6690 if (!HASHITEM_EMPTY(hi))
6691 {
6692 --todo;
6693 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6694 }
6695}
6696
6697/*
6698 * Mark all lists and dicts referenced through list "l" with "copyID".
6699 */
6700 static void
6701set_ref_in_list(l, copyID)
6702 list_T *l;
6703 int copyID;
6704{
6705 listitem_T *li;
6706
6707 for (li = l->lv_first; li != NULL; li = li->li_next)
6708 set_ref_in_item(&li->li_tv, copyID);
6709}
6710
6711/*
6712 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6713 */
6714 static void
6715set_ref_in_item(tv, copyID)
6716 typval_T *tv;
6717 int copyID;
6718{
6719 dict_T *dd;
6720 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006721
6722 switch (tv->v_type)
6723 {
6724 case VAR_DICT:
6725 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006726 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006727 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728 /* Didn't see this dict yet. */
6729 dd->dv_copyID = copyID;
6730 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733
6734 case VAR_LIST:
6735 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006736 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006737 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006738 /* Didn't see this list yet. */
6739 ll->lv_copyID = copyID;
6740 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006742 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006743 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006745}
6746
6747/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748 * Allocate an empty header for a dictionary.
6749 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006750 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751dict_alloc()
6752{
Bram Moolenaar33570922005-01-25 22:26:29 +00006753 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006754
Bram Moolenaar33570922005-01-25 22:26:29 +00006755 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006756 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006757 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006758 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759 if (first_dict != NULL)
6760 first_dict->dv_used_prev = d;
6761 d->dv_used_next = first_dict;
6762 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006763 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764
Bram Moolenaar33570922005-01-25 22:26:29 +00006765 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006766 d->dv_lock = 0;
6767 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006769 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006770 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006771}
6772
6773/*
6774 * Unreference a Dictionary: decrement the reference count and free it when it
6775 * becomes zero.
6776 */
6777 static void
6778dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006780{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006781 if (d != NULL && --d->dv_refcount <= 0)
6782 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006783}
6784
6785/*
6786 * Free a Dictionary, including all items it contains.
6787 * Ignores the reference count.
6788 */
6789 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006790dict_free(d, recurse)
6791 dict_T *d;
6792 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006793{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006794 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006795 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006796 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006797
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006798 /* Remove the dict from the list of dicts for garbage collection. */
6799 if (d->dv_used_prev == NULL)
6800 first_dict = d->dv_used_next;
6801 else
6802 d->dv_used_prev->dv_used_next = d->dv_used_next;
6803 if (d->dv_used_next != NULL)
6804 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6805
6806 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006807 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006808 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006810 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006811 if (!HASHITEM_EMPTY(hi))
6812 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006813 /* Remove the item before deleting it, just in case there is
6814 * something recursive causing trouble. */
6815 di = HI2DI(hi);
6816 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006817 if (recurse || (di->di_tv.v_type != VAR_LIST
6818 && di->di_tv.v_type != VAR_DICT))
6819 clear_tv(&di->di_tv);
6820 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006821 --todo;
6822 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006823 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006825 vim_free(d);
6826}
6827
6828/*
6829 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006830 * The "key" is copied to the new item.
6831 * Note that the value of the item "di_tv" still needs to be initialized!
6832 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006833 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006834 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006835dictitem_alloc(key)
6836 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006837{
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006839
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006840 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006841 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006842 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006843 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006844 di->di_flags = 0;
6845 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006846 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006847}
6848
6849/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006850 * Make a copy of a Dictionary item.
6851 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006852 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006853dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006854 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006855{
Bram Moolenaar33570922005-01-25 22:26:29 +00006856 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006857
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006858 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6859 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006860 if (di != NULL)
6861 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006862 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006863 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006864 copy_tv(&org->di_tv, &di->di_tv);
6865 }
6866 return di;
6867}
6868
6869/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006870 * Remove item "item" from Dictionary "dict" and free it.
6871 */
6872 static void
6873dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006874 dict_T *dict;
6875 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006876{
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006880 if (HASHITEM_EMPTY(hi))
6881 EMSG2(_(e_intern2), "dictitem_remove()");
6882 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006883 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006884 dictitem_free(item);
6885}
6886
6887/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006888 * Free a dict item. Also clears the value.
6889 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006890 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006892 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006893{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006894 clear_tv(&item->di_tv);
6895 vim_free(item);
6896}
6897
6898/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6900 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006901 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902 * Returns NULL when out of memory.
6903 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006904 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006905dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006906 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006907 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006908 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006909{
Bram Moolenaar33570922005-01-25 22:26:29 +00006910 dict_T *copy;
6911 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006912 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006913 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006914
6915 if (orig == NULL)
6916 return NULL;
6917
6918 copy = dict_alloc();
6919 if (copy != NULL)
6920 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006921 if (copyID != 0)
6922 {
6923 orig->dv_copyID = copyID;
6924 orig->dv_copydict = copy;
6925 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006926 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006927 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006928 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006929 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006930 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006931 --todo;
6932
6933 di = dictitem_alloc(hi->hi_key);
6934 if (di == NULL)
6935 break;
6936 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006937 {
6938 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6939 copyID) == FAIL)
6940 {
6941 vim_free(di);
6942 break;
6943 }
6944 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006945 else
6946 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6947 if (dict_add(copy, di) == FAIL)
6948 {
6949 dictitem_free(di);
6950 break;
6951 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006953 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006954
Bram Moolenaare9a41262005-01-15 22:18:47 +00006955 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006956 if (todo > 0)
6957 {
6958 dict_unref(copy);
6959 copy = NULL;
6960 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006961 }
6962
6963 return copy;
6964}
6965
6966/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006967 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006968 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006970 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006971dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 dict_T *d;
6973 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974{
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976}
6977
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006979 * Add a number or string entry to dictionary "d".
6980 * When "str" is NULL use number "nr", otherwise use "str".
6981 * Returns FAIL when out of memory and when key already exists.
6982 */
6983 int
6984dict_add_nr_str(d, key, nr, str)
6985 dict_T *d;
6986 char *key;
6987 long nr;
6988 char_u *str;
6989{
6990 dictitem_T *item;
6991
6992 item = dictitem_alloc((char_u *)key);
6993 if (item == NULL)
6994 return FAIL;
6995 item->di_tv.v_lock = 0;
6996 if (str == NULL)
6997 {
6998 item->di_tv.v_type = VAR_NUMBER;
6999 item->di_tv.vval.v_number = nr;
7000 }
7001 else
7002 {
7003 item->di_tv.v_type = VAR_STRING;
7004 item->di_tv.vval.v_string = vim_strsave(str);
7005 }
7006 if (dict_add(d, item) == FAIL)
7007 {
7008 dictitem_free(item);
7009 return FAIL;
7010 }
7011 return OK;
7012}
7013
7014/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 * Get the number of items in a Dictionary.
7016 */
7017 static long
7018dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007019 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007020{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007021 if (d == NULL)
7022 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007023 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024}
7025
7026/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027 * Find item "key[len]" in Dictionary "d".
7028 * If "len" is negative use strlen(key).
7029 * Returns NULL when not found.
7030 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007032dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007033 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034 char_u *key;
7035 int len;
7036{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007037#define AKEYLEN 200
7038 char_u buf[AKEYLEN];
7039 char_u *akey;
7040 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007041 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007043 if (len < 0)
7044 akey = key;
7045 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007046 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047 tofree = akey = vim_strnsave(key, len);
7048 if (akey == NULL)
7049 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 else
7052 {
7053 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007054 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007055 akey = buf;
7056 }
7057
Bram Moolenaar33570922005-01-25 22:26:29 +00007058 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007059 vim_free(tofree);
7060 if (HASHITEM_EMPTY(hi))
7061 return NULL;
7062 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063}
7064
7065/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007066 * Get a string item from a dictionary.
7067 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007068 * Returns NULL if the entry doesn't exist or out of memory.
7069 */
7070 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007071get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007072 dict_T *d;
7073 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007074 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007075{
7076 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007077 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007078
7079 di = dict_find(d, key, -1);
7080 if (di == NULL)
7081 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007082 s = get_tv_string(&di->di_tv);
7083 if (save && s != NULL)
7084 s = vim_strsave(s);
7085 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007086}
7087
7088/*
7089 * Get a number item from a dictionary.
7090 * Returns 0 if the entry doesn't exist or out of memory.
7091 */
7092 long
7093get_dict_number(d, key)
7094 dict_T *d;
7095 char_u *key;
7096{
7097 dictitem_T *di;
7098
7099 di = dict_find(d, key, -1);
7100 if (di == NULL)
7101 return 0;
7102 return get_tv_number(&di->di_tv);
7103}
7104
7105/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 * Return an allocated string with the string representation of a Dictionary.
7107 * May return NULL.
7108 */
7109 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007110dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007112 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007113{
7114 garray_T ga;
7115 int first = TRUE;
7116 char_u *tofree;
7117 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007119 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007122
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007124 return NULL;
7125 ga_init2(&ga, (int)sizeof(char), 80);
7126 ga_append(&ga, '{');
7127
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007128 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007129 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 --todo;
7134
7135 if (first)
7136 first = FALSE;
7137 else
7138 ga_concat(&ga, (char_u *)", ");
7139
7140 tofree = string_quote(hi->hi_key, FALSE);
7141 if (tofree != NULL)
7142 {
7143 ga_concat(&ga, tofree);
7144 vim_free(tofree);
7145 }
7146 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007147 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007148 if (s != NULL)
7149 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007150 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151 if (s == NULL)
7152 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007153 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007155 if (todo > 0)
7156 {
7157 vim_free(ga.ga_data);
7158 return NULL;
7159 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007160
7161 ga_append(&ga, '}');
7162 ga_append(&ga, NUL);
7163 return (char_u *)ga.ga_data;
7164}
7165
7166/*
7167 * Allocate a variable for a Dictionary and fill it from "*arg".
7168 * Return OK or FAIL. Returns NOTDONE for {expr}.
7169 */
7170 static int
7171get_dict_tv(arg, rettv, evaluate)
7172 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 int evaluate;
7175{
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 dict_T *d = NULL;
7177 typval_T tvkey;
7178 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007179 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183
7184 /*
7185 * First check if it's not a curly-braces thing: {expr}.
7186 * Must do this without evaluating, otherwise a function may be called
7187 * twice. Unfortunately this means we need to call eval1() twice for the
7188 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007189 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 if (*start != '}')
7192 {
7193 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7194 return FAIL;
7195 if (*start == '}')
7196 return NOTDONE;
7197 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198
7199 if (evaluate)
7200 {
7201 d = dict_alloc();
7202 if (d == NULL)
7203 return FAIL;
7204 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205 tvkey.v_type = VAR_UNKNOWN;
7206 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207
7208 *arg = skipwhite(*arg + 1);
7209 while (**arg != '}' && **arg != NUL)
7210 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007211 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 goto failret;
7213 if (**arg != ':')
7214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007215 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 goto failret;
7218 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007219 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007221 key = get_tv_string_buf_chk(&tvkey, buf);
7222 if (key == NULL || *key == NUL)
7223 {
7224 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7225 if (key != NULL)
7226 EMSG(_(e_emptykey));
7227 clear_tv(&tvkey);
7228 goto failret;
7229 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231
7232 *arg = skipwhite(*arg + 1);
7233 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7234 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007235 if (evaluate)
7236 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 goto failret;
7238 }
7239 if (evaluate)
7240 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242 if (item != NULL)
7243 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007244 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007245 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 clear_tv(&tv);
7247 goto failret;
7248 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249 item = dictitem_alloc(key);
7250 clear_tv(&tvkey);
7251 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007254 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 if (dict_add(d, item) == FAIL)
7256 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 }
7258 }
7259
7260 if (**arg == '}')
7261 break;
7262 if (**arg != ',')
7263 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007264 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 goto failret;
7266 }
7267 *arg = skipwhite(*arg + 1);
7268 }
7269
7270 if (**arg != '}')
7271 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007272 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273failret:
7274 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007275 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276 return FAIL;
7277 }
7278
7279 *arg = skipwhite(*arg + 1);
7280 if (evaluate)
7281 {
7282 rettv->v_type = VAR_DICT;
7283 rettv->vval.v_dict = d;
7284 ++d->dv_refcount;
7285 }
7286
7287 return OK;
7288}
7289
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007291 * Return a string with the string representation of a variable.
7292 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007293 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007294 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007295 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007296 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 */
7298 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007299echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007301 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007302 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007303 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007304{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 static int recurse = 0;
7306 char_u *r = NULL;
7307
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007310 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007311 *tofree = NULL;
7312 return NULL;
7313 }
7314 ++recurse;
7315
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 switch (tv->v_type)
7317 {
7318 case VAR_FUNC:
7319 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 r = tv->vval.v_string;
7321 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007323 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007324 if (tv->vval.v_list == NULL)
7325 {
7326 *tofree = NULL;
7327 r = NULL;
7328 }
7329 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7330 {
7331 *tofree = NULL;
7332 r = (char_u *)"[...]";
7333 }
7334 else
7335 {
7336 tv->vval.v_list->lv_copyID = copyID;
7337 *tofree = list2string(tv, copyID);
7338 r = *tofree;
7339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007341
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007343 if (tv->vval.v_dict == NULL)
7344 {
7345 *tofree = NULL;
7346 r = NULL;
7347 }
7348 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7349 {
7350 *tofree = NULL;
7351 r = (char_u *)"{...}";
7352 }
7353 else
7354 {
7355 tv->vval.v_dict->dv_copyID = copyID;
7356 *tofree = dict2string(tv, copyID);
7357 r = *tofree;
7358 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007359 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007361 case VAR_STRING:
7362 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 *tofree = NULL;
7364 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007365 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007366
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007367#ifdef FEAT_FLOAT
7368 case VAR_FLOAT:
7369 *tofree = NULL;
7370 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7371 r = numbuf;
7372 break;
7373#endif
7374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007375 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007376 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007378 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379
7380 --recurse;
7381 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007382}
7383
7384/*
7385 * Return a string with the string representation of a variable.
7386 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7387 * "numbuf" is used for a number.
7388 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007389 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007390 */
7391 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007392tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007394 char_u **tofree;
7395 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007396 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007397{
7398 switch (tv->v_type)
7399 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007400 case VAR_FUNC:
7401 *tofree = string_quote(tv->vval.v_string, TRUE);
7402 return *tofree;
7403 case VAR_STRING:
7404 *tofree = string_quote(tv->vval.v_string, FALSE);
7405 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007406#ifdef FEAT_FLOAT
7407 case VAR_FLOAT:
7408 *tofree = NULL;
7409 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7410 return numbuf;
7411#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007413 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007415 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007416 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007417 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007418 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007419 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007420}
7421
7422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 * Return string "str" in ' quotes, doubling ' characters.
7424 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007426 */
7427 static char_u *
7428string_quote(str, function)
7429 char_u *str;
7430 int function;
7431{
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007433 char_u *p, *r, *s;
7434
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 len = (function ? 13 : 3);
7436 if (str != NULL)
7437 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007438 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 for (p = str; *p != NUL; mb_ptr_adv(p))
7440 if (*p == '\'')
7441 ++len;
7442 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007443 s = r = alloc(len);
7444 if (r != NULL)
7445 {
7446 if (function)
7447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007449 r += 10;
7450 }
7451 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 if (str != NULL)
7454 for (p = str; *p != NUL; )
7455 {
7456 if (*p == '\'')
7457 *r++ = '\'';
7458 MB_COPY_CHAR(p, r);
7459 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007461 if (function)
7462 *r++ = ')';
7463 *r++ = NUL;
7464 }
7465 return s;
7466}
7467
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007468#ifdef FEAT_FLOAT
7469/*
7470 * Convert the string "text" to a floating point number.
7471 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7472 * this always uses a decimal point.
7473 * Returns the length of the text that was consumed.
7474 */
7475 static int
7476string2float(text, value)
7477 char_u *text;
7478 float_T *value; /* result stored here */
7479{
7480 char *s = (char *)text;
7481 float_T f;
7482
7483 f = strtod(s, &s);
7484 *value = f;
7485 return (int)((char_u *)s - text);
7486}
7487#endif
7488
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 * Get the value of an environment variable.
7491 * "arg" is pointing to the '$'. It is advanced to after the name.
7492 * If the environment variable was not set, silently assume it is empty.
7493 * Always return OK.
7494 */
7495 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007496get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 int evaluate;
7500{
7501 char_u *string = NULL;
7502 int len;
7503 int cc;
7504 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007505 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
7507 ++*arg;
7508 name = *arg;
7509 len = get_env_len(arg);
7510 if (evaluate)
7511 {
7512 if (len != 0)
7513 {
7514 cc = name[len];
7515 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007516 /* first try vim_getenv(), fast for normal environment vars */
7517 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007519 {
7520 if (!mustfree)
7521 string = vim_strsave(string);
7522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 else
7524 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007525 if (mustfree)
7526 vim_free(string);
7527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 /* next try expanding things like $VIM and ${HOME} */
7529 string = expand_env_save(name - 1);
7530 if (string != NULL && *string == '$')
7531 {
7532 vim_free(string);
7533 string = NULL;
7534 }
7535 }
7536 name[len] = cc;
7537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 rettv->v_type = VAR_STRING;
7539 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 }
7541
7542 return OK;
7543}
7544
7545/*
7546 * Array with names and number of arguments of all internal functions
7547 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7548 */
7549static struct fst
7550{
7551 char *f_name; /* function name */
7552 char f_min_argc; /* minimal number of arguments */
7553 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007555 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556} functions[] =
7557{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007558#ifdef FEAT_FLOAT
7559 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007560 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007561#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007562 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {"append", 2, 2, f_append},
7564 {"argc", 0, 0, f_argc},
7565 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007566 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007567#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007568 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007569 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007570 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007573 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {"bufexists", 1, 1, f_bufexists},
7575 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7576 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7577 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7578 {"buflisted", 1, 1, f_buflisted},
7579 {"bufloaded", 1, 1, f_bufloaded},
7580 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007581 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 {"bufwinnr", 1, 1, f_bufwinnr},
7583 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007584 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007585 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007586#ifdef FEAT_FLOAT
7587 {"ceil", 1, 1, f_ceil},
7588#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007589 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {"char2nr", 1, 1, f_char2nr},
7591 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007592 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007594#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007595 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007596 {"complete_add", 1, 1, f_complete_add},
7597 {"complete_check", 0, 0, f_complete_check},
7598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007600 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007601#ifdef FEAT_FLOAT
7602 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007603 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007604#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007605 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007607 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {"delete", 1, 1, f_delete},
7610 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007611 {"diff_filler", 1, 1, f_diff_filler},
7612 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007613 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007615 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {"eventhandler", 0, 0, f_eventhandler},
7617 {"executable", 1, 1, f_executable},
7618 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007619#ifdef FEAT_FLOAT
7620 {"exp", 1, 1, f_exp},
7621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007623 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007624 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7626 {"filereadable", 1, 1, f_filereadable},
7627 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007628 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007629 {"finddir", 1, 3, f_finddir},
7630 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007631#ifdef FEAT_FLOAT
7632 {"float2nr", 1, 1, f_float2nr},
7633 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007634 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007635#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007636 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {"fnamemodify", 2, 2, f_fnamemodify},
7638 {"foldclosed", 1, 1, f_foldclosed},
7639 {"foldclosedend", 1, 1, f_foldclosedend},
7640 {"foldlevel", 1, 1, f_foldlevel},
7641 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007642 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007644 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007645 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007646 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007647 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {"getbufvar", 2, 2, f_getbufvar},
7649 {"getchar", 0, 1, f_getchar},
7650 {"getcharmod", 0, 0, f_getcharmod},
7651 {"getcmdline", 0, 0, f_getcmdline},
7652 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007653 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007655 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007656 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {"getfsize", 1, 1, f_getfsize},
7658 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007659 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007660 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007661 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007662 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007663 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007664 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007665 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007666 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007668 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007669 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {"getwinposx", 0, 0, f_getwinposx},
7671 {"getwinposy", 0, 0, f_getwinposy},
7672 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007673 {"glob", 1, 2, f_glob},
7674 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007677 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007678 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7680 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7681 {"histadd", 2, 2, f_histadd},
7682 {"histdel", 1, 2, f_histdel},
7683 {"histget", 1, 2, f_histget},
7684 {"histnr", 1, 1, f_histnr},
7685 {"hlID", 1, 1, f_hlID},
7686 {"hlexists", 1, 1, f_hlexists},
7687 {"hostname", 0, 0, f_hostname},
7688 {"iconv", 3, 3, f_iconv},
7689 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007691 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007693 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {"inputrestore", 0, 0, f_inputrestore},
7695 {"inputsave", 0, 0, f_inputsave},
7696 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007697 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007699 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007701 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007704 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {"libcall", 3, 3, f_libcall},
7706 {"libcallnr", 3, 3, f_libcallnr},
7707 {"line", 1, 1, f_line},
7708 {"line2byte", 1, 1, f_line2byte},
7709 {"lispindent", 1, 1, f_lispindent},
7710 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007711#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007712 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007713 {"log10", 1, 1, f_log10},
7714#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007716 {"maparg", 1, 3, f_maparg},
7717 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007718 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007719 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007720 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007721 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007722 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007723 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007724 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007725 {"max", 1, 1, f_max},
7726 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007727#ifdef vim_mkdir
7728 {"mkdir", 1, 3, f_mkdir},
7729#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007730 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007731#ifdef FEAT_MZSCHEME
7732 {"mzeval", 1, 1, f_mzeval},
7733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {"nextnonblank", 1, 1, f_nextnonblank},
7735 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007736 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007737#ifdef FEAT_FLOAT
7738 {"pow", 2, 2, f_pow},
7739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007741 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007742 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007744 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007745 {"reltime", 0, 2, f_reltime},
7746 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {"remote_expr", 2, 3, f_remote_expr},
7748 {"remote_foreground", 1, 1, f_remote_foreground},
7749 {"remote_peek", 1, 2, f_remote_peek},
7750 {"remote_read", 1, 1, f_remote_read},
7751 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007752 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007754 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007756 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007757#ifdef FEAT_FLOAT
7758 {"round", 1, 1, f_round},
7759#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007760 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007761 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007762 {"searchpair", 3, 7, f_searchpair},
7763 {"searchpairpos", 3, 7, f_searchpairpos},
7764 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {"server2client", 2, 2, f_server2client},
7766 {"serverlist", 0, 0, f_serverlist},
7767 {"setbufvar", 3, 3, f_setbufvar},
7768 {"setcmdpos", 1, 1, f_setcmdpos},
7769 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007770 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007771 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007772 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007773 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007775 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007776 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007778 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007780#ifdef FEAT_FLOAT
7781 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007782 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007783#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007784 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007785 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007786 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007787 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007788 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007789#ifdef FEAT_FLOAT
7790 {"sqrt", 1, 1, f_sqrt},
7791 {"str2float", 1, 1, f_str2float},
7792#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007793 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794#ifdef HAVE_STRFTIME
7795 {"strftime", 1, 2, f_strftime},
7796#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007798 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {"strlen", 1, 1, f_strlen},
7800 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007801 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {"strtrans", 1, 1, f_strtrans},
7803 {"submatch", 1, 1, f_submatch},
7804 {"substitute", 4, 4, f_substitute},
7805 {"synID", 3, 3, f_synID},
7806 {"synIDattr", 2, 3, f_synIDattr},
7807 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007808 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007809 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007810 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007811 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007812 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007813 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007814 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007815#ifdef FEAT_FLOAT
7816 {"tan", 1, 1, f_tan},
7817 {"tanh", 1, 1, f_tanh},
7818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007820 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {"tolower", 1, 1, f_tolower},
7822 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007823 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007824#ifdef FEAT_FLOAT
7825 {"trunc", 1, 1, f_trunc},
7826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {"virtcol", 1, 1, f_virtcol},
7830 {"visualmode", 0, 1, f_visualmode},
7831 {"winbufnr", 1, 1, f_winbufnr},
7832 {"wincol", 0, 0, f_wincol},
7833 {"winheight", 1, 1, f_winheight},
7834 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007835 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007837 {"winrestview", 1, 1, f_winrestview},
7838 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007840 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841};
7842
7843#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7844
7845/*
7846 * Function given to ExpandGeneric() to obtain the list of internal
7847 * or user defined function names.
7848 */
7849 char_u *
7850get_function_name(xp, idx)
7851 expand_T *xp;
7852 int idx;
7853{
7854 static int intidx = -1;
7855 char_u *name;
7856
7857 if (idx == 0)
7858 intidx = -1;
7859 if (intidx < 0)
7860 {
7861 name = get_user_func_name(xp, idx);
7862 if (name != NULL)
7863 return name;
7864 }
7865 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7866 {
7867 STRCPY(IObuff, functions[intidx].f_name);
7868 STRCAT(IObuff, "(");
7869 if (functions[intidx].f_max_argc == 0)
7870 STRCAT(IObuff, ")");
7871 return IObuff;
7872 }
7873
7874 return NULL;
7875}
7876
7877/*
7878 * Function given to ExpandGeneric() to obtain the list of internal or
7879 * user defined variable or function names.
7880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 char_u *
7882get_expr_name(xp, idx)
7883 expand_T *xp;
7884 int idx;
7885{
7886 static int intidx = -1;
7887 char_u *name;
7888
7889 if (idx == 0)
7890 intidx = -1;
7891 if (intidx < 0)
7892 {
7893 name = get_function_name(xp, idx);
7894 if (name != NULL)
7895 return name;
7896 }
7897 return get_user_var_name(xp, ++intidx);
7898}
7899
7900#endif /* FEAT_CMDL_COMPL */
7901
7902/*
7903 * Find internal function in table above.
7904 * Return index, or -1 if not found
7905 */
7906 static int
7907find_internal_func(name)
7908 char_u *name; /* name of the function */
7909{
7910 int first = 0;
7911 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7912 int cmp;
7913 int x;
7914
7915 /*
7916 * Find the function name in the table. Binary search.
7917 */
7918 while (first <= last)
7919 {
7920 x = first + ((unsigned)(last - first) >> 1);
7921 cmp = STRCMP(name, functions[x].f_name);
7922 if (cmp < 0)
7923 last = x - 1;
7924 else if (cmp > 0)
7925 first = x + 1;
7926 else
7927 return x;
7928 }
7929 return -1;
7930}
7931
7932/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7934 * name it contains, otherwise return "name".
7935 */
7936 static char_u *
7937deref_func_name(name, lenp)
7938 char_u *name;
7939 int *lenp;
7940{
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007942 int cc;
7943
7944 cc = name[*lenp];
7945 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007946 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007947 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007950 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951 {
7952 *lenp = 0;
7953 return (char_u *)""; /* just in case */
7954 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007955 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007957 }
7958
7959 return name;
7960}
7961
7962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 * Allocate a variable for the result of a function.
7964 * Return OK or FAIL.
7965 */
7966 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007967get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7968 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 char_u *name; /* name of the function */
7970 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 char_u **arg; /* argument, pointing to the '(' */
7973 linenr_T firstline; /* first line of range */
7974 linenr_T lastline; /* last line of range */
7975 int *doesrange; /* return: function handled range */
7976 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 char_u *argp;
7980 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007981 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 int argcount = 0; /* number of arguments found */
7983
7984 /*
7985 * Get the arguments.
7986 */
7987 argp = *arg;
7988 while (argcount < MAX_FUNC_ARGS)
7989 {
7990 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7991 if (*argp == ')' || *argp == ',' || *argp == NUL)
7992 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7994 {
7995 ret = FAIL;
7996 break;
7997 }
7998 ++argcount;
7999 if (*argp != ',')
8000 break;
8001 }
8002 if (*argp == ')')
8003 ++argp;
8004 else
8005 ret = FAIL;
8006
8007 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008009 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008011 {
8012 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008013 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008015 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020
8021 *arg = skipwhite(argp);
8022 return ret;
8023}
8024
8025
8026/*
8027 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008028 * Return OK when the function can't be called, FAIL otherwise.
8029 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 */
8031 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008033 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 char_u *name; /* name of the function */
8035 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008038 typval_T *argvars; /* vars for arguments, must have "argcount"
8039 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 linenr_T firstline; /* first line of range */
8041 linenr_T lastline; /* last line of range */
8042 int *doesrange; /* return: function handled range */
8043 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
8046 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047#define ERROR_UNKNOWN 0
8048#define ERROR_TOOMANY 1
8049#define ERROR_TOOFEW 2
8050#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008051#define ERROR_DICT 4
8052#define ERROR_NONE 5
8053#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 int error = ERROR_NONE;
8055 int i;
8056 int llen;
8057 ufunc_T *fp;
8058 int cc;
8059#define FLEN_FIXED 40
8060 char_u fname_buf[FLEN_FIXED + 1];
8061 char_u *fname;
8062
8063 /*
8064 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8065 * Change <SNR>123_name() to K_SNR 123_name().
8066 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8067 */
8068 cc = name[len];
8069 name[len] = NUL;
8070 llen = eval_fname_script(name);
8071 if (llen > 0)
8072 {
8073 fname_buf[0] = K_SPECIAL;
8074 fname_buf[1] = KS_EXTRA;
8075 fname_buf[2] = (int)KE_SNR;
8076 i = 3;
8077 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8078 {
8079 if (current_SID <= 0)
8080 error = ERROR_SCRIPT;
8081 else
8082 {
8083 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8084 i = (int)STRLEN(fname_buf);
8085 }
8086 }
8087 if (i + STRLEN(name + llen) < FLEN_FIXED)
8088 {
8089 STRCPY(fname_buf + i, name + llen);
8090 fname = fname_buf;
8091 }
8092 else
8093 {
8094 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8095 if (fname == NULL)
8096 error = ERROR_OTHER;
8097 else
8098 {
8099 mch_memmove(fname, fname_buf, (size_t)i);
8100 STRCPY(fname + i, name + llen);
8101 }
8102 }
8103 }
8104 else
8105 fname = name;
8106
8107 *doesrange = FALSE;
8108
8109
8110 /* execute the function if no errors detected and executing */
8111 if (evaluate && error == ERROR_NONE)
8112 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008113 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8114 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 error = ERROR_UNKNOWN;
8116
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008117 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {
8119 /*
8120 * User defined function.
8121 */
8122 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008123
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008125 /* Trigger FuncUndefined event, may load the function. */
8126 if (fp == NULL
8127 && apply_autocmds(EVENT_FUNCUNDEFINED,
8128 fname, fname, TRUE, NULL)
8129 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008131 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 fp = find_func(fname);
8133 }
8134#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008135 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008136 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008137 {
8138 /* loaded a package, search for the function again */
8139 fp = find_func(fname);
8140 }
8141
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 if (fp != NULL)
8143 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008144 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008146 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008148 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008150 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008151 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 else
8153 {
8154 /*
8155 * Call the user function.
8156 * Save and restore search patterns, script variables and
8157 * redo buffer.
8158 */
8159 save_search_patterns();
8160 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008161 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008164 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8165 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8166 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008167 /* Function was unreferenced while being used, free it
8168 * now. */
8169 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 restoreRedobuff();
8171 restore_search_patterns();
8172 error = ERROR_NONE;
8173 }
8174 }
8175 }
8176 else
8177 {
8178 /*
8179 * Find the function name in the table, call its implementation.
8180 */
8181 i = find_internal_func(fname);
8182 if (i >= 0)
8183 {
8184 if (argcount < functions[i].f_min_argc)
8185 error = ERROR_TOOFEW;
8186 else if (argcount > functions[i].f_max_argc)
8187 error = ERROR_TOOMANY;
8188 else
8189 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008190 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008191 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 error = ERROR_NONE;
8193 }
8194 }
8195 }
8196 /*
8197 * The function call (or "FuncUndefined" autocommand sequence) might
8198 * have been aborted by an error, an interrupt, or an explicitly thrown
8199 * exception that has not been caught so far. This situation can be
8200 * tested for by calling aborting(). For an error in an internal
8201 * function or for the "E132" error in call_user_func(), however, the
8202 * throw point at which the "force_abort" flag (temporarily reset by
8203 * emsg()) is normally updated has not been reached yet. We need to
8204 * update that flag first to make aborting() reliable.
8205 */
8206 update_force_abort();
8207 }
8208 if (error == ERROR_NONE)
8209 ret = OK;
8210
8211 /*
8212 * Report an error unless the argument evaluation or function call has been
8213 * cancelled due to an aborting error, an interrupt, or an exception.
8214 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008215 if (!aborting())
8216 {
8217 switch (error)
8218 {
8219 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008220 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008221 break;
8222 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008223 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008224 break;
8225 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008226 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008227 name);
8228 break;
8229 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008230 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008231 name);
8232 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008233 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008234 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008235 name);
8236 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008237 }
8238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239
8240 name[len] = cc;
8241 if (fname != name && fname != fname_buf)
8242 vim_free(fname);
8243
8244 return ret;
8245}
8246
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008247/*
8248 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008249 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008250 */
8251 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008252emsg_funcname(ermsg, name)
8253 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008254 char_u *name;
8255{
8256 char_u *p;
8257
8258 if (*name == K_SPECIAL)
8259 p = concat_str((char_u *)"<SNR>", name + 3);
8260 else
8261 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008262 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008263 if (p != name)
8264 vim_free(p);
8265}
8266
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008267/*
8268 * Return TRUE for a non-zero Number and a non-empty String.
8269 */
8270 static int
8271non_zero_arg(argvars)
8272 typval_T *argvars;
8273{
8274 return ((argvars[0].v_type == VAR_NUMBER
8275 && argvars[0].vval.v_number != 0)
8276 || (argvars[0].v_type == VAR_STRING
8277 && argvars[0].vval.v_string != NULL
8278 && *argvars[0].vval.v_string != NUL));
8279}
8280
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281/*********************************************
8282 * Implementation of the built-in functions
8283 */
8284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008285#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008286static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8287
8288/*
8289 * Get the float value of "argvars[0]" into "f".
8290 * Returns FAIL when the argument is not a Number or Float.
8291 */
8292 static int
8293get_float_arg(argvars, f)
8294 typval_T *argvars;
8295 float_T *f;
8296{
8297 if (argvars[0].v_type == VAR_FLOAT)
8298 {
8299 *f = argvars[0].vval.v_float;
8300 return OK;
8301 }
8302 if (argvars[0].v_type == VAR_NUMBER)
8303 {
8304 *f = (float_T)argvars[0].vval.v_number;
8305 return OK;
8306 }
8307 EMSG(_("E808: Number or Float required"));
8308 return FAIL;
8309}
8310
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311/*
8312 * "abs(expr)" function
8313 */
8314 static void
8315f_abs(argvars, rettv)
8316 typval_T *argvars;
8317 typval_T *rettv;
8318{
8319 if (argvars[0].v_type == VAR_FLOAT)
8320 {
8321 rettv->v_type = VAR_FLOAT;
8322 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8323 }
8324 else
8325 {
8326 varnumber_T n;
8327 int error = FALSE;
8328
8329 n = get_tv_number_chk(&argvars[0], &error);
8330 if (error)
8331 rettv->vval.v_number = -1;
8332 else if (n > 0)
8333 rettv->vval.v_number = n;
8334 else
8335 rettv->vval.v_number = -n;
8336 }
8337}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008338
8339/*
8340 * "acos()" function
8341 */
8342 static void
8343f_acos(argvars, rettv)
8344 typval_T *argvars;
8345 typval_T *rettv;
8346{
8347 float_T f;
8348
8349 rettv->v_type = VAR_FLOAT;
8350 if (get_float_arg(argvars, &f) == OK)
8351 rettv->vval.v_float = acos(f);
8352 else
8353 rettv->vval.v_float = 0.0;
8354}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008355#endif
8356
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008358 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 */
8360 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008361f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008362 typval_T *argvars;
8363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364{
Bram Moolenaar33570922005-01-25 22:26:29 +00008365 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008367 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008368 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008370 if ((l = argvars[0].vval.v_list) != NULL
8371 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8372 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008374 }
8375 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008376 EMSG(_(e_listreq));
8377}
8378
8379/*
8380 * "append(lnum, string/list)" function
8381 */
8382 static void
8383f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008384 typval_T *argvars;
8385 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008386{
8387 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008388 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008389 list_T *l = NULL;
8390 listitem_T *li = NULL;
8391 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008392 long added = 0;
8393
Bram Moolenaar0d660222005-01-07 21:51:51 +00008394 lnum = get_tv_lnum(argvars);
8395 if (lnum >= 0
8396 && lnum <= curbuf->b_ml.ml_line_count
8397 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008398 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008399 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008400 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008401 l = argvars[1].vval.v_list;
8402 if (l == NULL)
8403 return;
8404 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008405 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008406 for (;;)
8407 {
8408 if (l == NULL)
8409 tv = &argvars[1]; /* append a string */
8410 else if (li == NULL)
8411 break; /* end of list */
8412 else
8413 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 line = get_tv_string_chk(tv);
8415 if (line == NULL) /* type error */
8416 {
8417 rettv->vval.v_number = 1; /* Failed */
8418 break;
8419 }
8420 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008421 ++added;
8422 if (l == NULL)
8423 break;
8424 li = li->li_next;
8425 }
8426
8427 appended_lines_mark(lnum, added);
8428 if (curwin->w_cursor.lnum > lnum)
8429 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 else
8432 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433}
8434
8435/*
8436 * "argc()" function
8437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008439f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008440 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008443 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444}
8445
8446/*
8447 * "argidx()" function
8448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008451 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455}
8456
8457/*
8458 * "argv(nr)" function
8459 */
8460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008461f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008462 typval_T *argvars;
8463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 int idx;
8466
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008467 if (argvars[0].v_type != VAR_UNKNOWN)
8468 {
8469 idx = get_tv_number_chk(&argvars[0], NULL);
8470 if (idx >= 0 && idx < ARGCOUNT)
8471 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8472 else
8473 rettv->vval.v_string = NULL;
8474 rettv->v_type = VAR_STRING;
8475 }
8476 else if (rettv_list_alloc(rettv) == OK)
8477 for (idx = 0; idx < ARGCOUNT; ++idx)
8478 list_append_string(rettv->vval.v_list,
8479 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480}
8481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008482#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008483/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008484 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008485 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008486 static void
8487f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008488 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008489 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008490{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008491 float_T f;
8492
8493 rettv->v_type = VAR_FLOAT;
8494 if (get_float_arg(argvars, &f) == OK)
8495 rettv->vval.v_float = asin(f);
8496 else
8497 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008498}
8499
8500/*
8501 * "atan()" function
8502 */
8503 static void
8504f_atan(argvars, rettv)
8505 typval_T *argvars;
8506 typval_T *rettv;
8507{
8508 float_T f;
8509
8510 rettv->v_type = VAR_FLOAT;
8511 if (get_float_arg(argvars, &f) == OK)
8512 rettv->vval.v_float = atan(f);
8513 else
8514 rettv->vval.v_float = 0.0;
8515}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008516
8517/*
8518 * "atan2()" function
8519 */
8520 static void
8521f_atan2(argvars, rettv)
8522 typval_T *argvars;
8523 typval_T *rettv;
8524{
8525 float_T fx, fy;
8526
8527 rettv->v_type = VAR_FLOAT;
8528 if (get_float_arg(argvars, &fx) == OK
8529 && get_float_arg(&argvars[1], &fy) == OK)
8530 rettv->vval.v_float = atan2(fx, fy);
8531 else
8532 rettv->vval.v_float = 0.0;
8533}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008534#endif
8535
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536/*
8537 * "browse(save, title, initdir, default)" function
8538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544#ifdef FEAT_BROWSE
8545 int save;
8546 char_u *title;
8547 char_u *initdir;
8548 char_u *defname;
8549 char_u buf[NUMBUFLEN];
8550 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008551 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008553 save = get_tv_number_chk(&argvars[0], &error);
8554 title = get_tv_string_chk(&argvars[1]);
8555 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8556 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008558 if (error || title == NULL || initdir == NULL || defname == NULL)
8559 rettv->vval.v_string = NULL;
8560 else
8561 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008562 do_browse(save ? BROWSE_SAVE : 0,
8563 title, defname, NULL, initdir, NULL, curbuf);
8564#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008566#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008568}
8569
8570/*
8571 * "browsedir(title, initdir)" function
8572 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008574f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008575 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008577{
8578#ifdef FEAT_BROWSE
8579 char_u *title;
8580 char_u *initdir;
8581 char_u buf[NUMBUFLEN];
8582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008583 title = get_tv_string_chk(&argvars[0]);
8584 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008586 if (title == NULL || initdir == NULL)
8587 rettv->vval.v_string = NULL;
8588 else
8589 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008590 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595}
8596
Bram Moolenaar33570922005-01-25 22:26:29 +00008597static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008598
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599/*
8600 * Find a buffer by number or exact name.
8601 */
8602 static buf_T *
8603find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605{
8606 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008608 if (avar->v_type == VAR_NUMBER)
8609 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008610 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008612 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008613 if (buf == NULL)
8614 {
8615 /* No full path name match, try a match with a URL or a "nofile"
8616 * buffer, these don't use the full path. */
8617 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8618 if (buf->b_fname != NULL
8619 && (path_with_url(buf->b_fname)
8620#ifdef FEAT_QUICKFIX
8621 || bt_nofile(buf)
8622#endif
8623 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008624 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008625 break;
8626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 }
8628 return buf;
8629}
8630
8631/*
8632 * "bufexists(expr)" function
8633 */
8634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008635f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 typval_T *argvars;
8637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640}
8641
8642/*
8643 * "buflisted(expr)" function
8644 */
8645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 typval_T *argvars;
8648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649{
8650 buf_T *buf;
8651
8652 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008653 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654}
8655
8656/*
8657 * "bufloaded(expr)" function
8658 */
8659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 typval_T *argvars;
8662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663{
8664 buf_T *buf;
8665
8666 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008667 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668}
8669
Bram Moolenaar33570922005-01-25 22:26:29 +00008670static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672/*
8673 * Get buffer by number or pattern.
8674 */
8675 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008676get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008677 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 int save_magic;
8681 char_u *save_cpo;
8682 buf_T *buf;
8683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684 if (tv->v_type == VAR_NUMBER)
8685 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008686 if (tv->v_type != VAR_STRING)
8687 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 if (name == NULL || *name == NUL)
8689 return curbuf;
8690 if (name[0] == '$' && name[1] == NUL)
8691 return lastbuf;
8692
8693 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8694 save_magic = p_magic;
8695 p_magic = TRUE;
8696 save_cpo = p_cpo;
8697 p_cpo = (char_u *)"";
8698
8699 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8700 TRUE, FALSE));
8701
8702 p_magic = save_magic;
8703 p_cpo = save_cpo;
8704
8705 /* If not found, try expanding the name, like done for bufexists(). */
8706 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
8709 return buf;
8710}
8711
8712/*
8713 * "bufname(expr)" function
8714 */
8715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008717 typval_T *argvars;
8718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
8720 buf_T *buf;
8721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008724 buf = get_buf_tv(&argvars[0]);
8725 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 --emsg_off;
8731}
8732
8733/*
8734 * "bufnr(expr)" function
8735 */
8736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008738 typval_T *argvars;
8739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740{
8741 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008742 int error = FALSE;
8743 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008745 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008748 --emsg_off;
8749
8750 /* If the buffer isn't found and the second argument is not zero create a
8751 * new buffer. */
8752 if (buf == NULL
8753 && argvars[1].v_type != VAR_UNKNOWN
8754 && get_tv_number_chk(&argvars[1], &error) != 0
8755 && !error
8756 && (name = get_tv_string_chk(&argvars[0])) != NULL
8757 && !error)
8758 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764}
8765
8766/*
8767 * "bufwinnr(nr)" function
8768 */
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774#ifdef FEAT_WINDOWS
8775 win_T *wp;
8776 int winnr = 0;
8777#endif
8778 buf_T *buf;
8779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008780 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783#ifdef FEAT_WINDOWS
8784 for (wp = firstwin; wp; wp = wp->w_next)
8785 {
8786 ++winnr;
8787 if (wp->w_buffer == buf)
8788 break;
8789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793#endif
8794 --emsg_off;
8795}
8796
8797/*
8798 * "byte2line(byte)" function
8799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008802 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807#else
8808 long boff = 0;
8809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008810 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 (linenr_T)0, &boff);
8816#endif
8817}
8818
8819/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008820 * "byteidx()" function
8821 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *argvars;
8825 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008826{
8827#ifdef FEAT_MBYTE
8828 char_u *t;
8829#endif
8830 char_u *str;
8831 long idx;
8832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008833 str = get_tv_string_chk(&argvars[0]);
8834 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008835 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008836 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008837 return;
8838
8839#ifdef FEAT_MBYTE
8840 t = str;
8841 for ( ; idx > 0; idx--)
8842 {
8843 if (*t == NUL) /* EOL reached */
8844 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008845 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008846 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008847 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008848#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008849 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008850 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008851#endif
8852}
8853
8854/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008855 * "call(func, arglist)" function
8856 */
8857 static void
8858f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 typval_T *argvars;
8860 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008861{
8862 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008863 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008864 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008865 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008866 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008867 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008868
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008869 if (argvars[1].v_type != VAR_LIST)
8870 {
8871 EMSG(_(e_listreq));
8872 return;
8873 }
8874 if (argvars[1].vval.v_list == NULL)
8875 return;
8876
8877 if (argvars[0].v_type == VAR_FUNC)
8878 func = argvars[0].vval.v_string;
8879 else
8880 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 if (*func == NUL)
8882 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008883
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 if (argvars[2].v_type != VAR_UNKNOWN)
8885 {
8886 if (argvars[2].v_type != VAR_DICT)
8887 {
8888 EMSG(_(e_dictreq));
8889 return;
8890 }
8891 selfdict = argvars[2].vval.v_dict;
8892 }
8893
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008894 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8895 item = item->li_next)
8896 {
8897 if (argc == MAX_FUNC_ARGS)
8898 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008899 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008900 break;
8901 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008902 /* Make a copy of each argument. This is needed to be able to set
8903 * v_lock to VAR_FIXED in the copy without changing the original list.
8904 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008905 copy_tv(&item->li_tv, &argv[argc++]);
8906 }
8907
8908 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008909 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008910 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8911 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008912
8913 /* Free the arguments. */
8914 while (argc > 0)
8915 clear_tv(&argv[--argc]);
8916}
8917
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008918#ifdef FEAT_FLOAT
8919/*
8920 * "ceil({float})" function
8921 */
8922 static void
8923f_ceil(argvars, rettv)
8924 typval_T *argvars;
8925 typval_T *rettv;
8926{
8927 float_T f;
8928
8929 rettv->v_type = VAR_FLOAT;
8930 if (get_float_arg(argvars, &f) == OK)
8931 rettv->vval.v_float = ceil(f);
8932 else
8933 rettv->vval.v_float = 0.0;
8934}
8935#endif
8936
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008937/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008938 * "changenr()" function
8939 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008940 static void
8941f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008942 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008943 typval_T *rettv;
8944{
8945 rettv->vval.v_number = curbuf->b_u_seq_cur;
8946}
8947
8948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 * "char2nr(string)" function
8950 */
8951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 typval_T *argvars;
8954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956#ifdef FEAT_MBYTE
8957 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008958 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 else
8960#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962}
8963
8964/*
8965 * "cindent(lnum)" function
8966 */
8967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008969 typval_T *argvars;
8970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972#ifdef FEAT_CINDENT
8973 pos_T pos;
8974 linenr_T lnum;
8975
8976 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8979 {
8980 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 curwin->w_cursor = pos;
8983 }
8984 else
8985#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
8988
8989/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008990 * "clearmatches()" function
8991 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008992 static void
8993f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008994 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008995 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008996{
8997#ifdef FEAT_SEARCH_EXTRA
8998 clear_matches(curwin);
8999#endif
9000}
9001
9002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 * "col(string)" function
9004 */
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 colnr_T col = 0;
9011 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009012 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009014 fp = var2fpos(&argvars[0], FALSE, &fnum);
9015 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 {
9017 if (fp->col == MAXCOL)
9018 {
9019 /* '> can be MAXCOL, get the length of the line then */
9020 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009021 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 else
9023 col = MAXCOL;
9024 }
9025 else
9026 {
9027 col = fp->col + 1;
9028#ifdef FEAT_VIRTUALEDIT
9029 /* col(".") when the cursor is on the NUL at the end of the line
9030 * because of "coladd" can be seen as an extra column. */
9031 if (virtual_active() && fp == &curwin->w_cursor)
9032 {
9033 char_u *p = ml_get_cursor();
9034
9035 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9036 curwin->w_virtcol - curwin->w_cursor.coladd))
9037 {
9038# ifdef FEAT_MBYTE
9039 int l;
9040
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009041 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 col += l;
9043# else
9044 if (*p != NUL && p[1] == NUL)
9045 ++col;
9046# endif
9047 }
9048 }
9049#endif
9050 }
9051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053}
9054
Bram Moolenaar572cb562005-08-05 21:35:02 +00009055#if defined(FEAT_INS_EXPAND)
9056/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009057 * "complete()" function
9058 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009059 static void
9060f_complete(argvars, rettv)
9061 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009062 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009063{
9064 int startcol;
9065
9066 if ((State & INSERT) == 0)
9067 {
9068 EMSG(_("E785: complete() can only be used in Insert mode"));
9069 return;
9070 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009071
9072 /* Check for undo allowed here, because if something was already inserted
9073 * the line was already saved for undo and this check isn't done. */
9074 if (!undo_allowed())
9075 return;
9076
Bram Moolenaarade00832006-03-10 21:46:58 +00009077 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9078 {
9079 EMSG(_(e_invarg));
9080 return;
9081 }
9082
9083 startcol = get_tv_number_chk(&argvars[0], NULL);
9084 if (startcol <= 0)
9085 return;
9086
9087 set_completion(startcol - 1, argvars[1].vval.v_list);
9088}
9089
9090/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009091 * "complete_add()" function
9092 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009093 static void
9094f_complete_add(argvars, rettv)
9095 typval_T *argvars;
9096 typval_T *rettv;
9097{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009098 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009099}
9100
9101/*
9102 * "complete_check()" function
9103 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009104 static void
9105f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009106 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009107 typval_T *rettv;
9108{
9109 int saved = RedrawingDisabled;
9110
9111 RedrawingDisabled = 0;
9112 ins_compl_check_keys(0);
9113 rettv->vval.v_number = compl_interrupted;
9114 RedrawingDisabled = saved;
9115}
9116#endif
9117
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118/*
9119 * "confirm(message, buttons[, default [, type]])" function
9120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009123 typval_T *argvars UNUSED;
9124 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9127 char_u *message;
9128 char_u *buttons = NULL;
9129 char_u buf[NUMBUFLEN];
9130 char_u buf2[NUMBUFLEN];
9131 int def = 1;
9132 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009133 char_u *typestr;
9134 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 message = get_tv_string_chk(&argvars[0]);
9137 if (message == NULL)
9138 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009139 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9142 if (buttons == NULL)
9143 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009144 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009147 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009149 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9150 if (typestr == NULL)
9151 error = TRUE;
9152 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 switch (TOUPPER_ASC(*typestr))
9155 {
9156 case 'E': type = VIM_ERROR; break;
9157 case 'Q': type = VIM_QUESTION; break;
9158 case 'I': type = VIM_INFO; break;
9159 case 'W': type = VIM_WARNING; break;
9160 case 'G': type = VIM_GENERIC; break;
9161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163 }
9164 }
9165 }
9166
9167 if (buttons == NULL || *buttons == NUL)
9168 buttons = (char_u *)_("&Ok");
9169
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009170 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009171 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173#endif
9174}
9175
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009176/*
9177 * "copy()" function
9178 */
9179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009181 typval_T *argvars;
9182 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009183{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009184 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009185}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009187#ifdef FEAT_FLOAT
9188/*
9189 * "cos()" function
9190 */
9191 static void
9192f_cos(argvars, rettv)
9193 typval_T *argvars;
9194 typval_T *rettv;
9195{
9196 float_T f;
9197
9198 rettv->v_type = VAR_FLOAT;
9199 if (get_float_arg(argvars, &f) == OK)
9200 rettv->vval.v_float = cos(f);
9201 else
9202 rettv->vval.v_float = 0.0;
9203}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009204
9205/*
9206 * "cosh()" function
9207 */
9208 static void
9209f_cosh(argvars, rettv)
9210 typval_T *argvars;
9211 typval_T *rettv;
9212{
9213 float_T f;
9214
9215 rettv->v_type = VAR_FLOAT;
9216 if (get_float_arg(argvars, &f) == OK)
9217 rettv->vval.v_float = cosh(f);
9218 else
9219 rettv->vval.v_float = 0.0;
9220}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009221#endif
9222
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009224 * "count()" function
9225 */
9226 static void
9227f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009228 typval_T *argvars;
9229 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009230{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009231 long n = 0;
9232 int ic = FALSE;
9233
Bram Moolenaare9a41262005-01-15 22:18:47 +00009234 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009235 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009236 listitem_T *li;
9237 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009238 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009239
Bram Moolenaare9a41262005-01-15 22:18:47 +00009240 if ((l = argvars[0].vval.v_list) != NULL)
9241 {
9242 li = l->lv_first;
9243 if (argvars[2].v_type != VAR_UNKNOWN)
9244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 int error = FALSE;
9246
9247 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009248 if (argvars[3].v_type != VAR_UNKNOWN)
9249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 idx = get_tv_number_chk(&argvars[3], &error);
9251 if (!error)
9252 {
9253 li = list_find(l, idx);
9254 if (li == NULL)
9255 EMSGN(_(e_listidx), idx);
9256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 if (error)
9259 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009260 }
9261
9262 for ( ; li != NULL; li = li->li_next)
9263 if (tv_equal(&li->li_tv, &argvars[1], ic))
9264 ++n;
9265 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009266 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009267 else if (argvars[0].v_type == VAR_DICT)
9268 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009269 int todo;
9270 dict_T *d;
9271 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009272
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009273 if ((d = argvars[0].vval.v_dict) != NULL)
9274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 int error = FALSE;
9276
Bram Moolenaare9a41262005-01-15 22:18:47 +00009277 if (argvars[2].v_type != VAR_UNKNOWN)
9278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009279 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 if (argvars[3].v_type != VAR_UNKNOWN)
9281 EMSG(_(e_invarg));
9282 }
9283
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009284 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009285 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009286 {
9287 if (!HASHITEM_EMPTY(hi))
9288 {
9289 --todo;
9290 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9291 ++n;
9292 }
9293 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009294 }
9295 }
9296 else
9297 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009298 rettv->vval.v_number = n;
9299}
9300
9301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9303 *
9304 * Checks the existence of a cscope connection.
9305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009308 typval_T *argvars UNUSED;
9309 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311#ifdef FEAT_CSCOPE
9312 int num = 0;
9313 char_u *dbpath = NULL;
9314 char_u *prepend = NULL;
9315 char_u buf[NUMBUFLEN];
9316
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009317 if (argvars[0].v_type != VAR_UNKNOWN
9318 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009320 num = (int)get_tv_number(&argvars[0]);
9321 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009322 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 }
9325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327#endif
9328}
9329
9330/*
9331 * "cursor(lnum, col)" function
9332 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009333 * Moves the cursor to the specified line and column.
9334 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009338 typval_T *argvars;
9339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009342#ifdef FEAT_VIRTUALEDIT
9343 long coladd = 0;
9344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009346 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009347 if (argvars[1].v_type == VAR_UNKNOWN)
9348 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009349 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009350
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009351 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009352 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009353 line = pos.lnum;
9354 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009355#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009356 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009357#endif
9358 }
9359 else
9360 {
9361 line = get_tv_lnum(argvars);
9362 col = get_tv_number_chk(&argvars[1], NULL);
9363#ifdef FEAT_VIRTUALEDIT
9364 if (argvars[2].v_type != VAR_UNKNOWN)
9365 coladd = get_tv_number_chk(&argvars[2], NULL);
9366#endif
9367 }
9368 if (line < 0 || col < 0
9369#ifdef FEAT_VIRTUALEDIT
9370 || coladd < 0
9371#endif
9372 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009373 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 if (line > 0)
9375 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 if (col > 0)
9377 curwin->w_cursor.col = col - 1;
9378#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009379 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380#endif
9381
9382 /* Make sure the cursor is in a valid position. */
9383 check_cursor();
9384#ifdef FEAT_MBYTE
9385 /* Correct cursor for multi-byte character. */
9386 if (has_mbyte)
9387 mb_adjust_cursor();
9388#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009389
9390 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009391 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392}
9393
9394/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009395 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 */
9397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009399 typval_T *argvars;
9400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009402 int noref = 0;
9403
9404 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009405 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009406 if (noref < 0 || noref > 1)
9407 EMSG(_(e_invarg));
9408 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009409 {
9410 current_copyID += COPYID_INC;
9411 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413}
9414
9415/*
9416 * "delete()" function
9417 */
9418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 typval_T *argvars;
9421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427}
9428
9429/*
9430 * "did_filetype()" function
9431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009434 typval_T *argvars UNUSED;
9435 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
9437#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439#endif
9440}
9441
9442/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009443 * "diff_filler()" function
9444 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009446f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009447 typval_T *argvars UNUSED;
9448 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009449{
9450#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009452#endif
9453}
9454
9455/*
9456 * "diff_hlID()" function
9457 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009460 typval_T *argvars UNUSED;
9461 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009462{
9463#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009465 static linenr_T prev_lnum = 0;
9466 static int changedtick = 0;
9467 static int fnum = 0;
9468 static int change_start = 0;
9469 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009470 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009471 int filler_lines;
9472 int col;
9473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 if (lnum < 0) /* ignore type error in {lnum} arg */
9475 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009476 if (lnum != prev_lnum
9477 || changedtick != curbuf->b_changedtick
9478 || fnum != curbuf->b_fnum)
9479 {
9480 /* New line, buffer, change: need to get the values. */
9481 filler_lines = diff_check(curwin, lnum);
9482 if (filler_lines < 0)
9483 {
9484 if (filler_lines == -1)
9485 {
9486 change_start = MAXCOL;
9487 change_end = -1;
9488 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9489 hlID = HLF_ADD; /* added line */
9490 else
9491 hlID = HLF_CHD; /* changed line */
9492 }
9493 else
9494 hlID = HLF_ADD; /* added line */
9495 }
9496 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009497 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009498 prev_lnum = lnum;
9499 changedtick = curbuf->b_changedtick;
9500 fnum = curbuf->b_fnum;
9501 }
9502
9503 if (hlID == HLF_CHD || hlID == HLF_TXD)
9504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009505 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009506 if (col >= change_start && col <= change_end)
9507 hlID = HLF_TXD; /* changed text */
9508 else
9509 hlID = HLF_CHD; /* changed line */
9510 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009511 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009512#endif
9513}
9514
9515/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009516 * "empty({expr})" function
9517 */
9518 static void
9519f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009520 typval_T *argvars;
9521 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009522{
9523 int n;
9524
9525 switch (argvars[0].v_type)
9526 {
9527 case VAR_STRING:
9528 case VAR_FUNC:
9529 n = argvars[0].vval.v_string == NULL
9530 || *argvars[0].vval.v_string == NUL;
9531 break;
9532 case VAR_NUMBER:
9533 n = argvars[0].vval.v_number == 0;
9534 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009535#ifdef FEAT_FLOAT
9536 case VAR_FLOAT:
9537 n = argvars[0].vval.v_float == 0.0;
9538 break;
9539#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009540 case VAR_LIST:
9541 n = argvars[0].vval.v_list == NULL
9542 || argvars[0].vval.v_list->lv_first == NULL;
9543 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009544 case VAR_DICT:
9545 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009546 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009547 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009548 default:
9549 EMSG2(_(e_intern2), "f_empty()");
9550 n = 0;
9551 }
9552
9553 rettv->vval.v_number = n;
9554}
9555
9556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 * "escape({string}, {chars})" function
9558 */
9559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009561 typval_T *argvars;
9562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 char_u buf[NUMBUFLEN];
9565
Bram Moolenaar758711c2005-02-02 23:11:38 +00009566 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9567 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009572 * "eval()" function
9573 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009574 static void
9575f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009576 typval_T *argvars;
9577 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009578{
9579 char_u *s;
9580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 s = get_tv_string_chk(&argvars[0]);
9582 if (s != NULL)
9583 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9586 {
9587 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009588 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009590 else if (*s != NUL)
9591 EMSG(_(e_trailing));
9592}
9593
9594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 * "eventhandler()" function
9596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009599 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * "executable()" function
9607 */
9608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 typval_T *argvars;
9611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614}
9615
9616/*
9617 * "exists()" function
9618 */
9619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 char_u *p;
9625 char_u *name;
9626 int n = FALSE;
9627 int len = 0;
9628
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 if (*p == '$') /* environment variable */
9631 {
9632 /* first try "normal" environment variables (fast) */
9633 if (mch_getenv(p + 1) != NULL)
9634 n = TRUE;
9635 else
9636 {
9637 /* try expanding things like $VIM and ${HOME} */
9638 p = expand_env_save(p);
9639 if (p != NULL && *p != '$')
9640 n = TRUE;
9641 vim_free(p);
9642 }
9643 }
9644 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009647 if (*skipwhite(p) != NUL)
9648 n = FALSE; /* trailing garbage */
9649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 else if (*p == '*') /* internal or user defined function */
9651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009652 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 }
9654 else if (*p == ':')
9655 {
9656 n = cmd_exists(p + 1);
9657 }
9658 else if (*p == '#')
9659 {
9660#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009661 if (p[1] == '#')
9662 n = autocmd_supported(p + 2);
9663 else
9664 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665#endif
9666 }
9667 else /* internal variable */
9668 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009669 char_u *tofree;
9670 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009672 /* get_name_len() takes care of expanding curly braces */
9673 name = p;
9674 len = get_name_len(&p, &tofree, TRUE, FALSE);
9675 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009677 if (tofree != NULL)
9678 name = tofree;
9679 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9680 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009682 /* handle d.key, l[idx], f(expr) */
9683 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9684 if (n)
9685 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 }
9687 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009688 if (*p != NUL)
9689 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009691 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 }
9693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695}
9696
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009697#ifdef FEAT_FLOAT
9698/*
9699 * "exp()" function
9700 */
9701 static void
9702f_exp(argvars, rettv)
9703 typval_T *argvars;
9704 typval_T *rettv;
9705{
9706 float_T f;
9707
9708 rettv->v_type = VAR_FLOAT;
9709 if (get_float_arg(argvars, &f) == OK)
9710 rettv->vval.v_float = exp(f);
9711 else
9712 rettv->vval.v_float = 0.0;
9713}
9714#endif
9715
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716/*
9717 * "expand()" function
9718 */
9719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009721 typval_T *argvars;
9722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
9724 char_u *s;
9725 int len;
9726 char_u *errormsg;
9727 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9728 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009729 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 rettv->v_type = VAR_STRING;
9732 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (*s == '%' || *s == '#' || *s == '<')
9734 {
9735 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009736 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 --emsg_off;
9738 }
9739 else
9740 {
9741 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009742 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009743 if (argvars[1].v_type != VAR_UNKNOWN
9744 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009746 if (!error)
9747 {
9748 ExpandInit(&xpc);
9749 xpc.xp_context = EXPAND_FILES;
9750 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009751 }
9752 else
9753 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 }
9755}
9756
9757/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009758 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009759 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009760 */
9761 static void
9762f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009763 typval_T *argvars;
9764 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009765{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009766 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009767 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 list_T *l1, *l2;
9769 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009770 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009772
Bram Moolenaare9a41262005-01-15 22:18:47 +00009773 l1 = argvars[0].vval.v_list;
9774 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009775 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9776 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009777 {
9778 if (argvars[2].v_type != VAR_UNKNOWN)
9779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009780 before = get_tv_number_chk(&argvars[2], &error);
9781 if (error)
9782 return; /* type error; errmsg already given */
9783
Bram Moolenaar758711c2005-02-02 23:11:38 +00009784 if (before == l1->lv_len)
9785 item = NULL;
9786 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009787 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009788 item = list_find(l1, before);
9789 if (item == NULL)
9790 {
9791 EMSGN(_(e_listidx), before);
9792 return;
9793 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009794 }
9795 }
9796 else
9797 item = NULL;
9798 list_extend(l1, l2, item);
9799
Bram Moolenaare9a41262005-01-15 22:18:47 +00009800 copy_tv(&argvars[0], rettv);
9801 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009803 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9804 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009805 dict_T *d1, *d2;
9806 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009807 char_u *action;
9808 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009809 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009810 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009811
9812 d1 = argvars[0].vval.v_dict;
9813 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009814 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9815 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009816 {
9817 /* Check the third argument. */
9818 if (argvars[2].v_type != VAR_UNKNOWN)
9819 {
9820 static char *(av[]) = {"keep", "force", "error"};
9821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 action = get_tv_string_chk(&argvars[2]);
9823 if (action == NULL)
9824 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009825 for (i = 0; i < 3; ++i)
9826 if (STRCMP(action, av[i]) == 0)
9827 break;
9828 if (i == 3)
9829 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009830 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009831 return;
9832 }
9833 }
9834 else
9835 action = (char_u *)"force";
9836
9837 /* Go over all entries in the second dict and add them to the
9838 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009839 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009840 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009841 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009842 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009843 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009844 --todo;
9845 di1 = dict_find(d1, hi2->hi_key, -1);
9846 if (di1 == NULL)
9847 {
9848 di1 = dictitem_copy(HI2DI(hi2));
9849 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9850 dictitem_free(di1);
9851 }
9852 else if (*action == 'e')
9853 {
9854 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9855 break;
9856 }
9857 else if (*action == 'f')
9858 {
9859 clear_tv(&di1->di_tv);
9860 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9861 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009862 }
9863 }
9864
Bram Moolenaare9a41262005-01-15 22:18:47 +00009865 copy_tv(&argvars[0], rettv);
9866 }
9867 }
9868 else
9869 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009870}
9871
9872/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009873 * "feedkeys()" function
9874 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009875 static void
9876f_feedkeys(argvars, rettv)
9877 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009878 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009879{
9880 int remap = TRUE;
9881 char_u *keys, *flags;
9882 char_u nbuf[NUMBUFLEN];
9883 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009884 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009885
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009886 /* This is not allowed in the sandbox. If the commands would still be
9887 * executed in the sandbox it would be OK, but it probably happens later,
9888 * when "sandbox" is no longer set. */
9889 if (check_secure())
9890 return;
9891
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009892 keys = get_tv_string(&argvars[0]);
9893 if (*keys != NUL)
9894 {
9895 if (argvars[1].v_type != VAR_UNKNOWN)
9896 {
9897 flags = get_tv_string_buf(&argvars[1], nbuf);
9898 for ( ; *flags != NUL; ++flags)
9899 {
9900 switch (*flags)
9901 {
9902 case 'n': remap = FALSE; break;
9903 case 'm': remap = TRUE; break;
9904 case 't': typed = TRUE; break;
9905 }
9906 }
9907 }
9908
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009909 /* Need to escape K_SPECIAL and CSI before putting the string in the
9910 * typeahead buffer. */
9911 keys_esc = vim_strsave_escape_csi(keys);
9912 if (keys_esc != NULL)
9913 {
9914 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009915 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009916 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009917 if (vgetc_busy)
9918 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009919 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009920 }
9921}
9922
9923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 * "filereadable()" function
9925 */
9926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009928 typval_T *argvars;
9929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009931 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 char_u *p;
9933 int n;
9934
Bram Moolenaarc236c162008-07-13 17:41:49 +00009935#ifndef O_NONBLOCK
9936# define O_NONBLOCK 0
9937#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009939 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9940 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 {
9942 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009943 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 }
9945 else
9946 n = FALSE;
9947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949}
9950
9951/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009952 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 * rights to write into.
9954 */
9955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009960 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961}
9962
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009963static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009964
9965 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009966findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 typval_T *argvars;
9968 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009969 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009970{
9971#ifdef FEAT_SEARCHPATH
9972 char_u *fname;
9973 char_u *fresult = NULL;
9974 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9975 char_u *p;
9976 char_u pathbuf[NUMBUFLEN];
9977 int count = 1;
9978 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009979 int error = FALSE;
9980#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009981
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009982 rettv->vval.v_string = NULL;
9983 rettv->v_type = VAR_STRING;
9984
9985#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009987
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009988 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009990 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9991 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009992 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 else
9994 {
9995 if (*p != NUL)
9996 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009999 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010000 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010001 }
10002
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010003 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10004 error = TRUE;
10005
10006 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010007 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008 do
10009 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010010 if (rettv->v_type == VAR_STRING)
10011 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010012 fresult = find_file_in_path_option(first ? fname : NULL,
10013 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010014 0, first, path,
10015 find_what,
10016 curbuf->b_ffname,
10017 find_what == FINDFILE_DIR
10018 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010019 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010020
10021 if (fresult != NULL && rettv->v_type == VAR_LIST)
10022 list_append_string(rettv->vval.v_list, fresult, -1);
10023
10024 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010026
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010027 if (rettv->v_type == VAR_STRING)
10028 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010029#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010030}
10031
Bram Moolenaar33570922005-01-25 22:26:29 +000010032static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10033static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010034
10035/*
10036 * Implementation of map() and filter().
10037 */
10038 static void
10039filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 typval_T *argvars;
10041 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010042 int map;
10043{
10044 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010045 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010046 listitem_T *li, *nli;
10047 list_T *l = NULL;
10048 dictitem_T *di;
10049 hashtab_T *ht;
10050 hashitem_T *hi;
10051 dict_T *d = NULL;
10052 typval_T save_val;
10053 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010054 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010055 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010056 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010057 int save_did_emsg;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010058 int index = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010059
Bram Moolenaare9a41262005-01-15 22:18:47 +000010060 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010061 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010062 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010063 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010064 return;
10065 }
10066 else if (argvars[0].v_type == VAR_DICT)
10067 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010068 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010069 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010070 return;
10071 }
10072 else
10073 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010074 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010075 return;
10076 }
10077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010078 expr = get_tv_string_buf_chk(&argvars[1], buf);
10079 /* On type errors, the preceding call has already displayed an error
10080 * message. Avoid a misleading error message for an empty string that
10081 * was not passed as argument. */
10082 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 prepare_vimvar(VV_VAL, &save_val);
10085 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010086
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010087 /* We reset "did_emsg" to be able to detect whether an error
10088 * occurred during evaluation of the expression. */
10089 save_did_emsg = did_emsg;
10090 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010091
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010092 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010095 vimvars[VV_KEY].vv_type = VAR_STRING;
10096
10097 ht = &d->dv_hashtab;
10098 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010099 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 if (!HASHITEM_EMPTY(hi))
10103 {
10104 --todo;
10105 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010106 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 break;
10108 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010109 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010110 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 break;
10112 if (!map && rem)
10113 dictitem_remove(d, di);
10114 clear_tv(&vimvars[VV_KEY].vv_tv);
10115 }
10116 }
10117 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 }
10119 else
10120 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010121 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 for (li = l->lv_first; li != NULL; li = nli)
10124 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010125 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010126 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 nli = li->li_next;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010128 vimvars[VV_KEY].vv_nr = index;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010129 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010130 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010131 break;
10132 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 listitem_remove(l, li);
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010134 ++index;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010135 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010136 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010137
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010138 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010139 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010140
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010141 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010142 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010143
10144 copy_tv(&argvars[0], rettv);
10145}
10146
10147 static int
10148filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010149 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010150 char_u *expr;
10151 int map;
10152 int *remp;
10153{
Bram Moolenaar33570922005-01-25 22:26:29 +000010154 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010155 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010156 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010157
Bram Moolenaar33570922005-01-25 22:26:29 +000010158 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010159 s = expr;
10160 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010161 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010162 if (*s != NUL) /* check for trailing chars after expr */
10163 {
10164 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010165 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010166 }
10167 if (map)
10168 {
10169 /* map(): replace the list item value */
10170 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010171 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010172 *tv = rettv;
10173 }
10174 else
10175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 int error = FALSE;
10177
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010181 /* On type error, nothing has been removed; return FAIL to stop the
10182 * loop. The error message was given by get_tv_number_chk(). */
10183 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010184 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010185 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010186 retval = OK;
10187theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010188 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010189 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010190}
10191
10192/*
10193 * "filter()" function
10194 */
10195 static void
10196f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010197 typval_T *argvars;
10198 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010199{
10200 filter_map(argvars, rettv, FALSE);
10201}
10202
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010203/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010204 * "finddir({fname}[, {path}[, {count}]])" function
10205 */
10206 static void
10207f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010208 typval_T *argvars;
10209 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010210{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010211 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010212}
10213
10214/*
10215 * "findfile({fname}[, {path}[, {count}]])" function
10216 */
10217 static void
10218f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010219 typval_T *argvars;
10220 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010221{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010222 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010223}
10224
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010225#ifdef FEAT_FLOAT
10226/*
10227 * "float2nr({float})" function
10228 */
10229 static void
10230f_float2nr(argvars, rettv)
10231 typval_T *argvars;
10232 typval_T *rettv;
10233{
10234 float_T f;
10235
10236 if (get_float_arg(argvars, &f) == OK)
10237 {
10238 if (f < -0x7fffffff)
10239 rettv->vval.v_number = -0x7fffffff;
10240 else if (f > 0x7fffffff)
10241 rettv->vval.v_number = 0x7fffffff;
10242 else
10243 rettv->vval.v_number = (varnumber_T)f;
10244 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010245}
10246
10247/*
10248 * "floor({float})" function
10249 */
10250 static void
10251f_floor(argvars, rettv)
10252 typval_T *argvars;
10253 typval_T *rettv;
10254{
10255 float_T f;
10256
10257 rettv->v_type = VAR_FLOAT;
10258 if (get_float_arg(argvars, &f) == OK)
10259 rettv->vval.v_float = floor(f);
10260 else
10261 rettv->vval.v_float = 0.0;
10262}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010263
10264/*
10265 * "fmod()" function
10266 */
10267 static void
10268f_fmod(argvars, rettv)
10269 typval_T *argvars;
10270 typval_T *rettv;
10271{
10272 float_T fx, fy;
10273
10274 rettv->v_type = VAR_FLOAT;
10275 if (get_float_arg(argvars, &fx) == OK
10276 && get_float_arg(&argvars[1], &fy) == OK)
10277 rettv->vval.v_float = fmod(fx, fy);
10278 else
10279 rettv->vval.v_float = 0.0;
10280}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010281#endif
10282
Bram Moolenaar0d660222005-01-07 21:51:51 +000010283/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010284 * "fnameescape({string})" function
10285 */
10286 static void
10287f_fnameescape(argvars, rettv)
10288 typval_T *argvars;
10289 typval_T *rettv;
10290{
10291 rettv->vval.v_string = vim_strsave_fnameescape(
10292 get_tv_string(&argvars[0]), FALSE);
10293 rettv->v_type = VAR_STRING;
10294}
10295
10296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 * "fnamemodify({fname}, {mods})" function
10298 */
10299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010300f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 typval_T *argvars;
10302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303{
10304 char_u *fname;
10305 char_u *mods;
10306 int usedlen = 0;
10307 int len;
10308 char_u *fbuf = NULL;
10309 char_u buf[NUMBUFLEN];
10310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 fname = get_tv_string_chk(&argvars[0]);
10312 mods = get_tv_string_buf_chk(&argvars[1], buf);
10313 if (fname == NULL || mods == NULL)
10314 fname = NULL;
10315 else
10316 {
10317 len = (int)STRLEN(fname);
10318 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010323 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 vim_free(fbuf);
10327}
10328
Bram Moolenaar33570922005-01-25 22:26:29 +000010329static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330
10331/*
10332 * "foldclosed()" function
10333 */
10334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010335foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010336 typval_T *argvars;
10337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 int end;
10339{
10340#ifdef FEAT_FOLDING
10341 linenr_T lnum;
10342 linenr_T first, last;
10343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10346 {
10347 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10348 {
10349 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010352 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 return;
10354 }
10355 }
10356#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358}
10359
10360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010361 * "foldclosed()" function
10362 */
10363 static void
10364f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010365 typval_T *argvars;
10366 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010367{
10368 foldclosed_both(argvars, rettv, FALSE);
10369}
10370
10371/*
10372 * "foldclosedend()" function
10373 */
10374 static void
10375f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010376 typval_T *argvars;
10377 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010378{
10379 foldclosed_both(argvars, rettv, TRUE);
10380}
10381
10382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 * "foldlevel()" function
10384 */
10385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010387 typval_T *argvars;
10388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389{
10390#ifdef FEAT_FOLDING
10391 linenr_T lnum;
10392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397}
10398
10399/*
10400 * "foldtext()" function
10401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010403f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010404 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
10407#ifdef FEAT_FOLDING
10408 linenr_T lnum;
10409 char_u *s;
10410 char_u *r;
10411 int len;
10412 char *txt;
10413#endif
10414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010415 rettv->v_type = VAR_STRING;
10416 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010418 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10419 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10420 <= curbuf->b_ml.ml_line_count
10421 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 {
10423 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10425 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 {
10427 if (!linewhite(lnum))
10428 break;
10429 ++lnum;
10430 }
10431
10432 /* Find interesting text in this line. */
10433 s = skipwhite(ml_get(lnum));
10434 /* skip C comment-start */
10435 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010438 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010439 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010440 {
10441 s = skipwhite(ml_get(lnum + 1));
10442 if (*s == '*')
10443 s = skipwhite(s + 1);
10444 }
10445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 txt = _("+-%s%3ld lines: ");
10447 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010448 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 + 20 /* for %3ld */
10450 + STRLEN(s))); /* concatenated */
10451 if (r != NULL)
10452 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010453 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10454 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10455 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 len = (int)STRLEN(r);
10457 STRCAT(r, s);
10458 /* remove 'foldmarker' and 'commentstring' */
10459 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 }
10462 }
10463#endif
10464}
10465
10466/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010467 * "foldtextresult(lnum)" function
10468 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010471 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010472 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010473{
10474#ifdef FEAT_FOLDING
10475 linenr_T lnum;
10476 char_u *text;
10477 char_u buf[51];
10478 foldinfo_T foldinfo;
10479 int fold_count;
10480#endif
10481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010482 rettv->v_type = VAR_STRING;
10483 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010484#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 /* treat illegal types and illegal string values for {lnum} the same */
10487 if (lnum < 0)
10488 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010489 fold_count = foldedCount(curwin, lnum, &foldinfo);
10490 if (fold_count > 0)
10491 {
10492 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10493 &foldinfo, buf);
10494 if (text == buf)
10495 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010496 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010497 }
10498#endif
10499}
10500
10501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 * "foreground()" function
10503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010506 typval_T *argvars UNUSED;
10507 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509#ifdef FEAT_GUI
10510 if (gui.in_use)
10511 gui_mch_set_foreground();
10512#else
10513# ifdef WIN32
10514 win32_set_foreground();
10515# endif
10516#endif
10517}
10518
10519/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010520 * "function()" function
10521 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010524 typval_T *argvars;
10525 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010526{
10527 char_u *s;
10528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010530 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010531 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010532 /* Don't check an autoload name for existence here. */
10533 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010534 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010535 else
10536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537 rettv->vval.v_string = vim_strsave(s);
10538 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010539 }
10540}
10541
10542/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010543 * "garbagecollect()" function
10544 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010545 static void
10546f_garbagecollect(argvars, rettv)
10547 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010548 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010549{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010550 /* This is postponed until we are back at the toplevel, because we may be
10551 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10552 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010553
10554 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10555 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010556}
10557
10558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010559 * "get()" function
10560 */
10561 static void
10562f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010563 typval_T *argvars;
10564 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010565{
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 listitem_T *li;
10567 list_T *l;
10568 dictitem_T *di;
10569 dict_T *d;
10570 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010571
Bram Moolenaare9a41262005-01-15 22:18:47 +000010572 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010573 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 int error = FALSE;
10577
10578 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10579 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010581 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 else if (argvars[0].v_type == VAR_DICT)
10584 {
10585 if ((d = argvars[0].vval.v_dict) != NULL)
10586 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010587 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 if (di != NULL)
10589 tv = &di->di_tv;
10590 }
10591 }
10592 else
10593 EMSG2(_(e_listdictarg), "get()");
10594
10595 if (tv == NULL)
10596 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010598 copy_tv(&argvars[2], rettv);
10599 }
10600 else
10601 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010602}
10603
Bram Moolenaar342337a2005-07-21 21:11:17 +000010604static 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 +000010605
10606/*
10607 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010608 * Return a range (from start to end) of lines in rettv from the specified
10609 * buffer.
10610 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010611 */
10612 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010613get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010614 buf_T *buf;
10615 linenr_T start;
10616 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010617 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010618 typval_T *rettv;
10619{
10620 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010621
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010622 if (retlist && rettv_list_alloc(rettv) == FAIL)
10623 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010624
10625 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10626 return;
10627
10628 if (!retlist)
10629 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010630 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10631 p = ml_get_buf(buf, start, FALSE);
10632 else
10633 p = (char_u *)"";
10634
10635 rettv->v_type = VAR_STRING;
10636 rettv->vval.v_string = vim_strsave(p);
10637 }
10638 else
10639 {
10640 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010641 return;
10642
10643 if (start < 1)
10644 start = 1;
10645 if (end > buf->b_ml.ml_line_count)
10646 end = buf->b_ml.ml_line_count;
10647 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010648 if (list_append_string(rettv->vval.v_list,
10649 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010650 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010651 }
10652}
10653
10654/*
10655 * "getbufline()" function
10656 */
10657 static void
10658f_getbufline(argvars, rettv)
10659 typval_T *argvars;
10660 typval_T *rettv;
10661{
10662 linenr_T lnum;
10663 linenr_T end;
10664 buf_T *buf;
10665
10666 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10667 ++emsg_off;
10668 buf = get_buf_tv(&argvars[0]);
10669 --emsg_off;
10670
Bram Moolenaar661b1822005-07-28 22:36:45 +000010671 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010672 if (argvars[2].v_type == VAR_UNKNOWN)
10673 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010674 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010675 end = get_tv_lnum_buf(&argvars[2], buf);
10676
Bram Moolenaar342337a2005-07-21 21:11:17 +000010677 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010678}
10679
Bram Moolenaar0d660222005-01-07 21:51:51 +000010680/*
10681 * "getbufvar()" function
10682 */
10683 static void
10684f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010685 typval_T *argvars;
10686 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010687{
10688 buf_T *buf;
10689 buf_T *save_curbuf;
10690 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10694 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010695 ++emsg_off;
10696 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010697
10698 rettv->v_type = VAR_STRING;
10699 rettv->vval.v_string = NULL;
10700
10701 if (buf != NULL && varname != NULL)
10702 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010703 /* set curbuf to be our buf, temporarily */
10704 save_curbuf = curbuf;
10705 curbuf = buf;
10706
Bram Moolenaar0d660222005-01-07 21:51:51 +000010707 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010708 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010709 else
10710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 if (*varname == NUL)
10712 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10713 * scope prefix before the NUL byte is required by
10714 * find_var_in_ht(). */
10715 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010717 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010718 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010720 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010721
10722 /* restore previous notion of curbuf */
10723 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010724 }
10725
10726 --emsg_off;
10727}
10728
10729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 * "getchar()" function
10731 */
10732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010734 typval_T *argvars;
10735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736{
10737 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010740 /* Position the cursor. Needed after a message that ends in a space. */
10741 windgoto(msg_row, msg_col);
10742
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 ++no_mapping;
10744 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010745 for (;;)
10746 {
10747 if (argvars[0].v_type == VAR_UNKNOWN)
10748 /* getchar(): blocking wait. */
10749 n = safe_vgetc();
10750 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10751 /* getchar(1): only check if char avail */
10752 n = vpeekc();
10753 else if (error || vpeekc() == NUL)
10754 /* illegal argument or getchar(0) and no char avail: return zero */
10755 n = 0;
10756 else
10757 /* getchar(0) and char avail: return char */
10758 n = safe_vgetc();
10759 if (n == K_IGNORE)
10760 continue;
10761 break;
10762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 --no_mapping;
10764 --allow_keys;
10765
Bram Moolenaar219b8702006-11-01 14:32:36 +000010766 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10767 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10768 vimvars[VV_MOUSE_COL].vv_nr = 0;
10769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 if (IS_SPECIAL(n) || mod_mask != 0)
10772 {
10773 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10774 int i = 0;
10775
10776 /* Turn a special key into three bytes, plus modifier. */
10777 if (mod_mask != 0)
10778 {
10779 temp[i++] = K_SPECIAL;
10780 temp[i++] = KS_MODIFIER;
10781 temp[i++] = mod_mask;
10782 }
10783 if (IS_SPECIAL(n))
10784 {
10785 temp[i++] = K_SPECIAL;
10786 temp[i++] = K_SECOND(n);
10787 temp[i++] = K_THIRD(n);
10788 }
10789#ifdef FEAT_MBYTE
10790 else if (has_mbyte)
10791 i += (*mb_char2bytes)(n, temp + i);
10792#endif
10793 else
10794 temp[i++] = n;
10795 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->v_type = VAR_STRING;
10797 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010798
10799#ifdef FEAT_MOUSE
10800 if (n == K_LEFTMOUSE
10801 || n == K_LEFTMOUSE_NM
10802 || n == K_LEFTDRAG
10803 || n == K_LEFTRELEASE
10804 || n == K_LEFTRELEASE_NM
10805 || n == K_MIDDLEMOUSE
10806 || n == K_MIDDLEDRAG
10807 || n == K_MIDDLERELEASE
10808 || n == K_RIGHTMOUSE
10809 || n == K_RIGHTDRAG
10810 || n == K_RIGHTRELEASE
10811 || n == K_X1MOUSE
10812 || n == K_X1DRAG
10813 || n == K_X1RELEASE
10814 || n == K_X2MOUSE
10815 || n == K_X2DRAG
10816 || n == K_X2RELEASE
10817 || n == K_MOUSEDOWN
10818 || n == K_MOUSEUP)
10819 {
10820 int row = mouse_row;
10821 int col = mouse_col;
10822 win_T *win;
10823 linenr_T lnum;
10824# ifdef FEAT_WINDOWS
10825 win_T *wp;
10826# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010827 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010828
10829 if (row >= 0 && col >= 0)
10830 {
10831 /* Find the window at the mouse coordinates and compute the
10832 * text position. */
10833 win = mouse_find_win(&row, &col);
10834 (void)mouse_comp_pos(win, &row, &col, &lnum);
10835# ifdef FEAT_WINDOWS
10836 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010837 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010838# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010839 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010840 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10841 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10842 }
10843 }
10844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 }
10846}
10847
10848/*
10849 * "getcharmod()" function
10850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857}
10858
10859/*
10860 * "getcmdline()" function
10861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010864 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->v_type = VAR_STRING;
10868 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869}
10870
10871/*
10872 * "getcmdpos()" function
10873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010876 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880}
10881
10882/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010883 * "getcmdtype()" function
10884 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010885 static void
10886f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010887 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010888 typval_T *rettv;
10889{
10890 rettv->v_type = VAR_STRING;
10891 rettv->vval.v_string = alloc(2);
10892 if (rettv->vval.v_string != NULL)
10893 {
10894 rettv->vval.v_string[0] = get_cmdline_type();
10895 rettv->vval.v_string[1] = NUL;
10896 }
10897}
10898
10899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 * "getcwd()" function
10901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
10907 char_u cwd[MAXPATHL];
10908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 else
10913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010916 if (rettv->vval.v_string != NULL)
10917 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918#endif
10919 }
10920}
10921
10922/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010923 * "getfontname()" function
10924 */
10925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010927 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010928 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010929{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930 rettv->v_type = VAR_STRING;
10931 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010932#ifdef FEAT_GUI
10933 if (gui.in_use)
10934 {
10935 GuiFont font;
10936 char_u *name = NULL;
10937
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010938 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010939 {
10940 /* Get the "Normal" font. Either the name saved by
10941 * hl_set_font_name() or from the font ID. */
10942 font = gui.norm_font;
10943 name = hl_get_font_name();
10944 }
10945 else
10946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010948 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10949 return;
10950 font = gui_mch_get_font(name, FALSE);
10951 if (font == NOFONT)
10952 return; /* Invalid font name, return empty string. */
10953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010955 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010956 gui_mch_free_font(font);
10957 }
10958#endif
10959}
10960
10961/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010962 * "getfperm({fname})" function
10963 */
10964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 typval_T *argvars;
10967 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010968{
10969 char_u *fname;
10970 struct stat st;
10971 char_u *perm = NULL;
10972 char_u flags[] = "rwx";
10973 int i;
10974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010978 if (mch_stat((char *)fname, &st) >= 0)
10979 {
10980 perm = vim_strsave((char_u *)"---------");
10981 if (perm != NULL)
10982 {
10983 for (i = 0; i < 9; i++)
10984 {
10985 if (st.st_mode & (1 << (8 - i)))
10986 perm[i] = flags[i % 3];
10987 }
10988 }
10989 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010991}
10992
10993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 * "getfsize({fname})" function
10995 */
10996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010997f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010998 typval_T *argvars;
10999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000{
11001 char_u *fname;
11002 struct stat st;
11003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007
11008 if (mch_stat((char *)fname, &st) >= 0)
11009 {
11010 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011015
11016 /* non-perfect check for overflow */
11017 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11018 rettv->vval.v_number = -2;
11019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 }
11021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023}
11024
11025/*
11026 * "getftime({fname})" function
11027 */
11028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011030 typval_T *argvars;
11031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
11033 char_u *fname;
11034 struct stat st;
11035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037
11038 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042}
11043
11044/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011045 * "getftype({fname})" function
11046 */
11047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011049 typval_T *argvars;
11050 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011051{
11052 char_u *fname;
11053 struct stat st;
11054 char_u *type = NULL;
11055 char *t;
11056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011060 if (mch_lstat((char *)fname, &st) >= 0)
11061 {
11062#ifdef S_ISREG
11063 if (S_ISREG(st.st_mode))
11064 t = "file";
11065 else if (S_ISDIR(st.st_mode))
11066 t = "dir";
11067# ifdef S_ISLNK
11068 else if (S_ISLNK(st.st_mode))
11069 t = "link";
11070# endif
11071# ifdef S_ISBLK
11072 else if (S_ISBLK(st.st_mode))
11073 t = "bdev";
11074# endif
11075# ifdef S_ISCHR
11076 else if (S_ISCHR(st.st_mode))
11077 t = "cdev";
11078# endif
11079# ifdef S_ISFIFO
11080 else if (S_ISFIFO(st.st_mode))
11081 t = "fifo";
11082# endif
11083# ifdef S_ISSOCK
11084 else if (S_ISSOCK(st.st_mode))
11085 t = "fifo";
11086# endif
11087 else
11088 t = "other";
11089#else
11090# ifdef S_IFMT
11091 switch (st.st_mode & S_IFMT)
11092 {
11093 case S_IFREG: t = "file"; break;
11094 case S_IFDIR: t = "dir"; break;
11095# ifdef S_IFLNK
11096 case S_IFLNK: t = "link"; break;
11097# endif
11098# ifdef S_IFBLK
11099 case S_IFBLK: t = "bdev"; break;
11100# endif
11101# ifdef S_IFCHR
11102 case S_IFCHR: t = "cdev"; break;
11103# endif
11104# ifdef S_IFIFO
11105 case S_IFIFO: t = "fifo"; break;
11106# endif
11107# ifdef S_IFSOCK
11108 case S_IFSOCK: t = "socket"; break;
11109# endif
11110 default: t = "other";
11111 }
11112# else
11113 if (mch_isdir(fname))
11114 t = "dir";
11115 else
11116 t = "file";
11117# endif
11118#endif
11119 type = vim_strsave((char_u *)t);
11120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011122}
11123
11124/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011125 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126 */
11127 static void
11128f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 typval_T *argvars;
11130 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131{
11132 linenr_T lnum;
11133 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011134 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011135
11136 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011137 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011138 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011139 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011140 retlist = FALSE;
11141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011142 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011143 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011144 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011145 retlist = TRUE;
11146 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011147
Bram Moolenaar342337a2005-07-21 21:11:17 +000011148 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149}
11150
11151/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011152 * "getmatches()" function
11153 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011154 static void
11155f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011156 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011157 typval_T *rettv;
11158{
11159#ifdef FEAT_SEARCH_EXTRA
11160 dict_T *dict;
11161 matchitem_T *cur = curwin->w_match_head;
11162
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011163 if (rettv_list_alloc(rettv) == OK)
11164 {
11165 while (cur != NULL)
11166 {
11167 dict = dict_alloc();
11168 if (dict == NULL)
11169 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011170 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11171 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11172 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11173 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11174 list_append_dict(rettv->vval.v_list, dict);
11175 cur = cur->next;
11176 }
11177 }
11178#endif
11179}
11180
11181/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011182 * "getpid()" function
11183 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011184 static void
11185f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011186 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011187 typval_T *rettv;
11188{
11189 rettv->vval.v_number = mch_get_pid();
11190}
11191
11192/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011193 * "getpos(string)" function
11194 */
11195 static void
11196f_getpos(argvars, rettv)
11197 typval_T *argvars;
11198 typval_T *rettv;
11199{
11200 pos_T *fp;
11201 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011202 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011203
11204 if (rettv_list_alloc(rettv) == OK)
11205 {
11206 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011207 fp = var2fpos(&argvars[0], TRUE, &fnum);
11208 if (fnum != -1)
11209 list_append_number(l, (varnumber_T)fnum);
11210 else
11211 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011212 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11213 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011214 list_append_number(l, (fp != NULL)
11215 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011216 : (varnumber_T)0);
11217 list_append_number(l,
11218#ifdef FEAT_VIRTUALEDIT
11219 (fp != NULL) ? (varnumber_T)fp->coladd :
11220#endif
11221 (varnumber_T)0);
11222 }
11223 else
11224 rettv->vval.v_number = FALSE;
11225}
11226
11227/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011228 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011229 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011230 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011231f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011232 typval_T *argvars UNUSED;
11233 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011234{
11235#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011236 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011237#endif
11238
Bram Moolenaar2641f772005-03-25 21:58:17 +000011239#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011240 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011241 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011242 wp = NULL;
11243 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11244 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011245 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011246 if (wp == NULL)
11247 return;
11248 }
11249
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011250 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011251 }
11252#endif
11253}
11254
11255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 * "getreg()" function
11257 */
11258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262{
11263 char_u *strregname;
11264 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011265 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011266 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011268 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 strregname = get_tv_string_chk(&argvars[0]);
11271 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011272 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011273 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011276 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 regname = (strregname == NULL ? '"' : *strregname);
11278 if (regname == 0)
11279 regname = '"';
11280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011282 rettv->vval.v_string = error ? NULL :
11283 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284}
11285
11286/*
11287 * "getregtype()" function
11288 */
11289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011291 typval_T *argvars;
11292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
11294 char_u *strregname;
11295 int regname;
11296 char_u buf[NUMBUFLEN + 2];
11297 long reglen = 0;
11298
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011299 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 {
11301 strregname = get_tv_string_chk(&argvars[0]);
11302 if (strregname == NULL) /* type error; errmsg already given */
11303 {
11304 rettv->v_type = VAR_STRING;
11305 rettv->vval.v_string = NULL;
11306 return;
11307 }
11308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 else
11310 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011311 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312
11313 regname = (strregname == NULL ? '"' : *strregname);
11314 if (regname == 0)
11315 regname = '"';
11316
11317 buf[0] = NUL;
11318 buf[1] = NUL;
11319 switch (get_reg_type(regname, &reglen))
11320 {
11321 case MLINE: buf[0] = 'V'; break;
11322 case MCHAR: buf[0] = 'v'; break;
11323#ifdef FEAT_VISUAL
11324 case MBLOCK:
11325 buf[0] = Ctrl_V;
11326 sprintf((char *)buf + 1, "%ld", reglen + 1);
11327 break;
11328#endif
11329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 rettv->v_type = VAR_STRING;
11331 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332}
11333
11334/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011335 * "gettabvar()" function
11336 */
11337 static void
11338f_gettabvar(argvars, rettv)
11339 typval_T *argvars;
11340 typval_T *rettv;
11341{
11342 tabpage_T *tp;
11343 dictitem_T *v;
11344 char_u *varname;
11345
11346 rettv->v_type = VAR_STRING;
11347 rettv->vval.v_string = NULL;
11348
11349 varname = get_tv_string_chk(&argvars[1]);
11350 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11351 if (tp != NULL && varname != NULL)
11352 {
11353 /* look up the variable */
11354 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11355 if (v != NULL)
11356 copy_tv(&v->di_tv, rettv);
11357 }
11358}
11359
11360/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011361 * "gettabwinvar()" function
11362 */
11363 static void
11364f_gettabwinvar(argvars, rettv)
11365 typval_T *argvars;
11366 typval_T *rettv;
11367{
11368 getwinvar(argvars, rettv, 1);
11369}
11370
11371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 * "getwinposx()" function
11373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011376 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380#ifdef FEAT_GUI
11381 if (gui.in_use)
11382 {
11383 int x, y;
11384
11385 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 }
11388#endif
11389}
11390
11391/*
11392 * "getwinposy()" function
11393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400#ifdef FEAT_GUI
11401 if (gui.in_use)
11402 {
11403 int x, y;
11404
11405 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 }
11408#endif
11409}
11410
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011411/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011412 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011413 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011414 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011415find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011416 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011417 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011418{
11419#ifdef FEAT_WINDOWS
11420 win_T *wp;
11421#endif
11422 int nr;
11423
11424 nr = get_tv_number_chk(vp, NULL);
11425
11426#ifdef FEAT_WINDOWS
11427 if (nr < 0)
11428 return NULL;
11429 if (nr == 0)
11430 return curwin;
11431
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011432 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11433 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011434 if (--nr <= 0)
11435 break;
11436 return wp;
11437#else
11438 if (nr == 0 || nr == 1)
11439 return curwin;
11440 return NULL;
11441#endif
11442}
11443
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444/*
11445 * "getwinvar()" function
11446 */
11447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011449 typval_T *argvars;
11450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011452 getwinvar(argvars, rettv, 0);
11453}
11454
11455/*
11456 * getwinvar() and gettabwinvar()
11457 */
11458 static void
11459getwinvar(argvars, rettv, off)
11460 typval_T *argvars;
11461 typval_T *rettv;
11462 int off; /* 1 for gettabwinvar() */
11463{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 win_T *win, *oldcurwin;
11465 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011467 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011469#ifdef FEAT_WINDOWS
11470 if (off == 1)
11471 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11472 else
11473 tp = curtab;
11474#endif
11475 win = find_win_by_nr(&argvars[off], tp);
11476 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479 rettv->v_type = VAR_STRING;
11480 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481
11482 if (win != NULL && varname != NULL)
11483 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011484 /* Set curwin to be our win, temporarily. Also set curbuf, so
11485 * that we can get buffer-local options. */
11486 oldcurwin = curwin;
11487 curwin = win;
11488 curbuf = win->w_buffer;
11489
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 else
11493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494 if (*varname == NUL)
11495 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11496 * scope prefix before the NUL byte is required by
11497 * find_var_in_ht(). */
11498 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011500 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011504
11505 /* restore previous notion of curwin */
11506 curwin = oldcurwin;
11507 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 }
11509
11510 --emsg_off;
11511}
11512
11513/*
11514 * "glob()" function
11515 */
11516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011518 typval_T *argvars;
11519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011521 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011523 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011525 /* When the optional second argument is non-zero, don't remove matches
11526 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11527 if (argvars[1].v_type != VAR_UNKNOWN
11528 && get_tv_number_chk(&argvars[1], &error))
11529 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011531 if (!error)
11532 {
11533 ExpandInit(&xpc);
11534 xpc.xp_context = EXPAND_FILES;
11535 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11536 NULL, flags, WILD_ALL);
11537 }
11538 else
11539 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540}
11541
11542/*
11543 * "globpath()" function
11544 */
11545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011547 typval_T *argvars;
11548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011550 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011552 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011553 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011555 /* When the optional second argument is non-zero, don't remove matches
11556 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11557 if (argvars[2].v_type != VAR_UNKNOWN
11558 && get_tv_number_chk(&argvars[2], &error))
11559 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011561 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011562 rettv->vval.v_string = NULL;
11563 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011564 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11565 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566}
11567
11568/*
11569 * "has()" function
11570 */
11571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011573 typval_T *argvars;
11574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575{
11576 int i;
11577 char_u *name;
11578 int n = FALSE;
11579 static char *(has_list[]) =
11580 {
11581#ifdef AMIGA
11582 "amiga",
11583# ifdef FEAT_ARP
11584 "arp",
11585# endif
11586#endif
11587#ifdef __BEOS__
11588 "beos",
11589#endif
11590#ifdef MSDOS
11591# ifdef DJGPP
11592 "dos32",
11593# else
11594 "dos16",
11595# endif
11596#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011597#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 "mac",
11599#endif
11600#if defined(MACOS_X_UNIX)
11601 "macunix",
11602#endif
11603#ifdef OS2
11604 "os2",
11605#endif
11606#ifdef __QNX__
11607 "qnx",
11608#endif
11609#ifdef RISCOS
11610 "riscos",
11611#endif
11612#ifdef UNIX
11613 "unix",
11614#endif
11615#ifdef VMS
11616 "vms",
11617#endif
11618#ifdef WIN16
11619 "win16",
11620#endif
11621#ifdef WIN32
11622 "win32",
11623#endif
11624#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11625 "win32unix",
11626#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011627#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 "win64",
11629#endif
11630#ifdef EBCDIC
11631 "ebcdic",
11632#endif
11633#ifndef CASE_INSENSITIVE_FILENAME
11634 "fname_case",
11635#endif
11636#ifdef FEAT_ARABIC
11637 "arabic",
11638#endif
11639#ifdef FEAT_AUTOCMD
11640 "autocmd",
11641#endif
11642#ifdef FEAT_BEVAL
11643 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011644# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11645 "balloon_multiline",
11646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647#endif
11648#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11649 "builtin_terms",
11650# ifdef ALL_BUILTIN_TCAPS
11651 "all_builtin_terms",
11652# endif
11653#endif
11654#ifdef FEAT_BYTEOFF
11655 "byte_offset",
11656#endif
11657#ifdef FEAT_CINDENT
11658 "cindent",
11659#endif
11660#ifdef FEAT_CLIENTSERVER
11661 "clientserver",
11662#endif
11663#ifdef FEAT_CLIPBOARD
11664 "clipboard",
11665#endif
11666#ifdef FEAT_CMDL_COMPL
11667 "cmdline_compl",
11668#endif
11669#ifdef FEAT_CMDHIST
11670 "cmdline_hist",
11671#endif
11672#ifdef FEAT_COMMENTS
11673 "comments",
11674#endif
11675#ifdef FEAT_CRYPT
11676 "cryptv",
11677#endif
11678#ifdef FEAT_CSCOPE
11679 "cscope",
11680#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011681#ifdef CURSOR_SHAPE
11682 "cursorshape",
11683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684#ifdef DEBUG
11685 "debug",
11686#endif
11687#ifdef FEAT_CON_DIALOG
11688 "dialog_con",
11689#endif
11690#ifdef FEAT_GUI_DIALOG
11691 "dialog_gui",
11692#endif
11693#ifdef FEAT_DIFF
11694 "diff",
11695#endif
11696#ifdef FEAT_DIGRAPHS
11697 "digraphs",
11698#endif
11699#ifdef FEAT_DND
11700 "dnd",
11701#endif
11702#ifdef FEAT_EMACS_TAGS
11703 "emacs_tags",
11704#endif
11705 "eval", /* always present, of course! */
11706#ifdef FEAT_EX_EXTRA
11707 "ex_extra",
11708#endif
11709#ifdef FEAT_SEARCH_EXTRA
11710 "extra_search",
11711#endif
11712#ifdef FEAT_FKMAP
11713 "farsi",
11714#endif
11715#ifdef FEAT_SEARCHPATH
11716 "file_in_path",
11717#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011718#if defined(UNIX) && !defined(USE_SYSTEM)
11719 "filterpipe",
11720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721#ifdef FEAT_FIND_ID
11722 "find_in_path",
11723#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011724#ifdef FEAT_FLOAT
11725 "float",
11726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727#ifdef FEAT_FOLDING
11728 "folding",
11729#endif
11730#ifdef FEAT_FOOTER
11731 "footer",
11732#endif
11733#if !defined(USE_SYSTEM) && defined(UNIX)
11734 "fork",
11735#endif
11736#ifdef FEAT_GETTEXT
11737 "gettext",
11738#endif
11739#ifdef FEAT_GUI
11740 "gui",
11741#endif
11742#ifdef FEAT_GUI_ATHENA
11743# ifdef FEAT_GUI_NEXTAW
11744 "gui_neXtaw",
11745# else
11746 "gui_athena",
11747# endif
11748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749#ifdef FEAT_GUI_GTK
11750 "gui_gtk",
11751# ifdef HAVE_GTK2
11752 "gui_gtk2",
11753# endif
11754#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011755#ifdef FEAT_GUI_GNOME
11756 "gui_gnome",
11757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758#ifdef FEAT_GUI_MAC
11759 "gui_mac",
11760#endif
11761#ifdef FEAT_GUI_MOTIF
11762 "gui_motif",
11763#endif
11764#ifdef FEAT_GUI_PHOTON
11765 "gui_photon",
11766#endif
11767#ifdef FEAT_GUI_W16
11768 "gui_win16",
11769#endif
11770#ifdef FEAT_GUI_W32
11771 "gui_win32",
11772#endif
11773#ifdef FEAT_HANGULIN
11774 "hangul_input",
11775#endif
11776#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11777 "iconv",
11778#endif
11779#ifdef FEAT_INS_EXPAND
11780 "insert_expand",
11781#endif
11782#ifdef FEAT_JUMPLIST
11783 "jumplist",
11784#endif
11785#ifdef FEAT_KEYMAP
11786 "keymap",
11787#endif
11788#ifdef FEAT_LANGMAP
11789 "langmap",
11790#endif
11791#ifdef FEAT_LIBCALL
11792 "libcall",
11793#endif
11794#ifdef FEAT_LINEBREAK
11795 "linebreak",
11796#endif
11797#ifdef FEAT_LISP
11798 "lispindent",
11799#endif
11800#ifdef FEAT_LISTCMDS
11801 "listcmds",
11802#endif
11803#ifdef FEAT_LOCALMAP
11804 "localmap",
11805#endif
11806#ifdef FEAT_MENU
11807 "menu",
11808#endif
11809#ifdef FEAT_SESSION
11810 "mksession",
11811#endif
11812#ifdef FEAT_MODIFY_FNAME
11813 "modify_fname",
11814#endif
11815#ifdef FEAT_MOUSE
11816 "mouse",
11817#endif
11818#ifdef FEAT_MOUSESHAPE
11819 "mouseshape",
11820#endif
11821#if defined(UNIX) || defined(VMS)
11822# ifdef FEAT_MOUSE_DEC
11823 "mouse_dec",
11824# endif
11825# ifdef FEAT_MOUSE_GPM
11826 "mouse_gpm",
11827# endif
11828# ifdef FEAT_MOUSE_JSB
11829 "mouse_jsbterm",
11830# endif
11831# ifdef FEAT_MOUSE_NET
11832 "mouse_netterm",
11833# endif
11834# ifdef FEAT_MOUSE_PTERM
11835 "mouse_pterm",
11836# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011837# ifdef FEAT_SYSMOUSE
11838 "mouse_sysmouse",
11839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840# ifdef FEAT_MOUSE_XTERM
11841 "mouse_xterm",
11842# endif
11843#endif
11844#ifdef FEAT_MBYTE
11845 "multi_byte",
11846#endif
11847#ifdef FEAT_MBYTE_IME
11848 "multi_byte_ime",
11849#endif
11850#ifdef FEAT_MULTI_LANG
11851 "multi_lang",
11852#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011853#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011854#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011855 "mzscheme",
11856#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858#ifdef FEAT_OLE
11859 "ole",
11860#endif
11861#ifdef FEAT_OSFILETYPE
11862 "osfiletype",
11863#endif
11864#ifdef FEAT_PATH_EXTRA
11865 "path_extra",
11866#endif
11867#ifdef FEAT_PERL
11868#ifndef DYNAMIC_PERL
11869 "perl",
11870#endif
11871#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011872#ifdef FEAT_PERSISTENT_UNDO
11873 "persistent_undo",
11874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875#ifdef FEAT_PYTHON
11876#ifndef DYNAMIC_PYTHON
11877 "python",
11878#endif
11879#endif
11880#ifdef FEAT_POSTSCRIPT
11881 "postscript",
11882#endif
11883#ifdef FEAT_PRINTER
11884 "printer",
11885#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011886#ifdef FEAT_PROFILE
11887 "profile",
11888#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011889#ifdef FEAT_RELTIME
11890 "reltime",
11891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892#ifdef FEAT_QUICKFIX
11893 "quickfix",
11894#endif
11895#ifdef FEAT_RIGHTLEFT
11896 "rightleft",
11897#endif
11898#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11899 "ruby",
11900#endif
11901#ifdef FEAT_SCROLLBIND
11902 "scrollbind",
11903#endif
11904#ifdef FEAT_CMDL_INFO
11905 "showcmd",
11906 "cmdline_info",
11907#endif
11908#ifdef FEAT_SIGNS
11909 "signs",
11910#endif
11911#ifdef FEAT_SMARTINDENT
11912 "smartindent",
11913#endif
11914#ifdef FEAT_SNIFF
11915 "sniff",
11916#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000011917#ifdef STARTUPTIME
11918 "startuptime",
11919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920#ifdef FEAT_STL_OPT
11921 "statusline",
11922#endif
11923#ifdef FEAT_SUN_WORKSHOP
11924 "sun_workshop",
11925#endif
11926#ifdef FEAT_NETBEANS_INTG
11927 "netbeans_intg",
11928#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011929#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011930 "spell",
11931#endif
11932#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 "syntax",
11934#endif
11935#if defined(USE_SYSTEM) || !defined(UNIX)
11936 "system",
11937#endif
11938#ifdef FEAT_TAG_BINS
11939 "tag_binary",
11940#endif
11941#ifdef FEAT_TAG_OLDSTATIC
11942 "tag_old_static",
11943#endif
11944#ifdef FEAT_TAG_ANYWHITE
11945 "tag_any_white",
11946#endif
11947#ifdef FEAT_TCL
11948# ifndef DYNAMIC_TCL
11949 "tcl",
11950# endif
11951#endif
11952#ifdef TERMINFO
11953 "terminfo",
11954#endif
11955#ifdef FEAT_TERMRESPONSE
11956 "termresponse",
11957#endif
11958#ifdef FEAT_TEXTOBJ
11959 "textobjects",
11960#endif
11961#ifdef HAVE_TGETENT
11962 "tgetent",
11963#endif
11964#ifdef FEAT_TITLE
11965 "title",
11966#endif
11967#ifdef FEAT_TOOLBAR
11968 "toolbar",
11969#endif
11970#ifdef FEAT_USR_CMDS
11971 "user-commands", /* was accidentally included in 5.4 */
11972 "user_commands",
11973#endif
11974#ifdef FEAT_VIMINFO
11975 "viminfo",
11976#endif
11977#ifdef FEAT_VERTSPLIT
11978 "vertsplit",
11979#endif
11980#ifdef FEAT_VIRTUALEDIT
11981 "virtualedit",
11982#endif
11983#ifdef FEAT_VISUAL
11984 "visual",
11985#endif
11986#ifdef FEAT_VISUALEXTRA
11987 "visualextra",
11988#endif
11989#ifdef FEAT_VREPLACE
11990 "vreplace",
11991#endif
11992#ifdef FEAT_WILDIGN
11993 "wildignore",
11994#endif
11995#ifdef FEAT_WILDMENU
11996 "wildmenu",
11997#endif
11998#ifdef FEAT_WINDOWS
11999 "windows",
12000#endif
12001#ifdef FEAT_WAK
12002 "winaltkeys",
12003#endif
12004#ifdef FEAT_WRITEBACKUP
12005 "writebackup",
12006#endif
12007#ifdef FEAT_XIM
12008 "xim",
12009#endif
12010#ifdef FEAT_XFONTSET
12011 "xfontset",
12012#endif
12013#ifdef USE_XSMP
12014 "xsmp",
12015#endif
12016#ifdef USE_XSMP_INTERACT
12017 "xsmp_interact",
12018#endif
12019#ifdef FEAT_XCLIPBOARD
12020 "xterm_clipboard",
12021#endif
12022#ifdef FEAT_XTERM_SAVE
12023 "xterm_save",
12024#endif
12025#if defined(UNIX) && defined(FEAT_X11)
12026 "X11",
12027#endif
12028 NULL
12029 };
12030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 for (i = 0; has_list[i] != NULL; ++i)
12033 if (STRICMP(name, has_list[i]) == 0)
12034 {
12035 n = TRUE;
12036 break;
12037 }
12038
12039 if (n == FALSE)
12040 {
12041 if (STRNICMP(name, "patch", 5) == 0)
12042 n = has_patch(atoi((char *)name + 5));
12043 else if (STRICMP(name, "vim_starting") == 0)
12044 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012045#ifdef FEAT_MBYTE
12046 else if (STRICMP(name, "multi_byte_encoding") == 0)
12047 n = has_mbyte;
12048#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012049#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12050 else if (STRICMP(name, "balloon_multiline") == 0)
12051 n = multiline_balloon_available();
12052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053#ifdef DYNAMIC_TCL
12054 else if (STRICMP(name, "tcl") == 0)
12055 n = tcl_enabled(FALSE);
12056#endif
12057#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12058 else if (STRICMP(name, "iconv") == 0)
12059 n = iconv_enabled(FALSE);
12060#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012061#ifdef DYNAMIC_MZSCHEME
12062 else if (STRICMP(name, "mzscheme") == 0)
12063 n = mzscheme_enabled(FALSE);
12064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065#ifdef DYNAMIC_RUBY
12066 else if (STRICMP(name, "ruby") == 0)
12067 n = ruby_enabled(FALSE);
12068#endif
12069#ifdef DYNAMIC_PYTHON
12070 else if (STRICMP(name, "python") == 0)
12071 n = python_enabled(FALSE);
12072#endif
12073#ifdef DYNAMIC_PERL
12074 else if (STRICMP(name, "perl") == 0)
12075 n = perl_enabled(FALSE);
12076#endif
12077#ifdef FEAT_GUI
12078 else if (STRICMP(name, "gui_running") == 0)
12079 n = (gui.in_use || gui.starting);
12080# ifdef FEAT_GUI_W32
12081 else if (STRICMP(name, "gui_win32s") == 0)
12082 n = gui_is_win32s();
12083# endif
12084# ifdef FEAT_BROWSE
12085 else if (STRICMP(name, "browse") == 0)
12086 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12087# endif
12088#endif
12089#ifdef FEAT_SYN_HL
12090 else if (STRICMP(name, "syntax_items") == 0)
12091 n = syntax_present(curbuf);
12092#endif
12093#if defined(WIN3264)
12094 else if (STRICMP(name, "win95") == 0)
12095 n = mch_windows95();
12096#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012097#ifdef FEAT_NETBEANS_INTG
12098 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012099 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 }
12102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104}
12105
12106/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012107 * "has_key()" function
12108 */
12109 static void
12110f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012111 typval_T *argvars;
12112 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012113{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012114 if (argvars[0].v_type != VAR_DICT)
12115 {
12116 EMSG(_(e_dictreq));
12117 return;
12118 }
12119 if (argvars[0].vval.v_dict == NULL)
12120 return;
12121
12122 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012123 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012124}
12125
12126/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012127 * "haslocaldir()" function
12128 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012129 static void
12130f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012131 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012132 typval_T *rettv;
12133{
12134 rettv->vval.v_number = (curwin->w_localdir != NULL);
12135}
12136
12137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138 * "hasmapto()" function
12139 */
12140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012142 typval_T *argvars;
12143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144{
12145 char_u *name;
12146 char_u *mode;
12147 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012148 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012151 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 mode = (char_u *)"nvo";
12153 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012156 if (argvars[2].v_type != VAR_UNKNOWN)
12157 abbr = get_tv_number(&argvars[2]);
12158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159
Bram Moolenaar2c932302006-03-18 21:42:09 +000012160 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012161 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012163 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164}
12165
12166/*
12167 * "histadd()" function
12168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012171 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173{
12174#ifdef FEAT_CMDHIST
12175 int histype;
12176 char_u *str;
12177 char_u buf[NUMBUFLEN];
12178#endif
12179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 if (check_restricted() || check_secure())
12182 return;
12183#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012184 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12185 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 if (histype >= 0)
12187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012188 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 if (*str != NUL)
12190 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012191 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 return;
12195 }
12196 }
12197#endif
12198}
12199
12200/*
12201 * "histdel()" function
12202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012205 typval_T *argvars UNUSED;
12206 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207{
12208#ifdef FEAT_CMDHIST
12209 int n;
12210 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012211 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012213 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12214 if (str == NULL)
12215 n = 0;
12216 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012218 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012219 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012221 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012222 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 else
12224 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012225 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012226 get_tv_string_buf(&argvars[1], buf));
12227 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228#endif
12229}
12230
12231/*
12232 * "histget()" function
12233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012236 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238{
12239#ifdef FEAT_CMDHIST
12240 int type;
12241 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012242 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012244 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12245 if (str == NULL)
12246 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012248 {
12249 type = get_histtype(str);
12250 if (argvars[1].v_type == VAR_UNKNOWN)
12251 idx = get_history_idx(type);
12252 else
12253 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12254 /* -1 on type error */
12255 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261}
12262
12263/*
12264 * "histnr()" function
12265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012268 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270{
12271 int i;
12272
12273#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012274 char_u *history = get_tv_string_chk(&argvars[0]);
12275
12276 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 if (i >= HIST_CMD && i < HIST_COUNT)
12278 i = get_history_idx(i);
12279 else
12280#endif
12281 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012282 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283}
12284
12285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286 * "highlightID(name)" function
12287 */
12288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012289f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012290 typval_T *argvars;
12291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012293 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294}
12295
12296/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012297 * "highlight_exists()" function
12298 */
12299 static void
12300f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012301 typval_T *argvars;
12302 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012303{
12304 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12305}
12306
12307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 * "hostname()" function
12309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012312 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314{
12315 char_u hostname[256];
12316
12317 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012318 rettv->v_type = VAR_STRING;
12319 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320}
12321
12322/*
12323 * iconv() function
12324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012327 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329{
12330#ifdef FEAT_MBYTE
12331 char_u buf1[NUMBUFLEN];
12332 char_u buf2[NUMBUFLEN];
12333 char_u *from, *to, *str;
12334 vimconv_T vimconv;
12335#endif
12336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012337 rettv->v_type = VAR_STRING;
12338 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339
12340#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 str = get_tv_string(&argvars[0]);
12342 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12343 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 vimconv.vc_type = CONV_NONE;
12345 convert_setup(&vimconv, from, to);
12346
12347 /* If the encodings are equal, no conversion needed. */
12348 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012351 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
12353 convert_setup(&vimconv, NULL, NULL);
12354 vim_free(from);
12355 vim_free(to);
12356#endif
12357}
12358
12359/*
12360 * "indent()" function
12361 */
12362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012364 typval_T *argvars;
12365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366{
12367 linenr_T lnum;
12368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012376/*
12377 * "index()" function
12378 */
12379 static void
12380f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 typval_T *argvars;
12382 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012383{
Bram Moolenaar33570922005-01-25 22:26:29 +000012384 list_T *l;
12385 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012386 long idx = 0;
12387 int ic = FALSE;
12388
12389 rettv->vval.v_number = -1;
12390 if (argvars[0].v_type != VAR_LIST)
12391 {
12392 EMSG(_(e_listreq));
12393 return;
12394 }
12395 l = argvars[0].vval.v_list;
12396 if (l != NULL)
12397 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012398 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012399 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 int error = FALSE;
12402
Bram Moolenaar758711c2005-02-02 23:11:38 +000012403 /* Start at specified item. Use the cached index that list_find()
12404 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012406 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012407 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012408 ic = get_tv_number_chk(&argvars[3], &error);
12409 if (error)
12410 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012411 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012412
Bram Moolenaar758711c2005-02-02 23:11:38 +000012413 for ( ; item != NULL; item = item->li_next, ++idx)
12414 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012415 {
12416 rettv->vval.v_number = idx;
12417 break;
12418 }
12419 }
12420}
12421
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422static int inputsecret_flag = 0;
12423
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012424static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12425
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012427 * This function is used by f_input() and f_inputdialog() functions. The third
12428 * argument to f_input() specifies the type of completion to use at the
12429 * prompt. The third argument to f_inputdialog() specifies the value to return
12430 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 */
12432 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012433get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012434 typval_T *argvars;
12435 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012436 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012438 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 char_u *p = NULL;
12440 int c;
12441 char_u buf[NUMBUFLEN];
12442 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012443 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012444 int xp_type = EXPAND_NOTHING;
12445 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012447 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012448 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449
12450#ifdef NO_CONSOLE_INPUT
12451 /* While starting up, there is no place to enter text. */
12452 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454#endif
12455
12456 cmd_silent = FALSE; /* Want to see the prompt. */
12457 if (prompt != NULL)
12458 {
12459 /* Only the part of the message after the last NL is considered as
12460 * prompt for the command line */
12461 p = vim_strrchr(prompt, '\n');
12462 if (p == NULL)
12463 p = prompt;
12464 else
12465 {
12466 ++p;
12467 c = *p;
12468 *p = NUL;
12469 msg_start();
12470 msg_clr_eos();
12471 msg_puts_attr(prompt, echo_attr);
12472 msg_didout = FALSE;
12473 msg_starthere();
12474 *p = c;
12475 }
12476 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012478 if (argvars[1].v_type != VAR_UNKNOWN)
12479 {
12480 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12481 if (defstr != NULL)
12482 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012484 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012485 {
12486 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012487 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012488 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012489
Bram Moolenaar4463f292005-09-25 22:20:24 +000012490 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012491
Bram Moolenaar4463f292005-09-25 22:20:24 +000012492 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12493 if (xp_name == NULL)
12494 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012495
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012496 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012497
Bram Moolenaar4463f292005-09-25 22:20:24 +000012498 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12499 &xp_arg) == FAIL)
12500 return;
12501 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012502 }
12503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 if (defstr != NULL)
12505 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012506 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12507 xp_type, xp_arg);
12508
12509 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012511 /* since the user typed this, no need to wait for return */
12512 need_wait_return = FALSE;
12513 msg_didout = FALSE;
12514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515 cmd_silent = cmd_silent_save;
12516}
12517
12518/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012519 * "input()" function
12520 * Also handles inputsecret() when inputsecret is set.
12521 */
12522 static void
12523f_input(argvars, rettv)
12524 typval_T *argvars;
12525 typval_T *rettv;
12526{
12527 get_user_input(argvars, rettv, FALSE);
12528}
12529
12530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531 * "inputdialog()" function
12532 */
12533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012535 typval_T *argvars;
12536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537{
12538#if defined(FEAT_GUI_TEXTDIALOG)
12539 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12540 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12541 {
12542 char_u *message;
12543 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012546 message = get_tv_string_chk(&argvars[0]);
12547 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012548 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012549 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 else
12551 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012552 if (message != NULL && defstr != NULL
12553 && do_dialog(VIM_QUESTION, NULL, message,
12554 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 else
12557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012558 if (message != NULL && defstr != NULL
12559 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012560 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561 rettv->vval.v_string = vim_strsave(
12562 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012566 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 }
12568 else
12569#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012570 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571}
12572
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012573/*
12574 * "inputlist()" function
12575 */
12576 static void
12577f_inputlist(argvars, rettv)
12578 typval_T *argvars;
12579 typval_T *rettv;
12580{
12581 listitem_T *li;
12582 int selected;
12583 int mouse_used;
12584
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012585#ifdef NO_CONSOLE_INPUT
12586 /* While starting up, there is no place to enter text. */
12587 if (no_console_input())
12588 return;
12589#endif
12590 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12591 {
12592 EMSG2(_(e_listarg), "inputlist()");
12593 return;
12594 }
12595
12596 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012597 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012598 lines_left = Rows; /* avoid more prompt */
12599 msg_scroll = TRUE;
12600 msg_clr_eos();
12601
12602 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12603 {
12604 msg_puts(get_tv_string(&li->li_tv));
12605 msg_putchar('\n');
12606 }
12607
12608 /* Ask for choice. */
12609 selected = prompt_for_number(&mouse_used);
12610 if (mouse_used)
12611 selected -= lines_left;
12612
12613 rettv->vval.v_number = selected;
12614}
12615
12616
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12618
12619/*
12620 * "inputrestore()" function
12621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012623f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012624 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626{
12627 if (ga_userinput.ga_len > 0)
12628 {
12629 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12631 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012632 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 }
12634 else if (p_verbose > 1)
12635 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012636 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 }
12639}
12640
12641/*
12642 * "inputsave()" function
12643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012645f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012646 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012649 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 if (ga_grow(&ga_userinput, 1) == OK)
12651 {
12652 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12653 + ga_userinput.ga_len);
12654 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012655 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 }
12657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659}
12660
12661/*
12662 * "inputsecret()" function
12663 */
12664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012665f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012666 typval_T *argvars;
12667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668{
12669 ++cmdline_star;
12670 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 --cmdline_star;
12673 --inputsecret_flag;
12674}
12675
12676/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012677 * "insert()" function
12678 */
12679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012681 typval_T *argvars;
12682 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012683{
12684 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012685 listitem_T *item;
12686 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012687 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012688
12689 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012690 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012691 else if ((l = argvars[0].vval.v_list) != NULL
12692 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012693 {
12694 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012695 before = get_tv_number_chk(&argvars[2], &error);
12696 if (error)
12697 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012698
Bram Moolenaar758711c2005-02-02 23:11:38 +000012699 if (before == l->lv_len)
12700 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701 else
12702 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012703 item = list_find(l, before);
12704 if (item == NULL)
12705 {
12706 EMSGN(_(e_listidx), before);
12707 l = NULL;
12708 }
12709 }
12710 if (l != NULL)
12711 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012712 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012713 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012714 }
12715 }
12716}
12717
12718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 * "isdirectory()" function
12720 */
12721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012723 typval_T *argvars;
12724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727}
12728
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012729/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012730 * "islocked()" function
12731 */
12732 static void
12733f_islocked(argvars, rettv)
12734 typval_T *argvars;
12735 typval_T *rettv;
12736{
12737 lval_T lv;
12738 char_u *end;
12739 dictitem_T *di;
12740
12741 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012742 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12743 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012744 if (end != NULL && lv.ll_name != NULL)
12745 {
12746 if (*end != NUL)
12747 EMSG(_(e_trailing));
12748 else
12749 {
12750 if (lv.ll_tv == NULL)
12751 {
12752 if (check_changedtick(lv.ll_name))
12753 rettv->vval.v_number = 1; /* always locked */
12754 else
12755 {
12756 di = find_var(lv.ll_name, NULL);
12757 if (di != NULL)
12758 {
12759 /* Consider a variable locked when:
12760 * 1. the variable itself is locked
12761 * 2. the value of the variable is locked.
12762 * 3. the List or Dict value is locked.
12763 */
12764 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12765 || tv_islocked(&di->di_tv));
12766 }
12767 }
12768 }
12769 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012770 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012771 else if (lv.ll_newkey != NULL)
12772 EMSG2(_(e_dictkey), lv.ll_newkey);
12773 else if (lv.ll_list != NULL)
12774 /* List item. */
12775 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12776 else
12777 /* Dictionary item. */
12778 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12779 }
12780 }
12781
12782 clear_lval(&lv);
12783}
12784
Bram Moolenaar33570922005-01-25 22:26:29 +000012785static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012786
12787/*
12788 * Turn a dict into a list:
12789 * "what" == 0: list of keys
12790 * "what" == 1: list of values
12791 * "what" == 2: list of items
12792 */
12793 static void
12794dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012795 typval_T *argvars;
12796 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012797 int what;
12798{
Bram Moolenaar33570922005-01-25 22:26:29 +000012799 list_T *l2;
12800 dictitem_T *di;
12801 hashitem_T *hi;
12802 listitem_T *li;
12803 listitem_T *li2;
12804 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012805 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012806
Bram Moolenaar8c711452005-01-14 21:53:12 +000012807 if (argvars[0].v_type != VAR_DICT)
12808 {
12809 EMSG(_(e_dictreq));
12810 return;
12811 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012812 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012813 return;
12814
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012815 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012816 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012817
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012818 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012820 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012821 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012822 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012823 --todo;
12824 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012825
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012826 li = listitem_alloc();
12827 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012828 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012829 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012830
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012831 if (what == 0)
12832 {
12833 /* keys() */
12834 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012835 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012836 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12837 }
12838 else if (what == 1)
12839 {
12840 /* values() */
12841 copy_tv(&di->di_tv, &li->li_tv);
12842 }
12843 else
12844 {
12845 /* items() */
12846 l2 = list_alloc();
12847 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012848 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012849 li->li_tv.vval.v_list = l2;
12850 if (l2 == NULL)
12851 break;
12852 ++l2->lv_refcount;
12853
12854 li2 = listitem_alloc();
12855 if (li2 == NULL)
12856 break;
12857 list_append(l2, li2);
12858 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012859 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012860 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12861
12862 li2 = listitem_alloc();
12863 if (li2 == NULL)
12864 break;
12865 list_append(l2, li2);
12866 copy_tv(&di->di_tv, &li2->li_tv);
12867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012868 }
12869 }
12870}
12871
12872/*
12873 * "items(dict)" function
12874 */
12875 static void
12876f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 typval_T *argvars;
12878 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012879{
12880 dict_list(argvars, rettv, 2);
12881}
12882
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012884 * "join()" function
12885 */
12886 static void
12887f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012888 typval_T *argvars;
12889 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012890{
12891 garray_T ga;
12892 char_u *sep;
12893
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012894 if (argvars[0].v_type != VAR_LIST)
12895 {
12896 EMSG(_(e_listreq));
12897 return;
12898 }
12899 if (argvars[0].vval.v_list == NULL)
12900 return;
12901 if (argvars[1].v_type == VAR_UNKNOWN)
12902 sep = (char_u *)" ";
12903 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012904 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012905
12906 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012907
12908 if (sep != NULL)
12909 {
12910 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012911 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012912 ga_append(&ga, NUL);
12913 rettv->vval.v_string = (char_u *)ga.ga_data;
12914 }
12915 else
12916 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012917}
12918
12919/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012920 * "keys()" function
12921 */
12922 static void
12923f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012924 typval_T *argvars;
12925 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012926{
12927 dict_list(argvars, rettv, 0);
12928}
12929
12930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 * "last_buffer_nr()" function.
12932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012934f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012935 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937{
12938 int n = 0;
12939 buf_T *buf;
12940
12941 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12942 if (n < buf->b_fnum)
12943 n = buf->b_fnum;
12944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012946}
12947
12948/*
12949 * "len()" function
12950 */
12951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012953 typval_T *argvars;
12954 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012955{
12956 switch (argvars[0].v_type)
12957 {
12958 case VAR_STRING:
12959 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 rettv->vval.v_number = (varnumber_T)STRLEN(
12961 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012962 break;
12963 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012965 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012966 case VAR_DICT:
12967 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12968 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012969 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012970 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012971 break;
12972 }
12973}
12974
Bram Moolenaar33570922005-01-25 22:26:29 +000012975static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012976
12977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012978libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012979 typval_T *argvars;
12980 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012981 int type;
12982{
12983#ifdef FEAT_LIBCALL
12984 char_u *string_in;
12985 char_u **string_result;
12986 int nr_result;
12987#endif
12988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012989 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012990 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012991 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012992
12993 if (check_restricted() || check_secure())
12994 return;
12995
12996#ifdef FEAT_LIBCALL
12997 /* The first two args must be strings, otherwise its meaningless */
12998 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12999 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013000 string_in = NULL;
13001 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013002 string_in = argvars[2].vval.v_string;
13003 if (type == VAR_NUMBER)
13004 string_result = NULL;
13005 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013006 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013007 if (mch_libcall(argvars[0].vval.v_string,
13008 argvars[1].vval.v_string,
13009 string_in,
13010 argvars[2].vval.v_number,
13011 string_result,
13012 &nr_result) == OK
13013 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013014 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013015 }
13016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017}
13018
13019/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013020 * "libcall()" function
13021 */
13022 static void
13023f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013024 typval_T *argvars;
13025 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013026{
13027 libcall_common(argvars, rettv, VAR_STRING);
13028}
13029
13030/*
13031 * "libcallnr()" function
13032 */
13033 static void
13034f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013035 typval_T *argvars;
13036 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013037{
13038 libcall_common(argvars, rettv, VAR_NUMBER);
13039}
13040
13041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 * "line(string)" function
13043 */
13044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013045f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013046 typval_T *argvars;
13047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048{
13049 linenr_T lnum = 0;
13050 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013051 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013053 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 if (fp != NULL)
13055 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057}
13058
13059/*
13060 * "line2byte(lnum)" function
13061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013063f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013064 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066{
13067#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013068 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069#else
13070 linenr_T lnum;
13071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013072 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13077 if (rettv->vval.v_number >= 0)
13078 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079#endif
13080}
13081
13082/*
13083 * "lispindent(lnum)" function
13084 */
13085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013086f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013087 typval_T *argvars;
13088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089{
13090#ifdef FEAT_LISP
13091 pos_T pos;
13092 linenr_T lnum;
13093
13094 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13097 {
13098 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013099 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100 curwin->w_cursor = pos;
13101 }
13102 else
13103#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105}
13106
13107/*
13108 * "localtime()" function
13109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013112 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116}
13117
Bram Moolenaar33570922005-01-25 22:26:29 +000013118static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119
13120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013122 typval_T *argvars;
13123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 int exact;
13125{
13126 char_u *keys;
13127 char_u *which;
13128 char_u buf[NUMBUFLEN];
13129 char_u *keys_buf = NULL;
13130 char_u *rhs;
13131 int mode;
13132 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013133 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134
13135 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136 rettv->v_type = VAR_STRING;
13137 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 if (*keys == NUL)
13141 return;
13142
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013143 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013145 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013146 if (argvars[2].v_type != VAR_UNKNOWN)
13147 abbr = get_tv_number(&argvars[2]);
13148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 else
13150 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013151 if (which == NULL)
13152 return;
13153
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 mode = get_map_mode(&which, 0);
13155
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013156 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013157 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 vim_free(keys_buf);
13159 if (rhs != NULL)
13160 {
13161 ga_init(&ga);
13162 ga.ga_itemsize = 1;
13163 ga.ga_growsize = 40;
13164
13165 while (*rhs != NUL)
13166 ga_concat(&ga, str2special(&rhs, FALSE));
13167
13168 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 }
13171}
13172
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013173#ifdef FEAT_FLOAT
13174/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013175 * "log()" function
13176 */
13177 static void
13178f_log(argvars, rettv)
13179 typval_T *argvars;
13180 typval_T *rettv;
13181{
13182 float_T f;
13183
13184 rettv->v_type = VAR_FLOAT;
13185 if (get_float_arg(argvars, &f) == OK)
13186 rettv->vval.v_float = log(f);
13187 else
13188 rettv->vval.v_float = 0.0;
13189}
13190
13191/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013192 * "log10()" function
13193 */
13194 static void
13195f_log10(argvars, rettv)
13196 typval_T *argvars;
13197 typval_T *rettv;
13198{
13199 float_T f;
13200
13201 rettv->v_type = VAR_FLOAT;
13202 if (get_float_arg(argvars, &f) == OK)
13203 rettv->vval.v_float = log10(f);
13204 else
13205 rettv->vval.v_float = 0.0;
13206}
13207#endif
13208
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013210 * "map()" function
13211 */
13212 static void
13213f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013214 typval_T *argvars;
13215 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013216{
13217 filter_map(argvars, rettv, TRUE);
13218}
13219
13220/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013221 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 */
13223 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013224f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013225 typval_T *argvars;
13226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013228 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229}
13230
13231/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013232 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 */
13234 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013235f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013236 typval_T *argvars;
13237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013239 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240}
13241
Bram Moolenaar33570922005-01-25 22:26:29 +000013242static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243
13244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013246 typval_T *argvars;
13247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 int type;
13249{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013250 char_u *str = NULL;
13251 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252 char_u *pat;
13253 regmatch_T regmatch;
13254 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013255 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256 char_u *save_cpo;
13257 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013258 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013259 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013260 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013261 list_T *l = NULL;
13262 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013263 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013264 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265
13266 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13267 save_cpo = p_cpo;
13268 p_cpo = (char_u *)"";
13269
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013270 rettv->vval.v_number = -1;
13271 if (type == 3)
13272 {
13273 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013274 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013275 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013276 }
13277 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013279 rettv->v_type = VAR_STRING;
13280 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013283 if (argvars[0].v_type == VAR_LIST)
13284 {
13285 if ((l = argvars[0].vval.v_list) == NULL)
13286 goto theend;
13287 li = l->lv_first;
13288 }
13289 else
13290 expr = str = get_tv_string(&argvars[0]);
13291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13293 if (pat == NULL)
13294 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013295
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013296 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013298 int error = FALSE;
13299
13300 start = get_tv_number_chk(&argvars[2], &error);
13301 if (error)
13302 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013303 if (l != NULL)
13304 {
13305 li = list_find(l, start);
13306 if (li == NULL)
13307 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013308 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013309 }
13310 else
13311 {
13312 if (start < 0)
13313 start = 0;
13314 if (start > (long)STRLEN(str))
13315 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013316 /* When "count" argument is there ignore matches before "start",
13317 * otherwise skip part of the string. Differs when pattern is "^"
13318 * or "\<". */
13319 if (argvars[3].v_type != VAR_UNKNOWN)
13320 startcol = start;
13321 else
13322 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013323 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013324
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013325 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013326 nth = get_tv_number_chk(&argvars[3], &error);
13327 if (error)
13328 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 }
13330
13331 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13332 if (regmatch.regprog != NULL)
13333 {
13334 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013335
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013336 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013337 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013338 if (l != NULL)
13339 {
13340 if (li == NULL)
13341 {
13342 match = FALSE;
13343 break;
13344 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013345 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013346 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013347 if (str == NULL)
13348 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013349 }
13350
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013351 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013352
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013353 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013354 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013355 if (l == NULL && !match)
13356 break;
13357
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013358 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013359 if (l != NULL)
13360 {
13361 li = li->li_next;
13362 ++idx;
13363 }
13364 else
13365 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013366#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013367 startcol = (colnr_T)(regmatch.startp[0]
13368 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013369#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013370 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013371#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013372 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013373 }
13374
13375 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013377 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013378 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013379 int i;
13380
13381 /* return list with matched string and submatches */
13382 for (i = 0; i < NSUBEXP; ++i)
13383 {
13384 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013385 {
13386 if (list_append_string(rettv->vval.v_list,
13387 (char_u *)"", 0) == FAIL)
13388 break;
13389 }
13390 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013391 regmatch.startp[i],
13392 (int)(regmatch.endp[i] - regmatch.startp[i]))
13393 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013394 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013395 }
13396 }
13397 else if (type == 2)
13398 {
13399 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013400 if (l != NULL)
13401 copy_tv(&li->li_tv, rettv);
13402 else
13403 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013405 }
13406 else if (l != NULL)
13407 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 else
13409 {
13410 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 (varnumber_T)(regmatch.startp[0] - str);
13413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013414 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013416 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 }
13418 }
13419 vim_free(regmatch.regprog);
13420 }
13421
13422theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013423 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424 p_cpo = save_cpo;
13425}
13426
13427/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013428 * "match()" function
13429 */
13430 static void
13431f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013432 typval_T *argvars;
13433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013434{
13435 find_some_match(argvars, rettv, 1);
13436}
13437
13438/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013439 * "matchadd()" function
13440 */
13441 static void
13442f_matchadd(argvars, rettv)
13443 typval_T *argvars;
13444 typval_T *rettv;
13445{
13446#ifdef FEAT_SEARCH_EXTRA
13447 char_u buf[NUMBUFLEN];
13448 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13449 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13450 int prio = 10; /* default priority */
13451 int id = -1;
13452 int error = FALSE;
13453
13454 rettv->vval.v_number = -1;
13455
13456 if (grp == NULL || pat == NULL)
13457 return;
13458 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013459 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013460 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013461 if (argvars[3].v_type != VAR_UNKNOWN)
13462 id = get_tv_number_chk(&argvars[3], &error);
13463 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013464 if (error == TRUE)
13465 return;
13466 if (id >= 1 && id <= 3)
13467 {
13468 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13469 return;
13470 }
13471
13472 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13473#endif
13474}
13475
13476/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013477 * "matcharg()" function
13478 */
13479 static void
13480f_matcharg(argvars, rettv)
13481 typval_T *argvars;
13482 typval_T *rettv;
13483{
13484 if (rettv_list_alloc(rettv) == OK)
13485 {
13486#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013487 int id = get_tv_number(&argvars[0]);
13488 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013489
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013490 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013491 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013492 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13493 {
13494 list_append_string(rettv->vval.v_list,
13495 syn_id2name(m->hlg_id), -1);
13496 list_append_string(rettv->vval.v_list, m->pattern, -1);
13497 }
13498 else
13499 {
13500 list_append_string(rettv->vval.v_list, NUL, -1);
13501 list_append_string(rettv->vval.v_list, NUL, -1);
13502 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013503 }
13504#endif
13505 }
13506}
13507
13508/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013509 * "matchdelete()" function
13510 */
13511 static void
13512f_matchdelete(argvars, rettv)
13513 typval_T *argvars;
13514 typval_T *rettv;
13515{
13516#ifdef FEAT_SEARCH_EXTRA
13517 rettv->vval.v_number = match_delete(curwin,
13518 (int)get_tv_number(&argvars[0]), TRUE);
13519#endif
13520}
13521
13522/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013523 * "matchend()" function
13524 */
13525 static void
13526f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013527 typval_T *argvars;
13528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013529{
13530 find_some_match(argvars, rettv, 0);
13531}
13532
13533/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013534 * "matchlist()" function
13535 */
13536 static void
13537f_matchlist(argvars, rettv)
13538 typval_T *argvars;
13539 typval_T *rettv;
13540{
13541 find_some_match(argvars, rettv, 3);
13542}
13543
13544/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545 * "matchstr()" function
13546 */
13547 static void
13548f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013549 typval_T *argvars;
13550 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013551{
13552 find_some_match(argvars, rettv, 2);
13553}
13554
Bram Moolenaar33570922005-01-25 22:26:29 +000013555static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013556
13557 static void
13558max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013559 typval_T *argvars;
13560 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013561 int domax;
13562{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013563 long n = 0;
13564 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013565 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013566
13567 if (argvars[0].v_type == VAR_LIST)
13568 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 list_T *l;
13570 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013571
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013572 l = argvars[0].vval.v_list;
13573 if (l != NULL)
13574 {
13575 li = l->lv_first;
13576 if (li != NULL)
13577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013579 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013580 {
13581 li = li->li_next;
13582 if (li == NULL)
13583 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013584 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013585 if (domax ? i > n : i < n)
13586 n = i;
13587 }
13588 }
13589 }
13590 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013591 else if (argvars[0].v_type == VAR_DICT)
13592 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013594 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013595 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013596 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013597
13598 d = argvars[0].vval.v_dict;
13599 if (d != NULL)
13600 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013601 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013602 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013603 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013604 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013606 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013607 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013608 if (first)
13609 {
13610 n = i;
13611 first = FALSE;
13612 }
13613 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013614 n = i;
13615 }
13616 }
13617 }
13618 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013619 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013620 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013622}
13623
13624/*
13625 * "max()" function
13626 */
13627 static void
13628f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013629 typval_T *argvars;
13630 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013631{
13632 max_min(argvars, rettv, TRUE);
13633}
13634
13635/*
13636 * "min()" function
13637 */
13638 static void
13639f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 typval_T *argvars;
13641 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013642{
13643 max_min(argvars, rettv, FALSE);
13644}
13645
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013646static int mkdir_recurse __ARGS((char_u *dir, int prot));
13647
13648/*
13649 * Create the directory in which "dir" is located, and higher levels when
13650 * needed.
13651 */
13652 static int
13653mkdir_recurse(dir, prot)
13654 char_u *dir;
13655 int prot;
13656{
13657 char_u *p;
13658 char_u *updir;
13659 int r = FAIL;
13660
13661 /* Get end of directory name in "dir".
13662 * We're done when it's "/" or "c:/". */
13663 p = gettail_sep(dir);
13664 if (p <= get_past_head(dir))
13665 return OK;
13666
13667 /* If the directory exists we're done. Otherwise: create it.*/
13668 updir = vim_strnsave(dir, (int)(p - dir));
13669 if (updir == NULL)
13670 return FAIL;
13671 if (mch_isdir(updir))
13672 r = OK;
13673 else if (mkdir_recurse(updir, prot) == OK)
13674 r = vim_mkdir_emsg(updir, prot);
13675 vim_free(updir);
13676 return r;
13677}
13678
13679#ifdef vim_mkdir
13680/*
13681 * "mkdir()" function
13682 */
13683 static void
13684f_mkdir(argvars, rettv)
13685 typval_T *argvars;
13686 typval_T *rettv;
13687{
13688 char_u *dir;
13689 char_u buf[NUMBUFLEN];
13690 int prot = 0755;
13691
13692 rettv->vval.v_number = FAIL;
13693 if (check_restricted() || check_secure())
13694 return;
13695
13696 dir = get_tv_string_buf(&argvars[0], buf);
13697 if (argvars[1].v_type != VAR_UNKNOWN)
13698 {
13699 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700 prot = get_tv_number_chk(&argvars[2], NULL);
13701 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013702 mkdir_recurse(dir, prot);
13703 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013705}
13706#endif
13707
Bram Moolenaar0d660222005-01-07 21:51:51 +000013708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 * "mode()" function
13710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013713 typval_T *argvars;
13714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013716 char_u buf[3];
13717
13718 buf[1] = NUL;
13719 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720
13721#ifdef FEAT_VISUAL
13722 if (VIsual_active)
13723 {
13724 if (VIsual_select)
13725 buf[0] = VIsual_mode + 's' - 'v';
13726 else
13727 buf[0] = VIsual_mode;
13728 }
13729 else
13730#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013731 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13732 || State == CONFIRM)
13733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013735 if (State == ASKMORE)
13736 buf[1] = 'm';
13737 else if (State == CONFIRM)
13738 buf[1] = '?';
13739 }
13740 else if (State == EXTERNCMD)
13741 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 else if (State & INSERT)
13743 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013744#ifdef FEAT_VREPLACE
13745 if (State & VREPLACE_FLAG)
13746 {
13747 buf[0] = 'R';
13748 buf[1] = 'v';
13749 }
13750 else
13751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 if (State & REPLACE_FLAG)
13753 buf[0] = 'R';
13754 else
13755 buf[0] = 'i';
13756 }
13757 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013760 if (exmode_active)
13761 buf[1] = 'v';
13762 }
13763 else if (exmode_active)
13764 {
13765 buf[0] = 'c';
13766 buf[1] = 'e';
13767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013771 if (finish_op)
13772 buf[1] = 'o';
13773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013775 /* Clear out the minor mode when the argument is not a non-zero number or
13776 * non-empty string. */
13777 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013778 buf[1] = NUL;
13779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013780 rettv->vval.v_string = vim_strsave(buf);
13781 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782}
13783
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013784#ifdef FEAT_MZSCHEME
13785/*
13786 * "mzeval()" function
13787 */
13788 static void
13789f_mzeval(argvars, rettv)
13790 typval_T *argvars;
13791 typval_T *rettv;
13792{
13793 char_u *str;
13794 char_u buf[NUMBUFLEN];
13795
13796 str = get_tv_string_buf(&argvars[0], buf);
13797 do_mzeval(str, rettv);
13798}
13799#endif
13800
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013802 * "nextnonblank()" function
13803 */
13804 static void
13805f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013806 typval_T *argvars;
13807 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013808{
13809 linenr_T lnum;
13810
13811 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013814 {
13815 lnum = 0;
13816 break;
13817 }
13818 if (*skipwhite(ml_get(lnum)) != NUL)
13819 break;
13820 }
13821 rettv->vval.v_number = lnum;
13822}
13823
13824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 * "nr2char()" function
13826 */
13827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013828f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013829 typval_T *argvars;
13830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831{
13832 char_u buf[NUMBUFLEN];
13833
13834#ifdef FEAT_MBYTE
13835 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013836 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 else
13838#endif
13839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013840 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 buf[1] = NUL;
13842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013843 rettv->v_type = VAR_STRING;
13844 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013845}
13846
13847/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013848 * "pathshorten()" function
13849 */
13850 static void
13851f_pathshorten(argvars, rettv)
13852 typval_T *argvars;
13853 typval_T *rettv;
13854{
13855 char_u *p;
13856
13857 rettv->v_type = VAR_STRING;
13858 p = get_tv_string_chk(&argvars[0]);
13859 if (p == NULL)
13860 rettv->vval.v_string = NULL;
13861 else
13862 {
13863 p = vim_strsave(p);
13864 rettv->vval.v_string = p;
13865 if (p != NULL)
13866 shorten_dir(p);
13867 }
13868}
13869
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013870#ifdef FEAT_FLOAT
13871/*
13872 * "pow()" function
13873 */
13874 static void
13875f_pow(argvars, rettv)
13876 typval_T *argvars;
13877 typval_T *rettv;
13878{
13879 float_T fx, fy;
13880
13881 rettv->v_type = VAR_FLOAT;
13882 if (get_float_arg(argvars, &fx) == OK
13883 && get_float_arg(&argvars[1], &fy) == OK)
13884 rettv->vval.v_float = pow(fx, fy);
13885 else
13886 rettv->vval.v_float = 0.0;
13887}
13888#endif
13889
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013891 * "prevnonblank()" function
13892 */
13893 static void
13894f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013895 typval_T *argvars;
13896 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013897{
13898 linenr_T lnum;
13899
13900 lnum = get_tv_lnum(argvars);
13901 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13902 lnum = 0;
13903 else
13904 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13905 --lnum;
13906 rettv->vval.v_number = lnum;
13907}
13908
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013909#ifdef HAVE_STDARG_H
13910/* This dummy va_list is here because:
13911 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13912 * - locally in the function results in a "used before set" warning
13913 * - using va_start() to initialize it gives "function with fixed args" error */
13914static va_list ap;
13915#endif
13916
Bram Moolenaar8c711452005-01-14 21:53:12 +000013917/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013918 * "printf()" function
13919 */
13920 static void
13921f_printf(argvars, rettv)
13922 typval_T *argvars;
13923 typval_T *rettv;
13924{
13925 rettv->v_type = VAR_STRING;
13926 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013927#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013928 {
13929 char_u buf[NUMBUFLEN];
13930 int len;
13931 char_u *s;
13932 int saved_did_emsg = did_emsg;
13933 char *fmt;
13934
13935 /* Get the required length, allocate the buffer and do it for real. */
13936 did_emsg = FALSE;
13937 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013938 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013939 if (!did_emsg)
13940 {
13941 s = alloc(len + 1);
13942 if (s != NULL)
13943 {
13944 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013945 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013946 }
13947 }
13948 did_emsg |= saved_did_emsg;
13949 }
13950#endif
13951}
13952
13953/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013954 * "pumvisible()" function
13955 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013956 static void
13957f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013958 typval_T *argvars UNUSED;
13959 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013960{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013961#ifdef FEAT_INS_EXPAND
13962 if (pum_visible())
13963 rettv->vval.v_number = 1;
13964#endif
13965}
13966
13967/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013968 * "range()" function
13969 */
13970 static void
13971f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013972 typval_T *argvars;
13973 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013974{
13975 long start;
13976 long end;
13977 long stride = 1;
13978 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013979 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013982 if (argvars[1].v_type == VAR_UNKNOWN)
13983 {
13984 end = start - 1;
13985 start = 0;
13986 }
13987 else
13988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013989 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013990 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013992 }
13993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994 if (error)
13995 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013996 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013997 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013998 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013999 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014000 else
14001 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014002 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014003 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014004 if (list_append_number(rettv->vval.v_list,
14005 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014006 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014007 }
14008}
14009
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014010/*
14011 * "readfile()" function
14012 */
14013 static void
14014f_readfile(argvars, rettv)
14015 typval_T *argvars;
14016 typval_T *rettv;
14017{
14018 int binary = FALSE;
14019 char_u *fname;
14020 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014021 listitem_T *li;
14022#define FREAD_SIZE 200 /* optimized for text lines */
14023 char_u buf[FREAD_SIZE];
14024 int readlen; /* size of last fread() */
14025 int buflen; /* nr of valid chars in buf[] */
14026 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14027 int tolist; /* first byte in buf[] still to be put in list */
14028 int chop; /* how many CR to chop off */
14029 char_u *prev = NULL; /* previously read bytes, if any */
14030 int prevlen = 0; /* length of "prev" if not NULL */
14031 char_u *s;
14032 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014033 long maxline = MAXLNUM;
14034 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014036 if (argvars[1].v_type != VAR_UNKNOWN)
14037 {
14038 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14039 binary = TRUE;
14040 if (argvars[2].v_type != VAR_UNKNOWN)
14041 maxline = get_tv_number(&argvars[2]);
14042 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014043
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014044 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014045 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014046
14047 /* Always open the file in binary mode, library functions have a mind of
14048 * their own about CR-LF conversion. */
14049 fname = get_tv_string(&argvars[0]);
14050 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14051 {
14052 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14053 return;
14054 }
14055
14056 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014057 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014058 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014059 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014060 buflen = filtd + readlen;
14061 tolist = 0;
14062 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14063 {
14064 if (buf[filtd] == '\n' || readlen <= 0)
14065 {
14066 /* Only when in binary mode add an empty list item when the
14067 * last line ends in a '\n'. */
14068 if (!binary && readlen == 0 && filtd == 0)
14069 break;
14070
14071 /* Found end-of-line or end-of-file: add a text line to the
14072 * list. */
14073 chop = 0;
14074 if (!binary)
14075 while (filtd - chop - 1 >= tolist
14076 && buf[filtd - chop - 1] == '\r')
14077 ++chop;
14078 len = filtd - tolist - chop;
14079 if (prev == NULL)
14080 s = vim_strnsave(buf + tolist, len);
14081 else
14082 {
14083 s = alloc((unsigned)(prevlen + len + 1));
14084 if (s != NULL)
14085 {
14086 mch_memmove(s, prev, prevlen);
14087 vim_free(prev);
14088 prev = NULL;
14089 mch_memmove(s + prevlen, buf + tolist, len);
14090 s[prevlen + len] = NUL;
14091 }
14092 }
14093 tolist = filtd + 1;
14094
14095 li = listitem_alloc();
14096 if (li == NULL)
14097 {
14098 vim_free(s);
14099 break;
14100 }
14101 li->li_tv.v_type = VAR_STRING;
14102 li->li_tv.v_lock = 0;
14103 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014104 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014105
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014106 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014107 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014108 if (readlen <= 0)
14109 break;
14110 }
14111 else if (buf[filtd] == NUL)
14112 buf[filtd] = '\n';
14113 }
14114 if (readlen <= 0)
14115 break;
14116
14117 if (tolist == 0)
14118 {
14119 /* "buf" is full, need to move text to an allocated buffer */
14120 if (prev == NULL)
14121 {
14122 prev = vim_strnsave(buf, buflen);
14123 prevlen = buflen;
14124 }
14125 else
14126 {
14127 s = alloc((unsigned)(prevlen + buflen));
14128 if (s != NULL)
14129 {
14130 mch_memmove(s, prev, prevlen);
14131 mch_memmove(s + prevlen, buf, buflen);
14132 vim_free(prev);
14133 prev = s;
14134 prevlen += buflen;
14135 }
14136 }
14137 filtd = 0;
14138 }
14139 else
14140 {
14141 mch_memmove(buf, buf + tolist, buflen - tolist);
14142 filtd -= tolist;
14143 }
14144 }
14145
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014146 /*
14147 * For a negative line count use only the lines at the end of the file,
14148 * free the rest.
14149 */
14150 if (maxline < 0)
14151 while (cnt > -maxline)
14152 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014153 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014154 --cnt;
14155 }
14156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014157 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014158 fclose(fd);
14159}
14160
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014161#if defined(FEAT_RELTIME)
14162static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14163
14164/*
14165 * Convert a List to proftime_T.
14166 * Return FAIL when there is something wrong.
14167 */
14168 static int
14169list2proftime(arg, tm)
14170 typval_T *arg;
14171 proftime_T *tm;
14172{
14173 long n1, n2;
14174 int error = FALSE;
14175
14176 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14177 || arg->vval.v_list->lv_len != 2)
14178 return FAIL;
14179 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14180 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14181# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014182 tm->HighPart = n1;
14183 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014184# else
14185 tm->tv_sec = n1;
14186 tm->tv_usec = n2;
14187# endif
14188 return error ? FAIL : OK;
14189}
14190#endif /* FEAT_RELTIME */
14191
14192/*
14193 * "reltime()" function
14194 */
14195 static void
14196f_reltime(argvars, rettv)
14197 typval_T *argvars;
14198 typval_T *rettv;
14199{
14200#ifdef FEAT_RELTIME
14201 proftime_T res;
14202 proftime_T start;
14203
14204 if (argvars[0].v_type == VAR_UNKNOWN)
14205 {
14206 /* No arguments: get current time. */
14207 profile_start(&res);
14208 }
14209 else if (argvars[1].v_type == VAR_UNKNOWN)
14210 {
14211 if (list2proftime(&argvars[0], &res) == FAIL)
14212 return;
14213 profile_end(&res);
14214 }
14215 else
14216 {
14217 /* Two arguments: compute the difference. */
14218 if (list2proftime(&argvars[0], &start) == FAIL
14219 || list2proftime(&argvars[1], &res) == FAIL)
14220 return;
14221 profile_sub(&res, &start);
14222 }
14223
14224 if (rettv_list_alloc(rettv) == OK)
14225 {
14226 long n1, n2;
14227
14228# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014229 n1 = res.HighPart;
14230 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014231# else
14232 n1 = res.tv_sec;
14233 n2 = res.tv_usec;
14234# endif
14235 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14236 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14237 }
14238#endif
14239}
14240
14241/*
14242 * "reltimestr()" function
14243 */
14244 static void
14245f_reltimestr(argvars, rettv)
14246 typval_T *argvars;
14247 typval_T *rettv;
14248{
14249#ifdef FEAT_RELTIME
14250 proftime_T tm;
14251#endif
14252
14253 rettv->v_type = VAR_STRING;
14254 rettv->vval.v_string = NULL;
14255#ifdef FEAT_RELTIME
14256 if (list2proftime(&argvars[0], &tm) == OK)
14257 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14258#endif
14259}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014260
Bram Moolenaar0d660222005-01-07 21:51:51 +000014261#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14262static void make_connection __ARGS((void));
14263static int check_connection __ARGS((void));
14264
14265 static void
14266make_connection()
14267{
14268 if (X_DISPLAY == NULL
14269# ifdef FEAT_GUI
14270 && !gui.in_use
14271# endif
14272 )
14273 {
14274 x_force_connect = TRUE;
14275 setup_term_clip();
14276 x_force_connect = FALSE;
14277 }
14278}
14279
14280 static int
14281check_connection()
14282{
14283 make_connection();
14284 if (X_DISPLAY == NULL)
14285 {
14286 EMSG(_("E240: No connection to Vim server"));
14287 return FAIL;
14288 }
14289 return OK;
14290}
14291#endif
14292
14293#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014294static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014295
14296 static void
14297remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014298 typval_T *argvars;
14299 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014300 int expr;
14301{
14302 char_u *server_name;
14303 char_u *keys;
14304 char_u *r = NULL;
14305 char_u buf[NUMBUFLEN];
14306# ifdef WIN32
14307 HWND w;
14308# else
14309 Window w;
14310# endif
14311
14312 if (check_restricted() || check_secure())
14313 return;
14314
14315# ifdef FEAT_X11
14316 if (check_connection() == FAIL)
14317 return;
14318# endif
14319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014320 server_name = get_tv_string_chk(&argvars[0]);
14321 if (server_name == NULL)
14322 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323 keys = get_tv_string_buf(&argvars[1], buf);
14324# ifdef WIN32
14325 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14326# else
14327 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14328 < 0)
14329# endif
14330 {
14331 if (r != NULL)
14332 EMSG(r); /* sending worked but evaluation failed */
14333 else
14334 EMSG2(_("E241: Unable to send to %s"), server_name);
14335 return;
14336 }
14337
14338 rettv->vval.v_string = r;
14339
14340 if (argvars[2].v_type != VAR_UNKNOWN)
14341 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014342 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014343 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014344 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014345
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014346 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014347 v.di_tv.v_type = VAR_STRING;
14348 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 idvar = get_tv_string_chk(&argvars[2]);
14350 if (idvar != NULL)
14351 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014352 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014353 }
14354}
14355#endif
14356
14357/*
14358 * "remote_expr()" function
14359 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014360 static void
14361f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014362 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014363 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014364{
14365 rettv->v_type = VAR_STRING;
14366 rettv->vval.v_string = NULL;
14367#ifdef FEAT_CLIENTSERVER
14368 remote_common(argvars, rettv, TRUE);
14369#endif
14370}
14371
14372/*
14373 * "remote_foreground()" function
14374 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014375 static void
14376f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014377 typval_T *argvars UNUSED;
14378 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014379{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014380#ifdef FEAT_CLIENTSERVER
14381# ifdef WIN32
14382 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014383 {
14384 char_u *server_name = get_tv_string_chk(&argvars[0]);
14385
14386 if (server_name != NULL)
14387 serverForeground(server_name);
14388 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389# else
14390 /* Send a foreground() expression to the server. */
14391 argvars[1].v_type = VAR_STRING;
14392 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14393 argvars[2].v_type = VAR_UNKNOWN;
14394 remote_common(argvars, rettv, TRUE);
14395 vim_free(argvars[1].vval.v_string);
14396# endif
14397#endif
14398}
14399
Bram Moolenaar0d660222005-01-07 21:51:51 +000014400 static void
14401f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014402 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014404{
14405#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014406 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014407 char_u *s = NULL;
14408# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014409 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014410# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014412
14413 if (check_restricted() || check_secure())
14414 {
14415 rettv->vval.v_number = -1;
14416 return;
14417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014418 serverid = get_tv_string_chk(&argvars[0]);
14419 if (serverid == NULL)
14420 {
14421 rettv->vval.v_number = -1;
14422 return; /* type error; errmsg already given */
14423 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014424# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014425 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014426 if (n == 0)
14427 rettv->vval.v_number = -1;
14428 else
14429 {
14430 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14431 rettv->vval.v_number = (s != NULL);
14432 }
14433# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434 if (check_connection() == FAIL)
14435 return;
14436
14437 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014438 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439# endif
14440
14441 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014443 char_u *retvar;
14444
Bram Moolenaar33570922005-01-25 22:26:29 +000014445 v.di_tv.v_type = VAR_STRING;
14446 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014447 retvar = get_tv_string_chk(&argvars[1]);
14448 if (retvar != NULL)
14449 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014450 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014451 }
14452#else
14453 rettv->vval.v_number = -1;
14454#endif
14455}
14456
Bram Moolenaar0d660222005-01-07 21:51:51 +000014457 static void
14458f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014459 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014460 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014461{
14462 char_u *r = NULL;
14463
14464#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014465 char_u *serverid = get_tv_string_chk(&argvars[0]);
14466
14467 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014468 {
14469# ifdef WIN32
14470 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014471 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014472
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014473 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014474 if (n != 0)
14475 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14476 if (r == NULL)
14477# else
14478 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014479 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480# endif
14481 EMSG(_("E277: Unable to read a server reply"));
14482 }
14483#endif
14484 rettv->v_type = VAR_STRING;
14485 rettv->vval.v_string = r;
14486}
14487
14488/*
14489 * "remote_send()" function
14490 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491 static void
14492f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014493 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495{
14496 rettv->v_type = VAR_STRING;
14497 rettv->vval.v_string = NULL;
14498#ifdef FEAT_CLIENTSERVER
14499 remote_common(argvars, rettv, FALSE);
14500#endif
14501}
14502
14503/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014504 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014505 */
14506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014508 typval_T *argvars;
14509 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014510{
Bram Moolenaar33570922005-01-25 22:26:29 +000014511 list_T *l;
14512 listitem_T *item, *item2;
14513 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014514 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014515 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014516 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014517 dict_T *d;
14518 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014519
Bram Moolenaar8c711452005-01-14 21:53:12 +000014520 if (argvars[0].v_type == VAR_DICT)
14521 {
14522 if (argvars[2].v_type != VAR_UNKNOWN)
14523 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014524 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014525 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014527 key = get_tv_string_chk(&argvars[1]);
14528 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 di = dict_find(d, key, -1);
14531 if (di == NULL)
14532 EMSG2(_(e_dictkey), key);
14533 else
14534 {
14535 *rettv = di->di_tv;
14536 init_tv(&di->di_tv);
14537 dictitem_remove(d, di);
14538 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014539 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014540 }
14541 }
14542 else if (argvars[0].v_type != VAR_LIST)
14543 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014544 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014545 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014547 int error = FALSE;
14548
14549 idx = get_tv_number_chk(&argvars[1], &error);
14550 if (error)
14551 ; /* type error: do nothing, errmsg already given */
14552 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014553 EMSGN(_(e_listidx), idx);
14554 else
14555 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014556 if (argvars[2].v_type == VAR_UNKNOWN)
14557 {
14558 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014559 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014560 *rettv = item->li_tv;
14561 vim_free(item);
14562 }
14563 else
14564 {
14565 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014566 end = get_tv_number_chk(&argvars[2], &error);
14567 if (error)
14568 ; /* type error: do nothing */
14569 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014570 EMSGN(_(e_listidx), end);
14571 else
14572 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014573 int cnt = 0;
14574
14575 for (li = item; li != NULL; li = li->li_next)
14576 {
14577 ++cnt;
14578 if (li == item2)
14579 break;
14580 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014581 if (li == NULL) /* didn't find "item2" after "item" */
14582 EMSG(_(e_invrange));
14583 else
14584 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014585 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014586 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014587 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014588 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014589 l->lv_first = item;
14590 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014591 item->li_prev = NULL;
14592 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014593 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014594 }
14595 }
14596 }
14597 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014598 }
14599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600}
14601
14602/*
14603 * "rename({from}, {to})" function
14604 */
14605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014607 typval_T *argvars;
14608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609{
14610 char_u buf[NUMBUFLEN];
14611
14612 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014615 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14616 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617}
14618
14619/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014620 * "repeat()" function
14621 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014622 static void
14623f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014624 typval_T *argvars;
14625 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014626{
14627 char_u *p;
14628 int n;
14629 int slen;
14630 int len;
14631 char_u *r;
14632 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014633
14634 n = get_tv_number(&argvars[1]);
14635 if (argvars[0].v_type == VAR_LIST)
14636 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014637 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014638 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014639 if (list_extend(rettv->vval.v_list,
14640 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014641 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014642 }
14643 else
14644 {
14645 p = get_tv_string(&argvars[0]);
14646 rettv->v_type = VAR_STRING;
14647 rettv->vval.v_string = NULL;
14648
14649 slen = (int)STRLEN(p);
14650 len = slen * n;
14651 if (len <= 0)
14652 return;
14653
14654 r = alloc(len + 1);
14655 if (r != NULL)
14656 {
14657 for (i = 0; i < n; i++)
14658 mch_memmove(r + i * slen, p, (size_t)slen);
14659 r[len] = NUL;
14660 }
14661
14662 rettv->vval.v_string = r;
14663 }
14664}
14665
14666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667 * "resolve()" function
14668 */
14669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014670f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014671 typval_T *argvars;
14672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673{
14674 char_u *p;
14675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677#ifdef FEAT_SHORTCUT
14678 {
14679 char_u *v = NULL;
14680
14681 v = mch_resolve_shortcut(p);
14682 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014685 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 }
14687#else
14688# ifdef HAVE_READLINK
14689 {
14690 char_u buf[MAXPATHL + 1];
14691 char_u *cpy;
14692 int len;
14693 char_u *remain = NULL;
14694 char_u *q;
14695 int is_relative_to_current = FALSE;
14696 int has_trailing_pathsep = FALSE;
14697 int limit = 100;
14698
14699 p = vim_strsave(p);
14700
14701 if (p[0] == '.' && (vim_ispathsep(p[1])
14702 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14703 is_relative_to_current = TRUE;
14704
14705 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014706 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 has_trailing_pathsep = TRUE;
14708
14709 q = getnextcomp(p);
14710 if (*q != NUL)
14711 {
14712 /* Separate the first path component in "p", and keep the
14713 * remainder (beginning with the path separator). */
14714 remain = vim_strsave(q - 1);
14715 q[-1] = NUL;
14716 }
14717
14718 for (;;)
14719 {
14720 for (;;)
14721 {
14722 len = readlink((char *)p, (char *)buf, MAXPATHL);
14723 if (len <= 0)
14724 break;
14725 buf[len] = NUL;
14726
14727 if (limit-- == 0)
14728 {
14729 vim_free(p);
14730 vim_free(remain);
14731 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014732 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 goto fail;
14734 }
14735
14736 /* Ensure that the result will have a trailing path separator
14737 * if the argument has one. */
14738 if (remain == NULL && has_trailing_pathsep)
14739 add_pathsep(buf);
14740
14741 /* Separate the first path component in the link value and
14742 * concatenate the remainders. */
14743 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14744 if (*q != NUL)
14745 {
14746 if (remain == NULL)
14747 remain = vim_strsave(q - 1);
14748 else
14749 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014750 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 if (cpy != NULL)
14752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 vim_free(remain);
14754 remain = cpy;
14755 }
14756 }
14757 q[-1] = NUL;
14758 }
14759
14760 q = gettail(p);
14761 if (q > p && *q == NUL)
14762 {
14763 /* Ignore trailing path separator. */
14764 q[-1] = NUL;
14765 q = gettail(p);
14766 }
14767 if (q > p && !mch_isFullName(buf))
14768 {
14769 /* symlink is relative to directory of argument */
14770 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14771 if (cpy != NULL)
14772 {
14773 STRCPY(cpy, p);
14774 STRCPY(gettail(cpy), buf);
14775 vim_free(p);
14776 p = cpy;
14777 }
14778 }
14779 else
14780 {
14781 vim_free(p);
14782 p = vim_strsave(buf);
14783 }
14784 }
14785
14786 if (remain == NULL)
14787 break;
14788
14789 /* Append the first path component of "remain" to "p". */
14790 q = getnextcomp(remain + 1);
14791 len = q - remain - (*q != NUL);
14792 cpy = vim_strnsave(p, STRLEN(p) + len);
14793 if (cpy != NULL)
14794 {
14795 STRNCAT(cpy, remain, len);
14796 vim_free(p);
14797 p = cpy;
14798 }
14799 /* Shorten "remain". */
14800 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014801 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802 else
14803 {
14804 vim_free(remain);
14805 remain = NULL;
14806 }
14807 }
14808
14809 /* If the result is a relative path name, make it explicitly relative to
14810 * the current directory if and only if the argument had this form. */
14811 if (!vim_ispathsep(*p))
14812 {
14813 if (is_relative_to_current
14814 && *p != NUL
14815 && !(p[0] == '.'
14816 && (p[1] == NUL
14817 || vim_ispathsep(p[1])
14818 || (p[1] == '.'
14819 && (p[2] == NUL
14820 || vim_ispathsep(p[2]))))))
14821 {
14822 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014823 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 if (cpy != NULL)
14825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 vim_free(p);
14827 p = cpy;
14828 }
14829 }
14830 else if (!is_relative_to_current)
14831 {
14832 /* Strip leading "./". */
14833 q = p;
14834 while (q[0] == '.' && vim_ispathsep(q[1]))
14835 q += 2;
14836 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014837 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 }
14839 }
14840
14841 /* Ensure that the result will have no trailing path separator
14842 * if the argument had none. But keep "/" or "//". */
14843 if (!has_trailing_pathsep)
14844 {
14845 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014846 if (after_pathsep(p, q))
14847 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 }
14849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 }
14852# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854# endif
14855#endif
14856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014857 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858
14859#ifdef HAVE_READLINK
14860fail:
14861#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014862 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863}
14864
14865/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014866 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 */
14868 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014869f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014870 typval_T *argvars;
14871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872{
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 list_T *l;
14874 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875
Bram Moolenaar0d660222005-01-07 21:51:51 +000014876 if (argvars[0].v_type != VAR_LIST)
14877 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014878 else if ((l = argvars[0].vval.v_list) != NULL
14879 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880 {
14881 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014882 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014883 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014884 while (li != NULL)
14885 {
14886 ni = li->li_prev;
14887 list_append(l, li);
14888 li = ni;
14889 }
14890 rettv->vval.v_list = l;
14891 rettv->v_type = VAR_LIST;
14892 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014893 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895}
14896
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014897#define SP_NOMOVE 0x01 /* don't move cursor */
14898#define SP_REPEAT 0x02 /* repeat to find outer pair */
14899#define SP_RETCOUNT 0x04 /* return matchcount */
14900#define SP_SETPCMARK 0x08 /* set previous context mark */
14901#define SP_START 0x10 /* accept match at start position */
14902#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14903#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014904
Bram Moolenaar33570922005-01-25 22:26:29 +000014905static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014906
14907/*
14908 * Get flags for a search function.
14909 * Possibly sets "p_ws".
14910 * Returns BACKWARD, FORWARD or zero (for an error).
14911 */
14912 static int
14913get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014914 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014915 int *flagsp;
14916{
14917 int dir = FORWARD;
14918 char_u *flags;
14919 char_u nbuf[NUMBUFLEN];
14920 int mask;
14921
14922 if (varp->v_type != VAR_UNKNOWN)
14923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014924 flags = get_tv_string_buf_chk(varp, nbuf);
14925 if (flags == NULL)
14926 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014927 while (*flags != NUL)
14928 {
14929 switch (*flags)
14930 {
14931 case 'b': dir = BACKWARD; break;
14932 case 'w': p_ws = TRUE; break;
14933 case 'W': p_ws = FALSE; break;
14934 default: mask = 0;
14935 if (flagsp != NULL)
14936 switch (*flags)
14937 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014938 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014939 case 'e': mask = SP_END; break;
14940 case 'm': mask = SP_RETCOUNT; break;
14941 case 'n': mask = SP_NOMOVE; break;
14942 case 'p': mask = SP_SUBPAT; break;
14943 case 'r': mask = SP_REPEAT; break;
14944 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014945 }
14946 if (mask == 0)
14947 {
14948 EMSG2(_(e_invarg2), flags);
14949 dir = 0;
14950 }
14951 else
14952 *flagsp |= mask;
14953 }
14954 if (dir == 0)
14955 break;
14956 ++flags;
14957 }
14958 }
14959 return dir;
14960}
14961
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014963 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014965 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014966search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014967 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014968 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014969 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014971 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 char_u *pat;
14973 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014974 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 int save_p_ws = p_ws;
14976 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014977 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014978 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014979 proftime_T tm;
14980#ifdef FEAT_RELTIME
14981 long time_limit = 0;
14982#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014983 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014984 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014986 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014987 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014988 if (dir == 0)
14989 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014990 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014991 if (flags & SP_START)
14992 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014993 if (flags & SP_END)
14994 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014995
Bram Moolenaar76929292008-01-06 19:07:36 +000014996 /* Optional arguments: line number to stop searching and timeout. */
14997 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014998 {
14999 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15000 if (lnum_stop < 0)
15001 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015002#ifdef FEAT_RELTIME
15003 if (argvars[3].v_type != VAR_UNKNOWN)
15004 {
15005 time_limit = get_tv_number_chk(&argvars[3], NULL);
15006 if (time_limit < 0)
15007 goto theend;
15008 }
15009#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015010 }
15011
Bram Moolenaar76929292008-01-06 19:07:36 +000015012#ifdef FEAT_RELTIME
15013 /* Set the time limit, if there is one. */
15014 profile_setlimit(time_limit, &tm);
15015#endif
15016
Bram Moolenaar231334e2005-07-25 20:46:57 +000015017 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015018 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015019 * Check to make sure only those flags are set.
15020 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15021 * flags cannot be set. Check for that condition also.
15022 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015023 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015024 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015026 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015027 goto theend;
15028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015030 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015031 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015032 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015033 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015035 if (flags & SP_SUBPAT)
15036 retval = subpatnum;
15037 else
15038 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015039 if (flags & SP_SETPCMARK)
15040 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015042 if (match_pos != NULL)
15043 {
15044 /* Store the match cursor position */
15045 match_pos->lnum = pos.lnum;
15046 match_pos->col = pos.col + 1;
15047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048 /* "/$" will put the cursor after the end of the line, may need to
15049 * correct that here */
15050 check_cursor();
15051 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015052
15053 /* If 'n' flag is used: restore cursor position. */
15054 if (flags & SP_NOMOVE)
15055 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015056 else
15057 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015058theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015060
15061 return retval;
15062}
15063
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015064#ifdef FEAT_FLOAT
15065/*
15066 * "round({float})" function
15067 */
15068 static void
15069f_round(argvars, rettv)
15070 typval_T *argvars;
15071 typval_T *rettv;
15072{
15073 float_T f;
15074
15075 rettv->v_type = VAR_FLOAT;
15076 if (get_float_arg(argvars, &f) == OK)
15077 /* round() is not in C90, use ceil() or floor() instead. */
15078 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15079 else
15080 rettv->vval.v_float = 0.0;
15081}
15082#endif
15083
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015084/*
15085 * "search()" function
15086 */
15087 static void
15088f_search(argvars, rettv)
15089 typval_T *argvars;
15090 typval_T *rettv;
15091{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015092 int flags = 0;
15093
15094 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095}
15096
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015098 * "searchdecl()" function
15099 */
15100 static void
15101f_searchdecl(argvars, rettv)
15102 typval_T *argvars;
15103 typval_T *rettv;
15104{
15105 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015106 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015107 int error = FALSE;
15108 char_u *name;
15109
15110 rettv->vval.v_number = 1; /* default: FAIL */
15111
15112 name = get_tv_string_chk(&argvars[0]);
15113 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015114 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015115 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015116 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15117 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15118 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015119 if (!error && name != NULL)
15120 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015121 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015122}
15123
15124/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015125 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015127 static int
15128searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015129 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015130 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131{
15132 char_u *spat, *mpat, *epat;
15133 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135 int dir;
15136 int flags = 0;
15137 char_u nbuf1[NUMBUFLEN];
15138 char_u nbuf2[NUMBUFLEN];
15139 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015140 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015141 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015142 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015145 spat = get_tv_string_chk(&argvars[0]);
15146 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15147 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15148 if (spat == NULL || mpat == NULL || epat == NULL)
15149 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 /* Handle the optional fourth argument: flags */
15152 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015153 if (dir == 0)
15154 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015155
15156 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015157 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15158 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015159 if ((flags & (SP_END | SP_SUBPAT)) != 0
15160 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015161 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015162 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015163 goto theend;
15164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015166 /* Using 'r' implies 'W', otherwise it doesn't work. */
15167 if (flags & SP_REPEAT)
15168 p_ws = FALSE;
15169
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015170 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015171 if (argvars[3].v_type == VAR_UNKNOWN
15172 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 skip = (char_u *)"";
15174 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015176 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015177 if (argvars[5].v_type != VAR_UNKNOWN)
15178 {
15179 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15180 if (lnum_stop < 0)
15181 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015182#ifdef FEAT_RELTIME
15183 if (argvars[6].v_type != VAR_UNKNOWN)
15184 {
15185 time_limit = get_tv_number_chk(&argvars[6], NULL);
15186 if (time_limit < 0)
15187 goto theend;
15188 }
15189#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015190 }
15191 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015192 if (skip == NULL)
15193 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015195 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015196 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015197
15198theend:
15199 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015200
15201 return retval;
15202}
15203
15204/*
15205 * "searchpair()" function
15206 */
15207 static void
15208f_searchpair(argvars, rettv)
15209 typval_T *argvars;
15210 typval_T *rettv;
15211{
15212 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15213}
15214
15215/*
15216 * "searchpairpos()" function
15217 */
15218 static void
15219f_searchpairpos(argvars, rettv)
15220 typval_T *argvars;
15221 typval_T *rettv;
15222{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015223 pos_T match_pos;
15224 int lnum = 0;
15225 int col = 0;
15226
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015227 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015228 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015229
15230 if (searchpair_cmn(argvars, &match_pos) > 0)
15231 {
15232 lnum = match_pos.lnum;
15233 col = match_pos.col;
15234 }
15235
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015236 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15237 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015238}
15239
15240/*
15241 * Search for a start/middle/end thing.
15242 * Used by searchpair(), see its documentation for the details.
15243 * Returns 0 or -1 for no match,
15244 */
15245 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015246do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15247 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015248 char_u *spat; /* start pattern */
15249 char_u *mpat; /* middle pattern */
15250 char_u *epat; /* end pattern */
15251 int dir; /* BACKWARD or FORWARD */
15252 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015253 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015254 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015255 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015256 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015257{
15258 char_u *save_cpo;
15259 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15260 long retval = 0;
15261 pos_T pos;
15262 pos_T firstpos;
15263 pos_T foundpos;
15264 pos_T save_cursor;
15265 pos_T save_pos;
15266 int n;
15267 int r;
15268 int nest = 1;
15269 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015270 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015271 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015272
15273 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15274 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015275 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015276
Bram Moolenaar76929292008-01-06 19:07:36 +000015277#ifdef FEAT_RELTIME
15278 /* Set the time limit, if there is one. */
15279 profile_setlimit(time_limit, &tm);
15280#endif
15281
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015282 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15283 * start/middle/end (pat3, for the top pair). */
15284 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15285 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15286 if (pat2 == NULL || pat3 == NULL)
15287 goto theend;
15288 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15289 if (*mpat == NUL)
15290 STRCPY(pat3, pat2);
15291 else
15292 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15293 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015294 if (flags & SP_START)
15295 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015296
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297 save_cursor = curwin->w_cursor;
15298 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015299 clearpos(&firstpos);
15300 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 pat = pat3;
15302 for (;;)
15303 {
15304 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015305 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15307 /* didn't find it or found the first match again: FAIL */
15308 break;
15309
15310 if (firstpos.lnum == 0)
15311 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015312 if (equalpos(pos, foundpos))
15313 {
15314 /* Found the same position again. Can happen with a pattern that
15315 * has "\zs" at the end and searching backwards. Advance one
15316 * character and try again. */
15317 if (dir == BACKWARD)
15318 decl(&pos);
15319 else
15320 incl(&pos);
15321 }
15322 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015324 /* clear the start flag to avoid getting stuck here */
15325 options &= ~SEARCH_START;
15326
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 /* If the skip pattern matches, ignore this match. */
15328 if (*skip != NUL)
15329 {
15330 save_pos = curwin->w_cursor;
15331 curwin->w_cursor = pos;
15332 r = eval_to_bool(skip, &err, NULL, FALSE);
15333 curwin->w_cursor = save_pos;
15334 if (err)
15335 {
15336 /* Evaluating {skip} caused an error, break here. */
15337 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015338 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 break;
15340 }
15341 if (r)
15342 continue;
15343 }
15344
15345 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15346 {
15347 /* Found end when searching backwards or start when searching
15348 * forward: nested pair. */
15349 ++nest;
15350 pat = pat2; /* nested, don't search for middle */
15351 }
15352 else
15353 {
15354 /* Found end when searching forward or start when searching
15355 * backward: end of (nested) pair; or found middle in outer pair. */
15356 if (--nest == 1)
15357 pat = pat3; /* outer level, search for middle */
15358 }
15359
15360 if (nest == 0)
15361 {
15362 /* Found the match: return matchcount or line number. */
15363 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015364 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015366 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015367 if (flags & SP_SETPCMARK)
15368 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 curwin->w_cursor = pos;
15370 if (!(flags & SP_REPEAT))
15371 break;
15372 nest = 1; /* search for next unmatched */
15373 }
15374 }
15375
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015376 if (match_pos != NULL)
15377 {
15378 /* Store the match cursor position */
15379 match_pos->lnum = curwin->w_cursor.lnum;
15380 match_pos->col = curwin->w_cursor.col + 1;
15381 }
15382
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015384 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 curwin->w_cursor = save_cursor;
15386
15387theend:
15388 vim_free(pat2);
15389 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015390 if (p_cpo == empty_option)
15391 p_cpo = save_cpo;
15392 else
15393 /* Darn, evaluating the {skip} expression changed the value. */
15394 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015395
15396 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397}
15398
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015399/*
15400 * "searchpos()" function
15401 */
15402 static void
15403f_searchpos(argvars, rettv)
15404 typval_T *argvars;
15405 typval_T *rettv;
15406{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015407 pos_T match_pos;
15408 int lnum = 0;
15409 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015410 int n;
15411 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015413 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015414 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015415
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015416 n = search_cmn(argvars, &match_pos, &flags);
15417 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015418 {
15419 lnum = match_pos.lnum;
15420 col = match_pos.col;
15421 }
15422
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015423 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15424 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015425 if (flags & SP_SUBPAT)
15426 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015427}
15428
15429
Bram Moolenaar0d660222005-01-07 21:51:51 +000015430 static void
15431f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015432 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015435#ifdef FEAT_CLIENTSERVER
15436 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015437 char_u *server = get_tv_string_chk(&argvars[0]);
15438 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439
Bram Moolenaar0d660222005-01-07 21:51:51 +000015440 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015441 if (server == NULL || reply == NULL)
15442 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015443 if (check_restricted() || check_secure())
15444 return;
15445# ifdef FEAT_X11
15446 if (check_connection() == FAIL)
15447 return;
15448# endif
15449
15450 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015452 EMSG(_("E258: Unable to send to client"));
15453 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015455 rettv->vval.v_number = 0;
15456#else
15457 rettv->vval.v_number = -1;
15458#endif
15459}
15460
Bram Moolenaar0d660222005-01-07 21:51:51 +000015461 static void
15462f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015463 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015465{
15466 char_u *r = NULL;
15467
15468#ifdef FEAT_CLIENTSERVER
15469# ifdef WIN32
15470 r = serverGetVimNames();
15471# else
15472 make_connection();
15473 if (X_DISPLAY != NULL)
15474 r = serverGetVimNames(X_DISPLAY);
15475# endif
15476#endif
15477 rettv->v_type = VAR_STRING;
15478 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479}
15480
15481/*
15482 * "setbufvar()" function
15483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015485f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015486 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015487 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488{
15489 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015492 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 char_u nbuf[NUMBUFLEN];
15494
15495 if (check_restricted() || check_secure())
15496 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015497 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15498 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 varp = &argvars[2];
15501
15502 if (buf != NULL && varname != NULL && varp != NULL)
15503 {
15504 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506
15507 if (*varname == '&')
15508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015509 long numval;
15510 char_u *strval;
15511 int error = FALSE;
15512
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015514 numval = get_tv_number_chk(varp, &error);
15515 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015516 if (!error && strval != NULL)
15517 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518 }
15519 else
15520 {
15521 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15522 if (bufvarname != NULL)
15523 {
15524 STRCPY(bufvarname, "b:");
15525 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015526 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527 vim_free(bufvarname);
15528 }
15529 }
15530
15531 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534}
15535
15536/*
15537 * "setcmdpos()" function
15538 */
15539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015540f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015541 typval_T *argvars;
15542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015544 int pos = (int)get_tv_number(&argvars[0]) - 1;
15545
15546 if (pos >= 0)
15547 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548}
15549
15550/*
15551 * "setline()" function
15552 */
15553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015555 typval_T *argvars;
15556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557{
15558 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015559 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015560 list_T *l = NULL;
15561 listitem_T *li = NULL;
15562 long added = 0;
15563 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015565 lnum = get_tv_lnum(&argvars[0]);
15566 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015568 l = argvars[1].vval.v_list;
15569 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015571 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015572 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015573
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015574 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015575 for (;;)
15576 {
15577 if (l != NULL)
15578 {
15579 /* list argument, get next string */
15580 if (li == NULL)
15581 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015582 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015583 li = li->li_next;
15584 }
15585
15586 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015587 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015588 break;
15589 if (lnum <= curbuf->b_ml.ml_line_count)
15590 {
15591 /* existing line, replace it */
15592 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15593 {
15594 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015595 if (lnum == curwin->w_cursor.lnum)
15596 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015597 rettv->vval.v_number = 0; /* OK */
15598 }
15599 }
15600 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15601 {
15602 /* lnum is one past the last line, append the line */
15603 ++added;
15604 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15605 rettv->vval.v_number = 0; /* OK */
15606 }
15607
15608 if (l == NULL) /* only one string argument */
15609 break;
15610 ++lnum;
15611 }
15612
15613 if (added > 0)
15614 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615}
15616
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015617static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15618
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015620 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015621 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015622 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015623set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015624 win_T *wp UNUSED;
15625 typval_T *list_arg UNUSED;
15626 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015627 typval_T *rettv;
15628{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015629#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015630 char_u *act;
15631 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015632#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015633
Bram Moolenaar2641f772005-03-25 21:58:17 +000015634 rettv->vval.v_number = -1;
15635
15636#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015637 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015638 EMSG(_(e_listreq));
15639 else
15640 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015641 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015642
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015643 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015644 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015645 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 if (act == NULL)
15647 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015648 if (*act == 'a' || *act == 'r')
15649 action = *act;
15650 }
15651
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015652 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015653 rettv->vval.v_number = 0;
15654 }
15655#endif
15656}
15657
15658/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015659 * "setloclist()" function
15660 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015661 static void
15662f_setloclist(argvars, rettv)
15663 typval_T *argvars;
15664 typval_T *rettv;
15665{
15666 win_T *win;
15667
15668 rettv->vval.v_number = -1;
15669
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015670 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015671 if (win != NULL)
15672 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15673}
15674
15675/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015676 * "setmatches()" function
15677 */
15678 static void
15679f_setmatches(argvars, rettv)
15680 typval_T *argvars;
15681 typval_T *rettv;
15682{
15683#ifdef FEAT_SEARCH_EXTRA
15684 list_T *l;
15685 listitem_T *li;
15686 dict_T *d;
15687
15688 rettv->vval.v_number = -1;
15689 if (argvars[0].v_type != VAR_LIST)
15690 {
15691 EMSG(_(e_listreq));
15692 return;
15693 }
15694 if ((l = argvars[0].vval.v_list) != NULL)
15695 {
15696
15697 /* To some extent make sure that we are dealing with a list from
15698 * "getmatches()". */
15699 li = l->lv_first;
15700 while (li != NULL)
15701 {
15702 if (li->li_tv.v_type != VAR_DICT
15703 || (d = li->li_tv.vval.v_dict) == NULL)
15704 {
15705 EMSG(_(e_invarg));
15706 return;
15707 }
15708 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15709 && dict_find(d, (char_u *)"pattern", -1) != NULL
15710 && dict_find(d, (char_u *)"priority", -1) != NULL
15711 && dict_find(d, (char_u *)"id", -1) != NULL))
15712 {
15713 EMSG(_(e_invarg));
15714 return;
15715 }
15716 li = li->li_next;
15717 }
15718
15719 clear_matches(curwin);
15720 li = l->lv_first;
15721 while (li != NULL)
15722 {
15723 d = li->li_tv.vval.v_dict;
15724 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15725 get_dict_string(d, (char_u *)"pattern", FALSE),
15726 (int)get_dict_number(d, (char_u *)"priority"),
15727 (int)get_dict_number(d, (char_u *)"id"));
15728 li = li->li_next;
15729 }
15730 rettv->vval.v_number = 0;
15731 }
15732#endif
15733}
15734
15735/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015736 * "setpos()" function
15737 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015738 static void
15739f_setpos(argvars, rettv)
15740 typval_T *argvars;
15741 typval_T *rettv;
15742{
15743 pos_T pos;
15744 int fnum;
15745 char_u *name;
15746
Bram Moolenaar08250432008-02-13 11:42:46 +000015747 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015748 name = get_tv_string_chk(argvars);
15749 if (name != NULL)
15750 {
15751 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15752 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015753 if (--pos.col < 0)
15754 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015755 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015756 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015757 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015758 if (fnum == curbuf->b_fnum)
15759 {
15760 curwin->w_cursor = pos;
15761 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015762 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015763 }
15764 else
15765 EMSG(_(e_invarg));
15766 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015767 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15768 {
15769 /* set mark */
15770 if (setmark_pos(name[1], &pos, fnum) == OK)
15771 rettv->vval.v_number = 0;
15772 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015773 else
15774 EMSG(_(e_invarg));
15775 }
15776 }
15777}
15778
15779/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015780 * "setqflist()" function
15781 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015782 static void
15783f_setqflist(argvars, rettv)
15784 typval_T *argvars;
15785 typval_T *rettv;
15786{
15787 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15788}
15789
15790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 * "setreg()" function
15792 */
15793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015794f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015795 typval_T *argvars;
15796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797{
15798 int regname;
15799 char_u *strregname;
15800 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015801 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 int append;
15803 char_u yank_type;
15804 long block_len;
15805
15806 block_len = -1;
15807 yank_type = MAUTO;
15808 append = FALSE;
15809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015810 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015811 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015813 if (strregname == NULL)
15814 return; /* type error; errmsg already given */
15815 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 if (regname == 0 || regname == '@')
15817 regname = '"';
15818 else if (regname == '=')
15819 return;
15820
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015821 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015823 stropt = get_tv_string_chk(&argvars[2]);
15824 if (stropt == NULL)
15825 return; /* type error */
15826 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827 switch (*stropt)
15828 {
15829 case 'a': case 'A': /* append */
15830 append = TRUE;
15831 break;
15832 case 'v': case 'c': /* character-wise selection */
15833 yank_type = MCHAR;
15834 break;
15835 case 'V': case 'l': /* line-wise selection */
15836 yank_type = MLINE;
15837 break;
15838#ifdef FEAT_VISUAL
15839 case 'b': case Ctrl_V: /* block-wise selection */
15840 yank_type = MBLOCK;
15841 if (VIM_ISDIGIT(stropt[1]))
15842 {
15843 ++stropt;
15844 block_len = getdigits(&stropt) - 1;
15845 --stropt;
15846 }
15847 break;
15848#endif
15849 }
15850 }
15851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015852 strval = get_tv_string_chk(&argvars[1]);
15853 if (strval != NULL)
15854 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015856 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857}
15858
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015859/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015860 * "settabvar()" function
15861 */
15862 static void
15863f_settabvar(argvars, rettv)
15864 typval_T *argvars;
15865 typval_T *rettv;
15866{
15867 tabpage_T *save_curtab;
15868 char_u *varname, *tabvarname;
15869 typval_T *varp;
15870 tabpage_T *tp;
15871
15872 rettv->vval.v_number = 0;
15873
15874 if (check_restricted() || check_secure())
15875 return;
15876
15877 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15878 varname = get_tv_string_chk(&argvars[1]);
15879 varp = &argvars[2];
15880
15881 if (tp != NULL && varname != NULL && varp != NULL)
15882 {
15883 save_curtab = curtab;
15884 goto_tabpage_tp(tp);
15885
15886 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
15887 if (tabvarname != NULL)
15888 {
15889 STRCPY(tabvarname, "t:");
15890 STRCPY(tabvarname + 2, varname);
15891 set_var(tabvarname, varp, TRUE);
15892 vim_free(tabvarname);
15893 }
15894
15895 /* Restore current tabpage */
15896 if (valid_tabpage(save_curtab))
15897 goto_tabpage_tp(save_curtab);
15898 }
15899}
15900
15901/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015902 * "settabwinvar()" function
15903 */
15904 static void
15905f_settabwinvar(argvars, rettv)
15906 typval_T *argvars;
15907 typval_T *rettv;
15908{
15909 setwinvar(argvars, rettv, 1);
15910}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911
15912/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015913 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015916f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015917 typval_T *argvars;
15918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015920 setwinvar(argvars, rettv, 0);
15921}
15922
15923/*
15924 * "setwinvar()" and "settabwinvar()" functions
15925 */
15926 static void
15927setwinvar(argvars, rettv, off)
15928 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015929 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015930 int off;
15931{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 win_T *win;
15933#ifdef FEAT_WINDOWS
15934 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015935 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936#endif
15937 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015938 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015940 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941
15942 if (check_restricted() || check_secure())
15943 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015944
15945#ifdef FEAT_WINDOWS
15946 if (off == 1)
15947 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15948 else
15949 tp = curtab;
15950#endif
15951 win = find_win_by_nr(&argvars[off], tp);
15952 varname = get_tv_string_chk(&argvars[off + 1]);
15953 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954
15955 if (win != NULL && varname != NULL && varp != NULL)
15956 {
15957#ifdef FEAT_WINDOWS
15958 /* set curwin to be our win, temporarily */
15959 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015960 save_curtab = curtab;
15961 goto_tabpage_tp(tp);
15962 if (!win_valid(win))
15963 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 curwin = win;
15965 curbuf = curwin->w_buffer;
15966#endif
15967
15968 if (*varname == '&')
15969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015970 long numval;
15971 char_u *strval;
15972 int error = FALSE;
15973
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015975 numval = get_tv_number_chk(varp, &error);
15976 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015977 if (!error && strval != NULL)
15978 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979 }
15980 else
15981 {
15982 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15983 if (winvarname != NULL)
15984 {
15985 STRCPY(winvarname, "w:");
15986 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015987 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988 vim_free(winvarname);
15989 }
15990 }
15991
15992#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015993 /* Restore current tabpage and window, if still valid (autocomands can
15994 * make them invalid). */
15995 if (valid_tabpage(save_curtab))
15996 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 if (win_valid(save_curwin))
15998 {
15999 curwin = save_curwin;
16000 curbuf = curwin->w_buffer;
16001 }
16002#endif
16003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004}
16005
16006/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016007 * "shellescape({string})" function
16008 */
16009 static void
16010f_shellescape(argvars, rettv)
16011 typval_T *argvars;
16012 typval_T *rettv;
16013{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016014 rettv->vval.v_string = vim_strsave_shellescape(
16015 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016016 rettv->v_type = VAR_STRING;
16017}
16018
16019/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016020 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 */
16022 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016023f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016024 typval_T *argvars;
16025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016027 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028
Bram Moolenaar0d660222005-01-07 21:51:51 +000016029 p = get_tv_string(&argvars[0]);
16030 rettv->vval.v_string = vim_strsave(p);
16031 simplify_filename(rettv->vval.v_string); /* simplify in place */
16032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033}
16034
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016035#ifdef FEAT_FLOAT
16036/*
16037 * "sin()" function
16038 */
16039 static void
16040f_sin(argvars, rettv)
16041 typval_T *argvars;
16042 typval_T *rettv;
16043{
16044 float_T f;
16045
16046 rettv->v_type = VAR_FLOAT;
16047 if (get_float_arg(argvars, &f) == OK)
16048 rettv->vval.v_float = sin(f);
16049 else
16050 rettv->vval.v_float = 0.0;
16051}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016052
16053/*
16054 * "sinh()" function
16055 */
16056 static void
16057f_sinh(argvars, rettv)
16058 typval_T *argvars;
16059 typval_T *rettv;
16060{
16061 float_T f;
16062
16063 rettv->v_type = VAR_FLOAT;
16064 if (get_float_arg(argvars, &f) == OK)
16065 rettv->vval.v_float = sinh(f);
16066 else
16067 rettv->vval.v_float = 0.0;
16068}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016069#endif
16070
Bram Moolenaar0d660222005-01-07 21:51:51 +000016071static int
16072#ifdef __BORLANDC__
16073 _RTLENTRYF
16074#endif
16075 item_compare __ARGS((const void *s1, const void *s2));
16076static int
16077#ifdef __BORLANDC__
16078 _RTLENTRYF
16079#endif
16080 item_compare2 __ARGS((const void *s1, const void *s2));
16081
16082static int item_compare_ic;
16083static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016084static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085#define ITEM_COMPARE_FAIL 999
16086
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016088 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090 static int
16091#ifdef __BORLANDC__
16092_RTLENTRYF
16093#endif
16094item_compare(s1, s2)
16095 const void *s1;
16096 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016098 char_u *p1, *p2;
16099 char_u *tofree1, *tofree2;
16100 int res;
16101 char_u numbuf1[NUMBUFLEN];
16102 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016104 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16105 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016106 if (p1 == NULL)
16107 p1 = (char_u *)"";
16108 if (p2 == NULL)
16109 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110 if (item_compare_ic)
16111 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016113 res = STRCMP(p1, p2);
16114 vim_free(tofree1);
16115 vim_free(tofree2);
16116 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117}
16118
16119 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016120#ifdef __BORLANDC__
16121_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016123item_compare2(s1, s2)
16124 const void *s1;
16125 const void *s2;
16126{
16127 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016128 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016129 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016130 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016132 /* shortcut after failure in previous call; compare all items equal */
16133 if (item_compare_func_err)
16134 return 0;
16135
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016136 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16137 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16139 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140
16141 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016142 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016143 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016144 clear_tv(&argv[0]);
16145 clear_tv(&argv[1]);
16146
16147 if (res == FAIL)
16148 res = ITEM_COMPARE_FAIL;
16149 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016150 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16151 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016152 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016153 clear_tv(&rettv);
16154 return res;
16155}
16156
16157/*
16158 * "sort({list})" function
16159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016161f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016162 typval_T *argvars;
16163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164{
Bram Moolenaar33570922005-01-25 22:26:29 +000016165 list_T *l;
16166 listitem_T *li;
16167 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168 long len;
16169 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171 if (argvars[0].v_type != VAR_LIST)
16172 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 else
16174 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016175 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016176 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177 return;
16178 rettv->vval.v_list = l;
16179 rettv->v_type = VAR_LIST;
16180 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181
Bram Moolenaar0d660222005-01-07 21:51:51 +000016182 len = list_len(l);
16183 if (len <= 1)
16184 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185
Bram Moolenaar0d660222005-01-07 21:51:51 +000016186 item_compare_ic = FALSE;
16187 item_compare_func = NULL;
16188 if (argvars[1].v_type != VAR_UNKNOWN)
16189 {
16190 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016191 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016192 else
16193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016194 int error = FALSE;
16195
16196 i = get_tv_number_chk(&argvars[1], &error);
16197 if (error)
16198 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199 if (i == 1)
16200 item_compare_ic = TRUE;
16201 else
16202 item_compare_func = get_tv_string(&argvars[1]);
16203 }
16204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205
Bram Moolenaar0d660222005-01-07 21:51:51 +000016206 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016207 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016208 if (ptrs == NULL)
16209 return;
16210 i = 0;
16211 for (li = l->lv_first; li != NULL; li = li->li_next)
16212 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016214 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016215 /* test the compare function */
16216 if (item_compare_func != NULL
16217 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16218 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016219 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016221 {
16222 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016223 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016224 item_compare_func == NULL ? item_compare : item_compare2);
16225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016226 if (!item_compare_func_err)
16227 {
16228 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016229 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016230 l->lv_len = 0;
16231 for (i = 0; i < len; ++i)
16232 list_append(l, ptrs[i]);
16233 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016234 }
16235
16236 vim_free(ptrs);
16237 }
16238}
16239
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016240/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016241 * "soundfold({word})" function
16242 */
16243 static void
16244f_soundfold(argvars, rettv)
16245 typval_T *argvars;
16246 typval_T *rettv;
16247{
16248 char_u *s;
16249
16250 rettv->v_type = VAR_STRING;
16251 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016252#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016253 rettv->vval.v_string = eval_soundfold(s);
16254#else
16255 rettv->vval.v_string = vim_strsave(s);
16256#endif
16257}
16258
16259/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016260 * "spellbadword()" function
16261 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016262 static void
16263f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016264 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016265 typval_T *rettv;
16266{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016267 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016268 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016269 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016270
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016271 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016272 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016273
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016274#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016275 if (argvars[0].v_type == VAR_UNKNOWN)
16276 {
16277 /* Find the start and length of the badly spelled word. */
16278 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16279 if (len != 0)
16280 word = ml_get_cursor();
16281 }
16282 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16283 {
16284 char_u *str = get_tv_string_chk(&argvars[0]);
16285 int capcol = -1;
16286
16287 if (str != NULL)
16288 {
16289 /* Check the argument for spelling. */
16290 while (*str != NUL)
16291 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016292 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016293 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016294 {
16295 word = str;
16296 break;
16297 }
16298 str += len;
16299 }
16300 }
16301 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016302#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016304 list_append_string(rettv->vval.v_list, word, len);
16305 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016306 attr == HLF_SPB ? "bad" :
16307 attr == HLF_SPR ? "rare" :
16308 attr == HLF_SPL ? "local" :
16309 attr == HLF_SPC ? "caps" :
16310 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016311}
16312
16313/*
16314 * "spellsuggest()" function
16315 */
16316 static void
16317f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016318 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016319 typval_T *rettv;
16320{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016321#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016322 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016323 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016324 int maxcount;
16325 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016326 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016327 listitem_T *li;
16328 int need_capital = FALSE;
16329#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016330
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016331 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016332 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016333
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016334#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016335 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16336 {
16337 str = get_tv_string(&argvars[0]);
16338 if (argvars[1].v_type != VAR_UNKNOWN)
16339 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016340 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016341 if (maxcount <= 0)
16342 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016343 if (argvars[2].v_type != VAR_UNKNOWN)
16344 {
16345 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16346 if (typeerr)
16347 return;
16348 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016349 }
16350 else
16351 maxcount = 25;
16352
Bram Moolenaar4770d092006-01-12 23:22:24 +000016353 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016354
16355 for (i = 0; i < ga.ga_len; ++i)
16356 {
16357 str = ((char_u **)ga.ga_data)[i];
16358
16359 li = listitem_alloc();
16360 if (li == NULL)
16361 vim_free(str);
16362 else
16363 {
16364 li->li_tv.v_type = VAR_STRING;
16365 li->li_tv.v_lock = 0;
16366 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016367 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016368 }
16369 }
16370 ga_clear(&ga);
16371 }
16372#endif
16373}
16374
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016376f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016377 typval_T *argvars;
16378 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016379{
16380 char_u *str;
16381 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016382 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383 regmatch_T regmatch;
16384 char_u patbuf[NUMBUFLEN];
16385 char_u *save_cpo;
16386 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016387 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016388 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016389 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390
16391 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16392 save_cpo = p_cpo;
16393 p_cpo = (char_u *)"";
16394
16395 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016396 if (argvars[1].v_type != VAR_UNKNOWN)
16397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016398 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16399 if (pat == NULL)
16400 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016401 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016402 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016403 }
16404 if (pat == NULL || *pat == NUL)
16405 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016407 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016409 if (typeerr)
16410 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16413 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016415 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016416 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016418 if (*str == NUL)
16419 match = FALSE; /* empty item at the end */
16420 else
16421 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016422 if (match)
16423 end = regmatch.startp[0];
16424 else
16425 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016426 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16427 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016428 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016429 if (list_append_string(rettv->vval.v_list, str,
16430 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016431 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016432 }
16433 if (!match)
16434 break;
16435 /* Advance to just after the match. */
16436 if (regmatch.endp[0] > str)
16437 col = 0;
16438 else
16439 {
16440 /* Don't get stuck at the same match. */
16441#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016442 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443#else
16444 col = 1;
16445#endif
16446 }
16447 str = regmatch.endp[0];
16448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452
Bram Moolenaar0d660222005-01-07 21:51:51 +000016453 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454}
16455
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016456#ifdef FEAT_FLOAT
16457/*
16458 * "sqrt()" function
16459 */
16460 static void
16461f_sqrt(argvars, rettv)
16462 typval_T *argvars;
16463 typval_T *rettv;
16464{
16465 float_T f;
16466
16467 rettv->v_type = VAR_FLOAT;
16468 if (get_float_arg(argvars, &f) == OK)
16469 rettv->vval.v_float = sqrt(f);
16470 else
16471 rettv->vval.v_float = 0.0;
16472}
16473
16474/*
16475 * "str2float()" function
16476 */
16477 static void
16478f_str2float(argvars, rettv)
16479 typval_T *argvars;
16480 typval_T *rettv;
16481{
16482 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16483
16484 if (*p == '+')
16485 p = skipwhite(p + 1);
16486 (void)string2float(p, &rettv->vval.v_float);
16487 rettv->v_type = VAR_FLOAT;
16488}
16489#endif
16490
Bram Moolenaar2c932302006-03-18 21:42:09 +000016491/*
16492 * "str2nr()" function
16493 */
16494 static void
16495f_str2nr(argvars, rettv)
16496 typval_T *argvars;
16497 typval_T *rettv;
16498{
16499 int base = 10;
16500 char_u *p;
16501 long n;
16502
16503 if (argvars[1].v_type != VAR_UNKNOWN)
16504 {
16505 base = get_tv_number(&argvars[1]);
16506 if (base != 8 && base != 10 && base != 16)
16507 {
16508 EMSG(_(e_invarg));
16509 return;
16510 }
16511 }
16512
16513 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016514 if (*p == '+')
16515 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016516 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16517 rettv->vval.v_number = n;
16518}
16519
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520#ifdef HAVE_STRFTIME
16521/*
16522 * "strftime({format}[, {time}])" function
16523 */
16524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016525f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016526 typval_T *argvars;
16527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528{
16529 char_u result_buf[256];
16530 struct tm *curtime;
16531 time_t seconds;
16532 char_u *p;
16533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016534 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016536 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016537 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 seconds = time(NULL);
16539 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016540 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541 curtime = localtime(&seconds);
16542 /* MSVC returns NULL for an invalid value of seconds. */
16543 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016544 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 else
16546 {
16547# ifdef FEAT_MBYTE
16548 vimconv_T conv;
16549 char_u *enc;
16550
16551 conv.vc_type = CONV_NONE;
16552 enc = enc_locale();
16553 convert_setup(&conv, p_enc, enc);
16554 if (conv.vc_type != CONV_NONE)
16555 p = string_convert(&conv, p, NULL);
16556# endif
16557 if (p != NULL)
16558 (void)strftime((char *)result_buf, sizeof(result_buf),
16559 (char *)p, curtime);
16560 else
16561 result_buf[0] = NUL;
16562
16563# ifdef FEAT_MBYTE
16564 if (conv.vc_type != CONV_NONE)
16565 vim_free(p);
16566 convert_setup(&conv, enc, p_enc);
16567 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016568 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 else
16570# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016571 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572
16573# ifdef FEAT_MBYTE
16574 /* Release conversion descriptors */
16575 convert_setup(&conv, NULL, NULL);
16576 vim_free(enc);
16577# endif
16578 }
16579}
16580#endif
16581
16582/*
16583 * "stridx()" function
16584 */
16585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016586f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016587 typval_T *argvars;
16588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589{
16590 char_u buf[NUMBUFLEN];
16591 char_u *needle;
16592 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016593 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016595 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016597 needle = get_tv_string_chk(&argvars[1]);
16598 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016599 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016600 if (needle == NULL || haystack == NULL)
16601 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602
Bram Moolenaar33570922005-01-25 22:26:29 +000016603 if (argvars[2].v_type != VAR_UNKNOWN)
16604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016605 int error = FALSE;
16606
16607 start_idx = get_tv_number_chk(&argvars[2], &error);
16608 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016609 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016610 if (start_idx >= 0)
16611 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016612 }
16613
16614 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16615 if (pos != NULL)
16616 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617}
16618
16619/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016620 * "string()" function
16621 */
16622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016623f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016624 typval_T *argvars;
16625 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016626{
16627 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016628 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016630 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016631 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016632 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016633 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635}
16636
16637/*
16638 * "strlen()" function
16639 */
16640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016641f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016642 typval_T *argvars;
16643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016645 rettv->vval.v_number = (varnumber_T)(STRLEN(
16646 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647}
16648
16649/*
16650 * "strpart()" function
16651 */
16652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016653f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016654 typval_T *argvars;
16655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656{
16657 char_u *p;
16658 int n;
16659 int len;
16660 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016661 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016663 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 slen = (int)STRLEN(p);
16665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016666 n = get_tv_number_chk(&argvars[1], &error);
16667 if (error)
16668 len = 0;
16669 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016670 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 else
16672 len = slen - n; /* default len: all bytes that are available. */
16673
16674 /*
16675 * Only return the overlap between the specified part and the actual
16676 * string.
16677 */
16678 if (n < 0)
16679 {
16680 len += n;
16681 n = 0;
16682 }
16683 else if (n > slen)
16684 n = slen;
16685 if (len < 0)
16686 len = 0;
16687 else if (n + len > slen)
16688 len = slen - n;
16689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016690 rettv->v_type = VAR_STRING;
16691 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692}
16693
16694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016695 * "strridx()" function
16696 */
16697 static void
16698f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016699 typval_T *argvars;
16700 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016701{
16702 char_u buf[NUMBUFLEN];
16703 char_u *needle;
16704 char_u *haystack;
16705 char_u *rest;
16706 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016707 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016709 needle = get_tv_string_chk(&argvars[1]);
16710 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016711
16712 rettv->vval.v_number = -1;
16713 if (needle == NULL || haystack == NULL)
16714 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016715
16716 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016717 if (argvars[2].v_type != VAR_UNKNOWN)
16718 {
16719 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016720 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016721 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016722 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016723 }
16724 else
16725 end_idx = haystack_len;
16726
Bram Moolenaar0d660222005-01-07 21:51:51 +000016727 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016728 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016730 lastmatch = haystack + end_idx;
16731 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016732 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016733 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016734 for (rest = haystack; *rest != '\0'; ++rest)
16735 {
16736 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016737 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016738 break;
16739 lastmatch = rest;
16740 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016741 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016742
16743 if (lastmatch == NULL)
16744 rettv->vval.v_number = -1;
16745 else
16746 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16747}
16748
16749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 * "strtrans()" function
16751 */
16752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016753f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016754 typval_T *argvars;
16755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016757 rettv->v_type = VAR_STRING;
16758 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759}
16760
16761/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016762 * "submatch()" function
16763 */
16764 static void
16765f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016766 typval_T *argvars;
16767 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016768{
16769 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016770 rettv->vval.v_string =
16771 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016772}
16773
16774/*
16775 * "substitute()" function
16776 */
16777 static void
16778f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016779 typval_T *argvars;
16780 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781{
16782 char_u patbuf[NUMBUFLEN];
16783 char_u subbuf[NUMBUFLEN];
16784 char_u flagsbuf[NUMBUFLEN];
16785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016786 char_u *str = get_tv_string_chk(&argvars[0]);
16787 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16788 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16789 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16790
Bram Moolenaar0d660222005-01-07 21:51:51 +000016791 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016792 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16793 rettv->vval.v_string = NULL;
16794 else
16795 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016796}
16797
16798/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016799 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016802f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016803 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805{
16806 int id = 0;
16807#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016808 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 long col;
16810 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016811 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016813 lnum = get_tv_lnum(argvars); /* -1 on type error */
16814 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16815 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016817 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016818 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016819 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820#endif
16821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016822 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823}
16824
16825/*
16826 * "synIDattr(id, what [, mode])" function
16827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016829f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832{
16833 char_u *p = NULL;
16834#ifdef FEAT_SYN_HL
16835 int id;
16836 char_u *what;
16837 char_u *mode;
16838 char_u modebuf[NUMBUFLEN];
16839 int modec;
16840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016841 id = get_tv_number(&argvars[0]);
16842 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016843 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016845 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 modec = TOLOWER_ASC(mode[0]);
16847 if (modec != 't' && modec != 'c'
16848#ifdef FEAT_GUI
16849 && modec != 'g'
16850#endif
16851 )
16852 modec = 0; /* replace invalid with current */
16853 }
16854 else
16855 {
16856#ifdef FEAT_GUI
16857 if (gui.in_use)
16858 modec = 'g';
16859 else
16860#endif
16861 if (t_colors > 1)
16862 modec = 'c';
16863 else
16864 modec = 't';
16865 }
16866
16867
16868 switch (TOLOWER_ASC(what[0]))
16869 {
16870 case 'b':
16871 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16872 p = highlight_color(id, what, modec);
16873 else /* bold */
16874 p = highlight_has_attr(id, HL_BOLD, modec);
16875 break;
16876
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016877 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 p = highlight_color(id, what, modec);
16879 break;
16880
16881 case 'i':
16882 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16883 p = highlight_has_attr(id, HL_INVERSE, modec);
16884 else /* italic */
16885 p = highlight_has_attr(id, HL_ITALIC, modec);
16886 break;
16887
16888 case 'n': /* name */
16889 p = get_highlight_name(NULL, id - 1);
16890 break;
16891
16892 case 'r': /* reverse */
16893 p = highlight_has_attr(id, HL_INVERSE, modec);
16894 break;
16895
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016896 case 's':
16897 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16898 p = highlight_color(id, what, modec);
16899 else /* standout */
16900 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 break;
16902
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016903 case 'u':
16904 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16905 /* underline */
16906 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16907 else
16908 /* undercurl */
16909 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910 break;
16911 }
16912
16913 if (p != NULL)
16914 p = vim_strsave(p);
16915#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016916 rettv->v_type = VAR_STRING;
16917 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918}
16919
16920/*
16921 * "synIDtrans(id)" function
16922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016924f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016925 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927{
16928 int id;
16929
16930#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016931 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932
16933 if (id > 0)
16934 id = syn_get_final_id(id);
16935 else
16936#endif
16937 id = 0;
16938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016939 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940}
16941
16942/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016943 * "synstack(lnum, col)" function
16944 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016945 static void
16946f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016947 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016948 typval_T *rettv;
16949{
16950#ifdef FEAT_SYN_HL
16951 long lnum;
16952 long col;
16953 int i;
16954 int id;
16955#endif
16956
16957 rettv->v_type = VAR_LIST;
16958 rettv->vval.v_list = NULL;
16959
16960#ifdef FEAT_SYN_HL
16961 lnum = get_tv_lnum(argvars); /* -1 on type error */
16962 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16963
16964 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016965 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016966 && rettv_list_alloc(rettv) != FAIL)
16967 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016968 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016969 for (i = 0; ; ++i)
16970 {
16971 id = syn_get_stack_item(i);
16972 if (id < 0)
16973 break;
16974 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16975 break;
16976 }
16977 }
16978#endif
16979}
16980
16981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 * "system()" function
16983 */
16984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016985f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016986 typval_T *argvars;
16987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016989 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016991 char_u *infile = NULL;
16992 char_u buf[NUMBUFLEN];
16993 int err = FALSE;
16994 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016996 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016997 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016998
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016999 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017000 {
17001 /*
17002 * Write the string to a temp file, to be used for input of the shell
17003 * command.
17004 */
17005 if ((infile = vim_tempname('i')) == NULL)
17006 {
17007 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017008 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017009 }
17010
17011 fd = mch_fopen((char *)infile, WRITEBIN);
17012 if (fd == NULL)
17013 {
17014 EMSG2(_(e_notopen), infile);
17015 goto done;
17016 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017017 p = get_tv_string_buf_chk(&argvars[1], buf);
17018 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017019 {
17020 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017022 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017023 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17024 err = TRUE;
17025 if (fclose(fd) != 0)
17026 err = TRUE;
17027 if (err)
17028 {
17029 EMSG(_("E677: Error writing temp file"));
17030 goto done;
17031 }
17032 }
17033
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017034 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17035 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017036
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037#ifdef USE_CR
17038 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017039 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 {
17041 char_u *s;
17042
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017043 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 {
17045 if (*s == CAR)
17046 *s = NL;
17047 }
17048 }
17049#else
17050# ifdef USE_CRNL
17051 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017052 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 {
17054 char_u *s, *d;
17055
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017056 d = res;
17057 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 {
17059 if (s[0] == CAR && s[1] == NL)
17060 ++s;
17061 *d++ = *s;
17062 }
17063 *d = NUL;
17064 }
17065# endif
17066#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017067
17068done:
17069 if (infile != NULL)
17070 {
17071 mch_remove(infile);
17072 vim_free(infile);
17073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017074 rettv->v_type = VAR_STRING;
17075 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076}
17077
17078/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017079 * "tabpagebuflist()" function
17080 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017081 static void
17082f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017083 typval_T *argvars UNUSED;
17084 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017085{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017086#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017087 tabpage_T *tp;
17088 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017089
17090 if (argvars[0].v_type == VAR_UNKNOWN)
17091 wp = firstwin;
17092 else
17093 {
17094 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17095 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017096 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017097 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017098 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017099 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017100 for (; wp != NULL; wp = wp->w_next)
17101 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017102 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017103 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017104 }
17105#endif
17106}
17107
17108
17109/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017110 * "tabpagenr()" function
17111 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017112 static void
17113f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017114 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017115 typval_T *rettv;
17116{
17117 int nr = 1;
17118#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017119 char_u *arg;
17120
17121 if (argvars[0].v_type != VAR_UNKNOWN)
17122 {
17123 arg = get_tv_string_chk(&argvars[0]);
17124 nr = 0;
17125 if (arg != NULL)
17126 {
17127 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017128 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017129 else
17130 EMSG2(_(e_invexpr2), arg);
17131 }
17132 }
17133 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017134 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017135#endif
17136 rettv->vval.v_number = nr;
17137}
17138
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017139
17140#ifdef FEAT_WINDOWS
17141static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17142
17143/*
17144 * Common code for tabpagewinnr() and winnr().
17145 */
17146 static int
17147get_winnr(tp, argvar)
17148 tabpage_T *tp;
17149 typval_T *argvar;
17150{
17151 win_T *twin;
17152 int nr = 1;
17153 win_T *wp;
17154 char_u *arg;
17155
17156 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17157 if (argvar->v_type != VAR_UNKNOWN)
17158 {
17159 arg = get_tv_string_chk(argvar);
17160 if (arg == NULL)
17161 nr = 0; /* type error; errmsg already given */
17162 else if (STRCMP(arg, "$") == 0)
17163 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17164 else if (STRCMP(arg, "#") == 0)
17165 {
17166 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17167 if (twin == NULL)
17168 nr = 0;
17169 }
17170 else
17171 {
17172 EMSG2(_(e_invexpr2), arg);
17173 nr = 0;
17174 }
17175 }
17176
17177 if (nr > 0)
17178 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17179 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017180 {
17181 if (wp == NULL)
17182 {
17183 /* didn't find it in this tabpage */
17184 nr = 0;
17185 break;
17186 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017187 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017188 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017189 return nr;
17190}
17191#endif
17192
17193/*
17194 * "tabpagewinnr()" function
17195 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017196 static void
17197f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017198 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017199 typval_T *rettv;
17200{
17201 int nr = 1;
17202#ifdef FEAT_WINDOWS
17203 tabpage_T *tp;
17204
17205 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17206 if (tp == NULL)
17207 nr = 0;
17208 else
17209 nr = get_winnr(tp, &argvars[1]);
17210#endif
17211 rettv->vval.v_number = nr;
17212}
17213
17214
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017215/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017216 * "tagfiles()" function
17217 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017218 static void
17219f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017220 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017221 typval_T *rettv;
17222{
17223 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017224 tagname_T tn;
17225 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017226
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017227 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017228 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017229
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017230 for (first = TRUE; ; first = FALSE)
17231 if (get_tagfname(&tn, first, fname) == FAIL
17232 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017233 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017234 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017235}
17236
17237/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017238 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017239 */
17240 static void
17241f_taglist(argvars, rettv)
17242 typval_T *argvars;
17243 typval_T *rettv;
17244{
17245 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017246
17247 tag_pattern = get_tv_string(&argvars[0]);
17248
17249 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017250 if (*tag_pattern == NUL)
17251 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017252
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017253 if (rettv_list_alloc(rettv) == OK)
17254 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017255}
17256
17257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 * "tempname()" function
17259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017261f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017262 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264{
17265 static int x = 'A';
17266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017267 rettv->v_type = VAR_STRING;
17268 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269
17270 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17271 * names. Skip 'I' and 'O', they are used for shell redirection. */
17272 do
17273 {
17274 if (x == 'Z')
17275 x = '0';
17276 else if (x == '9')
17277 x = 'A';
17278 else
17279 {
17280#ifdef EBCDIC
17281 if (x == 'I')
17282 x = 'J';
17283 else if (x == 'R')
17284 x = 'S';
17285 else
17286#endif
17287 ++x;
17288 }
17289 } while (x == 'I' || x == 'O');
17290}
17291
17292/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017293 * "test(list)" function: Just checking the walls...
17294 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017295 static void
17296f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017297 typval_T *argvars UNUSED;
17298 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017299{
17300 /* Used for unit testing. Change the code below to your liking. */
17301#if 0
17302 listitem_T *li;
17303 list_T *l;
17304 char_u *bad, *good;
17305
17306 if (argvars[0].v_type != VAR_LIST)
17307 return;
17308 l = argvars[0].vval.v_list;
17309 if (l == NULL)
17310 return;
17311 li = l->lv_first;
17312 if (li == NULL)
17313 return;
17314 bad = get_tv_string(&li->li_tv);
17315 li = li->li_next;
17316 if (li == NULL)
17317 return;
17318 good = get_tv_string(&li->li_tv);
17319 rettv->vval.v_number = test_edit_score(bad, good);
17320#endif
17321}
17322
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017323#ifdef FEAT_FLOAT
17324/*
17325 * "tan()" function
17326 */
17327 static void
17328f_tan(argvars, rettv)
17329 typval_T *argvars;
17330 typval_T *rettv;
17331{
17332 float_T f;
17333
17334 rettv->v_type = VAR_FLOAT;
17335 if (get_float_arg(argvars, &f) == OK)
17336 rettv->vval.v_float = tan(f);
17337 else
17338 rettv->vval.v_float = 0.0;
17339}
17340
17341/*
17342 * "tanh()" function
17343 */
17344 static void
17345f_tanh(argvars, rettv)
17346 typval_T *argvars;
17347 typval_T *rettv;
17348{
17349 float_T f;
17350
17351 rettv->v_type = VAR_FLOAT;
17352 if (get_float_arg(argvars, &f) == OK)
17353 rettv->vval.v_float = tanh(f);
17354 else
17355 rettv->vval.v_float = 0.0;
17356}
17357#endif
17358
Bram Moolenaard52d9742005-08-21 22:20:28 +000017359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 * "tolower(string)" function
17361 */
17362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017364 typval_T *argvars;
17365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366{
17367 char_u *p;
17368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369 p = vim_strsave(get_tv_string(&argvars[0]));
17370 rettv->v_type = VAR_STRING;
17371 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372
17373 if (p != NULL)
17374 while (*p != NUL)
17375 {
17376#ifdef FEAT_MBYTE
17377 int l;
17378
17379 if (enc_utf8)
17380 {
17381 int c, lc;
17382
17383 c = utf_ptr2char(p);
17384 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017385 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 /* TODO: reallocate string when byte count changes. */
17387 if (utf_char2len(lc) == l)
17388 utf_char2bytes(lc, p);
17389 p += l;
17390 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017391 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 p += l; /* skip multi-byte character */
17393 else
17394#endif
17395 {
17396 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17397 ++p;
17398 }
17399 }
17400}
17401
17402/*
17403 * "toupper(string)" function
17404 */
17405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017406f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017407 typval_T *argvars;
17408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017411 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412}
17413
17414/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017415 * "tr(string, fromstr, tostr)" function
17416 */
17417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017418f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017419 typval_T *argvars;
17420 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017421{
17422 char_u *instr;
17423 char_u *fromstr;
17424 char_u *tostr;
17425 char_u *p;
17426#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017427 int inlen;
17428 int fromlen;
17429 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017430 int idx;
17431 char_u *cpstr;
17432 int cplen;
17433 int first = TRUE;
17434#endif
17435 char_u buf[NUMBUFLEN];
17436 char_u buf2[NUMBUFLEN];
17437 garray_T ga;
17438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017439 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017440 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17441 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017442
17443 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017444 rettv->v_type = VAR_STRING;
17445 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017446 if (fromstr == NULL || tostr == NULL)
17447 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017448 ga_init2(&ga, (int)sizeof(char), 80);
17449
17450#ifdef FEAT_MBYTE
17451 if (!has_mbyte)
17452#endif
17453 /* not multi-byte: fromstr and tostr must be the same length */
17454 if (STRLEN(fromstr) != STRLEN(tostr))
17455 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017456#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017457error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017458#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017459 EMSG2(_(e_invarg2), fromstr);
17460 ga_clear(&ga);
17461 return;
17462 }
17463
17464 /* fromstr and tostr have to contain the same number of chars */
17465 while (*instr != NUL)
17466 {
17467#ifdef FEAT_MBYTE
17468 if (has_mbyte)
17469 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017470 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017471 cpstr = instr;
17472 cplen = inlen;
17473 idx = 0;
17474 for (p = fromstr; *p != NUL; p += fromlen)
17475 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017476 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017477 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17478 {
17479 for (p = tostr; *p != NUL; p += tolen)
17480 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017481 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017482 if (idx-- == 0)
17483 {
17484 cplen = tolen;
17485 cpstr = p;
17486 break;
17487 }
17488 }
17489 if (*p == NUL) /* tostr is shorter than fromstr */
17490 goto error;
17491 break;
17492 }
17493 ++idx;
17494 }
17495
17496 if (first && cpstr == instr)
17497 {
17498 /* Check that fromstr and tostr have the same number of
17499 * (multi-byte) characters. Done only once when a character
17500 * of instr doesn't appear in fromstr. */
17501 first = FALSE;
17502 for (p = tostr; *p != NUL; p += tolen)
17503 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017504 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017505 --idx;
17506 }
17507 if (idx != 0)
17508 goto error;
17509 }
17510
17511 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017512 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017513 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017514
17515 instr += inlen;
17516 }
17517 else
17518#endif
17519 {
17520 /* When not using multi-byte chars we can do it faster. */
17521 p = vim_strchr(fromstr, *instr);
17522 if (p != NULL)
17523 ga_append(&ga, tostr[p - fromstr]);
17524 else
17525 ga_append(&ga, *instr);
17526 ++instr;
17527 }
17528 }
17529
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017530 /* add a terminating NUL */
17531 ga_grow(&ga, 1);
17532 ga_append(&ga, NUL);
17533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017534 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017535}
17536
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017537#ifdef FEAT_FLOAT
17538/*
17539 * "trunc({float})" function
17540 */
17541 static void
17542f_trunc(argvars, rettv)
17543 typval_T *argvars;
17544 typval_T *rettv;
17545{
17546 float_T f;
17547
17548 rettv->v_type = VAR_FLOAT;
17549 if (get_float_arg(argvars, &f) == OK)
17550 /* trunc() is not in C90, use floor() or ceil() instead. */
17551 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17552 else
17553 rettv->vval.v_float = 0.0;
17554}
17555#endif
17556
Bram Moolenaar8299df92004-07-10 09:47:34 +000017557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 * "type(expr)" function
17559 */
17560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017561f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017562 typval_T *argvars;
17563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017565 int n;
17566
17567 switch (argvars[0].v_type)
17568 {
17569 case VAR_NUMBER: n = 0; break;
17570 case VAR_STRING: n = 1; break;
17571 case VAR_FUNC: n = 2; break;
17572 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017573 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017574#ifdef FEAT_FLOAT
17575 case VAR_FLOAT: n = 5; break;
17576#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017577 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17578 }
17579 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580}
17581
17582/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017583 * "values(dict)" function
17584 */
17585 static void
17586f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017587 typval_T *argvars;
17588 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017589{
17590 dict_list(argvars, rettv, 1);
17591}
17592
17593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 * "virtcol(string)" function
17595 */
17596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017597f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017598 typval_T *argvars;
17599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600{
17601 colnr_T vcol = 0;
17602 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017603 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017605 fp = var2fpos(&argvars[0], FALSE, &fnum);
17606 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17607 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 {
17609 getvvcol(curwin, fp, NULL, NULL, &vcol);
17610 ++vcol;
17611 }
17612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017613 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614}
17615
17616/*
17617 * "visualmode()" function
17618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017620f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017621 typval_T *argvars UNUSED;
17622 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623{
17624#ifdef FEAT_VISUAL
17625 char_u str[2];
17626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 str[0] = curbuf->b_visual_mode_eval;
17629 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631
17632 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017633 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635#endif
17636}
17637
17638/*
17639 * "winbufnr(nr)" function
17640 */
17641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017642f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017643 typval_T *argvars;
17644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645{
17646 win_T *wp;
17647
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017648 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017650 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017652 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653}
17654
17655/*
17656 * "wincol()" function
17657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017659f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662{
17663 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665}
17666
17667/*
17668 * "winheight(nr)" function
17669 */
17670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017672 typval_T *argvars;
17673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674{
17675 win_T *wp;
17676
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017677 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017681 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682}
17683
17684/*
17685 * "winline()" function
17686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017688f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691{
17692 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017693 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694}
17695
17696/*
17697 * "winnr()" function
17698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017701 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703{
17704 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017705
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017707 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017709 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710}
17711
17712/*
17713 * "winrestcmd()" function
17714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017716f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017717 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719{
17720#ifdef FEAT_WINDOWS
17721 win_T *wp;
17722 int winnr = 1;
17723 garray_T ga;
17724 char_u buf[50];
17725
17726 ga_init2(&ga, (int)sizeof(char), 70);
17727 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17728 {
17729 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17730 ga_concat(&ga, buf);
17731# ifdef FEAT_VERTSPLIT
17732 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17733 ga_concat(&ga, buf);
17734# endif
17735 ++winnr;
17736 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017737 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017739 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017741 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017743 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744}
17745
17746/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017747 * "winrestview()" function
17748 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017749 static void
17750f_winrestview(argvars, rettv)
17751 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017752 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017753{
17754 dict_T *dict;
17755
17756 if (argvars[0].v_type != VAR_DICT
17757 || (dict = argvars[0].vval.v_dict) == NULL)
17758 EMSG(_(e_invarg));
17759 else
17760 {
17761 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17762 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17763#ifdef FEAT_VIRTUALEDIT
17764 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17765#endif
17766 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017767 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017768
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017769 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017770#ifdef FEAT_DIFF
17771 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17772#endif
17773 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17774 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17775
17776 check_cursor();
17777 changed_cline_bef_curs();
17778 invalidate_botline();
17779 redraw_later(VALID);
17780
17781 if (curwin->w_topline == 0)
17782 curwin->w_topline = 1;
17783 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17784 curwin->w_topline = curbuf->b_ml.ml_line_count;
17785#ifdef FEAT_DIFF
17786 check_topfill(curwin, TRUE);
17787#endif
17788 }
17789}
17790
17791/*
17792 * "winsaveview()" function
17793 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017794 static void
17795f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017796 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017797 typval_T *rettv;
17798{
17799 dict_T *dict;
17800
17801 dict = dict_alloc();
17802 if (dict == NULL)
17803 return;
17804 rettv->v_type = VAR_DICT;
17805 rettv->vval.v_dict = dict;
17806 ++dict->dv_refcount;
17807
17808 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17809 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17810#ifdef FEAT_VIRTUALEDIT
17811 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17812#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017813 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017814 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17815
17816 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17817#ifdef FEAT_DIFF
17818 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17819#endif
17820 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17821 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17822}
17823
17824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825 * "winwidth(nr)" function
17826 */
17827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017828f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017829 typval_T *argvars;
17830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831{
17832 win_T *wp;
17833
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017834 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 else
17838#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017841 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842#endif
17843}
17844
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017846 * "writefile()" function
17847 */
17848 static void
17849f_writefile(argvars, rettv)
17850 typval_T *argvars;
17851 typval_T *rettv;
17852{
17853 int binary = FALSE;
17854 char_u *fname;
17855 FILE *fd;
17856 listitem_T *li;
17857 char_u *s;
17858 int ret = 0;
17859 int c;
17860
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017861 if (check_restricted() || check_secure())
17862 return;
17863
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017864 if (argvars[0].v_type != VAR_LIST)
17865 {
17866 EMSG2(_(e_listarg), "writefile()");
17867 return;
17868 }
17869 if (argvars[0].vval.v_list == NULL)
17870 return;
17871
17872 if (argvars[2].v_type != VAR_UNKNOWN
17873 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17874 binary = TRUE;
17875
17876 /* Always open the file in binary mode, library functions have a mind of
17877 * their own about CR-LF conversion. */
17878 fname = get_tv_string(&argvars[1]);
17879 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17880 {
17881 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17882 ret = -1;
17883 }
17884 else
17885 {
17886 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17887 li = li->li_next)
17888 {
17889 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17890 {
17891 if (*s == '\n')
17892 c = putc(NUL, fd);
17893 else
17894 c = putc(*s, fd);
17895 if (c == EOF)
17896 {
17897 ret = -1;
17898 break;
17899 }
17900 }
17901 if (!binary || li->li_next != NULL)
17902 if (putc('\n', fd) == EOF)
17903 {
17904 ret = -1;
17905 break;
17906 }
17907 if (ret < 0)
17908 {
17909 EMSG(_(e_write));
17910 break;
17911 }
17912 }
17913 fclose(fd);
17914 }
17915
17916 rettv->vval.v_number = ret;
17917}
17918
17919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017921 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 */
17923 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017924var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017925 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017926 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017927 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017929 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017931 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932
Bram Moolenaara5525202006-03-02 22:52:09 +000017933 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017934 if (varp->v_type == VAR_LIST)
17935 {
17936 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017937 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017938 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017939 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017940
17941 l = varp->vval.v_list;
17942 if (l == NULL)
17943 return NULL;
17944
17945 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017946 pos.lnum = list_find_nr(l, 0L, &error);
17947 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017948 return NULL; /* invalid line number */
17949
17950 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017951 pos.col = list_find_nr(l, 1L, &error);
17952 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017953 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017954 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017955
17956 /* We accept "$" for the column number: last column. */
17957 li = list_find(l, 1L);
17958 if (li != NULL && li->li_tv.v_type == VAR_STRING
17959 && li->li_tv.vval.v_string != NULL
17960 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17961 pos.col = len + 1;
17962
Bram Moolenaara5525202006-03-02 22:52:09 +000017963 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017964 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017965 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017966 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017967
Bram Moolenaara5525202006-03-02 22:52:09 +000017968#ifdef FEAT_VIRTUALEDIT
17969 /* Get the virtual offset. Defaults to zero. */
17970 pos.coladd = list_find_nr(l, 2L, &error);
17971 if (error)
17972 pos.coladd = 0;
17973#endif
17974
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017975 return &pos;
17976 }
17977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017978 name = get_tv_string_chk(varp);
17979 if (name == NULL)
17980 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017981 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017983#ifdef FEAT_VISUAL
17984 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17985 {
17986 if (VIsual_active)
17987 return &VIsual;
17988 return &curwin->w_cursor;
17989 }
17990#endif
17991 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017993 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17995 return NULL;
17996 return pp;
17997 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017998
17999#ifdef FEAT_VIRTUALEDIT
18000 pos.coladd = 0;
18001#endif
18002
Bram Moolenaar477933c2007-07-17 14:32:23 +000018003 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018004 {
18005 pos.col = 0;
18006 if (name[1] == '0') /* "w0": first visible line */
18007 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018008 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018009 pos.lnum = curwin->w_topline;
18010 return &pos;
18011 }
18012 else if (name[1] == '$') /* "w$": last visible line */
18013 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018014 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018015 pos.lnum = curwin->w_botline - 1;
18016 return &pos;
18017 }
18018 }
18019 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018021 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 {
18023 pos.lnum = curbuf->b_ml.ml_line_count;
18024 pos.col = 0;
18025 }
18026 else
18027 {
18028 pos.lnum = curwin->w_cursor.lnum;
18029 pos.col = (colnr_T)STRLEN(ml_get_curline());
18030 }
18031 return &pos;
18032 }
18033 return NULL;
18034}
18035
18036/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018037 * Convert list in "arg" into a position and optional file number.
18038 * When "fnump" is NULL there is no file number, only 3 items.
18039 * Note that the column is passed on as-is, the caller may want to decrement
18040 * it to use 1 for the first column.
18041 * Return FAIL when conversion is not possible, doesn't check the position for
18042 * validity.
18043 */
18044 static int
18045list2fpos(arg, posp, fnump)
18046 typval_T *arg;
18047 pos_T *posp;
18048 int *fnump;
18049{
18050 list_T *l = arg->vval.v_list;
18051 long i = 0;
18052 long n;
18053
Bram Moolenaarbde35262006-07-23 20:12:24 +000018054 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18055 * when "fnump" isn't NULL and "coladd" is optional. */
18056 if (arg->v_type != VAR_LIST
18057 || l == NULL
18058 || l->lv_len < (fnump == NULL ? 2 : 3)
18059 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018060 return FAIL;
18061
18062 if (fnump != NULL)
18063 {
18064 n = list_find_nr(l, i++, NULL); /* fnum */
18065 if (n < 0)
18066 return FAIL;
18067 if (n == 0)
18068 n = curbuf->b_fnum; /* current buffer */
18069 *fnump = n;
18070 }
18071
18072 n = list_find_nr(l, i++, NULL); /* lnum */
18073 if (n < 0)
18074 return FAIL;
18075 posp->lnum = n;
18076
18077 n = list_find_nr(l, i++, NULL); /* col */
18078 if (n < 0)
18079 return FAIL;
18080 posp->col = n;
18081
18082#ifdef FEAT_VIRTUALEDIT
18083 n = list_find_nr(l, i, NULL);
18084 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018085 posp->coladd = 0;
18086 else
18087 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018088#endif
18089
18090 return OK;
18091}
18092
18093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 * Get the length of an environment variable name.
18095 * Advance "arg" to the first character after the name.
18096 * Return 0 for error.
18097 */
18098 static int
18099get_env_len(arg)
18100 char_u **arg;
18101{
18102 char_u *p;
18103 int len;
18104
18105 for (p = *arg; vim_isIDc(*p); ++p)
18106 ;
18107 if (p == *arg) /* no name found */
18108 return 0;
18109
18110 len = (int)(p - *arg);
18111 *arg = p;
18112 return len;
18113}
18114
18115/*
18116 * Get the length of the name of a function or internal variable.
18117 * "arg" is advanced to the first non-white character after the name.
18118 * Return 0 if something is wrong.
18119 */
18120 static int
18121get_id_len(arg)
18122 char_u **arg;
18123{
18124 char_u *p;
18125 int len;
18126
18127 /* Find the end of the name. */
18128 for (p = *arg; eval_isnamec(*p); ++p)
18129 ;
18130 if (p == *arg) /* no name found */
18131 return 0;
18132
18133 len = (int)(p - *arg);
18134 *arg = skipwhite(p);
18135
18136 return len;
18137}
18138
18139/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018140 * Get the length of the name of a variable or function.
18141 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018143 * Return -1 if curly braces expansion failed.
18144 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 * If the name contains 'magic' {}'s, expand them and return the
18146 * expanded name in an allocated string via 'alias' - caller must free.
18147 */
18148 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018149get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150 char_u **arg;
18151 char_u **alias;
18152 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018153 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154{
18155 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156 char_u *p;
18157 char_u *expr_start;
18158 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159
18160 *alias = NULL; /* default to no alias */
18161
18162 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18163 && (*arg)[2] == (int)KE_SNR)
18164 {
18165 /* hard coded <SNR>, already translated */
18166 *arg += 3;
18167 return get_id_len(arg) + 3;
18168 }
18169 len = eval_fname_script(*arg);
18170 if (len > 0)
18171 {
18172 /* literal "<SID>", "s:" or "<SNR>" */
18173 *arg += len;
18174 }
18175
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018177 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018179 p = find_name_end(*arg, &expr_start, &expr_end,
18180 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 if (expr_start != NULL)
18182 {
18183 char_u *temp_string;
18184
18185 if (!evaluate)
18186 {
18187 len += (int)(p - *arg);
18188 *arg = skipwhite(p);
18189 return len;
18190 }
18191
18192 /*
18193 * Include any <SID> etc in the expanded string:
18194 * Thus the -len here.
18195 */
18196 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18197 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018198 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 *alias = temp_string;
18200 *arg = skipwhite(p);
18201 return (int)STRLEN(temp_string);
18202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203
18204 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018205 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 EMSG2(_(e_invexpr2), *arg);
18207
18208 return len;
18209}
18210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018211/*
18212 * Find the end of a variable or function name, taking care of magic braces.
18213 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18214 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018215 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018216 * Return a pointer to just after the name. Equal to "arg" if there is no
18217 * valid name.
18218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018220find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 char_u *arg;
18222 char_u **expr_start;
18223 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018224 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018226 int mb_nest = 0;
18227 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 char_u *p;
18229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018230 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018232 *expr_start = NULL;
18233 *expr_end = NULL;
18234 }
18235
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018236 /* Quick check for valid starting character. */
18237 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18238 return arg;
18239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018240 for (p = arg; *p != NUL
18241 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018242 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018243 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018244 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018245 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018246 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018247 if (*p == '\'')
18248 {
18249 /* skip over 'string' to avoid counting [ and ] inside it. */
18250 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18251 ;
18252 if (*p == NUL)
18253 break;
18254 }
18255 else if (*p == '"')
18256 {
18257 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18258 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18259 if (*p == '\\' && p[1] != NUL)
18260 ++p;
18261 if (*p == NUL)
18262 break;
18263 }
18264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018265 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018267 if (*p == '[')
18268 ++br_nest;
18269 else if (*p == ']')
18270 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018273 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018275 if (*p == '{')
18276 {
18277 mb_nest++;
18278 if (expr_start != NULL && *expr_start == NULL)
18279 *expr_start = p;
18280 }
18281 else if (*p == '}')
18282 {
18283 mb_nest--;
18284 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18285 *expr_end = p;
18286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 }
18289
18290 return p;
18291}
18292
18293/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018294 * Expands out the 'magic' {}'s in a variable/function name.
18295 * Note that this can call itself recursively, to deal with
18296 * constructs like foo{bar}{baz}{bam}
18297 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18298 * "in_start" ^
18299 * "expr_start" ^
18300 * "expr_end" ^
18301 * "in_end" ^
18302 *
18303 * Returns a new allocated string, which the caller must free.
18304 * Returns NULL for failure.
18305 */
18306 static char_u *
18307make_expanded_name(in_start, expr_start, expr_end, in_end)
18308 char_u *in_start;
18309 char_u *expr_start;
18310 char_u *expr_end;
18311 char_u *in_end;
18312{
18313 char_u c1;
18314 char_u *retval = NULL;
18315 char_u *temp_result;
18316 char_u *nextcmd = NULL;
18317
18318 if (expr_end == NULL || in_end == NULL)
18319 return NULL;
18320 *expr_start = NUL;
18321 *expr_end = NUL;
18322 c1 = *in_end;
18323 *in_end = NUL;
18324
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018325 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018326 if (temp_result != NULL && nextcmd == NULL)
18327 {
18328 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18329 + (in_end - expr_end) + 1));
18330 if (retval != NULL)
18331 {
18332 STRCPY(retval, in_start);
18333 STRCAT(retval, temp_result);
18334 STRCAT(retval, expr_end + 1);
18335 }
18336 }
18337 vim_free(temp_result);
18338
18339 *in_end = c1; /* put char back for error messages */
18340 *expr_start = '{';
18341 *expr_end = '}';
18342
18343 if (retval != NULL)
18344 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018345 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018346 if (expr_start != NULL)
18347 {
18348 /* Further expansion! */
18349 temp_result = make_expanded_name(retval, expr_start,
18350 expr_end, temp_result);
18351 vim_free(retval);
18352 retval = temp_result;
18353 }
18354 }
18355
18356 return retval;
18357}
18358
18359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018361 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 */
18363 static int
18364eval_isnamec(c)
18365 int c;
18366{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018367 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18368}
18369
18370/*
18371 * Return TRUE if character "c" can be used as the first character in a
18372 * variable or function name (excluding '{' and '}').
18373 */
18374 static int
18375eval_isnamec1(c)
18376 int c;
18377{
18378 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379}
18380
18381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 * Set number v: variable to "val".
18383 */
18384 void
18385set_vim_var_nr(idx, val)
18386 int idx;
18387 long val;
18388{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018389 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390}
18391
18392/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018393 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 */
18395 long
18396get_vim_var_nr(idx)
18397 int idx;
18398{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018399 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400}
18401
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018402/*
18403 * Get string v: variable value. Uses a static buffer, can only be used once.
18404 */
18405 char_u *
18406get_vim_var_str(idx)
18407 int idx;
18408{
18409 return get_tv_string(&vimvars[idx].vv_tv);
18410}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018411
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018413 * Get List v: variable value. Caller must take care of reference count when
18414 * needed.
18415 */
18416 list_T *
18417get_vim_var_list(idx)
18418 int idx;
18419{
18420 return vimvars[idx].vv_list;
18421}
18422
18423/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018424 * Set v:char to character "c".
18425 */
18426 void
18427set_vim_var_char(c)
18428 int c;
18429{
18430#ifdef FEAT_MBYTE
18431 char_u buf[MB_MAXBYTES];
18432#else
18433 char_u buf[2];
18434#endif
18435
18436#ifdef FEAT_MBYTE
18437 if (has_mbyte)
18438 buf[(*mb_char2bytes)(c, buf)] = NUL;
18439 else
18440#endif
18441 {
18442 buf[0] = c;
18443 buf[1] = NUL;
18444 }
18445 set_vim_var_string(VV_CHAR, buf, -1);
18446}
18447
18448/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018449 * Set v:count to "count" and v:count1 to "count1".
18450 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 */
18452 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018453set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454 long count;
18455 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018456 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018458 if (set_prevcount)
18459 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018460 vimvars[VV_COUNT].vv_nr = count;
18461 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462}
18463
18464/*
18465 * Set string v: variable to a copy of "val".
18466 */
18467 void
18468set_vim_var_string(idx, val, len)
18469 int idx;
18470 char_u *val;
18471 int len; /* length of "val" to use or -1 (whole string) */
18472{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018473 /* Need to do this (at least) once, since we can't initialize a union.
18474 * Will always be invoked when "v:progname" is set. */
18475 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18476
Bram Moolenaare9a41262005-01-15 22:18:47 +000018477 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018479 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018481 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018483 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484}
18485
18486/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018487 * Set List v: variable to "val".
18488 */
18489 void
18490set_vim_var_list(idx, val)
18491 int idx;
18492 list_T *val;
18493{
18494 list_unref(vimvars[idx].vv_list);
18495 vimvars[idx].vv_list = val;
18496 if (val != NULL)
18497 ++val->lv_refcount;
18498}
18499
18500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 * Set v:register if needed.
18502 */
18503 void
18504set_reg_var(c)
18505 int c;
18506{
18507 char_u regname;
18508
18509 if (c == 0 || c == ' ')
18510 regname = '"';
18511 else
18512 regname = c;
18513 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018514 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 set_vim_var_string(VV_REG, &regname, 1);
18516}
18517
18518/*
18519 * Get or set v:exception. If "oldval" == NULL, return the current value.
18520 * Otherwise, restore the value to "oldval" and return NULL.
18521 * Must always be called in pairs to save and restore v:exception! Does not
18522 * take care of memory allocations.
18523 */
18524 char_u *
18525v_exception(oldval)
18526 char_u *oldval;
18527{
18528 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018529 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530
Bram Moolenaare9a41262005-01-15 22:18:47 +000018531 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 return NULL;
18533}
18534
18535/*
18536 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18537 * Otherwise, restore the value to "oldval" and return NULL.
18538 * Must always be called in pairs to save and restore v:throwpoint! Does not
18539 * take care of memory allocations.
18540 */
18541 char_u *
18542v_throwpoint(oldval)
18543 char_u *oldval;
18544{
18545 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018546 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547
Bram Moolenaare9a41262005-01-15 22:18:47 +000018548 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 return NULL;
18550}
18551
18552#if defined(FEAT_AUTOCMD) || defined(PROTO)
18553/*
18554 * Set v:cmdarg.
18555 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18556 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18557 * Must always be called in pairs!
18558 */
18559 char_u *
18560set_cmdarg(eap, oldarg)
18561 exarg_T *eap;
18562 char_u *oldarg;
18563{
18564 char_u *oldval;
18565 char_u *newval;
18566 unsigned len;
18567
Bram Moolenaare9a41262005-01-15 22:18:47 +000018568 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018569 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018571 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018572 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018573 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 }
18575
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018576 if (eap->force_bin == FORCE_BIN)
18577 len = 6;
18578 else if (eap->force_bin == FORCE_NOBIN)
18579 len = 8;
18580 else
18581 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018582
18583 if (eap->read_edit)
18584 len += 7;
18585
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018586 if (eap->force_ff != 0)
18587 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18588# ifdef FEAT_MBYTE
18589 if (eap->force_enc != 0)
18590 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018591 if (eap->bad_char != 0)
18592 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018593# endif
18594
18595 newval = alloc(len + 1);
18596 if (newval == NULL)
18597 return NULL;
18598
18599 if (eap->force_bin == FORCE_BIN)
18600 sprintf((char *)newval, " ++bin");
18601 else if (eap->force_bin == FORCE_NOBIN)
18602 sprintf((char *)newval, " ++nobin");
18603 else
18604 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018605
18606 if (eap->read_edit)
18607 STRCAT(newval, " ++edit");
18608
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018609 if (eap->force_ff != 0)
18610 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18611 eap->cmd + eap->force_ff);
18612# ifdef FEAT_MBYTE
18613 if (eap->force_enc != 0)
18614 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18615 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018616 if (eap->bad_char == BAD_KEEP)
18617 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18618 else if (eap->bad_char == BAD_DROP)
18619 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18620 else if (eap->bad_char != 0)
18621 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018622# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018623 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018624 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625}
18626#endif
18627
18628/*
18629 * Get the value of internal variable "name".
18630 * Return OK or FAIL.
18631 */
18632 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018633get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 char_u *name;
18635 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018636 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018637 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638{
18639 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018640 typval_T *tv = NULL;
18641 typval_T atv;
18642 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644
18645 /* truncate the name, so that we can use strcmp() */
18646 cc = name[len];
18647 name[len] = NUL;
18648
18649 /*
18650 * Check for "b:changedtick".
18651 */
18652 if (STRCMP(name, "b:changedtick") == 0)
18653 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018654 atv.v_type = VAR_NUMBER;
18655 atv.vval.v_number = curbuf->b_changedtick;
18656 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 }
18658
18659 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 * Check for user-defined variables.
18661 */
18662 else
18663 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018664 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 }
18668
Bram Moolenaare9a41262005-01-15 22:18:47 +000018669 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018671 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 ret = FAIL;
18674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018676 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677
18678 name[len] = cc;
18679
18680 return ret;
18681}
18682
18683/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018684 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18685 * Also handle function call with Funcref variable: func(expr)
18686 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18687 */
18688 static int
18689handle_subscript(arg, rettv, evaluate, verbose)
18690 char_u **arg;
18691 typval_T *rettv;
18692 int evaluate; /* do more than finding the end */
18693 int verbose; /* give error messages */
18694{
18695 int ret = OK;
18696 dict_T *selfdict = NULL;
18697 char_u *s;
18698 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018699 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018700
18701 while (ret == OK
18702 && (**arg == '['
18703 || (**arg == '.' && rettv->v_type == VAR_DICT)
18704 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18705 && !vim_iswhite(*(*arg - 1)))
18706 {
18707 if (**arg == '(')
18708 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018709 /* need to copy the funcref so that we can clear rettv */
18710 functv = *rettv;
18711 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018712
18713 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018714 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018715 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018716 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18717 &len, evaluate, selfdict);
18718
18719 /* Clear the funcref afterwards, so that deleting it while
18720 * evaluating the arguments is possible (see test55). */
18721 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018722
18723 /* Stop the expression evaluation when immediately aborting on
18724 * error, or when an interrupt occurred or an exception was thrown
18725 * but not caught. */
18726 if (aborting())
18727 {
18728 if (ret == OK)
18729 clear_tv(rettv);
18730 ret = FAIL;
18731 }
18732 dict_unref(selfdict);
18733 selfdict = NULL;
18734 }
18735 else /* **arg == '[' || **arg == '.' */
18736 {
18737 dict_unref(selfdict);
18738 if (rettv->v_type == VAR_DICT)
18739 {
18740 selfdict = rettv->vval.v_dict;
18741 if (selfdict != NULL)
18742 ++selfdict->dv_refcount;
18743 }
18744 else
18745 selfdict = NULL;
18746 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18747 {
18748 clear_tv(rettv);
18749 ret = FAIL;
18750 }
18751 }
18752 }
18753 dict_unref(selfdict);
18754 return ret;
18755}
18756
18757/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018758 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018759 * value).
18760 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018763{
Bram Moolenaar33570922005-01-25 22:26:29 +000018764 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018765}
18766
18767/*
18768 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 * The string "s" must have been allocated, it is consumed.
18770 * Return NULL for out of memory, the variable otherwise.
18771 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018772 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018773alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 char_u *s;
18775{
Bram Moolenaar33570922005-01-25 22:26:29 +000018776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778 rettv = alloc_tv();
18779 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018781 rettv->v_type = VAR_STRING;
18782 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 }
18784 else
18785 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787}
18788
18789/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018790 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018792 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018793free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018794 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795{
18796 if (varp != NULL)
18797 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018798 switch (varp->v_type)
18799 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018800 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018801 func_unref(varp->vval.v_string);
18802 /*FALLTHROUGH*/
18803 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018804 vim_free(varp->vval.v_string);
18805 break;
18806 case VAR_LIST:
18807 list_unref(varp->vval.v_list);
18808 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018809 case VAR_DICT:
18810 dict_unref(varp->vval.v_dict);
18811 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018812 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018813#ifdef FEAT_FLOAT
18814 case VAR_FLOAT:
18815#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018816 case VAR_UNKNOWN:
18817 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018818 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018819 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018820 break;
18821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 vim_free(varp);
18823 }
18824}
18825
18826/*
18827 * Free the memory for a variable value and set the value to NULL or 0.
18828 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018829 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018830clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018831 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832{
18833 if (varp != NULL)
18834 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018835 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018837 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018838 func_unref(varp->vval.v_string);
18839 /*FALLTHROUGH*/
18840 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018841 vim_free(varp->vval.v_string);
18842 varp->vval.v_string = NULL;
18843 break;
18844 case VAR_LIST:
18845 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018846 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018847 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018848 case VAR_DICT:
18849 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018850 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018851 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018852 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018853 varp->vval.v_number = 0;
18854 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018855#ifdef FEAT_FLOAT
18856 case VAR_FLOAT:
18857 varp->vval.v_float = 0.0;
18858 break;
18859#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018860 case VAR_UNKNOWN:
18861 break;
18862 default:
18863 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018865 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 }
18867}
18868
18869/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018870 * Set the value of a variable to NULL without freeing items.
18871 */
18872 static void
18873init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018874 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875{
18876 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018877 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018878}
18879
18880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 * Get the number value of a variable.
18882 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018883 * For incompatible types, return 0.
18884 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18885 * caller of incompatible types: it sets *denote to TRUE if "denote"
18886 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 */
18888 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018889get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018890 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018892 int error = FALSE;
18893
18894 return get_tv_number_chk(varp, &error); /* return 0L on error */
18895}
18896
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018897 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018898get_tv_number_chk(varp, denote)
18899 typval_T *varp;
18900 int *denote;
18901{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018902 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018904 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018906 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018907 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018908#ifdef FEAT_FLOAT
18909 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018910 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018911 break;
18912#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018913 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018914 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018915 break;
18916 case VAR_STRING:
18917 if (varp->vval.v_string != NULL)
18918 vim_str2nr(varp->vval.v_string, NULL, NULL,
18919 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018920 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018921 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018922 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018923 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018924 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018925 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018926 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018927 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018928 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018931 if (denote == NULL) /* useful for values that must be unsigned */
18932 n = -1;
18933 else
18934 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018935 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936}
18937
18938/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018939 * Get the lnum from the first argument.
18940 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018941 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942 */
18943 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018944get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018945 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946{
Bram Moolenaar33570922005-01-25 22:26:29 +000018947 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 linenr_T lnum;
18949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018950 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 if (lnum == 0) /* no valid number, try using line() */
18952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018953 rettv.v_type = VAR_NUMBER;
18954 f_line(argvars, &rettv);
18955 lnum = rettv.vval.v_number;
18956 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 }
18958 return lnum;
18959}
18960
18961/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018962 * Get the lnum from the first argument.
18963 * Also accepts "$", then "buf" is used.
18964 * Returns 0 on error.
18965 */
18966 static linenr_T
18967get_tv_lnum_buf(argvars, buf)
18968 typval_T *argvars;
18969 buf_T *buf;
18970{
18971 if (argvars[0].v_type == VAR_STRING
18972 && argvars[0].vval.v_string != NULL
18973 && argvars[0].vval.v_string[0] == '$'
18974 && buf != NULL)
18975 return buf->b_ml.ml_line_count;
18976 return get_tv_number_chk(&argvars[0], NULL);
18977}
18978
18979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 * Get the string value of a variable.
18981 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018982 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18983 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 * If the String variable has never been set, return an empty string.
18985 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018986 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18987 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 */
18989 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018990get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018991 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992{
18993 static char_u mybuf[NUMBUFLEN];
18994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018995 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996}
18997
18998 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018999get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019000 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019001 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019003 char_u *res = get_tv_string_buf_chk(varp, buf);
19004
19005 return res != NULL ? res : (char_u *)"";
19006}
19007
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019008 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019009get_tv_string_chk(varp)
19010 typval_T *varp;
19011{
19012 static char_u mybuf[NUMBUFLEN];
19013
19014 return get_tv_string_buf_chk(varp, mybuf);
19015}
19016
19017 static char_u *
19018get_tv_string_buf_chk(varp, buf)
19019 typval_T *varp;
19020 char_u *buf;
19021{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019022 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019024 case VAR_NUMBER:
19025 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19026 return buf;
19027 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019028 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019029 break;
19030 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019031 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019032 break;
19033 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019034 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019035 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019036#ifdef FEAT_FLOAT
19037 case VAR_FLOAT:
19038 EMSG(_("E806: using Float as a String"));
19039 break;
19040#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019041 case VAR_STRING:
19042 if (varp->vval.v_string != NULL)
19043 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019044 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019045 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019046 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019049 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050}
19051
19052/*
19053 * Find variable "name" in the list of variables.
19054 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019055 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019056 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019057 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019060find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019062 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019065 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066
Bram Moolenaara7043832005-01-21 11:56:39 +000019067 ht = find_var_ht(name, &varname);
19068 if (htp != NULL)
19069 *htp = ht;
19070 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019072 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073}
19074
19075/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019076 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019077 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019079 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019080find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019081 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019082 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019083 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019084{
Bram Moolenaar33570922005-01-25 22:26:29 +000019085 hashitem_T *hi;
19086
19087 if (*varname == NUL)
19088 {
19089 /* Must be something like "s:", otherwise "ht" would be NULL. */
19090 switch (varname[-2])
19091 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019092 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019093 case 'g': return &globvars_var;
19094 case 'v': return &vimvars_var;
19095 case 'b': return &curbuf->b_bufvar;
19096 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019097#ifdef FEAT_WINDOWS
19098 case 't': return &curtab->tp_winvar;
19099#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019100 case 'l': return current_funccal == NULL
19101 ? NULL : &current_funccal->l_vars_var;
19102 case 'a': return current_funccal == NULL
19103 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019104 }
19105 return NULL;
19106 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019107
19108 hi = hash_find(ht, varname);
19109 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019110 {
19111 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019112 * worked find the variable again. Don't auto-load a script if it was
19113 * loaded already, otherwise it would be loaded every time when
19114 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019115 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019116 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019117 hi = hash_find(ht, varname);
19118 if (HASHITEM_EMPTY(hi))
19119 return NULL;
19120 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019121 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019122}
19123
19124/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019125 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019126 * Set "varname" to the start of name without ':'.
19127 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019128 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019129find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 char_u *name;
19131 char_u **varname;
19132{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019133 hashitem_T *hi;
19134
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 if (name[1] != ':')
19136 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019137 /* The name must not start with a colon or #. */
19138 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 return NULL;
19140 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019141
19142 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019143 hi = hash_find(&compat_hashtab, name);
19144 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019145 return &compat_hashtab;
19146
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019148 return &globvarht; /* global variable */
19149 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 }
19151 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019152 if (*name == 'g') /* global variable */
19153 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019154 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19155 */
19156 if (vim_strchr(name + 2, ':') != NULL
19157 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019158 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019160 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019162 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019163#ifdef FEAT_WINDOWS
19164 if (*name == 't') /* tab page variable */
19165 return &curtab->tp_vars.dv_hashtab;
19166#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019167 if (*name == 'v') /* v: variable */
19168 return &vimvarht;
19169 if (*name == 'a' && current_funccal != NULL) /* function argument */
19170 return &current_funccal->l_avars.dv_hashtab;
19171 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19172 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 if (*name == 's' /* script variable */
19174 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19175 return &SCRIPT_VARS(current_SID);
19176 return NULL;
19177}
19178
19179/*
19180 * Get the string value of a (global/local) variable.
19181 * Returns NULL when it doesn't exist.
19182 */
19183 char_u *
19184get_var_value(name)
19185 char_u *name;
19186{
Bram Moolenaar33570922005-01-25 22:26:29 +000019187 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188
Bram Moolenaara7043832005-01-21 11:56:39 +000019189 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 if (v == NULL)
19191 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019192 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193}
19194
19195/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019196 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 * sourcing this script and when executing functions defined in the script.
19198 */
19199 void
19200new_script_vars(id)
19201 scid_T id;
19202{
Bram Moolenaara7043832005-01-21 11:56:39 +000019203 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019204 hashtab_T *ht;
19205 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019206
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19208 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019209 /* Re-allocating ga_data means that an ht_array pointing to
19210 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019211 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019212 for (i = 1; i <= ga_scripts.ga_len; ++i)
19213 {
19214 ht = &SCRIPT_VARS(i);
19215 if (ht->ht_mask == HT_INIT_SIZE - 1)
19216 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019217 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019218 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019219 }
19220
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 while (ga_scripts.ga_len < id)
19222 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019223 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
19224 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019225 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 }
19228 }
19229}
19230
19231/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019232 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19233 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234 */
19235 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019236init_var_dict(dict, dict_var)
19237 dict_T *dict;
19238 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239{
Bram Moolenaar33570922005-01-25 22:26:29 +000019240 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019241 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019242 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019243 dict_var->di_tv.vval.v_dict = dict;
19244 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019245 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019246 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19247 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248}
19249
19250/*
19251 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 * Frees all allocated variables and the value they contain.
19253 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 */
19255 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019256vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019257 hashtab_T *ht;
19258{
19259 vars_clear_ext(ht, TRUE);
19260}
19261
19262/*
19263 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19264 */
19265 static void
19266vars_clear_ext(ht, free_val)
19267 hashtab_T *ht;
19268 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269{
Bram Moolenaara7043832005-01-21 11:56:39 +000019270 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 hashitem_T *hi;
19272 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273
Bram Moolenaar33570922005-01-25 22:26:29 +000019274 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019275 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019276 for (hi = ht->ht_array; todo > 0; ++hi)
19277 {
19278 if (!HASHITEM_EMPTY(hi))
19279 {
19280 --todo;
19281
Bram Moolenaar33570922005-01-25 22:26:29 +000019282 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019283 * ht_array might change then. hash_clear() takes care of it
19284 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019285 v = HI2DI(hi);
19286 if (free_val)
19287 clear_tv(&v->di_tv);
19288 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19289 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019290 }
19291 }
19292 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019293 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294}
19295
Bram Moolenaara7043832005-01-21 11:56:39 +000019296/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019297 * Delete a variable from hashtab "ht" at item "hi".
19298 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019301delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019302 hashtab_T *ht;
19303 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304{
Bram Moolenaar33570922005-01-25 22:26:29 +000019305 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019306
19307 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 clear_tv(&di->di_tv);
19309 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310}
19311
19312/*
19313 * List the value of one internal variable.
19314 */
19315 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019316list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019317 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019319 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019321 char_u *tofree;
19322 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019323 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019324
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019325 current_copyID += COPYID_INC;
19326 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019327 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019328 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019329 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330}
19331
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019333list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334 char_u *prefix;
19335 char_u *name;
19336 int type;
19337 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019338 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339{
Bram Moolenaar31859182007-08-14 20:41:13 +000019340 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19341 msg_start();
19342 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 if (name != NULL) /* "a:" vars don't have a name stored */
19344 msg_puts(name);
19345 msg_putchar(' ');
19346 msg_advance(22);
19347 if (type == VAR_NUMBER)
19348 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019349 else if (type == VAR_FUNC)
19350 msg_putchar('*');
19351 else if (type == VAR_LIST)
19352 {
19353 msg_putchar('[');
19354 if (*string == '[')
19355 ++string;
19356 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019357 else if (type == VAR_DICT)
19358 {
19359 msg_putchar('{');
19360 if (*string == '{')
19361 ++string;
19362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 else
19364 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019365
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019367
19368 if (type == VAR_FUNC)
19369 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019370 if (*first)
19371 {
19372 msg_clr_eos();
19373 *first = FALSE;
19374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375}
19376
19377/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019378 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 * If the variable already exists, the value is updated.
19380 * Otherwise the variable is created.
19381 */
19382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019383set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019385 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019386 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387{
Bram Moolenaar33570922005-01-25 22:26:29 +000019388 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019390 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019391 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019393 ht = find_var_ht(name, &varname);
19394 if (ht == NULL || *varname == NUL)
19395 {
19396 EMSG2(_(e_illvar), name);
19397 return;
19398 }
19399 v = find_var_in_ht(ht, varname, TRUE);
19400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019401 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019402 {
19403 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19404 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19405 ? name[2] : name[0]))
19406 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019407 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019408 return;
19409 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019410 /* Don't allow hiding a function. When "v" is not NULL we migth be
19411 * assigning another function to the same var, the type is checked
19412 * below. */
19413 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019414 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019415 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019416 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019417 return;
19418 }
19419 }
19420
Bram Moolenaar33570922005-01-25 22:26:29 +000019421 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019423 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019424 if (var_check_ro(v->di_flags, name)
19425 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019426 return;
19427 if (v->di_tv.v_type != tv->v_type
19428 && !((v->di_tv.v_type == VAR_STRING
19429 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019430 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019431 || tv->v_type == VAR_NUMBER))
19432#ifdef FEAT_FLOAT
19433 && !((v->di_tv.v_type == VAR_NUMBER
19434 || v->di_tv.v_type == VAR_FLOAT)
19435 && (tv->v_type == VAR_NUMBER
19436 || tv->v_type == VAR_FLOAT))
19437#endif
19438 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019439 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019440 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019441 return;
19442 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019443
19444 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019445 * Handle setting internal v: variables separately: we don't change
19446 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019447 */
19448 if (ht == &vimvarht)
19449 {
19450 if (v->di_tv.v_type == VAR_STRING)
19451 {
19452 vim_free(v->di_tv.vval.v_string);
19453 if (copy || tv->v_type != VAR_STRING)
19454 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19455 else
19456 {
19457 /* Take over the string to avoid an extra alloc/free. */
19458 v->di_tv.vval.v_string = tv->vval.v_string;
19459 tv->vval.v_string = NULL;
19460 }
19461 }
19462 else if (v->di_tv.v_type != VAR_NUMBER)
19463 EMSG2(_(e_intern2), "set_var()");
19464 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019465 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019466 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019467 if (STRCMP(varname, "searchforward") == 0)
19468 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19469 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019470 return;
19471 }
19472
19473 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 }
19475 else /* add a new variable */
19476 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019477 /* Can't add "v:" variable. */
19478 if (ht == &vimvarht)
19479 {
19480 EMSG2(_(e_illvar), name);
19481 return;
19482 }
19483
Bram Moolenaar92124a32005-06-17 22:03:40 +000019484 /* Make sure the variable name is valid. */
19485 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019486 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19487 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019488 {
19489 EMSG2(_(e_illvar), varname);
19490 return;
19491 }
19492
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019493 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19494 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019495 if (v == NULL)
19496 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019497 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019498 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019500 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 return;
19502 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019503 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019505
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019506 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019507 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019508 else
19509 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019510 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019511 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019512 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514}
19515
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019516/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019517 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019518 * Also give an error message.
19519 */
19520 static int
19521var_check_ro(flags, name)
19522 int flags;
19523 char_u *name;
19524{
19525 if (flags & DI_FLAGS_RO)
19526 {
19527 EMSG2(_(e_readonlyvar), name);
19528 return TRUE;
19529 }
19530 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19531 {
19532 EMSG2(_(e_readonlysbx), name);
19533 return TRUE;
19534 }
19535 return FALSE;
19536}
19537
19538/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019539 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19540 * Also give an error message.
19541 */
19542 static int
19543var_check_fixed(flags, name)
19544 int flags;
19545 char_u *name;
19546{
19547 if (flags & DI_FLAGS_FIX)
19548 {
19549 EMSG2(_("E795: Cannot delete variable %s"), name);
19550 return TRUE;
19551 }
19552 return FALSE;
19553}
19554
19555/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019556 * Return TRUE if typeval "tv" is set to be locked (immutable).
19557 * Also give an error message, using "name".
19558 */
19559 static int
19560tv_check_lock(lock, name)
19561 int lock;
19562 char_u *name;
19563{
19564 if (lock & VAR_LOCKED)
19565 {
19566 EMSG2(_("E741: Value is locked: %s"),
19567 name == NULL ? (char_u *)_("Unknown") : name);
19568 return TRUE;
19569 }
19570 if (lock & VAR_FIXED)
19571 {
19572 EMSG2(_("E742: Cannot change value of %s"),
19573 name == NULL ? (char_u *)_("Unknown") : name);
19574 return TRUE;
19575 }
19576 return FALSE;
19577}
19578
19579/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019580 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019581 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019582 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019583 * It is OK for "from" and "to" to point to the same item. This is used to
19584 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019585 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019586 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019587copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019588 typval_T *from;
19589 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019591 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019592 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019593 switch (from->v_type)
19594 {
19595 case VAR_NUMBER:
19596 to->vval.v_number = from->vval.v_number;
19597 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019598#ifdef FEAT_FLOAT
19599 case VAR_FLOAT:
19600 to->vval.v_float = from->vval.v_float;
19601 break;
19602#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019603 case VAR_STRING:
19604 case VAR_FUNC:
19605 if (from->vval.v_string == NULL)
19606 to->vval.v_string = NULL;
19607 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019609 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019610 if (from->v_type == VAR_FUNC)
19611 func_ref(to->vval.v_string);
19612 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019613 break;
19614 case VAR_LIST:
19615 if (from->vval.v_list == NULL)
19616 to->vval.v_list = NULL;
19617 else
19618 {
19619 to->vval.v_list = from->vval.v_list;
19620 ++to->vval.v_list->lv_refcount;
19621 }
19622 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019623 case VAR_DICT:
19624 if (from->vval.v_dict == NULL)
19625 to->vval.v_dict = NULL;
19626 else
19627 {
19628 to->vval.v_dict = from->vval.v_dict;
19629 ++to->vval.v_dict->dv_refcount;
19630 }
19631 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019632 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019633 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019634 break;
19635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636}
19637
19638/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019639 * Make a copy of an item.
19640 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019641 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19642 * reference to an already copied list/dict can be used.
19643 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019644 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019645 static int
19646item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 typval_T *from;
19648 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019649 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019650 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019651{
19652 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019653 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019654
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019656 {
19657 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019658 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019659 }
19660 ++recurse;
19661
19662 switch (from->v_type)
19663 {
19664 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019665#ifdef FEAT_FLOAT
19666 case VAR_FLOAT:
19667#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019668 case VAR_STRING:
19669 case VAR_FUNC:
19670 copy_tv(from, to);
19671 break;
19672 case VAR_LIST:
19673 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019674 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019675 if (from->vval.v_list == NULL)
19676 to->vval.v_list = NULL;
19677 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19678 {
19679 /* use the copy made earlier */
19680 to->vval.v_list = from->vval.v_list->lv_copylist;
19681 ++to->vval.v_list->lv_refcount;
19682 }
19683 else
19684 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19685 if (to->vval.v_list == NULL)
19686 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019687 break;
19688 case VAR_DICT:
19689 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019690 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019691 if (from->vval.v_dict == NULL)
19692 to->vval.v_dict = NULL;
19693 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19694 {
19695 /* use the copy made earlier */
19696 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19697 ++to->vval.v_dict->dv_refcount;
19698 }
19699 else
19700 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19701 if (to->vval.v_dict == NULL)
19702 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019703 break;
19704 default:
19705 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019706 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019707 }
19708 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019709 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019710}
19711
19712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 * ":echo expr1 ..." print each argument separated with a space, add a
19714 * newline at the end.
19715 * ":echon expr1 ..." print each argument plain.
19716 */
19717 void
19718ex_echo(eap)
19719 exarg_T *eap;
19720{
19721 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019722 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019723 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 char_u *p;
19725 int needclr = TRUE;
19726 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019727 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728
19729 if (eap->skip)
19730 ++emsg_skip;
19731 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19732 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019733 /* If eval1() causes an error message the text from the command may
19734 * still need to be cleared. E.g., "echo 22,44". */
19735 need_clr_eos = needclr;
19736
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019738 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 {
19740 /*
19741 * Report the invalid expression unless the expression evaluation
19742 * has been cancelled due to an aborting error, an interrupt, or an
19743 * exception.
19744 */
19745 if (!aborting())
19746 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019747 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 break;
19749 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019750 need_clr_eos = FALSE;
19751
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 if (!eap->skip)
19753 {
19754 if (atstart)
19755 {
19756 atstart = FALSE;
19757 /* Call msg_start() after eval1(), evaluating the expression
19758 * may cause a message to appear. */
19759 if (eap->cmdidx == CMD_echo)
19760 msg_start();
19761 }
19762 else if (eap->cmdidx == CMD_echo)
19763 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019764 current_copyID += COPYID_INC;
19765 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019766 if (p != NULL)
19767 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019769 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019771 if (*p != TAB && needclr)
19772 {
19773 /* remove any text still there from the command */
19774 msg_clr_eos();
19775 needclr = FALSE;
19776 }
19777 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 }
19779 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019780 {
19781#ifdef FEAT_MBYTE
19782 if (has_mbyte)
19783 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019784 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019785
19786 (void)msg_outtrans_len_attr(p, i, echo_attr);
19787 p += i - 1;
19788 }
19789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019791 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019794 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019796 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 arg = skipwhite(arg);
19798 }
19799 eap->nextcmd = check_nextcmd(arg);
19800
19801 if (eap->skip)
19802 --emsg_skip;
19803 else
19804 {
19805 /* remove text that may still be there from the command */
19806 if (needclr)
19807 msg_clr_eos();
19808 if (eap->cmdidx == CMD_echo)
19809 msg_end();
19810 }
19811}
19812
19813/*
19814 * ":echohl {name}".
19815 */
19816 void
19817ex_echohl(eap)
19818 exarg_T *eap;
19819{
19820 int id;
19821
19822 id = syn_name2id(eap->arg);
19823 if (id == 0)
19824 echo_attr = 0;
19825 else
19826 echo_attr = syn_id2attr(id);
19827}
19828
19829/*
19830 * ":execute expr1 ..." execute the result of an expression.
19831 * ":echomsg expr1 ..." Print a message
19832 * ":echoerr expr1 ..." Print an error
19833 * Each gets spaces around each argument and a newline at the end for
19834 * echo commands
19835 */
19836 void
19837ex_execute(eap)
19838 exarg_T *eap;
19839{
19840 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019841 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 int ret = OK;
19843 char_u *p;
19844 garray_T ga;
19845 int len;
19846 int save_did_emsg;
19847
19848 ga_init2(&ga, 1, 80);
19849
19850 if (eap->skip)
19851 ++emsg_skip;
19852 while (*arg != NUL && *arg != '|' && *arg != '\n')
19853 {
19854 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019855 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 {
19857 /*
19858 * Report the invalid expression unless the expression evaluation
19859 * has been cancelled due to an aborting error, an interrupt, or an
19860 * exception.
19861 */
19862 if (!aborting())
19863 EMSG2(_(e_invexpr2), p);
19864 ret = FAIL;
19865 break;
19866 }
19867
19868 if (!eap->skip)
19869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019870 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 len = (int)STRLEN(p);
19872 if (ga_grow(&ga, len + 2) == FAIL)
19873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019874 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 ret = FAIL;
19876 break;
19877 }
19878 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 ga.ga_len += len;
19882 }
19883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019884 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 arg = skipwhite(arg);
19886 }
19887
19888 if (ret != FAIL && ga.ga_data != NULL)
19889 {
19890 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019893 out_flush();
19894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 else if (eap->cmdidx == CMD_echoerr)
19896 {
19897 /* We don't want to abort following commands, restore did_emsg. */
19898 save_did_emsg = did_emsg;
19899 EMSG((char_u *)ga.ga_data);
19900 if (!force_abort)
19901 did_emsg = save_did_emsg;
19902 }
19903 else if (eap->cmdidx == CMD_execute)
19904 do_cmdline((char_u *)ga.ga_data,
19905 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19906 }
19907
19908 ga_clear(&ga);
19909
19910 if (eap->skip)
19911 --emsg_skip;
19912
19913 eap->nextcmd = check_nextcmd(arg);
19914}
19915
19916/*
19917 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19918 * "arg" points to the "&" or '+' when called, to "option" when returning.
19919 * Returns NULL when no option name found. Otherwise pointer to the char
19920 * after the option name.
19921 */
19922 static char_u *
19923find_option_end(arg, opt_flags)
19924 char_u **arg;
19925 int *opt_flags;
19926{
19927 char_u *p = *arg;
19928
19929 ++p;
19930 if (*p == 'g' && p[1] == ':')
19931 {
19932 *opt_flags = OPT_GLOBAL;
19933 p += 2;
19934 }
19935 else if (*p == 'l' && p[1] == ':')
19936 {
19937 *opt_flags = OPT_LOCAL;
19938 p += 2;
19939 }
19940 else
19941 *opt_flags = 0;
19942
19943 if (!ASCII_ISALPHA(*p))
19944 return NULL;
19945 *arg = p;
19946
19947 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19948 p += 4; /* termcap option */
19949 else
19950 while (ASCII_ISALPHA(*p))
19951 ++p;
19952 return p;
19953}
19954
19955/*
19956 * ":function"
19957 */
19958 void
19959ex_function(eap)
19960 exarg_T *eap;
19961{
19962 char_u *theline;
19963 int j;
19964 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 char_u *name = NULL;
19967 char_u *p;
19968 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019969 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 garray_T newargs;
19971 garray_T newlines;
19972 int varargs = FALSE;
19973 int mustend = FALSE;
19974 int flags = 0;
19975 ufunc_T *fp;
19976 int indent;
19977 int nesting;
19978 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019979 dictitem_T *v;
19980 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019981 static int func_nr = 0; /* number for nameless function */
19982 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019983 hashtab_T *ht;
19984 int todo;
19985 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019986 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987
19988 /*
19989 * ":function" without argument: list functions.
19990 */
19991 if (ends_excmd(*eap->arg))
19992 {
19993 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019994 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019995 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019996 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019997 {
19998 if (!HASHITEM_EMPTY(hi))
19999 {
20000 --todo;
20001 fp = HI2UF(hi);
20002 if (!isdigit(*fp->uf_name))
20003 list_func_head(fp, FALSE);
20004 }
20005 }
20006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 eap->nextcmd = check_nextcmd(eap->arg);
20008 return;
20009 }
20010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020011 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020012 * ":function /pat": list functions matching pattern.
20013 */
20014 if (*eap->arg == '/')
20015 {
20016 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20017 if (!eap->skip)
20018 {
20019 regmatch_T regmatch;
20020
20021 c = *p;
20022 *p = NUL;
20023 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20024 *p = c;
20025 if (regmatch.regprog != NULL)
20026 {
20027 regmatch.rm_ic = p_ic;
20028
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020029 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020030 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20031 {
20032 if (!HASHITEM_EMPTY(hi))
20033 {
20034 --todo;
20035 fp = HI2UF(hi);
20036 if (!isdigit(*fp->uf_name)
20037 && vim_regexec(&regmatch, fp->uf_name, 0))
20038 list_func_head(fp, FALSE);
20039 }
20040 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020041 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020042 }
20043 }
20044 if (*p == '/')
20045 ++p;
20046 eap->nextcmd = check_nextcmd(p);
20047 return;
20048 }
20049
20050 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020051 * Get the function name. There are these situations:
20052 * func normal function name
20053 * "name" == func, "fudi.fd_dict" == NULL
20054 * dict.func new dictionary entry
20055 * "name" == NULL, "fudi.fd_dict" set,
20056 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20057 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020058 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020059 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20060 * dict.func existing dict entry that's not a Funcref
20061 * "name" == NULL, "fudi.fd_dict" set,
20062 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020065 name = trans_function_name(&p, eap->skip, 0, &fudi);
20066 paren = (vim_strchr(p, '(') != NULL);
20067 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 {
20069 /*
20070 * Return on an invalid expression in braces, unless the expression
20071 * evaluation has been cancelled due to an aborting error, an
20072 * interrupt, or an exception.
20073 */
20074 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020075 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020076 if (!eap->skip && fudi.fd_newkey != NULL)
20077 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020078 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 else
20082 eap->skip = TRUE;
20083 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020084
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 /* An error in a function call during evaluation of an expression in magic
20086 * braces should not cause the function not to be defined. */
20087 saved_did_emsg = did_emsg;
20088 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089
20090 /*
20091 * ":function func" with only function name: list function.
20092 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020093 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 {
20095 if (!ends_excmd(*skipwhite(p)))
20096 {
20097 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020098 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 }
20100 eap->nextcmd = check_nextcmd(p);
20101 if (eap->nextcmd != NULL)
20102 *p = NUL;
20103 if (!eap->skip && !got_int)
20104 {
20105 fp = find_func(name);
20106 if (fp != NULL)
20107 {
20108 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020109 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020111 if (FUNCLINE(fp, j) == NULL)
20112 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 msg_putchar('\n');
20114 msg_outnum((long)(j + 1));
20115 if (j < 9)
20116 msg_putchar(' ');
20117 if (j < 99)
20118 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020119 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 out_flush(); /* show a line at a time */
20121 ui_breakcheck();
20122 }
20123 if (!got_int)
20124 {
20125 msg_putchar('\n');
20126 msg_puts((char_u *)" endfunction");
20127 }
20128 }
20129 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020130 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020132 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 }
20134
20135 /*
20136 * ":function name(arg1, arg2)" Define function.
20137 */
20138 p = skipwhite(p);
20139 if (*p != '(')
20140 {
20141 if (!eap->skip)
20142 {
20143 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020144 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 }
20146 /* attempt to continue by skipping some text */
20147 if (vim_strchr(p, '(') != NULL)
20148 p = vim_strchr(p, '(');
20149 }
20150 p = skipwhite(p + 1);
20151
20152 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20153 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20154
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020155 if (!eap->skip)
20156 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020157 /* Check the name of the function. Unless it's a dictionary function
20158 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020159 if (name != NULL)
20160 arg = name;
20161 else
20162 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020163 if (arg != NULL && (fudi.fd_di == NULL
20164 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020165 {
20166 if (*arg == K_SPECIAL)
20167 j = 3;
20168 else
20169 j = 0;
20170 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20171 : eval_isnamec(arg[j])))
20172 ++j;
20173 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020174 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020175 }
20176 }
20177
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 /*
20179 * Isolate the arguments: "arg1, arg2, ...)"
20180 */
20181 while (*p != ')')
20182 {
20183 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20184 {
20185 varargs = TRUE;
20186 p += 3;
20187 mustend = TRUE;
20188 }
20189 else
20190 {
20191 arg = p;
20192 while (ASCII_ISALNUM(*p) || *p == '_')
20193 ++p;
20194 if (arg == p || isdigit(*arg)
20195 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20196 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20197 {
20198 if (!eap->skip)
20199 EMSG2(_("E125: Illegal argument: %s"), arg);
20200 break;
20201 }
20202 if (ga_grow(&newargs, 1) == FAIL)
20203 goto erret;
20204 c = *p;
20205 *p = NUL;
20206 arg = vim_strsave(arg);
20207 if (arg == NULL)
20208 goto erret;
20209 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20210 *p = c;
20211 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 if (*p == ',')
20213 ++p;
20214 else
20215 mustend = TRUE;
20216 }
20217 p = skipwhite(p);
20218 if (mustend && *p != ')')
20219 {
20220 if (!eap->skip)
20221 EMSG2(_(e_invarg2), eap->arg);
20222 break;
20223 }
20224 }
20225 ++p; /* skip the ')' */
20226
Bram Moolenaare9a41262005-01-15 22:18:47 +000020227 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 for (;;)
20229 {
20230 p = skipwhite(p);
20231 if (STRNCMP(p, "range", 5) == 0)
20232 {
20233 flags |= FC_RANGE;
20234 p += 5;
20235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020236 else if (STRNCMP(p, "dict", 4) == 0)
20237 {
20238 flags |= FC_DICT;
20239 p += 4;
20240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 else if (STRNCMP(p, "abort", 5) == 0)
20242 {
20243 flags |= FC_ABORT;
20244 p += 5;
20245 }
20246 else
20247 break;
20248 }
20249
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020250 /* When there is a line break use what follows for the function body.
20251 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20252 if (*p == '\n')
20253 line_arg = p + 1;
20254 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 EMSG(_(e_trailing));
20256
20257 /*
20258 * Read the body of the function, until ":endfunction" is found.
20259 */
20260 if (KeyTyped)
20261 {
20262 /* Check if the function already exists, don't let the user type the
20263 * whole function before telling him it doesn't work! For a script we
20264 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020265 if (!eap->skip && !eap->forceit)
20266 {
20267 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20268 EMSG(_(e_funcdict));
20269 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020270 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020273 if (!eap->skip && did_emsg)
20274 goto erret;
20275
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 msg_putchar('\n'); /* don't overwrite the function name */
20277 cmdline_row = msg_row;
20278 }
20279
20280 indent = 2;
20281 nesting = 0;
20282 for (;;)
20283 {
20284 msg_scroll = TRUE;
20285 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020286 sourcing_lnum_off = sourcing_lnum;
20287
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020288 if (line_arg != NULL)
20289 {
20290 /* Use eap->arg, split up in parts by line breaks. */
20291 theline = line_arg;
20292 p = vim_strchr(theline, '\n');
20293 if (p == NULL)
20294 line_arg += STRLEN(line_arg);
20295 else
20296 {
20297 *p = NUL;
20298 line_arg = p + 1;
20299 }
20300 }
20301 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 theline = getcmdline(':', 0L, indent);
20303 else
20304 theline = eap->getline(':', eap->cookie, indent);
20305 if (KeyTyped)
20306 lines_left = Rows - 1;
20307 if (theline == NULL)
20308 {
20309 EMSG(_("E126: Missing :endfunction"));
20310 goto erret;
20311 }
20312
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020313 /* Detect line continuation: sourcing_lnum increased more than one. */
20314 if (sourcing_lnum > sourcing_lnum_off + 1)
20315 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20316 else
20317 sourcing_lnum_off = 0;
20318
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 if (skip_until != NULL)
20320 {
20321 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20322 * don't check for ":endfunc". */
20323 if (STRCMP(theline, skip_until) == 0)
20324 {
20325 vim_free(skip_until);
20326 skip_until = NULL;
20327 }
20328 }
20329 else
20330 {
20331 /* skip ':' and blanks*/
20332 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20333 ;
20334
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020335 /* Check for "endfunction". */
20336 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020338 if (line_arg == NULL)
20339 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 break;
20341 }
20342
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020343 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 * at "end". */
20345 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20346 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020347 else if (STRNCMP(p, "if", 2) == 0
20348 || STRNCMP(p, "wh", 2) == 0
20349 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 || STRNCMP(p, "try", 3) == 0)
20351 indent += 2;
20352
20353 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020354 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020356 if (*p == '!')
20357 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 p += eval_fname_script(p);
20359 if (ASCII_ISALPHA(*p))
20360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020361 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 if (*skipwhite(p) == '(')
20363 {
20364 ++nesting;
20365 indent += 2;
20366 }
20367 }
20368 }
20369
20370 /* Check for ":append" or ":insert". */
20371 p = skip_range(p, NULL);
20372 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20373 || (p[0] == 'i'
20374 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20375 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20376 skip_until = vim_strsave((char_u *)".");
20377
20378 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20379 arg = skipwhite(skiptowhite(p));
20380 if (arg[0] == '<' && arg[1] =='<'
20381 && ((p[0] == 'p' && p[1] == 'y'
20382 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20383 || (p[0] == 'p' && p[1] == 'e'
20384 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20385 || (p[0] == 't' && p[1] == 'c'
20386 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20387 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20388 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020389 || (p[0] == 'm' && p[1] == 'z'
20390 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 ))
20392 {
20393 /* ":python <<" continues until a dot, like ":append" */
20394 p = skipwhite(arg + 2);
20395 if (*p == NUL)
20396 skip_until = vim_strsave((char_u *)".");
20397 else
20398 skip_until = vim_strsave(p);
20399 }
20400 }
20401
20402 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020403 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020404 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020405 if (line_arg == NULL)
20406 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020408 }
20409
20410 /* Copy the line to newly allocated memory. get_one_sourceline()
20411 * allocates 250 bytes per line, this saves 80% on average. The cost
20412 * is an extra alloc/free. */
20413 p = vim_strsave(theline);
20414 if (p != NULL)
20415 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020416 if (line_arg == NULL)
20417 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020418 theline = p;
20419 }
20420
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020421 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20422
20423 /* Add NULL lines for continuation lines, so that the line count is
20424 * equal to the index in the growarray. */
20425 while (sourcing_lnum_off-- > 0)
20426 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020427
20428 /* Check for end of eap->arg. */
20429 if (line_arg != NULL && *line_arg == NUL)
20430 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 }
20432
20433 /* Don't define the function when skipping commands or when an error was
20434 * detected. */
20435 if (eap->skip || did_emsg)
20436 goto erret;
20437
20438 /*
20439 * If there are no errors, add the function
20440 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020441 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020442 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020443 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020444 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020445 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020446 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020447 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020448 goto erret;
20449 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020450
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020451 fp = find_func(name);
20452 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020454 if (!eap->forceit)
20455 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020456 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020457 goto erret;
20458 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020459 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020460 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020461 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020462 name);
20463 goto erret;
20464 }
20465 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020466 ga_clear_strings(&(fp->uf_args));
20467 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020468 vim_free(name);
20469 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 }
20472 else
20473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020474 char numbuf[20];
20475
20476 fp = NULL;
20477 if (fudi.fd_newkey == NULL && !eap->forceit)
20478 {
20479 EMSG(_(e_funcdict));
20480 goto erret;
20481 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020482 if (fudi.fd_di == NULL)
20483 {
20484 /* Can't add a function to a locked dictionary */
20485 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20486 goto erret;
20487 }
20488 /* Can't change an existing function if it is locked */
20489 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20490 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020491
20492 /* Give the function a sequential number. Can only be used with a
20493 * Funcref! */
20494 vim_free(name);
20495 sprintf(numbuf, "%d", ++func_nr);
20496 name = vim_strsave((char_u *)numbuf);
20497 if (name == NULL)
20498 goto erret;
20499 }
20500
20501 if (fp == NULL)
20502 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020503 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020504 {
20505 int slen, plen;
20506 char_u *scriptname;
20507
20508 /* Check that the autoload name matches the script name. */
20509 j = FAIL;
20510 if (sourcing_name != NULL)
20511 {
20512 scriptname = autoload_name(name);
20513 if (scriptname != NULL)
20514 {
20515 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020516 plen = (int)STRLEN(p);
20517 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020518 if (slen > plen && fnamecmp(p,
20519 sourcing_name + slen - plen) == 0)
20520 j = OK;
20521 vim_free(scriptname);
20522 }
20523 }
20524 if (j == FAIL)
20525 {
20526 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20527 goto erret;
20528 }
20529 }
20530
20531 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 if (fp == NULL)
20533 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020534
20535 if (fudi.fd_dict != NULL)
20536 {
20537 if (fudi.fd_di == NULL)
20538 {
20539 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020540 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020541 if (fudi.fd_di == NULL)
20542 {
20543 vim_free(fp);
20544 goto erret;
20545 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020546 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20547 {
20548 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020549 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020550 goto erret;
20551 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020552 }
20553 else
20554 /* overwrite existing dict entry */
20555 clear_tv(&fudi.fd_di->di_tv);
20556 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020557 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020558 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020559 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020560
20561 /* behave like "dict" was used */
20562 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020563 }
20564
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020566 STRCPY(fp->uf_name, name);
20567 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020569 fp->uf_args = newargs;
20570 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020571#ifdef FEAT_PROFILE
20572 fp->uf_tml_count = NULL;
20573 fp->uf_tml_total = NULL;
20574 fp->uf_tml_self = NULL;
20575 fp->uf_profiling = FALSE;
20576 if (prof_def_func())
20577 func_do_profile(fp);
20578#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020579 fp->uf_varargs = varargs;
20580 fp->uf_flags = flags;
20581 fp->uf_calls = 0;
20582 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020583 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584
20585erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 ga_clear_strings(&newargs);
20587 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020588ret_free:
20589 vim_free(skip_until);
20590 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593}
20594
20595/*
20596 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020597 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020599 * flags:
20600 * TFN_INT: internal function name OK
20601 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 * Advances "pp" to just after the function name (if no error).
20603 */
20604 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020605trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606 char_u **pp;
20607 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020608 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020609 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020611 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 char_u *start;
20613 char_u *end;
20614 int lead;
20615 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020618
20619 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020620 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020622
20623 /* Check for hard coded <SNR>: already translated function ID (from a user
20624 * command). */
20625 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20626 && (*pp)[2] == (int)KE_SNR)
20627 {
20628 *pp += 3;
20629 len = get_id_len(pp) + 3;
20630 return vim_strnsave(start, len);
20631 }
20632
20633 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20634 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020636 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020638
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020639 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20640 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020641 if (end == start)
20642 {
20643 if (!skip)
20644 EMSG(_("E129: Function name required"));
20645 goto theend;
20646 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020647 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020648 {
20649 /*
20650 * Report an invalid expression in braces, unless the expression
20651 * evaluation has been cancelled due to an aborting error, an
20652 * interrupt, or an exception.
20653 */
20654 if (!aborting())
20655 {
20656 if (end != NULL)
20657 EMSG2(_(e_invarg2), start);
20658 }
20659 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020660 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020661 goto theend;
20662 }
20663
20664 if (lv.ll_tv != NULL)
20665 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020666 if (fdp != NULL)
20667 {
20668 fdp->fd_dict = lv.ll_dict;
20669 fdp->fd_newkey = lv.ll_newkey;
20670 lv.ll_newkey = NULL;
20671 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020673 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20674 {
20675 name = vim_strsave(lv.ll_tv->vval.v_string);
20676 *pp = end;
20677 }
20678 else
20679 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020680 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20681 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020682 EMSG(_(e_funcref));
20683 else
20684 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020685 name = NULL;
20686 }
20687 goto theend;
20688 }
20689
20690 if (lv.ll_name == NULL)
20691 {
20692 /* Error found, but continue after the function name. */
20693 *pp = end;
20694 goto theend;
20695 }
20696
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020697 /* Check if the name is a Funcref. If so, use the value. */
20698 if (lv.ll_exp_name != NULL)
20699 {
20700 len = (int)STRLEN(lv.ll_exp_name);
20701 name = deref_func_name(lv.ll_exp_name, &len);
20702 if (name == lv.ll_exp_name)
20703 name = NULL;
20704 }
20705 else
20706 {
20707 len = (int)(end - *pp);
20708 name = deref_func_name(*pp, &len);
20709 if (name == *pp)
20710 name = NULL;
20711 }
20712 if (name != NULL)
20713 {
20714 name = vim_strsave(name);
20715 *pp = end;
20716 goto theend;
20717 }
20718
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020719 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020720 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020721 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020722 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20723 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20724 {
20725 /* When there was "s:" already or the name expanded to get a
20726 * leading "s:" then remove it. */
20727 lv.ll_name += 2;
20728 len -= 2;
20729 lead = 2;
20730 }
20731 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020732 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020733 {
20734 if (lead == 2) /* skip over "s:" */
20735 lv.ll_name += 2;
20736 len = (int)(end - lv.ll_name);
20737 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020738
20739 /*
20740 * Copy the function name to allocated memory.
20741 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20742 * Accept <SNR>123_name() outside a script.
20743 */
20744 if (skip)
20745 lead = 0; /* do nothing */
20746 else if (lead > 0)
20747 {
20748 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020749 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20750 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020751 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020752 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020753 if (current_SID <= 0)
20754 {
20755 EMSG(_(e_usingsid));
20756 goto theend;
20757 }
20758 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20759 lead += (int)STRLEN(sid_buf);
20760 }
20761 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020762 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020763 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020764 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020765 goto theend;
20766 }
20767 name = alloc((unsigned)(len + lead + 1));
20768 if (name != NULL)
20769 {
20770 if (lead > 0)
20771 {
20772 name[0] = K_SPECIAL;
20773 name[1] = KS_EXTRA;
20774 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020775 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020776 STRCPY(name + 3, sid_buf);
20777 }
20778 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20779 name[len + lead] = NUL;
20780 }
20781 *pp = end;
20782
20783theend:
20784 clear_lval(&lv);
20785 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786}
20787
20788/*
20789 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20790 * Return 2 if "p" starts with "s:".
20791 * Return 0 otherwise.
20792 */
20793 static int
20794eval_fname_script(p)
20795 char_u *p;
20796{
20797 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20798 || STRNICMP(p + 1, "SNR>", 4) == 0))
20799 return 5;
20800 if (p[0] == 's' && p[1] == ':')
20801 return 2;
20802 return 0;
20803}
20804
20805/*
20806 * Return TRUE if "p" starts with "<SID>" or "s:".
20807 * Only works if eval_fname_script() returned non-zero for "p"!
20808 */
20809 static int
20810eval_fname_sid(p)
20811 char_u *p;
20812{
20813 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20814}
20815
20816/*
20817 * List the head of the function: "name(arg1, arg2)".
20818 */
20819 static void
20820list_func_head(fp, indent)
20821 ufunc_T *fp;
20822 int indent;
20823{
20824 int j;
20825
20826 msg_start();
20827 if (indent)
20828 MSG_PUTS(" ");
20829 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020830 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 {
20832 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020833 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 }
20835 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020836 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020838 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 {
20840 if (j)
20841 MSG_PUTS(", ");
20842 msg_puts(FUNCARG(fp, j));
20843 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020844 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 {
20846 if (j)
20847 MSG_PUTS(", ");
20848 MSG_PUTS("...");
20849 }
20850 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020851 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020852 if (p_verbose > 0)
20853 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854}
20855
20856/*
20857 * Find a function by name, return pointer to it in ufuncs.
20858 * Return NULL for unknown function.
20859 */
20860 static ufunc_T *
20861find_func(name)
20862 char_u *name;
20863{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020864 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020866 hi = hash_find(&func_hashtab, name);
20867 if (!HASHITEM_EMPTY(hi))
20868 return HI2UF(hi);
20869 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870}
20871
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020872#if defined(EXITFREE) || defined(PROTO)
20873 void
20874free_all_functions()
20875{
20876 hashitem_T *hi;
20877
20878 /* Need to start all over every time, because func_free() may change the
20879 * hash table. */
20880 while (func_hashtab.ht_used > 0)
20881 for (hi = func_hashtab.ht_array; ; ++hi)
20882 if (!HASHITEM_EMPTY(hi))
20883 {
20884 func_free(HI2UF(hi));
20885 break;
20886 }
20887}
20888#endif
20889
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020890/*
20891 * Return TRUE if a function "name" exists.
20892 */
20893 static int
20894function_exists(name)
20895 char_u *name;
20896{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020897 char_u *nm = name;
20898 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020899 int n = FALSE;
20900
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020901 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020902 nm = skipwhite(nm);
20903
20904 /* Only accept "funcname", "funcname ", "funcname (..." and
20905 * "funcname(...", not "funcname!...". */
20906 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020907 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020908 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020909 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020910 else
20911 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020912 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020913 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020914 return n;
20915}
20916
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020917/*
20918 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020919 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020920 */
20921 static int
20922builtin_function(name)
20923 char_u *name;
20924{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020925 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20926 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020927}
20928
Bram Moolenaar05159a02005-02-26 23:04:13 +000020929#if defined(FEAT_PROFILE) || defined(PROTO)
20930/*
20931 * Start profiling function "fp".
20932 */
20933 static void
20934func_do_profile(fp)
20935 ufunc_T *fp;
20936{
20937 fp->uf_tm_count = 0;
20938 profile_zero(&fp->uf_tm_self);
20939 profile_zero(&fp->uf_tm_total);
20940 if (fp->uf_tml_count == NULL)
20941 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20942 (sizeof(int) * fp->uf_lines.ga_len));
20943 if (fp->uf_tml_total == NULL)
20944 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20945 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20946 if (fp->uf_tml_self == NULL)
20947 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20948 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20949 fp->uf_tml_idx = -1;
20950 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20951 || fp->uf_tml_self == NULL)
20952 return; /* out of memory */
20953
20954 fp->uf_profiling = TRUE;
20955}
20956
20957/*
20958 * Dump the profiling results for all functions in file "fd".
20959 */
20960 void
20961func_dump_profile(fd)
20962 FILE *fd;
20963{
20964 hashitem_T *hi;
20965 int todo;
20966 ufunc_T *fp;
20967 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020968 ufunc_T **sorttab;
20969 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020970
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020971 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020972 if (todo == 0)
20973 return; /* nothing to dump */
20974
Bram Moolenaar73830342005-02-28 22:48:19 +000020975 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20976
Bram Moolenaar05159a02005-02-26 23:04:13 +000020977 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20978 {
20979 if (!HASHITEM_EMPTY(hi))
20980 {
20981 --todo;
20982 fp = HI2UF(hi);
20983 if (fp->uf_profiling)
20984 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020985 if (sorttab != NULL)
20986 sorttab[st_len++] = fp;
20987
Bram Moolenaar05159a02005-02-26 23:04:13 +000020988 if (fp->uf_name[0] == K_SPECIAL)
20989 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20990 else
20991 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20992 if (fp->uf_tm_count == 1)
20993 fprintf(fd, "Called 1 time\n");
20994 else
20995 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20996 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20997 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20998 fprintf(fd, "\n");
20999 fprintf(fd, "count total (s) self (s)\n");
21000
21001 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21002 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021003 if (FUNCLINE(fp, i) == NULL)
21004 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021005 prof_func_line(fd, fp->uf_tml_count[i],
21006 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021007 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21008 }
21009 fprintf(fd, "\n");
21010 }
21011 }
21012 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021013
21014 if (sorttab != NULL && st_len > 0)
21015 {
21016 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21017 prof_total_cmp);
21018 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21019 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21020 prof_self_cmp);
21021 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21022 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021023
21024 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021025}
Bram Moolenaar73830342005-02-28 22:48:19 +000021026
21027 static void
21028prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21029 FILE *fd;
21030 ufunc_T **sorttab;
21031 int st_len;
21032 char *title;
21033 int prefer_self; /* when equal print only self time */
21034{
21035 int i;
21036 ufunc_T *fp;
21037
21038 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21039 fprintf(fd, "count total (s) self (s) function\n");
21040 for (i = 0; i < 20 && i < st_len; ++i)
21041 {
21042 fp = sorttab[i];
21043 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21044 prefer_self);
21045 if (fp->uf_name[0] == K_SPECIAL)
21046 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21047 else
21048 fprintf(fd, " %s()\n", fp->uf_name);
21049 }
21050 fprintf(fd, "\n");
21051}
21052
21053/*
21054 * Print the count and times for one function or function line.
21055 */
21056 static void
21057prof_func_line(fd, count, total, self, prefer_self)
21058 FILE *fd;
21059 int count;
21060 proftime_T *total;
21061 proftime_T *self;
21062 int prefer_self; /* when equal print only self time */
21063{
21064 if (count > 0)
21065 {
21066 fprintf(fd, "%5d ", count);
21067 if (prefer_self && profile_equal(total, self))
21068 fprintf(fd, " ");
21069 else
21070 fprintf(fd, "%s ", profile_msg(total));
21071 if (!prefer_self && profile_equal(total, self))
21072 fprintf(fd, " ");
21073 else
21074 fprintf(fd, "%s ", profile_msg(self));
21075 }
21076 else
21077 fprintf(fd, " ");
21078}
21079
21080/*
21081 * Compare function for total time sorting.
21082 */
21083 static int
21084#ifdef __BORLANDC__
21085_RTLENTRYF
21086#endif
21087prof_total_cmp(s1, s2)
21088 const void *s1;
21089 const void *s2;
21090{
21091 ufunc_T *p1, *p2;
21092
21093 p1 = *(ufunc_T **)s1;
21094 p2 = *(ufunc_T **)s2;
21095 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21096}
21097
21098/*
21099 * Compare function for self time sorting.
21100 */
21101 static int
21102#ifdef __BORLANDC__
21103_RTLENTRYF
21104#endif
21105prof_self_cmp(s1, s2)
21106 const void *s1;
21107 const void *s2;
21108{
21109 ufunc_T *p1, *p2;
21110
21111 p1 = *(ufunc_T **)s1;
21112 p2 = *(ufunc_T **)s2;
21113 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21114}
21115
Bram Moolenaar05159a02005-02-26 23:04:13 +000021116#endif
21117
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021118/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021119 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021120 * Return TRUE if a package was loaded.
21121 */
21122 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021123script_autoload(name, reload)
21124 char_u *name;
21125 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021126{
21127 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021128 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021129 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021130 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021131
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021132 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021133 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021134 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021135 return FALSE;
21136
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021137 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021138
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021139 /* Find the name in the list of previously loaded package names. Skip
21140 * "autoload/", it's always the same. */
21141 for (i = 0; i < ga_loaded.ga_len; ++i)
21142 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21143 break;
21144 if (!reload && i < ga_loaded.ga_len)
21145 ret = FALSE; /* was loaded already */
21146 else
21147 {
21148 /* Remember the name if it wasn't loaded already. */
21149 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21150 {
21151 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21152 tofree = NULL;
21153 }
21154
21155 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021156 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021157 ret = TRUE;
21158 }
21159
21160 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021161 return ret;
21162}
21163
21164/*
21165 * Return the autoload script name for a function or variable name.
21166 * Returns NULL when out of memory.
21167 */
21168 static char_u *
21169autoload_name(name)
21170 char_u *name;
21171{
21172 char_u *p;
21173 char_u *scriptname;
21174
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021175 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021176 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21177 if (scriptname == NULL)
21178 return FALSE;
21179 STRCPY(scriptname, "autoload/");
21180 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021181 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021182 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021183 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021184 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021185 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021186}
21187
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21189
21190/*
21191 * Function given to ExpandGeneric() to obtain the list of user defined
21192 * function names.
21193 */
21194 char_u *
21195get_user_func_name(xp, idx)
21196 expand_T *xp;
21197 int idx;
21198{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021199 static long_u done;
21200 static hashitem_T *hi;
21201 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202
21203 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021205 done = 0;
21206 hi = func_hashtab.ht_array;
21207 }
21208 if (done < func_hashtab.ht_used)
21209 {
21210 if (done++ > 0)
21211 ++hi;
21212 while (HASHITEM_EMPTY(hi))
21213 ++hi;
21214 fp = HI2UF(hi);
21215
21216 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21217 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218
21219 cat_func_name(IObuff, fp);
21220 if (xp->xp_context != EXPAND_USER_FUNC)
21221 {
21222 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021223 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 STRCAT(IObuff, ")");
21225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 return IObuff;
21227 }
21228 return NULL;
21229}
21230
21231#endif /* FEAT_CMDL_COMPL */
21232
21233/*
21234 * Copy the function name of "fp" to buffer "buf".
21235 * "buf" must be able to hold the function name plus three bytes.
21236 * Takes care of script-local function names.
21237 */
21238 static void
21239cat_func_name(buf, fp)
21240 char_u *buf;
21241 ufunc_T *fp;
21242{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021243 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 {
21245 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021246 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247 }
21248 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021249 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250}
21251
21252/*
21253 * ":delfunction {name}"
21254 */
21255 void
21256ex_delfunction(eap)
21257 exarg_T *eap;
21258{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021259 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 char_u *p;
21261 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021262 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263
21264 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021265 name = trans_function_name(&p, eap->skip, 0, &fudi);
21266 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021268 {
21269 if (fudi.fd_dict != NULL && !eap->skip)
21270 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 if (!ends_excmd(*skipwhite(p)))
21274 {
21275 vim_free(name);
21276 EMSG(_(e_trailing));
21277 return;
21278 }
21279 eap->nextcmd = check_nextcmd(p);
21280 if (eap->nextcmd != NULL)
21281 *p = NUL;
21282
21283 if (!eap->skip)
21284 fp = find_func(name);
21285 vim_free(name);
21286
21287 if (!eap->skip)
21288 {
21289 if (fp == NULL)
21290 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021291 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 return;
21293 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021294 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 {
21296 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21297 return;
21298 }
21299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021300 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021302 /* Delete the dict item that refers to the function, it will
21303 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021304 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021306 else
21307 func_free(fp);
21308 }
21309}
21310
21311/*
21312 * Free a function and remove it from the list of functions.
21313 */
21314 static void
21315func_free(fp)
21316 ufunc_T *fp;
21317{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021318 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021319
21320 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021321 ga_clear_strings(&(fp->uf_args));
21322 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021323#ifdef FEAT_PROFILE
21324 vim_free(fp->uf_tml_count);
21325 vim_free(fp->uf_tml_total);
21326 vim_free(fp->uf_tml_self);
21327#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021328
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021329 /* remove the function from the function hashtable */
21330 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21331 if (HASHITEM_EMPTY(hi))
21332 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021333 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021334 hash_remove(&func_hashtab, hi);
21335
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021336 vim_free(fp);
21337}
21338
21339/*
21340 * Unreference a Function: decrement the reference count and free it when it
21341 * becomes zero. Only for numbered functions.
21342 */
21343 static void
21344func_unref(name)
21345 char_u *name;
21346{
21347 ufunc_T *fp;
21348
21349 if (name != NULL && isdigit(*name))
21350 {
21351 fp = find_func(name);
21352 if (fp == NULL)
21353 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021354 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021355 {
21356 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021357 * when "uf_calls" becomes zero. */
21358 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021359 func_free(fp);
21360 }
21361 }
21362}
21363
21364/*
21365 * Count a reference to a Function.
21366 */
21367 static void
21368func_ref(name)
21369 char_u *name;
21370{
21371 ufunc_T *fp;
21372
21373 if (name != NULL && isdigit(*name))
21374 {
21375 fp = find_func(name);
21376 if (fp == NULL)
21377 EMSG2(_(e_intern2), "func_ref()");
21378 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021379 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 }
21381}
21382
21383/*
21384 * Call a user function.
21385 */
21386 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021387call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 ufunc_T *fp; /* pointer to function */
21389 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021390 typval_T *argvars; /* arguments */
21391 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 linenr_T firstline; /* first line of range */
21393 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021394 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395{
Bram Moolenaar33570922005-01-25 22:26:29 +000021396 char_u *save_sourcing_name;
21397 linenr_T save_sourcing_lnum;
21398 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021399 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021400 int save_did_emsg;
21401 static int depth = 0;
21402 dictitem_T *v;
21403 int fixvar_idx = 0; /* index in fixvar[] */
21404 int i;
21405 int ai;
21406 char_u numbuf[NUMBUFLEN];
21407 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021408#ifdef FEAT_PROFILE
21409 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021410 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412
21413 /* If depth of calling is getting too high, don't execute the function */
21414 if (depth >= p_mfd)
21415 {
21416 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021417 rettv->v_type = VAR_NUMBER;
21418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 return;
21420 }
21421 ++depth;
21422
21423 line_breakcheck(); /* check for CTRL-C hit */
21424
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021425 fc = (funccall_T *)alloc(sizeof(funccall_T));
21426 fc->caller = current_funccal;
21427 current_funccal = fc;
21428 fc->func = fp;
21429 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021430 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021431 fc->linenr = 0;
21432 fc->returned = FALSE;
21433 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021435 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21436 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437
Bram Moolenaar33570922005-01-25 22:26:29 +000021438 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021439 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021440 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21441 * each argument variable and saves a lot of time.
21442 */
21443 /*
21444 * Init l: variables.
21445 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021446 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021447 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021448 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021449 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21450 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021451 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021452 name = v->di_key;
21453 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021454 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021455 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021456 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021457 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021458 v->di_tv.vval.v_dict = selfdict;
21459 ++selfdict->dv_refcount;
21460 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021461
Bram Moolenaar33570922005-01-25 22:26:29 +000021462 /*
21463 * Init a: variables.
21464 * Set a:0 to "argcount".
21465 * Set a:000 to a list with room for the "..." arguments.
21466 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021467 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21468 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021469 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021470 /* Use "name" to avoid a warning from some compiler that checks the
21471 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021472 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021473 name = v->di_key;
21474 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021475 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021476 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021477 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021478 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021479 v->di_tv.vval.v_list = &fc->l_varlist;
21480 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21481 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21482 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021483
21484 /*
21485 * Set a:firstline to "firstline" and a:lastline to "lastline".
21486 * Set a:name to named arguments.
21487 * Set a:N to the "..." arguments.
21488 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021489 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021490 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021491 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021492 (varnumber_T)lastline);
21493 for (i = 0; i < argcount; ++i)
21494 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021495 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021496 if (ai < 0)
21497 /* named argument a:name */
21498 name = FUNCARG(fp, i);
21499 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021500 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021501 /* "..." argument a:1, a:2, etc. */
21502 sprintf((char *)numbuf, "%d", ai + 1);
21503 name = numbuf;
21504 }
21505 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21506 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021507 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021508 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21509 }
21510 else
21511 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021512 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21513 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021514 if (v == NULL)
21515 break;
21516 v->di_flags = DI_FLAGS_RO;
21517 }
21518 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021519 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021520
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021521 /* Note: the values are copied directly to avoid alloc/free.
21522 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021523 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021524 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021525
21526 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21527 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021528 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21529 fc->l_listitems[ai].li_tv = argvars[i];
21530 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021531 }
21532 }
21533
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 /* Don't redraw while executing the function. */
21535 ++RedrawingDisabled;
21536 save_sourcing_name = sourcing_name;
21537 save_sourcing_lnum = sourcing_lnum;
21538 sourcing_lnum = 1;
21539 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021540 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 if (sourcing_name != NULL)
21542 {
21543 if (save_sourcing_name != NULL
21544 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21545 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21546 else
21547 STRCPY(sourcing_name, "function ");
21548 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21549
21550 if (p_verbose >= 12)
21551 {
21552 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021553 verbose_enter_scroll();
21554
Bram Moolenaar555b2802005-05-19 21:08:39 +000021555 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 if (p_verbose >= 14)
21557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021559 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021560 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021561 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562
21563 msg_puts((char_u *)"(");
21564 for (i = 0; i < argcount; ++i)
21565 {
21566 if (i > 0)
21567 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021568 if (argvars[i].v_type == VAR_NUMBER)
21569 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 else
21571 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021572 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21573 if (s != NULL)
21574 {
21575 trunc_string(s, buf, MSG_BUF_CLEN);
21576 msg_puts(buf);
21577 vim_free(tofree);
21578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 }
21580 }
21581 msg_puts((char_u *)")");
21582 }
21583 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021584
21585 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 --no_wait_return;
21587 }
21588 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021589#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021590 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021591 {
21592 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21593 func_do_profile(fp);
21594 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021595 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021596 {
21597 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021598 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021599 profile_zero(&fp->uf_tm_children);
21600 }
21601 script_prof_save(&wait_start);
21602 }
21603#endif
21604
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021606 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 save_did_emsg = did_emsg;
21608 did_emsg = FALSE;
21609
21610 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021611 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21613
21614 --RedrawingDisabled;
21615
21616 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021617 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021619 clear_tv(rettv);
21620 rettv->v_type = VAR_NUMBER;
21621 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 }
21623
Bram Moolenaar05159a02005-02-26 23:04:13 +000021624#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021625 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021626 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021627 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021628 profile_end(&call_start);
21629 profile_sub_wait(&wait_start, &call_start);
21630 profile_add(&fp->uf_tm_total, &call_start);
21631 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021632 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021633 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021634 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21635 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021636 }
21637 }
21638#endif
21639
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 /* when being verbose, mention the return value */
21641 if (p_verbose >= 12)
21642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021644 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021647 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021648 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021649 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021650 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021651 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021653 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021654 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021655 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021656 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021657
Bram Moolenaar555b2802005-05-19 21:08:39 +000021658 /* The value may be very long. Skip the middle part, so that we
21659 * have some idea how it starts and ends. smsg() would always
21660 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021661 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021662 if (s != NULL)
21663 {
21664 trunc_string(s, buf, MSG_BUF_CLEN);
21665 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21666 vim_free(tofree);
21667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 }
21669 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021670
21671 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 --no_wait_return;
21673 }
21674
21675 vim_free(sourcing_name);
21676 sourcing_name = save_sourcing_name;
21677 sourcing_lnum = save_sourcing_lnum;
21678 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021679#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021680 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021681 script_prof_restore(&wait_start);
21682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683
21684 if (p_verbose >= 12 && sourcing_name != NULL)
21685 {
21686 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021687 verbose_enter_scroll();
21688
Bram Moolenaar555b2802005-05-19 21:08:39 +000021689 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021691
21692 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 --no_wait_return;
21694 }
21695
21696 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021697 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021699
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021700 /* If the a:000 list and the l: and a: dicts are not referenced we can
21701 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021702 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21703 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21704 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21705 {
21706 free_funccal(fc, FALSE);
21707 }
21708 else
21709 {
21710 hashitem_T *hi;
21711 listitem_T *li;
21712 int todo;
21713
21714 /* "fc" is still in use. This can happen when returning "a:000" or
21715 * assigning "l:" to a global variable.
21716 * Link "fc" in the list for garbage collection later. */
21717 fc->caller = previous_funccal;
21718 previous_funccal = fc;
21719
21720 /* Make a copy of the a: variables, since we didn't do that above. */
21721 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21722 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21723 {
21724 if (!HASHITEM_EMPTY(hi))
21725 {
21726 --todo;
21727 v = HI2DI(hi);
21728 copy_tv(&v->di_tv, &v->di_tv);
21729 }
21730 }
21731
21732 /* Make a copy of the a:000 items, since we didn't do that above. */
21733 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21734 copy_tv(&li->li_tv, &li->li_tv);
21735 }
21736}
21737
21738/*
21739 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021740 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021741 */
21742 static int
21743can_free_funccal(fc, copyID)
21744 funccall_T *fc;
21745 int copyID;
21746{
21747 return (fc->l_varlist.lv_copyID != copyID
21748 && fc->l_vars.dv_copyID != copyID
21749 && fc->l_avars.dv_copyID != copyID);
21750}
21751
21752/*
21753 * Free "fc" and what it contains.
21754 */
21755 static void
21756free_funccal(fc, free_val)
21757 funccall_T *fc;
21758 int free_val; /* a: vars were allocated */
21759{
21760 listitem_T *li;
21761
21762 /* The a: variables typevals may not have been allocated, only free the
21763 * allocated variables. */
21764 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21765
21766 /* free all l: variables */
21767 vars_clear(&fc->l_vars.dv_hashtab);
21768
21769 /* Free the a:000 variables if they were allocated. */
21770 if (free_val)
21771 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21772 clear_tv(&li->li_tv);
21773
21774 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775}
21776
21777/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021778 * Add a number variable "name" to dict "dp" with value "nr".
21779 */
21780 static void
21781add_nr_var(dp, v, name, nr)
21782 dict_T *dp;
21783 dictitem_T *v;
21784 char *name;
21785 varnumber_T nr;
21786{
21787 STRCPY(v->di_key, name);
21788 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21789 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21790 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021791 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 v->di_tv.vval.v_number = nr;
21793}
21794
21795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 * ":return [expr]"
21797 */
21798 void
21799ex_return(eap)
21800 exarg_T *eap;
21801{
21802 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021803 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 int returning = FALSE;
21805
21806 if (current_funccal == NULL)
21807 {
21808 EMSG(_("E133: :return not inside a function"));
21809 return;
21810 }
21811
21812 if (eap->skip)
21813 ++emsg_skip;
21814
21815 eap->nextcmd = NULL;
21816 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021817 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 {
21819 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021820 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021822 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 }
21824 /* It's safer to return also on error. */
21825 else if (!eap->skip)
21826 {
21827 /*
21828 * Return unless the expression evaluation has been cancelled due to an
21829 * aborting error, an interrupt, or an exception.
21830 */
21831 if (!aborting())
21832 returning = do_return(eap, FALSE, TRUE, NULL);
21833 }
21834
21835 /* When skipping or the return gets pending, advance to the next command
21836 * in this line (!returning). Otherwise, ignore the rest of the line.
21837 * Following lines will be ignored by get_func_line(). */
21838 if (returning)
21839 eap->nextcmd = NULL;
21840 else if (eap->nextcmd == NULL) /* no argument */
21841 eap->nextcmd = check_nextcmd(arg);
21842
21843 if (eap->skip)
21844 --emsg_skip;
21845}
21846
21847/*
21848 * Return from a function. Possibly makes the return pending. Also called
21849 * for a pending return at the ":endtry" or after returning from an extra
21850 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021851 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021852 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 * FALSE when the return gets pending.
21854 */
21855 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021856do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 exarg_T *eap;
21858 int reanimate;
21859 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021860 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861{
21862 int idx;
21863 struct condstack *cstack = eap->cstack;
21864
21865 if (reanimate)
21866 /* Undo the return. */
21867 current_funccal->returned = FALSE;
21868
21869 /*
21870 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21871 * not in its finally clause (which then is to be executed next) is found.
21872 * In this case, make the ":return" pending for execution at the ":endtry".
21873 * Otherwise, return normally.
21874 */
21875 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21876 if (idx >= 0)
21877 {
21878 cstack->cs_pending[idx] = CSTP_RETURN;
21879
21880 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021881 /* A pending return again gets pending. "rettv" points to an
21882 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021884 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 else
21886 {
21887 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021888 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021890 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021892 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 {
21894 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021895 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021896 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 else
21898 EMSG(_(e_outofmem));
21899 }
21900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021901 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902
21903 if (reanimate)
21904 {
21905 /* The pending return value could be overwritten by a ":return"
21906 * without argument in a finally clause; reset the default
21907 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021908 current_funccal->rettv->v_type = VAR_NUMBER;
21909 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 }
21911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021912 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 }
21914 else
21915 {
21916 current_funccal->returned = TRUE;
21917
21918 /* If the return is carried out now, store the return value. For
21919 * a return immediately after reanimation, the value is already
21920 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021921 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021923 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021924 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021926 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 }
21928 }
21929
21930 return idx < 0;
21931}
21932
21933/*
21934 * Free the variable with a pending return value.
21935 */
21936 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021937discard_pending_return(rettv)
21938 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939{
Bram Moolenaar33570922005-01-25 22:26:29 +000021940 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941}
21942
21943/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021944 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945 * is an allocated string. Used by report_pending() for verbose messages.
21946 */
21947 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021948get_return_cmd(rettv)
21949 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021951 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021952 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021953 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021955 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021956 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021957 if (s == NULL)
21958 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021959
21960 STRCPY(IObuff, ":return ");
21961 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21962 if (STRLEN(s) + 8 >= IOSIZE)
21963 STRCPY(IObuff + IOSIZE - 4, "...");
21964 vim_free(tofree);
21965 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966}
21967
21968/*
21969 * Get next function line.
21970 * Called by do_cmdline() to get the next line.
21971 * Returns allocated string, or NULL for end of function.
21972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 char_u *
21974get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021975 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021977 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978{
Bram Moolenaar33570922005-01-25 22:26:29 +000021979 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021980 ufunc_T *fp = fcp->func;
21981 char_u *retval;
21982 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983
21984 /* If breakpoints have been added/deleted need to check for it. */
21985 if (fcp->dbg_tick != debug_tick)
21986 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021987 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 sourcing_lnum);
21989 fcp->dbg_tick = debug_tick;
21990 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021991#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021992 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021993 func_line_end(cookie);
21994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995
Bram Moolenaar05159a02005-02-26 23:04:13 +000021996 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021997 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21998 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 retval = NULL;
22000 else
22001 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022002 /* Skip NULL lines (continuation lines). */
22003 while (fcp->linenr < gap->ga_len
22004 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22005 ++fcp->linenr;
22006 if (fcp->linenr >= gap->ga_len)
22007 retval = NULL;
22008 else
22009 {
22010 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22011 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022012#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022013 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022014 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022015#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 }
22018
22019 /* Did we encounter a breakpoint? */
22020 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22021 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022022 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022024 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 sourcing_lnum);
22026 fcp->dbg_tick = debug_tick;
22027 }
22028
22029 return retval;
22030}
22031
Bram Moolenaar05159a02005-02-26 23:04:13 +000022032#if defined(FEAT_PROFILE) || defined(PROTO)
22033/*
22034 * Called when starting to read a function line.
22035 * "sourcing_lnum" must be correct!
22036 * When skipping lines it may not actually be executed, but we won't find out
22037 * until later and we need to store the time now.
22038 */
22039 void
22040func_line_start(cookie)
22041 void *cookie;
22042{
22043 funccall_T *fcp = (funccall_T *)cookie;
22044 ufunc_T *fp = fcp->func;
22045
22046 if (fp->uf_profiling && sourcing_lnum >= 1
22047 && sourcing_lnum <= fp->uf_lines.ga_len)
22048 {
22049 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022050 /* Skip continuation lines. */
22051 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22052 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022053 fp->uf_tml_execed = FALSE;
22054 profile_start(&fp->uf_tml_start);
22055 profile_zero(&fp->uf_tml_children);
22056 profile_get_wait(&fp->uf_tml_wait);
22057 }
22058}
22059
22060/*
22061 * Called when actually executing a function line.
22062 */
22063 void
22064func_line_exec(cookie)
22065 void *cookie;
22066{
22067 funccall_T *fcp = (funccall_T *)cookie;
22068 ufunc_T *fp = fcp->func;
22069
22070 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22071 fp->uf_tml_execed = TRUE;
22072}
22073
22074/*
22075 * Called when done with a function line.
22076 */
22077 void
22078func_line_end(cookie)
22079 void *cookie;
22080{
22081 funccall_T *fcp = (funccall_T *)cookie;
22082 ufunc_T *fp = fcp->func;
22083
22084 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22085 {
22086 if (fp->uf_tml_execed)
22087 {
22088 ++fp->uf_tml_count[fp->uf_tml_idx];
22089 profile_end(&fp->uf_tml_start);
22090 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022091 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022092 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22093 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022094 }
22095 fp->uf_tml_idx = -1;
22096 }
22097}
22098#endif
22099
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100/*
22101 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022102 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 */
22104 int
22105func_has_ended(cookie)
22106 void *cookie;
22107{
Bram Moolenaar33570922005-01-25 22:26:29 +000022108 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109
22110 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22111 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022112 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113 || fcp->returned);
22114}
22115
22116/*
22117 * return TRUE if cookie indicates a function which "abort"s on errors.
22118 */
22119 int
22120func_has_abort(cookie)
22121 void *cookie;
22122{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022123 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124}
22125
22126#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22127typedef enum
22128{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022129 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22130 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22131 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132} var_flavour_T;
22133
22134static var_flavour_T var_flavour __ARGS((char_u *varname));
22135
22136 static var_flavour_T
22137var_flavour(varname)
22138 char_u *varname;
22139{
22140 char_u *p = varname;
22141
22142 if (ASCII_ISUPPER(*p))
22143 {
22144 while (*(++p))
22145 if (ASCII_ISLOWER(*p))
22146 return VAR_FLAVOUR_SESSION;
22147 return VAR_FLAVOUR_VIMINFO;
22148 }
22149 else
22150 return VAR_FLAVOUR_DEFAULT;
22151}
22152#endif
22153
22154#if defined(FEAT_VIMINFO) || defined(PROTO)
22155/*
22156 * Restore global vars that start with a capital from the viminfo file
22157 */
22158 int
22159read_viminfo_varlist(virp, writing)
22160 vir_T *virp;
22161 int writing;
22162{
22163 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022164 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022165 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166
22167 if (!writing && (find_viminfo_parameter('!') != NULL))
22168 {
22169 tab = vim_strchr(virp->vir_line + 1, '\t');
22170 if (tab != NULL)
22171 {
22172 *tab++ = '\0'; /* isolate the variable name */
22173 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022174 type = VAR_STRING;
22175#ifdef FEAT_FLOAT
22176 else if (*tab == 'F')
22177 type = VAR_FLOAT;
22178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179
22180 tab = vim_strchr(tab, '\t');
22181 if (tab != NULL)
22182 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022183 tv.v_type = type;
22184 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022185 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022187#ifdef FEAT_FLOAT
22188 else if (type == VAR_FLOAT)
22189 (void)string2float(tab + 1, &tv.vval.v_float);
22190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022192 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022193 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022194 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022195 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 }
22197 }
22198 }
22199
22200 return viminfo_readline(virp);
22201}
22202
22203/*
22204 * Write global vars that start with a capital to the viminfo file
22205 */
22206 void
22207write_viminfo_varlist(fp)
22208 FILE *fp;
22209{
Bram Moolenaar33570922005-01-25 22:26:29 +000022210 hashitem_T *hi;
22211 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022212 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022213 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022214 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022215 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022216 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217
22218 if (find_viminfo_parameter('!') == NULL)
22219 return;
22220
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022221 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022222
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022223 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022224 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022226 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022228 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022229 this_var = HI2DI(hi);
22230 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022231 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022232 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022233 {
22234 case VAR_STRING: s = "STR"; break;
22235 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022236#ifdef FEAT_FLOAT
22237 case VAR_FLOAT: s = "FLO"; break;
22238#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022239 default: continue;
22240 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022241 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022242 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022243 if (p != NULL)
22244 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022245 vim_free(tofree);
22246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 }
22248 }
22249}
22250#endif
22251
22252#if defined(FEAT_SESSION) || defined(PROTO)
22253 int
22254store_session_globals(fd)
22255 FILE *fd;
22256{
Bram Moolenaar33570922005-01-25 22:26:29 +000022257 hashitem_T *hi;
22258 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022259 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 char_u *p, *t;
22261
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022262 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022263 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022265 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022267 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022268 this_var = HI2DI(hi);
22269 if ((this_var->di_tv.v_type == VAR_NUMBER
22270 || this_var->di_tv.v_type == VAR_STRING)
22271 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022272 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022273 /* Escape special characters with a backslash. Turn a LF and
22274 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022275 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022276 (char_u *)"\\\"\n\r");
22277 if (p == NULL) /* out of memory */
22278 break;
22279 for (t = p; *t != NUL; ++t)
22280 if (*t == '\n')
22281 *t = 'n';
22282 else if (*t == '\r')
22283 *t = 'r';
22284 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022285 this_var->di_key,
22286 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22287 : ' ',
22288 p,
22289 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22290 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022291 || put_eol(fd) == FAIL)
22292 {
22293 vim_free(p);
22294 return FAIL;
22295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 vim_free(p);
22297 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022298#ifdef FEAT_FLOAT
22299 else if (this_var->di_tv.v_type == VAR_FLOAT
22300 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22301 {
22302 float_T f = this_var->di_tv.vval.v_float;
22303 int sign = ' ';
22304
22305 if (f < 0)
22306 {
22307 f = -f;
22308 sign = '-';
22309 }
22310 if ((fprintf(fd, "let %s = %c&%f",
22311 this_var->di_key, sign, f) < 0)
22312 || put_eol(fd) == FAIL)
22313 return FAIL;
22314 }
22315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 }
22317 }
22318 return OK;
22319}
22320#endif
22321
Bram Moolenaar661b1822005-07-28 22:36:45 +000022322/*
22323 * Display script name where an item was last set.
22324 * Should only be invoked when 'verbose' is non-zero.
22325 */
22326 void
22327last_set_msg(scriptID)
22328 scid_T scriptID;
22329{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022330 char_u *p;
22331
Bram Moolenaar661b1822005-07-28 22:36:45 +000022332 if (scriptID != 0)
22333 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022334 p = home_replace_save(NULL, get_scriptname(scriptID));
22335 if (p != NULL)
22336 {
22337 verbose_enter();
22338 MSG_PUTS(_("\n\tLast set from "));
22339 MSG_PUTS(p);
22340 vim_free(p);
22341 verbose_leave();
22342 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022343 }
22344}
22345
Bram Moolenaard812df62008-11-09 12:46:09 +000022346/*
22347 * List v:oldfiles in a nice way.
22348 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022349 void
22350ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022351 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022352{
22353 list_T *l = vimvars[VV_OLDFILES].vv_list;
22354 listitem_T *li;
22355 int nr = 0;
22356
22357 if (l == NULL)
22358 msg((char_u *)_("No old files"));
22359 else
22360 {
22361 msg_start();
22362 msg_scroll = TRUE;
22363 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22364 {
22365 msg_outnum((long)++nr);
22366 MSG_PUTS(": ");
22367 msg_outtrans(get_tv_string(&li->li_tv));
22368 msg_putchar('\n');
22369 out_flush(); /* output one line at a time */
22370 ui_breakcheck();
22371 }
22372 /* Assume "got_int" was set to truncate the listing. */
22373 got_int = FALSE;
22374
22375#ifdef FEAT_BROWSE_CMD
22376 if (cmdmod.browse)
22377 {
22378 quit_more = FALSE;
22379 nr = prompt_for_number(FALSE);
22380 msg_starthere();
22381 if (nr > 0)
22382 {
22383 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22384 (long)nr);
22385
22386 if (p != NULL)
22387 {
22388 p = expand_env_save(p);
22389 eap->arg = p;
22390 eap->cmdidx = CMD_edit;
22391 cmdmod.browse = FALSE;
22392 do_exedit(eap, NULL);
22393 vim_free(p);
22394 }
22395 }
22396 }
22397#endif
22398 }
22399}
22400
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401#endif /* FEAT_EVAL */
22402
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022404#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405
22406#ifdef WIN3264
22407/*
22408 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22409 */
22410static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22411static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22412static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22413
22414/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022415 * Get the short path (8.3) for the filename in "fnamep".
22416 * Only works for a valid file name.
22417 * When the path gets longer "fnamep" is changed and the allocated buffer
22418 * is put in "bufp".
22419 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22420 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 */
22422 static int
22423get_short_pathname(fnamep, bufp, fnamelen)
22424 char_u **fnamep;
22425 char_u **bufp;
22426 int *fnamelen;
22427{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022428 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 char_u *newbuf;
22430
22431 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 l = GetShortPathName(*fnamep, *fnamep, len);
22433 if (l > len - 1)
22434 {
22435 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022436 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 newbuf = vim_strnsave(*fnamep, l);
22438 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022439 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440
22441 vim_free(*bufp);
22442 *fnamep = *bufp = newbuf;
22443
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022444 /* Really should always succeed, as the buffer is big enough. */
22445 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 }
22447
22448 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022449 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450}
22451
22452/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022453 * Get the short path (8.3) for the filename in "fname". The converted
22454 * path is returned in "bufp".
22455 *
22456 * Some of the directories specified in "fname" may not exist. This function
22457 * will shorten the existing directories at the beginning of the path and then
22458 * append the remaining non-existing path.
22459 *
22460 * fname - Pointer to the filename to shorten. On return, contains the
22461 * pointer to the shortened pathname
22462 * bufp - Pointer to an allocated buffer for the filename.
22463 * fnamelen - Length of the filename pointed to by fname
22464 *
22465 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 */
22467 static int
22468shortpath_for_invalid_fname(fname, bufp, fnamelen)
22469 char_u **fname;
22470 char_u **bufp;
22471 int *fnamelen;
22472{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022473 char_u *short_fname, *save_fname, *pbuf_unused;
22474 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022476 int old_len, len;
22477 int new_len, sfx_len;
22478 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479
22480 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022481 old_len = *fnamelen;
22482 save_fname = vim_strnsave(*fname, old_len);
22483 pbuf_unused = NULL;
22484 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022486 endp = save_fname + old_len - 1; /* Find the end of the copy */
22487 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022489 /*
22490 * Try shortening the supplied path till it succeeds by removing one
22491 * directory at a time from the tail of the path.
22492 */
22493 len = 0;
22494 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022496 /* go back one path-separator */
22497 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22498 --endp;
22499 if (endp <= save_fname)
22500 break; /* processed the complete path */
22501
22502 /*
22503 * Replace the path separator with a NUL and try to shorten the
22504 * resulting path.
22505 */
22506 ch = *endp;
22507 *endp = 0;
22508 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022509 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022510 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22511 {
22512 retval = FAIL;
22513 goto theend;
22514 }
22515 *endp = ch; /* preserve the string */
22516
22517 if (len > 0)
22518 break; /* successfully shortened the path */
22519
22520 /* failed to shorten the path. Skip the path separator */
22521 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 }
22523
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022524 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022526 /*
22527 * Succeeded in shortening the path. Now concatenate the shortened
22528 * path with the remaining path at the tail.
22529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022531 /* Compute the length of the new path. */
22532 sfx_len = (int)(save_endp - endp) + 1;
22533 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022535 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022537 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022539 /* There is not enough space in the currently allocated string,
22540 * copy it to a buffer big enough. */
22541 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022543 {
22544 retval = FAIL;
22545 goto theend;
22546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 }
22548 else
22549 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022550 /* Transfer short_fname to the main buffer (it's big enough),
22551 * unless get_short_pathname() did its work in-place. */
22552 *fname = *bufp = save_fname;
22553 if (short_fname != save_fname)
22554 vim_strncpy(save_fname, short_fname, len);
22555 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022557
22558 /* concat the not-shortened part of the path */
22559 vim_strncpy(*fname + len, endp, sfx_len);
22560 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022562
22563theend:
22564 vim_free(pbuf_unused);
22565 vim_free(save_fname);
22566
22567 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568}
22569
22570/*
22571 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022572 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 */
22574 static int
22575shortpath_for_partial(fnamep, bufp, fnamelen)
22576 char_u **fnamep;
22577 char_u **bufp;
22578 int *fnamelen;
22579{
22580 int sepcount, len, tflen;
22581 char_u *p;
22582 char_u *pbuf, *tfname;
22583 int hasTilde;
22584
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022585 /* Count up the path separators from the RHS.. so we know which part
22586 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022588 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 if (vim_ispathsep(*p))
22590 ++sepcount;
22591
22592 /* Need full path first (use expand_env() to remove a "~/") */
22593 hasTilde = (**fnamep == '~');
22594 if (hasTilde)
22595 pbuf = tfname = expand_env_save(*fnamep);
22596 else
22597 pbuf = tfname = FullName_save(*fnamep, FALSE);
22598
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022599 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022601 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22602 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603
22604 if (len == 0)
22605 {
22606 /* Don't have a valid filename, so shorten the rest of the
22607 * path if we can. This CAN give us invalid 8.3 filenames, but
22608 * there's not a lot of point in guessing what it might be.
22609 */
22610 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022611 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22612 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 }
22614
22615 /* Count the paths backward to find the beginning of the desired string. */
22616 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022617 {
22618#ifdef FEAT_MBYTE
22619 if (has_mbyte)
22620 p -= mb_head_off(tfname, p);
22621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 if (vim_ispathsep(*p))
22623 {
22624 if (sepcount == 0 || (hasTilde && sepcount == 1))
22625 break;
22626 else
22627 sepcount --;
22628 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 if (hasTilde)
22631 {
22632 --p;
22633 if (p >= tfname)
22634 *p = '~';
22635 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022636 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 }
22638 else
22639 ++p;
22640
22641 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22642 vim_free(*bufp);
22643 *fnamelen = (int)STRLEN(p);
22644 *bufp = pbuf;
22645 *fnamep = p;
22646
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022647 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648}
22649#endif /* WIN3264 */
22650
22651/*
22652 * Adjust a filename, according to a string of modifiers.
22653 * *fnamep must be NUL terminated when called. When returning, the length is
22654 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022655 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 * When there is an error, *fnamep is set to NULL.
22657 */
22658 int
22659modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22660 char_u *src; /* string with modifiers */
22661 int *usedlen; /* characters after src that are used */
22662 char_u **fnamep; /* file name so far */
22663 char_u **bufp; /* buffer for allocated file name or NULL */
22664 int *fnamelen; /* length of fnamep */
22665{
22666 int valid = 0;
22667 char_u *tail;
22668 char_u *s, *p, *pbuf;
22669 char_u dirname[MAXPATHL];
22670 int c;
22671 int has_fullname = 0;
22672#ifdef WIN3264
22673 int has_shortname = 0;
22674#endif
22675
22676repeat:
22677 /* ":p" - full path/file_name */
22678 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22679 {
22680 has_fullname = 1;
22681
22682 valid |= VALID_PATH;
22683 *usedlen += 2;
22684
22685 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22686 if ((*fnamep)[0] == '~'
22687#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22688 && ((*fnamep)[1] == '/'
22689# ifdef BACKSLASH_IN_FILENAME
22690 || (*fnamep)[1] == '\\'
22691# endif
22692 || (*fnamep)[1] == NUL)
22693
22694#endif
22695 )
22696 {
22697 *fnamep = expand_env_save(*fnamep);
22698 vim_free(*bufp); /* free any allocated file name */
22699 *bufp = *fnamep;
22700 if (*fnamep == NULL)
22701 return -1;
22702 }
22703
22704 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022705 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 {
22707 if (vim_ispathsep(*p)
22708 && p[1] == '.'
22709 && (p[2] == NUL
22710 || vim_ispathsep(p[2])
22711 || (p[2] == '.'
22712 && (p[3] == NUL || vim_ispathsep(p[3])))))
22713 break;
22714 }
22715
22716 /* FullName_save() is slow, don't use it when not needed. */
22717 if (*p != NUL || !vim_isAbsName(*fnamep))
22718 {
22719 *fnamep = FullName_save(*fnamep, *p != NUL);
22720 vim_free(*bufp); /* free any allocated file name */
22721 *bufp = *fnamep;
22722 if (*fnamep == NULL)
22723 return -1;
22724 }
22725
22726 /* Append a path separator to a directory. */
22727 if (mch_isdir(*fnamep))
22728 {
22729 /* Make room for one or two extra characters. */
22730 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22731 vim_free(*bufp); /* free any allocated file name */
22732 *bufp = *fnamep;
22733 if (*fnamep == NULL)
22734 return -1;
22735 add_pathsep(*fnamep);
22736 }
22737 }
22738
22739 /* ":." - path relative to the current directory */
22740 /* ":~" - path relative to the home directory */
22741 /* ":8" - shortname path - postponed till after */
22742 while (src[*usedlen] == ':'
22743 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22744 {
22745 *usedlen += 2;
22746 if (c == '8')
22747 {
22748#ifdef WIN3264
22749 has_shortname = 1; /* Postpone this. */
22750#endif
22751 continue;
22752 }
22753 pbuf = NULL;
22754 /* Need full path first (use expand_env() to remove a "~/") */
22755 if (!has_fullname)
22756 {
22757 if (c == '.' && **fnamep == '~')
22758 p = pbuf = expand_env_save(*fnamep);
22759 else
22760 p = pbuf = FullName_save(*fnamep, FALSE);
22761 }
22762 else
22763 p = *fnamep;
22764
22765 has_fullname = 0;
22766
22767 if (p != NULL)
22768 {
22769 if (c == '.')
22770 {
22771 mch_dirname(dirname, MAXPATHL);
22772 s = shorten_fname(p, dirname);
22773 if (s != NULL)
22774 {
22775 *fnamep = s;
22776 if (pbuf != NULL)
22777 {
22778 vim_free(*bufp); /* free any allocated file name */
22779 *bufp = pbuf;
22780 pbuf = NULL;
22781 }
22782 }
22783 }
22784 else
22785 {
22786 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22787 /* Only replace it when it starts with '~' */
22788 if (*dirname == '~')
22789 {
22790 s = vim_strsave(dirname);
22791 if (s != NULL)
22792 {
22793 *fnamep = s;
22794 vim_free(*bufp);
22795 *bufp = s;
22796 }
22797 }
22798 }
22799 vim_free(pbuf);
22800 }
22801 }
22802
22803 tail = gettail(*fnamep);
22804 *fnamelen = (int)STRLEN(*fnamep);
22805
22806 /* ":h" - head, remove "/file_name", can be repeated */
22807 /* Don't remove the first "/" or "c:\" */
22808 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22809 {
22810 valid |= VALID_HEAD;
22811 *usedlen += 2;
22812 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022813 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022814 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 *fnamelen = (int)(tail - *fnamep);
22816#ifdef VMS
22817 if (*fnamelen > 0)
22818 *fnamelen += 1; /* the path separator is part of the path */
22819#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022820 if (*fnamelen == 0)
22821 {
22822 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22823 p = vim_strsave((char_u *)".");
22824 if (p == NULL)
22825 return -1;
22826 vim_free(*bufp);
22827 *bufp = *fnamep = tail = p;
22828 *fnamelen = 1;
22829 }
22830 else
22831 {
22832 while (tail > s && !after_pathsep(s, tail))
22833 mb_ptr_back(*fnamep, tail);
22834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 }
22836
22837 /* ":8" - shortname */
22838 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22839 {
22840 *usedlen += 2;
22841#ifdef WIN3264
22842 has_shortname = 1;
22843#endif
22844 }
22845
22846#ifdef WIN3264
22847 /* Check shortname after we have done 'heads' and before we do 'tails'
22848 */
22849 if (has_shortname)
22850 {
22851 pbuf = NULL;
22852 /* Copy the string if it is shortened by :h */
22853 if (*fnamelen < (int)STRLEN(*fnamep))
22854 {
22855 p = vim_strnsave(*fnamep, *fnamelen);
22856 if (p == 0)
22857 return -1;
22858 vim_free(*bufp);
22859 *bufp = *fnamep = p;
22860 }
22861
22862 /* Split into two implementations - makes it easier. First is where
22863 * there isn't a full name already, second is where there is.
22864 */
22865 if (!has_fullname && !vim_isAbsName(*fnamep))
22866 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022867 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 return -1;
22869 }
22870 else
22871 {
22872 int l;
22873
22874 /* Simple case, already have the full-name
22875 * Nearly always shorter, so try first time. */
22876 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022877 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 return -1;
22879
22880 if (l == 0)
22881 {
22882 /* Couldn't find the filename.. search the paths.
22883 */
22884 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022885 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 return -1;
22887 }
22888 *fnamelen = l;
22889 }
22890 }
22891#endif /* WIN3264 */
22892
22893 /* ":t" - tail, just the basename */
22894 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22895 {
22896 *usedlen += 2;
22897 *fnamelen -= (int)(tail - *fnamep);
22898 *fnamep = tail;
22899 }
22900
22901 /* ":e" - extension, can be repeated */
22902 /* ":r" - root, without extension, can be repeated */
22903 while (src[*usedlen] == ':'
22904 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22905 {
22906 /* find a '.' in the tail:
22907 * - for second :e: before the current fname
22908 * - otherwise: The last '.'
22909 */
22910 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22911 s = *fnamep - 2;
22912 else
22913 s = *fnamep + *fnamelen - 1;
22914 for ( ; s > tail; --s)
22915 if (s[0] == '.')
22916 break;
22917 if (src[*usedlen + 1] == 'e') /* :e */
22918 {
22919 if (s > tail)
22920 {
22921 *fnamelen += (int)(*fnamep - (s + 1));
22922 *fnamep = s + 1;
22923#ifdef VMS
22924 /* cut version from the extension */
22925 s = *fnamep + *fnamelen - 1;
22926 for ( ; s > *fnamep; --s)
22927 if (s[0] == ';')
22928 break;
22929 if (s > *fnamep)
22930 *fnamelen = s - *fnamep;
22931#endif
22932 }
22933 else if (*fnamep <= tail)
22934 *fnamelen = 0;
22935 }
22936 else /* :r */
22937 {
22938 if (s > tail) /* remove one extension */
22939 *fnamelen = (int)(s - *fnamep);
22940 }
22941 *usedlen += 2;
22942 }
22943
22944 /* ":s?pat?foo?" - substitute */
22945 /* ":gs?pat?foo?" - global substitute */
22946 if (src[*usedlen] == ':'
22947 && (src[*usedlen + 1] == 's'
22948 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22949 {
22950 char_u *str;
22951 char_u *pat;
22952 char_u *sub;
22953 int sep;
22954 char_u *flags;
22955 int didit = FALSE;
22956
22957 flags = (char_u *)"";
22958 s = src + *usedlen + 2;
22959 if (src[*usedlen + 1] == 'g')
22960 {
22961 flags = (char_u *)"g";
22962 ++s;
22963 }
22964
22965 sep = *s++;
22966 if (sep)
22967 {
22968 /* find end of pattern */
22969 p = vim_strchr(s, sep);
22970 if (p != NULL)
22971 {
22972 pat = vim_strnsave(s, (int)(p - s));
22973 if (pat != NULL)
22974 {
22975 s = p + 1;
22976 /* find end of substitution */
22977 p = vim_strchr(s, sep);
22978 if (p != NULL)
22979 {
22980 sub = vim_strnsave(s, (int)(p - s));
22981 str = vim_strnsave(*fnamep, *fnamelen);
22982 if (sub != NULL && str != NULL)
22983 {
22984 *usedlen = (int)(p + 1 - src);
22985 s = do_string_sub(str, pat, sub, flags);
22986 if (s != NULL)
22987 {
22988 *fnamep = s;
22989 *fnamelen = (int)STRLEN(s);
22990 vim_free(*bufp);
22991 *bufp = s;
22992 didit = TRUE;
22993 }
22994 }
22995 vim_free(sub);
22996 vim_free(str);
22997 }
22998 vim_free(pat);
22999 }
23000 }
23001 /* after using ":s", repeat all the modifiers */
23002 if (didit)
23003 goto repeat;
23004 }
23005 }
23006
23007 return valid;
23008}
23009
23010/*
23011 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23012 * "flags" can be "g" to do a global substitute.
23013 * Returns an allocated string, NULL for error.
23014 */
23015 char_u *
23016do_string_sub(str, pat, sub, flags)
23017 char_u *str;
23018 char_u *pat;
23019 char_u *sub;
23020 char_u *flags;
23021{
23022 int sublen;
23023 regmatch_T regmatch;
23024 int i;
23025 int do_all;
23026 char_u *tail;
23027 garray_T ga;
23028 char_u *ret;
23029 char_u *save_cpo;
23030
23031 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23032 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023033 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034
23035 ga_init2(&ga, 1, 200);
23036
23037 do_all = (flags[0] == 'g');
23038
23039 regmatch.rm_ic = p_ic;
23040 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23041 if (regmatch.regprog != NULL)
23042 {
23043 tail = str;
23044 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23045 {
23046 /*
23047 * Get some space for a temporary buffer to do the substitution
23048 * into. It will contain:
23049 * - The text up to where the match is.
23050 * - The substituted text.
23051 * - The text after the match.
23052 */
23053 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23054 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23055 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23056 {
23057 ga_clear(&ga);
23058 break;
23059 }
23060
23061 /* copy the text up to where the match is */
23062 i = (int)(regmatch.startp[0] - tail);
23063 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23064 /* add the substituted text */
23065 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23066 + ga.ga_len + i, TRUE, TRUE, FALSE);
23067 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 /* avoid getting stuck on a match with an empty string */
23069 if (tail == regmatch.endp[0])
23070 {
23071 if (*tail == NUL)
23072 break;
23073 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23074 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 }
23076 else
23077 {
23078 tail = regmatch.endp[0];
23079 if (*tail == NUL)
23080 break;
23081 }
23082 if (!do_all)
23083 break;
23084 }
23085
23086 if (ga.ga_data != NULL)
23087 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23088
23089 vim_free(regmatch.regprog);
23090 }
23091
23092 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23093 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023094 if (p_cpo == empty_option)
23095 p_cpo = save_cpo;
23096 else
23097 /* Darn, evaluating {sub} expression changed the value. */
23098 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099
23100 return ret;
23101}
23102
23103#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */